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