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