blob: b3ec771973ead315280dd087c1e934d557bc8494 [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"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004
Ratan Gupta3681a502017-06-17 19:20:04 +05305#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006
7#include <unistd.h>
8
Ratan Guptabd303b12017-08-18 17:10:07 +05309#include <regex>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <sdbusplus/bus.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053011
12namespace phosphor
13{
14namespace network
15{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050016
17constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
18constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
19constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
20constexpr auto IPV4_PREFIX = "169.254";
21constexpr auto IPV6_PREFIX = "fe80";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050022constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050023
Ratan Guptabd303b12017-08-18 17:10:07 +053024namespace mac_address
25{
26
27constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
28constexpr auto localAdminMask = 0x020000000000;
29constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
30
Nagaraju Goruganti59937632017-12-05 00:06:07 -060031constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053032constexpr size_t size = 18;
33
34/** @brief validate the mac address
35 * @param[in] value - MAC address.
36 * @returns true if validate otherwise false.
37 */
38inline bool validate(const std::string& value)
39{
40 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050041 return std::regex_search(value, regexToCheck) &&
42 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053043}
44
45/** @brief gets the MAC address from the Inventory.
46 * @param[in] bus - DBUS Bus Object.
47 */
48std::string getfromInventory(sdbusplus::bus::bus& bus);
49
50namespace internal
51{
52/** @brief Converts the given mac address into unsigned 64 bit integer
53 * @param[in] value - MAC address.
54 * @returns converted unsigned 64 bit number.
55 */
56inline uint64_t convertToInt(const std::string& value)
57{
58 unsigned char mac[6];
59
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1,
61 mac + 2, mac + 3, mac + 4, mac + 5);
62 return 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 | static_cast<uint64_t>(mac[5]);
Ratan Guptabd303b12017-08-18 17:10:07 +053067}
68
Gunnar Mills57d9c502018-09-14 14:42:34 -050069} // 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();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
132 SYSTEMD_INTERFACE, "RestartUnit");
Ratan Gupta068a8cf2017-07-11 19:18:29 +0530133
134 method.append(unit, "replace");
135 bus.call_noreply(method);
Ratan Gupta068a8cf2017-07-11 19:18:29 +0530136}
137
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530138/** @brief Get all the interfaces from the system.
139 * @returns list of interface names.
140 */
141InterfaceList getInterfaces();
142
Ratan Guptabc886292017-07-25 18:29:57 +0530143/** @brief Delete the given interface.
144 * @param[in] intf - interface name.
145 */
146void deleteInterface(const std::string& intf);
147
Ratan Gupta56187e72017-08-13 09:40:14 +0530148/** @brief read the DHCP value from the configuration file
149 * @param[in] confDir - Network configuration directory.
150 * @param[in] intf - Interface name.
151 */
152bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530153
Ratan Guptabd303b12017-08-18 17:10:07 +0530154namespace internal
155{
156
157/* @brief runs the given command in child process.
158 * @param[in] path - path of the binary file which needs to be execeuted.
159 * @param[in] args - arguments of the command.
160 */
161void executeCommandinChildProcess(const char* path, char** args);
162
163} // namespace internal
164
165/* @brief runs the given command in child process.
166 * @param[in] path -path of the binary file which needs to be execeuted.
167 * @param[in] tArgs - arguments of the command.
168 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530170void execute(const char* path, ArgTypes&&... tArgs)
171{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500172 using expandType = char* [];
Ratan Guptabd303b12017-08-18 17:10:07 +0530173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530175
176 internal::executeCommandinChildProcess(path, args);
177}
178
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530180
181class Descriptor
182{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 private:
184 /** default value */
185 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530186
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 public:
188 Descriptor() = default;
189 Descriptor(const Descriptor&) = delete;
190 Descriptor& operator=(const Descriptor&) = delete;
191 Descriptor(Descriptor&&) = delete;
192 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530193
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 explicit Descriptor(int fd) : fd(fd)
195 {
196 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530197
Gunnar Mills57d9c502018-09-14 14:42:34 -0500198 /* @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)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530206 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530208 }
209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 // close the old descriptor
211 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530212 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530214 }
215
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216 fd = descriptor;
217 }
218
219 ~Descriptor()
220 {
221 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530222 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500223 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530224 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500225 }
226
227 int operator()() const
228 {
229 return fd;
230 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530231};
232
Gunnar Mills57d9c502018-09-14 14:42:34 -0500233} // namespace phosphor