blob: abfd8ebd5dc11ab1551368f0008dbff296cd573b [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
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080021/* @brief converts a sockaddr for the specified address family into
22 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -070023 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080024 * @param[in] buf - The network byte order address
25 */
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070026constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf)
William A. Kennington III01c816f2022-11-07 14:12:43 -080027{
28 switch (family)
29 {
30 case AF_INET:
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070031 return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf);
William A. Kennington III01c816f2022-11-07 14:12:43 -080032 case AF_INET6:
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070033 return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf);
William A. Kennington III01c816f2022-11-07 14:12:43 -080034 }
35 throw std::invalid_argument("Unrecognized family");
36}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080037
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070038/** @brief Converts the interface name into a u-boot environment
39 * variable that would hold its ethernet address.
40 *
41 * @param[in] intf - interface name
42 * @return The name of th environment key
43 */
William A. Kennington III69f45542022-09-24 23:28:14 -070044std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070045
William A. Kennington IIIa520a392022-08-08 12:17:34 -070046/** @brief read the IPv6AcceptRA value from the configuration file
47 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +053048 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070049bool getIPv6AcceptRA(const config::Parser& config);
50
51/** @brief read the DHCP value from the configuration file
52 * @param[in] config - The parsed configuration.
53 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -070054struct DHCPVal
55{
56 bool v4, v6;
57};
58DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +053059
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070060/** @brief Read a boolean DHCP property from a conf file
61 * @param[in] config - The parsed configuration.
62 * @param[in] key - The property name.
63 */
64bool getDHCPProp(const config::Parser& config, std::string_view key);
65
Ratan Guptabd303b12017-08-18 17:10:07 +053066namespace internal
67{
68
69/* @brief runs the given command in child process.
70 * @param[in] path - path of the binary file which needs to be execeuted.
71 * @param[in] args - arguments of the command.
72 */
William A. Kennington III69f45542022-09-24 23:28:14 -070073void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +053074
Lei YU307554e2021-03-18 14:56:50 +080075/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070076std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +080077
78/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -070079std::unordered_set<std::string_view>
80 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +080081
82/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -070083const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +080084
Ratan Guptabd303b12017-08-18 17:10:07 +053085} // namespace internal
86
87/* @brief runs the given command in child process.
88 * @param[in] path -path of the binary file which needs to be execeuted.
89 * @param[in] tArgs - arguments of the command.
90 */
Gunnar Mills57d9c502018-09-14 14:42:34 -050091template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -070092void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +053093{
William A. Kennington III0420c6a2019-06-27 14:38:17 -070094 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +053095
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +053097
98 internal::executeCommandinChildProcess(path, args);
99}
100
Gunnar Mills57d9c502018-09-14 14:42:34 -0500101} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530102
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103} // namespace phosphor