Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
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
28namespace toolbox {
29inline namespace net {
30using AddrInfoPtr = std::unique_ptr<addrinfo, void (*)(addrinfo*)>;
31} // namespace net
32namespace os {
33
35inline AddrInfoPtr getaddrinfo(const char* node, const char* service, const addrinfo& hints,
36 std::error_code& ec) noexcept
37{
38 addrinfo* ai{nullptr};
39 const auto err = ::getaddrinfo(node, service, &hints, &ai);
40 if (err != 0) {
42 }
43 return {ai, freeaddrinfo};
44}
45
47inline AddrInfoPtr getaddrinfo(const char* node, const char* service, const addrinfo& hints)
48{
49 addrinfo* ai{nullptr};
50 const auto err = ::getaddrinfo(node, service, &hints, &ai);
51 if (err != 0) {
52 throw std::system_error{make_gai_error_code(err),
53 "getaddrinfo (" + std::string{node ? node : ""} + ")"};
54 }
55 return {ai, freeaddrinfo};
56}
57
59inline AddrInfoPtr getaddrinfo(const char* node, const char* service, int family, int type,
60 int protocol, std::error_code& ec) noexcept
61{
63 // If node is not specified, then return a wildcard address suitable for bind()ing.
64 hints.ai_flags = node ? 0 : AI_PASSIVE;
65 hints.ai_family = family;
66 hints.ai_socktype = type;
67 hints.ai_protocol = protocol;
68 return getaddrinfo(node, service, hints, ec);
69}
70
72inline AddrInfoPtr getaddrinfo(const char* node, const char* service, int family, int type,
73 int protocol)
74{
76 // If node is not specified, then return a wildcard address suitable for bind()ing.
77 hints.ai_flags = node ? 0 : AI_PASSIVE;
78 hints.ai_family = family;
79 hints.ai_socktype = type;
80 hints.ai_protocol = protocol;
81 return getaddrinfo(node, service, hints);
82}
83
85template <typename ProtocolT>
86inline AddrInfoPtr getaddrinfo(const char* node, const char* service, ProtocolT protocol,
87 std::error_code& ec) noexcept
88{
89 return getaddrinfo(node, service, protocol.family(), protocol.type(), protocol.protocol(), ec);
90}
91
93template <typename ProtocolT>
94inline AddrInfoPtr getaddrinfo(const char* node, const char* service, ProtocolT protocol)
95{
96 return getaddrinfo(node, service, protocol.family(), protocol.type(), protocol.protocol());
97}
98
100inline unsigned if_nametoindex(const char* ifname, std::error_code& ec) noexcept
101{
102 unsigned ifindex{0};
103 if (ifname) {
104 if (!(ifindex = ::if_nametoindex(ifname))) {
106 }
107 }
108 return ifindex;
109}
110
112inline unsigned if_nametoindex(const char* ifname)
113{
114 unsigned ifindex{0};
115 if (ifname) {
116 if (!(ifindex = ::if_nametoindex(ifname))) {
117 throw std::system_error{make_error(errno), "if_nametoindex"};
118 }
119 }
120 return ifindex;
121}
122
124inline FileHandle socket(int family, int type, int protocol, std::error_code& ec) noexcept
125{
126 const auto sockfd = ::socket(family, type, protocol);
127 if (sockfd < 0) {
129 }
130 return sockfd;
131}
132
134inline FileHandle socket(int family, int type, int protocol)
135{
136 const auto sockfd = ::socket(family, type, protocol);
137 if (sockfd < 0) {
138 throw std::system_error{make_error(errno), "socket"};
139 }
140 return sockfd;
141}
142
144template <typename ProtocolT>
145inline FileHandle socket(ProtocolT protocol, std::error_code& ec) noexcept
146{
147 return socket(protocol.family(), protocol.type(), protocol.protocol(), ec);
148}
149
151template <typename ProtocolT>
153{
154 return socket(protocol.family(), protocol.type(), protocol.protocol());
155}
156
158inline std::pair<FileHandle, FileHandle> socketpair(int family, int type, int protocol,
159 std::error_code& ec) noexcept
160{
161 int sv[2];
162 if (::socketpair(family, type, protocol, sv) < 0) {
164 }
165 return {FileHandle{sv[0]}, FileHandle{sv[1]}};
166}
167
169inline std::pair<FileHandle, FileHandle> socketpair(int family, int type, int protocol)
170{
171 int sv[2];
172 if (::socketpair(family, type, protocol, sv) < 0) {
173 throw std::system_error{make_error(errno), "socketpair"};
174 }
175 return {FileHandle{sv[0]}, FileHandle{sv[1]}};
176}
177
179template <typename ProtocolT>
180inline std::pair<FileHandle, FileHandle> socketpair(ProtocolT protocol,
181 std::error_code& ec) noexcept
182{
183 return socketpair(protocol.family(), protocol.type(), protocol.protocol(), ec);
184}
185
187template <typename ProtocolT>
188inline std::pair<FileHandle, FileHandle> socketpair(ProtocolT protocol)
189{
190 return socketpair(protocol.family(), protocol.type(), protocol.protocol());
191}
192
195 std::error_code& ec) noexcept
196{
197 // The addrlen argument is updated to contain the actual size of the source address.
198 // The returned address is truncated if the buffer provided is too small.
199 const auto fd = ::accept(sockfd, &addr, &addrlen);
200 if (fd < 0) {
202 }
203 return fd;
204}
205
208{
209 // The addrlen argument is updated to contain the actual size of the source address.
210 // The returned address is truncated if the buffer provided is too small.
211 const auto fd = ::accept(sockfd, &addr, &addrlen);
212 if (fd < 0) {
213 throw std::system_error{make_error(errno), "accept"};
214 }
215 return fd;
216}
217
219inline FileHandle accept(int sockfd, std::error_code& ec) noexcept
220{
221 const auto fd = ::accept(sockfd, nullptr, nullptr);
222 if (fd < 0) {
224 }
225 return fd;
226}
227
230{
231 const auto fd = ::accept(sockfd, nullptr, nullptr);
232 if (fd < 0) {
233 throw std::system_error{make_error(errno), "accept"};
234 }
235 return fd;
236}
237
239template <typename EndpointT>
240inline FileHandle accept(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
241{
242 socklen_t addrlen = ep.capacity();
243 FileHandle fh{accept(sockfd, *ep.data(), addrlen, ec)};
244 if (!ec) {
245 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
246 }
247 return fh;
248}
249
251template <typename EndpointT>
253{
254 socklen_t addrlen = ep.capacity();
255 FileHandle fh{accept(sockfd, *ep.data(), addrlen)};
256 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
257 return fh;
258}
259
261inline void bind(int sockfd, const sockaddr& addr, socklen_t addrlen, std::error_code& ec) noexcept
262{
263 if (::bind(sockfd, &addr, addrlen) < 0) {
265 }
266}
267
269inline void bind(int sockfd, const sockaddr& addr, socklen_t addrlen)
270{
271 if (::bind(sockfd, &addr, addrlen) < 0) {
272 throw std::system_error{make_error(errno), "bind"};
273 }
274}
275
277template <typename EndpointT>
278inline void bind(int sockfd, const EndpointT& ep, std::error_code& ec) noexcept
279{
280 bind(sockfd, *ep.data(), ep.size(), ec);
281}
282
284template <typename EndpointT>
285inline void bind(int sockfd, const EndpointT& ep)
286{
287 bind(sockfd, *ep.data(), ep.size());
288}
289
291inline void connect(int sockfd, const sockaddr& addr, socklen_t addrlen,
292 std::error_code& ec) noexcept
293{
294 if (::connect(sockfd, &addr, addrlen) < 0) {
296 }
297}
298
300inline void connect(int sockfd, const sockaddr& addr, socklen_t addrlen)
301{
302 if (::connect(sockfd, &addr, addrlen) < 0) {
303 throw std::system_error{make_error(errno), "connect"};
304 }
305}
306
308template <typename EndpointT>
309inline void connect(int sockfd, const EndpointT& ep, std::error_code& ec) noexcept
310{
311 connect(sockfd, *ep.data(), ep.size(), ec);
312}
313
315template <typename EndpointT>
316inline void connect(int sockfd, const EndpointT& ep)
317{
318 connect(sockfd, *ep.data(), ep.size());
319}
320
322inline void listen(int sockfd, int backlog, std::error_code& ec) noexcept
323{
324 if (::listen(sockfd, backlog) < 0) {
326 }
327}
328
330inline void listen(int sockfd, int backlog)
331{
332 if (::listen(sockfd, backlog) < 0) {
333 throw std::system_error{make_error(errno), "listen"};
334 }
335}
336
338inline void shutdown(int sockfd, int how, std::error_code& ec) noexcept
339{
340 if (::shutdown(sockfd, how) < 0) {
342 }
343}
344
346inline void shutdown(int sockfd, int how)
347{
348 if (::shutdown(sockfd, how) < 0) {
349 throw std::system_error{make_error(errno), "shutdown"};
350 }
351}
352
354inline ssize_t recv(int sockfd, void* buf, std::size_t len, int flags, std::error_code& ec) noexcept
355{
356 const auto ret = ::recv(sockfd, buf, len, flags);
357 if (ret < 0) {
359 }
360 return ret;
361}
362
364inline std::size_t recv(int sockfd, void* buf, std::size_t len, int flags)
365{
366 const auto ret = ::recv(sockfd, buf, len, flags);
367 if (ret < 0) {
368 throw std::system_error{make_error(errno), "recv"};
369 }
370 return ret;
371}
372
374inline ssize_t recv(int sockfd, MutableBuffer buf, int flags, std::error_code& ec) noexcept
375{
376 return recv(sockfd, buffer_cast<void*>(buf), buffer_size(buf), flags, ec);
377}
378
380inline std::size_t recv(int sockfd, MutableBuffer buf, int flags)
381{
382 return recv(sockfd, buffer_cast<void*>(buf), buffer_size(buf), flags);
383}
384
386inline ssize_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, sockaddr& addr,
387 socklen_t& addrlen, std::error_code& ec) noexcept
388{
389 // The addrlen argument is updated to contain the actual size of the source address.
390 // The returned address is truncated if the buffer provided is too small.
391 const auto ret = ::recvfrom(sockfd, buf, len, flags, &addr, &addrlen);
392 if (ret < 0) {
394 }
395 return ret;
396}
397
399inline std::size_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, sockaddr& addr,
401{
402 // The addrlen argument is updated to contain the actual size of the source address.
403 // The returned address is truncated if the buffer provided is too small.
404 const auto ret = ::recvfrom(sockfd, buf, len, flags, &addr, &addrlen);
405 if (ret < 0) {
406 throw std::system_error{make_error(errno), "recvfrom"};
407 }
408 return ret;
409}
410
412template <typename EndpointT>
413inline ssize_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, EndpointT& ep,
414 std::error_code& ec) noexcept
415{
416 socklen_t addrlen = ep.capacity();
417 const auto ret = recvfrom(sockfd, buf, len, flags, *ep.data(), addrlen, ec);
418 if (!ec) {
419 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
420 }
421 return ret;
422}
423
425template <typename EndpointT>
426inline std::size_t recvfrom(int sockfd, void* buf, std::size_t len, int flags, EndpointT& ep)
427{
428 socklen_t addrlen = ep.capacity();
429 const auto ret = recvfrom(sockfd, buf, len, flags, *ep.data(), addrlen);
430 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
431 return ret;
432}
433
435template <typename EndpointT>
437 std::error_code& ec) noexcept
438{
439 return recvfrom(sockfd, buffer_cast<void*>(buf), buffer_size(buf), flags, ep, ec);
440}
441
443template <typename EndpointT>
444inline std::size_t recvfrom(int sockfd, MutableBuffer buf, int flags, EndpointT& ep)
445{
446 return recvfrom(sockfd, buffer_cast<void*>(buf), buffer_size(buf), flags, ep);
447}
448
450inline ssize_t send(int sockfd, const void* buf, std::size_t len, int flags,
451 std::error_code& ec) noexcept
452{
453 const auto ret = ::send(sockfd, buf, len, flags);
454 if (ret < 0) {
456 }
457 return ret;
458}
459
461inline std::size_t send(int sockfd, const void* buf, std::size_t len, int flags)
462{
463 const auto ret = ::send(sockfd, buf, len, flags);
464 if (ret < 0) {
465 throw std::system_error{make_error(errno), "send"};
466 }
467 return ret;
468}
469
471inline ssize_t send(int sockfd, ConstBuffer buf, int flags, std::error_code& ec) noexcept
472{
473 return send(sockfd, buffer_cast<const void*>(buf), buffer_size(buf), flags, ec);
474}
475
477inline std::size_t send(int sockfd, ConstBuffer buf, int flags)
478{
479 return send(sockfd, buffer_cast<const void*>(buf), buffer_size(buf), flags);
480}
481
483inline ssize_t sendto(int sockfd, const void* buf, std::size_t len, int flags, const sockaddr& addr,
484 socklen_t addrlen, std::error_code& ec) noexcept
485{
486 const auto ret = ::sendto(sockfd, buf, len, flags, &addr, addrlen);
487 if (ret < 0) {
489 }
490 return ret;
491}
492
494inline std::size_t sendto(int sockfd, const void* buf, std::size_t len, int flags,
496{
497 const auto ret = ::sendto(sockfd, buf, len, flags, &addr, addrlen);
498 if (ret < 0) {
499 throw std::system_error{make_error(errno), "sendto"};
500 }
501 return ret;
502}
503
505template <typename EndpointT>
506inline ssize_t sendto(int sockfd, const void* buf, std::size_t len, int flags, const EndpointT& ep,
507 std::error_code& /*ec*/) noexcept
508{
509 return sendto(sockfd, buf, len, flags, *ep.data(), ep.size());
510}
511
513template <typename EndpointT>
514inline std::size_t sendto(int sockfd, const void* buf, std::size_t len, int flags,
515 const EndpointT& ep)
516{
517 return sendto(sockfd, buf, len, flags, *ep.data(), ep.size());
518}
519
521template <typename EndpointT>
523 std::error_code& ec) noexcept
524{
525 return sendto(sockfd, buffer_cast<const void*>(buf), buffer_size(buf), flags, *ep.data(),
526 ep.size(), ec);
527}
528
530template <typename EndpointT>
531inline std::size_t sendto(int sockfd, ConstBuffer buf, int flags, const EndpointT& ep)
532{
533 return sendto(sockfd, buffer_cast<const void*>(buf), buffer_size(buf), flags, *ep.data(),
534 ep.size());
535}
536
539 std::error_code& ec) noexcept
540{
541 // The addrlen argument is updated to contain the actual size of the source address.
542 // The returned address is truncated if the buffer provided is too small.
543 if (::getsockname(sockfd, &addr, &addrlen) < 0) {
545 }
546}
547
550{
551 // The addrlen argument is updated to contain the actual size of the source address.
552 // The returned address is truncated if the buffer provided is too small.
553 if (::getsockname(sockfd, &addr, &addrlen) < 0) {
554 throw std::system_error{make_error(errno), "getsockname"};
555 }
556}
557
559template <typename EndpointT>
560inline void getsockname(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
561{
562 socklen_t addrlen = ep.capacity();
563 getsockname(sockfd, *ep.data(), addrlen, ec);
564 if (!ec) {
565 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
566 }
567}
568
570template <typename EndpointT>
571inline void getsockname(int sockfd, EndpointT& ep)
572{
573 socklen_t addrlen = ep.capacity();
574 getsockname(sockfd, *ep.data(), addrlen);
575 ep.resize(std::min<std::size_t>(addrlen, ep.capacity()));
576}
577
579inline void getsockopt(int sockfd, int level, int optname, void* optval, socklen_t& optlen,
580 std::error_code& ec) noexcept
581{
582 if (::getsockopt(sockfd, level, optname, optval, &optlen) < 0) {
584 }
585}
586
588inline void getsockopt(int sockfd, int level, int optname, void* optval, socklen_t& optlen)
589{
590 if (::getsockopt(sockfd, level, optname, optval, &optlen) < 0) {
591 throw std::system_error{make_error(errno), "getsockopt"};
592 }
593}
594
596inline void setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen,
597 std::error_code& ec) noexcept
598{
599 if (::setsockopt(sockfd, level, optname, optval, optlen) < 0) {
601 }
602}
603
605inline void setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen)
606{
607 if (::setsockopt(sockfd, level, optname, optval, optlen) < 0) {
608 throw std::system_error{make_error(errno), "setsockopt"};
609 }
610}
611
612} // namespace os
613inline namespace net {
614
616TOOLBOX_API AddrInfoPtr get_unix_addrinfo(std::string_view path, int type);
617
618template <typename EndpointT>
619inline void get_sock_name(int sockfd, EndpointT& ep, std::error_code& ec) noexcept
620{
622}
623
624template <typename EndpointT>
626{
628}
629
630inline std::error_code get_so_error(int sockfd, std::error_code& ec) noexcept
631{
632 int optval{};
633 socklen_t optlen{sizeof(optval)};
635 return make_error(optval);
636}
637
638inline std::error_code get_so_error(int sockfd)
639{
640 int optval{};
641 socklen_t optlen{sizeof(optval)};
643 return make_error(optval);
644}
645
646inline int get_so_rcv_buf(int sockfd, std::error_code& ec) noexcept
647{
648 int optval{};
649 socklen_t optlen{sizeof(optval)};
651 return optval;
652}
653
654inline int get_so_rcv_buf(int sockfd)
655{
656 int optval{};
657 socklen_t optlen{sizeof(optval)};
659 return optval;
660}
661
662inline int get_so_snd_buf(int sockfd, std::error_code& ec) noexcept
663{
664 int optval{};
665 socklen_t optlen{sizeof(optval)};
667 return optval;
668}
669
670inline int get_so_snd_buf(int sockfd)
671{
672 int optval{};
673 socklen_t optlen{sizeof(optval)};
675 return optval;
676}
677
678inline bool is_tcp_no_delay(int sockfd, std::error_code& ec) noexcept
679{
680 int optval{};
681 socklen_t optlen{sizeof(optval)};
683 return optval != 0;
684}
685
686inline bool is_tcp_no_delay(int sockfd)
687{
688 int optval{};
689 socklen_t optlen{sizeof(optval)};
691 return optval != 0;
692}
693
694inline void set_so_rcv_buf(int sockfd, int size, std::error_code& ec) noexcept
695{
697}
698
699inline void set_so_rcv_buf(int sockfd, int size)
700{
702}
703
704inline void set_so_reuse_addr(int sockfd, bool enabled, std::error_code& ec) noexcept
705{
706 int optval{enabled ? 1 : 0};
708}
709
710inline void set_so_reuse_addr(int sockfd, bool enabled)
711{
712 int optval{enabled ? 1 : 0};
714}
715
716inline void set_so_snd_buf(int sockfd, int size, std::error_code& ec) noexcept
717{
719}
720
721inline void set_so_snd_buf(int sockfd, int size)
722{
724}
725
736inline void set_tcp_no_delay(int sockfd, bool enabled, std::error_code& ec) noexcept
737{
738 int optval{enabled ? 1 : 0};
740}
741
751inline void set_tcp_no_delay(int sockfd, bool enabled)
752{
753 int optval{enabled ? 1 : 0};
755}
756
764inline void set_tcp_syn_nt(int sockfd, int retrans, std::error_code& ec) noexcept
765{
767}
768
775inline void set_tcp_syn_nt(int sockfd, int retrans)
776{
778}
779
780struct Sock : FileHandle {
783 , family_{family}
784 {
785 }
787
788 int family() const noexcept { return family_; }
789 bool is_ip_family() const noexcept { return family_ == AF_INET || family_ == AF_INET6; }
790
791 // Logically const.
792 std::error_code get_error(std::error_code& ec) const noexcept
793 {
794 return toolbox::get_so_error(get(), ec);
795 }
796 std::error_code get_error() const { return toolbox::get_so_error(get()); }
797
798 int get_rcv_buf(std::error_code& ec) const noexcept
799 {
800 return toolbox::get_so_rcv_buf(get(), ec);
801 }
802 int get_rcv_buf() const { return toolbox::get_so_rcv_buf(get()); }
803
804 int get_snd_buf(std::error_code& ec) const noexcept
805 {
806 return toolbox::get_so_snd_buf(get(), ec);
807 }
808 int get_snd_buf() const { return toolbox::get_so_snd_buf(get()); }
809
810 bool is_tcp_no_delay(std::error_code& ec) const noexcept
811 {
813 }
814 bool is_tcp_no_delay() const { return toolbox::is_tcp_no_delay(get()); }
815 void close() { reset(); }
816
817 void set_non_block(std::error_code& ec) noexcept { toolbox::set_non_block(get(), ec); }
819
820 void set_rcv_buf(int size, std::error_code& ec) noexcept
821 {
823 }
825
826 void set_reuse_addr(bool enabled, std::error_code& ec) noexcept
827 {
829 }
831
832 void set_snd_buf(int size, std::error_code& ec) noexcept
833 {
835 }
837
838 private:
839 int family_{};
840};
841
842} // namespace net
843} // namespace toolbox
844
845#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:662
void set_so_reuse_addr(int sockfd, bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:704
std::unique_ptr< addrinfo, void(*)(addrinfo *)> AddrInfoPtr
Definition Socket.hpp:30
void set_so_snd_buf(int sockfd, int size, std::error_code &ec) noexcept
Definition Socket.hpp:716
void set_so_rcv_buf(int sockfd, int size, std::error_code &ec) noexcept
Definition Socket.hpp:694
std::error_code get_so_error(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:630
void set_tcp_no_delay(int sockfd, bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:736
void set_tcp_syn_nt(int sockfd, int retrans, std::error_code &ec) noexcept
Definition Socket.hpp:764
void get_sock_name(int sockfd, EndpointT &ep, std::error_code &ec) noexcept
Definition Socket.hpp:619
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:678
int get_so_rcv_buf(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:646
FileHandle accept(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Accept a connection on a socket.
Definition Socket.hpp:194
void getsockopt(int sockfd, int level, int optname, void *optval, socklen_t &optlen, std::error_code &ec) noexcept
Get socket option.
Definition Socket.hpp:579
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:596
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:158
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:35
void listen(int sockfd, int backlog, std::error_code &ec) noexcept
Listen for connections on a socket.
Definition Socket.hpp:322
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:386
void getsockname(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Get the socket name.
Definition Socket.hpp:538
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:100
FileHandle socket(int family, int type, int protocol, std::error_code &ec) noexcept
Create an endpoint for communication.
Definition Socket.hpp:124
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:450
void shutdown(int sockfd, int how, std::error_code &ec) noexcept
Shut-down part of a full-duplex connection.
Definition Socket.hpp:338
void connect(int sockfd, const sockaddr &addr, socklen_t addrlen, std::error_code &ec) noexcept
Initiate a connection on a socket.
Definition Socket.hpp:291
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:483
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:354
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
TOOLBOX_API void reset(std::ostream &os) noexcept
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:92
bool is_tcp_no_delay() const
Definition Socket.hpp:814
void set_reuse_addr(bool enabled)
Definition Socket.hpp:830
int family() const noexcept
Definition Socket.hpp:788
int get_rcv_buf(std::error_code &ec) const noexcept
Definition Socket.hpp:798
int get_snd_buf() const
Definition Socket.hpp:808
std::error_code get_error(std::error_code &ec) const noexcept
Definition Socket.hpp:792
void set_rcv_buf(int size)
Definition Socket.hpp:824
bool is_tcp_no_delay(std::error_code &ec) const noexcept
Definition Socket.hpp:810
int get_rcv_buf() const
Definition Socket.hpp:802
Sock() noexcept=default
void set_snd_buf(int size)
Definition Socket.hpp:836
Sock(FileHandle &&sock, int family)
Definition Socket.hpp:781
void set_reuse_addr(bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:826
int get_snd_buf(std::error_code &ec) const noexcept
Definition Socket.hpp:804
void set_non_block(std::error_code &ec) noexcept
Definition Socket.hpp:817
std::error_code get_error() const
Definition Socket.hpp:796
void set_snd_buf(int size, std::error_code &ec) noexcept
Definition Socket.hpp:832
void set_rcv_buf(int size, std::error_code &ec) noexcept
Definition Socket.hpp:820
bool is_ip_family() const noexcept
Definition Socket.hpp:789