blob: fe9444478de4173303817113245e469534229fc0 [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
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -0700203/** @brief Copies data from a buffer into a copyable type
204 *
205 * @param[in] data - The data buffer being extracted from
206 * @param[in] emsg - The message to print if extraction fails
207 * @return The copyable type with data populated
208 */
209template <typename T>
210T copyFrom(std::string_view data, const char* emsg = "Extract Failed")
211{
212 static_assert(std::is_trivially_copyable_v<T>);
213 T ret;
214 if (data.size() < sizeof(ret))
215 {
216 throw std::runtime_error(emsg);
217 }
218 std::memcpy(&ret, data.data(), sizeof(ret));
219 return ret;
220}
221
222/** @brief Extracts data from a buffer into a copyable type
223 * Updates the data buffer to show that data was removed
224 *
225 * @param[in,out] data - The data buffer being extracted from
226 * @param[in] emsg - The message to print if extraction fails
227 * @return The copyable type with data populated
228 */
229template <typename T>
230T extract(std::string_view& data, const char* emsg = "Extract Failed")
231{
232 T ret = copyFrom<T>(data, emsg);
233 data.remove_prefix(sizeof(ret));
234 return ret;
235}
236
237/** @brief Compares two of the same trivially copyable types
238 *
239 * @param[in] a - The data buffer being extracted from
240 * @param[in] b - The message to print if extraction fails
241 * @return True if the parameters are bitwise identical
242 */
243template <typename T>
244bool equal(const T& a, const T& b)
245{
246 static_assert(std::is_trivially_copyable_v<T>);
247 return memcmp(&a, &b, sizeof(T)) == 0;
248}
249
Ratan Gupta8804feb2017-05-25 10:49:57 +0530250class Descriptor
251{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500252 private:
253 /** default value */
254 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530255
Gunnar Mills57d9c502018-09-14 14:42:34 -0500256 public:
257 Descriptor() = default;
258 Descriptor(const Descriptor&) = delete;
259 Descriptor& operator=(const Descriptor&) = delete;
260 Descriptor(Descriptor&&) = delete;
261 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530262
Gunnar Mills57d9c502018-09-14 14:42:34 -0500263 explicit Descriptor(int fd) : fd(fd)
264 {
265 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 /* @brief sets the internal file descriptor with the given descriptor
268 * and closes the old descriptor.
269 * @param[in] descriptor - File/Socket descriptor.
270 */
271 void set(int descriptor)
272 {
273 // same descriptor given
274 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530275 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500276 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530277 }
278
Gunnar Mills57d9c502018-09-14 14:42:34 -0500279 // close the old descriptor
280 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530281 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500282 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530283 }
284
Gunnar Mills57d9c502018-09-14 14:42:34 -0500285 fd = descriptor;
286 }
287
288 ~Descriptor()
289 {
290 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530291 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500292 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530293 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500294 }
295
296 int operator()() const
297 {
298 return fd;
299 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530300};
301
Gunnar Mills57d9c502018-09-14 14:42:34 -0500302} // namespace phosphor