Toolbox snapshot
The Reactive C++ Toolbox
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
Socket.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_NET_SOCKET_HPP
18#define TOOLBOX_NET_SOCKET_HPP
19
20#include <toolbox/net/Error.hpp>
21
22#include <toolbox/io/File.hpp>
23
24#include <net/if.h>
25#include <netdb.h>
26#include <netinet/tcp.h>
27#include <linux/net_tstamp.h>
28
29namespace toolbox {
30inline namespace net {
31using AddrInfoPtr = std::unique_ptr<addrinfo, void (*)(addrinfo*)>;
32} // namespace net
33namespace os {
34
36inline AddrInfoPtr getaddrinfo(const char* node, const char* service, const addrinfo& hints,
37 std::error_code& ec) noexcept
38{
39 addrinfo* ai{nullptr};
40 const auto err = ::getaddrinfo(node, service, &hints, &ai);
41 if (err != 0) {
43 }
44 return {ai, freeaddrinfo};
45}
46
48inline AddrInfoPtr getaddrinfo(const char* node, const char* service, const addrinfo& hints)
49{
50 addrinfo* ai{nullptr};
51 const auto err = ::getaddrinfo(node, service, &hints, &ai);
52 if (err != 0) {
53 throw std::system_error{make_gai_error_code(err),
54 "getaddrinfo (" + std::string{node ? node : ""} + ")"};
55 }
56 return {ai, freeaddrinfo};
57}
58
60inline AddrInfoPtr getaddrinfo(const char* node, const char* service, int family, int type,
61 int protocol, std::error_code& ec) noexcept
62{
64 // If node is not specified, then return a wildcard address suitable for bind()ing.
65 hints.ai_flags = node ? 0 : AI_PASSIVE;
66 hints.ai_family = family;
67 hints.ai_socktype = type;
68 hints.ai_protocol = protocol;
69 return getaddrinfo(node, service, hints, ec);
70}
71
73inline AddrInfoPtr getaddrinfo(const char* node, const char* service, int family, int type,
74 int protocol)
75{
77 // If node is not specified, then return a wildcard address suitable for bind()ing.
78 hints.ai_flags = node ? 0 : AI_PASSIVE;
79 hints.ai_family = family;
80 hints.ai_socktype = type;
81 hints.ai_protocol = protocol;
82 return getaddrinfo(node, service, hints);
83}
84
86template <typename ProtocolT>
87inline AddrInfoPtr getaddrinfo(const char* node, const char* service, ProtocolT protocol,
88 std::error_code& ec) noexcept
89{
90 return getaddrinfo(node, service, protocol.family(), protocol.type(), protocol.protocol(), ec);
91}
92
94template <typename ProtocolT>
95inline AddrInfoPtr getaddrinfo(const char* node, const char* service, ProtocolT protocol)
96{
97 return getaddrinfo(node, service, protocol.family(), protocol.type(), protocol.protocol());
98}
99
101inline unsigned if_nametoindex(const char* ifname, std::error_code& ec) noexcept
102{
103 unsigned ifindex{0};
104 if (ifname) {
105 if (!(ifindex = ::if_nametoindex(ifname))) {
107 }
108 }
109 return ifindex;
110}
111
113inline unsigned if_nametoindex(const char* ifname)
114{
115 unsigned ifindex{0};
116 if (ifname) {
117 if (!(ifindex = ::if_nametoindex(ifname))) {
118 throw std::system_error{make_error(errno), "if_nametoindex"};
119 }
120 }
121 return ifindex;
122}
123
125inline FileHandle socket(int family, int type, int protocol, std::error_code& ec) noexcept
126{
127 const auto sockfd = ::socket(family, type, protocol);
128 if (sockfd < 0) {
130 }
131 return sockfd;
132}
133
135inline FileHandle socket(int family, int type, int protocol)
136{
137 const auto sockfd = ::socket(family, type, protocol);
138 if (sockfd < 0) {
139 throw std::system_error{make_error(errno), "socket"};
140 }
141 return sockfd;
142}
143
145template <typename ProtocolT>
146inline FileHandle socket(ProtocolT protocol, std::error_code& ec) noexcept
147{
148 return socket(protocol.family(), protocol.type(), protocol.protocol(), ec);
149}
150
152template <typename ProtocolT>
154{
155 return socket(protocol.family(), protocol.type(), protocol.protocol());
156}
157
159inline std::pair<FileHandle, FileHandle> socketpair(int family, int type, int protocol,
160 std::error_code& ec) noexcept
161{
162 int sv[2];
163 if (::socketpair(family, type, protocol, sv) < 0) {
165 }
166 return {FileHandle{sv[0]}, FileHandle{sv[1]}};
167}
168
170inline std::pair<FileHandle, FileHandle> socketpair(int family, int type, int protocol)
171{
172 int sv[2];
173 if (::socketpair(family, type, protocol, sv) < 0) {
174 throw std::system_error{make_error(errno), "socketpair"};
175 }
176 return {FileHandle{sv[0]}, FileHandle{sv[1]}};
177}
178
180template <typename ProtocolT>
181inline std::pair<FileHandle, FileHandle> socketpair(ProtocolT protocol,
182 std::error_code& ec) noexcept
183{
184 return socketpair(protocol.family(), protocol.type(), protocol.protocol(), ec);
185}
186
188template <typename ProtocolT>
189inline std::pair<FileHandle, FileHandle> socketpair(ProtocolT protocol)
190{
191 return socketpair(protocol.family(), protocol.type(), protocol.protocol());
192}
193
196 std::error_code& ec) noexcept
197{
198 // The addrlen argument is updated to contain the actual size of the source address.
199 // The returned address is truncated if the buffer provided is too small.
200 const auto fd = ::accept(sockfd, &addr, &addrlen);
201 if (fd < 0) {
203 }
204 return fd;
205}
206
209{
210 // The addrlen argument is updated to contain the actual size of the source address.
211 // The returned address is truncated if the buffer provided is too small.
212 const auto fd = ::accept(sockfd, &addr, &addrlen);
213 if (fd < 0) {
214 throw std::system_error{make_error(errno), "accept"};
215 }
216 return fd;
217}
218
220inline FileHandle accept(int sockfd, std::error_code& ec) noexcept
221{
222 const auto fd = ::accept(sockfd, nullptr, nullptr);
223 if (fd < 0) {
225 }
226 return fd;
227}
228
231{
232 const auto fd = ::accept(sockfd, nullptr, nullptr);
233 if (fd < 0) {
234 throw std::system_error{make_error(errno), "accept"};
235 }
236 return fd;
237}
238
240template <typename EndpointT>
241inline FileHandle accept(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
242{
243 socklen_t addrlen = ep.capacity();
244 FileHandle fh{accept(sockfd, *ep.data(), addrlen, ec)};
245 if (!ec) {
246 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
247 }
248 return fh;
249}
250
252template <typename EndpointT>
254{
255 socklen_t addrlen = ep.capacity();
256 FileHandle fh{accept(sockfd, *ep.data(), addrlen)};
257 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
258 return fh;
259}
260
262inline void bind(int sockfd, const sockaddr& addr, socklen_t addrlen, std::error_code& ec) noexcept
263{
264 if (::bind(sockfd, &addr, addrlen) < 0) {
266 }
267}
268
270inline void bind(int sockfd, const sockaddr& addr, socklen_t addrlen)
271{
272 if (::bind(sockfd, &addr, addrlen) < 0) {
273 throw std::system_error{make_error(errno), "bind"};
274 }
275}
276
278template <typename EndpointT>
279inline void bind(int sockfd, const EndpointT& ep, std::error_code& ec) noexcept
280{
281 bind(sockfd, *ep.data(), ep.size(), ec);
282}
283
285template <typename EndpointT>
286inline void bind(int sockfd, const EndpointT& ep)
287{
288 bind(sockfd, *ep.data(), ep.size());
289}
290
292inline void connect(int sockfd, const sockaddr& addr, socklen_t addrlen,
293 std::error_code& ec) noexcept
294{
295 if (::connect(sockfd, &addr, addrlen) < 0) {
297 }
298}
299
301inline void connect(int sockfd, const sockaddr& addr, socklen_t addrlen)
302{
303 if (::connect(sockfd, &addr, addrlen) < 0) {
304 throw std::system_error{make_error(errno), "connect"};
305 }
306}
307
309template <typename EndpointT>
310inline void connect(int sockfd, const EndpointT& ep, std::error_code& ec) noexcept
311{
312 connect(sockfd, *ep.data(), ep.size(), ec);
313}
314
316template <typename EndpointT>
317inline void connect(int sockfd, const EndpointT& ep)
318{
319 connect(sockfd, *ep.data(), ep.size());
320}
321
323inline void listen(int sockfd, int backlog, std::error_code& ec) noexcept
324{
325 if (::listen(sockfd, backlog) < 0) {
327 }
328}
329
331inline void listen(int sockfd, int backlog)
332{
333 if (::listen(sockfd, backlog) < 0) {
334 throw std::system_error{make_error(errno), "listen"};
335 }
336}
337
339inline void shutdown(int sockfd, int how, std::error_code& ec) noexcept
340{
341 if (::shutdown(sockfd, how) < 0) {
343 }
344}
345
347inline void shutdown(int sockfd, int how)
348{
349 if (::shutdown(sockfd, how) < 0) {
350 throw std::system_error{make_error(errno), "shutdown"};
351 }
352}
353
355inline ssize_t recv(int sockfd, void* buf, std::size_t len, int flags, std::error_code& ec) noexcept
356{
357 const auto ret = ::recv(sockfd, buf, len, flags);
358 if (ret < 0) {
360 }
361 return ret;
362}
363
365inline std::size_t recv(int sockfd, void* buf, std::size_t len, int flags)
366{
367 const auto ret = ::recv(sockfd, buf, len, flags);
368 if (ret < 0) {
369 throw std::system_error{make_error(errno), "recv"};
370 }
371 return ret;
372}
373
375inline ssize_t recv(int sockfd, MutableBuffer buf, int flags, std::error_code& ec) noexcept
376{
377 return recv(sockfd, static_cast<void*>(buf.data()), buffer_size(buf), flags, ec);
378}
379
381inline std::size_t recv(int sockfd, MutableBuffer buf, int flags)
382{
383 return recv(sockfd, static_cast<void*>(buf.data()), buffer_size(buf), flags);
384}
385
386inline ssize_t recvmsg(int sockfd, msghdr& msg, int flags, std::error_code& ec) noexcept
387{
388 const auto ret = ::recvmsg(sockfd, &msg, flags);
389 if (ret < 0) {
391 }
392 return ret;
393}
394
395inline std::size_t recvmsg(int sockfd, msghdr& msg, int flags)
396{
397 const auto ret = ::recvmsg(sockfd, &msg, flags);
398 if (ret < 0) {
399 throw std::system_error{make_error(errno), "recvmsg"};
400 }
401 return ret;
402}
403
405inline ssize_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, sockaddr& addr,
406 socklen_t& addrlen, std::error_code& ec) noexcept
407{
408 // The addrlen argument is updated to contain the actual size of the source address.
409 // The returned address is truncated if the buffer provided is too small.
410 const auto ret = ::recvfrom(sockfd, buf, len, flags, &addr, &addrlen);
411 if (ret < 0) {
413 }
414 return ret;
415}
416
418inline std::size_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, sockaddr& addr,
420{
421 // The addrlen argument is updated to contain the actual size of the source address.
422 // The returned address is truncated if the buffer provided is too small.
423 const auto ret = ::recvfrom(sockfd, buf, len, flags, &addr, &addrlen);
424 if (ret < 0) {
425 throw std::system_error{make_error(errno), "recvfrom"};
426 }
427 return ret;
428}
429
431template <typename EndpointT>
432inline ssize_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, EndpointT& ep,
433 std::error_code& ec) noexcept
434{
435 socklen_t addrlen = ep.capacity();
436 const auto ret = recvfrom(sockfd, buf, len, flags, *ep.data(), addrlen, ec);
437 if (!ec) {
438 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
439 }
440 return ret;
441}
442
444template <typename EndpointT>
445inline std::size_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, EndpointT& ep)
446{
447 socklen_t addrlen = ep.capacity();
448 const auto ret = recvfrom(sockfd, buf, len, flags, *ep.data(), addrlen);
449 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
450 return ret;
451}
452
454template <typename EndpointT>
456 std::error_code& ec) noexcept
457{
458 return recvfrom(sockfd, static_cast<void*>(buf.data()), buffer_size(buf), flags, ep, ec);
459}
460
462template <typename EndpointT>
463inline std::size_t recvfrom(int sockfd, MutableBuffer buf, int flags, EndpointT& ep)
464{
465 return recvfrom(sockfd, static_cast<void*>(buf.data()), buffer_size(buf), flags, ep);
466}
467
469inline ssize_t send(int sockfd, const void* buf, std::size_t len, int flags,
470 std::error_code& ec) noexcept
471{
472 const auto ret = ::send(sockfd, buf, len, flags);
473 if (ret < 0) {
475 }
476 return ret;
477}
478
480inline std::size_t send(int sockfd, const void* buf, std::size_t len, int flags)
481{
482 const auto ret = ::send(sockfd, buf, len, flags);
483 if (ret < 0) {
484 throw std::system_error{make_error(errno), "send"};
485 }
486 return ret;
487}
488
490inline ssize_t send(int sockfd, ConstBuffer buf, int flags, std::error_code& ec) noexcept
491{
492 return send(sockfd, static_cast<const void*>(buf.data()), buffer_size(buf), flags, ec);
493}
494
496inline std::size_t send(int sockfd, ConstBuffer buf, int flags)
497{
498 return send(sockfd, static_cast<const void*>(buf.data()), buffer_size(buf), flags);
499}
500
502inline ssize_t sendto(int sockfd, const void* buf, std::size_t len, int flags, const sockaddr& addr,
503 socklen_t addrlen, std::error_code& ec) noexcept
504{
505 const auto ret = ::sendto(sockfd, buf, len, flags, &addr, addrlen);
506 if (ret < 0) {
508 }
509 return ret;
510}
511
513inline std::size_t sendto(int sockfd, const void* buf, std::size_t len, int flags,
515{
516 const auto ret = ::sendto(sockfd, buf, len, flags, &addr, addrlen);
517 if (ret < 0) {
518 throw std::system_error{make_error(errno), "sendto"};
519 }
520 return ret;
521}
522
524template <typename EndpointT>
525inline ssize_t sendto(int sockfd, const void* buf, std::size_t len, int flags, const EndpointT& ep,
526 std::error_code& /*ec*/) noexcept
527{
528 return sendto(sockfd, buf, len, flags, *ep.data(), ep.size());
529}
530
532template <typename EndpointT>
533inline std::size_t sendto(int sockfd, const void* buf, std::size_t len, int flags,
534 const EndpointT& ep)
535{
536 return sendto(sockfd, buf, len, flags, *ep.data(), ep.size());
537}
538
540template <typename EndpointT>
542 std::error_code& ec) noexcept
543{
544 return sendto(sockfd, static_cast<const void*>(buf.data()), buffer_size(buf), flags, *ep.data(),
545 ep.size(), ec);
546}
547
549template <typename EndpointT>
550inline std::size_t sendto(int sockfd, ConstBuffer buf, int flags, const EndpointT& ep)
551{
552 return sendto(sockfd, static_cast<const void*>(buf.data()), buffer_size(buf), flags, *ep.data(),
553 ep.size());
554}
555
558 std::error_code& ec) noexcept
559{
560 // The addrlen argument is updated to contain the actual size of the source address.
561 // The returned address is truncated if the buffer provided is too small.
562 if (::getsockname(sockfd, &addr, &addrlen) < 0) {
564 }
565}
566
569{
570 // The addrlen argument is updated to contain the actual size of the source address.
571 // The returned address is truncated if the buffer provided is too small.
572 if (::getsockname(sockfd, &addr, &addrlen) < 0) {
573 throw std::system_error{make_error(errno), "getsockname"};
574 }
575}
576
578template <typename EndpointT>
579inline void getsockname(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
580{
581 socklen_t addrlen = ep.capacity();
582 getsockname(sockfd, *ep.data(), addrlen, ec);
583 if (!ec) {
584 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
585 }
586}
587
589template <typename EndpointT>
590inline void getsockname(int sockfd, EndpointT& ep)
591{
592 socklen_t addrlen = ep.capacity();
593 getsockname(sockfd, *ep.data(), addrlen);
594 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
595}
596
598inline void getsockopt(int sockfd, int level, int optname, void* optval, socklen_t& optlen,
599 std::error_code& ec) noexcept
600{
601 if (::getsockopt(sockfd, level, optname, optval, &optlen) < 0) {
603 }
604}
605
607inline void getsockopt(int sockfd, int level, int optname, void* optval, socklen_t& optlen)
608{
609 if (::getsockopt(sockfd, level, optname, optval, &optlen) < 0) {
610 throw std::system_error{make_error(errno), "getsockopt"};
611 }
612}
613
615inline void setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen,
616 std::error_code& ec) noexcept
617{
618 if (::setsockopt(sockfd, level, optname, optval, optlen) < 0) {
620 }
621}
622
624inline void setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen)
625{
626 if (::setsockopt(sockfd, level, optname, optval, optlen) < 0) {
627 throw std::system_error{make_error(errno), "setsockopt"};
628 }
629}
630
631} // namespace os
632inline namespace net {
633
635TOOLBOX_API AddrInfoPtr get_unix_addrinfo(std::string_view path, int type);
636
637template <typename EndpointT>
638inline void get_sock_name(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
639{
641}
642
643template <typename EndpointT>
645{
647}
648
649inline std::error_code get_so_error(int sockfd, std::error_code& ec) noexcept
650{
651 int optval{};
652 socklen_t optlen{sizeof(optval)};
654 return make_error(optval);
655}
656
657inline std::error_code get_so_error(int sockfd)
658{
659 int optval{};
660 socklen_t optlen{sizeof(optval)};
662 return make_error(optval);
663}
664
665inline int get_so_rcv_buf(int sockfd, std::error_code& ec) noexcept
666{
667 int optval{};
668 socklen_t optlen{sizeof(optval)};
670 return optval;
671}
672
673inline int get_so_rcv_buf(int sockfd)
674{
675 int optval{};
676 socklen_t optlen{sizeof(optval)};
678 return optval;
679}
680
681inline int get_so_snd_buf(int sockfd, std::error_code& ec) noexcept
682{
683 int optval{};
684 socklen_t optlen{sizeof(optval)};
686 return optval;
687}
688
689inline int get_so_snd_buf(int sockfd)
690{
691 int optval{};
692 socklen_t optlen{sizeof(optval)};
694 return optval;
695}
696
697inline bool is_tcp_no_delay(int sockfd, std::error_code& ec) noexcept
698{
699 int optval{};
700 socklen_t optlen{sizeof(optval)};
702 return optval != 0;
703}
704
705inline bool is_tcp_no_delay(int sockfd)
706{
707 int optval{};
708 socklen_t optlen{sizeof(optval)};
710 return optval != 0;
711}
712
713inline void set_so_rcv_buf(int sockfd, int size, std::error_code& ec) noexcept
714{
716}
717
718inline void set_so_rcv_buf(int sockfd, int size)
719{
721}
722
723inline void set_so_reuse_addr(int sockfd, bool enabled, std::error_code& ec) noexcept
724{
725 int optval{enabled ? 1 : 0};
727}
728
729inline void set_so_reuse_addr(int sockfd, bool enabled)
730{
731 int optval{enabled ? 1 : 0};
733}
734
735inline void set_so_snd_buf(int sockfd, int size, std::error_code& ec) noexcept
736{
738}
739
740inline void set_so_snd_buf(int sockfd, int size)
741{
743}
744
755inline void set_tcp_no_delay(int sockfd, bool enabled, std::error_code& ec) noexcept
756{
757 int optval{enabled ? 1 : 0};
759}
760
770inline void set_tcp_no_delay(int sockfd, bool enabled)
771{
772 int optval{enabled ? 1 : 0};
774}
775
783inline void set_tcp_syn_nt(int sockfd, int retrans, std::error_code& ec) noexcept
784{
786}
787
794inline void set_tcp_syn_nt(int sockfd, int retrans)
795{
797}
798
799inline void set_so_timestamping(int sockfd, int flags, std::error_code& ec) noexcept
800{
802}
803
804inline void set_so_timestamping(int sockfd, int flags)
805{
807}
808
809inline int get_so_timestamping(int sockfd, std::error_code& ec) noexcept
810{
811 int optval{};
812 socklen_t optlen{sizeof(optval)};
814 return optval;
815}
816
818{
819 int optval{};
820 socklen_t optlen{sizeof(optval)};
822 return optval;
823}
824
825struct Sock : FileHandle {
828 , family_{family}
829 {
830 }
832
833 int family() const noexcept { return family_; }
834 bool is_ip_family() const noexcept { return family_ == AF_INET || family_ == AF_INET6; }
835
836 // Logically const.
837 std::error_code get_error(std::error_code& ec) const noexcept
838 {
839 return toolbox::get_so_error(get(), ec);
840 }
841 std::error_code get_error() const { return toolbox::get_so_error(get()); }
842
843 int get_rcv_buf(std::error_code& ec) const noexcept
844 {
845 return toolbox::get_so_rcv_buf(get(), ec);
846 }
847 int get_rcv_buf() const { return toolbox::get_so_rcv_buf(get()); }
848
849 int get_snd_buf(std::error_code& ec) const noexcept
850 {
851 return toolbox::get_so_snd_buf(get(), ec);
852 }
853 int get_snd_buf() const { return toolbox::get_so_snd_buf(get()); }
854
855 bool is_tcp_no_delay(std::error_code& ec) const noexcept
856 {
858 }
859 bool is_tcp_no_delay() const { return toolbox::is_tcp_no_delay(get()); }
860 void close() { reset(); }
861
862 void set_non_block(std::error_code& ec) noexcept { toolbox::set_non_block(get(), ec); }
864
865 void set_rcv_buf(int size, std::error_code& ec) noexcept
866 {
868 }
870
871 void set_reuse_addr(bool enabled, std::error_code& ec) noexcept
872 {
874 }
876
877 void set_snd_buf(int size, std::error_code& ec) noexcept
878 {
880 }
882
889
890 void enable_hardware_rcv_timestamps(std::error_code& ec) noexcept
891 {
892 if (int flags = get_so_timestamping(get(), ec); !ec) {
895 }
896 }
897
907
908 void disable_hardware_rcv_timestamps(std::error_code& ec) noexcept
909 {
910 if (int flags = get_so_timestamping(get(), ec); !ec) {
914 }
916 }
917 }
918
919 private:
920 int family_{};
921};
922
923} // namespace net
924} // namespace toolbox
925
926#endif // TOOLBOX_NET_SOCKET_HPP
#define TOOLBOX_API
Definition Config.h:39
STL namespace.
boost::asio::const_buffer ConstBuffer
Definition Buffer.hpp:28
boost::asio::mutable_buffer MutableBuffer
Definition Buffer.hpp:29
void set_non_block(int fd, std::error_code &ec) noexcept
Definition File.hpp:270
int get_so_snd_buf(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:681
void set_so_reuse_addr(int sockfd, bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:723
std::unique_ptr< addrinfo, void(*)(addrinfo *)> AddrInfoPtr
Definition Socket.hpp:31
void set_so_snd_buf(int sockfd, int size, std::error_code &ec) noexcept
Definition Socket.hpp:735
void set_so_rcv_buf(int sockfd, int size, std::error_code &ec) noexcept
Definition Socket.hpp:713
std::error_code get_so_error(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:649
void set_tcp_no_delay(int sockfd, bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:755
int get_so_timestamping(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:809
void set_tcp_syn_nt(int sockfd, int retrans, std::error_code &ec) noexcept
Definition Socket.hpp:783
void get_sock_name(int sockfd, EndpointT &ep, std::error_code &ec) noexcept
Definition Socket.hpp:638
std::error_code make_gai_error_code(int err) noexcept
Definition Error.cpp:54
AddrInfoPtr get_unix_addrinfo(string_view path, int type)
Create an addrinfo structure for a Unix domain address.
Definition Socket.cpp:35
bool is_tcp_no_delay(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:697
void set_so_timestamping(int sockfd, int flags, std::error_code &ec) noexcept
Definition Socket.hpp:799
int get_so_rcv_buf(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:665
FileHandle accept(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Accept a connection on a socket.
Definition Socket.hpp:195
void getsockopt(int sockfd, int level, int optname, void *optval, socklen_t &optlen, std::error_code &ec) noexcept
Get socket option.
Definition Socket.hpp:598
ssize_t recvmsg(int sockfd, msghdr &msg, int flags, std::error_code &ec) noexcept
Definition Socket.hpp:386
void setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen, std::error_code &ec) noexcept
Set socket option.
Definition Socket.hpp:615
std::pair< FileHandle, FileHandle > socketpair(int family, int type, int protocol, std::error_code &ec) noexcept
Create a pair of connected sockets.
Definition Socket.hpp:159
AddrInfoPtr getaddrinfo(const char *node, const char *service, const addrinfo &hints, std::error_code &ec) noexcept
Get network address from Internet host and service.
Definition Socket.hpp:36
void listen(int sockfd, int backlog, std::error_code &ec) noexcept
Listen for connections on a socket.
Definition Socket.hpp:323
ssize_t recvfrom(int sockfd, void *buf, std::size_t len, int flags, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Receive a message from a socket.
Definition Socket.hpp:405
void getsockname(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Get the socket name.
Definition Socket.hpp:557
unsigned if_nametoindex(const char *ifname, std::error_code &ec) noexcept
Returns the index of the network interface corresponding to the name ifname.
Definition Socket.hpp:101
FileHandle socket(int family, int type, int protocol, std::error_code &ec) noexcept
Create an endpoint for communication.
Definition Socket.hpp:125
ssize_t send(int sockfd, const void *buf, std::size_t len, int flags, std::error_code &ec) noexcept
Send a message on a socket.
Definition Socket.hpp:469
void shutdown(int sockfd, int how, std::error_code &ec) noexcept
Shut-down part of a full-duplex connection.
Definition Socket.hpp:339
void connect(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Initiate a connection on a socket.
Definition Socket.hpp:292
ssize_t sendto(int sockfd, const void *buf, std::size_t len, int flags, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Send a message on a socket.
Definition Socket.hpp:502
ssize_t recv(int sockfd, void *buf, std::size_t len, int flags, std::error_code &ec) noexcept
Receive a message from a socket.
Definition Socket.hpp:355
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
std::string_view sv
Definition Tokeniser.hpp:26
constexpr const auto & get(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s, TagT tag={})
Definition Struct.hpp:110
constexpr auto bind() noexcept
Definition Slot.hpp:97
bool is_tcp_no_delay() const
Definition Socket.hpp:859
void enable_hardware_rcv_timestamps()
Definition Socket.hpp:883
void set_reuse_addr(bool enabled)
Definition Socket.hpp:875
void disable_hardware_rcv_timestamps()
Definition Socket.hpp:898
int family() const noexcept
Definition Socket.hpp:833
int get_rcv_buf(std::error_code &ec) const noexcept
Definition Socket.hpp:843
int get_snd_buf() const
Definition Socket.hpp:853
std::error_code get_error(std::error_code &ec) const noexcept
Definition Socket.hpp:837
void set_rcv_buf(int size)
Definition Socket.hpp:869
bool is_tcp_no_delay(std::error_code &ec) const noexcept
Definition Socket.hpp:855
int get_rcv_buf() const
Definition Socket.hpp:847
void disable_hardware_rcv_timestamps(std::error_code &ec) noexcept
Definition Socket.hpp:908
Sock() noexcept=default
void set_snd_buf(int size)
Definition Socket.hpp:881
Sock(FileHandle &&sock, int family)
Definition Socket.hpp:826
void set_reuse_addr(bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:871
void enable_hardware_rcv_timestamps(std::error_code &ec) noexcept
Definition Socket.hpp:890
int get_snd_buf(std::error_code &ec) const noexcept
Definition Socket.hpp:849
void set_non_block(std::error_code &ec) noexcept
Definition Socket.hpp:862
std::error_code get_error() const
Definition Socket.hpp:841
void set_snd_buf(int size, std::error_code &ec) noexcept
Definition Socket.hpp:877
void set_rcv_buf(int size, std::error_code &ec) noexcept
Definition Socket.hpp:865
bool is_ip_family() const noexcept
Definition Socket.hpp:834