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 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 71 | /** @brief read the DHCP value from the configuration file |
| 72 | * @param[in] confDir - Network configuration directory. |
| 73 | * @param[in] intf - Interface name. |
| 74 | */ |
| 75 | bool getDHCPValue(const std::string& confDir, const std::string& intf); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 76 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 77 | } //namespace network |
| 78 | |
| 79 | class Descriptor |
| 80 | { |
| 81 | private: |
| 82 | /** default value */ |
| 83 | int fd = -1; |
| 84 | |
| 85 | public: |
| 86 | Descriptor() = delete; |
| 87 | Descriptor(const Descriptor&) = delete; |
| 88 | Descriptor& operator=(const Descriptor&) = delete; |
| 89 | Descriptor(Descriptor&&) = delete; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 90 | Descriptor& operator=(Descriptor &&) = delete; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 91 | |
| 92 | Descriptor(int fd) : fd(fd) {} |
| 93 | |
| 94 | ~Descriptor() |
| 95 | { |
| 96 | if (fd >= 0) |
| 97 | { |
| 98 | close(fd); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int operator()() const |
| 103 | { |
| 104 | return fd; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | } //namespace phosphor |