blob: 3df928d5077d7d2080a933b55cdbd1220dfaff40 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
2
Ratan Gupta068a8cf2017-07-11 19:18:29 +05303#include "config.h"
Ratan Gupta3681a502017-06-17 19:20:04 +05304#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07005
6#include <unistd.h>
7
Ratan Guptabd303b12017-08-18 17:10:07 +05308#include <regex>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <sdbusplus/bus.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053010
11namespace phosphor
12{
13namespace network
14{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050015
16constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
17constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
18constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
19constexpr auto IPV4_PREFIX = "169.254";
20constexpr auto IPV6_PREFIX = "fe80";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050021constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050022
Ratan Guptabd303b12017-08-18 17:10:07 +053023namespace mac_address
24{
25
26constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
27constexpr auto localAdminMask = 0x020000000000;
28constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
29
Nagaraju Goruganti59937632017-12-05 00:06:07 -060030constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053031constexpr size_t size = 18;
32
33/** @brief validate the mac address
34 * @param[in] value - MAC address.
35 * @returns true if validate otherwise false.
36 */
37inline bool validate(const std::string& value)
38{
39 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050040 return std::regex_search(value, regexToCheck) &&
41 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053042}
43
44/** @brief gets the MAC address from the Inventory.
45 * @param[in] bus - DBUS Bus Object.
46 */
47std::string getfromInventory(sdbusplus::bus::bus& bus);
48
49namespace internal
50{
51/** @brief Converts the given mac address into unsigned 64 bit integer
52 * @param[in] value - MAC address.
53 * @returns converted unsigned 64 bit number.
54 */
55inline uint64_t convertToInt(const std::string& value)
56{
57 unsigned char mac[6];
58
59 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
60 mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5);
61 return
62 static_cast<uint64_t>(mac[0]) << 40 |
63 static_cast<uint64_t>(mac[1]) << 32 |
64 static_cast<uint64_t>(mac[2]) << 24 |
65 static_cast<uint64_t>(mac[3]) << 16 |
66 static_cast<uint64_t>(mac[4]) << 8 |
67 static_cast<uint64_t>(mac[5]);
68}
69
70}//namespace internal
71}//namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053072
Ratan Gupta497c0c92017-08-22 19:15:59 +053073constexpr auto networkdService = "systemd-networkd.service";
74constexpr auto timeSynchdService = "systemd-timesyncd.service";
75
Ratan Gupta8804feb2017-05-25 10:49:57 +053076/* @brief converts the given subnet into prefix notation.
77 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
78 * @param[in] mask - Subnet Mask.
79 * @returns prefix.
80 */
81uint8_t toCidr(int addressFamily, const std::string& mask);
82
83/* @brief converts the prefix into subnetmask.
84 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
85 * @param[in] prefix - prefix length.
86 * @returns subnet mask.
87 */
88std::string toMask(int addressFamily, uint8_t prefix);
89
90/* @brief checks that the given ip address is link local or not.
91 * @param[in] address - IP address.
92 * @returns true if it is linklocal otherwise false.
93 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050094bool isLinkLocalIP(const std::string& address);
95
96/* @brief checks that the given ip address valid or not.
97 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
98 * @param[in] address - IP address.
99 * @returns true if it is valid otherwise false.
100 */
101bool isValidIP(int addressFamily, const std::string& address);
102
103/* @brief checks that the given prefix is valid or not.
104 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
105 * @param[in] prefix - prefix length.
106 * @returns true if it is valid otherwise false.
107 */
108bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530109
Gunnar Millsd75f0492017-10-25 20:33:32 -0500110/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530111 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
112 * @param[in] ipaddress - IP address.
113 * @param[in] prefix - prefix length.
114 * @returns network section of the ipaddress.
115 */
116std::string getNetworkID(int addressFamily, const std::string& ipaddress,
117 uint8_t prefix);
118
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530119/** @brief Gets the map of interface and the associated
120 * address.
121 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530122 */
123IntfAddrMap getInterfaceAddrs();
124
Ratan Gupta068a8cf2017-07-11 19:18:29 +0530125/** @brief Restart the systemd unit
126 * @param[in] unit - systemd unit name which needs to be
127 * restarted.
128 */
129inline void restartSystemdUnit(const std::string& unit)
130{
131 auto bus = sdbusplus::bus::new_default();
132 auto method = bus.new_method_call(
133 SYSTEMD_BUSNAME,
134 SYSTEMD_PATH,
135 SYSTEMD_INTERFACE,
136 "RestartUnit");
137
138 method.append(unit, "replace");
139 bus.call_noreply(method);
140
141}
142
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530143/** @brief Get all the interfaces from the system.
144 * @returns list of interface names.
145 */
146InterfaceList getInterfaces();
147
Ratan Guptabc886292017-07-25 18:29:57 +0530148/** @brief Delete the given interface.
149 * @param[in] intf - interface name.
150 */
151void deleteInterface(const std::string& intf);
152
Ratan Gupta56187e72017-08-13 09:40:14 +0530153/** @brief read the DHCP value from the configuration file
154 * @param[in] confDir - Network configuration directory.
155 * @param[in] intf - Interface name.
156 */
157bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530158
Ratan Guptabd303b12017-08-18 17:10:07 +0530159namespace internal
160{
161
162/* @brief runs the given command in child process.
163 * @param[in] path - path of the binary file which needs to be execeuted.
164 * @param[in] args - arguments of the command.
165 */
166void executeCommandinChildProcess(const char* path, char** args);
167
168} // namespace internal
169
170/* @brief runs the given command in child process.
171 * @param[in] path -path of the binary file which needs to be execeuted.
172 * @param[in] tArgs - arguments of the command.
173 */
174template<typename... ArgTypes>
175void execute(const char* path, ArgTypes&&... tArgs)
176{
177 using expandType = char*[];
178
179 expandType args = { const_cast<char*>(tArgs)..., nullptr };
180
181 internal::executeCommandinChildProcess(path, args);
182}
183
Ratan Gupta8804feb2017-05-25 10:49:57 +0530184} //namespace network
185
186class Descriptor
187{
188 private:
189 /** default value */
190 int fd = -1;
191
192 public:
Ratan Gupta4b4a0082017-11-16 19:42:49 +0530193 Descriptor() = default;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530194 Descriptor(const Descriptor&) = delete;
195 Descriptor& operator=(const Descriptor&) = delete;
196 Descriptor(Descriptor&&) = delete;
Ratan Gupta11cef802017-05-29 08:41:48 +0530197 Descriptor& operator=(Descriptor &&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530198
Ratan Gupta62fb7352017-11-16 19:37:52 +0530199 explicit Descriptor(int fd) : fd(fd) {}
Ratan Gupta8804feb2017-05-25 10:49:57 +0530200
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530201 /* @brief sets the internal file descriptor with the given descriptor
202 * and closes the old descriptor.
203 * @param[in] descriptor - File/Socket descriptor.
204 */
205 void set(int descriptor)
206 {
207 // same descriptor given
208 if (fd == descriptor)
209 {
210 return;
211 }
212
213 // close the old descriptor
214 if (fd >= 0)
215 {
216 close(fd);
217 }
218
219 fd = descriptor;
220 }
221
Ratan Gupta8804feb2017-05-25 10:49:57 +0530222 ~Descriptor()
223 {
224 if (fd >= 0)
225 {
226 close(fd);
227 }
228 }
229
230 int operator()() const
231 {
232 return fd;
233 }
234};
235
236} //namespace phosphor