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 <memory>
34#include <vector>
35
36// C.
37#include <linux/rtnetlink.h>
38#include <cstdint>
39
40namespace join
41{
43 class RouteManager;
44
48 struct RouteKey
49 {
51 uint32_t _index = 0;
52
55
57 uint32_t _prefix = 0;
58 };
59
66 inline bool operator== (const RouteKey& lhs, const RouteKey& rhs) noexcept
67 {
68 return lhs._index == rhs._index && lhs._dest == rhs._dest && lhs._prefix == rhs._prefix;
69 }
70
77 inline bool operator< (const RouteKey& lhs, const RouteKey& rhs) noexcept
78 {
79 if (lhs._index != rhs._index)
80 {
81 return lhs._index < rhs._index;
82 }
83 if (lhs._dest != rhs._dest)
84 {
85 return lhs._dest < rhs._dest;
86 }
87 return lhs._prefix < rhs._prefix;
88 }
89
93 class Route
94 {
95 private:
101 explicit Route (RouteManager* manager, const RouteKey& key);
102
103 public:
104 using Ptr = std::shared_ptr<Route>;
105
109 Route () = delete;
110
114 ~Route () = default;
115
120 uint32_t index () const noexcept;
121
126 const IpAddress& dest () const noexcept;
127
132 uint32_t prefix () const noexcept;
133
138 IpAddress gateway () const;
139
144 uint32_t metric () const;
145
150 uint8_t type () const;
151
156 uint8_t scope () const;
157
162 uint8_t protocol () const;
163
168 bool isUnicast () const noexcept;
169
174 bool isBlackhole () const noexcept;
175
180 bool isUnreachable () const noexcept;
181
186 bool isProhibit () const noexcept;
187
192 bool isLocal () const noexcept;
193
198 bool isScopeUniverse () const noexcept;
199
204 bool isScopeLink () const noexcept;
205
210 bool isScopeHost () const noexcept;
211
216 bool isStatic () const noexcept;
217
222 bool isKernel () const noexcept;
223
228 bool isDhcp () const noexcept;
229
234 bool isBoot () const noexcept;
235
243 int set (const IpAddress& gateway, uint32_t metric = 0, bool sync = false) const;
244
250 int remove (bool sync = false) const;
251
252 private:
254 RouteManager* _manager = nullptr;
255
257 uint32_t _index = 0;
258
260 IpAddress _dest;
261
263 uint32_t _prefix = 0;
264
266 IpAddress _gateway;
267
269 uint32_t _metric = 0;
270
272 uint8_t _type = RTN_UNICAST;
273
275 uint8_t _scope = RT_SCOPE_UNIVERSE;
276
278 uint8_t _protocol = RTPROT_STATIC;
279
281 mutable Mutex _mutex;
282
283 // friendship with equal operator.
284 friend bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
285
286 // friendship with less operator.
287 friend bool operator< (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
288
289 // friendship with route manager.
290 friend class RouteManager;
291 };
292
299 inline bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
300 {
301 if (!lhs && !rhs)
302 {
303 return true;
304 }
305 if (!lhs || !rhs)
306 {
307 return false;
308 }
309 return lhs->_index == rhs->_index && lhs->_dest == rhs->_dest && lhs->_prefix == rhs->_prefix;
310 }
311
318 inline bool operator< (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
319 {
320 if (!lhs)
321 {
322 return true;
323 }
324 if (!rhs)
325 {
326 return false;
327 }
328 if (lhs->_index != rhs->_index)
329 {
330 return lhs->_index < rhs->_index;
331 }
332 if (lhs->_dest != rhs->_dest)
333 {
334 return lhs->_dest < rhs->_dest;
335 }
336 return lhs->_prefix < rhs->_prefix;
337 }
338
340 using RouteList = std::vector<Route::Ptr>;
341
342}
343
344namespace std
345{
349 template <>
350 struct hash<join::RouteKey>
351 {
352 size_t operator() (const join::RouteKey& key) const noexcept
353 {
354 size_t h = std::hash<uint32_t>{}(key._index);
355 h ^= std::hash<join::IpAddress>{}(key._dest) + 0x9e3779b9 + (h << 6) + (h >> 2);
356 h ^= std::hash<uint32_t>{}(key._prefix) + 0x9e3779b9 + (h << 6) + (h >> 2);
357 return h;
358 }
359 };
360}
361
362#endif
IPv6, IPv4 address class.
Definition ip_address.hpp:51
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:94
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
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
friend bool operator<(const Route::Ptr &lhs, const Route::Ptr &rhs) noexcept
compare two routes for ordering.
Definition route.hpp:318
~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:104
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:186
std::vector< Route::Ptr > RouteList
list of routes.
Definition route.hpp:340
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
Definition error.hpp:144
key identifying a routing table entry.
Definition route.hpp:49
uint32_t _index
interface index.
Definition route.hpp:51
uint32_t _prefix
prefix length.
Definition route.hpp:57
IpAddress _dest
destination network address.
Definition route.hpp:54