blob: 1d74300e9107ad0b32b3dda09f9a491f58a83209 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
2
3#include <unistd.h>
Ratan Gupta068a8cf2017-07-11 19:18:29 +05304
5#include "config.h"
Ratan Gupta3681a502017-06-17 19:20:04 +05306#include "types.hpp"
Ratan Gupta068a8cf2017-07-11 19:18:29 +05307#include <sdbusplus/bus.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +05308
9namespace phosphor
10{
11namespace 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 */
19uint8_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 */
26std::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 */
32bool isLinkLocal(const std::string& address);
33
Ratan Gupta11cef802017-05-29 08:41:48 +053034/* @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 */
40std::string getNetworkID(int addressFamily, const std::string& ipaddress,
41 uint8_t prefix);
42
Ratan Gupta3681a502017-06-17 19:20:04 +053043/** @brief Get all the interfaces from the system.
44 * @returns list of interface names.
45 */
46IntfAddrMap getInterfaceAddrs();
47
Ratan Gupta068a8cf2017-07-11 19:18:29 +053048/** @brief Restart the systemd unit
49 * @param[in] unit - systemd unit name which needs to be
50 * restarted.
51 */
52inline 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 Guptabc886292017-07-25 18:29:57 +053066/** @brief Delete the given interface.
67 * @param[in] intf - interface name.
68 */
69void deleteInterface(const std::string& intf);
70
71
Ratan Gupta8804feb2017-05-25 10:49:57 +053072} //namespace network
73
74class 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 Gupta11cef802017-05-29 08:41:48 +053085 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +053086
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