blob: 723c941e1bcd205165bb159fd7a257b9545eb4ec [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
William A. Kennington III4966e962019-04-08 01:58:10 -07007#include <netinet/ether.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <unistd.h>
9
William A. Kennington III4966e962019-04-08 01:58:10 -070010#include <cstring>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <sdbusplus/bus.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080012#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080013#include <string_view>
Ratan Gupta8804feb2017-05-25 10:49:57 +053014
15namespace phosphor
16{
17namespace network
18{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050019
20constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
21constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
22constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
23constexpr auto IPV4_PREFIX = "169.254";
24constexpr auto IPV6_PREFIX = "fe80";
25
Ratan Guptabd303b12017-08-18 17:10:07 +053026namespace mac_address
27{
28
Ratan Guptabd303b12017-08-18 17:10:07 +053029constexpr 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{
William A. Kennington III4966e962019-04-08 01:58:10 -070041 struct ether_addr* addr = ether_aton(value.c_str());
42 if (addr == nullptr)
43 {
44 return false;
45 }
46 ether_addr zero{};
47 return std::memcmp(addr, &zero, sizeof(zero)) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053048}
49
50/** @brief gets the MAC address from the Inventory.
51 * @param[in] bus - DBUS Bus Object.
52 */
53std::string getfromInventory(sdbusplus::bus::bus& bus);
54
William A. Kennington IIIa14879e2019-02-01 21:43:11 -080055/* @brief Marshalls the bytes for a mac address into a MacAddr.
56 * @param[in] buf - The network byte order address
57 */
58MacAddr fromBuf(std::string_view buf);
59
William A. Kennington IIId27410f2019-01-30 17:15:43 -080060/** @brief Converts the given mac address bytes into a string
61 * @param[in] bytes - The mac address
62 * @returns A valid mac address string
63 */
64std::string toString(const MacAddr& mac);
65
Ratan Guptabd303b12017-08-18 17:10:07 +053066namespace internal
67{
68/** @brief Converts the given mac address into unsigned 64 bit integer
69 * @param[in] value - MAC address.
70 * @returns converted unsigned 64 bit number.
71 */
72inline uint64_t convertToInt(const std::string& value)
73{
William A. Kennington III4966e962019-04-08 01:58:10 -070074 struct ether_addr* addr = ether_aton(value.c_str());
75 if (addr == nullptr)
76 {
77 return 0;
78 }
79 uint64_t ret = 0;
80 static_assert(sizeof(ret) >= sizeof(*addr));
81 std::memcpy(&ret, addr, sizeof(*addr));
82 return ret;
Ratan Guptabd303b12017-08-18 17:10:07 +053083}
84
William A. Kennington IIId27410f2019-01-30 17:15:43 -080085/** @brief Converts the lower nibble of a byte value to a hex digit
86 */
87inline char toHex(std::byte byte)
88{
89 uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
90 return val < 10 ? '0' + val : 'A' + (val - 10);
91}
Gunnar Mills57d9c502018-09-14 14:42:34 -050092} // namespace internal
93} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053094
Ratan Gupta497c0c92017-08-22 19:15:59 +053095constexpr auto networkdService = "systemd-networkd.service";
96constexpr auto timeSynchdService = "systemd-timesyncd.service";
97
Ratan Gupta8804feb2017-05-25 10:49:57 +053098/* @brief converts the given subnet into prefix notation.
99 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
100 * @param[in] mask - Subnet Mask.
101 * @returns prefix.
102 */
103uint8_t toCidr(int addressFamily, const std::string& mask);
104
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800105/* @brief converts a sockaddr for the specified address family into
106 * a type_safe InAddrAny.
107 * @param[in] addressFamily - The address family of the buf
108 * @param[in] buf - The network byte order address
109 */
110InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
111
William A. Kennington III5058f572019-01-30 17:18:14 -0800112/* @brief converts the ip bytes into a string representation
113 * @param[in] addr - input ip address to convert.
114 * @returns String representation of the ip.
115 */
116std::string toString(const InAddrAny& addr);
117
Ratan Gupta8804feb2017-05-25 10:49:57 +0530118/* @brief converts the prefix into subnetmask.
119 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
120 * @param[in] prefix - prefix length.
121 * @returns subnet mask.
122 */
123std::string toMask(int addressFamily, uint8_t prefix);
124
125/* @brief checks that the given ip address is link local or not.
126 * @param[in] address - IP address.
127 * @returns true if it is linklocal otherwise false.
128 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500129bool isLinkLocalIP(const std::string& address);
130
131/* @brief checks that the given ip address valid or not.
132 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
133 * @param[in] address - IP address.
134 * @returns true if it is valid otherwise false.
135 */
136bool isValidIP(int addressFamily, const std::string& address);
137
138/* @brief checks that the given prefix is valid or not.
139 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
140 * @param[in] prefix - prefix length.
141 * @returns true if it is valid otherwise false.
142 */
143bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530144
Gunnar Millsd75f0492017-10-25 20:33:32 -0500145/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530146 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
147 * @param[in] ipaddress - IP address.
148 * @param[in] prefix - prefix length.
149 * @returns network section of the ipaddress.
150 */
151std::string getNetworkID(int addressFamily, const std::string& ipaddress,
152 uint8_t prefix);
153
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530154/** @brief Gets the map of interface and the associated
155 * address.
156 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530157 */
158IntfAddrMap getInterfaceAddrs();
159
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530160/** @brief Get all the interfaces from the system.
161 * @returns list of interface names.
162 */
163InterfaceList getInterfaces();
164
Ratan Guptabc886292017-07-25 18:29:57 +0530165/** @brief Delete the given interface.
166 * @param[in] intf - interface name.
167 */
168void deleteInterface(const std::string& intf);
169
Ratan Gupta56187e72017-08-13 09:40:14 +0530170/** @brief read the DHCP value from the configuration file
171 * @param[in] confDir - Network configuration directory.
172 * @param[in] intf - Interface name.
173 */
174bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530175
Ratan Guptabd303b12017-08-18 17:10:07 +0530176namespace internal
177{
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] args - arguments of the command.
182 */
183void executeCommandinChildProcess(const char* path, char** args);
184
185} // namespace internal
186
187/* @brief runs the given command in child process.
188 * @param[in] path -path of the binary file which needs to be execeuted.
189 * @param[in] tArgs - arguments of the command.
190 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500191template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530192void execute(const char* path, ArgTypes&&... tArgs)
193{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700194 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530195
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530197
198 internal::executeCommandinChildProcess(path, args);
199}
200
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530202
203class Descriptor
204{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500205 private:
206 /** default value */
207 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530208
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209 public:
210 Descriptor() = default;
211 Descriptor(const Descriptor&) = delete;
212 Descriptor& operator=(const Descriptor&) = delete;
213 Descriptor(Descriptor&&) = delete;
214 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530215
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216 explicit Descriptor(int fd) : fd(fd)
217 {
218 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530219
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 /* @brief sets the internal file descriptor with the given descriptor
221 * and closes the old descriptor.
222 * @param[in] descriptor - File/Socket descriptor.
223 */
224 void set(int descriptor)
225 {
226 // same descriptor given
227 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530228 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530230 }
231
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232 // close the old descriptor
233 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530234 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500235 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530236 }
237
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 fd = descriptor;
239 }
240
241 ~Descriptor()
242 {
243 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530244 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530246 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247 }
248
249 int operator()() const
250 {
251 return fd;
252 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530253};
254
Gunnar Mills57d9c502018-09-14 14:42:34 -0500255} // namespace phosphor