blob: 3434aca694fcb925fb5199f1a82c13022255dff4 [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 Gupta8804feb2017-05-25 10:49:57 +053066} //namespace network
67
68class Descriptor
69{
70 private:
71 /** default value */
72 int fd = -1;
73
74 public:
75 Descriptor() = delete;
76 Descriptor(const Descriptor&) = delete;
77 Descriptor& operator=(const Descriptor&) = delete;
78 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +053079 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +053080
81 Descriptor(int fd) : fd(fd) {}
82
83 ~Descriptor()
84 {
85 if (fd >= 0)
86 {
87 close(fd);
88 }
89 }
90
91 int operator()() const
92 {
93 return fd;
94 }
95};
96
97} //namespace phosphor