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