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