25#ifndef JOIN_CORE_ENDPOINT_HPP
26#define JOIN_CORE_ENDPOINT_HPP
35#include <linux/if_packet.h>
37#include <linux/if_xdp.h>
47 template <
class Protocol>
56 this->
_addr.ss_family = Protocol ().family ();
73 struct sockaddr*
addr () noexcept
75 return reinterpret_cast<struct sockaddr*
> (&this->
_addr);
82 const struct sockaddr*
addr () const noexcept
84 return reinterpret_cast<const struct sockaddr*
> (&this->
_addr);
89 struct sockaddr_storage
_addr = {};
95 template <
class Protocol>
105 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_protocol = this->
protocol ().protocol ();
125 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev);
150 constexpr socklen_t
length () const noexcept
152 return sizeof (
struct sockaddr_ll);
159 void device (
const std::string& dev)
noexcept
161 reinterpret_cast<struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev.c_str ());
170 char ifname[IFNAMSIZ];
171 if (if_indextoname (
reinterpret_cast<const struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex, ifname))
185 template <
class Protocol>
188 return a.device () == b.device ();
197 template <
class Protocol>
209 template <
class Protocol>
212 return a.device () < b.device ();
221 template <
class Protocol>
233 template <
class Protocol>
245 template <
class Protocol>
257 template <
class Protocol>
268 template <
class Protocol>
269 class BasicXdpEndpoint :
public BasicEndpoint<Protocol>
275 BasicXdpEndpoint () noexcept
276 : BasicEndpoint<Protocol> ()
285 BasicXdpEndpoint (
const struct sockaddr* addr, socklen_t len) noexcept
286 : BasicEndpoint<Protocol> (addr, len)
295 BasicXdpEndpoint (
const char* dev, uint32_t queue = 0) noexcept
296 : BasicXdpEndpoint ()
298 struct sockaddr_xdp* sa =
reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr);
299 sa->sxdp_ifindex = if_nametoindex (dev);
300 sa->sxdp_queue_id = queue;
308 BasicXdpEndpoint (
const std::string& dev, uint32_t queue = 0) noexcept
309 : BasicXdpEndpoint<Protocol> (dev.c_str (), queue)
317 constexpr Protocol protocol () const noexcept
326 constexpr socklen_t length () const noexcept
328 return sizeof (
struct sockaddr_xdp);
335 void device (
const std::string& dev)
noexcept
337 reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr)->sxdp_ifindex = if_nametoindex (dev.c_str ());
344 std::string device ()
const
346 char ifname[IFNAMSIZ];
347 if (if_indextoname (
reinterpret_cast<const struct sockaddr_xdp*
> (&this->_addr)->sxdp_ifindex, ifname))
358 void queue (uint32_t queue)
noexcept
360 reinterpret_cast<struct sockaddr_xdp*
> (&this->_addr)->sxdp_queue_id = queue;
367 uint32_t queue () const noexcept
369 return reinterpret_cast<const struct sockaddr_xdp*
> (&this->_addr)->sxdp_queue_id;
379 template <
class Protocol>
380 bool operator== (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
382 return a.device () == b.device () && a.queue () == b.queue ();
391 template <
class Protocol>
392 bool operator!= (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
403 template <
class Protocol>
404 bool operator< (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
406 return std::make_tuple (a.device (), a.queue ()) < std::make_tuple (b.device (), b.queue ());
415 template <
class Protocol>
416 bool operator> (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
427 template <
class Protocol>
428 bool operator<= (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
439 template <
class Protocol>
440 bool operator>= (
const BasicXdpEndpoint<Protocol>& a,
const BasicXdpEndpoint<Protocol>& b)
noexcept
451 template <
class Protocol>
452 std::ostream&
operator<< (std::ostream& os,
const BasicXdpEndpoint<Protocol>& endpoint)
454 os << endpoint.device () <<
":" << endpoint.queue ();
462 template <
class Protocol>
491 struct sockaddr_un* sa =
reinterpret_cast<struct sockaddr_un*
> (&this->
_addr);
492 strncpy (sa->sun_path, dev, sizeof (sa->sun_path) - 1);
517 constexpr socklen_t
length () const noexcept
519 return sizeof (
struct sockaddr_un);
526 void device (
const std::string& dev)
noexcept
528 struct sockaddr_un* sa =
reinterpret_cast<struct sockaddr_un*
> (&this->
_addr);
529 strncpy (sa->sun_path, dev.c_str (), sizeof (sa->sun_path) - 1);
538 return reinterpret_cast<const struct sockaddr_un*
> (&this->
_addr)->sun_path;
548 template <
class Protocol>
551 return a.device () == b.device ();
560 template <
class Protocol>
572 template <
class Protocol>
575 return a.device () < b.device ();
584 template <
class Protocol>
596 template <
class Protocol>
608 template <
class Protocol>
620 template <
class Protocol>
630 template <
class Protocol>
661 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
662 sa->sin6_family = AF_INET6;
663 sa->sin6_port = htons (port);
664 memcpy (&sa->sin6_addr, ip.addr (), ip.length ());
665 sa->sin6_scope_id = ip.scope ();
669 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
670 sa->sin_family = AF_INET;
671 sa->sin_port = htons (port);
672 memcpy (&sa->sin_addr, ip.addr (), ip.length ());
706 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
707 sa->sin6_family = AF_INET6;
708 sa->sin6_port = htons (port);
712 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
713 sa->sin_family = AF_INET;
714 sa->sin_port = htons (port);
744 struct sockaddr_in6* sa =
reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr);
745 sa->sin6_family = AF_INET6;
747 sa->sin6_scope_id =
ip.
scope ();
751 struct sockaddr_in* sa =
reinterpret_cast<struct sockaddr_in*
> (&this->
_addr);
752 sa->sin_family = AF_INET;
763 return *
reinterpret_cast<const struct sockaddr*
> (&this->
_addr);
770 void port (uint16_t p)
noexcept
772 if (this->
_addr.ss_family == AF_INET6)
774 reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr)->sin6_port = htons (p);
778 reinterpret_cast<struct sockaddr_in*
> (&this->
_addr)->sin_port = htons (p);
786 uint16_t
port () const noexcept
788 if (this->
_addr.ss_family == AF_INET6)
790 return ntohs (
reinterpret_cast<const struct sockaddr_in6*
> (&this->
_addr)->sin6_port);
794 return ntohs (
reinterpret_cast<const struct sockaddr_in*
> (&this->
_addr)->sin_port);
804 if (this->
_addr.ss_family == AF_INET)
806 return Protocol::v4 ();
808 return Protocol::v6 ();
817 if (this->
_addr.ss_family == AF_INET)
819 return sizeof (
struct sockaddr_in);
821 return sizeof (
struct sockaddr_in6);
828 void device (
const std::string& dev)
noexcept
830 if (this->
_addr.ss_family == AF_INET6)
832 reinterpret_cast<struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id = if_nametoindex (dev.c_str ());
842 if (this->
_addr.ss_family == AF_INET6)
844 char ifname[IFNAMSIZ];
845 if (if_indextoname (
reinterpret_cast<const struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id, ifname))
864 template <
class Protocol>
867 return a.ip () == b.ip () && a.port () == b.port ();
876 template <
class Protocol>
888 template <
class Protocol>
891 return std::tie (a.ip (), a.port ()) < std::tie (b.ip (), b.port ());
900 template <
class Protocol>
912 template <
class Protocol>
924 template <
class Protocol>
936 template <
class Protocol>
939 if (endpoint.
protocol () == Protocol::v6 ())
940 os <<
"[" << endpoint.
ip () <<
"]";
942 os << endpoint.
ip ();
943 if (endpoint.
port ())
944 os <<
":" << endpoint.
port ();
basic endpoint class.
Definition endpoint.hpp:49
constexpr BasicEndpoint() noexcept
default constructor.
Definition endpoint.hpp:54
BasicEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:64
const struct sockaddr * addr() const noexcept
get socket address.
Definition endpoint.hpp:82
struct sockaddr * addr() noexcept
get socket address.
Definition endpoint.hpp:73
struct sockaddr_storage _addr
socket address storage.
Definition endpoint.hpp:89
basic internet endpoint class.
Definition endpoint.hpp:632
void port(uint16_t p) noexcept
set endpoint port number.
Definition endpoint.hpp:770
std::string _hostname
endpoint hostname.
Definition endpoint.hpp:855
const std::string & hostname() const noexcept
get the endpoint hostname.
Definition endpoint.hpp:731
std::string device() const
get endpoint device name.
Definition endpoint.hpp:840
uint16_t port() const noexcept
get endpoint port number.
Definition endpoint.hpp:786
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:828
BasicInternetEndpoint(const std::string &ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:681
IpAddress ip() const noexcept
get endpoint IP address.
Definition endpoint.hpp:761
BasicInternetEndpoint(const Protocol &protocol, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:701
BasicInternetEndpoint(const IpAddress &ip, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:656
Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:802
BasicInternetEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create the endpoint instance.
Definition endpoint.hpp:646
void hostname(const std::string &hostname) noexcept
set endpoint hostname.
Definition endpoint.hpp:722
socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:815
constexpr BasicInternetEndpoint() noexcept
default constructor.
Definition endpoint.hpp:637
BasicInternetEndpoint(const char *ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:691
void ip(const IpAddress &ip) noexcept
set endpoint IP address.
Definition endpoint.hpp:740
basic link layer endpoint class.
Definition endpoint.hpp:97
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:159
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:141
BasicLinkLayerEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:113
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:150
BasicLinkLayerEndpoint() noexcept
default constructor.
Definition endpoint.hpp:102
std::string device() const
get endpoint device name.
Definition endpoint.hpp:168
BasicLinkLayerEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:122
BasicLinkLayerEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:132
basic unix endpoint class.
Definition endpoint.hpp:464
BasicUnixEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:488
BasicUnixEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:479
std::string device() const
get endpoint device name.
Definition endpoint.hpp:536
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:517
constexpr BasicUnixEndpoint() noexcept
default constructor.
Definition endpoint.hpp:469
BasicUnixEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:499
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:508
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:526
IPv6, IPv4 address class.
Definition ip_address.hpp:51
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 endpoint is lower or equal.
Definition endpoint.hpp:234
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:186
bool operator!=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:198
bool operator>=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:246
std::ostream & operator<<(std::ostream &os, const BasicLinkLayerEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:258
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
bool operator>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:222