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