blob: 1c71eabf7697ac2ad9fb9950c5c1d90e829b7209 [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
Ravi Tejaf7116de2024-12-08 22:48:21 -06007#include <map>
Patrick Williams89d734b2023-05-10 07:50:25 -05008#include <optional>
William A. Kennington IIId27410f2019-01-30 17:15:43 -08009#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080010#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -070011#include <unordered_set>
Ratan Gupta8804feb2017-05-25 10:49:57 +053012
13namespace phosphor
14{
15namespace network
16{
William A. Kennington III20efa792022-10-04 17:19:08 -070017namespace config
18{
19class Parser;
20}
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050021
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080022/* @brief converts a sockaddr for the specified address family into
23 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -070024 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080025 * @param[in] buf - The network byte order address
26 */
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070027constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf)
William A. Kennington III01c816f2022-11-07 14:12:43 -080028{
29 switch (family)
30 {
31 case AF_INET:
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070032 return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf);
William A. Kennington III01c816f2022-11-07 14:12:43 -080033 case AF_INET6:
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070034 return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf);
William A. Kennington III01c816f2022-11-07 14:12:43 -080035 }
36 throw std::invalid_argument("Unrecognized family");
37}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080038
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070039/** @brief Converts the interface name into a u-boot environment
40 * variable that would hold its ethernet address.
41 *
42 * @param[in] intf - interface name
43 * @return The name of th environment key
44 */
William A. Kennington III69f45542022-09-24 23:28:14 -070045std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070046
William A. Kennington IIIa520a392022-08-08 12:17:34 -070047/** @brief read the IPv6AcceptRA value from the configuration file
48 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +053049 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070050bool getIPv6AcceptRA(const config::Parser& config);
51
52/** @brief read the DHCP value from the configuration file
53 * @param[in] config - The parsed configuration.
54 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -070055struct DHCPVal
56{
57 bool v4, v6;
58};
Jishnu CM57dfea92023-05-05 06:07:26 -050059
60enum class DHCPType
61{
62 v4,
63 v6
64};
65
William A. Kennington III8060c0d2022-08-18 19:19:34 -070066DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +053067
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070068/** @brief Read a boolean DHCP property from a conf file
69 * @param[in] config - The parsed configuration.
Jishnu CM57dfea92023-05-05 06:07:26 -050070 * @param[in] nwType - The network type.
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070071 * @param[in] key - The property name.
72 */
Jishnu CM57dfea92023-05-05 06:07:26 -050073bool getDHCPProp(const config::Parser& config, DHCPType dhcpType,
74 std::string_view key);
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070075
Ravi Tejaf7116de2024-12-08 22:48:21 -060076/** @brief Read LLDP configuration from lldpd conf file
77 */
78std::map<std::string, bool> parseLLDPConf();
79
Ratan Guptabd303b12017-08-18 17:10:07 +053080namespace internal
81{
82
83/* @brief runs the given command in child process.
84 * @param[in] path - path of the binary file which needs to be execeuted.
85 * @param[in] args - arguments of the command.
86 */
William A. Kennington III86642522023-07-24 17:55:55 -070087void executeCommandinChildProcess(stdplus::zstring_view path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +053088
Lei YU307554e2021-03-18 14:56:50 +080089/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070090std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +080091
92/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -070093std::unordered_set<std::string_view>
94 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +080095
96/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -070097const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +080098
Ratan Guptabd303b12017-08-18 17:10:07 +053099} // namespace internal
100
101/* @brief runs the given command in child process.
102 * @param[in] path -path of the binary file which needs to be execeuted.
103 * @param[in] tArgs - arguments of the command.
104 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105template <typename... ArgTypes>
William A. Kennington III86642522023-07-24 17:55:55 -0700106void execute(stdplus::zstring_view path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530107{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700108 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530109
Gunnar Mills57d9c502018-09-14 14:42:34 -0500110 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530111
112 internal::executeCommandinChildProcess(path, args);
113}
114
Gunnar Mills57d9c502018-09-14 14:42:34 -0500115} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530116
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117} // namespace phosphor