xref: /openbmc/phosphor-networkd/src/util.hpp (revision 186099d1)
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 checks that the given ip address valid or not.
86  * @param[in] family - IP address family(AF_INET/AF_INET6).
87  * @param[in] address - IP address.
88  * @returns true if it is valid otherwise false.
89  */
90 bool isValidIP(int family, stdplus::const_zstring address) noexcept;
91 bool isValidIP(stdplus::const_zstring address) noexcept;
92 
93 /** @brief Delete the given interface.
94  *  @param[in] intf - interface name.
95  */
96 void deleteInterface(stdplus::const_zstring intf);
97 
98 /** @brief Converts the interface name into a u-boot environment
99  *         variable that would hold its ethernet address.
100  *
101  *  @param[in] intf - interface name
102  *  @return The name of th environment key
103  */
104 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
105 
106 /** @brief read the IPv6AcceptRA value from the configuration file
107  *  @param[in] config - The parsed configuration.
108  */
109 bool getIPv6AcceptRA(const config::Parser& config);
110 
111 /** @brief read the DHCP value from the configuration file
112  *  @param[in] config - The parsed configuration.
113  */
114 struct DHCPVal
115 {
116     bool v4, v6;
117 };
118 DHCPVal getDHCPValue(const config::Parser& config);
119 
120 /** @brief Read a boolean DHCP property from a conf file
121  *  @param[in] config - The parsed configuration.
122  *  @param[in] key - The property name.
123  */
124 bool getDHCPProp(const config::Parser& config, std::string_view key);
125 
126 namespace internal
127 {
128 
129 /* @brief runs the given command in child process.
130  * @param[in] path - path of the binary file which needs to be execeuted.
131  * @param[in] args - arguments of the command.
132  */
133 void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
134 
135 /** @brief Get ignored interfaces from environment */
136 std::string_view getIgnoredInterfacesEnv();
137 
138 /** @brief Parse the comma separated interface names */
139 std::unordered_set<std::string_view>
140     parseInterfaces(std::string_view interfaces);
141 
142 /** @brief Get the ignored interfaces */
143 const std::unordered_set<std::string_view>& getIgnoredInterfaces();
144 
145 } // namespace internal
146 
147 /* @brief runs the given command in child process.
148  * @param[in] path -path of the binary file which needs to be execeuted.
149  * @param[in] tArgs - arguments of the command.
150  */
151 template <typename... ArgTypes>
152 void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
153 {
154     using expandType = char*[];
155 
156     expandType args = {const_cast<char*>(tArgs)..., nullptr};
157 
158     internal::executeCommandinChildProcess(path, args);
159 }
160 
161 } // namespace network
162 
163 } // namespace phosphor
164