blob: 4a9d5f7583e8ab1843de7be588462e8483bb9258 [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 Guptabd303b12017-08-18 17:10:07 +05308#include <regex>
Ratan Gupta8804feb2017-05-25 10:49:57 +05309
10namespace phosphor
11{
12namespace network
13{
Ratan Guptabd303b12017-08-18 17:10:07 +053014namespace mac_address
15{
16
17constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
18constexpr auto localAdminMask = 0x020000000000;
19constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
20
21constexpr auto format = "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx";
22constexpr size_t size = 18;
23
24/** @brief validate the mac address
25 * @param[in] value - MAC address.
26 * @returns true if validate otherwise false.
27 */
28inline bool validate(const std::string& value)
29{
30 std::regex regexToCheck(regex);
31 return std::regex_search(value, regexToCheck);
32}
33
34/** @brief gets the MAC address from the Inventory.
35 * @param[in] bus - DBUS Bus Object.
36 */
37std::string getfromInventory(sdbusplus::bus::bus& bus);
38
39namespace internal
40{
41/** @brief Converts the given mac address into unsigned 64 bit integer
42 * @param[in] value - MAC address.
43 * @returns converted unsigned 64 bit number.
44 */
45inline uint64_t convertToInt(const std::string& value)
46{
47 unsigned char mac[6];
48
49 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
50 mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5);
51 return
52 static_cast<uint64_t>(mac[0]) << 40 |
53 static_cast<uint64_t>(mac[1]) << 32 |
54 static_cast<uint64_t>(mac[2]) << 24 |
55 static_cast<uint64_t>(mac[3]) << 16 |
56 static_cast<uint64_t>(mac[4]) << 8 |
57 static_cast<uint64_t>(mac[5]);
58}
59
60}//namespace internal
61}//namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053062
63/* @brief converts the given subnet into prefix notation.
64 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
65 * @param[in] mask - Subnet Mask.
66 * @returns prefix.
67 */
68uint8_t toCidr(int addressFamily, const std::string& mask);
69
70/* @brief converts the prefix into subnetmask.
71 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
72 * @param[in] prefix - prefix length.
73 * @returns subnet mask.
74 */
75std::string toMask(int addressFamily, uint8_t prefix);
76
77/* @brief checks that the given ip address is link local or not.
78 * @param[in] address - IP address.
79 * @returns true if it is linklocal otherwise false.
80 */
81bool isLinkLocal(const std::string& address);
82
Ratan Gupta11cef802017-05-29 08:41:48 +053083/* @brief gets the network section of the ip adress.
84 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
85 * @param[in] ipaddress - IP address.
86 * @param[in] prefix - prefix length.
87 * @returns network section of the ipaddress.
88 */
89std::string getNetworkID(int addressFamily, const std::string& ipaddress,
90 uint8_t prefix);
91
Ratan Gupta3681a502017-06-17 19:20:04 +053092/** @brief Get all the interfaces from the system.
93 * @returns list of interface names.
94 */
95IntfAddrMap getInterfaceAddrs();
96
Ratan Gupta068a8cf2017-07-11 19:18:29 +053097/** @brief Restart the systemd unit
98 * @param[in] unit - systemd unit name which needs to be
99 * restarted.
100 */
101inline void restartSystemdUnit(const std::string& unit)
102{
103 auto bus = sdbusplus::bus::new_default();
104 auto method = bus.new_method_call(
105 SYSTEMD_BUSNAME,
106 SYSTEMD_PATH,
107 SYSTEMD_INTERFACE,
108 "RestartUnit");
109
110 method.append(unit, "replace");
111 bus.call_noreply(method);
112
113}
114
Ratan Guptabc886292017-07-25 18:29:57 +0530115/** @brief Delete the given interface.
116 * @param[in] intf - interface name.
117 */
118void deleteInterface(const std::string& intf);
119
Ratan Gupta56187e72017-08-13 09:40:14 +0530120/** @brief read the DHCP value from the configuration file
121 * @param[in] confDir - Network configuration directory.
122 * @param[in] intf - Interface name.
123 */
124bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530125
Ratan Guptabd303b12017-08-18 17:10:07 +0530126namespace internal
127{
128
129/* @brief runs the given command in child process.
130 * @param[in] path - path of the binary file which needs to be execeuted.
131 * @param[in] args - arguments of the command.
132 */
133void executeCommandinChildProcess(const char* path, char** args);
134
135} // namespace internal
136
137/* @brief runs the given command in child process.
138 * @param[in] path -path of the binary file which needs to be execeuted.
139 * @param[in] tArgs - arguments of the command.
140 */
141template<typename... ArgTypes>
142void execute(const char* path, ArgTypes&&... tArgs)
143{
144 using expandType = char*[];
145
146 expandType args = { const_cast<char*>(tArgs)..., nullptr };
147
148 internal::executeCommandinChildProcess(path, args);
149}
150
Ratan Gupta8804feb2017-05-25 10:49:57 +0530151} //namespace network
152
153class Descriptor
154{
155 private:
156 /** default value */
157 int fd = -1;
158
159 public:
160 Descriptor() = delete;
161 Descriptor(const Descriptor&) = delete;
162 Descriptor& operator=(const Descriptor&) = delete;
163 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +0530164 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530165
166 Descriptor(int fd) : fd(fd) {}
167
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530168 /* @brief sets the internal file descriptor with the given descriptor
169 * and closes the old descriptor.
170 * @param[in] descriptor - File/Socket descriptor.
171 */
172 void set(int descriptor)
173 {
174 // same descriptor given
175 if (fd == descriptor)
176 {
177 return;
178 }
179
180 // close the old descriptor
181 if (fd >= 0)
182 {
183 close(fd);
184 }
185
186 fd = descriptor;
187 }
188
Ratan Gupta8804feb2017-05-25 10:49:57 +0530189 ~Descriptor()
190 {
191 if (fd >= 0)
192 {
193 close(fd);
194 }
195 }
196
197 int operator()() const
198 {
199 return fd;
200 }
201};
202
203} //namespace phosphor