blob: 2015b0c4138323a143879357bafde14debe6cfdb [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
Ratan Gupta56187e72017-08-13 09:40:14 +053071/** @brief read the DHCP value from the configuration file
72 * @param[in] confDir - Network configuration directory.
73 * @param[in] intf - Interface name.
74 */
75bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +053076
Ratan Gupta8804feb2017-05-25 10:49:57 +053077} //namespace network
78
79class 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 Gupta11cef802017-05-29 08:41:48 +053090 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +053091
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