Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "types.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | |
William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 4 | #include <stdplus/raw.hpp> |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 5 | #include <stdplus/zstring_view.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 6 | |
| 7 | #include <optional> |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 8 | #include <string> |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 9 | #include <string_view> |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 10 | #include <unordered_set> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
William A. Kennington III | 20efa79 | 2022-10-04 17:19:08 -0700 | [diff] [blame] | 16 | namespace config |
| 17 | { |
| 18 | class Parser; |
| 19 | } |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 20 | |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 21 | /* @brief converts a sockaddr for the specified address family into |
| 22 | * a type_safe InAddrAny. |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 23 | * @param[in] family - The address family of the buf |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 24 | * @param[in] buf - The network byte order address |
| 25 | */ |
William A. Kennington III | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 26 | constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf) |
William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 27 | { |
| 28 | switch (family) |
| 29 | { |
| 30 | case AF_INET: |
William A. Kennington III | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 31 | return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf); |
William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 32 | case AF_INET6: |
William A. Kennington III | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 33 | return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf); |
William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 34 | } |
| 35 | throw std::invalid_argument("Unrecognized family"); |
| 36 | } |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 37 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 38 | /** @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 III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 44 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf); |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 45 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 46 | /** @brief read the IPv6AcceptRA value from the configuration file |
| 47 | * @param[in] config - The parsed configuration. |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 48 | */ |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 49 | bool 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 III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 54 | struct DHCPVal |
| 55 | { |
| 56 | bool v4, v6; |
| 57 | }; |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 58 | |
| 59 | enum class DHCPType |
| 60 | { |
| 61 | v4, |
| 62 | v6 |
| 63 | }; |
| 64 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 65 | DHCPVal getDHCPValue(const config::Parser& config); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 66 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 67 | /** @brief Read a boolean DHCP property from a conf file |
| 68 | * @param[in] config - The parsed configuration. |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 69 | * @param[in] nwType - The network type. |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 70 | * @param[in] key - The property name. |
| 71 | */ |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 72 | bool getDHCPProp(const config::Parser& config, DHCPType dhcpType, |
| 73 | std::string_view key); |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 74 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 75 | namespace 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 III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 82 | void executeCommandinChildProcess(stdplus::zstring_view path, char** args); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 83 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 84 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 85 | std::string_view getIgnoredInterfacesEnv(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 86 | |
| 87 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 88 | std::unordered_set<std::string_view> |
| 89 | parseInterfaces(std::string_view interfaces); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 90 | |
| 91 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 92 | const std::unordered_set<std::string_view>& getIgnoredInterfaces(); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 93 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 94 | } // 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 100 | template <typename... ArgTypes> |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 101 | void execute(stdplus::zstring_view path, ArgTypes&&... tArgs) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 102 | { |
William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 103 | using expandType = char*[]; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 104 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 105 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 106 | |
| 107 | internal::executeCommandinChildProcess(path, args); |
| 108 | } |
| 109 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 110 | } // namespace network |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 111 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 112 | } // namespace phosphor |