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