blob: e50e150358ab526c2af87c6fd63376bfaffc84c6 [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;
William A. Kennington III29f35bc2020-11-03 23:30:31 -080041} __attribute__((packed));
42
43ipmi_ret_t getMachineName(const uint8_t* reqBuf, uint8_t* replyBuf,
44 size_t* dataLen, HandlerInterface* handler)
45{
46 GetMachineNameRequest request;
47 if (*dataLen < sizeof(request))
48 {
49 std::fprintf(stderr, "Invalid command length: %zu\n", *dataLen);
50 return IPMI_CC_REQ_DATA_LEN_INVALID;
51 }
52 std::memcpy(&request, reqBuf, sizeof(request));
53
54 static std::optional<std::string> machineName;
55 if (!machineName)
56 {
57 try
58 {
59 machineName = handler->getMachineName();
60 }
61 catch (const IpmiException& e)
62 {
63 return e.getIpmiError();
64 }
65 }
66
67 GetMachineNameReply reply;
68 size_t len = sizeof(reply) + machineName->size();
69 if (len > MAX_IPMI_BUFFER)
70 {
71 std::fprintf(stderr, "Response would overflow response buffer\n");
72 return IPMI_CC_INVALID;
73 }
74 reply.subcommand = request.subcommand;
75 reply.machineNameLength = machineName->size();
76 std::memcpy(replyBuf, &reply, sizeof(reply));
77 std::memcpy(replyBuf + sizeof(reply), machineName->data(),
78 machineName->size());
79 (*dataLen) = len;
80 return IPMI_CC_OK;
81}
82
83} // namespace ipmi
84} // namespace google