25#ifndef JOIN_CORE_ENDPOINT_HPP
26#define JOIN_CORE_ENDPOINT_HPP
36#include <linux/if_packet.h>
38#include <linux/if_xdp.h>
48 template <
class Protocol>
57 this->
_addr.ss_family = Protocol ().family ();
74 struct sockaddr*
addr () noexcept
76 return reinterpret_cast<struct sockaddr*
> (&this->
_addr);
83 const struct sockaddr*
addr () const noexcept
85 return reinterpret_cast<const struct sockaddr*
> (&this->
_addr);
90 struct sockaddr_storage
_addr = {};
96 template <
class Protocol>
106 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_protocol = this->
protocol ().protocol ();
126 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev);
151 constexpr socklen_t
length () const noexcept
153 return sizeof (
struct sockaddr_ll);
160 void device (
const std::string& dev)
noexcept
162 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev.c_str ());
171 char ifname[IFNAMSIZ];
172 if (if_indextoname (
reinterpret_cast<const struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex, ifname))
185 return reinterpret_cast<const struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex;
195 template <
class Protocol>
198 return a.index () == b.index ();
207 template <
class Protocol>
211 return a.index () <=> b.index ();
220 template <
class Protocol>
231 template <
class Protocol>
232 class BasicXdpEndpoint :
public BasicEndpoint<Protocol>
238 BasicXdpEndpoint () noexcept
239 : BasicEndpoint<Protocol> ()
248 BasicXdpEndpoint (
const struct sockaddr* addr, socklen_t len) noexcept
249 : BasicEndpoint<Protocol> (addr, len)
258 BasicXdpEndpoint (
const char* dev, uint32_t queue = 0) noexcept
259 : BasicXdpEndpoint ()
261 struct sockaddr_xdp* sa =
reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr);
262 sa->sxdp_ifindex = if_nametoindex (dev);
263 sa->sxdp_queue_id = queue;
271 BasicXdpEndpoint (
const std::string& dev, uint32_t queue = 0) noexcept
272 : BasicXdpEndpoint<Protocol> (dev.c_str (), queue)
280 constexpr Protocol protocol () const noexcept
289 constexpr socklen_t length () const noexcept
291 return sizeof (
struct sockaddr_xdp);
298 void device (
const std::string& dev)
noexcept
300 reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr)->sxdp_ifindex = if_nametoindex (dev.c_str ());
307 std::string device ()
const
309 char ifname[IFNAMSIZ];
310 if (if_indextoname (
reinterpret_cast<const struct sockaddr_xdp*
> (&this->_addr)->sxdp_ifindex, ifname))
321 uint32_t index () const noexcept
323 return reinterpret_cast<const struct sockaddr_xdp*
> (&this->_addr)->sxdp_ifindex;
330 void queue (uint32_t queue)
noexcept
332 reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr)->sxdp_queue_id = queue;
339 uint32_t queue () const noexcept
341 return reinterpret_cast<const struct sockaddr_xdp*
> (&this->_addr)->sxdp_queue_id;
351 template <
class Protocol>
352 bool operator== (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
354 return a.index () == b.index () && a.queue () == b.queue ();
363 template <
class Protocol>
364 std::strong_ordering
operator<=> (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
366 if (
auto cmp = a.index () <=> b.index (); cmp != 0)
370 return a.queue () <=> b.queue ();
379 template <
class Protocol>
380 std::ostream&
operator<< (std::ostream& os,
const BasicXdpEndpoint<Protocol>& endpoint)
382 os << endpoint.device () <<
":" << endpoint.queue ();
390 template <
class Protocol>
419 struct sockaddr_un* sa =
reinterpret_cast<struct sockaddr_un*
> (&this->
_addr);
420 strncpy (sa->sun_path, dev, sizeof (sa->sun_path) - 1);
445 constexpr socklen_t
length () const noexcept
447 return sizeof (
struct sockaddr_un);
454 void device (
const std::string& dev)
noexcept
456 struct sockaddr_un* sa =
reinterpret_cast<struct sockaddr_un*
> (&this->
_addr);
457 strncpy (sa->sun_path, dev.c_str (), sizeof (sa->sun_path) - 1);
466 return reinterpret_cast<const struct sockaddr_un*
> (&this->
_addr)->sun_path;
476 template <
class Protocol>
479 return std::strncmp (
reinterpret_cast<const struct sockaddr_un*
> (a.addr ())->sun_path,
480 reinterpret_cast<const struct sockaddr_un*
> (b.addr ())->sun_path,
481 sizeof (sockaddr_un::sun_path)) == 0;
490 template <
class Protocol>
494 return std::strncmp (
reinterpret_cast<const struct sockaddr_un*
> (a.addr ())->sun_path,
495 reinterpret_cast<const struct sockaddr_un*
> (b.addr ())->sun_path,
496 sizeof (sockaddr_un::sun_path)) <=> 0;
505 template <
class Protocol>
515 template <
class Protocol>
546 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
547 sa->sin6_family = AF_INET6;
548 sa->sin6_port = htons (port);
549 memcpy (&sa->sin6_addr, ip.addr (), ip.length ());
550 sa->sin6_scope_id = ip.scope ();
554 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
555 sa->sin_family = AF_INET;
556 sa->sin_port = htons (port);
557 memcpy (&sa->sin_addr, ip.addr (), ip.length ());
591 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
592 sa->sin6_family = AF_INET6;
593 sa->sin6_port = htons (port);
597 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
598 sa->sin_family = AF_INET;
599 sa->sin_port = htons (port);
629 struct sockaddr_in6* sa =
reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr);
630 sa->sin6_family = AF_INET6;
632 sa->sin6_scope_id =
ip.
scope ();
636 struct sockaddr_in* sa =
reinterpret_cast<struct sockaddr_in*
> (&this->
_addr);
637 sa->sin_family = AF_INET;
648 return *
reinterpret_cast<const struct sockaddr*
> (&this->
_addr);
655 void port (uint16_t p)
noexcept
657 if (this->
_addr.ss_family == AF_INET6)
659 reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr)->sin6_port = htons (p);
663 reinterpret_cast<struct sockaddr_in*
> (&this->
_addr)->sin_port = htons (p);
671 uint16_t
port () const noexcept
673 if (this->
_addr.ss_family == AF_INET6)
675 return ntohs (
reinterpret_cast<const struct sockaddr_in6*
> (&this->
_addr)->sin6_port);
679 return ntohs (
reinterpret_cast<const struct sockaddr_in*
> (&this->
_addr)->sin_port);
689 if (this->
_addr.ss_family == AF_INET)
691 return Protocol::v4 ();
693 return Protocol::v6 ();
702 if (this->
_addr.ss_family == AF_INET)
704 return sizeof (
struct sockaddr_in);
706 return sizeof (
struct sockaddr_in6);
713 void device (
const std::string& dev)
noexcept
715 if (this->
_addr.ss_family == AF_INET6)
717 reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id = if_nametoindex (dev.c_str ());
727 if (this->
_addr.ss_family == AF_INET6)
729 char ifname[IFNAMSIZ];
730 if (if_indextoname (
reinterpret_cast<const struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id, ifname))
749 template <
class Protocol>
752 return a.ip () == b.ip () && a.port () == b.port ();
761 template <
class Protocol>
765 if (
auto cmp = a.ip () <=> b.ip (); cmp != 0)
769 return a.port () <=> b.port ();
778 template <
class Protocol>
781 if (endpoint.
protocol () == Protocol::v6 ())
782 os <<
"[" << endpoint.
ip () <<
"]";
784 os << endpoint.
ip ();
785 if (endpoint.
port ())
786 os <<
":" << endpoint.
port ();
basic endpoint class.
Definition endpoint.hpp:50
constexpr BasicEndpoint() noexcept
default constructor.
Definition endpoint.hpp:55
BasicEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:65
const struct sockaddr * addr() const noexcept
get socket address.
Definition endpoint.hpp:83
struct sockaddr * addr() noexcept
get socket address.
Definition endpoint.hpp:74
struct sockaddr_storage _addr
socket address storage.
Definition endpoint.hpp:90
basic internet endpoint class.
Definition endpoint.hpp:517
void port(uint16_t p) noexcept
set endpoint port number.
Definition endpoint.hpp:655
std::string _hostname
endpoint hostname.
Definition endpoint.hpp:740
const std::string & hostname() const noexcept
get the endpoint hostname.
Definition endpoint.hpp:616
std::string device() const
get endpoint device name.
Definition endpoint.hpp:725
uint16_t port() const noexcept
get endpoint port number.
Definition endpoint.hpp:671
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:713
BasicInternetEndpoint(const std::string &ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:566
IpAddress ip() const noexcept
get endpoint IP address.
Definition endpoint.hpp:646
BasicInternetEndpoint(const Protocol &protocol, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:586
BasicInternetEndpoint(const IpAddress &ip, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:541
Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:687
BasicInternetEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create the endpoint instance.
Definition endpoint.hpp:531
void hostname(const std::string &hostname) noexcept
set endpoint hostname.
Definition endpoint.hpp:607
socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:700
constexpr BasicInternetEndpoint() noexcept
default constructor.
Definition endpoint.hpp:522
BasicInternetEndpoint(const char *ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:576
void ip(const IpAddress &ip) noexcept
set endpoint IP address.
Definition endpoint.hpp:625
basic link layer endpoint class.
Definition endpoint.hpp:98
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:160
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:142
BasicLinkLayerEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:114
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:151
BasicLinkLayerEndpoint() noexcept
default constructor.
Definition endpoint.hpp:103
std::string device() const
get endpoint device name.
Definition endpoint.hpp:169
int index() const noexcept
get endpoint interface index.
Definition endpoint.hpp:183
BasicLinkLayerEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:123
BasicLinkLayerEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:133
basic unix endpoint class.
Definition endpoint.hpp:392
BasicUnixEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:416
BasicUnixEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:407
std::string device() const
get endpoint device name.
Definition endpoint.hpp:464
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:445
constexpr BasicUnixEndpoint() noexcept
default constructor.
Definition endpoint.hpp:397
BasicUnixEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:427
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:436
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:454
IPv6, IPv4 address class.
Definition ip_address.hpp:52
uint32_t scope() const
get the scope identifier of the address.
Definition ip_address.cpp:1277
int family() const
get address family.
Definition ip_address.cpp:1250
socklen_t length() const
get the size in byte of the internal address structure.
Definition ip_address.cpp:1268
const void * addr() const
get the internal address structure.
Definition ip_address.cpp:1259
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::ostream & operator<<(std::ostream &os, const BasicLinkLayerEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:221
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208