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