25#ifndef __JOIN_ENDPOINT_HPP__
26#define __JOIN_ENDPOINT_HPP__
36#include <linux/netfilter/nfnetlink.h>
37#include <linux/if_packet.h>
38#include <linux/rtnetlink.h>
39#include <linux/netlink.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>
114 : BasicEndpoint <Protocol> (
addr, len)
125 struct sockaddr_un* sa =
reinterpret_cast <struct sockaddr_un*
> (&this->
_addr);
126 strncpy (sa->sun_path, dev, sizeof (sa->sun_path) - 1);
151 constexpr socklen_t
length () const noexcept
153 return sizeof (
struct sockaddr_un);
160 void device (
const std::string& dev)
noexcept
162 struct sockaddr_un* sa =
reinterpret_cast <struct sockaddr_un*
> (&this->
_addr);
163 strncpy (sa->sun_path, dev.c_str (), sizeof (sa->sun_path) - 1);
172 return reinterpret_cast <const struct sockaddr_un*
> (&this->
_addr)->sun_path;
182 template <
class Protocol>
183 bool operator== (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
185 return a.device () == b.device ();
194 template <
class Protocol>
195 bool operator!= (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
206 template <
class Protocol>
207 bool operator< (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
209 return a.device () < b.device ();
218 template <
class Protocol>
219 bool operator> (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
230 template <
class Protocol>
231 bool operator<= (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
242 template <
class Protocol>
243 bool operator>= (
const BasicUnixEndpoint <Protocol>& a,
const BasicUnixEndpoint <Protocol>& b)
noexcept
254 template <
class Protocol>
255 std::ostream&
operator<< (std::ostream& os,
const BasicUnixEndpoint <Protocol>& endpoint)
264 template <
class Protocol>
283 : BasicEndpoint <Protocol> (
addr, len)
295 : BasicEndpoint <Protocol> ()
298 struct sockaddr_nl* nl =
reinterpret_cast <struct sockaddr_nl*
> (&this->
_addr);
340 return Protocol::nf ();
342 return Protocol::rt ();
349 constexpr socklen_t
length () const noexcept
351 return sizeof (
struct sockaddr_nl);
360 reinterpret_cast <struct sockaddr_nl*
> (&this->
_addr)->nl_pid =
pid;
367 uint32_t
pid () const noexcept
369 return reinterpret_cast <const struct sockaddr_nl*
> (&this->
_addr)->nl_pid;
378 reinterpret_cast <struct sockaddr_nl*
> (&this->
_addr)->nl_groups =
groups;
387 return reinterpret_cast <const struct sockaddr_nl*
> (&this->
_addr)->nl_groups;
396 return std::string ();
410 template <
class Protocol>
411 bool operator== (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
413 return a.pid () == b.pid () && a.groups () == b.groups ();
422 template <
class Protocol>
423 bool operator!= (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
434 template <
class Protocol>
435 bool operator< (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
437 if (a.pid () != b.pid ())
439 return a.pid () < b.pid ();
441 return a.groups () < b.groups ();
450 template <
class Protocol>
451 bool operator> (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
462 template <
class Protocol>
463 bool operator<= (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
474 template <
class Protocol>
475 bool operator>= (
const BasicNetlinkEndpoint <Protocol>& a,
const BasicNetlinkEndpoint <Protocol>& b)
noexcept
486 template <
class Protocol>
487 std::ostream&
operator<< (std::ostream& os,
const BasicNetlinkEndpoint <Protocol>& endpoint)
489 os <<
"pid=" << endpoint.
pid () <<
",groups=" << endpoint.
groups ();
496 template <
class Protocol>
506 reinterpret_cast <struct sockaddr_ll*
> (&this->
_addr)->sll_protocol = this->
protocol ().protocol ();
515 : BasicEndpoint <Protocol> (
addr, len)
524 : BasicEndpoint <Protocol> ()
526 reinterpret_cast <struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev);
551 constexpr socklen_t
length () const noexcept
553 return sizeof (
struct sockaddr_ll);
560 void device (
const std::string& dev)
noexcept
562 reinterpret_cast <struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex = if_nametoindex (dev.c_str ());
571 char ifname[IFNAMSIZ];
572 if (if_indextoname (
reinterpret_cast <const struct sockaddr_ll*
> (&this->
_addr)->sll_ifindex, ifname))
586 template <
class Protocol>
587 bool operator== (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
589 return a.device () == b.device ();
598 template <
class Protocol>
599 bool operator!= (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
610 template <
class Protocol>
611 bool operator< (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
613 return a.device () < b.device ();
622 template <
class Protocol>
623 bool operator> (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
634 template <
class Protocol>
635 bool operator<= (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
646 template <
class Protocol>
647 bool operator>= (
const BasicLinkLayerEndpoint <Protocol>& a,
const BasicLinkLayerEndpoint <Protocol>& b)
noexcept
658 template <
class Protocol>
659 std::ostream&
operator<< (std::ostream& os,
const BasicLinkLayerEndpoint <Protocol>& endpoint)
668 template <
class Protocol>
685 : BasicEndpoint <Protocol> (
addr, len)
699 struct sockaddr_in6* sa = reinterpret_cast <struct sockaddr_in6*> (&this->_addr);
700 sa->sin6_family = AF_INET6;
701 sa->sin6_port = htons (port);
702 memcpy (&sa->sin6_addr, ip.addr (), ip.length ());
703 sa->sin6_scope_id = ip.scope ();
707 struct sockaddr_in* sa = reinterpret_cast <struct sockaddr_in*> (&this->_addr);
708 sa->sin_family = AF_INET;
709 sa->sin_port = htons (port);
710 memcpy (&sa->sin_addr, ip.addr (), ip.length ());
744 struct sockaddr_in6* sa = reinterpret_cast <struct sockaddr_in6*> (&this->_addr);
745 sa->sin6_family = AF_INET6;
746 sa->sin6_port = htons (port);
750 struct sockaddr_in* sa = reinterpret_cast <struct sockaddr_in*> (&this->_addr);
751 sa->sin_family = AF_INET;
752 sa->sin_port = htons (port);
782 struct sockaddr_in6* sa =
reinterpret_cast <struct sockaddr_in6*
> (&this->
_addr);
783 sa->sin6_family = AF_INET6;
785 sa->sin6_scope_id =
ip.
scope ();
789 struct sockaddr_in* sa =
reinterpret_cast <struct sockaddr_in*
> (&this->
_addr);
790 sa->sin_family = AF_INET;
801 return *
reinterpret_cast <const struct sockaddr*
> (&this->
_addr);
808 void port (uint16_t p)
noexcept
810 if (this->
_addr.ss_family == AF_INET6)
812 reinterpret_cast <struct sockaddr_in6*
> (&this->
_addr)->sin6_port = htons (p);
816 reinterpret_cast <struct sockaddr_in*
> (&this->
_addr)->sin_port = htons (p);
824 uint16_t
port () const noexcept
826 if (this->
_addr.ss_family == AF_INET6)
828 return ntohs (
reinterpret_cast <const struct sockaddr_in6*
> (&this->
_addr)->sin6_port);
832 return ntohs (
reinterpret_cast <const struct sockaddr_in*
> (&this->
_addr)->sin_port);
842 if (this->
_addr.ss_family == AF_INET)
844 return Protocol::v4 ();
846 return Protocol::v6 ();
855 if (this->
_addr.ss_family == AF_INET)
857 return sizeof (
struct sockaddr_in);
859 return sizeof (
struct sockaddr_in6);
866 void device (
const std::string& dev)
noexcept
868 if (this->
_addr.ss_family == AF_INET6)
870 reinterpret_cast <struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id = if_nametoindex (dev.c_str ());
880 if (this->
_addr.ss_family == AF_INET6)
882 char ifname[IFNAMSIZ];
883 if (if_indextoname (
reinterpret_cast <const struct sockaddr_in6*
> (&this->
_addr)->sin6_scope_id, ifname))
902 template <
class Protocol>
903 bool operator== (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
905 return a.ip () == b.ip () && a.port () == b.port ();
914 template <
class Protocol>
915 bool operator!= (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
926 template <
class Protocol>
927 bool operator< (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
929 return std::tie (a.ip (), a.port ()) < std::tie (b.ip (), b.port ());
938 template <
class Protocol>
939 bool operator> (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
950 template <
class Protocol>
951 bool operator<= (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
962 template <
class Protocol>
963 bool operator>= (
const BasicInternetEndpoint <Protocol>& a,
const BasicInternetEndpoint <Protocol>& b)
noexcept
974 template <
class Protocol>
975 std::ostream&
operator<< (std::ostream& os,
const BasicInternetEndpoint <Protocol>& endpoint)
977 if (endpoint.
protocol () == Protocol::v6 ())
978 os <<
"[" << endpoint.
ip () <<
"]";
980 os << endpoint.
ip ();
981 if (endpoint.
port ())
982 os <<
":" << endpoint.
port ();
basic endpoint class.
Definition shared.hpp:613
constexpr BasicEndpoint() noexcept
default constructor.
Definition endpoint.hpp:55
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
BasicEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:65
basic internet endpoint class.
Definition endpoint.hpp:670
void port(uint16_t p) noexcept
set endpoint port number.
Definition endpoint.hpp:808
std::string _hostname
endpoint hostname.
Definition endpoint.hpp:893
const std::string & hostname() const noexcept
get the endpoint hostname.
Definition endpoint.hpp:769
std::string device() const
get endpoint device name.
Definition endpoint.hpp:878
uint16_t port() const noexcept
get endpoint port number.
Definition endpoint.hpp:824
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:866
BasicInternetEndpoint(const std::string &ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:719
IpAddress ip() const noexcept
get endpoint IP address.
Definition endpoint.hpp:799
BasicInternetEndpoint(const Protocol &protocol, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:739
BasicInternetEndpoint(const IpAddress &ip, uint16_t port=0) noexcept
create the endpoint instance.
Definition endpoint.hpp:694
Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:840
BasicInternetEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create the endpoint instance.
Definition endpoint.hpp:684
void hostname(const std::string &hostname) noexcept
set endpoint hostname.
Definition endpoint.hpp:760
socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:853
constexpr BasicInternetEndpoint() noexcept
default constructor.
Definition endpoint.hpp:675
BasicInternetEndpoint(const char *ip, uint16_t port=0)
create the endpoint instance.
Definition endpoint.hpp:729
void ip(const IpAddress &ip) noexcept
set endpoint IP address.
Definition endpoint.hpp:778
basic link layer endpoint class.
Definition endpoint.hpp:498
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:560
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:542
BasicLinkLayerEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:514
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:551
BasicLinkLayerEndpoint() noexcept
default constructor.
Definition endpoint.hpp:503
std::string device() const
get endpoint device name.
Definition endpoint.hpp:569
BasicLinkLayerEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:523
BasicLinkLayerEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:533
basic netlink endpoint class.
Definition endpoint.hpp:266
uint32_t groups() const noexcept
get netlink groups.
Definition endpoint.hpp:385
uint32_t pid() const noexcept
get process id.
Definition endpoint.hpp:367
int _protocol
netlink protocol type.
Definition endpoint.hpp:401
void groups(uint32_t groups) noexcept
set netlink groups.
Definition endpoint.hpp:376
std::string device() const
get device name (not applicable for netlink).
Definition endpoint.hpp:394
constexpr BasicNetlinkEndpoint() noexcept
default constructor.
Definition endpoint.hpp:271
BasicNetlinkEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:282
BasicNetlinkEndpoint(uint32_t pid, uint32_t groups) noexcept
create instance using netlink groups.
Definition endpoint.hpp:308
BasicNetlinkEndpoint(const Protocol &protocol, uint32_t groups) noexcept
create instance using netlink groups.
Definition endpoint.hpp:318
Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:336
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:349
BasicNetlinkEndpoint(const Protocol &protocol, uint32_t pid, uint32_t groups) noexcept
create instance using netlink groups.
Definition endpoint.hpp:294
void pid(uint32_t pid) noexcept
set process id.
Definition endpoint.hpp:358
BasicNetlinkEndpoint(uint32_t groups) noexcept
create instance using netlink groups.
Definition endpoint.hpp:327
basic unix endpoint class.
Definition endpoint.hpp:98
BasicUnixEndpoint(const char *dev) noexcept
create instance using device name.
Definition endpoint.hpp:122
BasicUnixEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition endpoint.hpp:113
std::string device() const
get endpoint device name.
Definition endpoint.hpp:170
constexpr socklen_t length() const noexcept
get socket address length.
Definition endpoint.hpp:151
constexpr BasicUnixEndpoint() noexcept
default constructor.
Definition endpoint.hpp:103
BasicUnixEndpoint(const std::string &dev) noexcept
create instance using device name.
Definition endpoint.hpp:133
constexpr Protocol protocol() const noexcept
get endpoint protocol.
Definition endpoint.hpp:142
void device(const std::string &dev) noexcept
set endpoint device name.
Definition endpoint.hpp:160
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
uint32_t scope() const
get the scope identifier of the address.
Definition ipaddress.cpp:1277
int family() const
get address family.
Definition ipaddress.cpp:1250
socklen_t length() const
get the size in byte of the internal address structure.
Definition ipaddress.cpp:1268
const void * addr() const
get the internal address structure.
Definition ipaddress.cpp:1259
Definition acceptor.hpp:32
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
bool operator>(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:219
bool operator!=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:195
bool operator>=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:243
std::ostream & operator<<(std::ostream &os, const BasicUnixEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:255
bool operator<=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:231
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183