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