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