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