join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
endpoint.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_ENDPOINT_HPP
26#define JOIN_CORE_ENDPOINT_HPP
27
28// libjoin.
29#include <join/mac_address.hpp>
30
31// C++.
32#include <ostream>
33
34// C.
35#include <linux/if_packet.h>
36#ifdef JOIN_HAS_XDP
37#include <linux/if_xdp.h>
38#endif
39#include <sys/un.h>
40#include <cstring>
41
42namespace join
43{
47 template <class Protocol>
49 {
50 public:
54 constexpr BasicEndpoint () noexcept
55 {
56 this->_addr.ss_family = Protocol ().family ();
57 }
58
64 BasicEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
65 {
66 memcpy (&this->_addr, addr, len);
67 }
68
73 struct sockaddr* addr () noexcept
74 {
75 return reinterpret_cast<struct sockaddr*> (&this->_addr);
76 }
77
82 const struct sockaddr* addr () const noexcept
83 {
84 return reinterpret_cast<const struct sockaddr*> (&this->_addr);
85 }
86
87 protected:
89 struct sockaddr_storage _addr = {};
90 };
91
95 template <class Protocol>
96 class BasicLinkLayerEndpoint : public BasicEndpoint<Protocol>
97 {
98 public:
103 : BasicEndpoint<Protocol> ()
104 {
105 reinterpret_cast<struct sockaddr_ll*> (&this->_addr)->sll_protocol = this->protocol ().protocol ();
106 }
107
113 BasicLinkLayerEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
115 {
116 }
117
122 BasicLinkLayerEndpoint (const char* dev) noexcept
124 {
125 reinterpret_cast<struct sockaddr_ll*> (&this->_addr)->sll_ifindex = if_nametoindex (dev);
126 }
127
132 BasicLinkLayerEndpoint (const std::string& dev) noexcept
133 : BasicLinkLayerEndpoint<Protocol> (dev.c_str ())
134 {
135 }
136
141 constexpr Protocol protocol () const noexcept
142 {
143 return Protocol ();
144 }
145
150 constexpr socklen_t length () const noexcept
151 {
152 return sizeof (struct sockaddr_ll);
153 }
154
159 void device (const std::string& dev) noexcept
160 {
161 reinterpret_cast<struct sockaddr_ll*> (&this->_addr)->sll_ifindex = if_nametoindex (dev.c_str ());
162 }
163
168 std::string device () const
169 {
170 char ifname[IFNAMSIZ];
171 if (if_indextoname (reinterpret_cast<const struct sockaddr_ll*> (&this->_addr)->sll_ifindex, ifname))
172 {
173 return ifname;
174 }
175 return {};
176 }
177 };
178
185 template <class Protocol>
187 {
188 return a.device () == b.device ();
189 }
190
197 template <class Protocol>
199 {
200 return !(a == b);
201 }
202
209 template <class Protocol>
211 {
212 return a.device () < b.device ();
213 }
214
221 template <class Protocol>
223 {
224 return b < a;
225 }
226
233 template <class Protocol>
235 {
236 return !(b < a);
237 }
238
245 template <class Protocol>
247 {
248 return !(a < b);
249 }
250
257 template <class Protocol>
258 std::ostream& operator<< (std::ostream& os, const BasicLinkLayerEndpoint<Protocol>& endpoint)
259 {
260 os << endpoint.device ();
261 return os;
262 }
263
264#ifdef JOIN_HAS_XDP
268 template <class Protocol>
269 class BasicXdpEndpoint : public BasicEndpoint<Protocol>
270 {
271 public:
275 BasicXdpEndpoint () noexcept
276 : BasicEndpoint<Protocol> ()
277 {
278 }
279
285 BasicXdpEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
286 : BasicEndpoint<Protocol> (addr, len)
287 {
288 }
289
295 BasicXdpEndpoint (const char* dev, uint32_t queue = 0) noexcept
296 : BasicXdpEndpoint ()
297 {
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;
301 }
302
308 BasicXdpEndpoint (const std::string& dev, uint32_t queue = 0) noexcept
309 : BasicXdpEndpoint<Protocol> (dev.c_str (), queue)
310 {
311 }
312
317 constexpr Protocol protocol () const noexcept
318 {
319 return Protocol ();
320 }
321
326 constexpr socklen_t length () const noexcept
327 {
328 return sizeof (struct sockaddr_xdp);
329 }
330
335 void device (const std::string& dev) noexcept
336 {
337 reinterpret_cast<struct sockaddr_xdp*> (&this->_addr)->sxdp_ifindex = if_nametoindex (dev.c_str ());
338 }
339
344 std::string device () const
345 {
346 char ifname[IFNAMSIZ];
347 if (if_indextoname (reinterpret_cast<const struct sockaddr_xdp*> (&this->_addr)->sxdp_ifindex, ifname))
348 {
349 return ifname;
350 }
351 return {};
352 }
353
358 void queue (uint32_t queue) noexcept
359 {
360 reinterpret_cast<struct sockaddr_xdp*> (&this->_addr)->sxdp_queue_id = queue;
361 }
362
367 uint32_t queue () const noexcept
368 {
369 return reinterpret_cast<const struct sockaddr_xdp*> (&this->_addr)->sxdp_queue_id;
370 }
371 };
372
379 template <class Protocol>
380 bool operator== (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
381 {
382 return a.device () == b.device () && a.queue () == b.queue ();
383 }
384
391 template <class Protocol>
392 bool operator!= (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
393 {
394 return !(a == b);
395 }
396
403 template <class Protocol>
404 bool operator< (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
405 {
406 return std::make_tuple (a.device (), a.queue ()) < std::make_tuple (b.device (), b.queue ());
407 }
408
415 template <class Protocol>
416 bool operator> (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
417 {
418 return b < a;
419 }
420
427 template <class Protocol>
428 bool operator<= (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
429 {
430 return !(b < a);
431 }
432
439 template <class Protocol>
440 bool operator>= (const BasicXdpEndpoint<Protocol>& a, const BasicXdpEndpoint<Protocol>& b) noexcept
441 {
442 return !(a < b);
443 }
444
451 template <class Protocol>
452 std::ostream& operator<< (std::ostream& os, const BasicXdpEndpoint<Protocol>& endpoint)
453 {
454 os << endpoint.device () << ":" << endpoint.queue ();
455 return os;
456 }
457#endif
458
462 template <class Protocol>
463 class BasicUnixEndpoint : public BasicEndpoint<Protocol>
464 {
465 public:
469 constexpr BasicUnixEndpoint () noexcept
470 : BasicEndpoint<Protocol> ()
471 {
472 }
473
479 BasicUnixEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
481 {
482 }
483
488 BasicUnixEndpoint (const char* dev) noexcept
490 {
491 struct sockaddr_un* sa = reinterpret_cast<struct sockaddr_un*> (&this->_addr);
492 strncpy (sa->sun_path, dev, sizeof (sa->sun_path) - 1);
493 }
494
499 BasicUnixEndpoint (const std::string& dev) noexcept
500 : BasicUnixEndpoint<Protocol> (dev.c_str ())
501 {
502 }
503
508 constexpr Protocol protocol () const noexcept
509 {
510 return Protocol ();
511 }
512
517 constexpr socklen_t length () const noexcept
518 {
519 return sizeof (struct sockaddr_un);
520 }
521
526 void device (const std::string& dev) noexcept
527 {
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);
530 }
531
536 std::string device () const
537 {
538 return reinterpret_cast<const struct sockaddr_un*> (&this->_addr)->sun_path;
539 }
540 };
541
548 template <class Protocol>
550 {
551 return a.device () == b.device ();
552 }
553
560 template <class Protocol>
562 {
563 return !(a == b);
564 }
565
572 template <class Protocol>
574 {
575 return a.device () < b.device ();
576 }
577
584 template <class Protocol>
586 {
587 return b < a;
588 }
589
596 template <class Protocol>
598 {
599 return !(b < a);
600 }
601
608 template <class Protocol>
610 {
611 return !(a < b);
612 }
613
620 template <class Protocol>
621 std::ostream& operator<< (std::ostream& os, const BasicUnixEndpoint<Protocol>& endpoint)
622 {
623 os << endpoint.device ();
624 return os;
625 }
626
630 template <class Protocol>
631 class BasicInternetEndpoint : public BasicEndpoint<Protocol>
632 {
633 public:
637 constexpr BasicInternetEndpoint () noexcept
638 : BasicEndpoint<Protocol> ()
639 {
640 }
641
646 BasicInternetEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
648 {
649 }
650
656 BasicInternetEndpoint (const IpAddress& ip, uint16_t port = 0) noexcept
657 : BasicEndpoint<Protocol> ()
658 {
659 if (ip.family () == AF_INET6)
660 {
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 ();
666 }
667 else
668 {
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 ());
673 }
674 }
675
681 BasicInternetEndpoint (const std::string& ip, uint16_t port = 0)
682 : BasicInternetEndpoint<Protocol> (IpAddress (ip), port)
683 {
684 }
685
691 BasicInternetEndpoint (const char* ip, uint16_t port = 0)
692 : BasicInternetEndpoint<Protocol> (IpAddress (ip), port)
693 {
694 }
695
701 BasicInternetEndpoint (const Protocol& protocol, uint16_t port = 0) noexcept
702 : BasicEndpoint<Protocol> ()
703 {
704 if (protocol.family () == AF_INET6)
705 {
706 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
707 sa->sin6_family = AF_INET6;
708 sa->sin6_port = htons (port);
709 }
710 else
711 {
712 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
713 sa->sin_family = AF_INET;
714 sa->sin_port = htons (port);
715 }
716 }
717
722 void hostname (const std::string& hostname) noexcept
723 {
725 }
726
731 const std::string& hostname () const noexcept
732 {
733 return _hostname;
734 }
735
740 void ip (const IpAddress& ip) noexcept
741 {
742 if (ip.family () == AF_INET6)
743 {
744 struct sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*> (&this->_addr);
745 sa->sin6_family = AF_INET6;
746 memcpy (&sa->sin6_addr, ip.addr (), ip.length ());
747 sa->sin6_scope_id = ip.scope ();
748 }
749 else
750 {
751 struct sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*> (&this->_addr);
752 sa->sin_family = AF_INET;
753 memcpy (&sa->sin_addr, ip.addr (), ip.length ());
754 }
755 }
756
761 IpAddress ip () const noexcept
762 {
763 return *reinterpret_cast<const struct sockaddr*> (&this->_addr);
764 }
765
770 void port (uint16_t p) noexcept
771 {
772 if (this->_addr.ss_family == AF_INET6)
773 {
774 reinterpret_cast<struct sockaddr_in6*> (&this->_addr)->sin6_port = htons (p);
775 }
776 else
777 {
778 reinterpret_cast<struct sockaddr_in*> (&this->_addr)->sin_port = htons (p);
779 }
780 }
781
786 uint16_t port () const noexcept
787 {
788 if (this->_addr.ss_family == AF_INET6)
789 {
790 return ntohs (reinterpret_cast<const struct sockaddr_in6*> (&this->_addr)->sin6_port);
791 }
792 else
793 {
794 return ntohs (reinterpret_cast<const struct sockaddr_in*> (&this->_addr)->sin_port);
795 }
796 }
797
802 Protocol protocol () const noexcept
803 {
804 if (this->_addr.ss_family == AF_INET)
805 {
806 return Protocol::v4 ();
807 }
808 return Protocol::v6 ();
809 }
810
815 socklen_t length () const noexcept
816 {
817 if (this->_addr.ss_family == AF_INET)
818 {
819 return sizeof (struct sockaddr_in);
820 }
821 return sizeof (struct sockaddr_in6);
822 }
823
828 void device (const std::string& dev) noexcept
829 {
830 if (this->_addr.ss_family == AF_INET6)
831 {
832 reinterpret_cast<struct sockaddr_in6*> (&this->_addr)->sin6_scope_id = if_nametoindex (dev.c_str ());
833 }
834 }
835
840 std::string device () const
841 {
842 if (this->_addr.ss_family == AF_INET6)
843 {
844 char ifname[IFNAMSIZ];
845 if (if_indextoname (reinterpret_cast<const struct sockaddr_in6*> (&this->_addr)->sin6_scope_id, ifname))
846 {
847 return ifname;
848 }
849 }
850 return {};
851 }
852
853 protected:
855 std::string _hostname;
856 };
857
864 template <class Protocol>
866 {
867 return a.ip () == b.ip () && a.port () == b.port ();
868 }
869
876 template <class Protocol>
878 {
879 return !(a == b);
880 }
881
888 template <class Protocol>
890 {
891 return std::tie (a.ip (), a.port ()) < std::tie (b.ip (), b.port ());
892 }
893
900 template <class Protocol>
902 {
903 return b < a;
904 }
905
912 template <class Protocol>
914 {
915 return !(b < a);
916 }
917
924 template <class Protocol>
926 {
927 return !(a < b);
928 }
929
936 template <class Protocol>
937 std::ostream& operator<< (std::ostream& os, const BasicInternetEndpoint<Protocol>& endpoint)
938 {
939 if (endpoint.protocol () == Protocol::v6 ())
940 os << "[" << endpoint.ip () << "]";
941 else
942 os << endpoint.ip ();
943 if (endpoint.port ())
944 os << ":" << endpoint.port ();
945 return os;
946 }
947}
948
949#endif
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