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