blob: ae34ad6b6e3948c7877b7da7c47a2c143611ce84 [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 III86642522023-07-24 17:55:55 -07005#include <stdplus/zstring_view.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};
Jishnu CM57dfea92023-05-05 06:07:26 -050058
59enum class DHCPType
60{
61 v4,
62 v6
63};
64
William A. Kennington III8060c0d2022-08-18 19:19:34 -070065DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +053066
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070067/** @brief Read a boolean DHCP property from a conf file
68 * @param[in] config - The parsed configuration.
Jishnu CM57dfea92023-05-05 06:07:26 -050069 * @param[in] nwType - The network type.
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070070 * @param[in] key - The property name.
71 */
Jishnu CM57dfea92023-05-05 06:07:26 -050072bool getDHCPProp(const config::Parser& config, DHCPType dhcpType,
73 std::string_view key);
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070074
Ratan Guptabd303b12017-08-18 17:10:07 +053075namespace internal
76{
77
78/* @brief runs the given command in child process.
79 * @param[in] path - path of the binary file which needs to be execeuted.
80 * @param[in] args - arguments of the command.
81 */
William A. Kennington III86642522023-07-24 17:55:55 -070082void executeCommandinChildProcess(stdplus::zstring_view path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +053083
Lei YU307554e2021-03-18 14:56:50 +080084/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070085std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +080086
87/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -070088std::unordered_set<std::string_view>
89 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +080090
91/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -070092const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +080093
Ratan Guptabd303b12017-08-18 17:10:07 +053094} // namespace internal
95
96/* @brief runs the given command in child process.
97 * @param[in] path -path of the binary file which needs to be execeuted.
98 * @param[in] tArgs - arguments of the command.
99 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100template <typename... ArgTypes>
William A. Kennington III86642522023-07-24 17:55:55 -0700101void execute(stdplus::zstring_view path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530102{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700103 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530104
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530106
107 internal::executeCommandinChildProcess(path, args);
108}
109
Gunnar Mills57d9c502018-09-14 14:42:34 -0500110} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530111
Gunnar Mills57d9c502018-09-14 14:42:34 -0500112} // namespace phosphor