blob: a07fa49c98c886967ab9e4aa40395daf3f875d3a [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 IIId27410f2019-01-30 17:15:43 -080052/** @brief Converts the given mac address bytes into a string
53 * @param[in] bytes - The mac address
54 * @returns A valid mac address string
55 */
56std::string toString(const MacAddr& mac);
57
Ratan Guptabd303b12017-08-18 17:10:07 +053058namespace internal
59{
60/** @brief Converts the given mac address into unsigned 64 bit integer
61 * @param[in] value - MAC address.
62 * @returns converted unsigned 64 bit number.
63 */
64inline uint64_t convertToInt(const std::string& value)
65{
66 unsigned char mac[6];
67
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1,
69 mac + 2, mac + 3, mac + 4, mac + 5);
70 return static_cast<uint64_t>(mac[0]) << 40 |
71 static_cast<uint64_t>(mac[1]) << 32 |
72 static_cast<uint64_t>(mac[2]) << 24 |
73 static_cast<uint64_t>(mac[3]) << 16 |
74 static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]);
Ratan Guptabd303b12017-08-18 17:10:07 +053075}
76
William A. Kennington IIId27410f2019-01-30 17:15:43 -080077/** @brief Converts the lower nibble of a byte value to a hex digit
78 */
79inline char toHex(std::byte byte)
80{
81 uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
82 return val < 10 ? '0' + val : 'A' + (val - 10);
83}
Gunnar Mills57d9c502018-09-14 14:42:34 -050084} // namespace internal
85} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053086
Ratan Gupta497c0c92017-08-22 19:15:59 +053087constexpr auto networkdService = "systemd-networkd.service";
88constexpr auto timeSynchdService = "systemd-timesyncd.service";
89
Ratan Gupta8804feb2017-05-25 10:49:57 +053090/* @brief converts the given subnet into prefix notation.
91 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
92 * @param[in] mask - Subnet Mask.
93 * @returns prefix.
94 */
95uint8_t toCidr(int addressFamily, const std::string& mask);
96
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080097/* @brief converts a sockaddr for the specified address family into
98 * a type_safe InAddrAny.
99 * @param[in] addressFamily - The address family of the buf
100 * @param[in] buf - The network byte order address
101 */
102InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
103
William A. Kennington III5058f572019-01-30 17:18:14 -0800104/* @brief converts the ip bytes into a string representation
105 * @param[in] addr - input ip address to convert.
106 * @returns String representation of the ip.
107 */
108std::string toString(const InAddrAny& addr);
109
Ratan Gupta8804feb2017-05-25 10:49:57 +0530110/* @brief converts the prefix into subnetmask.
111 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
112 * @param[in] prefix - prefix length.
113 * @returns subnet mask.
114 */
115std::string toMask(int addressFamily, uint8_t prefix);
116
117/* @brief checks that the given ip address is link local or not.
118 * @param[in] address - IP address.
119 * @returns true if it is linklocal otherwise false.
120 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500121bool isLinkLocalIP(const std::string& address);
122
123/* @brief checks that the given ip address valid or not.
124 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
125 * @param[in] address - IP address.
126 * @returns true if it is valid otherwise false.
127 */
128bool isValidIP(int addressFamily, const std::string& address);
129
130/* @brief checks that the given prefix is valid or not.
131 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
132 * @param[in] prefix - prefix length.
133 * @returns true if it is valid otherwise false.
134 */
135bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530136
Gunnar Millsd75f0492017-10-25 20:33:32 -0500137/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530138 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
139 * @param[in] ipaddress - IP address.
140 * @param[in] prefix - prefix length.
141 * @returns network section of the ipaddress.
142 */
143std::string getNetworkID(int addressFamily, const std::string& ipaddress,
144 uint8_t prefix);
145
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530146/** @brief Gets the map of interface and the associated
147 * address.
148 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530149 */
150IntfAddrMap getInterfaceAddrs();
151
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530152/** @brief Get all the interfaces from the system.
153 * @returns list of interface names.
154 */
155InterfaceList getInterfaces();
156
Ratan Guptabc886292017-07-25 18:29:57 +0530157/** @brief Delete the given interface.
158 * @param[in] intf - interface name.
159 */
160void deleteInterface(const std::string& intf);
161
Ratan Gupta56187e72017-08-13 09:40:14 +0530162/** @brief read the DHCP value from the configuration file
163 * @param[in] confDir - Network configuration directory.
164 * @param[in] intf - Interface name.
165 */
166bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530167
Ratan Guptabd303b12017-08-18 17:10:07 +0530168namespace internal
169{
170
171/* @brief runs the given command in child process.
172 * @param[in] path - path of the binary file which needs to be execeuted.
173 * @param[in] args - arguments of the command.
174 */
175void executeCommandinChildProcess(const char* path, char** args);
176
177} // namespace internal
178
179/* @brief runs the given command in child process.
180 * @param[in] path -path of the binary file which needs to be execeuted.
181 * @param[in] tArgs - arguments of the command.
182 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530184void execute(const char* path, ArgTypes&&... tArgs)
185{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 using expandType = char* [];
Ratan Guptabd303b12017-08-18 17:10:07 +0530187
Gunnar Mills57d9c502018-09-14 14:42:34 -0500188 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530189
190 internal::executeCommandinChildProcess(path, args);
191}
192
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530194
195class Descriptor
196{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500197 private:
198 /** default value */
199 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530200
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201 public:
202 Descriptor() = default;
203 Descriptor(const Descriptor&) = delete;
204 Descriptor& operator=(const Descriptor&) = delete;
205 Descriptor(Descriptor&&) = delete;
206 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530207
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208 explicit Descriptor(int fd) : fd(fd)
209 {
210 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 /* @brief sets the internal file descriptor with the given descriptor
213 * and closes the old descriptor.
214 * @param[in] descriptor - File/Socket descriptor.
215 */
216 void set(int descriptor)
217 {
218 // same descriptor given
219 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530220 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500221 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530222 }
223
Gunnar Mills57d9c502018-09-14 14:42:34 -0500224 // close the old descriptor
225 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530226 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500227 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530228 }
229
Gunnar Mills57d9c502018-09-14 14:42:34 -0500230 fd = descriptor;
231 }
232
233 ~Descriptor()
234 {
235 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530236 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500237 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530238 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500239 }
240
241 int operator()() const
242 {
243 return fd;
244 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530245};
246
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247} // namespace phosphor