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 | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace network |
| 12 | { |
| 13 | |
| 14 | /* @brief converts the given subnet into prefix notation. |
| 15 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 16 | * @param[in] mask - Subnet Mask. |
| 17 | * @returns prefix. |
| 18 | */ |
| 19 | uint8_t toCidr(int addressFamily, const std::string& mask); |
| 20 | |
| 21 | /* @brief converts the prefix into subnetmask. |
| 22 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 23 | * @param[in] prefix - prefix length. |
| 24 | * @returns subnet mask. |
| 25 | */ |
| 26 | std::string toMask(int addressFamily, uint8_t prefix); |
| 27 | |
| 28 | /* @brief checks that the given ip address is link local or not. |
| 29 | * @param[in] address - IP address. |
| 30 | * @returns true if it is linklocal otherwise false. |
| 31 | */ |
| 32 | bool isLinkLocal(const std::string& address); |
| 33 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 34 | /* @brief gets the network section of the ip adress. |
| 35 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 36 | * @param[in] ipaddress - IP address. |
| 37 | * @param[in] prefix - prefix length. |
| 38 | * @returns network section of the ipaddress. |
| 39 | */ |
| 40 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
| 41 | uint8_t prefix); |
| 42 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 43 | /** @brief Get all the interfaces from the system. |
| 44 | * @returns list of interface names. |
| 45 | */ |
| 46 | IntfAddrMap getInterfaceAddrs(); |
| 47 | |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 48 | /** @brief Restart the systemd unit |
| 49 | * @param[in] unit - systemd unit name which needs to be |
| 50 | * restarted. |
| 51 | */ |
| 52 | inline void restartSystemdUnit(const std::string& unit) |
| 53 | { |
| 54 | auto bus = sdbusplus::bus::new_default(); |
| 55 | auto method = bus.new_method_call( |
| 56 | SYSTEMD_BUSNAME, |
| 57 | SYSTEMD_PATH, |
| 58 | SYSTEMD_INTERFACE, |
| 59 | "RestartUnit"); |
| 60 | |
| 61 | method.append(unit, "replace"); |
| 62 | bus.call_noreply(method); |
| 63 | |
| 64 | } |
| 65 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 66 | /** @brief Delete the given interface. |
| 67 | * @param[in] intf - interface name. |
| 68 | */ |
| 69 | void deleteInterface(const std::string& intf); |
| 70 | |
| 71 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 72 | } //namespace network |
| 73 | |
| 74 | class Descriptor |
| 75 | { |
| 76 | private: |
| 77 | /** default value */ |
| 78 | int fd = -1; |
| 79 | |
| 80 | public: |
| 81 | Descriptor() = delete; |
| 82 | Descriptor(const Descriptor&) = delete; |
| 83 | Descriptor& operator=(const Descriptor&) = delete; |
| 84 | Descriptor(Descriptor&&) = delete; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 85 | Descriptor& operator=(Descriptor &&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 86 | |
| 87 | Descriptor(int fd) : fd(fd) {} |
| 88 | |
| 89 | ~Descriptor() |
| 90 | { |
| 91 | if (fd >= 0) |
| 92 | { |
| 93 | close(fd); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | int operator()() const |
| 98 | { |
| 99 | return fd; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | } //namespace phosphor |