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/macaddress.hpp>
30#include <join/ipaddress.hpp>
31#include <join/mutex.hpp>
32
33// C++.
34#include <memory>
35#include <vector>
36
37// C.
38#include <linux/neighbour.h>
39#include <cstdint>
40
41namespace join
42{
44 class NeighborManager;
45
50 {
52 uint32_t _index = 0;
53
56 };
57
64 inline bool operator== (const NeighborKey& lhs, const NeighborKey& rhs) noexcept
65 {
66 return lhs._index == rhs._index && lhs._ip == rhs._ip;
67 }
68
75 inline bool operator< (const NeighborKey& lhs, const NeighborKey& rhs) noexcept
76 {
77 if (lhs._index != rhs._index)
78 {
79 return lhs._index < rhs._index;
80 }
81 return lhs._ip < rhs._ip;
82 }
83
88 {
89 private:
95 explicit Neighbor (NeighborManager* manager, const NeighborKey& key);
96
97 public:
98 using Ptr = std::shared_ptr<Neighbor>;
99
103 Neighbor () = delete;
104
108 ~Neighbor () = default;
109
114 uint32_t index () const noexcept;
115
120 const IpAddress& ip () const noexcept;
121
126 MacAddress mac () const;
127
132 uint16_t state () const;
133
138 bool isReachable () const noexcept;
139
144 bool isStale () const noexcept;
145
150 bool isPermanent () const noexcept;
151
156 bool isIncomplete () const noexcept;
157
162 bool isFailed () const noexcept;
163
171 int set (const MacAddress& macAddress, uint16_t state = NUD_PERMANENT, bool sync = false) const;
172
178 int remove (bool sync = false) const;
179
180 private:
182 NeighborManager* _manager = nullptr;
183
185 uint32_t _index = 0;
186
188 IpAddress _ip;
189
191 MacAddress _mac;
192
194 uint16_t _state = NUD_NONE;
195
197 mutable Mutex _mutex;
198
199 // friendship with equal operator.
200 friend bool operator== (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept;
201
202 // friendship with less operator.
203 friend bool operator< (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept;
204
205 // friendship with neighbor manager.
206 friend class NeighborManager;
207 };
208
215 inline bool operator== (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept
216 {
217 if (!lhs && !rhs)
218 {
219 return true;
220 }
221 if (!lhs || !rhs)
222 {
223 return false;
224 }
225 return lhs->_index == rhs->_index && lhs->_ip == rhs->_ip;
226 }
227
234 inline bool operator< (const Neighbor::Ptr& lhs, const Neighbor::Ptr& rhs) noexcept
235 {
236 if (!lhs)
237 {
238 return true;
239 }
240 if (!rhs)
241 {
242 return false;
243 }
244 if (lhs->_index != rhs->_index)
245 {
246 return lhs->_index < rhs->_index;
247 }
248 return lhs->_ip < rhs->_ip;
249 }
250
252 using NeighborList = std::vector<Neighbor::Ptr>;
253};
254
255namespace std
256{
260 template <>
261 struct hash<join::NeighborKey>
262 {
263 size_t operator() (const join::NeighborKey& key) const noexcept
264 {
265 size_t h = std::hash<uint32_t>{}(key._index);
266 h ^= std::hash<join::IpAddress>{}(key._ip) + 0x9e3779b9 + (h << 6) + (h >> 2);
267 return h;
268 }
269 };
270}
271
272#endif
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
ARP / NDP neighbor manager class.
Definition neighbormanager.hpp:138
class representing a single ARP / NDP neighbor cache entry.
Definition neighbor.hpp:88
MacAddress mac() const
get the address resolved for this entry.
Definition neighbor.cpp:67
std::shared_ptr< Neighbor > Ptr
Definition neighbor.hpp:98
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
friend bool operator<(const Neighbor::Ptr &lhs, const Neighbor::Ptr &rhs) noexcept
compare two neighbor for ordering.
Definition neighbor.hpp:234
uint32_t index() const noexcept
get interface index.
Definition neighbor.cpp:49
Neighbor()=delete
create instance.
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 BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
std::vector< Neighbor::Ptr > NeighborList
list of neighbors.
Definition neighbor.hpp:252
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 neighbor cache entry.
Definition neighbor.hpp:50
uint32_t _index
interface index.
Definition neighbor.hpp:52
IpAddress _ip
destination address.
Definition neighbor.hpp:55