blob: 9531d4fc80de6560a11eb50858b349b61846fda4 [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>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080012#include <string_view>
Ratan Gupta8804feb2017-05-25 10:49:57 +053013
14namespace phosphor
15{
16namespace network
17{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050018
19constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
20constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
21constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
22constexpr auto IPV4_PREFIX = "169.254";
23constexpr auto IPV6_PREFIX = "fe80";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050024constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050025
Ratan Guptabd303b12017-08-18 17:10:07 +053026namespace mac_address
27{
28
29constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
30constexpr auto localAdminMask = 0x020000000000;
31constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
32
Nagaraju Goruganti59937632017-12-05 00:06:07 -060033constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053034constexpr size_t size = 18;
35
36/** @brief validate the mac address
37 * @param[in] value - MAC address.
38 * @returns true if validate otherwise false.
39 */
40inline bool validate(const std::string& value)
41{
42 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050043 return std::regex_search(value, regexToCheck) &&
44 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053045}
46
47/** @brief gets the MAC address from the Inventory.
48 * @param[in] bus - DBUS Bus Object.
49 */
50std::string getfromInventory(sdbusplus::bus::bus& bus);
51
William A. Kennington IIIa14879e2019-02-01 21:43:11 -080052/* @brief Marshalls the bytes for a mac address into a MacAddr.
53 * @param[in] buf - The network byte order address
54 */
55MacAddr fromBuf(std::string_view buf);
56
William A. Kennington IIId27410f2019-01-30 17:15:43 -080057/** @brief Converts the given mac address bytes into a string
58 * @param[in] bytes - The mac address
59 * @returns A valid mac address string
60 */
61std::string toString(const MacAddr& mac);
62
Ratan Guptabd303b12017-08-18 17:10:07 +053063namespace internal
64{
65/** @brief Converts the given mac address into unsigned 64 bit integer
66 * @param[in] value - MAC address.
67 * @returns converted unsigned 64 bit number.
68 */
69inline uint64_t convertToInt(const std::string& value)
70{
71 unsigned char mac[6];
72
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1,
74 mac + 2, mac + 3, mac + 4, mac + 5);
75 return static_cast<uint64_t>(mac[0]) << 40 |
76 static_cast<uint64_t>(mac[1]) << 32 |
77 static_cast<uint64_t>(mac[2]) << 24 |
78 static_cast<uint64_t>(mac[3]) << 16 |
79 static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]);
Ratan Guptabd303b12017-08-18 17:10:07 +053080}
81
William A. Kennington IIId27410f2019-01-30 17:15:43 -080082/** @brief Converts the lower nibble of a byte value to a hex digit
83 */
84inline char toHex(std::byte byte)
85{
86 uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
87 return val < 10 ? '0' + val : 'A' + (val - 10);
88}
Gunnar Mills57d9c502018-09-14 14:42:34 -050089} // namespace internal
90} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053091
Ratan Gupta497c0c92017-08-22 19:15:59 +053092constexpr auto networkdService = "systemd-networkd.service";
93constexpr auto timeSynchdService = "systemd-timesyncd.service";
94
Ratan Gupta8804feb2017-05-25 10:49:57 +053095/* @brief converts the given subnet into prefix notation.
96 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
97 * @param[in] mask - Subnet Mask.
98 * @returns prefix.
99 */
100uint8_t toCidr(int addressFamily, const std::string& mask);
101
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800102/* @brief converts a sockaddr for the specified address family into
103 * a type_safe InAddrAny.
104 * @param[in] addressFamily - The address family of the buf
105 * @param[in] buf - The network byte order address
106 */
107InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
108
William A. Kennington III5058f572019-01-30 17:18:14 -0800109/* @brief converts the ip bytes into a string representation
110 * @param[in] addr - input ip address to convert.
111 * @returns String representation of the ip.
112 */
113std::string toString(const InAddrAny& addr);
114
Ratan Gupta8804feb2017-05-25 10:49:57 +0530115/* @brief converts the prefix into subnetmask.
116 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
117 * @param[in] prefix - prefix length.
118 * @returns subnet mask.
119 */
120std::string toMask(int addressFamily, uint8_t prefix);
121
122/* @brief checks that the given ip address is link local or not.
123 * @param[in] address - IP address.
124 * @returns true if it is linklocal otherwise false.
125 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500126bool isLinkLocalIP(const std::string& address);
127
128/* @brief checks that the given ip address valid or not.
129 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
130 * @param[in] address - IP address.
131 * @returns true if it is valid otherwise false.
132 */
133bool isValidIP(int addressFamily, const std::string& address);
134
135/* @brief checks that the given prefix is valid or not.
136 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
137 * @param[in] prefix - prefix length.
138 * @returns true if it is valid otherwise false.
139 */
140bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530141
Gunnar Millsd75f0492017-10-25 20:33:32 -0500142/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530143 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
144 * @param[in] ipaddress - IP address.
145 * @param[in] prefix - prefix length.
146 * @returns network section of the ipaddress.
147 */
148std::string getNetworkID(int addressFamily, const std::string& ipaddress,
149 uint8_t prefix);
150
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530151/** @brief Gets the map of interface and the associated
152 * address.
153 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530154 */
155IntfAddrMap getInterfaceAddrs();
156
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530157/** @brief Get all the interfaces from the system.
158 * @returns list of interface names.
159 */
160InterfaceList getInterfaces();
161
Ratan Guptabc886292017-07-25 18:29:57 +0530162/** @brief Delete the given interface.
163 * @param[in] intf - interface name.
164 */
165void deleteInterface(const std::string& intf);
166
Ratan Gupta56187e72017-08-13 09:40:14 +0530167/** @brief read the DHCP value from the configuration file
168 * @param[in] confDir - Network configuration directory.
169 * @param[in] intf - Interface name.
170 */
171bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530172
Ratan Guptabd303b12017-08-18 17:10:07 +0530173namespace internal
174{
175
176/* @brief runs the given command in child process.
177 * @param[in] path - path of the binary file which needs to be execeuted.
178 * @param[in] args - arguments of the command.
179 */
180void executeCommandinChildProcess(const char* path, char** args);
181
182} // namespace internal
183
184/* @brief runs the given command in child process.
185 * @param[in] path -path of the binary file which needs to be execeuted.
186 * @param[in] tArgs - arguments of the command.
187 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500188template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530189void execute(const char* path, ArgTypes&&... tArgs)
190{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500191 using expandType = char* [];
Ratan Guptabd303b12017-08-18 17:10:07 +0530192
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530194
195 internal::executeCommandinChildProcess(path, args);
196}
197
Gunnar Mills57d9c502018-09-14 14:42:34 -0500198} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530199
200class Descriptor
201{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 private:
203 /** default value */
204 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 public:
207 Descriptor() = default;
208 Descriptor(const Descriptor&) = delete;
209 Descriptor& operator=(const Descriptor&) = delete;
210 Descriptor(Descriptor&&) = delete;
211 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530212
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213 explicit Descriptor(int fd) : fd(fd)
214 {
215 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530216
Gunnar Mills57d9c502018-09-14 14:42:34 -0500217 /* @brief sets the internal file descriptor with the given descriptor
218 * and closes the old descriptor.
219 * @param[in] descriptor - File/Socket descriptor.
220 */
221 void set(int descriptor)
222 {
223 // same descriptor given
224 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530225 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530227 }
228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 // close the old descriptor
230 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530231 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530233 }
234
Gunnar Mills57d9c502018-09-14 14:42:34 -0500235 fd = descriptor;
236 }
237
238 ~Descriptor()
239 {
240 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530241 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500242 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530243 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500244 }
245
246 int operator()() const
247 {
248 return fd;
249 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530250};
251
Gunnar Mills57d9c502018-09-14 14:42:34 -0500252} // namespace phosphor