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 | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 92 | /** @brief Get all the interfaces from the system. |
| 93 | * @returns list of interface names. |
| 94 | */ |
| 95 | IntfAddrMap getInterfaceAddrs(); |
| 96 | |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 97 | /** @brief Restart the systemd unit |
| 98 | * @param[in] unit - systemd unit name which needs to be |
| 99 | * restarted. |
| 100 | */ |
| 101 | inline void restartSystemdUnit(const std::string& unit) |
| 102 | { |
| 103 | auto bus = sdbusplus::bus::new_default(); |
| 104 | auto method = bus.new_method_call( |
| 105 | SYSTEMD_BUSNAME, |
| 106 | SYSTEMD_PATH, |
| 107 | SYSTEMD_INTERFACE, |
| 108 | "RestartUnit"); |
| 109 | |
| 110 | method.append(unit, "replace"); |
| 111 | bus.call_noreply(method); |
| 112 | |
| 113 | } |
| 114 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 115 | /** @brief Delete the given interface. |
| 116 | * @param[in] intf - interface name. |
| 117 | */ |
| 118 | void deleteInterface(const std::string& intf); |
| 119 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 120 | /** @brief read the DHCP value from the configuration file |
| 121 | * @param[in] confDir - Network configuration directory. |
| 122 | * @param[in] intf - Interface name. |
| 123 | */ |
| 124 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 125 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 126 | namespace internal |
| 127 | { |
| 128 | |
| 129 | /* @brief runs the given command in child process. |
| 130 | * @param[in] path - path of the binary file which needs to be execeuted. |
| 131 | * @param[in] args - arguments of the command. |
| 132 | */ |
| 133 | void executeCommandinChildProcess(const char* path, char** args); |
| 134 | |
| 135 | } // namespace internal |
| 136 | |
| 137 | /* @brief runs the given command in child process. |
| 138 | * @param[in] path -path of the binary file which needs to be execeuted. |
| 139 | * @param[in] tArgs - arguments of the command. |
| 140 | */ |
| 141 | template<typename... ArgTypes> |
| 142 | void execute(const char* path, ArgTypes&&... tArgs) |
| 143 | { |
| 144 | using expandType = char*[]; |
| 145 | |
| 146 | expandType args = { const_cast<char*>(tArgs)..., nullptr }; |
| 147 | |
| 148 | internal::executeCommandinChildProcess(path, args); |
| 149 | } |
| 150 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 151 | } //namespace network |
| 152 | |
| 153 | class Descriptor |
| 154 | { |
| 155 | private: |
| 156 | /** default value */ |
| 157 | int fd = -1; |
| 158 | |
| 159 | public: |
| 160 | Descriptor() = delete; |
| 161 | Descriptor(const Descriptor&) = delete; |
| 162 | Descriptor& operator=(const Descriptor&) = delete; |
| 163 | Descriptor(Descriptor&&) = delete; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 164 | Descriptor& operator=(Descriptor &&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 165 | |
| 166 | Descriptor(int fd) : fd(fd) {} |
| 167 | |
| 168 | ~Descriptor() |
| 169 | { |
| 170 | if (fd >= 0) |
| 171 | { |
| 172 | close(fd); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | int operator()() const |
| 177 | { |
| 178 | return fd; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | } //namespace phosphor |