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