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