join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
neighbor.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_NEIGHBOR_HPP
26#define JOIN_FABRIC_NEIGHBOR_HPP
27
28// libjoin.
29#include <join/mac_address.hpp>
30#include <join/ip_address.hpp>
31#include <join/mutex.hpp>
32
33// C++.
34#include <compare>
35#include <memory>
36#include <vector>
37#include <tuple>
38
39// C.
40#include <linux/neighbour.h>
41#include <cstdint>
42
43namespace join
44{
46 class NeighborManager;
47
52 {
54 uint32_t _index = 0;
55
58 };
59
66 inline bool operator== (const NeighborKey& lhs, const NeighborKey& rhs) noexcept
67 {
68 return lhs._index == rhs._index && lhs._ip == rhs._ip;
69 }
70
77 inline std::strong_ordering operator<=> (const NeighborKey& lhs, const NeighborKey& rhs) noexcept
78 {
79 return std::tie (lhs._index, lhs._ip) <=> std::tie (rhs._index, rhs._ip);
80 }
81
86 {
87 private:
93 explicit Neighbor (NeighborManager* manager, const NeighborKey& key);
94
95 public:
96 using Ptr = std::shared_ptr<Neighbor>;
97
101 Neighbor () = delete;
102
106 ~Neighbor () = default;
107
112 uint32_t index () const noexcept;
113
118 const IpAddress& ip () const noexcept;
119
124 MacAddress mac () const;
125
130 uint16_t state () const;
131
136 bool isReachable () const noexcept;
137
142 bool isStale () const noexcept;
143
148 bool isPermanent () const noexcept;
149
154 bool isIncomplete () const noexcept;
155
160 bool isFailed () const noexcept;
161
169 int set (const MacAddress& macAddress, uint16_t state = NUD_PERMANENT, bool sync = false) const;
170
176 int remove (bool sync = false) const;
177
178 private:
180 NeighborManager* _manager = nullptr;
181
183 uint32_t _index = 0;
184
186 IpAddress _ip;
187
189 MacAddress _mac;
190
192 uint16_t _state = NUD_NONE;
193
195 mutable Mutex _mutex;
196
197 // friendship with equal operator.
198 friend bool operator== (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept;
199
200 // friendship with three-way comparison operator.
201 friend std::strong_ordering operator<=> (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept;
202
203 // friendship with neighbor manager.
204 friend class NeighborManager;
205 };
206
213 inline bool operator== (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept
214 {
215 if (!lhs && !rhs)
216 {
217 return true;
218 }
219 if (!lhs || !rhs)
220 {
221 return false;
222 }
223 return lhs->_index == rhs->_index && lhs->_ip == rhs->_ip;
224 }
225
232 inline std::strong_ordering operator<=> (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept
233 {
234 if (!lhs || !rhs)
235 {
236 return bool (lhs) <=> bool (rhs);
237 }
238 return std::tie (lhs->_index, lhs->_ip) <=> std::tie (rhs->_index, rhs->_ip);
239 }
240
242 using NeighborList = std::vector<Neighbor::Ptr>;
243};
244
245namespace std
246{
250 template <>
251 struct hash<join::NeighborKey>
252 {
253 size_t operator() (const join::NeighborKey& key) const noexcept
254 {
255 size_t h = std::hash<uint32_t>{}(key._index);
256 h ^= std::hash<join::IpAddress>{}(key._ip) + 0x9e3779b9 + (h << 6) + (h >> 2);
257 return h;
258 }
259 };
260}
261
262#endif
IPv6, IPv4 address class.
Definition ip_address.hpp:52
MAC address class.
Definition mac_address.hpp:47
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
ARP / NDP neighbor manager class.
Definition neighbor_manager.hpp:138
class representing a single ARP / NDP neighbor cache entry.
Definition neighbor.hpp:86
MacAddress mac() const
get the address resolved for this entry.
Definition neighbor.cpp:67
std::shared_ptr< Neighbor > Ptr
Definition neighbor.hpp:96
bool isPermanent() const noexcept
is the entry in NUD_PERMANENT state (static).
Definition neighbor.cpp:105
bool isIncomplete() const noexcept
is the entry in NUD_INCOMPLETE state (resolution ongoing).
Definition neighbor.cpp:114
uint32_t index() const noexcept
get interface index.
Definition neighbor.cpp:49
Neighbor()=delete
create instance.
friend std::strong_ordering operator<=>(const Neighbor::Ptr &lhs, const Neighbor::Ptr &rhs) noexcept
three-way compare neighbors.
Definition neighbor.hpp:232
uint16_t state() const
get the NUD state bitmask.
Definition neighbor.cpp:77
~Neighbor()=default
destroy instance.
bool isFailed() const noexcept
is the entry in NUD_FAILED state.
Definition neighbor.cpp:123
int set(const MacAddress &macAddress, uint16_t state=NUD_PERMANENT, bool sync=false) const
update or replace this neighbor entry in the kernel table.
Definition neighbor.cpp:132
bool isStale() const noexcept
is the entry in NUD_STALE state.
Definition neighbor.cpp:96
const IpAddress & ip() const noexcept
get the destination address.
Definition neighbor.cpp:58
int remove(bool sync=false) const
remove this neighbor entry from the kernel table.
Definition neighbor.cpp:141
bool isReachable() const noexcept
is the entry in NUD_REACHABLE state.
Definition neighbor.cpp:87
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< Neighbor::Ptr > NeighborList
list of neighbors.
Definition neighbor.hpp:242
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 neighbor cache entry.
Definition neighbor.hpp:52
uint32_t _index
interface index.
Definition neighbor.hpp:54
IpAddress _ip
destination address.
Definition neighbor.hpp:57