blob: e01c74449df7b4c049000ba33d1d7ec4f7c816c4 [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>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080011#include <string>
Ratan Gupta8804feb2017-05-25 10:49:57 +053012
13namespace phosphor
14{
15namespace network
16{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050017
18constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
19constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
20constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
21constexpr auto IPV4_PREFIX = "169.254";
22constexpr auto IPV6_PREFIX = "fe80";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050023constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050024
Ratan Guptabd303b12017-08-18 17:10:07 +053025namespace mac_address
26{
27
28constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
29constexpr auto localAdminMask = 0x020000000000;
30constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
31
Nagaraju Goruganti59937632017-12-05 00:06:07 -060032constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053033constexpr size_t size = 18;
34
35/** @brief validate the mac address
36 * @param[in] value - MAC address.
37 * @returns true if validate otherwise false.
38 */
39inline bool validate(const std::string& value)
40{
41 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050042 return std::regex_search(value, regexToCheck) &&
43 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053044}
45
46/** @brief gets the MAC address from the Inventory.
47 * @param[in] bus - DBUS Bus Object.
48 */
49std::string getfromInventory(sdbusplus::bus::bus& bus);
50
William A. Kennington IIId27410f2019-01-30 17:15:43 -080051/** @brief Converts the given mac address bytes into a string
52 * @param[in] bytes - The mac address
53 * @returns A valid mac address string
54 */
55std::string toString(const MacAddr& mac);
56
Ratan Guptabd303b12017-08-18 17:10:07 +053057namespace internal
58{
59/** @brief Converts the given mac address into unsigned 64 bit integer
60 * @param[in] value - MAC address.
61 * @returns converted unsigned 64 bit number.
62 */
63inline uint64_t convertToInt(const std::string& value)
64{
65 unsigned char mac[6];
66
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1,
68 mac + 2, mac + 3, mac + 4, mac + 5);
69 return static_cast<uint64_t>(mac[0]) << 40 |
70 static_cast<uint64_t>(mac[1]) << 32 |
71 static_cast<uint64_t>(mac[2]) << 24 |
72 static_cast<uint64_t>(mac[3]) << 16 |
73 static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]);
Ratan Guptabd303b12017-08-18 17:10:07 +053074}
75
William A. Kennington IIId27410f2019-01-30 17:15:43 -080076/** @brief Converts the lower nibble of a byte value to a hex digit
77 */
78inline char toHex(std::byte byte)
79{
80 uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
81 return val < 10 ? '0' + val : 'A' + (val - 10);
82}
Gunnar Mills57d9c502018-09-14 14:42:34 -050083} // namespace internal
84} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053085
Ratan Gupta497c0c92017-08-22 19:15:59 +053086constexpr auto networkdService = "systemd-networkd.service";
87constexpr auto timeSynchdService = "systemd-timesyncd.service";
88
Ratan Gupta8804feb2017-05-25 10:49:57 +053089/* @brief converts the given subnet into prefix notation.
90 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
91 * @param[in] mask - Subnet Mask.
92 * @returns prefix.
93 */
94uint8_t toCidr(int addressFamily, const std::string& mask);
95
96/* @brief converts the prefix into subnetmask.
97 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
98 * @param[in] prefix - prefix length.
99 * @returns subnet mask.
100 */
101std::string toMask(int addressFamily, uint8_t prefix);
102
103/* @brief checks that the given ip address is link local or not.
104 * @param[in] address - IP address.
105 * @returns true if it is linklocal otherwise false.
106 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500107bool isLinkLocalIP(const std::string& address);
108
109/* @brief checks that the given ip address valid or not.
110 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
111 * @param[in] address - IP address.
112 * @returns true if it is valid otherwise false.
113 */
114bool isValidIP(int addressFamily, const std::string& address);
115
116/* @brief checks that the given prefix is valid or not.
117 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
118 * @param[in] prefix - prefix length.
119 * @returns true if it is valid otherwise false.
120 */
121bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530122
Gunnar Millsd75f0492017-10-25 20:33:32 -0500123/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530124 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
125 * @param[in] ipaddress - IP address.
126 * @param[in] prefix - prefix length.
127 * @returns network section of the ipaddress.
128 */
129std::string getNetworkID(int addressFamily, const std::string& ipaddress,
130 uint8_t prefix);
131
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530132/** @brief Gets the map of interface and the associated
133 * address.
134 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530135 */
136IntfAddrMap getInterfaceAddrs();
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