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/ipaddress.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 using List = std::vector<Ptr>;
106
110 Route () = delete;
111
115 ~Route () = default;
116
121 uint32_t index () const noexcept;
122
127 const IpAddress& dest () const noexcept;
128
133 uint32_t prefix () const noexcept;
134
139 IpAddress gateway () const;
140
145 uint32_t metric () const;
146
151 uint8_t type () const;
152
157 uint8_t scope () const;
158
163 uint8_t protocol () const;
164
169 bool isUnicast () const noexcept;
170
175 bool isBlackhole () const noexcept;
176
181 bool isUnreachable () const noexcept;
182
187 bool isProhibit () const noexcept;
188
193 bool isLocal () const noexcept;
194
199 bool isScopeUniverse () const noexcept;
200
205 bool isScopeLink () const noexcept;
206
211 bool isScopeHost () const noexcept;
212
217 bool isStatic () const noexcept;
218
223 bool isKernel () const noexcept;
224
229 bool isDhcp () const noexcept;
230
235 bool isBoot () const noexcept;
236
244 int set (const IpAddress& gateway, uint32_t metric = 0, bool sync = false) const;
245
251 int remove (bool sync = false) const;
252
253 private:
255 RouteManager* _manager = nullptr;
256
258 uint32_t _index = 0;
259
261 IpAddress _dest;
262
264 uint32_t _prefix = 0;
265
267 IpAddress _gateway;
268
270 uint32_t _metric = 0;
271
273 uint8_t _type = RTN_UNICAST;
274
276 uint8_t _scope = RT_SCOPE_UNIVERSE;
277
279 uint8_t _protocol = RTPROT_STATIC;
280
282 mutable Mutex _mutex;
283
284 // friendship with equal operator.
285 friend bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
286
287 // friendship with less operator.
288 friend bool operator< (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept;
289
290 // friendship with route manager.
291 friend class RouteManager;
292 };
293
300 inline bool operator== (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
301 {
302 if (!lhs && !rhs)
303 {
304 return true;
305 }
306 if (!lhs || !rhs)
307 {
308 return false;
309 }
310 return lhs->_index == rhs->_index && lhs->_dest == rhs->_dest && lhs->_prefix == rhs->_prefix;
311 }
312
319 inline bool operator< (const Route::Ptr& lhs, const Route::Ptr& rhs) noexcept
320 {
321 if (!lhs)
322 {
323 return true;
324 }
325 if (!rhs)
326 {
327 return false;
328 }
329 if (lhs->_index != rhs->_index)
330 {
331 return lhs->_index < rhs->_index;
332 }
333 if (lhs->_dest != rhs->_dest)
334 {
335 return lhs->_dest < rhs->_dest;
336 }
337 return lhs->_prefix < rhs->_prefix;
338 }
339
341 using RouteList = std::vector<Route::Ptr>;
342
343}
344
345namespace std
346{
350 template <>
351 struct hash<join::RouteKey>
352 {
353 size_t operator() (const join::RouteKey& key) const noexcept
354 {
355 size_t h = std::hash<uint32_t>{}(key._index);
356 h ^= std::hash<join::IpAddress>{}(key._dest) + 0x9e3779b9 + (h << 6) + (h >> 2);
357 h ^= std::hash<uint32_t>{}(key._prefix) + 0x9e3779b9 + (h << 6) + (h >> 2);
358 return h;
359 }
360 };
361}
362
363#endif
IPv6, IPv4 address class.
Definition ipaddress.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 routemanager.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
std::vector< Ptr > List
Definition route.hpp:105
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:319
~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 BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
std::vector< Route::Ptr > RouteList
list of routes.
Definition route.hpp:341
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
Definition error.hpp:137
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