25#ifndef JOIN_FABRIC_ARP_HPP
26#define JOIN_FABRIC_ARP_HPP
35#include <unordered_map>
39#include <linux/filter.h>
40#include <net/if_arp.h>
101 template <
typename Rep,
typename Period>
104 if (ip.
family () != AF_INET)
127 return get (ip, std::chrono::seconds (5));
137 template <
typename Rep,
typename Period>
139 std::chrono::duration<Rep, Period> timeout)
141 return Arp (interface).get (ip, timeout);
152 return get (interface, ip, std::chrono::seconds (5));
161 template <
typename Rep,
typename Period>
164 if (ip.
family () != AF_INET)
175 ::memcpy (out.eth.h_source, srcMac.
addr (), ETH_ALEN);
176 out.eth.h_proto = ::htons (ETH_P_ARP);
178 out.arp.ar_hrd = ::htons (ARPHRD_ETHER);
179 out.arp.ar_pro = ::htons (ETH_P_IP);
180 out.arp.ar_hln = ETH_ALEN;
182 out.arp.ar_op = ::htons (ARPOP_REQUEST);
183 ::memcpy (out.arp.ar_sha, srcMac.
addr (), ETH_ALEN);
184 ::memcpy (&out.arp.ar_sip, srcIp.
addr (), sizeof (uint32_t));
186 ::memcpy (&out.arp.ar_tip, ip.
addr (), sizeof (uint32_t));
191 ::memcpy (&tip, &out.arp.ar_tip, sizeof (tip));
192 auto inserted = _pending.emplace (tip, std::make_unique<PendingRequest> ());
193 if (!inserted.second)
201 if (_socket.
write (
reinterpret_cast<const char*
> (&out), sizeof (Packet)) == -1)
204 _pending.erase (inserted.first);
209 if (!inserted.first->second->cond.timedWait (lock, timeout))
211 _pending.erase (inserted.first);
212 lastError = std::make_error_code (std::errc::no_such_device_or_address);
217 _pending.erase (inserted.first);
229 return request (ip, std::chrono::seconds (5));
239 template <
typename Rep,
typename Period>
241 std::chrono::duration<Rep, Period> timeout)
243 return Arp (interface).request (ip, timeout);
254 return request (interface, ip, std::chrono::seconds (5));
308 struct __attribute__ ((packed)) ArpPacket
315 uint8_t ar_sha[ETH_ALEN];
317 uint8_t ar_tha[ETH_ALEN];
324 struct __attribute__ ((packed)) Packet
334 void onReadable (
int fd)
noexcept override;
337 static constexpr size_t _bufferSize = 4096;
342 struct PendingRequest
349 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>> _pending;
358 const std::string _interface;
364 NeighborManager& _neighbors;
ARP protocol class.
Definition arp.hpp:49
static MacAddress request(const std::string &interface, const IpAddress &ip)
get the MAC address for the given IP address using ARP request.
Definition arp.hpp:252
int add(const MacAddress &mac, const IpAddress &ip)
add entry the MAC address of the given IP address to ARP cache.
Definition arp.cpp:90
~Arp()
destroy the Arp instance.
Definition arp.cpp:81
MacAddress request(const IpAddress &ip, std::chrono::duration< Rep, Period > timeout)
get the MAC address for the given IP address using ARP request.
Definition arp.hpp:162
Arp(const Arp &other)=delete
create instance by copy.
static MacAddress request(const std::string &interface, const IpAddress &ip, std::chrono::duration< Rep, Period > timeout)
get the MAC address for the given IP address using ARP request.
Definition arp.hpp:240
static MacAddress get(const std::string &interface, const IpAddress &ip)
discover the MAC address for the given internet layer address.
Definition arp.hpp:150
static MacAddress get(const std::string &interface, const IpAddress &ip, std::chrono::duration< Rep, Period > timeout)
discover the MAC address for the given internet layer address.
Definition arp.hpp:138
Arp()=delete
create the Arp instance.
MacAddress get(const IpAddress &ip, std::chrono::duration< Rep, Period > timeout)
get the MAC address for the given IP address using netlink neighbor cache or ARP request.
Definition arp.hpp:102
int remove(const IpAddress &ip)
remove the MAC address of the given IP address from ARP cache.
Definition arp.cpp:114
MacAddress cache(const IpAddress &ip)
get the MAC address for the given IP address using ARP cache.
Definition arp.cpp:138
Arp & operator=(const Arp &other)=delete
assign instance by copy.
MacAddress request(const IpAddress &ip)
get the MAC address for the given IP address using ARP request.
Definition arp.hpp:227
MacAddress get(const IpAddress &ip)
get the MAC address for the given IP address using ARP cache or ARP request.
Definition arp.hpp:125
Arp(Arp &&other)=delete
create instance by move.
ssize_t write(const char *data, size_t maxSize) noexcept
write data.
Definition raw_socket.hpp:247
Event handler interface class.
Definition reactor.hpp:48
friend class Reactor
friendship with reactor.
Definition reactor.hpp:149
IPv6, IPv4 address class.
Definition ip_address.hpp:51
int family() const
get address family.
Definition ip_address.cpp:1250
static IpAddress ipv4Address(const std::string &interface)
get the specified interface IPv4 address.
Definition ip_address.cpp:1526
const void * addr() const
get the internal address structure.
Definition ip_address.cpp:1259
MAC address class.
Definition mac_address.hpp:46
static const MacAddress broadcast
broadcast MAC address.
Definition mac_address.hpp:309
bool isWildcard() const
check if MAC address is a wildcard address.
Definition mac_address.cpp:182
static const MacAddress wildcard
wildcard MAC address.
Definition mac_address.hpp:306
static MacAddress address(const std::string &interface)
get the specified interface MAC address.
Definition mac_address.cpp:355
const uint8_t * addr() const
get the internal MAC address array address.
Definition mac_address.cpp:164
ARP / NDP neighbor manager class.
Definition neighbor_manager.hpp:138
static NeighborManager & instance()
get the a singleton instance.
Definition neighbor_manager.cpp:62
class owning a mutex for the duration of a scoped block.
Definition mutex.hpp:246
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195