William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 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 | |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 15 | #include <sdeventplus/event.hpp> |
| 16 | #include <sdeventplus/source/io.hpp> |
| 17 | #include <stdplus/fd/create.hpp> |
| 18 | #include <stdplus/fd/ops.hpp> |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 19 | #include <stdplus/print.hpp> |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 20 | |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 21 | using namespace std::string_view_literals; |
| 22 | |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 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; |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 26 | enum : uint8_t |
| 27 | { |
| 28 | DONE = 0, |
| 29 | POWERCYCLE = 1, |
| 30 | }; |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 31 | |
| 32 | stdplus::ManagedFd createListener() |
| 33 | { |
| 34 | using namespace stdplus::fd; |
Patrick Williams | 2be4523 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 35 | auto sock = socket(SocketDomain::INet6, SocketType::Stream, |
| 36 | SocketProto::TCP); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 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 | |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 46 | int main(int argc, char* argv[]) |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 47 | { |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 48 | if (argc != 2) |
| 49 | { |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 50 | stdplus::println(stderr, "Invalid parameter count"); |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 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 | { |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 66 | stdplus::println(stderr, "Invalid parameter"); |
Yuxiao Zhang | 5ca20ff | 2023-03-30 14:54:11 -0700 | [diff] [blame] | 67 | return 1; |
| 68 | } |
| 69 | |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 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) { |
Patrick Williams | 2be4523 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 77 | while (auto fd = stdplus::fd::accept(listener)) |
| 78 | { |
| 79 | stdplus::fd::sendExact(*fd, data, stdplus::fd::SendFlags(0)); |
| 80 | } |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 81 | }); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 82 | return event.loop(); |
| 83 | } |
| 84 | catch (const std::exception& e) |
| 85 | { |
Willy Tu | 253e646 | 2023-12-28 09:57:27 -0800 | [diff] [blame] | 86 | stdplus::println(stderr, "Failed: {}", e.what()); |
William A. Kennington III | dffd652 | 2022-02-08 01:40:43 -0800 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | } |