join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
protocol.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_PROTOCOL_HPP
26#define JOIN_CORE_PROTOCOL_HPP
27
28// libjoin.
29#include <join/endpoint.hpp>
30
31// C.
32#include <net/ethernet.h>
33#ifdef JOIN_HAS_XDP
34#include <linux/if_xdp.h>
35#endif
36#include <cstddef>
37
38namespace join
39{
40 template <class Protocol>
41 class BasicSocket;
42
43 template <class Protocol>
44 class BasicRawSocket;
45
46 template <class Protocol>
48
49 template <class Protocol>
51
52 template <class Protocol>
54
55 template <class Protocol>
57
58#ifdef JOIN_HAS_IO_URING
59 struct IoDefaultPolicy;
60
61 template <typename Policy>
62 class BasicProactor;
63
64 template <class Protocol, class Proactor = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
65 class BasicAsyncSocket;
66
67 template <class Protocol, class Proactor = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
69
70 template <class Protocol, class Proactor = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
72
73 template <class Protocol, class Proactor = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
75
76 template <class Protocol, class Proactor = BasicProactor<IoDefaultPolicy>>
78#else
79 class BasicProactor;
80
81 template <class Protocol, class Proactor = BasicProactor, size_t OpCount = 16>
83
84 template <class Protocol, class Proactor = BasicProactor, size_t OpCount = 16>
86
87 template <class Protocol, class Proactor = BasicProactor, size_t OpCount = 16>
89
90 template <class Protocol, class Proactor = BasicProactor, size_t OpCount = 16>
92
93 template <class Protocol, class Proactor = BasicProactor>
95#endif
96
100 class Raw
101 {
102 public:
106
110 constexpr Raw () noexcept = default;
111
116 constexpr int family () const noexcept
117 {
118 return AF_PACKET;
119 }
120
125 constexpr int type () const noexcept
126 {
127 return SOCK_RAW;
128 }
129
134 constexpr int protocol () const noexcept
135 {
136 if (BYTE_ORDER == LITTLE_ENDIAN)
137 {
138 return (ETH_P_ALL >> 8) | (ETH_P_ALL << 8);
139 }
140 return ETH_P_ALL;
141 }
142 };
143
144#ifdef JOIN_HAS_XDP
148 class Xdp
149 {
150 public:
151 using Endpoint = BasicXdpEndpoint<Xdp>;
152
156 constexpr Xdp () noexcept = default;
157
162 constexpr int family () const noexcept
163 {
164 return AF_XDP;
165 }
166
171 constexpr int type () const noexcept
172 {
173 return SOCK_RAW;
174 }
175
180 constexpr int protocol () const noexcept
181 {
182 return 0;
183 }
184 };
185#endif
186
191 {
192 public:
196
200 constexpr UnixDgram () noexcept = default;
201
206 constexpr int family () const noexcept
207 {
208 return AF_UNIX;
209 }
210
215 constexpr int type () const noexcept
216 {
217 return SOCK_DGRAM;
218 }
219
224 constexpr int protocol () const noexcept
225 {
226 return 0;
227 }
228 };
229
234 {
235 public:
242
246 constexpr UnixStream () noexcept = default;
247
252 constexpr int family () const noexcept
253 {
254 return AF_UNIX;
255 }
256
261 constexpr int type () const noexcept
262 {
263 return SOCK_STREAM;
264 }
265
270 constexpr int protocol () const noexcept
271 {
272 return 0;
273 }
274 };
275
279 class Udp
280 {
281 public:
285
290 constexpr explicit Udp (int family = AF_INET) noexcept
291 : _family (family)
292 {
293 }
294
299 static inline Udp& v4 () noexcept
300 {
301 static Udp udpv4 (AF_INET);
302 return udpv4;
303 }
304
309 static inline Udp& v6 () noexcept
310 {
311 static Udp udpv6 (AF_INET6);
312 return udpv6;
313 }
314
319 constexpr int family () const noexcept
320 {
321 return _family;
322 }
323
328 constexpr int type () const noexcept
329 {
330 return SOCK_DGRAM;
331 }
332
337 constexpr int protocol () const noexcept
338 {
339 return IPPROTO_UDP;
340 }
341
342 private:
344 int _family;
345 };
346
353 constexpr bool operator== (const Udp& a, const Udp& b) noexcept
354 {
355 return a.family () == b.family ();
356 }
357
364 constexpr bool operator!= (const Udp& a, const Udp& b) noexcept
365 {
366 return !(a == b);
367 }
368
372 class Icmp
373 {
374 public:
378
383 constexpr explicit Icmp (int family = AF_INET) noexcept
384 : _family (family)
385 {
386 }
387
392 static inline Icmp& v4 () noexcept
393 {
394 static Icmp icmpv4 (AF_INET);
395 return icmpv4;
396 }
397
402 static inline Icmp& v6 () noexcept
403 {
404 static Icmp icmpv6 (AF_INET6);
405 return icmpv6;
406 }
407
412 constexpr int family () const noexcept
413 {
414 return _family;
415 }
416
421 constexpr int type () const noexcept
422 {
423 return SOCK_RAW;
424 }
425
430 constexpr int protocol () const noexcept
431 {
432 if (family () == AF_INET6)
433 {
434 return int (IPPROTO_ICMPV6);
435 }
436
437 return IPPROTO_ICMP;
438 }
439
440 private:
442 int _family;
443 };
444
451 constexpr bool operator== (const Icmp& a, const Icmp& b) noexcept
452 {
453 return a.family () == b.family ();
454 }
455
462 constexpr bool operator!= (const Icmp& a, const Icmp& b) noexcept
463 {
464 return !(a == b);
465 }
466
470 class Tcp
471 {
472 public:
479
484 constexpr explicit Tcp (int family = AF_INET) noexcept
485 : _family (family)
486 {
487 }
488
493 static inline Tcp& v4 () noexcept
494 {
495 static Tcp tcpv4 (AF_INET);
496 return tcpv4;
497 }
498
503 static inline Tcp& v6 () noexcept
504 {
505 static Tcp tcpv6 (AF_INET6);
506 return tcpv6;
507 }
508
513 constexpr int family () const noexcept
514 {
515 return _family;
516 }
517
522 constexpr int type () const noexcept
523 {
524 return SOCK_STREAM;
525 }
526
531 constexpr int protocol () const noexcept
532 {
533 return IPPROTO_TCP;
534 }
535
536 private:
538 int _family;
539 };
540
547 constexpr bool operator== (const Tcp& a, const Tcp& b) noexcept
548 {
549 return a.family () == b.family ();
550 }
551
558 constexpr bool operator!= (const Tcp& a, const Tcp& b) noexcept
559 {
560 return !(a == b);
561 }
562}
563
564#endif
asynchronous datagram socket class.
Definition protocol.hpp:88
asynchronous raw socket class.
Definition protocol.hpp:85
basic asynchronous socket class.
Definition protocol.hpp:82
asynchronous stream acceptor class.
Definition protocol.hpp:94
asynchronous stream socket class.
Definition protocol.hpp:91
basic datagram socket class.
Definition protocol.hpp:47
basic internet endpoint class.
Definition endpoint.hpp:632
basic link layer endpoint class.
Definition endpoint.hpp:97
basic proactor class.
Definition proactor.hpp:156
socket stream class.
Definition socket_stream.hpp:350
basic stream acceptor class.
Definition protocol.hpp:53
basic stream socket class.
Definition stream_socket.hpp:44
basic unix endpoint class.
Definition endpoint.hpp:464
ICMP protocol class.
Definition protocol.hpp:373
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:412
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:430
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:421
static Icmp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:402
static Icmp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:392
constexpr Icmp(int family=AF_INET) noexcept
create the icmp protocol instance.
Definition protocol.hpp:383
RAW protocol class.
Definition protocol.hpp:101
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:125
constexpr Raw() noexcept=default
default constructor.
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:116
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:134
TCP protocol class.
Definition protocol.hpp:471
static Tcp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:503
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:522
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:513
constexpr Tcp(int family=AF_INET) noexcept
create the tcp protocol instance.
Definition protocol.hpp:484
static Tcp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:493
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:531
UDP protocol class.
Definition protocol.hpp:280
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:328
static Udp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:309
static Udp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:299
constexpr Udp(int family=AF_INET) noexcept
construct the udp protocol instance.
Definition protocol.hpp:290
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:319
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:337
unix datagram protocol class.
Definition protocol.hpp:191
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:215
constexpr UnixDgram() noexcept=default
construct the unix datagram protocol instance by default.
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:224
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:206
unix stream protocol class.
Definition protocol.hpp:234
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:261
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:252
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:270
Definition acceptor.hpp:32
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
Definition io_policy.hpp:35