Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 3 | #include "config.h" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 5 | #include "types.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 7 | #include <netinet/ether.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | |
William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 10 | #include <cstring> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 12 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 13 | #include <string_view> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 19 | |
| 20 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 21 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
| 22 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 64; |
| 23 | constexpr auto IPV4_PREFIX = "169.254"; |
| 24 | constexpr auto IPV6_PREFIX = "fe80"; |
| 25 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 26 | namespace mac_address |
| 27 | { |
| 28 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 29 | /** @brief gets the MAC address from the Inventory. |
| 30 | * @param[in] bus - DBUS Bus Object. |
| 31 | */ |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 32 | ether_addr getfromInventory(sdbusplus::bus::bus& bus); |
| 33 | |
| 34 | /** @brief Converts the given mac address into byte form |
| 35 | * @param[in] str - The mac address in human readable form |
| 36 | * @returns A mac address in network byte order |
| 37 | * @throws std::runtime_error for bad mac |
| 38 | */ |
| 39 | ether_addr fromString(const char* str); |
| 40 | inline ether_addr fromString(const std::string& str) |
| 41 | { |
| 42 | return fromString(str.c_str()); |
| 43 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 44 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 45 | /** @brief Converts the given mac address bytes into a string |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 46 | * @param[in] mac - The mac address |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 47 | * @returns A valid mac address string |
| 48 | */ |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 49 | std::string toString(const ether_addr& mac); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 50 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 51 | /** @brief Determines if the mac address is empty |
| 52 | * @param[in] mac - The mac address |
| 53 | * @return True if 00:00:00:00:00:00 |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 54 | */ |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 55 | bool isEmpty(const ether_addr& mac); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 56 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 57 | /** @brief Determines if the mac address is a multicast address |
| 58 | * @param[in] mac - The mac address |
| 59 | * @return True if multicast bit is set |
| 60 | */ |
| 61 | bool isMulticast(const ether_addr& mac); |
| 62 | |
| 63 | /** @brief Determines if the mac address is a unicast address |
| 64 | * @param[in] mac - The mac address |
| 65 | * @return True if not multicast or empty |
| 66 | */ |
| 67 | bool isUnicast(const ether_addr& mac); |
| 68 | |
| 69 | /** @brief Determines if the mac address is locally managed |
| 70 | * @param[in] mac - The mac address |
| 71 | * @return True if local admin bit is set |
| 72 | */ |
| 73 | bool isLocalAdmin(const ether_addr& mac); |
| 74 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 75 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 76 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 77 | constexpr auto networkdService = "systemd-networkd.service"; |
| 78 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 79 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 80 | /* @brief converts the given subnet into prefix notation. |
| 81 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 82 | * @param[in] mask - Subnet Mask. |
| 83 | * @returns prefix. |
| 84 | */ |
| 85 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 86 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 87 | /* @brief converts a sockaddr for the specified address family into |
| 88 | * a type_safe InAddrAny. |
| 89 | * @param[in] addressFamily - The address family of the buf |
| 90 | * @param[in] buf - The network byte order address |
| 91 | */ |
| 92 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 93 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 94 | /* @brief converts the ip bytes into a string representation |
| 95 | * @param[in] addr - input ip address to convert. |
| 96 | * @returns String representation of the ip. |
| 97 | */ |
| 98 | std::string toString(const InAddrAny& addr); |
| 99 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 100 | /* @brief converts the prefix into subnetmask. |
| 101 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 102 | * @param[in] prefix - prefix length. |
| 103 | * @returns subnet mask. |
| 104 | */ |
| 105 | std::string toMask(int addressFamily, uint8_t prefix); |
| 106 | |
| 107 | /* @brief checks that the given ip address is link local or not. |
| 108 | * @param[in] address - IP address. |
| 109 | * @returns true if it is linklocal otherwise false. |
| 110 | */ |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 111 | bool isLinkLocalIP(const std::string& address); |
| 112 | |
| 113 | /* @brief checks that the given ip address valid or not. |
| 114 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 115 | * @param[in] address - IP address. |
| 116 | * @returns true if it is valid otherwise false. |
| 117 | */ |
| 118 | bool isValidIP(int addressFamily, const std::string& address); |
| 119 | |
| 120 | /* @brief checks that the given prefix is valid or not. |
| 121 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 122 | * @param[in] prefix - prefix length. |
| 123 | * @returns true if it is valid otherwise false. |
| 124 | */ |
| 125 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 126 | |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 127 | /* @brief gets the network section of the ip address. |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 128 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 129 | * @param[in] ipaddress - IP address. |
| 130 | * @param[in] prefix - prefix length. |
| 131 | * @returns network section of the ipaddress. |
| 132 | */ |
| 133 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
| 134 | uint8_t prefix); |
| 135 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 136 | /** @brief Gets the map of interface and the associated |
| 137 | * address. |
| 138 | * @returns map of interface and the address. |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 139 | */ |
| 140 | IntfAddrMap getInterfaceAddrs(); |
| 141 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 142 | /** @brief Get all the interfaces from the system. |
| 143 | * @returns list of interface names. |
| 144 | */ |
| 145 | InterfaceList getInterfaces(); |
| 146 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 147 | /** @brief Delete the given interface. |
| 148 | * @param[in] intf - interface name. |
| 149 | */ |
| 150 | void deleteInterface(const std::string& intf); |
| 151 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 152 | /** @brief read the DHCP value from the configuration file |
| 153 | * @param[in] confDir - Network configuration directory. |
| 154 | * @param[in] intf - Interface name. |
| 155 | */ |
| 156 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 157 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 158 | namespace internal |
| 159 | { |
| 160 | |
| 161 | /* @brief runs the given command in child process. |
| 162 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 163 | * @param[in] args - arguments of the command. |
| 164 | */ |
| 165 | void executeCommandinChildProcess(const char* path, char** args); |
| 166 | |
| 167 | } // namespace internal |
| 168 | |
| 169 | /* @brief runs the given command in child process. |
| 170 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 171 | * @param[in] tArgs - arguments of the command. |
| 172 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 173 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 174 | void execute(const char* path, ArgTypes&&... tArgs) |
| 175 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 176 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 177 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 178 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 179 | |
| 180 | internal::executeCommandinChildProcess(path, args); |
| 181 | } |
| 182 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 183 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 184 | |
William A. Kennington III | c920bdb | 2019-04-19 14:23:06 -0700 | [diff] [blame] | 185 | /** @brief Copies data from a buffer into a copyable type |
| 186 | * |
| 187 | * @param[in] data - The data buffer being extracted from |
| 188 | * @param[in] emsg - The message to print if extraction fails |
| 189 | * @return The copyable type with data populated |
| 190 | */ |
| 191 | template <typename T> |
| 192 | T copyFrom(std::string_view data, const char* emsg = "Extract Failed") |
| 193 | { |
| 194 | static_assert(std::is_trivially_copyable_v<T>); |
| 195 | T ret; |
| 196 | if (data.size() < sizeof(ret)) |
| 197 | { |
| 198 | throw std::runtime_error(emsg); |
| 199 | } |
| 200 | std::memcpy(&ret, data.data(), sizeof(ret)); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /** @brief Extracts data from a buffer into a copyable type |
| 205 | * Updates the data buffer to show that data was removed |
| 206 | * |
| 207 | * @param[in,out] data - The data buffer being extracted from |
| 208 | * @param[in] emsg - The message to print if extraction fails |
| 209 | * @return The copyable type with data populated |
| 210 | */ |
| 211 | template <typename T> |
| 212 | T extract(std::string_view& data, const char* emsg = "Extract Failed") |
| 213 | { |
| 214 | T ret = copyFrom<T>(data, emsg); |
| 215 | data.remove_prefix(sizeof(ret)); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | /** @brief Compares two of the same trivially copyable types |
| 220 | * |
| 221 | * @param[in] a - The data buffer being extracted from |
| 222 | * @param[in] b - The message to print if extraction fails |
| 223 | * @return True if the parameters are bitwise identical |
| 224 | */ |
| 225 | template <typename T> |
| 226 | bool equal(const T& a, const T& b) |
| 227 | { |
| 228 | static_assert(std::is_trivially_copyable_v<T>); |
| 229 | return memcmp(&a, &b, sizeof(T)) == 0; |
| 230 | } |
| 231 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 232 | class Descriptor |
| 233 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 234 | private: |
| 235 | /** default value */ |
| 236 | int fd = -1; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 237 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 238 | public: |
| 239 | Descriptor() = default; |
| 240 | Descriptor(const Descriptor&) = delete; |
| 241 | Descriptor& operator=(const Descriptor&) = delete; |
| 242 | Descriptor(Descriptor&&) = delete; |
| 243 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 244 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 245 | explicit Descriptor(int fd) : fd(fd) |
| 246 | { |
| 247 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 248 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 249 | /* @brief sets the internal file descriptor with the given descriptor |
| 250 | * and closes the old descriptor. |
| 251 | * @param[in] descriptor - File/Socket descriptor. |
| 252 | */ |
| 253 | void set(int descriptor) |
| 254 | { |
| 255 | // same descriptor given |
| 256 | if (fd == descriptor) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 257 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 258 | return; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 261 | // close the old descriptor |
| 262 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 263 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 264 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 267 | fd = descriptor; |
| 268 | } |
| 269 | |
| 270 | ~Descriptor() |
| 271 | { |
| 272 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 273 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 274 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 275 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | int operator()() const |
| 279 | { |
| 280 | return fd; |
| 281 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 282 | }; |
| 283 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 284 | } // namespace phosphor |