xref: /openbmc/phosphor-networkd/src/util.hpp (revision a520a39d)
1 #pragma once
2 
3 #include "config.h"
4 
5 #include "ethernet_interface.hpp"
6 #include "types.hpp"
7 
8 #include <netinet/ether.h>
9 #include <unistd.h>
10 
11 #include <cstring>
12 #include <filesystem>
13 #include <optional>
14 #include <sdbusplus/bus.hpp>
15 #include <string>
16 #include <string_view>
17 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
18 
19 namespace phosphor
20 {
21 namespace network
22 {
23 
24 using EthernetInterfaceIntf =
25     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
26 
27 constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
28 constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
29 constexpr auto IPV6_MAX_PREFIX_LENGTH = 128;
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 Converts the given mac address into byte form
41  *  @param[in] str - The mac address in human readable form
42  *  @returns A mac address in network byte order
43  *  @throws std::runtime_error for bad mac
44  */
45 ether_addr fromString(const char* str);
46 inline ether_addr fromString(const std::string& str)
47 {
48     return fromString(str.c_str());
49 }
50 
51 /** @brief Converts the given mac address bytes into a string
52  *  @param[in] mac - The mac address
53  *  @returns A valid mac address string
54  */
55 std::string toString(const ether_addr& mac);
56 
57 /** @brief Determines if the mac address is empty
58  *  @param[in] mac - The mac address
59  *  @return True if 00:00:00:00:00:00
60  */
61 bool isEmpty(const ether_addr& mac);
62 
63 /** @brief Determines if the mac address is a multicast address
64  *  @param[in] mac - The mac address
65  *  @return True if multicast bit is set
66  */
67 bool isMulticast(const ether_addr& mac);
68 
69 /** @brief Determines if the mac address is a unicast address
70  *  @param[in] mac - The mac address
71  *  @return True if not multicast or empty
72  */
73 bool isUnicast(const ether_addr& mac);
74 
75 } // namespace mac_address
76 
77 constexpr auto networkdService = "systemd-networkd.service";
78 constexpr auto timeSynchdService = "systemd-timesyncd.service";
79 
80 /* @brief converts a sockaddr for the specified address family into
81  *        a type_safe InAddrAny.
82  * @param[in] addressFamily - The address family of the buf
83  * @param[in] buf - The network byte order address
84  */
85 InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
86 
87 /* @brief converts the ip bytes into a string representation
88  * @param[in] addr - input ip address to convert.
89  * @returns String representation of the ip.
90  */
91 std::string toString(const InAddrAny& addr);
92 std::string toString(const struct in_addr& addr);
93 std::string toString(const struct in6_addr& addr);
94 
95 /* @brief checks that the given ip address valid or not.
96  * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
97  * @param[in] address - IP address.
98  * @returns true if it is valid otherwise false.
99  */
100 bool isValidIP(int addressFamily, const std::string& address);
101 
102 /* @brief checks that the given prefix is valid or not.
103  * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
104  * @param[in] prefix - prefix length.
105  * @returns true if it is valid otherwise false.
106  */
107 bool isValidPrefix(int addressFamily, uint8_t prefixLength);
108 
109 /** @brief Get all the interfaces from the system.
110  *  @returns list of interface names.
111  */
112 InterfaceList getInterfaces();
113 
114 /** @brief Delete the given interface.
115  *  @param[in] intf - interface name.
116  */
117 void deleteInterface(const std::string& intf);
118 
119 /** @brief Converts the interface name into a u-boot environment
120  *         variable that would hold its ethernet address.
121  *
122  *  @param[in] intf - interface name
123  *  @return The name of th environment key
124  */
125 std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
126 
127 /** @brief read the IPv6AcceptRA value from the configuration file
128  *  @param[in] config - The parsed configuration.
129  */
130 bool getIPv6AcceptRA(const config::Parser& config);
131 
132 /** @brief read the DHCP value from the configuration file
133  *  @param[in] config - The parsed configuration.
134  */
135 EthernetInterfaceIntf::DHCPConf getDHCPValue(const config::Parser& config);
136 
137 namespace internal
138 {
139 
140 /* @brief runs the given command in child process.
141  * @param[in] path - path of the binary file which needs to be execeuted.
142  * @param[in] args - arguments of the command.
143  */
144 void executeCommandinChildProcess(const char* path, char** args);
145 
146 /** @brief Get ignored interfaces from environment */
147 std::string_view getIgnoredInterfacesEnv();
148 
149 /** @brief Parse the comma separated interface names */
150 std::set<std::string_view> parseInterfaces(std::string_view interfaces);
151 
152 /** @brief Get the ignored interfaces */
153 const std::set<std::string_view>& getIgnoredInterfaces();
154 
155 } // namespace internal
156 
157 /* @brief runs the given command in child process.
158  * @param[in] path -path of the binary file which needs to be execeuted.
159  * @param[in] tArgs - arguments of the command.
160  */
161 template <typename... ArgTypes>
162 void execute(const char* path, ArgTypes&&... tArgs)
163 {
164     using expandType = char*[];
165 
166     expandType args = {const_cast<char*>(tArgs)..., nullptr};
167 
168     internal::executeCommandinChildProcess(path, args);
169 }
170 
171 } // namespace network
172 
173 } // namespace phosphor
174