xref: /openbmc/phosphor-networkd/src/util.hpp (revision 618ac419947e8036adef91c2add8cbecac201b71)
1 #pragma once
2 #include "types.hpp"
3 
4 #include <stdplus/raw.hpp>
5 #include <stdplus/zstring_view.hpp>
6 
7 #include <map>
8 #include <optional>
9 #include <string>
10 #include <string_view>
11 #include <unordered_set>
12 
13 namespace phosphor
14 {
15 namespace network
16 {
17 namespace config
18 {
19 class Parser;
20 }
21 
22 /* @brief converts a sockaddr for the specified address family into
23  *        a type_safe InAddrAny.
24  * @param[in] family - The address family of the buf
25  * @param[in] buf - The network byte order address
26  */
addrFromBuf(int family,std::string_view buf)27 constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf)
28 {
29     switch (family)
30     {
31         case AF_INET:
32             return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf);
33         case AF_INET6:
34             return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf);
35     }
36     throw std::invalid_argument("Unrecognized family");
37 }
38 
39 /** @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  */
45 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
46 
47 /** @brief read the IPv6AcceptRA value from the configuration file
48  *  @param[in] config - The parsed configuration.
49  */
50 bool getIPv6AcceptRA(const config::Parser& config);
51 
52 /** @brief read the DHCP value from the configuration file
53  *  @param[in] config - The parsed configuration.
54  */
55 struct DHCPVal
56 {
57     bool v4, v6;
58 };
59 
60 enum class DHCPType
61 {
62     v4,
63     v6
64 };
65 
66 DHCPVal getDHCPValue(const config::Parser& config);
67 
68 /** @brief Read a boolean DHCP property from a conf file
69  *  @param[in] config - The parsed configuration.
70  *  @param[in] nwType - The network type.
71  *  @param[in] key - The property name.
72  */
73 bool getDHCPProp(const config::Parser& config, DHCPType dhcpType,
74                  std::string_view key);
75 
76 /** @brief Read LLDP configuration from lldpd conf file
77  */
78 std::map<std::string, bool> parseLLDPConf();
79 
80 namespace 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  */
87 void executeCommandinChildProcess(stdplus::zstring_view path, char** args);
88 
89 /** @brief Get ignored interfaces from environment */
90 std::string_view getIgnoredInterfacesEnv();
91 
92 /** @brief Parse the comma separated interface names */
93 std::unordered_set<std::string_view> parseInterfaces(
94     std::string_view interfaces);
95 
96 /** @brief Get the ignored interfaces */
97 const std::unordered_set<std::string_view>& getIgnoredInterfaces();
98 
99 } // 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  */
105 template <typename... ArgTypes>
execute(stdplus::zstring_view path,ArgTypes &&...tArgs)106 void execute(stdplus::zstring_view path, ArgTypes&&... tArgs)
107 {
108     using expandType = char*[];
109 
110     expandType args = {const_cast<char*>(tArgs)..., nullptr};
111 
112     internal::executeCommandinChildProcess(path, args);
113 }
114 
115 } // namespace network
116 
117 } // namespace phosphor
118