join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
neighbormanager.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_NEIGHBORMANAGER_HPP
26#define JOIN_FABRIC_NEIGHBORMANAGER_HPP
27
28// libjoin.
30#include <join/neighbor.hpp>
31
32// C++.
33#include <functional>
34
35namespace join
36{
40 enum class NeighborChangeType : uint32_t
41 {
42 Added = 1L << 0,
43 Deleted = 1L << 1,
44 Modified = 1L << 2,
45 MacChanged = 1L << 3,
46 StateChanged = 1L << 4,
47 };
48
56 {
57 return NeighborChangeType (static_cast<uint32_t> (a) & static_cast<uint32_t> (b));
58 }
59
67 {
68 return NeighborChangeType (static_cast<uint32_t> (a) | static_cast<uint32_t> (b));
69 }
70
78 {
79 return NeighborChangeType (static_cast<uint32_t> (a) ^ static_cast<uint32_t> (b));
80 }
81
88 {
89 return NeighborChangeType (~static_cast<uint32_t> (a));
90 }
91
99 {
100 return a = a & b;
101 }
102
110 {
111 return a = a | b;
112 }
113
121 {
122 return a = a ^ b;
123 }
124
133
138 {
139 public:
140 using NeighborNotify = std::function<void (const NeighborInfo& info)>;
141
146 explicit NeighborManager (Reactor* reactor = nullptr);
147
152 NeighborManager (const NeighborManager& other) = delete;
153
159
166
173
178
183 static NeighborManager& instance ();
184
191 Neighbor::Ptr findByIndex (uint32_t index, const IpAddress& ipAddress);
192
199 Neighbor::Ptr findByName (const std::string& interfaceName, const IpAddress& ipAddress);
200
206
212 NeighborList enumerate (uint32_t index);
213
219 NeighborList enumerate (const std::string& interfaceName);
220
225 int refresh ();
226
232 uint64_t addNeighborListener (const NeighborNotify& cb);
233
238 void removeNeighborListener (uint64_t id);
239
249 int addNeighbor (uint32_t index, const IpAddress& ipAddress, const MacAddress& macAddress,
250 uint16_t state = NUD_PERMANENT, bool sync = false);
251
261 int addNeighbor (const std::string& interfaceName, const IpAddress& ipAddress, const MacAddress& macAddress,
262 uint16_t state = NUD_PERMANENT, bool sync = false);
263
271 int removeNeighbor (uint32_t index, const IpAddress& ipAddress, bool sync = false);
272
280 int removeNeighbor (const std::string& interfaceName, const IpAddress& ipAddress, bool sync = false);
281
288 int flushNeighbors (uint32_t index, bool sync = false);
289
296 int flushNeighbors (const std::string& interfaceName, bool sync = false);
297
298 private:
308 int setNeighbor (uint32_t index, const IpAddress& ipAddress, const MacAddress& macAddress, uint16_t state,
309 bool sync);
310
316 int dumpNeighbors (bool sync = false);
317
322 void onMessage (struct nlmsghdr* nlh) override;
323
328 void onNeighborMessage (struct nlmsghdr* nlh);
329
334 void notifyNeighborUpdate (const NeighborInfo& info);
335
343 Neighbor::Ptr acquire (uint32_t index, const IpAddress& ipAddress, NeighborInfo& info);
344
346 std::unordered_map<NeighborKey, Neighbor::Ptr> _neighbors;
347
349 Mutex _neighMutex;
350
352 std::unordered_map<uint64_t, NeighborNotify> _neighborListeners;
353
355 uint64_t _listenerCounter = 0;
356
358 Mutex _listenerMutex;
359
360 // friendship with neighbor.
361 friend class Neighbor;
362 };
363}
364
365#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
Neighbor::Ptr findByIndex(uint32_t index, const IpAddress &ipAddress)
find a neighbor entry by interface index and IP address.
Definition neighbormanager.cpp:71
int refresh()
refresh all data.
Definition neighbormanager.cpp:146
Neighbor::Ptr findByName(const std::string &interfaceName, const IpAddress &ipAddress)
find a neighbor entry by interface name and IP address.
Definition neighbormanager.cpp:88
static NeighborManager & instance()
get the a singleton instance.
Definition neighbormanager.cpp:61
int addNeighbor(uint32_t index, const IpAddress &ipAddress, const MacAddress &macAddress, uint16_t state=NUD_PERMANENT, bool sync=false)
add or replace a neighbor entry in the kernel table.
Definition neighbormanager.cpp:180
NeighborManager(Reactor *reactor=nullptr)
create instance.
Definition neighbormanager.cpp:41
void removeNeighborListener(uint64_t id)
unregister a previously registered callback.
Definition neighbormanager.cpp:170
std::function< void(const NeighborInfo &info)> NeighborNotify
Definition neighbormanager.hpp:140
~NeighborManager()
destroy instance.
Definition neighbormanager.cpp:52
uint64_t addNeighborListener(const NeighborNotify &cb)
register a callback invoked on any neighbor table change.
Definition neighbormanager.cpp:160
NeighborManager(NeighborManager &&other)=delete
create instance by move.
int flushNeighbors(uint32_t index, bool sync=false)
flush all neighbor entries for a given interface.
Definition neighbormanager.cpp:258
NeighborManager & operator=(const NeighborManager &other)=delete
assign instance by copy.
NeighborList enumerate()
enumerate all cached neighbor entries.
Definition neighbormanager.cpp:97
NeighborManager(const NeighborManager &other)=delete
create instance by copy.
int removeNeighbor(uint32_t index, const IpAddress &ipAddress, bool sync=false)
remove a neighbor entry from the kernel table.
Definition neighbormanager.cpp:223
class representing a single ARP / NDP neighbor cache entry.
Definition neighbor.hpp:88
std::shared_ptr< Neighbor > Ptr
Definition neighbor.hpp:98
base class for netlink-based managers.
Definition netlinkmanager.hpp:49
Reactor * reactor() const noexcept
get the event loop reactor.
Definition netlinkmanager.cpp:52
Reactor class.
Definition reactor.hpp:120
Definition acceptor.hpp:32
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1719
constexpr const JsonReadMode & operator&=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary AND on JsonReadMode.
Definition json.hpp:916
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1665
InterfaceChangeType operator~(InterfaceChangeType a)
perform binary NOT on InterfaceChangeType.
Definition interfacemanager.hpp:95
std::vector< Neighbor::Ptr > NeighborList
list of neighbors.
Definition neighbor.hpp:252
NeighborChangeType
enumeration of neighbor change types.
Definition neighbormanager.hpp:41
constexpr const JsonReadMode & operator|=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary OR on JsonReadMode.
Definition json.hpp:927
const InterfaceChangeType & operator^=(InterfaceChangeType &a, InterfaceChangeType b)
perform binary XOR on InterfaceChangeType.
Definition interfacemanager.hpp:128
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1692
neighbor change notification payload.
Definition neighbormanager.hpp:129
NeighborChangeType flags
Definition neighbormanager.hpp:131
Neighbor::Ptr neighbor
Definition neighbormanager.hpp:130