xref: /openbmc/phosphor-networkd/src/util.hpp (revision 6d217512)
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 Converts the interface name into a u-boot environment
78  *         variable that would hold its ethernet address.
79  *
80  *  @param[in] intf - interface name
81  *  @return The name of th environment key
82  */
83 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
84 
85 /** @brief read the IPv6AcceptRA value from the configuration file
86  *  @param[in] config - The parsed configuration.
87  */
88 bool getIPv6AcceptRA(const config::Parser& config);
89 
90 /** @brief read the DHCP value from the configuration file
91  *  @param[in] config - The parsed configuration.
92  */
93 struct DHCPVal
94 {
95     bool v4, v6;
96 };
97 DHCPVal getDHCPValue(const config::Parser& config);
98 
99 /** @brief Read a boolean DHCP property from a conf file
100  *  @param[in] config - The parsed configuration.
101  *  @param[in] key - The property name.
102  */
103 bool getDHCPProp(const config::Parser& config, std::string_view key);
104 
105 namespace internal
106 {
107 
108 /* @brief runs the given command in child process.
109  * @param[in] path - path of the binary file which needs to be execeuted.
110  * @param[in] args - arguments of the command.
111  */
112 void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
113 
114 /** @brief Get ignored interfaces from environment */
115 std::string_view getIgnoredInterfacesEnv();
116 
117 /** @brief Parse the comma separated interface names */
118 std::unordered_set<std::string_view>
119     parseInterfaces(std::string_view interfaces);
120 
121 /** @brief Get the ignored interfaces */
122 const std::unordered_set<std::string_view>& getIgnoredInterfaces();
123 
124 } // namespace internal
125 
126 /* @brief runs the given command in child process.
127  * @param[in] path -path of the binary file which needs to be execeuted.
128  * @param[in] tArgs - arguments of the command.
129  */
130 template <typename... ArgTypes>
131 void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
132 {
133     using expandType = char*[];
134 
135     expandType args = {const_cast<char*>(tArgs)..., nullptr};
136 
137     internal::executeCommandinChildProcess(path, args);
138 }
139 
140 } // namespace network
141 
142 } // namespace phosphor
143