25#ifndef JOIN_CORE_SOCKET_HPP
26#define JOIN_CORE_SOCKET_HPP
37#include <openssl/err.h>
44#include <netinet/tcp.h>
45#include <linux/icmp.h>
58 template <
class Protocol>
62 using Ptr = std::unique_ptr<BasicSocket<Protocol>>;
153 other._protocol = Protocol ();
165 this->
_state = other._state;
166 this->
_mode = other._mode;
173 other._protocol = Protocol ();
209 lastError = std::error_code (errno, std::generic_category ());
245 if (endpoint.protocol ().family () == AF_PACKET)
247 if (
reinterpret_cast<const struct sockaddr_ll*
> (endpoint.addr ())->sll_ifindex == 0)
249 lastError = std::make_error_code (std::errc::no_such_device);
253 else if ((endpoint.protocol ().family () == AF_INET6) || (endpoint.protocol ().family () == AF_INET))
257 else if (endpoint.protocol ().family () == AF_UNIX)
259 ::unlink (endpoint.device ().c_str ());
262 if (
::bind (this->
_handle, endpoint.addr (), endpoint.length ()) == -1)
264 lastError = std::error_code (errno, std::generic_category ());
280 if (::ioctl (this->
_handle, FIONREAD, &available) == -1)
282 lastError = std::error_code (errno, std::generic_category ());
296 return (this->
wait (
true,
false, timeout) == 0);
305 virtual int read (
char* data,
unsigned long maxSize)
noexcept
309 iov.iov_len = maxSize;
311 struct msghdr message;
312 message.msg_name =
nullptr;
313 message.msg_namelen = 0;
314 message.msg_iov = &iov;
315 message.msg_iovlen = 1;
316 message.msg_control =
nullptr;
317 message.msg_controllen = 0;
319 int size = ::recvmsg (this->
_handle, &message, 0);
324 lastError = std::error_code (errno, std::generic_category ());
343 return (this->
wait (
false,
true, timeout) == 0);
352 virtual int write (
const char* data,
unsigned long maxSize)
noexcept
355 iov.iov_base =
const_cast<char*
> (data);
356 iov.iov_len = maxSize;
358 struct msghdr message;
359 message.msg_name =
nullptr;
360 message.msg_namelen = 0;
361 message.msg_iov = &iov;
362 message.msg_iovlen = 1;
363 message.msg_control =
nullptr;
364 message.msg_controllen = 0;
366 int result = ::sendmsg (this->
_handle, &message, 0);
369 lastError = std::error_code (errno, std::generic_category ());
386 int flags = ::fcntl (this->
_handle, F_GETFL, 0);
390 flags = flags | O_NONBLOCK;
394 flags = flags & ~O_NONBLOCK;
397 ::fcntl (this->
_handle, F_SETFL, flags);
409 int optlevel, optname;
414 optlevel = SOL_SOCKET;
415 optname = SO_KEEPALIVE;
419 optlevel = SOL_SOCKET;
424 optlevel = SOL_SOCKET;
429 optlevel = SOL_SOCKET;
430 optname = SO_TIMESTAMP;
434 optlevel = SOL_SOCKET;
435 optname = SO_REUSEADDR;
439 optlevel = SOL_SOCKET;
440 optname = SO_REUSEPORT;
444 optlevel = SOL_SOCKET;
445 optname = SO_BROADCAST;
449 optlevel = SOL_PACKET;
450 optname = PACKET_AUXDATA;
458 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
461 lastError = std::error_code (errno, std::generic_category ());
474 struct sockaddr_storage sa;
475 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
477 if (::getsockname (this->
_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len) == -1)
482 return Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
546 static uint16_t
checksum (
const uint16_t* data,
size_t len, uint16_t current = 0)
548 uint32_t sum = current;
558#if __BYTE_ORDER == __LITTLE_ENDIAN
559 sum += *
reinterpret_cast<const uint8_t*
> (data);
561 sum += *
reinterpret_cast<const uint8_t*
> (data) << 8;
565 sum = (sum >> 16) + (sum & 0xffff);
568 return static_cast<uint16_t
> (~sum);
579 int wait (
bool wantRead,
bool wantWrite,
int timeout)
const noexcept
581 struct pollfd
handle = {.fd = this->
_handle, .events = 0, .revents = 0};
593 int nset = (
handle.fd > -1) ? ::poll (&
handle, 1, timeout) : -1;
602 lastError = std::error_code (errno, std::generic_category ());
634 template <
class Protocol>
637 return a.handle () < b.handle ();
643 template <
class Protocol>
647 using Ptr = std::unique_ptr<BasicDatagramSocket<Protocol>>;
705 _remote = std::move (other._remote);
723 virtual int open (
const Protocol&
protocol = Protocol ()) noexcept
override
733 if ((
protocol.protocol () == IPPROTO_UDP) || (
protocol.protocol () == IPPROTO_TCP))
735 if ((
protocol.family () == AF_INET6) &&
736 (::setsockopt (this->_handle, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof (off)) == -1))
738 lastError = std::error_code (errno, std::generic_category ());
744 if ((
protocol.protocol () == IPPROTO_ICMPV6) || (
protocol.protocol () == IPPROTO_ICMP))
746 if ((
protocol.family () == AF_INET) &&
747 (::setsockopt (this->_handle, IPPROTO_IP, IP_HDRINCL, &off, sizeof (off)) == -1))
749 lastError = std::error_code (errno, std::generic_category ());
768 if (this->
_state == State::Closed)
774 if (this->
_state == State::Connected)
780 if ((this->
_protocol.family () == AF_INET6) || (this->_protocol.family () == AF_INET))
785 int result = setsockopt (this->
_handle, SOL_SOCKET, SO_BINDTODEVICE, device.c_str (), device.size ());
788 lastError = std::error_code (errno, std::generic_category ());
802 if ((this->
_state != State::Closed) && (this->
_state != State::Disconnected))
808 if ((this->
_state == State::Closed) && (this->
open (endpoint.protocol ()) == -1))
813 int result =
::connect (this->
_handle, endpoint.addr (), endpoint.length ());
815 this->
_state = State::Connecting;
820 lastError = std::error_code (errno, std::generic_category ());
821 if (lastError != std::errc::operation_in_progress)
828 this->
_state = State::Connected;
839 if (this->
_state == State::Connected)
841 struct sockaddr_storage nullAddr;
842 ::memset (&nullAddr, 0,
sizeof (nullAddr));
844 nullAddr.ss_family = AF_UNSPEC;
846 int result =
::connect (this->
_handle,
reinterpret_cast<struct sockaddr*
> (&nullAddr),
847 sizeof (
struct sockaddr_storage));
850 if (errno != EAFNOSUPPORT)
852 lastError = std::error_code (errno, std::generic_category ());
857 this->
_state = State::Disconnected;
867 virtual void close () noexcept
override
879 virtual int read (
char* data,
unsigned long maxSize)
noexcept override
891 virtual int readFrom (
char* data,
unsigned long maxSize,
Endpoint* endpoint =
nullptr) noexcept
893 struct sockaddr_storage sa;
894 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
896 int size = ::recvfrom (this->
_handle, data, maxSize, 0,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len);
901 lastError = std::error_code (errno, std::generic_category ());
906 this->
_state = State::Disconnected;
912 if (endpoint !=
nullptr)
914 *endpoint =
Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
926 virtual int write (
const char* data,
unsigned long maxSize)
noexcept override
938 virtual int writeTo (
const char* data,
unsigned long maxSize,
const Endpoint& endpoint)
noexcept
940 if ((this->
_state == State::Closed) && (this->
open (endpoint.protocol ()) == -1))
945 int result = ::sendto (this->
_handle, data, maxSize, 0, endpoint.addr (), endpoint.length ());
948 lastError = std::error_code (errno, std::generic_category ());
963 if (this->
_state == State::Closed)
969 int optlevel, optname;
974 if (this->
family () == AF_INET6)
976 optlevel = IPPROTO_IPV6;
977 optname = IPV6_UNICAST_HOPS;
981 optlevel = IPPROTO_IP;
986 case Option::MulticastLoop:
987 if (this->
family () == AF_INET6)
989 optlevel = IPPROTO_IPV6;
990 optname = IPV6_MULTICAST_LOOP;
994 optlevel = IPPROTO_IP;
995 optname = IP_MULTICAST_LOOP;
999 case Option::MulticastTtl:
1000 if (this->
family () == AF_INET6)
1002 optlevel = IPPROTO_IPV6;
1003 optname = IPV6_MULTICAST_HOPS;
1007 optlevel = IPPROTO_IP;
1008 optname = IP_MULTICAST_TTL;
1012 case Option::PathMtuDiscover:
1013 if (this->
family () == AF_INET6)
1015 optlevel = IPPROTO_IPV6;
1016 optname = IPV6_MTU_DISCOVER;
1020 optlevel = IPPROTO_IP;
1021 optname = IP_MTU_DISCOVER;
1025 case Option::RcvError:
1026 if (this->
family () == AF_INET6)
1028 optlevel = IPPROTO_IPV6;
1029 optname = IPV6_RECVERR;
1033 optlevel = IPPROTO_IP;
1034 optname = IP_RECVERR;
1042 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
1045 lastError = std::error_code (errno, std::generic_category ());
1067 return (this->
_state == State::Connected);
1076 if (this->
_state == State::Closed)
1082 int result = -1, value = -1;
1083 socklen_t valueLen =
sizeof (value);
1085 if (this->
_protocol.family () == AF_INET6)
1087 result = ::getsockopt (this->
_handle, IPPROTO_IPV6, IPV6_MTU, &value, &valueLen);
1089 else if (this->
_protocol.family () == AF_INET)
1091 result = ::getsockopt (this->
_handle, IPPROTO_IP, IP_MTU, &value, &valueLen);
1101 lastError = std::error_code (errno, std::generic_category ());
1131 template <
class Protocol>
1134 return a.handle () < b.handle ();
1140 template <
class Protocol>
1144 using Ptr = std::unique_ptr<BasicStreamSocket<Protocol>>;
1213 if (this->
_state != State::Connected)
1215 if (this->
_state != State::Connecting)
1238 if (this->
_state == State::Connected)
1240 ::shutdown (this->
_handle, SHUT_WR);
1241 this->
_state = State::Disconnecting;
1244 if (this->
_state == State::Disconnecting)
1252 int result = this->
read (buffer,
sizeof (buffer));
1264 ::shutdown (this->
_handle, SHUT_RD);
1265 this->
_state = State::Disconnected;
1280 if ((this->
_state != State::Disconnected) && (this->
_state != State::Closed))
1282 if (this->
_state != State::Disconnecting)
1288 auto start = std::chrono::steady_clock::now ();
1305 elapsed = std::chrono::duration_cast<std::chrono::milliseconds> (
1306 std::chrono::steady_clock::now () - start)
1326 unsigned long numRead = 0;
1328 while (numRead < size)
1330 int result = this->
read (data + numRead, size - numRead);
1355 int readExactly (std::string& data,
unsigned long size,
int timeout = 0)
1370 unsigned long numWrite = 0;
1372 while (numWrite < size)
1374 int result = this->
write (data + numWrite, size - numWrite);
1400 if (this->
_state == State::Closed)
1406 int optlevel, optname;
1410 case Option::NoDelay:
1411 optlevel = IPPROTO_TCP;
1412 optname = TCP_NODELAY;
1415 case Option::KeepIdle:
1416 optlevel = IPPROTO_TCP;
1417 optname = TCP_KEEPIDLE;
1420 case Option::KeepIntvl:
1421 optlevel = IPPROTO_TCP;
1422 optname = TCP_KEEPINTVL;
1425 case Option::KeepCount:
1426 optlevel = IPPROTO_TCP;
1427 optname = TCP_KEEPCNT;
1434 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
1437 lastError = std::error_code (errno, std::generic_category ());
1450 return (this->
_state == State::Connecting);
1459 if (this->
_state == State::Connected)
1463 else if (this->
_state != State::Connecting)
1469 socklen_t optlen =
sizeof (optval);
1471 int result = ::getsockopt (this->
_handle, SOL_SOCKET, SO_ERROR, &optval, &optlen);
1472 if ((result == -1) || (optval != 0))
1477 this->
_state = State::Connected;
1492 template <
class Protocol>
1495 return a.handle () < b.handle ();
1517 virtual const char*
name ()
const noexcept;
1524 virtual std::string
message (
int code)
const;
1550 template <
class Protocol>
1554 using Ptr = std::unique_ptr<BasicTlsSocket<Protocol>>;
1574 ,
_tlsContext (SSL_CTX_new (TLS_client_method ()))
1577 SSL_CTX_set_options (this->
_tlsContext.get (), SSL_OP_ALL);
1580 SSL_CTX_set_options (this->
_tlsContext.get (), SSL_OP_NO_COMPRESSION);
1584 SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
1588 SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1591 SSL_CTX_set_mode (this->
_tlsContext.get (), SSL_MODE_AUTO_RETRY);
1594 SSL_CTX_set_session_cache_mode (this->
_tlsContext.get (), SSL_SESS_CACHE_CLIENT);
1597 SSL_CTX_set_verify (this->
_tlsContext.get (), SSL_VERIFY_NONE,
nullptr);
1626 throw std::invalid_argument (
"OpenSSL context is invalid");
1655 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1670 this->
_tlsContext = std::move (other._tlsContext);
1671 this->
_tlsHandle = std::move (other._tlsHandle);
1676 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1725 if (SSL_set_fd (this->
_tlsHandle.get (), this->_handle) != 1)
1732 if (SSL_is_server (this->
_tlsHandle.get ()) == 0)
1734 if (!this->
_remote.hostname ().empty () &&
1735 (SSL_set_tlsext_host_name (this->
_tlsHandle.get (), this->_remote.hostname ().c_str ()) != 1))
1742 SSL_set_connect_state (this->
_tlsHandle.get ());
1746 SSL_set_accept_state (this->
_tlsHandle.get ());
1749 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1770 if (this->
_state == State::Connecting)
1813 if ((SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN) ==
false)
1816 int result = SSL_shutdown (this->
_tlsHandle.get ());
1820 switch (SSL_get_error (this->
_tlsHandle.get (), result))
1822 case SSL_ERROR_WANT_READ:
1823 case SSL_ERROR_WANT_WRITE:
1827 case SSL_ERROR_SYSCALL:
1836 this->
_state = State::Disconnected;
1839 lastError = std::error_code (errno, std::generic_category ());
1846 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
1854 else if (result == 1)
1907 return SSL_pending (this->
_tlsHandle.get ());
1919 virtual int read (
char* data,
unsigned long maxSize)
noexcept override
1924 int result = SSL_read (this->
_tlsHandle.get (), data, int (maxSize));
1927 switch (SSL_get_error (this->
_tlsHandle.get (), result))
1929 case SSL_ERROR_WANT_READ:
1930 case SSL_ERROR_WANT_WRITE:
1931 case SSL_ERROR_WANT_X509_LOOKUP:
1935 case SSL_ERROR_ZERO_RETURN:
1939 if (SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN)
1944 case SSL_ERROR_SYSCALL:
1953 this->
_state = State::Disconnected;
1956 lastError = std::error_code (errno, std::generic_category ());
1963 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2001 virtual int write (
const char* data,
unsigned long maxSize)
noexcept override
2006 int result = SSL_write (this->
_tlsHandle.get (), data, int (maxSize));
2009 switch (SSL_get_error (this->
_tlsHandle.get (), result))
2011 case SSL_ERROR_WANT_READ:
2012 case SSL_ERROR_WANT_WRITE:
2013 case SSL_ERROR_WANT_X509_LOOKUP:
2017 case SSL_ERROR_ZERO_RETURN:
2021 if (SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN)
2026 case SSL_ERROR_SYSCALL:
2035 this->
_state = State::Disconnected;
2038 lastError = std::error_code (errno, std::generic_category ());
2045 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2078 ? SSL_use_certificate_file (this->
_tlsHandle.get (), cert.c_str (), SSL_FILETYPE_PEM)
2079 : SSL_CTX_use_certificate_file (this->
_tlsContext.get (), cert.c_str (), SSL_FILETYPE_PEM)) == 0)
2088 ? SSL_use_PrivateKey_file (this->
_tlsHandle.get (),
key.c_str (), SSL_FILETYPE_PEM)
2089 : SSL_CTX_use_PrivateKey_file (this->
_tlsContext.get (),
key.c_str (), SSL_FILETYPE_PEM)) == 0)
2097 : SSL_CTX_check_private_key (this->
_tlsContext.get ())) == 0)
2114 if (stat (caPath.c_str (), &st) != 0 || !S_ISDIR (st.st_mode) ||
2115 SSL_CTX_load_verify_locations (this->
_tlsContext.get (),
nullptr, caPath.c_str ()) == 0)
2132 if (stat (caFile.c_str (), &st) != 0 || !S_ISREG (st.st_mode) ||
2133 SSL_CTX_load_verify_locations (this->
_tlsContext.get (), caFile.c_str (),
nullptr) == 0)
2152 SSL_CTX_set_verify_depth (this->
_tlsContext.get (), depth);
2156 SSL_CTX_set_verify (this->
_tlsContext.get (), SSL_VERIFY_NONE,
nullptr);
2168 : SSL_CTX_set_cipher_list (this->
_tlsContext.get (), cipher.c_str ())) == 0)
2185 : SSL_CTX_set_ciphersuites (this->
_tlsContext.get (), cipher.c_str ())) == 0)
2211 int result = SSL_do_handshake (this->
_tlsHandle.get ());
2214 switch (SSL_get_error (this->
_tlsHandle.get (), result))
2216 case SSL_ERROR_WANT_READ:
2217 case SSL_ERROR_WANT_WRITE:
2218 case SSL_ERROR_WANT_X509_LOOKUP:
2222 case SSL_ERROR_ZERO_RETURN:
2227 case SSL_ERROR_SYSCALL:
2235 this->
_state = State::Disconnected;
2238 lastError = std::error_code (errno, std::generic_category ());
2245 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2278 if (where & SSL_CB_ALERT)
2280 std::cout <<
"SSL/TLS Alert ";
2281 (where & SSL_CB_READ) ? std::cout <<
"[read] " : std::cout <<
"[write] ";
2282 std::cout << SSL_alert_type_string_long (ret) <<
":";
2283 std::cout << SSL_alert_desc_string_long (ret);
2284 std::cout << std::endl;
2286 else if (where & SSL_CB_LOOP)
2288 std::cout <<
"SSL/TLS State ";
2289 (SSL_in_connect_init (this->
_tlsHandle.get ())) ? std::cout <<
"[connect] "
2290 : (SSL_in_accept_init (this->
_tlsHandle.get ())) ? std::cout <<
"[accept] "
2291 : std::cout <<
"[undefined] ";
2292 std::cout << SSL_state_string_long (this->
_tlsHandle.get ());
2293 std::cout << std::endl;
2295 else if (where & SSL_CB_HANDSHAKE_START)
2297 std::cout <<
"SSL/TLS Handshake [Start] " << SSL_state_string_long (this->
_tlsHandle.get ())
2300 else if (where & SSL_CB_HANDSHAKE_DONE)
2302 std::cout <<
"SSL/TLS Handshake [Done] " << SSL_state_string_long (this->
_tlsHandle.get ())
2304 std::cout << SSL_CTX_sess_number (this->
_tlsContext.get ()) <<
" items in the session cache"
2306 std::cout << SSL_CTX_sess_connect (this->
_tlsContext.get ()) <<
" client connects" << std::endl;
2307 std::cout << SSL_CTX_sess_connect_good (this->
_tlsContext.get ()) <<
" client connects that finished"
2309 std::cout << SSL_CTX_sess_connect_renegotiate (this->
_tlsContext.get ())
2310 <<
" client renegotiations requested" << std::endl;
2311 std::cout << SSL_CTX_sess_accept (this->
_tlsContext.get ()) <<
" server connects" << std::endl;
2312 std::cout << SSL_CTX_sess_accept_good (this->
_tlsContext.get ()) <<
" server connects that finished"
2314 std::cout << SSL_CTX_sess_accept_renegotiate (this->
_tlsContext.get ())
2315 <<
" server renegotiations requested" << std::endl;
2316 std::cout << SSL_CTX_sess_hits (this->
_tlsContext.get ()) <<
" session cache hits" << std::endl;
2317 std::cout << SSL_CTX_sess_cb_hits (this->
_tlsContext.get ()) <<
" external session cache hits"
2319 std::cout << SSL_CTX_sess_misses (this->
_tlsContext.get ()) <<
" session cache misses" << std::endl;
2320 std::cout << SSL_CTX_sess_timeouts (this->
_tlsContext.get ()) <<
" session cache timeouts" << std::endl;
2321 std::cout <<
"negotiated " << SSL_get_cipher (this->
_tlsHandle.get ()) <<
" cipher suite" << std::endl;
2333 SSL* ssl =
static_cast<SSL*
> (X509_STORE_CTX_get_ex_data (context, SSL_get_ex_data_X509_STORE_CTX_idx ()));
2348 int maxDepth = SSL_get_verify_depth (this->
_tlsHandle.get ());
2349 int dpth = X509_STORE_CTX_get_error_depth (context);
2352 std::cout <<
"verification started at depth=" << dpth << std::endl;
2356 if ((maxDepth >= 0) && (dpth > maxDepth))
2359 X509_STORE_CTX_set_error (context, X509_V_ERR_CERT_CHAIN_TOO_LONG);
2365 std::cout <<
"verification failed at depth=" << dpth <<
" - "
2366 << X509_verify_cert_error_string (X509_STORE_CTX_get_error (context)) << std::endl;
2375 std::cout <<
"rejected by CERT at depth=" << dpth << std::endl;
2399 std::cout <<
"certificate accepted at depth=" << dpth << std::endl;
2412 int depth = X509_STORE_CTX_get_error_depth (context);
2413 X509* cert = X509_STORE_CTX_get_current_cert (context);
2416 X509_NAME_oneline (X509_get_subject_name (cert), buf,
sizeof (buf));
2418 std::cout <<
"subject=" << buf << std::endl;
2428 std::cout <<
"no match for hostname in the certificate" << std::endl;
2448 X509_get_ext_d2i (certificate, NID_subject_alt_name, 0, 0)));
2451 for (
int i = 0; (i < sk_GENERAL_NAME_num (altnames.get ())) && !match; ++i)
2454 GENERAL_NAME* current_name = sk_GENERAL_NAME_value (altnames.get (), i);
2456 if (current_name->type == GEN_DNS)
2459 const char* host =
reinterpret_cast<const char*
> (ASN1_STRING_get0_data (current_name->d.ia5));
2460 size_t len = size_t (ASN1_STRING_length (current_name->d.ia5));
2461 std::string pattern (host, host + len), serverName (this->
_remote.hostname ());
2464 if (pattern.back () ==
'.')
2466 pattern.pop_back ();
2469 if (serverName.back () ==
'.')
2471 serverName.pop_back ();
2475 if (fnmatch (pattern.c_str (), serverName.c_str (), 0) == 0)
2526 template <
class Protocol>
2529 return a.handle () < b.handle ();
2537 struct is_error_condition_enum<
join::TlsErrc> :
public true_type
basic datagram socket class.
Definition socket.hpp:645
virtual ~BasicDatagramSocket()=default
Destroy the instance.
BasicDatagramSocket(const BasicDatagramSocket &other)=delete
Copy constructor.
typename BasicSocket< Protocol >::Option Option
Definition socket.hpp:649
BasicDatagramSocket(Mode mode, int ttl=60)
Create instance specifying the mode.
Definition socket.hpp:665
virtual int connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socket.hpp:800
virtual int bindToDevice(const std::string &device) noexcept
assigns the specified device to the socket.
Definition socket.hpp:766
std::unique_ptr< BasicDatagramSocket< Protocol > > Ptr
Definition socket.hpp:647
virtual bool connected() noexcept
check if the socket is connected.
Definition socket.hpp:1065
virtual int setOption(Option option, int value) noexcept override
set the given option to the given value.
Definition socket.hpp:961
Endpoint _remote
remote endpoint.
Definition socket.hpp:1119
virtual int read(char *data, unsigned long maxSize) noexcept override
read data.
Definition socket.hpp:879
int _ttl
packet time to live.
Definition socket.hpp:1122
virtual int write(const char *data, unsigned long maxSize) noexcept override
write data.
Definition socket.hpp:926
virtual void close() noexcept override
close the socket handle.
Definition socket.hpp:867
typename Protocol::Endpoint Endpoint
Definition socket.hpp:651
int ttl() const
returns the Time-To-Live value.
Definition socket.hpp:1112
BasicDatagramSocket(int ttl=60)
Default constructor.
Definition socket.hpp:656
typename BasicSocket< Protocol >::State State
Definition socket.hpp:650
int mtu() const
get socket mtu.
Definition socket.hpp:1074
BasicDatagramSocket & operator=(const BasicDatagramSocket &other)=delete
Copy assignment operator.
virtual int readFrom(char *data, unsigned long maxSize, Endpoint *endpoint=nullptr) noexcept
read data on the socket.
Definition socket.hpp:891
virtual int writeTo(const char *data, unsigned long maxSize, const Endpoint &endpoint) noexcept
write data on the socket.
Definition socket.hpp:938
virtual int disconnect()
shutdown the connection.
Definition socket.hpp:837
typename BasicSocket< Protocol >::Mode Mode
Definition socket.hpp:648
virtual int open(const Protocol &protocol=Protocol()) noexcept override
open socket using the given protocol.
Definition socket.hpp:723
BasicDatagramSocket(BasicDatagramSocket &&other)
Move constructor.
Definition socket.hpp:688
const Endpoint & remoteEndpoint() const
determine the remote endpoint associated with this socket.
Definition socket.hpp:1056
basic socket class.
Definition socket.hpp:60
bool opened() const noexcept
check if the socket is opened.
Definition socket.hpp:489
BasicSocket & operator=(const BasicSocket &other)=delete
copy assignment operator.
virtual int open(const Protocol &protocol=Protocol()) noexcept
open socket using the given protocol.
Definition socket.hpp:194
static uint16_t checksum(const uint16_t *data, size_t len, uint16_t current=0)
get standard 1s complement checksum.
Definition socket.hpp:546
void setMode(Mode mode) noexcept
set the socket to the non-blocking or blocking mode.
Definition socket.hpp:380
State
socket states.
Definition socket.hpp:102
@ Disconnected
Definition socket.hpp:106
@ Connecting
Definition socket.hpp:103
@ Disconnecting
Definition socket.hpp:105
@ Closed
Definition socket.hpp:107
@ Connected
Definition socket.hpp:104
Protocol _protocol
protocol.
Definition socket.hpp:625
virtual int setOption(Option option, int value) noexcept
set the given option to the given value.
Definition socket.hpp:407
Mode _mode
socket mode.
Definition socket.hpp:619
virtual void close() noexcept
close the socket.
Definition socket.hpp:223
int family() const noexcept
get socket address family.
Definition socket.hpp:507
Mode
socket modes.
Definition socket.hpp:69
@ Blocking
Definition socket.hpp:70
@ NonBlocking
Definition socket.hpp:71
virtual bool waitReadyRead(int timeout=0) const noexcept
block until new data is available for reading.
Definition socket.hpp:294
std::unique_ptr< BasicSocket< Protocol > > Ptr
Definition socket.hpp:62
virtual ~BasicSocket()
destroy the socket instance.
Definition socket.hpp:181
virtual int bind(const Endpoint &endpoint) noexcept
assigns the specified endpoint to the socket.
Definition socket.hpp:238
Endpoint localEndpoint() const
determine the local endpoint associated with this socket.
Definition socket.hpp:472
BasicSocket()
default constructor.
Definition socket.hpp:113
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:516
State _state
socket state.
Definition socket.hpp:616
typename Protocol::Endpoint Endpoint
Definition socket.hpp:63
virtual bool encrypted() const noexcept
check if the socket is secure.
Definition socket.hpp:498
Option
socket options.
Definition socket.hpp:78
@ MulticastTtl
Definition socket.hpp:92
@ SndBuffer
Definition socket.hpp:84
@ Ttl
Definition socket.hpp:90
@ Broadcast
Definition socket.hpp:89
@ RcvError
Definition socket.hpp:94
@ ReusePort
Definition socket.hpp:88
@ MulticastLoop
Definition socket.hpp:91
@ KeepAlive
Definition socket.hpp:80
@ ReuseAddr
Definition socket.hpp:87
@ KeepCount
Definition socket.hpp:83
@ KeepIntvl
Definition socket.hpp:82
@ AuxData
Definition socket.hpp:95
@ PathMtuDiscover
Definition socket.hpp:93
@ NoDelay
Definition socket.hpp:79
@ TimeStamp
Definition socket.hpp:86
@ KeepIdle
Definition socket.hpp:81
@ RcvBuffer
Definition socket.hpp:85
virtual bool waitReadyWrite(int timeout=0) const noexcept
block until at least one byte can be written.
Definition socket.hpp:341
BasicSocket(BasicSocket &&other)
move constructor.
Definition socket.hpp:144
int handle() const noexcept
get socket native handle.
Definition socket.hpp:534
virtual int read(char *data, unsigned long maxSize) noexcept
read data.
Definition socket.hpp:305
virtual int write(const char *data, unsigned long maxSize) noexcept
write data.
Definition socket.hpp:352
BasicSocket(Mode mode)
create socket instance specifying the mode.
Definition socket.hpp:122
int wait(bool wantRead, bool wantWrite, int timeout) const noexcept
wait for the socket handle to become ready.
Definition socket.hpp:579
int protocol() const noexcept
get socket protocol.
Definition socket.hpp:525
int _handle
socket handle.
Definition socket.hpp:622
virtual int canRead() const noexcept
get the number of readable bytes.
Definition socket.hpp:275
BasicSocket(const BasicSocket &other)=delete
copy constructor.
basic stream acceptor class.
Definition protocol.hpp:52
basic stream socket class.
Definition socket.hpp:1142
virtual ~BasicStreamSocket()=default
destroy the instance.
virtual int setOption(Option option, int value) noexcept override
set the given option to the given value.
Definition socket.hpp:1398
virtual bool connecting() const noexcept
check if the socket is connecting.
Definition socket.hpp:1448
BasicStreamSocket(const BasicStreamSocket &other)=delete
copy constructor.
BasicStreamSocket(BasicStreamSocket &&other)
move constructor.
Definition socket.hpp:1184
virtual bool waitConnected(int timeout=0)
block until connected.
Definition socket.hpp:1211
int readExactly(std::string &data, unsigned long size, int timeout=0)
read data until size is reached or an error occurred.
Definition socket.hpp:1355
typename BasicDatagramSocket< Protocol >::Mode Mode
Definition socket.hpp:1145
int writeExactly(const char *data, unsigned long size, int timeout=0)
write data until size is reached or an error occurred.
Definition socket.hpp:1368
BasicStreamSocket(Mode mode)
create instance specifying the mode.
Definition socket.hpp:1162
typename Protocol::Endpoint Endpoint
Definition socket.hpp:1148
std::unique_ptr< BasicStreamSocket< Protocol > > Ptr
Definition socket.hpp:1144
BasicStreamSocket & operator=(const BasicStreamSocket &other)=delete
copy assignment operator.
typename BasicDatagramSocket< Protocol >::Option Option
Definition socket.hpp:1146
virtual bool connected() noexcept override
check if the socket is connected.
Definition socket.hpp:1457
virtual bool waitDisconnected(int timeout=0)
wait until the connection as been shut down.
Definition socket.hpp:1278
virtual int disconnect() override
shutdown the connection.
Definition socket.hpp:1236
BasicStreamSocket()
default constructor.
Definition socket.hpp:1153
int readExactly(char *data, unsigned long size, int timeout=0)
read data until size is reached or an error occurred.
Definition socket.hpp:1324
typename BasicDatagramSocket< Protocol >::State State
Definition socket.hpp:1147
basic TLS acceptor class.
Definition protocol.hpp:54
basic TLS socket class.
Definition socket.hpp:1552
BasicTlsSocket()
default constructor.
Definition socket.hpp:1563
BasicTlsSocket & operator=(const BasicTlsSocket &other)=delete
copy assignment operator.
int setCaFile(const std::string &caFile)
set the location of the trusted CA certificate file.
Definition socket.hpp:2129
virtual int canRead() const noexcept override
get the number of readable bytes.
Definition socket.hpp:1903
int startEncryption()
start socket encryption (perform TLS handshake).
Definition socket.hpp:1714
bool waitEncrypted(int timeout=0)
wait until TLS handshake is performed or timeout occur (non blocking socket).
Definition socket.hpp:1766
virtual bool encrypted() const noexcept override
check if the socket is secure.
Definition socket.hpp:2064
BasicTlsSocket(Mode mode, join::SslCtxPtr tlsContext)
Create socket instance specifying the socket mode and TLS context.
Definition socket.hpp:1620
join::SslCtxPtr _tlsContext
verify certificate revocation using CRL.
Definition socket.hpp:2508
virtual int read(char *data, unsigned long maxSize) noexcept override
read data on the socket.
Definition socket.hpp:1919
void setVerify(bool verify, int depth=-1)
Enable/Disable the verification of the peer certificate.
Definition socket.hpp:2147
join::SslPtr _tlsHandle
TLS handle.
Definition socket.hpp:2511
int verifyCallback(int preverified, X509_STORE_CTX *context) const
trusted CA certificates verification callback.
Definition socket.hpp:2346
virtual bool waitReadyRead(int timeout=0) const noexcept override
block until new data is available for reading.
Definition socket.hpp:1887
int verifyCert(X509_STORE_CTX *context) const
verify certificate validity.
Definition socket.hpp:2410
void infoCallback(int where, int ret) const
state information callback.
Definition socket.hpp:2276
std::unique_ptr< BasicTlsSocket< Protocol > > Ptr
Definition socket.hpp:1554
BasicTlsSocket(Mode mode)
create instance specifying the mode.
Definition socket.hpp:1572
TlsState
TLS state.
Definition socket.hpp:2199
@ Encrypted
Definition socket.hpp:2200
@ NonEncrypted
Definition socket.hpp:2201
TlsState _tlsState
TLS state.
Definition socket.hpp:2514
bool checkHostName(X509 *certificate) const
confirm a match between the hostname contacted and the hostnames listed in the certificate.
Definition socket.hpp:2442
virtual int disconnect() override
shutdown the connection.
Definition socket.hpp:1808
int setCertificate(const std::string &cert, const std::string &key="")
set the certificate and the private key.
Definition socket.hpp:2075
virtual bool waitReadyWrite(int timeout=0) const noexcept override
block until until at least one byte can be written on the socket.
Definition socket.hpp:1983
int setCipher(const std::string &cipher)
set the cipher list (TLSv1.2 and below).
Definition socket.hpp:2165
typename BasicStreamSocket< Protocol >::State State
Definition socket.hpp:1557
typename Protocol::Endpoint Endpoint
Definition socket.hpp:1558
virtual void close() noexcept override
close the socket handle.
Definition socket.hpp:1875
int setCipher_1_3(const std::string &cipher)
set the cipher list (TLSv1.3).
Definition socket.hpp:2182
virtual ~BasicTlsSocket()=default
destroy the instance.
int setCaPath(const std::string &caPath)
set the location of the trusted CA certificates.
Definition socket.hpp:2111
BasicTlsSocket(const BasicTlsSocket &other)=delete
copy constructor.
int startHandshake()
Start SSL handshake.
Definition socket.hpp:2208
typename BasicStreamSocket< Protocol >::Option Option
Definition socket.hpp:1556
static void infoWrapper(const SSL *ssl, int where, int ret)
c style callback wrapper for the state information callback.
Definition socket.hpp:2265
virtual int write(const char *data, unsigned long maxSize) noexcept override
write data on the socket.
Definition socket.hpp:2001
BasicTlsSocket(join::SslCtxPtr tlsContext)
create instance specifying TLS context.
Definition socket.hpp:1610
typename BasicStreamSocket< Protocol >::Mode Mode
Definition socket.hpp:1555
static int verifyWrapper(int preverified, X509_STORE_CTX *context)
c style callback wrapper for the Trusted CA certificates verification callback.
Definition socket.hpp:2331
BasicTlsSocket(BasicTlsSocket &&other)
move constructor.
Definition socket.hpp:1647
int connectEncrypted(const Endpoint &endpoint)
make an encrypted connection to the given endpoint.
Definition socket.hpp:1694
Event handler interface class.
Definition reactor.hpp:46
TLS error category.
Definition socket.hpp:1511
virtual std::string message(int code) const
translate digest error code to human readable error string.
Definition socket.cpp:44
virtual const char * name() const noexcept
get digest error category name.
Definition socket.cpp:35
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
std::unique_ptr< SSL_CTX, SslCtxDelete > SslCtxPtr
Definition openssl.hpp:240
std::unique_ptr< STACK_OF(GENERAL_NAME), StackOfGeneralNameDelete > StackOfGeneralNamePtr
Definition openssl.hpp:210
const std::string defaultCipher_1_3
Definition openssl.cpp:40
TlsErrc
TLS error codes.
Definition socket.hpp:1502
const std::error_category & getTlsCategory()
get error category.
Definition socket.cpp:61
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:150
const std::string defaultCipher
Definition openssl.cpp:36
std::unique_ptr< SSL, SslDelete > SslPtr
Definition openssl.hpp:225
std::error_condition make_error_condition(join::Errc code) noexcept
Create an std::error_condition object.
Definition error.cpp:159