blob: c8bf701ea8bfe0b43c85c9aafa329ba0a60e124f [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
William A. Kennington III29f35bc2020-11-03 23:30:31 -080014
15#include "machine_name.hpp"
16
17#include "errors.hpp"
18
19#include <cstddef>
20#include <cstdio>
21#include <cstring>
22#include <optional>
23#include <string>
24
25namespace google
26{
27namespace ipmi
28{
29
30struct GetMachineNameRequest
31{
32 uint8_t subcommand;
33} __attribute__((packed));
34
35struct GetMachineNameReply
36{
37 uint8_t subcommand;
38 uint8_t machineNameLength;
William A. Kennington III29f35bc2020-11-03 23:30:31 -080039} __attribute__((packed));
40
41ipmi_ret_t getMachineName(const uint8_t* reqBuf, uint8_t* replyBuf,
42 size_t* dataLen, HandlerInterface* handler)
43{
44 GetMachineNameRequest request;
45 if (*dataLen < sizeof(request))
46 {
47 std::fprintf(stderr, "Invalid command length: %zu\n", *dataLen);
48 return IPMI_CC_REQ_DATA_LEN_INVALID;
49 }
50 std::memcpy(&request, reqBuf, sizeof(request));
51
52 static std::optional<std::string> machineName;
53 if (!machineName)
54 {
55 try
56 {
57 machineName = handler->getMachineName();
58 }
59 catch (const IpmiException& e)
60 {
61 return e.getIpmiError();
62 }
63 }
64
65 GetMachineNameReply reply;
66 size_t len = sizeof(reply) + machineName->size();
67 if (len > MAX_IPMI_BUFFER)
68 {
69 std::fprintf(stderr, "Response would overflow response buffer\n");
70 return IPMI_CC_INVALID;
71 }
72 reply.subcommand = request.subcommand;
73 reply.machineNameLength = machineName->size();
74 std::memcpy(replyBuf, &reply, sizeof(reply));
75 std::memcpy(replyBuf + sizeof(reply), machineName->data(),
76 machineName->size());
77 (*dataLen) = len;
78 return IPMI_CC_OK;
79}
80
81} // namespace ipmi
82} // namespace google