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 | |
| 7 | #include <unistd.h> |
| 8 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 9 | #include <regex> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 11 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 12 | #include <string_view> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 18 | |
| 19 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 20 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
| 21 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 64; |
| 22 | constexpr auto IPV4_PREFIX = "169.254"; |
| 23 | constexpr auto IPV6_PREFIX = "fe80"; |
Nagaraju Goruganti | 98fc588 | 2017-10-31 04:40:12 -0500 | [diff] [blame] | 24 | constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 25 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 26 | namespace mac_address |
| 27 | { |
| 28 | |
| 29 | constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"; |
| 30 | constexpr auto localAdminMask = 0x020000000000; |
| 31 | constexpr auto broadcastMac = 0xFFFFFFFFFFFF; |
| 32 | |
Nagaraju Goruganti | 5993763 | 2017-12-05 00:06:07 -0600 | [diff] [blame] | 33 | constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 34 | constexpr size_t size = 18; |
| 35 | |
| 36 | /** @brief validate the mac address |
| 37 | * @param[in] value - MAC address. |
| 38 | * @returns true if validate otherwise false. |
| 39 | */ |
| 40 | inline bool validate(const std::string& value) |
| 41 | { |
| 42 | std::regex regexToCheck(regex); |
Nagaraju Goruganti | 98fc588 | 2017-10-31 04:40:12 -0500 | [diff] [blame] | 43 | return std::regex_search(value, regexToCheck) && |
| 44 | value.find(ZEROMACADDRESS) != 0; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** @brief gets the MAC address from the Inventory. |
| 48 | * @param[in] bus - DBUS Bus Object. |
| 49 | */ |
| 50 | std::string getfromInventory(sdbusplus::bus::bus& bus); |
| 51 | |
William A. Kennington III | a14879e | 2019-02-01 21:43:11 -0800 | [diff] [blame] | 52 | /* @brief Marshalls the bytes for a mac address into a MacAddr. |
| 53 | * @param[in] buf - The network byte order address |
| 54 | */ |
| 55 | MacAddr fromBuf(std::string_view buf); |
| 56 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 57 | /** @brief Converts the given mac address bytes into a string |
| 58 | * @param[in] bytes - The mac address |
| 59 | * @returns A valid mac address string |
| 60 | */ |
| 61 | std::string toString(const MacAddr& mac); |
| 62 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 63 | namespace internal |
| 64 | { |
| 65 | /** @brief Converts the given mac address into unsigned 64 bit integer |
| 66 | * @param[in] value - MAC address. |
| 67 | * @returns converted unsigned 64 bit number. |
| 68 | */ |
| 69 | inline uint64_t convertToInt(const std::string& value) |
| 70 | { |
| 71 | unsigned char mac[6]; |
| 72 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1, |
| 74 | mac + 2, mac + 3, mac + 4, mac + 5); |
| 75 | return static_cast<uint64_t>(mac[0]) << 40 | |
| 76 | static_cast<uint64_t>(mac[1]) << 32 | |
| 77 | static_cast<uint64_t>(mac[2]) << 24 | |
| 78 | static_cast<uint64_t>(mac[3]) << 16 | |
| 79 | static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 80 | } |
| 81 | |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 82 | /** @brief Converts the lower nibble of a byte value to a hex digit |
| 83 | */ |
| 84 | inline char toHex(std::byte byte) |
| 85 | { |
| 86 | uint8_t val = std::to_integer<uint8_t>(byte) & 0xf; |
| 87 | return val < 10 ? '0' + val : 'A' + (val - 10); |
| 88 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 89 | } // namespace internal |
| 90 | } // namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 91 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 92 | constexpr auto networkdService = "systemd-networkd.service"; |
| 93 | constexpr auto timeSynchdService = "systemd-timesyncd.service"; |
| 94 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 95 | /* @brief converts the given subnet into prefix notation. |
| 96 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 97 | * @param[in] mask - Subnet Mask. |
| 98 | * @returns prefix. |
| 99 | */ |
| 100 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 101 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 102 | /* @brief converts a sockaddr for the specified address family into |
| 103 | * a type_safe InAddrAny. |
| 104 | * @param[in] addressFamily - The address family of the buf |
| 105 | * @param[in] buf - The network byte order address |
| 106 | */ |
| 107 | InAddrAny addrFromBuf(int addressFamily, std::string_view buf); |
| 108 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 109 | /* @brief converts the ip bytes into a string representation |
| 110 | * @param[in] addr - input ip address to convert. |
| 111 | * @returns String representation of the ip. |
| 112 | */ |
| 113 | std::string toString(const InAddrAny& addr); |
| 114 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 115 | /* @brief converts the prefix into subnetmask. |
| 116 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 117 | * @param[in] prefix - prefix length. |
| 118 | * @returns subnet mask. |
| 119 | */ |
| 120 | std::string toMask(int addressFamily, uint8_t prefix); |
| 121 | |
| 122 | /* @brief checks that the given ip address is link local or not. |
| 123 | * @param[in] address - IP address. |
| 124 | * @returns true if it is linklocal otherwise false. |
| 125 | */ |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 126 | bool isLinkLocalIP(const std::string& address); |
| 127 | |
| 128 | /* @brief checks that the given ip address valid or not. |
| 129 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 130 | * @param[in] address - IP address. |
| 131 | * @returns true if it is valid otherwise false. |
| 132 | */ |
| 133 | bool isValidIP(int addressFamily, const std::string& address); |
| 134 | |
| 135 | /* @brief checks that the given prefix is valid or not. |
| 136 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 137 | * @param[in] prefix - prefix length. |
| 138 | * @returns true if it is valid otherwise false. |
| 139 | */ |
| 140 | bool isValidPrefix(int addressFamily, uint8_t prefixLength); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 141 | |
Gunnar Mills | d75f049 | 2017-10-25 20:33:32 -0500 | [diff] [blame] | 142 | /* @brief gets the network section of the ip address. |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 143 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 144 | * @param[in] ipaddress - IP address. |
| 145 | * @param[in] prefix - prefix length. |
| 146 | * @returns network section of the ipaddress. |
| 147 | */ |
| 148 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
| 149 | uint8_t prefix); |
| 150 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 151 | /** @brief Gets the map of interface and the associated |
| 152 | * address. |
| 153 | * @returns map of interface and the address. |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 154 | */ |
| 155 | IntfAddrMap getInterfaceAddrs(); |
| 156 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 157 | /** @brief Get all the interfaces from the system. |
| 158 | * @returns list of interface names. |
| 159 | */ |
| 160 | InterfaceList getInterfaces(); |
| 161 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 162 | /** @brief Delete the given interface. |
| 163 | * @param[in] intf - interface name. |
| 164 | */ |
| 165 | void deleteInterface(const std::string& intf); |
| 166 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 167 | /** @brief read the DHCP value from the configuration file |
| 168 | * @param[in] confDir - Network configuration directory. |
| 169 | * @param[in] intf - Interface name. |
| 170 | */ |
| 171 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 172 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 173 | namespace internal |
| 174 | { |
| 175 | |
| 176 | /* @brief runs the given command in child process. |
| 177 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 178 | * @param[in] args - arguments of the command. |
| 179 | */ |
| 180 | void executeCommandinChildProcess(const char* path, char** args); |
| 181 | |
| 182 | } // namespace internal |
| 183 | |
| 184 | /* @brief runs the given command in child process. |
| 185 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 186 | * @param[in] tArgs - arguments of the command. |
| 187 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 188 | template <typename... ArgTypes> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 189 | void execute(const char* path, ArgTypes&&... tArgs) |
| 190 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 191 | using expandType = char* []; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 192 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 193 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 194 | |
| 195 | internal::executeCommandinChildProcess(path, args); |
| 196 | } |
| 197 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 198 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 199 | |
| 200 | class Descriptor |
| 201 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 202 | private: |
| 203 | /** default value */ |
| 204 | int fd = -1; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 205 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 206 | public: |
| 207 | Descriptor() = default; |
| 208 | Descriptor(const Descriptor&) = delete; |
| 209 | Descriptor& operator=(const Descriptor&) = delete; |
| 210 | Descriptor(Descriptor&&) = delete; |
| 211 | Descriptor& operator=(Descriptor&&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 212 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 213 | explicit Descriptor(int fd) : fd(fd) |
| 214 | { |
| 215 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 216 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 217 | /* @brief sets the internal file descriptor with the given descriptor |
| 218 | * and closes the old descriptor. |
| 219 | * @param[in] descriptor - File/Socket descriptor. |
| 220 | */ |
| 221 | void set(int descriptor) |
| 222 | { |
| 223 | // same descriptor given |
| 224 | if (fd == descriptor) |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 225 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 226 | return; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 227 | } |
| 228 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 229 | // close the old descriptor |
| 230 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 231 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 232 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 233 | } |
| 234 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 235 | fd = descriptor; |
| 236 | } |
| 237 | |
| 238 | ~Descriptor() |
| 239 | { |
| 240 | if (fd >= 0) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 241 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 242 | close(fd); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 243 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int operator()() const |
| 247 | { |
| 248 | return fd; |
| 249 | } |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 250 | }; |
| 251 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 252 | } // namespace phosphor |