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 | |
| 15 | #include <fmt/format.h> |
| 16 | |
| 17 | #include <sdeventplus/event.hpp> |
| 18 | #include <sdeventplus/source/io.hpp> |
| 19 | #include <stdplus/fd/create.hpp> |
| 20 | #include <stdplus/fd/ops.hpp> |
| 21 | |
| 22 | // A privileged port that is reserved for querying BMC DHCP completion. |
| 23 | // This is well known by the clients querying the status. |
| 24 | constexpr uint16_t kListenPort = 23; |
| 25 | |
| 26 | stdplus::ManagedFd createListener() |
| 27 | { |
| 28 | using namespace stdplus::fd; |
| 29 | auto sock = |
| 30 | socket(SocketDomain::INet6, SocketType::Stream, SocketProto::TCP); |
| 31 | setFileFlags(sock, getFileFlags(sock).set(stdplus::fd::FileFlag::NonBlock)); |
| 32 | sockaddr_in6 addr = {}; |
| 33 | addr.sin6_family = AF_INET6; |
| 34 | addr.sin6_port = htons(kListenPort); |
| 35 | bind(sock, addr); |
| 36 | listen(sock, 10); |
| 37 | return sock; |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | try |
| 43 | { |
| 44 | auto listener = createListener(); |
| 45 | auto event = sdeventplus::Event::get_default(); |
| 46 | sdeventplus::source::IO do_accept( |
| 47 | event, listener.get(), EPOLLIN | EPOLLET, |
| 48 | [&](sdeventplus::source::IO&, int, uint32_t) { |
| 49 | while (stdplus::fd::accept(listener)) |
| 50 | ; |
| 51 | }); |
| 52 | return event.loop(); |
| 53 | } |
| 54 | catch (const std::exception& e) |
| 55 | { |
| 56 | fmt::print(stderr, "Failed: {}\n", e.what()); |
| 57 | return 1; |
| 58 | } |
| 59 | } |