blob: 90beca278396f8666dadb5dd89b61502d90ff3a0 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
Ratan Gupta3681a502017-06-17 19:20:04 +05302#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07003
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -07004#include <optional>
William A. Kennington III01c816f2022-11-07 14:12:43 -08005#include <stdplus/raw.hpp>
William A. Kennington III69f45542022-09-24 23:28:14 -07006#include <stdplus/zstring.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -08007#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -08008#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -07009#include <unordered_set>
Ratan Gupta8804feb2017-05-25 10:49:57 +053010
11namespace phosphor
12{
13namespace network
14{
William A. Kennington III20efa792022-10-04 17:19:08 -070015namespace config
16{
17class Parser;
18}
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050019
Ratan Guptabd303b12017-08-18 17:10:07 +053020namespace mac_address
21{
22
William A. Kennington III1137a972019-04-20 20:49:58 -070023/** @brief Determines if the mac address is empty
24 * @param[in] mac - The mac address
25 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053026 */
William A. Kennington III1137a972019-04-20 20:49:58 -070027bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053028
William A. Kennington III1137a972019-04-20 20:49:58 -070029/** @brief Determines if the mac address is a multicast address
30 * @param[in] mac - The mac address
31 * @return True if multicast bit is set
32 */
33bool isMulticast(const ether_addr& mac);
34
35/** @brief Determines if the mac address is a unicast address
36 * @param[in] mac - The mac address
37 * @return True if not multicast or empty
38 */
39bool isUnicast(const ether_addr& mac);
40
Gunnar Mills57d9c502018-09-14 14:42:34 -050041} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053042
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080043/* @brief converts a sockaddr for the specified address family into
44 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -070045 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080046 * @param[in] buf - The network byte order address
47 */
William A. Kennington III01c816f2022-11-07 14:12:43 -080048constexpr InAddrAny addrFromBuf(int family, std::string_view buf)
49{
50 switch (family)
51 {
52 case AF_INET:
53 return stdplus::raw::copyFromStrict<in_addr>(buf);
54 case AF_INET6:
55 return stdplus::raw::copyFromStrict<in6_addr>(buf);
56 }
57 throw std::invalid_argument("Unrecognized family");
58}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080059
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070060/** @brief Converts the interface name into a u-boot environment
61 * variable that would hold its ethernet address.
62 *
63 * @param[in] intf - interface name
64 * @return The name of th environment key
65 */
William A. Kennington III69f45542022-09-24 23:28:14 -070066std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070067
William A. Kennington IIIa520a392022-08-08 12:17:34 -070068/** @brief read the IPv6AcceptRA value from the configuration file
69 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +053070 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070071bool getIPv6AcceptRA(const config::Parser& config);
72
73/** @brief read the DHCP value from the configuration file
74 * @param[in] config - The parsed configuration.
75 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -070076struct DHCPVal
77{
78 bool v4, v6;
79};
80DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +053081
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070082/** @brief Read a boolean DHCP property from a conf file
83 * @param[in] config - The parsed configuration.
84 * @param[in] key - The property name.
85 */
86bool getDHCPProp(const config::Parser& config, std::string_view key);
87
Ratan Guptabd303b12017-08-18 17:10:07 +053088namespace internal
89{
90
91/* @brief runs the given command in child process.
92 * @param[in] path - path of the binary file which needs to be execeuted.
93 * @param[in] args - arguments of the command.
94 */
William A. Kennington III69f45542022-09-24 23:28:14 -070095void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +053096
Lei YU307554e2021-03-18 14:56:50 +080097/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070098std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +080099
100/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700101std::unordered_set<std::string_view>
102 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800103
104/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700105const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800106
Ratan Guptabd303b12017-08-18 17:10:07 +0530107} // namespace internal
108
109/* @brief runs the given command in child process.
110 * @param[in] path -path of the binary file which needs to be execeuted.
111 * @param[in] tArgs - arguments of the command.
112 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500113template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -0700114void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530115{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700116 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530117
Gunnar Mills57d9c502018-09-14 14:42:34 -0500118 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530119
120 internal::executeCommandinChildProcess(path, args);
121}
122
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530124
Gunnar Mills57d9c502018-09-14 14:42:34 -0500125} // namespace phosphor