1 // 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. 14 15 #include "eth.hpp" 16 17 #include "commands.hpp" 18 #include "handler.hpp" 19 20 #include <cstdint> 21 #include <cstring> 22 #include <ipmid/api-types.hpp> 23 #include <string> 24 #include <tuple> 25 #include <vector> 26 27 namespace google 28 { 29 namespace ipmi 30 { 31 32 // TOOD(venture): The ipmid.h has this macro, which is a header we 33 // can't normally access. 34 #ifndef MAX_IPMI_BUFFER 35 #define MAX_IPMI_BUFFER 64 36 #endif 37 38 Resp getEthDevice(const std::vector<std::uint8_t>& data, 39 const HandlerInterface* handler) 40 { 41 std::tuple<std::uint8_t, std::string> details = 42 handler->getEthDetails(std::string(data.begin(), data.end())); 43 44 std::string device = std::get<1>(details); 45 if (device.length() == 0) 46 { 47 std::fprintf(stderr, "Invalid eth string\n"); 48 return ::ipmi::responseReqDataLenInvalid(); 49 } 50 51 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER) 52 { 53 std::fprintf(stderr, "Response would overflow response buffer\n"); 54 return ::ipmi::responseRetBytesUnavailable(); 55 } 56 57 std::vector<std::uint8_t> reply; 58 reply.reserve(device.length() + sizeof(struct EthDeviceReply)); 59 reply.emplace_back(std::get<0>(details)); /* channel */ 60 reply.emplace_back(device.length()); /* ifNameLength */ 61 reply.insert(reply.end(), device.begin(), device.end()); /* name */ 62 63 return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply); 64 } 65 66 } // namespace ipmi 67 } // namespace google 68