blob: 79593c5c763c347c401c3e21d5093a9c77e6a722 [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 +053029/** @brief gets the MAC address from the Inventory.
30 * @param[in] bus - DBUS Bus Object.
31 */
William A. Kennington III1137a972019-04-20 20:49:58 -070032ether_addr getfromInventory(sdbusplus::bus::bus& bus);
33
34/** @brief Converts the given mac address into byte form
35 * @param[in] str - The mac address in human readable form
36 * @returns A mac address in network byte order
37 * @throws std::runtime_error for bad mac
38 */
39ether_addr fromString(const char* str);
40inline ether_addr fromString(const std::string& str)
41{
42 return fromString(str.c_str());
43}
Ratan Guptabd303b12017-08-18 17:10:07 +053044
William A. Kennington IIId27410f2019-01-30 17:15:43 -080045/** @brief Converts the given mac address bytes into a string
William A. Kennington III1137a972019-04-20 20:49:58 -070046 * @param[in] mac - The mac address
William A. Kennington IIId27410f2019-01-30 17:15:43 -080047 * @returns A valid mac address string
48 */
William A. Kennington III6ca08d82019-04-20 16:04:18 -070049std::string toString(const ether_addr& mac);
William A. Kennington IIId27410f2019-01-30 17:15:43 -080050
William A. Kennington III1137a972019-04-20 20:49:58 -070051/** @brief Determines if the mac address is empty
52 * @param[in] mac - The mac address
53 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053054 */
William A. Kennington III1137a972019-04-20 20:49:58 -070055bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053056
William A. Kennington III1137a972019-04-20 20:49:58 -070057/** @brief Determines if the mac address is a multicast address
58 * @param[in] mac - The mac address
59 * @return True if multicast bit is set
60 */
61bool isMulticast(const ether_addr& mac);
62
63/** @brief Determines if the mac address is a unicast address
64 * @param[in] mac - The mac address
65 * @return True if not multicast or empty
66 */
67bool isUnicast(const ether_addr& mac);
68
69/** @brief Determines if the mac address is locally managed
70 * @param[in] mac - The mac address
71 * @return True if local admin bit is set
72 */
73bool isLocalAdmin(const ether_addr& mac);
74
Gunnar Mills57d9c502018-09-14 14:42:34 -050075} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053076
Ratan Gupta497c0c92017-08-22 19:15:59 +053077constexpr auto networkdService = "systemd-networkd.service";
78constexpr auto timeSynchdService = "systemd-timesyncd.service";
79
Ratan Gupta8804feb2017-05-25 10:49:57 +053080/* @brief converts the given subnet into prefix notation.
81 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
82 * @param[in] mask - Subnet Mask.
83 * @returns prefix.
84 */
85uint8_t toCidr(int addressFamily, const std::string& mask);
86
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080087/* @brief converts a sockaddr for the specified address family into
88 * a type_safe InAddrAny.
89 * @param[in] addressFamily - The address family of the buf
90 * @param[in] buf - The network byte order address
91 */
92InAddrAny addrFromBuf(int addressFamily, std::string_view buf);
93
William A. Kennington III5058f572019-01-30 17:18:14 -080094/* @brief converts the ip bytes into a string representation
95 * @param[in] addr - input ip address to convert.
96 * @returns String representation of the ip.
97 */
98std::string toString(const InAddrAny& addr);
99
Ratan Gupta8804feb2017-05-25 10:49:57 +0530100/* @brief converts the prefix into subnetmask.
101 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
102 * @param[in] prefix - prefix length.
103 * @returns subnet mask.
104 */
105std::string toMask(int addressFamily, uint8_t prefix);
106
107/* @brief checks that the given ip address is link local or not.
108 * @param[in] address - IP address.
109 * @returns true if it is linklocal otherwise false.
110 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500111bool isLinkLocalIP(const std::string& address);
112
113/* @brief checks that the given ip address valid or not.
114 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
115 * @param[in] address - IP address.
116 * @returns true if it is valid otherwise false.
117 */
118bool isValidIP(int addressFamily, const std::string& address);
119
120/* @brief checks that the given prefix is valid or not.
121 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
122 * @param[in] prefix - prefix length.
123 * @returns true if it is valid otherwise false.
124 */
125bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530126
Gunnar Millsd75f0492017-10-25 20:33:32 -0500127/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530128 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
129 * @param[in] ipaddress - IP address.
130 * @param[in] prefix - prefix length.
131 * @returns network section of the ipaddress.
132 */
133std::string getNetworkID(int addressFamily, const std::string& ipaddress,
134 uint8_t prefix);
135
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530136/** @brief Gets the map of interface and the associated
137 * address.
138 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530139 */
140IntfAddrMap getInterfaceAddrs();
141
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530142/** @brief Get all the interfaces from the system.
143 * @returns list of interface names.
144 */
145InterfaceList getInterfaces();
146
Ratan Guptabc886292017-07-25 18:29:57 +0530147/** @brief Delete the given interface.
148 * @param[in] intf - interface name.
149 */
150void deleteInterface(const std::string& intf);
151
Ratan Gupta56187e72017-08-13 09:40:14 +0530152/** @brief read the DHCP value from the configuration file
153 * @param[in] confDir - Network configuration directory.
154 * @param[in] intf - Interface name.
155 */
156bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530157
Ratan Guptabd303b12017-08-18 17:10:07 +0530158namespace internal
159{
160
161/* @brief runs the given command in child process.
162 * @param[in] path - path of the binary file which needs to be execeuted.
163 * @param[in] args - arguments of the command.
164 */
165void executeCommandinChildProcess(const char* path, char** args);
166
167} // namespace internal
168
169/* @brief runs the given command in child process.
170 * @param[in] path -path of the binary file which needs to be execeuted.
171 * @param[in] tArgs - arguments of the command.
172 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530174void execute(const char* path, ArgTypes&&... tArgs)
175{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700176 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530177
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530179
180 internal::executeCommandinChildProcess(path, args);
181}
182
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530184
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -0700185/** @brief Copies data from a buffer into a copyable type
186 *
187 * @param[in] data - The data buffer being extracted from
188 * @param[in] emsg - The message to print if extraction fails
189 * @return The copyable type with data populated
190 */
191template <typename T>
192T copyFrom(std::string_view data, const char* emsg = "Extract Failed")
193{
194 static_assert(std::is_trivially_copyable_v<T>);
195 T ret;
196 if (data.size() < sizeof(ret))
197 {
198 throw std::runtime_error(emsg);
199 }
200 std::memcpy(&ret, data.data(), sizeof(ret));
201 return ret;
202}
203
204/** @brief Extracts data from a buffer into a copyable type
205 * Updates the data buffer to show that data was removed
206 *
207 * @param[in,out] data - The data buffer being extracted from
208 * @param[in] emsg - The message to print if extraction fails
209 * @return The copyable type with data populated
210 */
211template <typename T>
212T extract(std::string_view& data, const char* emsg = "Extract Failed")
213{
214 T ret = copyFrom<T>(data, emsg);
215 data.remove_prefix(sizeof(ret));
216 return ret;
217}
218
219/** @brief Compares two of the same trivially copyable types
220 *
221 * @param[in] a - The data buffer being extracted from
222 * @param[in] b - The message to print if extraction fails
223 * @return True if the parameters are bitwise identical
224 */
225template <typename T>
226bool equal(const T& a, const T& b)
227{
228 static_assert(std::is_trivially_copyable_v<T>);
229 return memcmp(&a, &b, sizeof(T)) == 0;
230}
231
Ratan Gupta8804feb2017-05-25 10:49:57 +0530232class Descriptor
233{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500234 private:
235 /** default value */
236 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530237
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 public:
239 Descriptor() = default;
240 Descriptor(const Descriptor&) = delete;
241 Descriptor& operator=(const Descriptor&) = delete;
242 Descriptor(Descriptor&&) = delete;
243 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530244
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 explicit Descriptor(int fd) : fd(fd)
246 {
247 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530248
Gunnar Mills57d9c502018-09-14 14:42:34 -0500249 /* @brief sets the internal file descriptor with the given descriptor
250 * and closes the old descriptor.
251 * @param[in] descriptor - File/Socket descriptor.
252 */
253 void set(int descriptor)
254 {
255 // same descriptor given
256 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530257 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500258 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530259 }
260
Gunnar Mills57d9c502018-09-14 14:42:34 -0500261 // close the old descriptor
262 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530263 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500264 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530265 }
266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 fd = descriptor;
268 }
269
270 ~Descriptor()
271 {
272 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530273 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500274 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530275 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500276 }
277
278 int operator()() const
279 {
280 return fd;
281 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530282};
283
Gunnar Mills57d9c502018-09-14 14:42:34 -0500284} // namespace phosphor