xref: /openbmc/phosphor-networkd/src/util.hpp (revision ad205028)
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 
59 enum class DHCPType
60 {
61     v4,
62     v6
63 };
64 
65 DHCPVal getDHCPValue(const config::Parser& config);
66 
67 /** @brief Read a boolean DHCP property from a conf file
68  *  @param[in] config - The parsed configuration.
69  *  @param[in] nwType - The network type.
70  *  @param[in] key - The property name.
71  */
72 bool getDHCPProp(const config::Parser& config, DHCPType dhcpType,
73                  std::string_view key);
74 
75 namespace internal
76 {
77 
78 /* @brief runs the given command in child process.
79  * @param[in] path - path of the binary file which needs to be execeuted.
80  * @param[in] args - arguments of the command.
81  */
82 void executeCommandinChildProcess(stdplus::zstring_view path, char** args);
83 
84 /** @brief Get ignored interfaces from environment */
85 std::string_view getIgnoredInterfacesEnv();
86 
87 /** @brief Parse the comma separated interface names */
88 std::unordered_set<std::string_view>
89     parseInterfaces(std::string_view interfaces);
90 
91 /** @brief Get the ignored interfaces */
92 const std::unordered_set<std::string_view>& getIgnoredInterfaces();
93 
94 } // namespace internal
95 
96 /* @brief runs the given command in child process.
97  * @param[in] path -path of the binary file which needs to be execeuted.
98  * @param[in] tArgs - arguments of the command.
99  */
100 template <typename... ArgTypes>
101 void execute(stdplus::zstring_view path, ArgTypes&&... tArgs)
102 {
103     using expandType = char*[];
104 
105     expandType args = {const_cast<char*>(tArgs)..., nullptr};
106 
107     internal::executeCommandinChildProcess(path, args);
108 }
109 
110 } // namespace network
111 
112 } // namespace phosphor
113