blob: 429b213cc05bd10d6c3df60fe0c97fc32f3341af [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 Guptafd4b0f02017-09-16 06:01:24 +053092/** @brief Gets the map of interface and the associated
93 * address.
94 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +053095 */
96IntfAddrMap getInterfaceAddrs();
97
Ratan Gupta068a8cf2017-07-11 19:18:29 +053098/** @brief Restart the systemd unit
99 * @param[in] unit - systemd unit name which needs to be
100 * restarted.
101 */
102inline void restartSystemdUnit(const std::string& unit)
103{
104 auto bus = sdbusplus::bus::new_default();
105 auto method = bus.new_method_call(
106 SYSTEMD_BUSNAME,
107 SYSTEMD_PATH,
108 SYSTEMD_INTERFACE,
109 "RestartUnit");
110
111 method.append(unit, "replace");
112 bus.call_noreply(method);
113
114}
115
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530116/** @brief Get all the interfaces from the system.
117 * @returns list of interface names.
118 */
119InterfaceList getInterfaces();
120
Ratan Guptabc886292017-07-25 18:29:57 +0530121/** @brief Delete the given interface.
122 * @param[in] intf - interface name.
123 */
124void deleteInterface(const std::string& intf);
125
Ratan Gupta56187e72017-08-13 09:40:14 +0530126/** @brief read the DHCP value from the configuration file
127 * @param[in] confDir - Network configuration directory.
128 * @param[in] intf - Interface name.
129 */
130bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530131
Ratan Guptabd303b12017-08-18 17:10:07 +0530132namespace internal
133{
134
135/* @brief runs the given command in child process.
136 * @param[in] path - path of the binary file which needs to be execeuted.
137 * @param[in] args - arguments of the command.
138 */
139void executeCommandinChildProcess(const char* path, char** args);
140
141} // namespace internal
142
143/* @brief runs the given command in child process.
144 * @param[in] path -path of the binary file which needs to be execeuted.
145 * @param[in] tArgs - arguments of the command.
146 */
147template<typename... ArgTypes>
148void execute(const char* path, ArgTypes&&... tArgs)
149{
150 using expandType = char*[];
151
152 expandType args = { const_cast<char*>(tArgs)..., nullptr };
153
154 internal::executeCommandinChildProcess(path, args);
155}
156
Ratan Gupta8804feb2017-05-25 10:49:57 +0530157} //namespace network
158
159class Descriptor
160{
161 private:
162 /** default value */
163 int fd = -1;
164
165 public:
166 Descriptor() = delete;
167 Descriptor(const Descriptor&) = delete;
168 Descriptor& operator=(const Descriptor&) = delete;
169 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +0530170 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530171
172 Descriptor(int fd) : fd(fd) {}
173
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530174 /* @brief sets the internal file descriptor with the given descriptor
175 * and closes the old descriptor.
176 * @param[in] descriptor - File/Socket descriptor.
177 */
178 void set(int descriptor)
179 {
180 // same descriptor given
181 if (fd == descriptor)
182 {
183 return;
184 }
185
186 // close the old descriptor
187 if (fd >= 0)
188 {
189 close(fd);
190 }
191
192 fd = descriptor;
193 }
194
Ratan Gupta8804feb2017-05-25 10:49:57 +0530195 ~Descriptor()
196 {
197 if (fd >= 0)
198 {
199 close(fd);
200 }
201 }
202
203 int operator()() const
204 {
205 return fd;
206 }
207};
208
209} //namespace phosphor