blob: d6c5c96fe0dfbf7f466e3bb756548602c564ae75 [file] [log] [blame]
William A. Kennington IIIdffd6522022-02-08 01:40:43 -08001// 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
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080015#include <sdeventplus/event.hpp>
16#include <sdeventplus/source/io.hpp>
17#include <stdplus/fd/create.hpp>
18#include <stdplus/fd/ops.hpp>
Willy Tu253e6462023-12-28 09:57:27 -080019#include <stdplus/print.hpp>
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080020
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070021using namespace std::string_view_literals;
22
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080023// A privileged port that is reserved for querying BMC DHCP completion.
24// This is well known by the clients querying the status.
25constexpr uint16_t kListenPort = 23;
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070026enum : uint8_t
27{
28 DONE = 0,
29 POWERCYCLE = 1,
30};
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080031
32stdplus::ManagedFd createListener()
33{
34 using namespace stdplus::fd;
Patrick Williams2be45232023-05-10 07:51:22 -050035 auto sock = socket(SocketDomain::INet6, SocketType::Stream,
36 SocketProto::TCP);
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080037 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
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070046int main(int argc, char* argv[])
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080047{
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070048 if (argc != 2)
49 {
Willy Tu253e6462023-12-28 09:57:27 -080050 stdplus::println(stderr, "Invalid parameter count");
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070051 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 {
Willy Tu253e6462023-12-28 09:57:27 -080066 stdplus::println(stderr, "Invalid parameter");
Yuxiao Zhang5ca20ff2023-03-30 14:54:11 -070067 return 1;
68 }
69
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080070 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) {
Patrick Williams2be45232023-05-10 07:51:22 -050077 while (auto fd = stdplus::fd::accept(listener))
78 {
79 stdplus::fd::sendExact(*fd, data, stdplus::fd::SendFlags(0));
80 }
Patrick Williams60849572023-10-20 11:19:52 -050081 });
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080082 return event.loop();
83 }
84 catch (const std::exception& e)
85 {
Willy Tu253e6462023-12-28 09:57:27 -080086 stdplus::println(stderr, "Failed: {}", e.what());
William A. Kennington IIIdffd6522022-02-08 01:40:43 -080087 return 1;
88 }
89}