join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
protocol.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_PROTOCOL_HPP__
26#define __JOIN_PROTOCOL_HPP__
27
28// libjoin.
29#include <join/endpoint.hpp>
30
31// C.
32#include <linux/netlink.h>
33#include <net/ethernet.h>
34
35namespace join
36{
37 template <class Protocol> class BasicSocket;
38 template <class Protocol> class BasicDatagramSocket;
39 template <class Protocol> class BasicStreamSocket;
40 template <class Protocol> class BasicTlsSocket;
41
42 template <class Protocol> class BasicSocketStream;
43 template <class Protocol> class BasicTlsStream;
44
45 template <class Protocol> class BasicStreamAcceptor;
46 template <class Protocol> class BasicTlsAcceptor;
47
48 template <class Protocol> class BasicHttpClient;
49 template <class Protocol> class BasicHttpSecureClient;
50
51 template <class Protocol> class BasicHttpWorker;
52 template <class Protocol> class BasicHttpServer;
53 template <class Protocol> class BasicHttpSecureServer;
54
55 template <class Protocol> class BasicSmtpClient;
56 template <class Protocol> class BasicSmtpSecureClient;
57
62 {
63 public:
64 using Endpoint = BasicUnixEndpoint <UnixDgram>;
65 using Socket = BasicDatagramSocket <UnixDgram>;
66
70 constexpr UnixDgram () noexcept = default;
71
76 constexpr int family () const noexcept
77 {
78 return AF_UNIX;
79 }
80
85 constexpr int type () const noexcept
86 {
87 return SOCK_DGRAM;
88 }
89
94 constexpr int protocol () const noexcept
95 {
96 return 0;
97 }
98 };
99
104 {
105 public:
106 using Endpoint = BasicUnixEndpoint <UnixStream>;
107 using Socket = BasicStreamSocket <UnixStream>;
108 using Stream = BasicSocketStream <UnixStream>;
109 using Acceptor = BasicStreamAcceptor <UnixStream>;
110
114 constexpr UnixStream () noexcept = default;
115
120 constexpr int family () const noexcept
121 {
122 return AF_UNIX;
123 }
124
129 constexpr int type () const noexcept
130 {
131 return SOCK_STREAM;
132 }
133
138 constexpr int protocol () const noexcept
139 {
140 return 0;
141 }
142 };
143
148 {
149 public:
150 using Endpoint = BasicNetlinkEndpoint <Netlink>;
151 using Socket = BasicDatagramSocket <Netlink>;
152
157 constexpr Netlink (int proto = NETLINK_ROUTE) noexcept
158 : _proto (proto)
159 {
160 }
161
166 static inline Netlink& rt () noexcept
167 {
168 static Netlink route (NETLINK_ROUTE);
169 return route;
170 }
171
176 static inline Netlink& nf () noexcept
177 {
178 static Netlink netfilter (NETLINK_NETFILTER);
179 return netfilter;
180 }
181
186 constexpr int family () const noexcept
187 {
188 return AF_NETLINK;
189 }
190
195 constexpr int type () const noexcept
196 {
197 return SOCK_RAW;
198 }
199
204 constexpr int protocol () const noexcept
205 {
206 return _proto;
207 }
208
209 private:
211 int _proto;
212 };
213
220 constexpr bool operator== (const Netlink& a, const Netlink& b) noexcept
221 {
222 return a.protocol () == b.protocol ();
223 }
224
231 constexpr bool operator!= (const Netlink& a, const Netlink& b) noexcept
232 {
233 return !(a == b);
234 }
235
239 class Raw
240 {
241 public:
242 using Endpoint = BasicLinkLayerEndpoint <Raw>;
243 using Socket = BasicSocket <Raw>;
244
248 constexpr Raw () noexcept = default;
249
254 constexpr int family () const noexcept
255 {
256 return AF_PACKET;
257 }
258
263 constexpr int type () const noexcept
264 {
265 return SOCK_RAW;
266 }
267
272 constexpr int protocol () const noexcept
273 {
274 if (BYTE_ORDER == LITTLE_ENDIAN)
275 {
276 return (ETH_P_ALL >> 8) | (ETH_P_ALL << 8);
277 }
278 return ETH_P_ALL;
279 }
280 };
281
285 class Udp
286 {
287 public:
288 using Endpoint = BasicInternetEndpoint <Udp>;
289 using Socket = BasicDatagramSocket <Udp>;
290
295 constexpr Udp (int family = AF_INET) noexcept
296 : _family (family)
297 {
298 }
299
304 static inline Udp& v4 () noexcept
305 {
306 static Udp udpv4 (AF_INET);
307 return udpv4;
308 }
309
314 static inline Udp& v6 () noexcept
315 {
316 static Udp udpv6 (AF_INET6);
317 return udpv6;
318 }
319
324 constexpr int family () const noexcept
325 {
326 return _family;
327 }
328
333 constexpr int type () const noexcept
334 {
335 return SOCK_DGRAM;
336 }
337
342 constexpr int protocol () const noexcept
343 {
344 return IPPROTO_UDP;
345 }
346
347 private:
349 int _family;
350 };
351
358 constexpr bool operator== (const Udp& a, const Udp& b) noexcept
359 {
360 return a.family () == b.family ();
361 }
362
369 constexpr bool operator!= (const Udp& a, const Udp& b) noexcept
370 {
371 return !(a == b);
372 }
373
377 class Icmp
378 {
379 public:
380 using Endpoint = BasicInternetEndpoint <Icmp>;
381 using Socket = BasicDatagramSocket <Icmp>;
382
387 constexpr Icmp (int family = AF_INET) noexcept
388 : _family (family)
389 {
390 }
391
396 static inline Icmp& v4 () noexcept
397 {
398 static Icmp icmpv4 (AF_INET);
399 return icmpv4;
400 }
401
406 static inline Icmp& v6 () noexcept
407 {
408 static Icmp icmpv6 (AF_INET6);
409 return icmpv6;
410 }
411
416 constexpr int family () const noexcept
417 {
418 return _family;
419 }
420
425 constexpr int type () const noexcept
426 {
427 return SOCK_RAW;
428 }
429
434 constexpr int protocol () const noexcept
435 {
436 if (family () == AF_INET6)
437 {
438 return int (IPPROTO_ICMPV6);
439 }
440
441 return IPPROTO_ICMP;
442 }
443
444 private:
446 int _family;
447 };
448
455 constexpr bool operator== (const Icmp& a, const Icmp& b) noexcept
456 {
457 return a.family () == b.family ();
458 }
459
466 constexpr bool operator!= (const Icmp& a, const Icmp& b) noexcept
467 {
468 return !(a == b);
469 }
470
474 class Tcp
475 {
476 public:
477 using Endpoint = BasicInternetEndpoint <Tcp>;
478 using Socket = BasicStreamSocket <Tcp>;
479 using Stream = BasicSocketStream <Tcp>;
480 using Acceptor = BasicStreamAcceptor <Tcp>;
481
486 constexpr Tcp (int family = AF_INET) noexcept
487 : _family (family)
488 {
489 }
490
495 static inline Tcp& v4 () noexcept
496 {
497 static Tcp tcpv4 (AF_INET);
498 return tcpv4;
499 }
500
505 static inline Tcp& v6 () noexcept
506 {
507 static Tcp tcpv6 (AF_INET6);
508 return tcpv6;
509 }
510
515 constexpr int family () const noexcept
516 {
517 return _family;
518 }
519
524 constexpr int type () const noexcept
525 {
526 return SOCK_STREAM;
527 }
528
533 constexpr int protocol () const noexcept
534 {
535 return IPPROTO_TCP;
536 }
537
538 private:
540 int _family;
541 };
542
549 constexpr bool operator== (const Tcp& a, const Tcp& b) noexcept
550 {
551 return a.family () == b.family ();
552 }
553
560 constexpr bool operator!= (const Tcp& a, const Tcp& b) noexcept
561 {
562 return !(a == b);
563 }
564
568 class Tls
569 {
570 public:
571 using Endpoint = BasicInternetEndpoint <Tls>;
572 using Socket = BasicTlsSocket <Tls>;
573 using Stream = BasicTlsStream <Tls>;
574 using Acceptor = BasicTlsAcceptor <Tls>;
575
580 constexpr Tls (int family = AF_INET) noexcept
581 : _family (family)
582 {
583 }
584
589 static inline Tls& v4 () noexcept
590 {
591 static Tls tlsv4 (AF_INET);
592 return tlsv4;
593 }
594
599 static inline Tls& v6 () noexcept
600 {
601 static Tls tlsv6 (AF_INET6);
602 return tlsv6;
603 }
604
609 constexpr int family () const noexcept
610 {
611 return _family;
612 }
613
618 constexpr int type () const noexcept
619 {
620 return SOCK_STREAM;
621 }
622
627 constexpr int protocol () const noexcept
628 {
629 return IPPROTO_TCP;
630 }
631
632 private:
634 int _family;
635 };
636
643 constexpr bool operator== (const Tls& a, const Tls& b) noexcept
644 {
645 return a.family () == b.family ();
646 }
647
654 constexpr bool operator!= (const Tls& a, const Tls& b) noexcept
655 {
656 return !(a == b);
657 }
658
662 class Http
663 {
664 public:
665 using Endpoint = BasicInternetEndpoint <Http>;
666 using Socket = BasicStreamSocket <Http>;
667 using Stream = BasicSocketStream <Http>;
668 using Acceptor = BasicStreamAcceptor <Http>;
669 using Client = BasicHttpClient <Http>;
670 using Worker = BasicHttpWorker <Http>;
671 using Server = BasicHttpServer <Http>;
672
677 constexpr Http (int family = AF_INET) noexcept
678 : _family (family)
679 {
680 }
681
686 static inline Http& v4 () noexcept
687 {
688 static Http httpv4 (AF_INET);
689 return httpv4;
690 }
691
696 static inline Http& v6 () noexcept
697 {
698 static Http httpv6 (AF_INET6);
699 return httpv6;
700 }
701
706 constexpr int family () const noexcept
707 {
708 return _family;
709 }
710
715 constexpr int type () const noexcept
716 {
717 return SOCK_STREAM;
718 }
719
724 constexpr int protocol () const noexcept
725 {
726 return IPPROTO_TCP;
727 }
728
729 private:
731 int _family;
732 };
733
740 constexpr bool operator== (const Http& a, const Http& b) noexcept
741 {
742 return a.family () == b.family ();
743 }
744
751 constexpr bool operator!= (const Http& a, const Http& b) noexcept
752 {
753 return !(a == b);
754 }
755
759 class Https
760 {
761 public:
762 using Endpoint = BasicInternetEndpoint <Https>;
763 using Socket = BasicTlsSocket <Https>;
764 using Stream = BasicTlsStream <Https>;
765 using Acceptor = BasicTlsAcceptor <Https>;
766 using Client = BasicHttpSecureClient <Https>;
767 using Worker = BasicHttpWorker <Https>;
768 using Server = BasicHttpSecureServer <Https>;
769
774 constexpr Https (int family = AF_INET) noexcept
775 : _family (family)
776 {
777 }
778
783 static inline Https& v4 () noexcept
784 {
785 static Https httpsv4 (AF_INET);
786 return httpsv4;
787 }
788
793 static inline Https& v6 () noexcept
794 {
795 static Https httpsv6 (AF_INET6);
796 return httpsv6;
797 }
798
803 constexpr int family () const noexcept
804 {
805 return _family;
806 }
807
812 constexpr int type () const noexcept
813 {
814 return SOCK_STREAM;
815 }
816
821 constexpr int protocol () const noexcept
822 {
823 return IPPROTO_TCP;
824 }
825
826 private:
828 int _family;
829 };
830
837 constexpr bool operator== (const Https& a, const Https& b) noexcept
838 {
839 return a.family () == b.family ();
840 }
841
848 constexpr bool operator!= (const Https& a, const Https& b) noexcept
849 {
850 return !(a == b);
851 }
852
856 class Smtp
857 {
858 public:
859 using Endpoint = BasicInternetEndpoint <Smtp>;
860 using Socket = BasicTlsSocket <Smtp>;
861 using Stream = BasicTlsStream <Smtp>;
862 using Client = BasicSmtpClient <Smtp>;
863
868 constexpr Smtp (int family = AF_INET) noexcept
869 : _family (family)
870 {
871 }
872
877 static inline Smtp& v4 () noexcept
878 {
879 static Smtp smtpv4 (AF_INET);
880 return smtpv4;
881 }
882
887 static inline Smtp& v6 () noexcept
888 {
889 static Smtp smtpv6 (AF_INET6);
890 return smtpv6;
891 }
892
897 constexpr int family () const noexcept
898 {
899 return _family;
900 }
901
906 constexpr int type () const noexcept
907 {
908 return SOCK_STREAM;
909 }
910
915 constexpr int protocol () const noexcept
916 {
917 return IPPROTO_TCP;
918 }
919
920 private:
922 int _family;
923 };
924
931 constexpr bool operator== (const Smtp& a, const Smtp& b) noexcept
932 {
933 return a.family () == b.family ();
934 }
935
942 constexpr bool operator!= (const Smtp& a, const Smtp& b) noexcept
943 {
944 return !(a == b);
945 }
946
950 class Smtps
951 {
952 public:
953 using Endpoint = BasicInternetEndpoint <Smtps>;
954 using Socket = BasicTlsSocket <Smtps>;
955 using Stream = BasicTlsStream <Smtps>;
956 using Client = BasicSmtpSecureClient <Smtps>;
957
962 constexpr Smtps (int family = AF_INET) noexcept
963 : _family (family)
964 {
965 }
966
971 static inline Smtps& v4 () noexcept
972 {
973 static Smtps smtpsv4 (AF_INET);
974 return smtpsv4;
975 }
976
981 static inline Smtps& v6 () noexcept
982 {
983 static Smtps smtpsv6 (AF_INET6);
984 return smtpsv6;
985 }
986
991 constexpr int family () const noexcept
992 {
993 return _family;
994 }
995
1000 constexpr int type () const noexcept
1001 {
1002 return SOCK_STREAM;
1003 }
1004
1009 constexpr int protocol () const noexcept
1010 {
1011 return IPPROTO_TCP;
1012 }
1013
1014 private:
1016 int _family;
1017 };
1018
1025 constexpr bool operator== (const Smtps& a, const Smtps& b) noexcept
1026 {
1027 return a.family () == b.family ();
1028 }
1029
1036 constexpr bool operator!= (const Smtps& a, const Smtps& b) noexcept
1037 {
1038 return !(a == b);
1039 }
1040}
1041
1042#endif
basic datagram socket class.
Definition socket.hpp:645
basic HTTP client.
Definition httpclient.hpp:46
basic HTTPS client.
Definition httpclient.hpp:486
basic HTTPS server.
Definition httpserver.hpp:971
basic HTTP server.
Definition httpserver.hpp:639
basic HTTP worker.
Definition httpserver.hpp:81
basic internet endpoint class.
Definition endpoint.hpp:670
basic link layer endpoint class.
Definition endpoint.hpp:498
basic netlink endpoint class.
Definition endpoint.hpp:266
basic SMTP client.
Definition smtpclient.hpp:41
Basic SMTPS client.
Definition smtpclient.hpp:648
socket stream class.
Definition socketstream.hpp:329
basic socket class.
Definition socket.hpp:60
basic stream acceptor class.
Definition protocol.hpp:45
basic stream socket class.
Definition socket.hpp:1139
basic TLS acceptor class.
Definition protocol.hpp:46
basic TLS socket class.
Definition socket.hpp:1547
TLS stream class.
Definition socketstream.hpp:513
basic unix endpoint class.
Definition endpoint.hpp:98
HTTP protocol class.
Definition protocol.hpp:663
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:724
constexpr Http(int family=AF_INET) noexcept
create the HTTP protocol instance.
Definition protocol.hpp:677
static Http & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:686
static Http & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:696
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:715
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:706
HTTPS protocol class.
Definition protocol.hpp:760
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:821
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:803
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:812
static Https & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:793
static Https & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:783
constexpr Https(int family=AF_INET) noexcept
create the HTTPS protocol instance.
Definition protocol.hpp:774
ICMP protocol class.
Definition protocol.hpp:378
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:416
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:434
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:425
static Icmp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:406
static Icmp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:396
constexpr Icmp(int family=AF_INET) noexcept
create the icmp protocol instance.
Definition protocol.hpp:387
RAW protocol class.
Definition protocol.hpp:240
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:263
constexpr Raw() noexcept=default
default constructor.
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:254
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:272
SMTP protocol class.
Definition protocol.hpp:857
static Smtp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:877
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:915
static Smtp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:887
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:897
constexpr Smtp(int family=AF_INET) noexcept
create the SMTP protocol instance.
Definition protocol.hpp:868
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:906
SMTPS protocol class.
Definition protocol.hpp:951
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:1009
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:1000
constexpr Smtps(int family=AF_INET) noexcept
create the SMTPS protocol instance.
Definition protocol.hpp:962
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:991
static Smtps & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:981
static Smtps & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:971
TCP protocol class.
Definition protocol.hpp:475
static Tcp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:505
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:524
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:515
constexpr Tcp(int family=AF_INET) noexcept
create the tcp protocol instance.
Definition protocol.hpp:486
static Tcp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:495
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:533
SSL/TLS protocol class.
Definition protocol.hpp:569
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:627
static Tls & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:599
static Tls & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:589
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:609
constexpr Tls(int family=AF_INET) noexcept
create the tcp protocol instance.
Definition protocol.hpp:580
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:618
UDP protocol class.
Definition protocol.hpp:286
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:333
static Udp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:314
static Udp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:304
constexpr Udp(int family=AF_INET) noexcept
construct the udp protocol instance.
Definition protocol.hpp:295
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:324
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:342
unix datagram protocol class.
Definition protocol.hpp:62
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:85
constexpr UnixDgram() noexcept=default
construct the unix datagram protocol instance by default.
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:94
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:76
unix stream protocol class.
Definition protocol.hpp:104
constexpr UnixStream() noexcept=default
construct the unix stream protocol instance by default.
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:129
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:120
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:138
Definition acceptor.hpp:32
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 endpoints are equal.
Definition endpoint.hpp:183