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 "src/default_addresses.h" 16 17 #include "src/host_manager.hpp" 18 #include "src/nemora.hpp" 19 20 #include <arpa/inet.h> 21 #include <fmt/format.h> 22 23 #include <CLI/CLI.hpp> 24 #include <phosphor-logging/log.hpp> 25 26 #include <csignal> 27 #include <cstdint> 28 #include <iostream> 29 #include <regex> 30 #include <string> 31 #include <thread> 32 #include <unordered_map> 33 #include <vector> 34 35 using fmt::format; 36 using phosphor::logging::level; 37 using phosphor::logging::log; 38 39 namespace 40 { 41 volatile std::sig_atomic_t gSignalStatus; 42 } 43 44 void signal_handler(int signal) 45 { 46 gSignalStatus = signal; 47 } 48 49 void NemoraUdpPoll(Nemora* nemora) 50 { 51 while (!gSignalStatus) 52 { 53 nemora->UdpPoll(); 54 } 55 } 56 57 int main(int argc, char* argv[]) 58 { 59 // Init arg parser 60 CLI::App app("gBMC-side Nemora implementation (POST-code only)"); 61 62 std::string udp_address_v4_str; 63 auto* ipv4_option = app.add_option( 64 "--udp4", udp_address_v4_str, 65 "Target IPv4 address for UDP communication, i.e., POST streaming.", 66 true); 67 68 std::string udp_address_v6_str; 69 auto* ipv6_option = app.add_option( 70 "--udp6", udp_address_v6_str, 71 "Target IPv6 address for UDP communication, i.e., POST streaming.", 72 true); 73 74 // interface is last, and required. 75 std::string iface_name; 76 app.add_option("interface", iface_name, 77 "Network interface for TCP communication. Ex: eth0") 78 ->required(); 79 80 CLI11_PARSE(app, argc, argv); 81 82 in_addr udp_address_v4; 83 udp_address_v4.s_addr = htonl(DEFAULT_ADDRESSES_TARGET_IP); 84 if (*ipv4_option && 85 !inet_pton(AF_INET, udp_address_v4_str.c_str(), &udp_address_v4)) 86 { 87 std::cerr << "Invalid IPv4 address supplied: " << udp_address_v4_str 88 << std::endl; 89 } 90 91 // The value from default_addresses.h is designed for LWIP which EC uses, 92 // and needs to be translated to network byte order. 93 in6_addr udp_address_v6; 94 uint32_t default_addr_6[4] = DEFAULT_ADDRESSES_TARGET_IP6; 95 96 auto htonl_inplace = [](uint32_t& i) { i = htonl(i); }; 97 std::for_each(std::begin(default_addr_6), std::end(default_addr_6), 98 htonl_inplace); 99 std::memcpy(udp_address_v6.s6_addr, default_addr_6, sizeof(default_addr_6)); 100 101 if (*ipv6_option && 102 !inet_pton(AF_INET6, udp_address_v6_str.c_str(), &udp_address_v6)) 103 { 104 std::cerr << "Invalid IPv6 address supplied: " << udp_address_v6_str 105 << std::endl; 106 } 107 108 log<level::INFO>("Start Nemora..."); 109 Nemora nemora(iface_name, udp_address_v4, udp_address_v6); 110 111 // Install a signal handler 112 std::signal(SIGINT, signal_handler); 113 114 std::thread udp(NemoraUdpPoll, &nemora); 115 116 udp.join(); 117 118 return EXIT_SUCCESS; 119 } 120