Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 4 | |
| 5 | #include "config.h" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 6 | #include "types.hpp" |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 8 | #include <regex> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace network |
| 13 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 14 | |
| 15 | constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; |
| 16 | constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; |
| 17 | constexpr auto IPV6_MAX_PREFIX_LENGTH = 64; |
| 18 | constexpr auto IPV4_PREFIX = "169.254"; |
| 19 | constexpr auto IPV6_PREFIX = "fe80"; |
Nagaraju Goruganti | 98fc588 | 2017-10-31 04:40:12 -0500 | [diff] [blame] | 20 | constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00"; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 21 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 22 | namespace mac_address |
| 23 | { |
| 24 | |
| 25 | constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"; |
| 26 | constexpr auto localAdminMask = 0x020000000000; |
| 27 | constexpr auto broadcastMac = 0xFFFFFFFFFFFF; |
| 28 | |
Nagaraju Goruganti | 5993763 | 2017-12-05 00:06:07 -0600 | [diff] [blame] | 29 | constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 30 | constexpr size_t size = 18; |
| 31 | |
| 32 | /** @brief validate the mac address |
| 33 | * @param[in] value - MAC address. |
| 34 | * @returns true if validate otherwise false. |
| 35 | */ |
| 36 | inline bool validate(const std::string& value) |
| 37 | { |
| 38 | std::regex regexToCheck(regex); |
Nagaraju Goruganti | 98fc588 | 2017-10-31 04:40:12 -0500 | [diff] [blame] | 39 | return std::regex_search(value, regexToCheck) && |
| 40 | value.find(ZEROMACADDRESS) != 0; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** @brief gets the MAC address from the Inventory. |
| 44 | * @param[in] bus - DBUS Bus Object. |
| 45 | */ |
| 46 | std::string getfromInventory(sdbusplus::bus::bus& bus); |
| 47 | |
| 48 | namespace internal |
| 49 | { |
| 50 | /** @brief Converts the given mac address into unsigned 64 bit integer |
| 51 | * @param[in] value - MAC address. |
| 52 | * @returns converted unsigned 64 bit number. |
| 53 | */ |
| 54 | inline uint64_t convertToInt(const std::string& value) |
| 55 | { |
| 56 | unsigned char mac[6]; |
| 57 | |
| 58 | sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 59 | mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5); |
| 60 | return |
| 61 | static_cast<uint64_t>(mac[0]) << 40 | |
| 62 | static_cast<uint64_t>(mac[1]) << 32 | |
| 63 | static_cast<uint64_t>(mac[2]) << 24 | |
| 64 | static_cast<uint64_t>(mac[3]) << 16 | |
| 65 | static_cast<uint64_t>(mac[4]) << 8 | |
| 66 | static_cast<uint64_t>(mac[5]); |
| 67 | } |
| 68 | |
| 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 | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 124 | /** @brief Restart the systemd unit |
| 125 | * @param[in] unit - systemd unit name which needs to be |
| 126 | * restarted. |
| 127 | */ |
| 128 | inline void restartSystemdUnit(const std::string& unit) |
| 129 | { |
| 130 | auto bus = sdbusplus::bus::new_default(); |
| 131 | auto method = bus.new_method_call( |
| 132 | SYSTEMD_BUSNAME, |
| 133 | SYSTEMD_PATH, |
| 134 | SYSTEMD_INTERFACE, |
| 135 | "RestartUnit"); |
| 136 | |
| 137 | method.append(unit, "replace"); |
| 138 | bus.call_noreply(method); |
| 139 | |
| 140 | } |
| 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 | */ |
| 173 | template<typename... ArgTypes> |
| 174 | void execute(const char* path, ArgTypes&&... tArgs) |
| 175 | { |
| 176 | using expandType = char*[]; |
| 177 | |
| 178 | expandType args = { const_cast<char*>(tArgs)..., nullptr }; |
| 179 | |
| 180 | internal::executeCommandinChildProcess(path, args); |
| 181 | } |
| 182 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 183 | } //namespace network |
| 184 | |
| 185 | class Descriptor |
| 186 | { |
| 187 | private: |
| 188 | /** default value */ |
| 189 | int fd = -1; |
| 190 | |
| 191 | public: |
Ratan Gupta | 4b4a008 | 2017-11-16 19:42:49 +0530 | [diff] [blame] | 192 | Descriptor() = default; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 193 | Descriptor(const Descriptor&) = delete; |
| 194 | Descriptor& operator=(const Descriptor&) = delete; |
| 195 | Descriptor(Descriptor&&) = delete; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 196 | Descriptor& operator=(Descriptor &&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 197 | |
Ratan Gupta | 62fb735 | 2017-11-16 19:37:52 +0530 | [diff] [blame] | 198 | explicit Descriptor(int fd) : fd(fd) {} |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 199 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 200 | /* @brief sets the internal file descriptor with the given descriptor |
| 201 | * and closes the old descriptor. |
| 202 | * @param[in] descriptor - File/Socket descriptor. |
| 203 | */ |
| 204 | void set(int descriptor) |
| 205 | { |
| 206 | // same descriptor given |
| 207 | if (fd == descriptor) |
| 208 | { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // close the old descriptor |
| 213 | if (fd >= 0) |
| 214 | { |
| 215 | close(fd); |
| 216 | } |
| 217 | |
| 218 | fd = descriptor; |
| 219 | } |
| 220 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 221 | ~Descriptor() |
| 222 | { |
| 223 | if (fd >= 0) |
| 224 | { |
| 225 | close(fd); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | int operator()() const |
| 230 | { |
| 231 | return fd; |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | } //namespace phosphor |