join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
route.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_ROUTE_HPP
26#define JOIN_FABRIC_ROUTE_HPP
27
28// libjoin.
29#include <join/ip_address.hpp>
30#include <join/mutex.hpp>
31
32// C++.
33#include <compare>
34#include <memory>
35#include <vector>
36#include <tuple>
37
38// C.
39#include <linux/rtnetlink.h>
40#include <cstdint>
41
42namespace join
43{
45 class RouteManager;
46
50 struct RouteKey
51 {
53 uint32_t _index = 0;
54
57
59 uint32_t _prefix = 0;
60 };
61
68 inline bool operator== (const RouteKey& lhs, const RouteKey& rhs) noexcept
69 {
70 return lhs._index == rhs._index && lhs._dest == rhs._dest && lhs._prefix == rhs._prefix;
71 }
72
79 inline std::strong_ordering operator<=> (const RouteKey& lhs, const RouteKey& rhs) noexcept
80 {
81 return std::tie (lhs._index, lhs._dest, lhs._prefix) <=> std::tie (rhs._index, rhs._dest, rhs._prefix);
82 }
83
87 class Route
88 {
89 private:
95 explicit Route (RouteManager* manager, const RouteKey& key);
96
97 public:
98 using Ptr = std::shared_ptr<Route>;
99
103 Route () = delete;
104
108 ~Route () = default;
109
114 uint32_t index () const noexcept;
115
120 const IpAddress& dest () const noexcept;
121
126 uint32_t prefix () const noexcept;
127
132 IpAddress gateway () const;
133
138 uint32_t metric () const;
139
144 uint8_t type () const;
145
150 uint8_t scope () const;
151
156 uint8_t protocol () const;
157
162 bool isUnicast () const noexcept;
163
168 bool isBlackhole () const noexcept;
169
174 bool isUnreachable () const noexcept;
175
180 bool isProhibit () const noexcept;
181
186 bool isLocal () const noexcept;
187
192 bool isScopeUniverse () const noexcept;
193
198 bool isScopeLink () const noexcept;
199
204 bool isScopeHost () const noexcept;
205
210 bool isStatic () const noexcept;
211
216 bool isKernel () const noexcept;
217
222 bool isDhcp () const noexcept;
223
228 bool isBoot () const noexcept;
229
237 int set (const IpAddress& gateway, uint32_t metric = 0, bool sync = false) const;
238
244 int remove (bool sync = false) const;
245
246 private:
248 RouteManager* _manager = nullptr;
249
251 uint32_t _index = 0;
252
254 IpAddress _dest;
255
257 uint32_t _prefix = 0;
258
260 IpAddress _gateway;
261
263 uint32_t _metric = 0;
264
266 uint8_t _type = RTN_UNICAST;
267
269 uint8_t _scope = RT_SCOPE_UNIVERSE;
270
272 uint8_t _protocol = RTPROT_STATIC;
273
275 mutable Mutex _mutex;
276
277 // friendship with equal operator.
278 friend bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
279
280 // friendship with three-way comparison operator.
281 friend std::strong_ordering operator<=> (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
282
283 // friendship with route manager.
284 friend class RouteManager;
285 };
286
293 inline bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
294 {
295 if (!lhs && !rhs)
296 {
297 return true;
298 }
299 if (!lhs || !rhs)
300 {
301 return false;
302 }
303 return lhs->_index == rhs->_index && lhs->_dest == rhs->_dest && lhs->_prefix == rhs->_prefix;
304 }
305
312 inline std::strong_ordering operator<=> (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
313 {
314 if (!lhs || !rhs)
315 {
316 return bool (lhs) <=> bool (rhs);
317 }
318 return std::tie (lhs->_index, lhs->_dest, lhs->_prefix) <=> std::tie (rhs->_index, rhs->_dest, rhs->_prefix);
319 }
320
322 using RouteList = std::vector<Route::Ptr>;
323
324}
325
326namespace std
327{
331 template <>
332 struct hash<join::RouteKey>
333 {
334 size_t operator() (const join::RouteKey& key) const noexcept
335 {
336 size_t h = std::hash<uint32_t>{}(key._index);
337 h ^= std::hash<join::IpAddress>{}(key._dest) + 0x9e3779b9 + (h << 6) + (h >> 2);
338 h ^= std::hash<uint32_t>{}(key._prefix) + 0x9e3779b9 + (h << 6) + (h >> 2);
339 return h;
340 }
341 };
342}
343
344#endif
IPv6, IPv4 address class.
Definition ip_address.hpp:52
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
kernel routing table manager class.
Definition route_manager.hpp:141
class representing a single kernel routing table entry.
Definition route.hpp:88
const IpAddress & dest() const noexcept
get the destination network address.
Definition route.cpp:58
bool isProhibit() const noexcept
check if route returns ICMP prohibited.
Definition route.cpp:153
int remove(bool sync=false) const
remove this route entry from the kernel table.
Definition route.cpp:243
uint8_t scope() const
get the route scope.
Definition route.cpp:106
friend std::strong_ordering operator<=>(const Route::Ptr &lhs, const Route::Ptr &rhs) noexcept
three-way compare routes.
Definition route.hpp:312
uint8_t type() const
get the route type.
Definition route.cpp:96
bool isLocal() const noexcept
check if route is a local route.
Definition route.cpp:162
bool isDhcp() const noexcept
check if route was added by a DHCP client.
Definition route.cpp:216
bool isUnicast() const noexcept
check if route is a standard unicast route.
Definition route.cpp:126
bool isScopeUniverse() const noexcept
check if route is universe-scoped.
Definition route.cpp:171
uint32_t metric() const
get the route metric.
Definition route.cpp:86
uint32_t index() const noexcept
get interface index.
Definition route.cpp:49
bool isUnreachable() const noexcept
check if route returns ICMP unreachable.
Definition route.cpp:144
Route()=delete
create instance.
bool isBlackhole() const noexcept
check if route silently drops packets.
Definition route.cpp:135
int set(const IpAddress &gateway, uint32_t metric=0, bool sync=false) const
update this route entry in the kernel table.
Definition route.cpp:234
bool isScopeHost() const noexcept
check if route is host-scoped.
Definition route.cpp:189
bool isScopeLink() const noexcept
check if route is link-scoped.
Definition route.cpp:180
bool isStatic() const noexcept
check if route was added statically.
Definition route.cpp:198
~Route()=default
destroy instance.
bool isBoot() const noexcept
check if route was added at boot time.
Definition route.cpp:225
bool isKernel() const noexcept
check if route was added by the kernel.
Definition route.cpp:207
uint8_t protocol() const
get the route protocol.
Definition route.cpp:116
IpAddress gateway() const
get the gateway address.
Definition route.cpp:76
std::shared_ptr< Route > Ptr
Definition route.hpp:98
uint32_t prefix() const noexcept
get the prefix length.
Definition route.cpp:67
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
std::vector< Route::Ptr > RouteList
list of routes.
Definition route.hpp:322
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208
Definition error.hpp:144
key identifying a routing table entry.
Definition route.hpp:51
uint32_t _index
interface index.
Definition route.hpp:53
uint32_t _prefix
prefix length.
Definition route.hpp:59
IpAddress _dest
destination network address.
Definition route.hpp:56