blob: ff91d172cf3e0f1efb00ba190e54707ad3e9021b [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{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050014
15constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
16constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
17constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
18constexpr auto IPV4_PREFIX = "169.254";
19constexpr auto IPV6_PREFIX = "fe80";
20
Ratan Guptabd303b12017-08-18 17:10:07 +053021namespace mac_address
22{
23
24constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
25constexpr auto localAdminMask = 0x020000000000;
26constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
27
28constexpr auto format = "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx";
29constexpr size_t size = 18;
30
31/** @brief validate the mac address
32 * @param[in] value - MAC address.
33 * @returns true if validate otherwise false.
34 */
35inline bool validate(const std::string& value)
36{
37 std::regex regexToCheck(regex);
38 return std::regex_search(value, regexToCheck);
39}
40
41/** @brief gets the MAC address from the Inventory.
42 * @param[in] bus - DBUS Bus Object.
43 */
44std::string getfromInventory(sdbusplus::bus::bus& bus);
45
46namespace internal
47{
48/** @brief Converts the given mac address into unsigned 64 bit integer
49 * @param[in] value - MAC address.
50 * @returns converted unsigned 64 bit number.
51 */
52inline uint64_t convertToInt(const std::string& value)
53{
54 unsigned char mac[6];
55
56 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
57 mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5);
58 return
59 static_cast<uint64_t>(mac[0]) << 40 |
60 static_cast<uint64_t>(mac[1]) << 32 |
61 static_cast<uint64_t>(mac[2]) << 24 |
62 static_cast<uint64_t>(mac[3]) << 16 |
63 static_cast<uint64_t>(mac[4]) << 8 |
64 static_cast<uint64_t>(mac[5]);
65}
66
67}//namespace internal
68}//namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053069
Ratan Gupta497c0c92017-08-22 19:15:59 +053070constexpr auto networkdService = "systemd-networkd.service";
71constexpr auto timeSynchdService = "systemd-timesyncd.service";
72
Ratan Gupta8804feb2017-05-25 10:49:57 +053073/* @brief converts the given subnet into prefix notation.
74 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
75 * @param[in] mask - Subnet Mask.
76 * @returns prefix.
77 */
78uint8_t toCidr(int addressFamily, const std::string& mask);
79
80/* @brief converts the prefix into subnetmask.
81 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
82 * @param[in] prefix - prefix length.
83 * @returns subnet mask.
84 */
85std::string toMask(int addressFamily, uint8_t prefix);
86
87/* @brief checks that the given ip address is link local or not.
88 * @param[in] address - IP address.
89 * @returns true if it is linklocal otherwise false.
90 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050091bool isLinkLocalIP(const std::string& address);
92
93/* @brief checks that the given ip address valid or not.
94 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
95 * @param[in] address - IP address.
96 * @returns true if it is valid otherwise false.
97 */
98bool isValidIP(int addressFamily, const std::string& address);
99
100/* @brief checks that the given prefix is valid or not.
101 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
102 * @param[in] prefix - prefix length.
103 * @returns true if it is valid otherwise false.
104 */
105bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530106
Ratan Gupta11cef802017-05-29 08:41:48 +0530107/* @brief gets the network section of the ip adress.
108 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
109 * @param[in] ipaddress - IP address.
110 * @param[in] prefix - prefix length.
111 * @returns network section of the ipaddress.
112 */
113std::string getNetworkID(int addressFamily, const std::string& ipaddress,
114 uint8_t prefix);
115
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530116/** @brief Gets the map of interface and the associated
117 * address.
118 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530119 */
120IntfAddrMap getInterfaceAddrs();
121
Ratan Gupta068a8cf2017-07-11 19:18:29 +0530122/** @brief Restart the systemd unit
123 * @param[in] unit - systemd unit name which needs to be
124 * restarted.
125 */
126inline void restartSystemdUnit(const std::string& unit)
127{
128 auto bus = sdbusplus::bus::new_default();
129 auto method = bus.new_method_call(
130 SYSTEMD_BUSNAME,
131 SYSTEMD_PATH,
132 SYSTEMD_INTERFACE,
133 "RestartUnit");
134
135 method.append(unit, "replace");
136 bus.call_noreply(method);
137
138}
139
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530140/** @brief Get all the interfaces from the system.
141 * @returns list of interface names.
142 */
143InterfaceList getInterfaces();
144
Ratan Guptabc886292017-07-25 18:29:57 +0530145/** @brief Delete the given interface.
146 * @param[in] intf - interface name.
147 */
148void deleteInterface(const std::string& intf);
149
Ratan Gupta56187e72017-08-13 09:40:14 +0530150/** @brief read the DHCP value from the configuration file
151 * @param[in] confDir - Network configuration directory.
152 * @param[in] intf - Interface name.
153 */
154bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530155
Ratan Guptabd303b12017-08-18 17:10:07 +0530156namespace internal
157{
158
159/* @brief runs the given command in child process.
160 * @param[in] path - path of the binary file which needs to be execeuted.
161 * @param[in] args - arguments of the command.
162 */
163void executeCommandinChildProcess(const char* path, char** args);
164
165} // namespace internal
166
167/* @brief runs the given command in child process.
168 * @param[in] path -path of the binary file which needs to be execeuted.
169 * @param[in] tArgs - arguments of the command.
170 */
171template<typename... ArgTypes>
172void execute(const char* path, ArgTypes&&... tArgs)
173{
174 using expandType = char*[];
175
176 expandType args = { const_cast<char*>(tArgs)..., nullptr };
177
178 internal::executeCommandinChildProcess(path, args);
179}
180
Ratan Gupta8804feb2017-05-25 10:49:57 +0530181} //namespace network
182
183class Descriptor
184{
185 private:
186 /** default value */
187 int fd = -1;
188
189 public:
190 Descriptor() = delete;
191 Descriptor(const Descriptor&) = delete;
192 Descriptor& operator=(const Descriptor&) = delete;
193 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +0530194 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530195
196 Descriptor(int fd) : fd(fd) {}
197
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530198 /* @brief sets the internal file descriptor with the given descriptor
199 * and closes the old descriptor.
200 * @param[in] descriptor - File/Socket descriptor.
201 */
202 void set(int descriptor)
203 {
204 // same descriptor given
205 if (fd == descriptor)
206 {
207 return;
208 }
209
210 // close the old descriptor
211 if (fd >= 0)
212 {
213 close(fd);
214 }
215
216 fd = descriptor;
217 }
218
Ratan Gupta8804feb2017-05-25 10:49:57 +0530219 ~Descriptor()
220 {
221 if (fd >= 0)
222 {
223 close(fd);
224 }
225 }
226
227 int operator()() const
228 {
229 return fd;
230 }
231};
232
233} //namespace phosphor