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 <bit>
33
34// C.
35#include <net/ethernet.h>
36#ifdef JOIN_HAS_XDP
37#include <linux/if_xdp.h>
38#endif
39#include <cstddef>
40
41namespace join
42{
43 template <class Protocol>
44 class BasicSocket;
45
46 template <class Protocol>
47 class BasicRawSocket;
48
49 template <class Protocol>
51
52 template <class Protocol>
54
55 template <class Protocol>
57
58 template <class Protocol>
60
61#ifdef JOIN_HAS_IO_URING
62 struct IoDefaultPolicy;
63
64 template <typename IoPolicy>
65 class BasicProactor;
66
67 template <class Protocol, class ProactorType = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
68 class BasicAsyncSocket;
69
70 template <class Protocol, class ProactorType = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
72
73 template <class Protocol, class ProactorType = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
75
76 template <class Protocol, class ProactorType = BasicProactor<IoDefaultPolicy>, size_t OpCount = 16>
78
79 template <class Protocol, class ProactorType = BasicProactor<IoDefaultPolicy>>
81#else
82 class BasicProactor;
83
84 template <class Protocol, class ProactorType = BasicProactor, size_t OpCount = 16>
86
87 template <class Protocol, class ProactorType = BasicProactor, size_t OpCount = 16>
89
90 template <class Protocol, class ProactorType = BasicProactor, size_t OpCount = 16>
92
93 template <class Protocol, class ProactorType = BasicProactor, size_t OpCount = 16>
95
96 template <class Protocol, class ProactorType = BasicProactor>
98#endif
99
103 class Raw
104 {
105 public:
109
113 constexpr Raw () noexcept = default;
114
119 constexpr int family () const noexcept
120 {
121 return AF_PACKET;
122 }
123
128 constexpr int type () const noexcept
129 {
130 return SOCK_RAW;
131 }
132
137 constexpr int protocol () const noexcept
138 {
139 if constexpr (std::endian::native == std::endian::little)
140 {
141 return (ETH_P_ALL >> 8) | (ETH_P_ALL << 8);
142 }
143 return ETH_P_ALL;
144 }
145 };
146
147#ifdef JOIN_HAS_XDP
151 class Xdp
152 {
153 public:
154 using Endpoint = BasicXdpEndpoint<Xdp>;
155
159 constexpr Xdp () noexcept = default;
160
165 constexpr int family () const noexcept
166 {
167 return AF_XDP;
168 }
169
174 constexpr int type () const noexcept
175 {
176 return SOCK_RAW;
177 }
178
183 constexpr int protocol () const noexcept
184 {
185 return 0;
186 }
187 };
188#endif
189
194 {
195 public:
199
203 constexpr UnixDgram () noexcept = default;
204
209 constexpr int family () const noexcept
210 {
211 return AF_UNIX;
212 }
213
218 constexpr int type () const noexcept
219 {
220 return SOCK_DGRAM;
221 }
222
227 constexpr int protocol () const noexcept
228 {
229 return 0;
230 }
231 };
232
237 {
238 public:
245
249 constexpr UnixStream () noexcept = default;
250
255 constexpr int family () const noexcept
256 {
257 return AF_UNIX;
258 }
259
264 constexpr int type () const noexcept
265 {
266 return SOCK_STREAM;
267 }
268
273 constexpr int protocol () const noexcept
274 {
275 return 0;
276 }
277 };
278
282 class Udp
283 {
284 public:
288
293 constexpr explicit Udp (int family = AF_INET) noexcept
294 : _family (family)
295 {
296 }
297
302 static inline Udp& v4 () noexcept
303 {
304 static Udp udpv4 (AF_INET);
305 return udpv4;
306 }
307
312 static inline Udp& v6 () noexcept
313 {
314 static Udp udpv6 (AF_INET6);
315 return udpv6;
316 }
317
322 constexpr int family () const noexcept
323 {
324 return _family;
325 }
326
331 constexpr int type () const noexcept
332 {
333 return SOCK_DGRAM;
334 }
335
340 constexpr int protocol () const noexcept
341 {
342 return IPPROTO_UDP;
343 }
344
345 private:
347 int _family;
348 };
349
356 constexpr bool operator== (const Udp& a, const Udp& b) noexcept
357 {
358 return a.family () == b.family ();
359 }
360
364 class Icmp
365 {
366 public:
370
375 constexpr explicit Icmp (int family = AF_INET) noexcept
376 : _family (family)
377 {
378 }
379
384 static inline Icmp& v4 () noexcept
385 {
386 static Icmp icmpv4 (AF_INET);
387 return icmpv4;
388 }
389
394 static inline Icmp& v6 () noexcept
395 {
396 static Icmp icmpv6 (AF_INET6);
397 return icmpv6;
398 }
399
404 constexpr int family () const noexcept
405 {
406 return _family;
407 }
408
413 constexpr int type () const noexcept
414 {
415 return SOCK_RAW;
416 }
417
422 constexpr int protocol () const noexcept
423 {
424 if (family () == AF_INET6)
425 {
426 return int (IPPROTO_ICMPV6);
427 }
428
429 return IPPROTO_ICMP;
430 }
431
432 private:
434 int _family;
435 };
436
443 constexpr bool operator== (const Icmp& a, const Icmp& b) noexcept
444 {
445 return a.family () == b.family ();
446 }
447
451 class Tcp
452 {
453 public:
460
465 constexpr explicit Tcp (int family = AF_INET) noexcept
466 : _family (family)
467 {
468 }
469
474 static inline Tcp& v4 () noexcept
475 {
476 static Tcp tcpv4 (AF_INET);
477 return tcpv4;
478 }
479
484 static inline Tcp& v6 () noexcept
485 {
486 static Tcp tcpv6 (AF_INET6);
487 return tcpv6;
488 }
489
494 constexpr int family () const noexcept
495 {
496 return _family;
497 }
498
503 constexpr int type () const noexcept
504 {
505 return SOCK_STREAM;
506 }
507
512 constexpr int protocol () const noexcept
513 {
514 return IPPROTO_TCP;
515 }
516
517 private:
519 int _family;
520 };
521
528 constexpr bool operator== (const Tcp& a, const Tcp& b) noexcept
529 {
530 return a.family () == b.family ();
531 }
532}
533
534#endif
asynchronous datagram socket class.
Definition protocol.hpp:91
asynchronous raw socket class.
Definition protocol.hpp:88
basic asynchronous socket class.
Definition protocol.hpp:85
asynchronous stream acceptor class.
Definition protocol.hpp:97
asynchronous stream socket class.
Definition protocol.hpp:94
basic datagram socket class.
Definition protocol.hpp:50
basic internet endpoint class.
Definition endpoint.hpp:517
basic link layer endpoint class.
Definition endpoint.hpp:98
basic proactor class.
Definition proactor.hpp:158
socket stream class.
Definition socket_stream.hpp:350
basic stream acceptor class.
Definition protocol.hpp:56
basic stream socket class.
Definition stream_socket.hpp:44
basic unix endpoint class.
Definition endpoint.hpp:392
ICMP protocol class.
Definition protocol.hpp:365
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:404
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:422
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:413
static Icmp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:394
static Icmp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:384
constexpr Icmp(int family=AF_INET) noexcept
create the icmp protocol instance.
Definition protocol.hpp:375
RAW protocol class.
Definition protocol.hpp:104
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:128
constexpr Raw() noexcept=default
default constructor.
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:119
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:137
TCP protocol class.
Definition protocol.hpp:452
static Tcp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:484
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:503
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:494
constexpr Tcp(int family=AF_INET) noexcept
create the tcp protocol instance.
Definition protocol.hpp:465
static Tcp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:474
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:512
UDP protocol class.
Definition protocol.hpp:283
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:331
static Udp & v6() noexcept
get protocol suitable for IPv6 address family.
Definition protocol.hpp:312
static Udp & v4() noexcept
get protocol suitable for IPv4 address family.
Definition protocol.hpp:302
constexpr Udp(int family=AF_INET) noexcept
construct the udp protocol instance.
Definition protocol.hpp:293
constexpr int family() const noexcept
get the protocol IP address family.
Definition protocol.hpp:322
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:340
unix datagram protocol class.
Definition protocol.hpp:194
constexpr int type() const noexcept
get the protocol communication semantic.
Definition protocol.hpp:218
constexpr UnixDgram() noexcept=default
construct the unix datagram protocol instance by default.
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:227
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:209
unix stream protocol class.
Definition protocol.hpp:237
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:264
constexpr int family() const noexcept
get the protocol ip address family.
Definition protocol.hpp:255
constexpr int protocol() const noexcept
get the protocol type.
Definition protocol.hpp:273
Definition acceptor.hpp:32
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
Definition io_policy.hpp:35