xref: /openbmc/phosphor-networkd/src/util.hpp (revision c9900079)
1 #pragma once
2 #include "types.hpp"
3 
4 #include <stdplus/raw.hpp>
5 #include <stdplus/zstring_view.hpp>
6 
7 #include <optional>
8 #include <string>
9 #include <string_view>
10 #include <unordered_set>
11 
12 namespace phosphor
13 {
14 namespace network
15 {
16 namespace config
17 {
18 class Parser;
19 }
20 
21 /* @brief converts a sockaddr for the specified address family into
22  *        a type_safe InAddrAny.
23  * @param[in] family - The address family of the buf
24  * @param[in] buf - The network byte order address
25  */
26 constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf)
27 {
28     switch (family)
29     {
30         case AF_INET:
31             return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf);
32         case AF_INET6:
33             return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf);
34     }
35     throw std::invalid_argument("Unrecognized family");
36 }
37 
38 /** @brief Converts the interface name into a u-boot environment
39  *         variable that would hold its ethernet address.
40  *
41  *  @param[in] intf - interface name
42  *  @return The name of th environment key
43  */
44 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
45 
46 /** @brief read the IPv6AcceptRA value from the configuration file
47  *  @param[in] config - The parsed configuration.
48  */
49 bool getIPv6AcceptRA(const config::Parser& config);
50 
51 /** @brief read the DHCP value from the configuration file
52  *  @param[in] config - The parsed configuration.
53  */
54 struct DHCPVal
55 {
56     bool v4, v6;
57 };
58 DHCPVal getDHCPValue(const config::Parser& config);
59 
60 /** @brief Read a boolean DHCP property from a conf file
61  *  @param[in] config - The parsed configuration.
62  *  @param[in] key - The property name.
63  */
64 bool getDHCPProp(const config::Parser& config, std::string_view key);
65 
66 namespace internal
67 {
68 
69 /* @brief runs the given command in child process.
70  * @param[in] path - path of the binary file which needs to be execeuted.
71  * @param[in] args - arguments of the command.
72  */
73 void executeCommandinChildProcess(stdplus::zstring_view path, char** args);
74 
75 /** @brief Get ignored interfaces from environment */
76 std::string_view getIgnoredInterfacesEnv();
77 
78 /** @brief Parse the comma separated interface names */
79 std::unordered_set<std::string_view>
80     parseInterfaces(std::string_view interfaces);
81 
82 /** @brief Get the ignored interfaces */
83 const std::unordered_set<std::string_view>& getIgnoredInterfaces();
84 
85 } // namespace internal
86 
87 /* @brief runs the given command in child process.
88  * @param[in] path -path of the binary file which needs to be execeuted.
89  * @param[in] tArgs - arguments of the command.
90  */
91 template <typename... ArgTypes>
92 void execute(stdplus::zstring_view path, ArgTypes&&... tArgs)
93 {
94     using expandType = char*[];
95 
96     expandType args = {const_cast<char*>(tArgs)..., nullptr};
97 
98     internal::executeCommandinChildProcess(path, args);
99 }
100 
101 } // namespace network
102 
103 } // namespace phosphor
104