blob: 51c2e04e6ef09d35cb32448faa6a34d4f0145b36 [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
7#include <unistd.h>
8
Ratan Guptabd303b12017-08-18 17:10:07 +05309#include <regex>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <sdbusplus/bus.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053011
12namespace phosphor
13{
14namespace network
15{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050016
17constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
18constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
19constexpr auto IPV6_MAX_PREFIX_LENGTH = 64;
20constexpr auto IPV4_PREFIX = "169.254";
21constexpr auto IPV6_PREFIX = "fe80";
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050022constexpr auto ZEROMACADDRESS = "00:00:00:00:00:00";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050023
Ratan Guptabd303b12017-08-18 17:10:07 +053024namespace mac_address
25{
26
27constexpr auto regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
28constexpr auto localAdminMask = 0x020000000000;
29constexpr auto broadcastMac = 0xFFFFFFFFFFFF;
30
Nagaraju Goruganti59937632017-12-05 00:06:07 -060031constexpr auto format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Guptabd303b12017-08-18 17:10:07 +053032constexpr size_t size = 18;
33
34/** @brief validate the mac address
35 * @param[in] value - MAC address.
36 * @returns true if validate otherwise false.
37 */
38inline bool validate(const std::string& value)
39{
40 std::regex regexToCheck(regex);
Nagaraju Goruganti98fc5882017-10-31 04:40:12 -050041 return std::regex_search(value, regexToCheck) &&
42 value.find(ZEROMACADDRESS) != 0;
Ratan Guptabd303b12017-08-18 17:10:07 +053043}
44
45/** @brief gets the MAC address from the Inventory.
46 * @param[in] bus - DBUS Bus Object.
47 */
48std::string getfromInventory(sdbusplus::bus::bus& bus);
49
50namespace internal
51{
52/** @brief Converts the given mac address into unsigned 64 bit integer
53 * @param[in] value - MAC address.
54 * @returns converted unsigned 64 bit number.
55 */
56inline uint64_t convertToInt(const std::string& value)
57{
58 unsigned char mac[6];
59
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 sscanf(value.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac + 0, mac + 1,
61 mac + 2, mac + 3, mac + 4, mac + 5);
62 return static_cast<uint64_t>(mac[0]) << 40 |
63 static_cast<uint64_t>(mac[1]) << 32 |
64 static_cast<uint64_t>(mac[2]) << 24 |
65 static_cast<uint64_t>(mac[3]) << 16 |
66 static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]);
Ratan Guptabd303b12017-08-18 17:10:07 +053067}
68
Gunnar Mills57d9c502018-09-14 14:42:34 -050069} // namespace internal
70} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053071
Ratan Gupta497c0c92017-08-22 19:15:59 +053072constexpr auto networkdService = "systemd-networkd.service";
73constexpr auto timeSynchdService = "systemd-timesyncd.service";
74
Ratan Gupta8804feb2017-05-25 10:49:57 +053075/* @brief converts the given subnet into prefix notation.
76 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
77 * @param[in] mask - Subnet Mask.
78 * @returns prefix.
79 */
80uint8_t toCidr(int addressFamily, const std::string& mask);
81
82/* @brief converts the prefix into subnetmask.
83 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
84 * @param[in] prefix - prefix length.
85 * @returns subnet mask.
86 */
87std::string toMask(int addressFamily, uint8_t prefix);
88
89/* @brief checks that the given ip address is link local or not.
90 * @param[in] address - IP address.
91 * @returns true if it is linklocal otherwise false.
92 */
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050093bool isLinkLocalIP(const std::string& address);
94
95/* @brief checks that the given ip address valid or not.
96 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
97 * @param[in] address - IP address.
98 * @returns true if it is valid otherwise false.
99 */
100bool isValidIP(int addressFamily, const std::string& address);
101
102/* @brief checks that the given prefix is valid or not.
103 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
104 * @param[in] prefix - prefix length.
105 * @returns true if it is valid otherwise false.
106 */
107bool isValidPrefix(int addressFamily, uint8_t prefixLength);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530108
Gunnar Millsd75f0492017-10-25 20:33:32 -0500109/* @brief gets the network section of the ip address.
Ratan Gupta11cef802017-05-29 08:41:48 +0530110 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
111 * @param[in] ipaddress - IP address.
112 * @param[in] prefix - prefix length.
113 * @returns network section of the ipaddress.
114 */
115std::string getNetworkID(int addressFamily, const std::string& ipaddress,
116 uint8_t prefix);
117
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530118/** @brief Gets the map of interface and the associated
119 * address.
120 * @returns map of interface and the address.
Ratan Gupta3681a502017-06-17 19:20:04 +0530121 */
122IntfAddrMap getInterfaceAddrs();
123
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530124/** @brief Get all the interfaces from the system.
125 * @returns list of interface names.
126 */
127InterfaceList getInterfaces();
128
Ratan Guptabc886292017-07-25 18:29:57 +0530129/** @brief Delete the given interface.
130 * @param[in] intf - interface name.
131 */
132void deleteInterface(const std::string& intf);
133
Ratan Gupta56187e72017-08-13 09:40:14 +0530134/** @brief read the DHCP value from the configuration file
135 * @param[in] confDir - Network configuration directory.
136 * @param[in] intf - Interface name.
137 */
138bool getDHCPValue(const std::string& confDir, const std::string& intf);
Ratan Guptabc886292017-07-25 18:29:57 +0530139
Ratan Guptabd303b12017-08-18 17:10:07 +0530140namespace internal
141{
142
143/* @brief runs the given command in child process.
144 * @param[in] path - path of the binary file which needs to be execeuted.
145 * @param[in] args - arguments of the command.
146 */
147void executeCommandinChildProcess(const char* path, char** args);
148
149} // namespace internal
150
151/* @brief runs the given command in child process.
152 * @param[in] path -path of the binary file which needs to be execeuted.
153 * @param[in] tArgs - arguments of the command.
154 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155template <typename... ArgTypes>
Ratan Guptabd303b12017-08-18 17:10:07 +0530156void execute(const char* path, ArgTypes&&... tArgs)
157{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500158 using expandType = char* [];
Ratan Guptabd303b12017-08-18 17:10:07 +0530159
Gunnar Mills57d9c502018-09-14 14:42:34 -0500160 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530161
162 internal::executeCommandinChildProcess(path, args);
163}
164
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530166
167class Descriptor
168{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 private:
170 /** default value */
171 int fd = -1;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530172
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173 public:
174 Descriptor() = default;
175 Descriptor(const Descriptor&) = delete;
176 Descriptor& operator=(const Descriptor&) = delete;
177 Descriptor(Descriptor&&) = delete;
178 Descriptor& operator=(Descriptor&&) = delete;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180 explicit Descriptor(int fd) : fd(fd)
181 {
182 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530183
Gunnar Mills57d9c502018-09-14 14:42:34 -0500184 /* @brief sets the internal file descriptor with the given descriptor
185 * and closes the old descriptor.
186 * @param[in] descriptor - File/Socket descriptor.
187 */
188 void set(int descriptor)
189 {
190 // same descriptor given
191 if (fd == descriptor)
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530192 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193 return;
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530194 }
195
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 // close the old descriptor
197 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530198 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530200 }
201
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 fd = descriptor;
203 }
204
205 ~Descriptor()
206 {
207 if (fd >= 0)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530208 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209 close(fd);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530210 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211 }
212
213 int operator()() const
214 {
215 return fd;
216 }
Ratan Gupta8804feb2017-05-25 10:49:57 +0530217};
218
Gunnar Mills57d9c502018-09-14 14:42:34 -0500219} // namespace phosphor