blob: ab7ea51725c0d2b0f1e0df472391941978a659ac [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";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050020constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050021
Ratan Guptabd303b12017-08-18 17:10:07 +053022namespace mac_address
23{
24
25constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
26constexpr auto localAdminMask = 0x020000000000;
27constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
28
Nagaraju Goruganti59937632017-12-05 00:06:07 -060029constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053030constexpr size_t size = 18;
31
32/** @brief validate the mac address
33 * @param[in] value - MAC address.
34 * @returns true if validate otherwise false.
35 */
36inline bool validate(const std::string& value)
37{
38 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050039 return std::regex_search(value, regexToCheck) &&
40 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053041}
42
43/** @brief gets the MAC address from the Inventory.
44 * @param[in] bus - DBUS Bus Object.
45 */
46std::string getfromInventory(sdbusplus::bus::bus& bus);
47
48namespace internal
49{
50/** @brief Converts the given mac address into unsigned 64 bit integer
51 * @param[in] value - MAC address.
52 * @returns converted unsigned 64 bit number.
53 */
54inline uint64_t convertToInt(const std::string& value)
55{
56 unsigned char mac[6];
57
58 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
59 mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5);
60 return
61 static_cast<uint64_t>(mac[0]) << 40 |
62 static_cast<uint64_t>(mac[1]) << 32 |
63 static_cast<uint64_t>(mac[2]) << 24 |
64 static_cast<uint64_t>(mac[3]) << 16 |
65 static_cast<uint64_t>(mac[4]) << 8 |
66 static_cast<uint64_t>(mac[5]);
67}
68
69}//namespace internal
70}//namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053071
Ratan Gupta497c0c92017-08-22 19:15:59 +053072constexpr auto networkdService = "systemd-networkd.service";
73constexpr auto timeSynchdService = "systemd-timesyncd.service";
74
Ratan Gupta8804feb2017-05-25 10:49:57 +053075/* @brief converts the given subnet into prefix notation.
76 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
77 * @param[in] mask - Subnet Mask.
78 * @returns prefix.
79 */
80uint8_t toCidr(int addressFamily, const std::string& mask);
81
82/* @brief converts the prefix into subnetmask.
83 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
84 * @param[in] prefix - prefix length.
85 * @returns subnet mask.
86 */
87std::string toMask(int addressFamily, uint8_t prefix);
88
89/* @brief checks that the given ip address is link local or not.
90 * @param[in] address - IP address.
91 * @returns true if it is linklocal otherwise false.
92 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050093bool isLinkLocalIP(const std::string& address);
94
95/* @brief checks that the given ip address valid or not.
96 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
97 * @param[in] address - IP address.
98 * @returns true if it is valid otherwise false.
99 */
100bool isValidIP(int addressFamily, const std::string& address);
101
102/* @brief checks that the given prefix is valid or not.
103 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
104 * @param[in] prefix - prefix length.
105 * @returns true if it is valid otherwise false.
106 */
107bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530108
Gunnar Millsd75f0492017-10-25 20:33:32 -0500109/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530110 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
111 * @param[in] ipaddress - IP address.
112 * @param[in] prefix - prefix length.
113 * @returns network section of the ipaddress.
114 */
115std::string getNetworkID(int addressFamily, const std::string& ipaddress,
116 uint8_t prefix);
117
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530118/** @brief Gets the map of interface and the associated
119 * address.
120 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530121 */
122IntfAddrMap getInterfaceAddrs();
123
Ratan Gupta068a8cf2017-07-11 19:18:29 +0530124/** @brief Restart the systemd unit
125 * @param[in] unit - systemd unit name which needs to be
126 * restarted.
127 */
128inline void restartSystemdUnit(const std::string& unit)
129{
130 auto bus = sdbusplus::bus::new_default();
131 auto method = bus.new_method_call(
132 SYSTEMD_BUSNAME,
133 SYSTEMD_PATH,
134 SYSTEMD_INTERFACE,
135 "RestartUnit");
136
137 method.append(unit, "replace");
138 bus.call_noreply(method);
139
140}
141
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530142/** @brief Get all the interfaces from the system.
143 * @returns list of interface names.
144 */
145InterfaceList getInterfaces();
146
Ratan Guptabc886292017-07-25 18:29:57 +0530147/** @brief Delete the given interface.
148 * @param[in] intf - interface name.
149 */
150void deleteInterface(const std::string& intf);
151
Ratan Gupta56187e72017-08-13 09:40:14 +0530152/** @brief read the DHCP value from the configuration file
153 * @param[in] confDir - Network configuration directory.
154 * @param[in] intf - Interface name.
155 */
156bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530157
Ratan Guptabd303b12017-08-18 17:10:07 +0530158namespace internal
159{
160
161/* @brief runs the given command in child process.
162 * @param[in] path - path of the binary file which needs to be execeuted.
163 * @param[in] args - arguments of the command.
164 */
165void executeCommandinChildProcess(const char* path, char** args);
166
167} // namespace internal
168
169/* @brief runs the given command in child process.
170 * @param[in] path -path of the binary file which needs to be execeuted.
171 * @param[in] tArgs - arguments of the command.
172 */
173template<typename... ArgTypes>
174void execute(const char* path, ArgTypes&&... tArgs)
175{
176 using expandType = char*[];
177
178 expandType args = { const_cast<char*>(tArgs)..., nullptr };
179
180 internal::executeCommandinChildProcess(path, args);
181}
182
Ratan Gupta8804feb2017-05-25 10:49:57 +0530183} //namespace network
184
185class Descriptor
186{
187 private:
188 /** default value */
189 int fd = -1;
190
191 public:
Ratan Gupta4b4a0082017-11-16 19:42:49 +0530192 Descriptor() = default;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530193 Descriptor(const Descriptor&) = delete;
194 Descriptor& operator=(const Descriptor&) = delete;
195 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +0530196 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530197
Ratan Gupta62fb7352017-11-16 19:37:52 +0530198 explicit Descriptor(int fd) : fd(fd) {}
Ratan Gupta8804feb2017-05-25 10:49:57 +0530199
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530200 /* @brief sets the internal file descriptor with the given descriptor
201 * and closes the old descriptor.
202 * @param[in] descriptor - File/Socket descriptor.
203 */
204 void set(int descriptor)
205 {
206 // same descriptor given
207 if (fd == descriptor)
208 {
209 return;
210 }
211
212 // close the old descriptor
213 if (fd >= 0)
214 {
215 close(fd);
216 }
217
218 fd = descriptor;
219 }
220
Ratan Gupta8804feb2017-05-25 10:49:57 +0530221 ~Descriptor()
222 {
223 if (fd >= 0)
224 {
225 close(fd);
226 }
227 }
228
229 int operator()() const
230 {
231 return fd;
232 }
233};
234
235} //namespace phosphor