blob: 1f868155e7b61428dc14c854f5063265e9801876 [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
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530127/** @brief Gets the map of interface and the associated
128 * address.
129 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530130 */
131IntfAddrMap getInterfaceAddrs();
132
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530133/** @brief Get all the interfaces from the system.
134 * @returns list of interface names.
135 */
136InterfaceList getInterfaces();
137
Ratan Guptabc886292017-07-25 18:29:57 +0530138/** @brief Delete the given interface.
139 * @param[in] intf - interface name.
140 */
141void deleteInterface(const std::string& intf);
142
Ratan Gupta56187e72017-08-13 09:40:14 +0530143/** @brief read the DHCP value from the configuration file
144 * @param[in] confDir - Network configuration directory.
145 * @param[in] intf - Interface name.
146 */
147bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530148
Ratan Guptabd303b12017-08-18 17:10:07 +0530149namespace internal
150{
151
152/* @brief runs the given command in child process.
153 * @param[in] path - path of the binary file which needs to be execeuted.
154 * @param[in] args - arguments of the command.
155 */
156void executeCommandinChildProcess(const char* path, char** args);
157
158} // namespace internal
159
160/* @brief runs the given command in child process.
161 * @param[in] path -path of the binary file which needs to be execeuted.
162 * @param[in] tArgs - arguments of the command.
163 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500164template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530165void execute(const char* path, ArgTypes&&... tArgs)
166{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700167 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530168
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530170
171 internal::executeCommandinChildProcess(path, args);
172}
173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530175
William A. Kennington IIIc920bdb2019-04-19 14:23:06 -0700176/** @brief Copies data from a buffer into a copyable type
177 *
178 * @param[in] data - The data buffer being extracted from
179 * @param[in] emsg - The message to print if extraction fails
180 * @return The copyable type with data populated
181 */
182template <typename T>
183T copyFrom(std::string_view data, const char* emsg = "Extract Failed")
184{
185 static_assert(std::is_trivially_copyable_v<T>);
186 T ret;
187 if (data.size() < sizeof(ret))
188 {
189 throw std::runtime_error(emsg);
190 }
191 std::memcpy(&ret, data.data(), sizeof(ret));
192 return ret;
193}
194
195/** @brief Extracts data from a buffer into a copyable type
196 * Updates the data buffer to show that data was removed
197 *
198 * @param[in,out] data - The data buffer being extracted from
199 * @param[in] emsg - The message to print if extraction fails
200 * @return The copyable type with data populated
201 */
202template <typename T>
203T extract(std::string_view& data, const char* emsg = "Extract Failed")
204{
205 T ret = copyFrom<T>(data, emsg);
206 data.remove_prefix(sizeof(ret));
207 return ret;
208}
209
210/** @brief Compares two of the same trivially copyable types
211 *
212 * @param[in] a - The data buffer being extracted from
213 * @param[in] b - The message to print if extraction fails
214 * @return True if the parameters are bitwise identical
215 */
216template <typename T>
217bool equal(const T& a, const T& b)
218{
219 static_assert(std::is_trivially_copyable_v<T>);
220 return memcmp(&a, &b, sizeof(T)) == 0;
221}
222
Ratan Gupta8804feb2017-05-25 10:49:57 +0530223class Descriptor
224{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500225 private:
226 /** default value */
227 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 public:
230 Descriptor() = default;
231 Descriptor(const Descriptor&) = delete;
232 Descriptor& operator=(const Descriptor&) = delete;
233 Descriptor(Descriptor&&) = delete;
234 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530235
Gunnar Mills57d9c502018-09-14 14:42:34 -0500236 explicit Descriptor(int fd) : fd(fd)
237 {
238 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530239
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240 /* @brief sets the internal file descriptor with the given descriptor
241 * and closes the old descriptor.
242 * @param[in] descriptor - File/Socket descriptor.
243 */
244 void set(int descriptor)
245 {
246 // same descriptor given
247 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530248 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500249 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530250 }
251
Gunnar Mills57d9c502018-09-14 14:42:34 -0500252 // close the old descriptor
253 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530254 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500255 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530256 }
257
Gunnar Mills57d9c502018-09-14 14:42:34 -0500258 fd = descriptor;
259 }
260
261 ~Descriptor()
262 {
263 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530264 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500265 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530266 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 }
268
269 int operator()() const
270 {
271 return fd;
272 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530273};
274
Gunnar Mills57d9c502018-09-14 14:42:34 -0500275} // namespace phosphor