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