25#ifndef JOIN_CORE_SOCKET_HPP
26#define JOIN_CORE_SOCKET_HPP
36#include <openssl/err.h>
43#include <netinet/tcp.h>
44#include <linux/icmp.h>
57 template <
class Protocol>
61 using Ptr = std::unique_ptr<BasicSocket<Protocol>>;
152 other._protocol = Protocol ();
164 this->
_state = other._state;
165 this->
_mode = other._mode;
172 other._protocol = Protocol ();
208 lastError = std::error_code (errno, std::generic_category ());
244 if (endpoint.protocol ().family () == AF_PACKET)
246 if (
reinterpret_cast<const struct sockaddr_ll*
> (endpoint.addr ())->sll_ifindex == 0)
248 lastError = std::make_error_code (std::errc::no_such_device);
252 else if ((endpoint.protocol ().family () == AF_INET6) || (endpoint.protocol ().family () == AF_INET))
256 else if (endpoint.protocol ().family () == AF_UNIX)
258 ::unlink (endpoint.device ().c_str ());
261 if (
::bind (this->
_handle, endpoint.addr (), endpoint.length ()) == -1)
263 lastError = std::error_code (errno, std::generic_category ());
279 if (::ioctl (this->
_handle, FIONREAD, &available) == -1)
281 lastError = std::error_code (errno, std::generic_category ());
295 return (this->
wait (
true,
false, timeout) == 0);
304 virtual int read (
char* data,
unsigned long maxSize)
noexcept
308 iov.iov_len = maxSize;
310 struct msghdr message;
311 message.msg_name =
nullptr;
312 message.msg_namelen = 0;
313 message.msg_iov = &iov;
314 message.msg_iovlen = 1;
315 message.msg_control =
nullptr;
316 message.msg_controllen = 0;
318 int size = ::recvmsg (this->
_handle, &message, 0);
323 lastError = std::error_code (errno, std::generic_category ());
342 return (this->
wait (
false,
true, timeout) == 0);
351 virtual int write (
const char* data,
unsigned long maxSize)
noexcept
354 iov.iov_base =
const_cast<char*
> (data);
355 iov.iov_len = maxSize;
357 struct msghdr message;
358 message.msg_name =
nullptr;
359 message.msg_namelen = 0;
360 message.msg_iov = &iov;
361 message.msg_iovlen = 1;
362 message.msg_control =
nullptr;
363 message.msg_controllen = 0;
365 int result = ::sendmsg (this->
_handle, &message, 0);
368 lastError = std::error_code (errno, std::generic_category ());
385 int flags = ::fcntl (this->
_handle, F_GETFL, 0);
389 flags = flags | O_NONBLOCK;
393 flags = flags & ~O_NONBLOCK;
396 ::fcntl (this->
_handle, F_SETFL, flags);
408 int optlevel, optname;
413 optlevel = SOL_SOCKET;
414 optname = SO_KEEPALIVE;
418 optlevel = SOL_SOCKET;
423 optlevel = SOL_SOCKET;
428 optlevel = SOL_SOCKET;
429 optname = SO_TIMESTAMP;
433 optlevel = SOL_SOCKET;
434 optname = SO_REUSEADDR;
438 optlevel = SOL_SOCKET;
439 optname = SO_REUSEPORT;
443 optlevel = SOL_SOCKET;
444 optname = SO_BROADCAST;
448 optlevel = SOL_PACKET;
449 optname = PACKET_AUXDATA;
457 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
460 lastError = std::error_code (errno, std::generic_category ());
473 struct sockaddr_storage sa;
474 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
476 if (::getsockname (this->
_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len) == -1)
481 return Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
545 static uint16_t
checksum (
const uint16_t* data,
size_t len, uint16_t current = 0)
547 uint32_t sum = current;
557#if __BYTE_ORDER == __LITTLE_ENDIAN
558 sum += *
reinterpret_cast<const uint8_t*
> (data);
560 sum += *
reinterpret_cast<const uint8_t*
> (data) << 8;
564 sum = (sum >> 16) + (sum & 0xffff);
567 return static_cast<uint16_t
> (~sum);
578 int wait (
bool wantRead,
bool wantWrite,
int timeout)
const noexcept
580 struct pollfd
handle = {.fd = this->
_handle, .events = 0, .revents = 0};
592 int nset = (
handle.fd > -1) ? ::poll (&
handle, 1, timeout == 0 ? -1 : timeout) : -1;
601 lastError = std::error_code (errno, std::generic_category ());
633 template <
class Protocol>
636 return a.handle () < b.handle ();
642 template <
class Protocol>
646 using Ptr = std::unique_ptr<BasicDatagramSocket<Protocol>>;
704 _remote = std::move (other._remote);
722 virtual int open (
const Protocol&
protocol = Protocol ()) noexcept
override
732 if ((
protocol.protocol () == IPPROTO_UDP) || (
protocol.protocol () == IPPROTO_TCP))
734 if ((
protocol.family () == AF_INET6) &&
735 (::setsockopt (this->_handle, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof (off)) == -1))
737 lastError = std::error_code (errno, std::generic_category ());
743 if ((
protocol.protocol () == IPPROTO_ICMPV6) || (
protocol.protocol () == IPPROTO_ICMP))
745 if ((
protocol.family () == AF_INET) &&
746 (::setsockopt (this->_handle, IPPROTO_IP, IP_HDRINCL, &off, sizeof (off)) == -1))
748 lastError = std::error_code (errno, std::generic_category ());
767 if (this->
_state == State::Closed)
773 if (this->
_state == State::Connected)
779 if ((this->
_protocol.family () == AF_INET6) || (this->_protocol.family () == AF_INET))
784 int result = setsockopt (this->
_handle, SOL_SOCKET, SO_BINDTODEVICE, device.c_str (), device.size ());
787 lastError = std::error_code (errno, std::generic_category ());
801 if ((this->
_state != State::Closed) && (this->
_state != State::Disconnected))
807 if ((this->
_state == State::Closed) && (this->
open (endpoint.protocol ()) == -1))
812 int result =
::connect (this->
_handle, endpoint.addr (), endpoint.length ());
814 this->
_state = State::Connecting;
819 lastError = std::error_code (errno, std::generic_category ());
820 if (lastError != std::errc::operation_in_progress)
827 this->
_state = State::Connected;
838 if (this->
_state == State::Connected)
840 struct sockaddr_storage nullAddr;
841 ::memset (&nullAddr, 0,
sizeof (nullAddr));
843 nullAddr.ss_family = AF_UNSPEC;
845 int result =
::connect (this->
_handle,
reinterpret_cast<struct sockaddr*
> (&nullAddr),
846 sizeof (
struct sockaddr_storage));
849 if (errno != EAFNOSUPPORT)
851 lastError = std::error_code (errno, std::generic_category ());
856 this->
_state = State::Disconnected;
866 virtual void close () noexcept
override
878 virtual int read (
char* data,
unsigned long maxSize)
noexcept override
890 virtual int readFrom (
char* data,
unsigned long maxSize,
Endpoint* endpoint =
nullptr) noexcept
892 struct sockaddr_storage sa;
893 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
895 int size = ::recvfrom (this->
_handle, data, maxSize, 0,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len);
900 lastError = std::error_code (errno, std::generic_category ());
905 this->
_state = State::Disconnected;
911 if (endpoint !=
nullptr)
913 *endpoint =
Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
925 virtual int write (
const char* data,
unsigned long maxSize)
noexcept override
937 virtual int writeTo (
const char* data,
unsigned long maxSize,
const Endpoint& endpoint)
noexcept
939 if ((this->
_state == State::Closed) && (this->
open (endpoint.protocol ()) == -1))
944 int result = ::sendto (this->
_handle, data, maxSize, 0, endpoint.addr (), endpoint.length ());
947 lastError = std::error_code (errno, std::generic_category ());
962 if (this->
_state == State::Closed)
968 int optlevel, optname;
973 if (this->
family () == AF_INET6)
975 optlevel = IPPROTO_IPV6;
976 optname = IPV6_UNICAST_HOPS;
980 optlevel = IPPROTO_IP;
985 case Option::MulticastLoop:
986 if (this->
family () == AF_INET6)
988 optlevel = IPPROTO_IPV6;
989 optname = IPV6_MULTICAST_LOOP;
993 optlevel = IPPROTO_IP;
994 optname = IP_MULTICAST_LOOP;
998 case Option::MulticastTtl:
999 if (this->
family () == AF_INET6)
1001 optlevel = IPPROTO_IPV6;
1002 optname = IPV6_MULTICAST_HOPS;
1006 optlevel = IPPROTO_IP;
1007 optname = IP_MULTICAST_TTL;
1011 case Option::PathMtuDiscover:
1012 if (this->
family () == AF_INET6)
1014 optlevel = IPPROTO_IPV6;
1015 optname = IPV6_MTU_DISCOVER;
1019 optlevel = IPPROTO_IP;
1020 optname = IP_MTU_DISCOVER;
1024 case Option::RcvError:
1025 if (this->
family () == AF_INET6)
1027 optlevel = IPPROTO_IPV6;
1028 optname = IPV6_RECVERR;
1032 optlevel = IPPROTO_IP;
1033 optname = IP_RECVERR;
1041 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
1044 lastError = std::error_code (errno, std::generic_category ());
1066 return (this->
_state == State::Connected);
1075 if (this->
_state == State::Closed)
1081 int result = -1, value = -1;
1082 socklen_t valueLen =
sizeof (value);
1084 if (this->
_protocol.family () == AF_INET6)
1086 result = ::getsockopt (this->
_handle, IPPROTO_IPV6, IPV6_MTU, &value, &valueLen);
1088 else if (this->
_protocol.family () == AF_INET)
1090 result = ::getsockopt (this->
_handle, IPPROTO_IP, IP_MTU, &value, &valueLen);
1100 lastError = std::error_code (errno, std::generic_category ());
1130 template <
class Protocol>
1133 return a.handle () < b.handle ();
1139 template <
class Protocol>
1143 using Ptr = std::unique_ptr<BasicStreamSocket<Protocol>>;
1212 if (this->
_state != State::Connected)
1214 if (this->
_state != State::Connecting)
1237 if (this->
_state == State::Connected)
1239 ::shutdown (this->
_handle, SHUT_WR);
1240 this->
_state = State::Disconnecting;
1243 if (this->
_state == State::Disconnecting)
1251 int result = this->
read (buffer,
sizeof (buffer));
1263 ::shutdown (this->
_handle, SHUT_RD);
1264 this->
_state = State::Disconnected;
1279 if ((this->
_state != State::Disconnected) && (this->
_state != State::Closed))
1281 if (this->
_state != State::Disconnecting)
1287 auto start = std::chrono::steady_clock::now ();
1304 elapsed = std::chrono::duration_cast<std::chrono::milliseconds> (
1305 std::chrono::steady_clock::now () - start)
1325 unsigned long numRead = 0;
1327 while (numRead < size)
1329 int result = this->
read (data + numRead, size - numRead);
1354 int readExactly (std::string& data,
unsigned long size,
int timeout = 0)
1369 unsigned long numWrite = 0;
1371 while (numWrite < size)
1373 int result = this->
write (data + numWrite, size - numWrite);
1399 if (this->
_state == State::Closed)
1405 int optlevel, optname;
1409 case Option::NoDelay:
1410 optlevel = IPPROTO_TCP;
1411 optname = TCP_NODELAY;
1414 case Option::KeepIdle:
1415 optlevel = IPPROTO_TCP;
1416 optname = TCP_KEEPIDLE;
1419 case Option::KeepIntvl:
1420 optlevel = IPPROTO_TCP;
1421 optname = TCP_KEEPINTVL;
1424 case Option::KeepCount:
1425 optlevel = IPPROTO_TCP;
1426 optname = TCP_KEEPCNT;
1433 int result = ::setsockopt (this->
_handle, optlevel, optname, &value,
sizeof (value));
1436 lastError = std::error_code (errno, std::generic_category ());
1449 return (this->
_state == State::Connecting);
1458 if (this->
_state == State::Connected)
1462 else if (this->
_state != State::Connecting)
1468 socklen_t optlen =
sizeof (optval);
1470 int result = ::getsockopt (this->
_handle, SOL_SOCKET, SO_ERROR, &optval, &optlen);
1471 if ((result == -1) || (optval != 0))
1476 this->
_state = State::Connected;
1491 template <
class Protocol>
1494 return a.handle () < b.handle ();
1516 virtual const char*
name ()
const noexcept;
1523 virtual std::string
message (
int code)
const;
1549 template <
class Protocol>
1553 using Ptr = std::unique_ptr<BasicTlsSocket<Protocol>>;
1573 ,
_tlsContext (SSL_CTX_new (TLS_client_method ()))
1576 SSL_CTX_set_options (this->
_tlsContext.get (), SSL_OP_ALL);
1579 SSL_CTX_set_options (this->
_tlsContext.get (), SSL_OP_NO_COMPRESSION);
1583 SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
1587 SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1590 SSL_CTX_set_mode (this->
_tlsContext.get (), SSL_MODE_AUTO_RETRY);
1593 SSL_CTX_set_session_cache_mode (this->
_tlsContext.get (), SSL_SESS_CACHE_CLIENT);
1596 SSL_CTX_set_verify (this->
_tlsContext.get (), SSL_VERIFY_NONE,
nullptr);
1625 throw std::invalid_argument (
"OpenSSL context is invalid");
1654 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1669 this->
_tlsContext = std::move (other._tlsContext);
1670 this->
_tlsHandle = std::move (other._tlsHandle);
1675 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1695 if (this->
connect (endpoint) == -1)
1727 if (SSL_set_fd (this->
_tlsHandle.get (), this->_handle) != 1)
1734 if (SSL_is_server (this->
_tlsHandle.get ()) == 0)
1736 if (!this->
_remote.hostname ().empty () &&
1737 (SSL_set_tlsext_host_name (this->
_tlsHandle.get (), this->_remote.hostname ().c_str ()) != 1))
1744 SSL_set_connect_state (this->
_tlsHandle.get ());
1748 SSL_set_accept_state (this->
_tlsHandle.get ());
1751 SSL_set_app_data (this->
_tlsHandle.get (),
this);
1772 if (this->
_state == State::Connecting)
1815 if ((SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN) ==
false)
1818 int result = SSL_shutdown (this->
_tlsHandle.get ());
1822 switch (SSL_get_error (this->
_tlsHandle.get (), result))
1824 case SSL_ERROR_WANT_READ:
1825 case SSL_ERROR_WANT_WRITE:
1829 case SSL_ERROR_SYSCALL:
1838 this->
_state = State::Disconnected;
1841 lastError = std::error_code (errno, std::generic_category ());
1848 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
1856 else if (result == 1)
1909 return SSL_pending (this->
_tlsHandle.get ());
1921 virtual int read (
char* data,
unsigned long maxSize)
noexcept override
1926 int result = SSL_read (this->
_tlsHandle.get (), data, int (maxSize));
1929 switch (SSL_get_error (this->
_tlsHandle.get (), result))
1931 case SSL_ERROR_WANT_READ:
1932 case SSL_ERROR_WANT_WRITE:
1933 case SSL_ERROR_WANT_X509_LOOKUP:
1937 case SSL_ERROR_ZERO_RETURN:
1941 if (SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN)
1946 case SSL_ERROR_SYSCALL:
1955 this->
_state = State::Disconnected;
1958 lastError = std::error_code (errno, std::generic_category ());
1965 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2003 virtual int write (
const char* data,
unsigned long maxSize)
noexcept override
2008 int result = SSL_write (this->
_tlsHandle.get (), data, int (maxSize));
2011 switch (SSL_get_error (this->
_tlsHandle.get (), result))
2013 case SSL_ERROR_WANT_READ:
2014 case SSL_ERROR_WANT_WRITE:
2015 case SSL_ERROR_WANT_X509_LOOKUP:
2019 case SSL_ERROR_ZERO_RETURN:
2023 if (SSL_get_shutdown (this->
_tlsHandle.get ()) & SSL_SENT_SHUTDOWN)
2028 case SSL_ERROR_SYSCALL:
2037 this->
_state = State::Disconnected;
2040 lastError = std::error_code (errno, std::generic_category ());
2047 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2080 ? SSL_use_certificate_file (this->
_tlsHandle.get (), cert.c_str (), SSL_FILETYPE_PEM)
2081 : SSL_CTX_use_certificate_file (this->
_tlsContext.get (), cert.c_str (), SSL_FILETYPE_PEM)) == 0)
2090 ? SSL_use_PrivateKey_file (this->
_tlsHandle.get (),
key.c_str (), SSL_FILETYPE_PEM)
2091 : SSL_CTX_use_PrivateKey_file (this->
_tlsContext.get (),
key.c_str (), SSL_FILETYPE_PEM)) == 0)
2099 : SSL_CTX_check_private_key (this->
_tlsContext.get ())) == 0)
2116 if (stat (caPath.c_str (), &st) != 0 || !S_ISDIR (st.st_mode) ||
2117 SSL_CTX_load_verify_locations (this->
_tlsContext.get (),
nullptr, caPath.c_str ()) == 0)
2134 if (stat (caFile.c_str (), &st) != 0 || !S_ISREG (st.st_mode) ||
2135 SSL_CTX_load_verify_locations (this->
_tlsContext.get (), caFile.c_str (),
nullptr) == 0)
2154 SSL_CTX_set_verify_depth (this->
_tlsContext.get (), depth);
2158 SSL_CTX_set_verify (this->
_tlsContext.get (), SSL_VERIFY_NONE,
nullptr);
2170 : SSL_CTX_set_cipher_list (this->
_tlsContext.get (), cipher.c_str ())) == 0)
2187 : SSL_CTX_set_ciphersuites (this->
_tlsContext.get (), cipher.c_str ())) == 0)
2203 std::vector<uint8_t> wire;
2206 for (
auto const& proto : protocols)
2208 wire.push_back (
static_cast<uint8_t
> (proto.size ()));
2209 wire.insert (wire.end (), proto.begin (), proto.end ());
2212 if (SSL_CTX_set_alpn_protos (this->
_tlsContext.get (), wire.data (),
2213 static_cast<unsigned int> (wire.size ())) != 0)
2239 int result = SSL_do_handshake (this->
_tlsHandle.get ());
2242 switch (SSL_get_error (this->
_tlsHandle.get (), result))
2244 case SSL_ERROR_WANT_READ:
2245 case SSL_ERROR_WANT_WRITE:
2246 case SSL_ERROR_WANT_X509_LOOKUP:
2250 case SSL_ERROR_ZERO_RETURN:
2255 case SSL_ERROR_SYSCALL:
2263 this->
_state = State::Disconnected;
2266 lastError = std::error_code (errno, std::generic_category ());
2273 std::cout << ERR_reason_error_string (ERR_get_error ()) << std::endl;
2306 if (where & SSL_CB_ALERT)
2308 std::cout <<
"SSL/TLS Alert ";
2309 (where & SSL_CB_READ) ? std::cout <<
"[read] " : std::cout <<
"[write] ";
2310 std::cout << SSL_alert_type_string_long (ret) <<
":";
2311 std::cout << SSL_alert_desc_string_long (ret);
2312 std::cout << std::endl;
2314 else if (where & SSL_CB_LOOP)
2316 std::cout <<
"SSL/TLS State ";
2317 (SSL_in_connect_init (this->
_tlsHandle.get ())) ? std::cout <<
"[connect] "
2318 : (SSL_in_accept_init (this->
_tlsHandle.get ())) ? std::cout <<
"[accept] "
2319 : std::cout <<
"[undefined] ";
2320 std::cout << SSL_state_string_long (this->
_tlsHandle.get ());
2321 std::cout << std::endl;
2323 else if (where & SSL_CB_HANDSHAKE_START)
2325 std::cout <<
"SSL/TLS Handshake [Start] " << SSL_state_string_long (this->
_tlsHandle.get ())
2328 else if (where & SSL_CB_HANDSHAKE_DONE)
2330 std::cout <<
"SSL/TLS Handshake [Done] " << SSL_state_string_long (this->
_tlsHandle.get ())
2332 std::cout << SSL_CTX_sess_number (this->
_tlsContext.get ()) <<
" items in the session cache"
2334 std::cout << SSL_CTX_sess_connect (this->
_tlsContext.get ()) <<
" client connects" << std::endl;
2335 std::cout << SSL_CTX_sess_connect_good (this->
_tlsContext.get ()) <<
" client connects that finished"
2337 std::cout << SSL_CTX_sess_connect_renegotiate (this->
_tlsContext.get ())
2338 <<
" client renegotiations requested" << std::endl;
2339 std::cout << SSL_CTX_sess_accept (this->
_tlsContext.get ()) <<
" server connects" << std::endl;
2340 std::cout << SSL_CTX_sess_accept_good (this->
_tlsContext.get ()) <<
" server connects that finished"
2342 std::cout << SSL_CTX_sess_accept_renegotiate (this->
_tlsContext.get ())
2343 <<
" server renegotiations requested" << std::endl;
2344 std::cout << SSL_CTX_sess_hits (this->
_tlsContext.get ()) <<
" session cache hits" << std::endl;
2345 std::cout << SSL_CTX_sess_cb_hits (this->
_tlsContext.get ()) <<
" external session cache hits"
2347 std::cout << SSL_CTX_sess_misses (this->
_tlsContext.get ()) <<
" session cache misses" << std::endl;
2348 std::cout << SSL_CTX_sess_timeouts (this->
_tlsContext.get ()) <<
" session cache timeouts" << std::endl;
2349 std::cout <<
"negotiated " << SSL_get_cipher (this->
_tlsHandle.get ()) <<
" cipher suite" << std::endl;
2361 SSL* ssl =
static_cast<SSL*
> (X509_STORE_CTX_get_ex_data (context, SSL_get_ex_data_X509_STORE_CTX_idx ()));
2376 int maxDepth = SSL_get_verify_depth (this->
_tlsHandle.get ());
2377 int dpth = X509_STORE_CTX_get_error_depth (context);
2380 std::cout <<
"verification started at depth=" << dpth << std::endl;
2384 if ((maxDepth >= 0) && (dpth > maxDepth))
2387 X509_STORE_CTX_set_error (context, X509_V_ERR_CERT_CHAIN_TOO_LONG);
2393 std::cout <<
"verification failed at depth=" << dpth <<
" - "
2394 << X509_verify_cert_error_string (X509_STORE_CTX_get_error (context)) << std::endl;
2403 std::cout <<
"rejected by CERT at depth=" << dpth << std::endl;
2427 std::cout <<
"certificate accepted at depth=" << dpth << std::endl;
2440 int depth = X509_STORE_CTX_get_error_depth (context);
2441 X509* cert = X509_STORE_CTX_get_current_cert (context);
2444 X509_NAME_oneline (X509_get_subject_name (cert), buf,
sizeof (buf));
2446 std::cout <<
"subject=" << buf << std::endl;
2456 std::cout <<
"no match for hostname in the certificate" << std::endl;
2476 X509_get_ext_d2i (certificate, NID_subject_alt_name, 0, 0)));
2479 for (
int i = 0; (i < sk_GENERAL_NAME_num (altnames.get ())) && !match; ++i)
2482 GENERAL_NAME* current_name = sk_GENERAL_NAME_value (altnames.get (), i);
2484 if (current_name->type == GEN_DNS)
2487 const char* host =
reinterpret_cast<const char*
> (ASN1_STRING_get0_data (current_name->d.ia5));
2488 size_t len = size_t (ASN1_STRING_length (current_name->d.ia5));
2489 std::string pattern (host, host + len), serverName (this->
_remote.hostname ());
2492 if (pattern.back () ==
'.')
2494 pattern.pop_back ();
2497 if (serverName.back () ==
'.')
2499 serverName.pop_back ();
2503 if (fnmatch (pattern.c_str (), serverName.c_str (), 0) == 0)
2554 template <
class Protocol>
2557 return a.handle () < b.handle ();
2565 struct is_error_condition_enum<
join::TlsErrc> :
public true_type
basic datagram socket class.
Definition socket.hpp:644
virtual ~BasicDatagramSocket()=default
Destroy the instance.
BasicDatagramSocket(const BasicDatagramSocket &other)=delete
Copy constructor.
typename BasicSocket< Protocol >::Option Option
Definition socket.hpp:648
BasicDatagramSocket(Mode mode, int ttl=60)
Create instance specifying the mode.
Definition socket.hpp:664
const Endpoint & remoteEndpoint() const noexcept
determine the remote endpoint associated with this socket.
Definition socket.hpp:1055
virtual int connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socket.hpp:799
virtual int bindToDevice(const std::string &device) noexcept
assigns the specified device to the socket.
Definition socket.hpp:765
std::unique_ptr< BasicDatagramSocket< Protocol > > Ptr
Definition socket.hpp:646
virtual bool connected() noexcept
check if the socket is connected.
Definition socket.hpp:1064
virtual int setOption(Option option, int value) noexcept override
set the given option to the given value.
Definition socket.hpp:960
Endpoint _remote
remote endpoint.
Definition socket.hpp:1118
virtual int read(char *data, unsigned long maxSize) noexcept override
read data.
Definition socket.hpp:878
int _ttl
packet time to live.
Definition socket.hpp:1121
virtual int write(const char *data, unsigned long maxSize) noexcept override
write data.
Definition socket.hpp:925
virtual void close() noexcept override
close the socket handle.
Definition socket.hpp:866
typename Protocol::Endpoint Endpoint
Definition socket.hpp:650
BasicDatagramSocket(int ttl=60)
Default constructor.
Definition socket.hpp:655
typename BasicSocket< Protocol >::State State
Definition socket.hpp:649
int mtu() const
get socket mtu.
Definition socket.hpp:1073
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:890
virtual int writeTo(const char *data, unsigned long maxSize, const Endpoint &endpoint) noexcept
write data on the socket.
Definition socket.hpp:937
virtual int disconnect()
shutdown the connection.
Definition socket.hpp:836
typename BasicSocket< Protocol >::Mode Mode
Definition socket.hpp:647
virtual int open(const Protocol &protocol=Protocol()) noexcept override
open socket using the given protocol.
Definition socket.hpp:722
BasicDatagramSocket(BasicDatagramSocket &&other)
Move constructor.
Definition socket.hpp:687
int ttl() const noexcept
returns the Time-To-Live value.
Definition socket.hpp:1111
basic socket class.
Definition socket.hpp:59
bool opened() const noexcept
check if the socket is opened.
Definition socket.hpp:488
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:193
static uint16_t checksum(const uint16_t *data, size_t len, uint16_t current=0)
get standard 1s complement checksum.
Definition socket.hpp:545
void setMode(Mode mode) noexcept
set the socket to the non-blocking or blocking mode.
Definition socket.hpp:379
State
socket states.
Definition socket.hpp:101
@ Disconnected
Definition socket.hpp:105
@ Connecting
Definition socket.hpp:102
@ Disconnecting
Definition socket.hpp:104
@ Closed
Definition socket.hpp:106
@ Connected
Definition socket.hpp:103
Protocol _protocol
protocol.
Definition socket.hpp:624
virtual int setOption(Option option, int value) noexcept
set the given option to the given value.
Definition socket.hpp:406
Mode _mode
socket mode.
Definition socket.hpp:618
virtual void close() noexcept
close the socket.
Definition socket.hpp:222
int family() const noexcept
get socket address family.
Definition socket.hpp:506
Mode
socket modes.
Definition socket.hpp:68
@ Blocking
Definition socket.hpp:69
@ NonBlocking
Definition socket.hpp:70
virtual bool waitReadyRead(int timeout=0) const noexcept
block until new data is available for reading.
Definition socket.hpp:293
std::unique_ptr< BasicSocket< Protocol > > Ptr
Definition socket.hpp:61
virtual ~BasicSocket()
destroy the socket instance.
Definition socket.hpp:180
virtual int bind(const Endpoint &endpoint) noexcept
assigns the specified endpoint to the socket.
Definition socket.hpp:237
BasicSocket()
default constructor.
Definition socket.hpp:112
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:515
State _state
socket state.
Definition socket.hpp:615
typename Protocol::Endpoint Endpoint
Definition socket.hpp:62
virtual bool encrypted() const noexcept
check if the socket is secure.
Definition socket.hpp:497
Option
socket options.
Definition socket.hpp:77
@ MulticastTtl
Definition socket.hpp:91
@ SndBuffer
Definition socket.hpp:83
@ Ttl
Definition socket.hpp:89
@ Broadcast
Definition socket.hpp:88
@ RcvError
Definition socket.hpp:93
@ ReusePort
Definition socket.hpp:87
@ MulticastLoop
Definition socket.hpp:90
@ KeepAlive
Definition socket.hpp:79
@ ReuseAddr
Definition socket.hpp:86
@ KeepCount
Definition socket.hpp:82
@ KeepIntvl
Definition socket.hpp:81
@ AuxData
Definition socket.hpp:94
@ PathMtuDiscover
Definition socket.hpp:92
@ NoDelay
Definition socket.hpp:78
@ TimeStamp
Definition socket.hpp:85
@ KeepIdle
Definition socket.hpp:80
@ RcvBuffer
Definition socket.hpp:84
Endpoint localEndpoint() const noexcept
determine the local endpoint associated with this socket.
Definition socket.hpp:471
virtual bool waitReadyWrite(int timeout=0) const noexcept
block until at least one byte can be written.
Definition socket.hpp:340
BasicSocket(BasicSocket &&other)
move constructor.
Definition socket.hpp:143
int handle() const noexcept
get socket native handle.
Definition socket.hpp:533
virtual int read(char *data, unsigned long maxSize) noexcept
read data.
Definition socket.hpp:304
virtual int write(const char *data, unsigned long maxSize) noexcept
write data.
Definition socket.hpp:351
BasicSocket(Mode mode)
create socket instance specifying the mode.
Definition socket.hpp:121
int wait(bool wantRead, bool wantWrite, int timeout) const noexcept
wait for the socket handle to become ready.
Definition socket.hpp:578
int protocol() const noexcept
get socket protocol.
Definition socket.hpp:524
int _handle
socket handle.
Definition socket.hpp:621
virtual int canRead() const noexcept
get the number of readable bytes.
Definition socket.hpp:274
BasicSocket(const BasicSocket &other)=delete
copy constructor.
basic stream acceptor class.
Definition protocol.hpp:52
basic stream socket class.
Definition socket.hpp:1141
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:1397
virtual bool connecting() const noexcept
check if the socket is connecting.
Definition socket.hpp:1447
BasicStreamSocket(const BasicStreamSocket &other)=delete
copy constructor.
BasicStreamSocket(BasicStreamSocket &&other)
move constructor.
Definition socket.hpp:1183
virtual bool waitConnected(int timeout=0)
block until connected.
Definition socket.hpp:1210
int readExactly(std::string &data, unsigned long size, int timeout=0)
read data until size is reached or an error occurred.
Definition socket.hpp:1354
typename BasicDatagramSocket< Protocol >::Mode Mode
Definition socket.hpp:1144
int writeExactly(const char *data, unsigned long size, int timeout=0)
write data until size is reached or an error occurred.
Definition socket.hpp:1367
BasicStreamSocket(Mode mode)
create instance specifying the mode.
Definition socket.hpp:1161
typename Protocol::Endpoint Endpoint
Definition socket.hpp:1147
std::unique_ptr< BasicStreamSocket< Protocol > > Ptr
Definition socket.hpp:1143
BasicStreamSocket & operator=(const BasicStreamSocket &other)=delete
copy assignment operator.
typename BasicDatagramSocket< Protocol >::Option Option
Definition socket.hpp:1145
virtual bool connected() noexcept override
check if the socket is connected.
Definition socket.hpp:1456
virtual bool waitDisconnected(int timeout=0)
wait until the connection as been shut down.
Definition socket.hpp:1277
virtual int disconnect() override
shutdown the connection.
Definition socket.hpp:1235
BasicStreamSocket()
default constructor.
Definition socket.hpp:1152
int readExactly(char *data, unsigned long size, int timeout=0)
read data until size is reached or an error occurred.
Definition socket.hpp:1323
typename BasicDatagramSocket< Protocol >::State State
Definition socket.hpp:1146
basic TLS acceptor class.
Definition protocol.hpp:54
basic TLS socket class.
Definition socket.hpp:1551
BasicTlsSocket()
default constructor.
Definition socket.hpp:1562
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:2131
virtual int canRead() const noexcept override
get the number of readable bytes.
Definition socket.hpp:1905
int startEncryption()
start socket encryption (perform TLS handshake).
Definition socket.hpp:1716
virtual bool encrypted() const noexcept override
check if the socket is secure.
Definition socket.hpp:2066
BasicTlsSocket(Mode mode, join::SslCtxPtr tlsContext)
Create socket instance specifying the socket mode and TLS context.
Definition socket.hpp:1619
join::SslCtxPtr _tlsContext
verify certificate revocation using CRL.
Definition socket.hpp:2536
virtual int read(char *data, unsigned long maxSize) noexcept override
read data on the socket.
Definition socket.hpp:1921
join::SslPtr _tlsHandle
TLS handle.
Definition socket.hpp:2539
virtual bool waitEncrypted(int timeout=0)
wait until TLS handshake is performed or timeout occur (non blocking socket).
Definition socket.hpp:1768
int verifyCallback(int preverified, X509_STORE_CTX *context) const
trusted CA certificates verification callback.
Definition socket.hpp:2374
virtual bool waitReadyRead(int timeout=0) const noexcept override
block until new data is available for reading.
Definition socket.hpp:1889
int verifyCert(X509_STORE_CTX *context) const
verify certificate validity.
Definition socket.hpp:2438
void infoCallback(int where, int ret) const
state information callback.
Definition socket.hpp:2304
int setAlpnProtocols(const std::vector< std::string > &protocols)
set the ALPN protocols list.
Definition socket.hpp:2201
std::unique_ptr< BasicTlsSocket< Protocol > > Ptr
Definition socket.hpp:1553
BasicTlsSocket(Mode mode)
create instance specifying the mode.
Definition socket.hpp:1571
TlsState
TLS state.
Definition socket.hpp:2227
@ Encrypted
Definition socket.hpp:2228
@ NonEncrypted
Definition socket.hpp:2229
TlsState _tlsState
TLS state.
Definition socket.hpp:2542
bool checkHostName(X509 *certificate) const
confirm a match between the hostname contacted and the hostnames listed in the certificate.
Definition socket.hpp:2470
virtual int disconnect() override
shutdown the connection.
Definition socket.hpp:1810
int setCertificate(const std::string &cert, const std::string &key="")
set the certificate and the private key.
Definition socket.hpp:2077
void setVerify(bool verify, int depth=-1) noexcept
Enable/Disable the verification of the peer certificate.
Definition socket.hpp:2149
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:1985
int setCipher(const std::string &cipher)
set the cipher list (TLSv1.2 and below).
Definition socket.hpp:2167
typename BasicStreamSocket< Protocol >::State State
Definition socket.hpp:1556
typename Protocol::Endpoint Endpoint
Definition socket.hpp:1557
virtual void close() noexcept override
close the socket handle.
Definition socket.hpp:1877
int setCipher_1_3(const std::string &cipher)
set the cipher list (TLSv1.3).
Definition socket.hpp:2184
virtual ~BasicTlsSocket()=default
destroy the instance.
int setCaPath(const std::string &caPath)
set the location of the trusted CA certificates.
Definition socket.hpp:2113
BasicTlsSocket(const BasicTlsSocket &other)=delete
copy constructor.
int startHandshake()
Start SSL handshake.
Definition socket.hpp:2236
virtual int connectEncrypted(const Endpoint &endpoint)
make an encrypted connection to the given endpoint.
Definition socket.hpp:1693
typename BasicStreamSocket< Protocol >::Option Option
Definition socket.hpp:1555
static void infoWrapper(const SSL *ssl, int where, int ret)
c style callback wrapper for the state information callback.
Definition socket.hpp:2293
virtual int write(const char *data, unsigned long maxSize) noexcept override
write data on the socket.
Definition socket.hpp:2003
BasicTlsSocket(join::SslCtxPtr tlsContext)
create instance specifying TLS context.
Definition socket.hpp:1609
typename BasicStreamSocket< Protocol >::Mode Mode
Definition socket.hpp:1554
static int verifyWrapper(int preverified, X509_STORE_CTX *context)
c style callback wrapper for the Trusted CA certificates verification callback.
Definition socket.hpp:2359
BasicTlsSocket(BasicTlsSocket &&other)
move constructor.
Definition socket.hpp:1646
TLS error category.
Definition socket.hpp:1510
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:1501
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