1 // Copyright 2022 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 <sdeventplus/event.hpp> 16 #include <sdeventplus/source/io.hpp> 17 #include <stdplus/fd/create.hpp> 18 #include <stdplus/fd/ops.hpp> 19 #include <stdplus/print.hpp> 20 21 using namespace std::string_view_literals; 22 23 // A privileged port that is reserved for querying BMC DHCP completion. 24 // This is well known by the clients querying the status. 25 constexpr uint16_t kListenPort = 23; 26 enum : uint8_t 27 { 28 DONE = 0, 29 POWERCYCLE = 1, 30 }; 31 32 stdplus::ManagedFd createListener() 33 { 34 using namespace stdplus::fd; 35 auto sock = socket(SocketDomain::INet6, SocketType::Stream, 36 SocketProto::TCP); 37 setFileFlags(sock, getFileFlags(sock).set(stdplus::fd::FileFlag::NonBlock)); 38 sockaddr_in6 addr = {}; 39 addr.sin6_family = AF_INET6; 40 addr.sin6_port = htons(kListenPort); 41 bind(sock, addr); 42 listen(sock, 10); 43 return sock; 44 } 45 46 int main(int argc, char* argv[]) 47 { 48 if (argc != 2) 49 { 50 stdplus::println(stderr, "Invalid parameter count"); 51 return 1; 52 } 53 54 std::vector<uint8_t> data; 55 56 if (argv[1] == "POWERCYCLE"sv) 57 { 58 data.push_back(POWERCYCLE); 59 } 60 else if (argv[1] == "DONE"sv) 61 { 62 data.push_back(DONE); 63 } 64 else 65 { 66 stdplus::println(stderr, "Invalid parameter"); 67 return 1; 68 } 69 70 try 71 { 72 auto listener = createListener(); 73 auto event = sdeventplus::Event::get_default(); 74 sdeventplus::source::IO do_accept( 75 event, listener.get(), EPOLLIN | EPOLLET, 76 [&](sdeventplus::source::IO&, int, uint32_t) { 77 while (auto fd = stdplus::fd::accept(listener)) 78 { 79 stdplus::fd::sendExact(*fd, data, stdplus::fd::SendFlags(0)); 80 } 81 }); 82 return event.loop(); 83 } 84 catch (const std::exception& e) 85 { 86 stdplus::println(stderr, "Failed: {}", e.what()); 87 return 1; 88 } 89 } 90