blob: f35e5124429771f7c677d4483bb12df85219651b [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 III01c816f2022-11-07 14:12:43 -08004#include <stdplus/raw.hpp>
William A. Kennington III69f45542022-09-24 23:28:14 -07005#include <stdplus/zstring.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -05006
7#include <optional>
William A. Kennington IIId27410f2019-01-30 17:15:43 -08008#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -08009#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -070010#include <unordered_set>
Ratan Gupta8804feb2017-05-25 10:49:57 +053011
12namespace phosphor
13{
14namespace network
15{
William A. Kennington III20efa792022-10-04 17:19:08 -070016namespace config
17{
18class Parser;
19}
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050020
Ratan Guptabd303b12017-08-18 17:10:07 +053021namespace mac_address
22{
23
William A. Kennington III1137a972019-04-20 20:49:58 -070024/** @brief Determines if the mac address is empty
25 * @param[in] mac - The mac address
26 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053027 */
William A. Kennington III1137a972019-04-20 20:49:58 -070028bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053029
William A. Kennington III1137a972019-04-20 20:49:58 -070030/** @brief Determines if the mac address is a multicast address
31 * @param[in] mac - The mac address
32 * @return True if multicast bit is set
33 */
34bool isMulticast(const ether_addr& mac);
35
36/** @brief Determines if the mac address is a unicast address
37 * @param[in] mac - The mac address
38 * @return True if not multicast or empty
39 */
40bool isUnicast(const ether_addr& mac);
41
Gunnar Mills57d9c502018-09-14 14:42:34 -050042} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053043
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080044/* @brief converts a sockaddr for the specified address family into
45 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -070046 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080047 * @param[in] buf - The network byte order address
48 */
William A. Kennington III01c816f2022-11-07 14:12:43 -080049constexpr InAddrAny addrFromBuf(int family, std::string_view buf)
50{
51 switch (family)
52 {
53 case AF_INET:
54 return stdplus::raw::copyFromStrict<in_addr>(buf);
55 case AF_INET6:
56 return stdplus::raw::copyFromStrict<in6_addr>(buf);
57 }
58 throw std::invalid_argument("Unrecognized family");
59}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080060
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070061/** @brief Converts the interface name into a u-boot environment
62 * variable that would hold its ethernet address.
63 *
64 * @param[in] intf - interface name
65 * @return The name of th environment key
66 */
William A. Kennington III69f45542022-09-24 23:28:14 -070067std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070068
William A. Kennington IIIa520a392022-08-08 12:17:34 -070069/** @brief read the IPv6AcceptRA value from the configuration file
70 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +053071 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070072bool getIPv6AcceptRA(const config::Parser& config);
73
74/** @brief read the DHCP value from the configuration file
75 * @param[in] config - The parsed configuration.
76 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -070077struct DHCPVal
78{
79 bool v4, v6;
80};
81DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +053082
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070083/** @brief Read a boolean DHCP property from a conf file
84 * @param[in] config - The parsed configuration.
85 * @param[in] key - The property name.
86 */
87bool getDHCPProp(const config::Parser& config, std::string_view key);
88
Ratan Guptabd303b12017-08-18 17:10:07 +053089namespace internal
90{
91
92/* @brief runs the given command in child process.
93 * @param[in] path - path of the binary file which needs to be execeuted.
94 * @param[in] args - arguments of the command.
95 */
William A. Kennington III69f45542022-09-24 23:28:14 -070096void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +053097
Lei YU307554e2021-03-18 14:56:50 +080098/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070099std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800100
101/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700102std::unordered_set<std::string_view>
103 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800104
105/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700106const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800107
Ratan Guptabd303b12017-08-18 17:10:07 +0530108} // namespace internal
109
110/* @brief runs the given command in child process.
111 * @param[in] path -path of the binary file which needs to be execeuted.
112 * @param[in] tArgs - arguments of the command.
113 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -0700115void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530116{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700117 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530118
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530120
121 internal::executeCommandinChildProcess(path, args);
122}
123
Gunnar Mills57d9c502018-09-14 14:42:34 -0500124} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530125
Gunnar Mills57d9c502018-09-14 14:42:34 -0500126} // namespace phosphor