blob: cf61e168b04b7634d32add3884d816715ce2e84d [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 IIId27410f2019-01-30 17:15:43 -080055/** @brief Converts the given mac address bytes into a string
56 * @param[in] bytes - The mac address
57 * @returns A valid mac address string
58 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070059std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080060
Ratan Guptabd303b12017-08-18 17:10:07 +053061namespace internal
62{
63/** @brief Converts the given mac address into unsigned 64 bit integer
64 * @param[in] value - MAC address.
65 * @returns converted unsigned 64 bit number.
66 */
67inline uint64_t convertToInt(const std::string& value)
68{
William A. Kennington III4966e962019-04-08 01:58:10 -070069 struct ether_addr* addr = ether_aton(value.c_str());
70 if (addr == nullptr)
71 {
72 return 0;
73 }
74 uint64_t ret = 0;
75 static_assert(sizeof(ret) >= sizeof(*addr));
76 std::memcpy(&ret, addr, sizeof(*addr));
77 return ret;
Ratan Guptabd303b12017-08-18 17:10:07 +053078}
79
Gunnar Mills57d9c502018-09-14 14:42:34 -050080} // namespace internal
81} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053082
Ratan Gupta497c0c92017-08-22 19:15:59 +053083constexpr auto networkdService = "systemd-networkd.service";
84constexpr auto timeSynchdService = "systemd-timesyncd.service";
85
Ratan Gupta8804feb2017-05-25 10:49:57 +053086/* @brief converts the given subnet into prefix notation.
87 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
88 * @param[in] mask - Subnet Mask.
89 * @returns prefix.
90 */
91uint8_t toCidr(int addressFamily, const std::string& mask);
92
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080093/* @brief converts a sockaddr for the specified address family into
94 * a type_safe InAddrAny.
95 * @param[in] addressFamily - The address family of the buf
96 * @param[in] buf - The network byte order address
97 */
98InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
99
William A. Kennington III5058f572019-01-30 17:18:14 -0800100/* @brief converts the ip bytes into a string representation
101 * @param[in] addr - input ip address to convert.
102 * @returns String representation of the ip.
103 */
104std::string toString(const InAddrAny& addr);
105
Ratan Gupta8804feb2017-05-25 10:49:57 +0530106/* @brief converts the prefix into subnetmask.
107 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
108 * @param[in] prefix - prefix length.
109 * @returns subnet mask.
110 */
111std::string toMask(int addressFamily, uint8_t prefix);
112
113/* @brief checks that the given ip address is link local or not.
114 * @param[in] address - IP address.
115 * @returns true if it is linklocal otherwise false.
116 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500117bool isLinkLocalIP(const std::string& address);
118
119/* @brief checks that the given ip address valid or not.
120 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
121 * @param[in] address - IP address.
122 * @returns true if it is valid otherwise false.
123 */
124bool isValidIP(int addressFamily, const std::string& address);
125
126/* @brief checks that the given prefix is valid or not.
127 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
128 * @param[in] prefix - prefix length.
129 * @returns true if it is valid otherwise false.
130 */
131bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530132
Gunnar Millsd75f0492017-10-25 20:33:32 -0500133/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530134 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
135 * @param[in] ipaddress - IP address.
136 * @param[in] prefix - prefix length.
137 * @returns network section of the ipaddress.
138 */
139std::string getNetworkID(int addressFamily, const std::string& ipaddress,
140 uint8_t prefix);
141
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530142/** @brief Gets the map of interface and the associated
143 * address.
144 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530145 */
146IntfAddrMap getInterfaceAddrs();
147
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530148/** @brief Get all the interfaces from the system.
149 * @returns list of interface names.
150 */
151InterfaceList getInterfaces();
152
Ratan Guptabc886292017-07-25 18:29:57 +0530153/** @brief Delete the given interface.
154 * @param[in] intf - interface name.
155 */
156void deleteInterface(const std::string& intf);
157
Ratan Gupta56187e72017-08-13 09:40:14 +0530158/** @brief read the DHCP value from the configuration file
159 * @param[in] confDir - Network configuration directory.
160 * @param[in] intf - Interface name.
161 */
162bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530163
Ratan Guptabd303b12017-08-18 17:10:07 +0530164namespace internal
165{
166
167/* @brief runs the given command in child process.
168 * @param[in] path - path of the binary file which needs to be execeuted.
169 * @param[in] args - arguments of the command.
170 */
171void executeCommandinChildProcess(const char* path, char** args);
172
173} // namespace internal
174
175/* @brief runs the given command in child process.
176 * @param[in] path -path of the binary file which needs to be execeuted.
177 * @param[in] tArgs - arguments of the command.
178 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530180void execute(const char* path, ArgTypes&&... tArgs)
181{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700182 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530183
Gunnar Mills57d9c502018-09-14 14:42:34 -0500184 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530185
186 internal::executeCommandinChildProcess(path, args);
187}
188
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530190
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -0700191/** @brief Copies data from a buffer into a copyable type
192 *
193 * @param[in] data - The data buffer being extracted from
194 * @param[in] emsg - The message to print if extraction fails
195 * @return The copyable type with data populated
196 */
197template <typename T>
198T copyFrom(std::string_view data, const char* emsg = "Extract Failed")
199{
200 static_assert(std::is_trivially_copyable_v<T>);
201 T ret;
202 if (data.size() < sizeof(ret))
203 {
204 throw std::runtime_error(emsg);
205 }
206 std::memcpy(&ret, data.data(), sizeof(ret));
207 return ret;
208}
209
210/** @brief Extracts data from a buffer into a copyable type
211 * Updates the data buffer to show that data was removed
212 *
213 * @param[in,out] data - The data buffer being extracted from
214 * @param[in] emsg - The message to print if extraction fails
215 * @return The copyable type with data populated
216 */
217template <typename T>
218T extract(std::string_view& data, const char* emsg = "Extract Failed")
219{
220 T ret = copyFrom<T>(data, emsg);
221 data.remove_prefix(sizeof(ret));
222 return ret;
223}
224
225/** @brief Compares two of the same trivially copyable types
226 *
227 * @param[in] a - The data buffer being extracted from
228 * @param[in] b - The message to print if extraction fails
229 * @return True if the parameters are bitwise identical
230 */
231template <typename T>
232bool equal(const T& a, const T& b)
233{
234 static_assert(std::is_trivially_copyable_v<T>);
235 return memcmp(&a, &b, sizeof(T)) == 0;
236}
237
Ratan Gupta8804feb2017-05-25 10:49:57 +0530238class Descriptor
239{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240 private:
241 /** default value */
242 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530243
Gunnar Mills57d9c502018-09-14 14:42:34 -0500244 public:
245 Descriptor() = default;
246 Descriptor(const Descriptor&) = delete;
247 Descriptor& operator=(const Descriptor&) = delete;
248 Descriptor(Descriptor&&) = delete;
249 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530250
Gunnar Mills57d9c502018-09-14 14:42:34 -0500251 explicit Descriptor(int fd) : fd(fd)
252 {
253 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530254
Gunnar Mills57d9c502018-09-14 14:42:34 -0500255 /* @brief sets the internal file descriptor with the given descriptor
256 * and closes the old descriptor.
257 * @param[in] descriptor - File/Socket descriptor.
258 */
259 void set(int descriptor)
260 {
261 // same descriptor given
262 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530263 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500264 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530265 }
266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 // close the old descriptor
268 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530269 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500270 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530271 }
272
Gunnar Mills57d9c502018-09-14 14:42:34 -0500273 fd = descriptor;
274 }
275
276 ~Descriptor()
277 {
278 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530279 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500280 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530281 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500282 }
283
284 int operator()() const
285 {
286 return fd;
287 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530288};
289
Gunnar Mills57d9c502018-09-14 14:42:34 -0500290} // namespace phosphor