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