xref: /openbmc/phosphor-networkd/src/types.hpp (revision 90434ee4)
1 #pragma once
2 
3 #include <ifaddrs.h>
4 #include <netinet/in.h>
5 #include <systemd/sd-event.h>
6 
7 #include <chrono>
8 #include <memory>
9 #include <sdeventplus/clock.hpp>
10 #include <sdeventplus/utility/timer.hpp>
11 #include <string>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <variant>
15 
16 namespace phosphor
17 {
18 namespace network
19 {
20 
21 using namespace std::chrono_literals;
22 
23 // wait for three seconds before reloading systemd-networkd
24 constexpr auto reloadTimeout = 3s;
25 
26 // refresh the objets after four seconds as network
27 // configuration takes 3-4 sec to reconfigure at most.
28 constexpr auto refreshTimeout = 4s;
29 
30 using Addr_t = ifaddrs*;
31 
32 struct AddrDeleter
33 {
34     void operator()(Addr_t ptr) const
35     {
36         freeifaddrs(ptr);
37     }
38 };
39 
40 using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
41 
42 /* Need a custom deleter for freeing up sd_event */
43 struct EventDeleter
44 {
45     void operator()(sd_event* event) const
46     {
47         sd_event_unref(event);
48     }
49 };
50 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
51 
52 // Byte representations for common address types in network byte order
53 using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
54 
55 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
56 
57 struct string_hash : public std::hash<std::string_view>
58 {
59     using is_transparent = void;
60 };
61 template <typename V>
62 using string_umap =
63     std::unordered_map<std::string, V, string_hash, std::equal_to<>>;
64 using string_uset =
65     std::unordered_set<std::string, string_hash, std::equal_to<>>;
66 
67 constexpr std::size_t hash_multi()
68 {
69     return 0;
70 }
71 
72 template <typename T, typename... Args>
73 constexpr std::size_t hash_multi(const T& v, Args... args)
74 {
75     const std::size_t seed = hash_multi(args...);
76     return seed ^ (std::hash<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
77 }
78 
79 } // namespace network
80 } // namespace phosphor
81