52static char* _n_strndup(
const char* s,
size_t n) {
53 size_t len = strlen(s);
55 char* p = (
char*)malloc(len + 1);
62#define strndup _n_strndup
66#include <openssl/sha.h>
67#include <openssl/rand.h>
68#include <openssl/x509v3.h>
75#define _Thread_local __thread
87 vsnprintf(
netw->netw_errors[
netw->netw_err_next], 512, fmt, ap);
89 netw->netw_err_next = (
netw->netw_err_next + 1) % 8;
90 if (
netw->netw_err_count < 8)
netw->netw_err_count++;
107 return netw ?
netw->netw_err_count : 0;
111 if (!
netw || index < 0 || index >=
netw->netw_err_count)
return NULL;
112 int oldest = (
netw->netw_err_next -
netw->netw_err_count + 8) % 8;
113 return netw->netw_errors[(oldest + index) % 8];
118 netw->netw_err_count = 0;
119 netw->netw_err_next = 0;
146#if __BYTE_ORDER == __LITTLE_ENDIAN
147 if (
sizeof(
size_t) == 4) {
148 return (
size_t)htonl((uint32_t)value);
149 }
else if (
sizeof(
size_t) == 8) {
150 return ((
size_t)htonl((uint32_t)(value >> 32)) |
151 ((
size_t)htonl((uint32_t)value) << 32));
163#if __BYTE_ORDER == __LITTLE_ENDIAN
164 if (
sizeof(
size_t) == 4) {
165 return (
size_t)ntohl((uint32_t)value);
166 }
else if (
sizeof(
size_t) == 8) {
167 return ((
size_t)ntohl((uint32_t)(value >> 32)) |
168 ((
size_t)ntohl((uint32_t)value) << 32));
181char* wchar_to_char(
const wchar_t* pwchar) {
185 int currentCharIndex = 0;
186 char currentChar = (char)pwchar[currentCharIndex];
187 char* filePathC = NULL;
189 while (currentChar !=
'\0') {
191 currentChar = (char)pwchar[currentCharIndex];
194 const int charCount = currentCharIndex + 1;
197 Malloc(filePathC,
char, (
size_t)charCount);
200 for (
int i = 0; i < charCount; i++) {
202 char character = (char)pwchar[i];
204 *filePathC = character;
206 filePathC +=
sizeof(char);
210 filePathC -= (
sizeof(char) * (size_t)charCount);
216#define NETW_CALL_RETRY(__retvar, __expression, __max_tries) \
218 int __nb_retries = 0; \
220 __retvar = (__expression); \
222 } while (__retvar == -1 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) && __nb_retries < (__max_tries)); \
223 if (__retvar == -1 && __nb_retries >= (__max_tries)) __retvar = -2; \
227#define neterrno WSAGetLastError()
234 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
236 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
237 (LPWSTR)&ws, 0, NULL);
238 char* netstr = wchar_to_char(ws);
243#if __GNUC__ <= 6 && __GNUC_MINOR__ <= 3
279size_t strlcpy(
char* dst,
const char* src,
size_t siz) {
287 if ((*d++ = *s++) ==
'\0')
299 return (s - src - 1);
324static char* inet_ntop4(
const unsigned char* src,
char* dst, socklen_t size);
325static char* inet_ntop6(
const unsigned char* src,
char* dst, socklen_t size);
335char* inet_ntop(
int af,
const void* src,
char* dst, socklen_t size) {
338 return (inet_ntop4((
const unsigned char*)src, dst, size));
340 return (inet_ntop6((
const unsigned char*)src, dst, size));
358static char* inet_ntop4(
const unsigned char* src,
char* dst, socklen_t size) {
359 static const char fmt[] =
"%u.%u.%u.%u";
360 char tmp[
sizeof "255.255.255.255"];
363 l = snprintf(tmp,
sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
364 if (l <= 0 || (socklen_t)l >= size) {
367 strlcpy(dst, tmp, size);
377static char* inet_ntop6(
const unsigned char* src,
char* dst, socklen_t size) {
385 char tmp[
sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
390#define NS_IN6ADDRSZ 16
392 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
400 memset(words,
'\0',
sizeof words);
401 for (i = 0; i < NS_IN6ADDRSZ; i++)
402 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
407 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
410 cur.base = i, cur.len = 1;
414 if (cur.base != -1) {
415 if (best.base == -1 || cur.len > best.len)
421 if (cur.base != -1) {
422 if (best.base == -1 || cur.len > best.len)
425 if (best.base != -1 && best.len < 2)
432 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
434 if (best.base != -1 && i >= best.base &&
435 i < (best.base + best.len)) {
444 if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 7 && words[7] != 0x0001) || (best.len == 5 && words[5] == 0xffff))) {
445 if (!inet_ntop4(src + 12, tp,
sizeof tmp - (tp - tmp)))
450 tp += sprintf(tp,
"%x", words[i]);
453 if (best.base != -1 && (best.base + best.len) ==
454 (NS_IN6ADDRSZ / NS_INT16SZ))
461 if ((socklen_t)(tp - tmp) > size) {
490static int inet_pton4(
const char* src, u_char* dst);
491static int inet_pton6(
const char* src, u_char* dst);
500int inet_pton(
int af,
const char* src,
void* dst) {
503 return (inet_pton4(src, (
unsigned char*)dst));
505 return (inet_pton6(src, (
unsigned char*)dst));
522static int inet_pton4(
const char* src, u_char* dst) {
523 static const char digits[] =
"0123456789";
524 int saw_digit, octets, ch;
530 u_char tmp[NS_INADDRSZ] = {0}, *tp;
535 while ((ch = *src++) !=
'\0') {
538 if ((pch = strchr(digits, ch)) != NULL) {
539 u_int uiNew = *tp * 10 + (pch - digits);
541 if (saw_digit && *tp == 0)
551 }
else if (ch ==
'.' && saw_digit) {
561 memcpy(dst, tmp, NS_INADDRSZ);
578static int inet_pton6(
const char* src, u_char* dst) {
579 static const char xdigits_l[] =
"0123456789abcdef",
580 xdigits_u[] =
"0123456789ABCDEF";
581#define NS_IN6ADDRSZ 16
583 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
585 int ch, seen_xdigits;
588 memset((tp = tmp),
'\0', NS_IN6ADDRSZ);
589 endp = tp + NS_IN6ADDRSZ;
598 while ((ch = *src++) !=
'\0') {
599 const char* xdigits :
const char* pch;
601 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
602 pch = strchr((xdigits = xdigits_u), ch);
605 val |= (pch - xdigits);
606 if (++seen_xdigits > 4)
617 }
else if (*src ==
'\0') {
620 if (tp + NS_INT16SZ > endp)
622 *tp++ = (u_char)(val >> 8) & 0xff;
623 *tp++ = (u_char)val & 0xff;
628 if (ch ==
'.' && ((tp + NS_INADDRSZ) <= endp) &&
629 inet_pton4(curtok, tp) > 0) {
637 if (tp + NS_INT16SZ > endp)
639 *tp++ = (u_char)(val >> 8) & 0xff;
640 *tp++ = (u_char)val & 0xff;
642 if (colonp != NULL) {
647 const int n = tp - colonp;
652 for (i = 1; i <= n; i++) {
653 endp[-i] = colonp[n - i];
660 memcpy(dst, tmp, NS_IN6ADDRSZ);
668#include <sys/types.h>
672#define NETW_CALL_RETRY(__retvar, __expression, __max_tries) \
674 int __nb_retries = 0; \
676 __retvar = (__expression); \
678 } while (__retvar == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) && __nb_retries < (__max_tries)); \
679 if (__retvar == -1 && __nb_retries >= (__max_tries)) __retvar = -2; \
683#define neterrno errno
703 return strdup(strerror(code));
721 netw->nb_pending = -1;
724 netw->nb_running_threads = 0;
733 netw->link.sock = INVALID_SOCKET;
735 netw->link.ip = NULL;
736 memset(&
netw->link.hints, 0,
sizeof(
struct addrinfo));
737 memset(&
netw->link.raddr, 0,
sizeof(
struct sockaddr_storage));
740 if (pthread_mutex_init(&
netw->sendbolt, NULL) != 0) {
746 if (pthread_mutex_init(&
netw->recvbolt, NULL) != 0) {
748 pthread_mutex_destroy(&
netw->sendbolt);
753 if (pthread_mutex_init(&
netw->eventbolt, NULL) != 0) {
755 pthread_mutex_destroy(&
netw->sendbolt);
756 pthread_mutex_destroy(&
netw->recvbolt);
761 if (sem_init(&
netw->send_blocker, 0, 0) != 0) {
763 pthread_mutex_destroy(&
netw->eventbolt);
764 pthread_mutex_destroy(&
netw->sendbolt);
765 pthread_mutex_destroy(&
netw->recvbolt);
771 if (!
netw->recv_buf) {
772 n_log(
LOG_ERR,
"Error when creating receive list with %zu item limit", recv_list_limit);
777 if (!
netw->send_buf) {
778 n_log(
LOG_ERR,
"Error when creating send list with %zu item limit", send_list_limit);
788 netw->addr_infos_loaded = 0;
789 netw->send_queue_consecutive_wait = 0;
790 netw->so_reuseaddr = -1;
792 netw->so_keepalive = -1;
793 netw->tcpnodelay = -1;
794 netw->so_sndbuf = -1;
795 netw->so_rcvbuf = -1;
796 netw->so_rcvtimeo = -1;
797 netw->so_sndtimeo = -1;
798 netw->so_linger = -1;
799 netw->deplete_socket_timeout = -1;
800 netw->deplete_queues_timeout = -1;
801 netw->wait_close_timeout = -1;
815 netw->certificate = NULL;
818 netw->link.is_blocking = 1;
829 return sa->sa_family == AF_INET
830 ? (
char*)&(((
struct sockaddr_in*)sa)->sin_addr)
831 : (
char*)&(((
struct sockaddr_in6*)sa)->sin6_addr);
842 int compiler_warning_suppressor = 0;
843#if !defined(__linux__) && !defined(__sun) && !defined(_AIX)
844 static WSADATA WSAdata;
845 static int WSA_IS_INITIALIZED = 0;
851 return WSA_IS_INITIALIZED;
855 if (WSA_IS_INITIALIZED == 1)
857 if ((WSAStartup(MAKEWORD(v1, v2), &WSAdata)) != 0) {
858 WSA_IS_INITIALIZED = 0;
861 WSA_IS_INITIALIZED = 1;
867 if (WSA_IS_INITIALIZED == 0)
869 if (WSACleanup() == 0) {
870 WSA_IS_INITIALIZED = 0;
876 compiler_warning_suppressor =
mode + v1 + v2;
877 (void)compiler_warning_suppressor;
878 compiler_warning_suppressor = TRUE;
879 return compiler_warning_suppressor;
918#if defined(__linux__) || defined(__sun)
920 flags = fcntl(
netw->link.sock, F_GETFL, 0);
921 if (
netw->link.is_blocking != 0 && !is_blocking) {
922 if (flags & O_NONBLOCK) {
925 netw->link.is_blocking = 0;
928 }
else if (
netw->link.is_blocking != 1 && is_blocking) {
929 if (!(flags & O_NONBLOCK)) {
932 netw->link.is_blocking = 1;
936 if (fcntl(
netw->link.sock, F_SETFL, is_blocking ? flags & ~O_NONBLOCK : flags | O_NONBLOCK) == -1) {
940 n_log(
LOG_ERR,
"couldn't set blocking mode %lu on %d: %s", is_blocking,
netw->link.sock,
_str(errmsg));
945 unsigned long int blocking = 1 - is_blocking;
946 int res = ioctlsocket(
netw->link.sock, (
long)FIONBIO, &blocking);
948 if (res != NO_ERROR) {
951 n_log(
LOG_ERR,
"ioctlsocket failed with error: %d , neterrno: %s", res,
_str(errmsg));
957 netw->link.is_blocking = is_blocking;
979 if (setsockopt(
netw->link.sock, IPPROTO_TCP, TCP_NODELAY, (
const char*)&value,
sizeof(value)) == -1) {
988 netw->tcpnodelay = value;
993 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_SNDBUF, (
const char*)&value,
sizeof(value)) == -1) {
1002 netw->so_sndbuf = value;
1007 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_RCVBUF, (
const char*)&value,
sizeof(value)) == -1) {
1016 netw->so_rcvbuf = value;
1020 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_REUSEADDR, (
char*)&value,
sizeof(value)) == -1) {
1028 netw->so_reuseaddr = value;
1047 }
else if (value == 0) {
1053 ling.l_linger = (u_short)value;
1055 ling.l_linger = value;
1059 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_LINGER, &ling,
sizeof(ling)) == -1) {
1063 n_log(
LOG_ERR,
"Error from setsockopt(SO_LINGER) on socket %d. neterrno: %s",
netw->link.sock,
_str(errmsg));
1068 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_LINGER, (
const char*)&ling,
sizeof(ling)) == -1) {
1077 netw->so_linger = value;
1086 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_RCVTIMEO, (
const char*)&tv,
sizeof tv) == -1) {
1090 n_log(
LOG_ERR,
"Error from setsockopt(SO_RCVTIMEO) on socket %d. neterrno: %s",
netw->link.sock,
_str(errmsg));
1096 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_RCVTIMEO, (
const char*)&value,
sizeof value) == -1) {
1106 netw->so_rcvtimeo = value;
1116 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_SNDTIMEO, (
const char*)&tv,
sizeof tv) == -1) {
1120 n_log(
LOG_ERR,
"Error from setsockopt(SO_SNDTIMEO) on socket %d. neterrno: %s",
netw->link.sock,
_str(errmsg));
1126 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_SNDTIMEO, (
const char*)&value,
sizeof value) == -1) {
1136 netw->so_sndtimeo = value;
1139 netw->deplete_socket_timeout = value;
1142 netw->deplete_queues_timeout = value;
1145 netw->wait_close_timeout = value;
1148 netw->send_queue_consecutive_wait = value;
1151 case TCP_USER_TIMEOUT:
1153 if (setsockopt(
netw->link.sock, IPPROTO_TCP, TCP_USER_TIMEOUT, (
const char*)&value,
sizeof value) == -1) {
1157 n_log(
LOG_ERR,
"Error from setsockopt(TCP_USER_TIMEOUT) on socket %d. neterrno: %s",
netw->link.sock,
_str(errmsg));
1164 if (setsockopt(
netw->link.sock, IPPROTO_TCP, TCP_QUICKACK, &value,
sizeof(value)) < 0) {
1168 n_log(
LOG_ERR,
"Error setting setsockopt(TCP_QUICKACK) to %d on sock %d. neterrno: %s", value,
netw->link.sock,
_str(errmsg));
1175 if (setsockopt(
netw->link.sock, SOL_SOCKET, SO_KEEPALIVE, (
const char*)&value,
sizeof value) == -1) {
1183 netw->so_keepalive = value;
1188 n_log(
LOG_ERR,
"%d is not a supported setsockopt", optname);
1201 BIO* bio = BIO_new(BIO_s_mem());
1206 ERR_print_errors(bio);
1209 size_t len = (size_t)BIO_get_mem_data(bio, &buf);
1212 char* error_str = malloc(len + 1);
1214 memcpy(error_str, buf, len);
1215 error_str[len] =
'\0';
1232 unsigned long error = 0;
1233 while ((error = ERR_get_error())) {
1235#ifdef SSL_R_UNEXPECTED_EOF_WHILE_READING
1242 if (ERR_GET_REASON(error) == SSL_R_UNEXPECTED_EOF_WHILE_READING)
1291 if (
mode & CRYPTO_LOCK) {
1298__attribute__((unused))
static unsigned long thread_id(
void) {
1301 ret = (
unsigned long)pthread_self();
1308 size_t lock_count = (size_t)CRYPTO_num_locks();
1309 netw_ssl_lockarray = (pthread_mutex_t*)OPENSSL_malloc((
size_t)(lock_count *
sizeof(pthread_mutex_t)));
1311 for (i = 0; i < CRYPTO_num_locks(); i++) {
1315 CRYPTO_set_id_callback((
unsigned long (*)())thread_id);
1316 CRYPTO_set_locking_callback((
void (*)())netw_ssl_lock_callback);
1322 CRYPTO_set_locking_callback(NULL);
1323 for (i = 0; i < CRYPTO_num_locks(); i++)
1340 SSL_load_error_strings();
1342#if OPENSSL_VERSION_NUMBER < 0x10100000L
1343 ERR_load_BIO_strings();
1345 OpenSSL_add_all_algorithms();
1356 signal(SIGPIPE, SIG_IGN);
1387 SSL_CTX_free(
netw->ctx);
1405 SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
1406 SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET);
1407 SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
1408#ifdef SSL_OP_NO_SSLv2
1409 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
1411#ifdef SSL_OP_NO_SSLv3
1412 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
1414#ifdef SSL_OP_NO_RENEGOTIATION
1415 SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION);
1417#if N_OSSL_HAVE_PROTO_VERSION_SETTERS
1418 if (SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) != 1) {
1419 n_log(
LOG_ERR,
"SSL_CTX_set_min_proto_version(TLS 1.2) failed");
1423 if (SSL_CTX_set_cipher_list(ctx,
"ECDHE+AESGCM:ECDHE+CHACHA20:!aNULL") != 1) {
1424 n_log(
LOG_ERR,
"SSL_CTX_set_cipher_list(secure defaults) failed");
1427#if N_OSSL_OPENSSL_ATLEAST(0x10101000L) || N_OSSL_LIBRESSL_ATLEAST(0x2070000fL)
1428 if (SSL_CTX_set1_groups_list(ctx,
"X25519:P-256:P-384") != 1) {
1429 n_log(
LOG_ERR,
"SSL_CTX_set1_groups_list(secure defaults) failed");
1432#elif defined(SSL_CTX_set_ecdh_auto)
1433 SSL_CTX_set_ecdh_auto(ctx, 1);
1450 SSL_CTX* ctx = NULL;
1454#if OPENSSL_VERSION_NUMBER >= 0x10100000L
1455 ctx = SSL_CTX_new(TLS_server_method());
1457 ctx = SSL_CTX_new(TLSv1_2_server_method());
1463 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1469#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1470 SSL_CTX_set_num_tickets(ctx, 0);
1472 if (SSL_CTX_use_certificate_chain_file(ctx, cert_chain_file) != 1 ||
1473 SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) != 1 || SSL_CTX_check_private_key(ctx) != 1) {
1474 n_log(
LOG_ERR,
"netw_ssl_server_ctx_new: cannot load %s / %s, or they do not match", cert_chain_file, key_file);
1492 char* tmp_key = NULL;
1493 char* tmp_cert = NULL;
1494 if (
key && strlen(
key) > 0) {
1495 tmp_key = strdup(
key);
1497 n_log(
LOG_ERR,
"strdup failed for key in netw_set_crypto");
1501 if (certificate && strlen(certificate) > 0) {
1502 tmp_cert = strdup(certificate);
1504 n_log(
LOG_ERR,
"strdup failed for certificate in netw_set_crypto");
1511 netw->key = tmp_key;
1515 netw->certificate = tmp_cert;
1517 if (
key && certificate) {
1519#if OPENSSL_VERSION_NUMBER >= 0x10100000L
1520 netw->method = TLS_method();
1522 netw->method = TLSv1_2_method();
1524 netw->ctx = SSL_CTX_new(
netw->method);
1527 if (
netw->ctx == NULL) {
1536 SSL_CTX_set_mode(
netw->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1543#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1558 SSL_CTX_set_num_tickets(
netw->ctx, 0);
1562 if (SSL_CTX_load_verify_locations(
netw->ctx, NULL,
"/etc/ssl/certs/") != 1) {
1568 if (SSL_CTX_use_certificate_file(
netw->ctx, certificate, SSL_FILETYPE_PEM) <= 0) {
1573 if (SSL_CTX_use_PrivateKey_file(
netw->ctx,
key, SSL_FILETYPE_PEM) <= 0) {
1603#if OPENSSL_VERSION_NUMBER >= 0x10100000L
1604 netw->method = TLS_method();
1606 netw->method = TLSv1_2_method();
1608 netw->ctx = SSL_CTX_new(
netw->method);
1609 if (
netw->ctx == NULL) {
1618 SSL_CTX_set_mode(
netw->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1625#if OPENSSL_VERSION_NUMBER >= 0x10101000L
1631 SSL_CTX_set_num_tickets(
netw->ctx, 0);
1635 BIO* cert_bio = BIO_new_mem_buf(cert_pem, -1);
1637 n_log(
LOG_ERR,
"Failed to create BIO for certificate PEM");
1641 X509*
cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL);
1649 if (SSL_CTX_use_certificate(
netw->ctx,
cert) <= 0) {
1658 BIO* key_bio = BIO_new_mem_buf(key_pem, -1);
1664 EVP_PKEY* pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL);
1672 if (SSL_CTX_use_PrivateKey(
netw->ctx, pkey) <= 0) {
1673 EVP_PKEY_free(pkey);
1678 EVP_PKEY_free(pkey);
1681 if (!SSL_CTX_check_private_key(
netw->ctx)) {
1682 n_log(
LOG_ERR,
"Private key does not match the certificate");
1715 if (SSL_CTX_use_certificate_chain_file(
netw->ctx, certificate) <= 0) {
1716 n_log(
LOG_ERR,
"Failed to load certificate chain from %s", certificate);
1722 if (SSL_CTX_load_verify_locations(
netw->ctx,
ca_file, NULL) != 1) {
1750 BIO* ca_bio = BIO_new_mem_buf(ca_pem, -1);
1756 X509_STORE* store = SSL_CTX_get_cert_store(
netw->ctx);
1759 n_log(
LOG_ERR,
"Failed to get certificate store from SSL context");
1763 X509* ca_cert = NULL;
1765 while ((ca_cert = PEM_read_bio_X509(ca_bio, NULL, NULL, NULL)) != NULL) {
1766 if (X509_STORE_add_cert(store, ca_cert) != 1) {
1767 n_log(
LOG_ERR,
"Failed to add CA certificate to store");
1777 if (ca_loaded == 0) {
1778 n_log(
LOG_ERR,
"No CA certificates were loaded from PEM string");
1783 BIO* chain_bio = BIO_new_mem_buf(cert_pem, -1);
1786 X509* skip_cert = PEM_read_bio_X509(chain_bio, NULL, NULL, NULL);
1788 X509_free(skip_cert);
1791 X509* chain_cert = NULL;
1792 while ((chain_cert = PEM_read_bio_X509(chain_bio, NULL, NULL, NULL)) != NULL) {
1793 if (SSL_CTX_add_extra_chain_cert(
netw->ctx, chain_cert) != 1) {
1795 X509_free(chain_cert);
1799 BIO_free(chain_bio);
1808static int _ssl_use_pem(SSL* ssl,
const char* key_pem,
const char* cert_pem) {
1809 BIO* cbio = BIO_new_mem_buf(cert_pem, -1);
1814 cert = PEM_read_bio_X509(cbio, NULL, NULL, NULL);
1818 if (SSL_use_certificate(ssl,
cert) == 1) {
1819 BIO* kbio = BIO_new_mem_buf(key_pem, -1);
1821 EVP_PKEY* pkey = PEM_read_bio_PrivateKey(kbio, NULL, NULL, NULL);
1824 if (SSL_use_PrivateKey(ssl, pkey) == 1 && SSL_check_private_key(ssl) == 1)
1826 EVP_PKEY_free(pkey);
1843 return SSL_get_servername(
netw->ssl, TLSEXT_NAMETYPE_host_name);
1879static int _netw_alpn_select_cb(SSL* ssl,
const unsigned char** out,
unsigned char* outlen,
const unsigned char* in,
unsigned int inlen,
void* arg) {
1884 return SSL_TLSEXT_ERR_NOACK;
1886 size_t want = a->
alpn[i] ? strlen(a->
alpn[i]) : 0;
1887 unsigned int off = 0;
1888 if (want == 0 || want > 255)
1890 while (off < inlen) {
1891 unsigned int l = in[off];
1892 if (l == 0 || off + 1 + l > inlen)
1894 if (l == want && memcmp(in + off + 1, a->
alpn[i], want) == 0) {
1895 *out = in + off + 1;
1896 *outlen = (
unsigned char)l;
1897 return SSL_TLSEXT_ERR_OK;
1902 return SSL_TLSEXT_ERR_NOACK;
1907static size_t _netw_alpn_encode(
const char*
const* protos,
size_t count,
unsigned char* out,
size_t out_cap) {
1909 if (!protos || count == 0 || !out)
1911 for (i = 0; i < count; i++) {
1912 size_t l = protos[i] ? strlen(protos[i]) : 0;
1913 if (l == 0 || l > 255 || off + 1 + l > out_cap)
1915 out[off++] = (
unsigned char)l;
1916 memcpy(out + off, protos[i], l);
1935 unsigned char wire[512];
1945 if (SSL_CTX_set_alpn_protos(
netw->ctx, wire, (
unsigned int)len) != 0) {
1946 n_log(
LOG_ERR,
"OpenSSL refused the ALPN protocol list");
1965 const unsigned char* proto = NULL;
1966 unsigned int plen = 0;
1967 if (out && out_len > 0)
1971 if (!
netw->ssl || out_len < 2)
1973 SSL_get0_alpn_selected(
netw->ssl, &proto, &plen);
1974 if (!proto || plen == 0 || plen >= out_len)
1976 memcpy(out, proto, plen);
1987 N_STR* cert_pem = NULL;
1988 N_STR* key_pem = NULL;
1992 return SSL_TLSEXT_ERR_OK;
1993 sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1994 if (a->
pick(sni, &cert_pem, &key_pem, a->
user_data) != 0 || !cert_pem || !key_pem) {
1999 return SSL_TLSEXT_ERR_ALERT_FATAL;
2004 return ok ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL;
2053#if OPENSSL_VERSION_NUMBER >= 0x10100000L
2054 netw->method = TLS_method();
2056 netw->method = TLSv1_2_method();
2058 netw->ctx = SSL_CTX_new(
netw->method);
2063 SSL_CTX_set_mode(
netw->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
2070#if OPENSSL_VERSION_NUMBER >= 0x10101000L
2071 SSL_CTX_set_num_tickets(
netw->ctx, 0);
2081 SSL_CTX_set_tlsext_servername_arg(
netw->ctx, &arg);
2090 SSL_set_fd(
netw->ssl, (
int)
netw->link.sock);
2097 if (SSL_accept(
netw->ssl) <= 0) {
2142 n_log(
LOG_ERR,
"At least one of ca_file or ca_path must be specified");
2146 if (SSL_CTX_load_verify_locations(
netw->ctx,
ca_file, ca_path) != 1) {
2167 SSL_CTX_set_verify(
netw->ctx, SSL_VERIFY_PEER, NULL);
2169 SSL_CTX_set_verify(
netw->ctx, SSL_VERIFY_NONE, NULL);
2198#if N_OSSL_HAVE_PROTO_VERSION_SETTERS
2199 if (min_version != 0 && SSL_CTX_set_min_proto_version(
netw->ctx, min_version) != 1) {
2200 n_log(
LOG_ERR,
"SSL_CTX_set_min_proto_version(%d) failed", min_version);
2203 if (max_version != 0 && SSL_CTX_set_max_proto_version(
netw->ctx, max_version) != 1) {
2204 n_log(
LOG_ERR,
"SSL_CTX_set_max_proto_version(%d) failed", max_version);
2216 if (min_version != 0 && min_version > TLS1_2_VERSION) {
2217 n_log(
LOG_ERR,
"netw_ssl_set_versions: minimum %d is above TLS 1.2, the highest this OpenSSL supports", min_version);
2220 if (max_version != 0 && max_version < TLS1_2_VERSION) {
2221 n_log(
LOG_ERR,
"netw_ssl_set_versions: maximum %d is below TLS 1.2, the only version this OpenSSL context speaks", max_version);
2248 if (cipher_list && cipher_list[0] && SSL_CTX_set_cipher_list(
netw->ctx, cipher_list) != 1) {
2249 n_log(
LOG_ERR,
"SSL_CTX_set_cipher_list(%s) failed: no cipher matched", cipher_list);
2253 if (ciphersuites && ciphersuites[0] && SSL_CTX_set_ciphersuites(
netw->ctx, ciphersuites) != 1) {
2254 n_log(
LOG_ERR,
"SSL_CTX_set_ciphersuites(%s) failed", ciphersuites);
2265 if (ciphersuites && ciphersuites[0]) {
2266 n_log(
LOG_DEBUG,
"netw_ssl_set_ciphers: ignoring the TLS 1.3 ciphersuites '%s', this OpenSSL cannot negotiate TLS 1.3", ciphersuites);
2290 STACK_OF(X509)* chain = NULL;
2291 X509_STORE* store = NULL;
2292 X509_STORE_CTX* store_ctx = NULL;
2297 if (errbuf && errsz > 0)
2304 if (errbuf && errsz > 0)
2305 snprintf(errbuf, errsz,
"no peer certificate");
2308 chain = SSL_get_peer_cert_chain(
netw->ssl);
2310 store = X509_STORE_new();
2311 store_ctx = X509_STORE_CTX_new();
2312 if (!store || !store_ctx) {
2313 if (errbuf && errsz > 0)
2314 snprintf(errbuf, errsz,
"out of memory");
2317 X509_STORE_set_default_paths(store);
2318 if (X509_STORE_CTX_init(store_ctx, store,
cert, chain) != 1) {
2319 if (errbuf && errsz > 0)
2320 snprintf(errbuf, errsz,
"verify init failed");
2323 if (X509_verify_cert(store_ctx) == 1) {
2326 int verr = X509_STORE_CTX_get_error(store_ctx);
2327 if (errbuf && errsz > 0)
2328 snprintf(errbuf, errsz,
"%s", X509_verify_cert_error_string(verr));
2330 if (expected_host && expected_host[0]) {
2331 if (X509_check_host(
cert, expected_host, 0, 0, NULL) != 1) {
2333 if (errbuf && errsz > 0 && chain_ok)
2334 snprintf(errbuf, errsz,
"hostname mismatch for %s", expected_host);
2337 result = (chain_ok && host_ok) ? TRUE : FALSE;
2341 X509_STORE_CTX_free(store_ctx);
2343 X509_STORE_free(store);
2360 if (SSL_CTX_use_certificate_file(
netw->ctx, cert_file, SSL_FILETYPE_PEM) != 1) {
2361 n_log(
LOG_ERR,
"Failed to load client certificate from %s", cert_file);
2367 const char* kf = key_file ? key_file : cert_file;
2368 if (SSL_CTX_use_PrivateKey_file(
netw->ctx, kf, SSL_FILETYPE_PEM) != 1) {
2369 n_log(
LOG_ERR,
"Failed to load client private key from %s", kf);
2375 if (SSL_CTX_check_private_key(
netw->ctx) != 1) {
2376 n_log(
LOG_ERR,
"Client certificate and private key do not match");
2389#define NETW_CONNECT_ABORT_POLL_MS 100
2412 if (connect_timeout_ms <= 0) {
2413 return connect(sock, rp->ai_addr, (socklen_t)rp->ai_addrlen);
2418 return connect(sock, rp->ai_addr, (socklen_t)rp->ai_addrlen);
2421 int rc = connect(sock, rp->ai_addr, (socklen_t)rp->ai_addrlen);
2427 int in_progress = 0;
2429 if (
neterrno == WSAEWOULDBLOCK ||
neterrno == WSAEINPROGRESS) in_progress = 1;
2431 if (
neterrno == EINPROGRESS) in_progress = 1;
2449 time_t remaining_us = (time_t)connect_timeout_ms * 1000;
2465 FD_SET(sock, &wset);
2467 FD_SET(sock, &eset);
2468 time_t wait_us = remaining_us;
2469 if (poll_us > 0 && wait_us > poll_us)
2472 tv.tv_sec = (long)(wait_us / 1000000);
2473 tv.tv_usec = (long)(wait_us % 1000000);
2475 int sel = select((
int)sock + 1, NULL, &wset, &eset, &tv);
2486 if (remaining_us <= 0)
2492 int interrupted = (
neterrno == WSAEINTR);
2494 int interrupted = (
neterrno == EINTR);
2502 if (remaining_us <= 0) {
2507 if (aborted || !connected) {
2513 socklen_t slen =
sizeof(so_error);
2514 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (
char*)&so_error, &slen) == -1 || so_error != 0) {
2560 (void)ssl_cert_file;
2561 int error = 0, net_status = 0;
2562 char* errmsg = NULL;
2566 n_log(
LOG_ERR,
"Unable to allocate (*netw), already existing. You must use empty NETWORK *structs.");
2571 (*netw) =
netw_new(send_list_limit, recv_list_limit);
2583 (*netw)->link.hints.ai_family = AF_INET;
2585 (*netw)->link.hints.ai_family = AF_INET6;
2588 (*netw)->link.hints.ai_family = AF_UNSPEC;
2591 (*netw)->link.hints.ai_socktype = SOCK_STREAM;
2592 (*netw)->link.hints.ai_protocol = IPPROTO_TCP;
2593 (*netw)->link.hints.ai_flags = AI_PASSIVE;
2594 (*netw)->link.hints.ai_canonname = NULL;
2595 (*netw)->link.hints.ai_next = NULL;
2607 error = getaddrinfo(host,
port, &(*netw)->link.hints, &(*netw)->link.rhost);
2610 n_log(
LOG_ERR,
"Error when resolving %s:%s getaddrinfo: %s", host,
port, gai_strerror(error));
2615 (*netw)->addr_infos_loaded = 1;
2616 (*netw)->connect_dns_usec = (
long long)
get_usec(&connect_timer);
2617 Malloc((*netw)->link.ip,
char, 64);
2621 struct addrinfo* rp = NULL;
2622 for (rp = (*netw)->link.rhost; rp != NULL; rp = rp->ai_next) {
2623 SOCKET sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
2624 if (sock == INVALID_SOCKET) {
2632 (*netw)->link.sock = sock;
2635 if (net_status == -1) {
2641 (*netw)->link.sock = INVALID_SOCKET;
2645 if (!inet_ntop(rp->ai_family,
get_in_addr(rp->ai_addr), (*netw)->link.ip, 64)) {
2657 n_log(
LOG_ERR,
"Couldn't connect to %s:%s : no address succeeded", host,
port);
2662 (*netw)->connect_tcp_usec = (
long long)
get_usec(&connect_timer);
2664 (*netw)->link.port = strdup(
port);
2667 if (ssl_key_file && ssl_cert_file) {
2677 (*netw)->ssl = SSL_new((*netw)->ctx);
2678 SSL_set_fd((*netw)->ssl, (
int)(*netw)->link.sock);
2681 if (SSL_connect((*netw)->ssl) <= 0) {
2688 n_log(
LOG_DEBUG,
"SSL-Connected to %s:%s", (*netw)->link.ip, (*netw)->link.port);
2690 _netw_capture_error(*
netw,
"%s:%s trying to configure SSL but application was compiled without SSL support !", (*netw)->link.ip, (*netw)->link.port);
2691 n_log(
LOG_ERR,
"%s:%s trying to configure SSL but application was compiled without SSL support !", (*netw)->link.ip, (*netw)->link.port);
2694 n_log(
LOG_DEBUG,
"Connected to %s:%s", (*netw)->link.ip, (*netw)->link.port);
2713 while (
netw->link.connect_rp) {
2714 struct addrinfo* rp =
netw->link.connect_rp;
2715 SOCKET sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
2716 if (sock == INVALID_SOCKET) {
2717 netw->link.connect_rp = rp->ai_next;
2720 netw->link.sock = sock;
2723 netw->link.sock = INVALID_SOCKET;
2724 netw->link.connect_rp = rp->ai_next;
2727 if (connect(sock, rp->ai_addr, (socklen_t)rp->ai_addrlen) == 0)
2737 netw->link.sock = INVALID_SOCKET;
2738 netw->link.connect_rp = rp->ai_next;
2764 n_log(
LOG_ERR,
"netw_connect_nonblock: pass an empty NETWORK* (got a non-NULL one)");
2770 n_log(
LOG_ERR,
"netw_connect_nonblock: unable to load WSA");
2775 (*netw)->link.hints.ai_family = AF_INET;
2777 (*netw)->link.hints.ai_family = AF_INET6;
2779 (*netw)->link.hints.ai_family = AF_UNSPEC;
2780 (*netw)->link.hints.ai_socktype = SOCK_STREAM;
2781 (*netw)->link.hints.ai_protocol = IPPROTO_TCP;
2786 Malloc((*netw)->link.ip,
char, 64);
2789 error = getaddrinfo(host,
port, &(*netw)->link.hints, &(*netw)->link.rhost);
2792 n_log(
LOG_ERR,
"netw_connect_nonblock resolve %s:%s: %s", host,
port, gai_strerror(error));
2796 (*netw)->addr_infos_loaded = 1;
2797 (*netw)->link.connect_rp = (*netw)->link.rhost;
2798 (*netw)->link.port = strdup(
port);
2803 n_log(
LOG_ERR,
"netw_connect_nonblock: could not initiate connect to %s:%s", host,
port);
2824 if (
netw->link.sock == INVALID_SOCKET)
2829 FD_SET(
netw->link.sock, &wset);
2831 FD_SET(
netw->link.sock, &eset);
2835 int sel = select((
int)
netw->link.sock + 1, NULL, &wset, &eset, &tv);
2846 socklen_t slen =
sizeof(so_err);
2847 if (getsockopt(
netw->link.sock, SOL_SOCKET, SO_ERROR, (
char*)&so_err, &slen) != 0)
2852 if (
netw->link.connect_rp &&
2853 !inet_ntop(
netw->link.connect_rp->ai_family,
get_in_addr(
netw->link.connect_rp->ai_addr),
netw->link.ip, 64)) {
2863 n_log(
LOG_INFO,
"netw_connect_check: connect failed on an address: %s",
_str(errmsg));
2866 closesocket(
netw->link.sock);
2867 netw->link.sock = INVALID_SOCKET;
2868 netw->link.connect_rp =
netw->link.connect_rp ?
netw->link.connect_rp->ai_next : NULL;
2981#if OPENSSL_VERSION_NUMBER >= 0x10100000L
2982 netw->method = TLS_client_method();
2984 netw->method = TLSv1_2_client_method();
2986 netw->ctx = SSL_CTX_new(
netw->method);
2996 SSL_CTX_set_mode(
netw->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
2998 SSL_CTX_set_default_verify_paths(
netw->ctx);
3037 if (!host || !host[0])
3039 if (inet_pton(AF_INET, host, &v4) == 1)
3041 if (inet_pton(AF_INET6, host, &v6) == 1)
3067 SSL_set_fd(
netw->ssl, (
int)
netw->link.sock);
3069 SSL_set_tlsext_host_name(
netw->ssl, sni_hostname);
3083 X509_VERIFY_PARAM* vp = SSL_get0_param(
netw->ssl);
3084 if (!vp || X509_VERIFY_PARAM_set1_ip_asc(vp, sni_hostname) != 1)
3085 n_log(
LOG_ERR,
"netw_ssl_do_handshake: could not set the expected peer IP %s", sni_hostname);
3089 n_log(
LOG_ERR,
"netw_ssl_do_handshake: could not set the expected peer name %s", sni_hostname);
3092 if (SSL_connect(
netw->ssl) <= 0) {
3094 unsigned long err = ERR_peek_error();
3097 err ? ERR_reason_error_string(err) :
"unknown error");
3115 int r = SSL_do_handshake(
netw->ssl);
3116 int sys_errno = errno;
3119 int e = SSL_get_error(
netw->ssl, r);
3120 if (e == SSL_ERROR_WANT_READ)
3122 if (e == SSL_ERROR_WANT_WRITE)
3125 unsigned long err = ERR_peek_error();
3126 const char* why = err ? ERR_reason_error_string(err) : NULL;
3129 why ? why :
"unknown error");
3135 netw->link.sock, e, why ? why : (e == SSL_ERROR_ZERO_RETURN ?
"peer closed" : (e == SSL_ERROR_SYSCALL ?
"syscall/EOF" :
"no reason")), sys_errno);
3169 SSL_set_fd(
netw->ssl, (
int)
netw->link.sock);
3170 SSL_set_connect_state(
netw->ssl);
3172 SSL_set_tlsext_host_name(
netw->ssl, sni_hostname);
3206 netw->ssl = SSL_new(ctx);
3212 SSL_set_fd(
netw->ssl, (
int)
netw->link.sock);
3213 SSL_set_accept_state(
netw->ssl);
3240 pthread_mutex_lock(&
netw->eventbolt);
3243 if (thr_engine_status)
3244 (*thr_engine_status) =
netw->threaded_engine_status;
3245 pthread_mutex_unlock(&
netw->eventbolt);
3262 pthread_mutex_lock(&
netw->sendbolt);
3265 pthread_mutex_unlock(&
netw->sendbolt);
3268 pthread_mutex_lock(&
netw->recvbolt);
3271 pthread_mutex_unlock(&
netw->recvbolt);
3274 pthread_mutex_lock(&
netw->sendbolt);
3277 pthread_mutex_unlock(&
netw->sendbolt);
3280 pthread_mutex_lock(&
netw->recvbolt);
3283 pthread_mutex_unlock(&
netw->recvbolt);
3285 pthread_mutex_lock(&
netw->eventbolt);
3310 pthread_mutex_unlock(&
netw->eventbolt);
3324 sem_post(&
netw->send_blocker);
3337#if defined(__linux__)
3338 int outstanding = 0;
3342 for (
int it = 0; it < timeout; it += 100) {
3344 if (ioctl(fd, SIOCOUTQ, &outstanding) == -1) {
3346 n_log(
LOG_ERR,
"ioctl SIOCOUTQ returned -1: %s for socket %d", strerror(error), fd);
3370 int thr_engine_status = 0;
3393#if N_REACTOR_AVAILABLE
3394 if (__atomic_load_n(&(*netw)->reactor_registered, __ATOMIC_ACQUIRE)) {
3399 if ((*netw)->deplete_queues_timeout > 0) {
3401 int max_iterations = (*netw)->deplete_queues_timeout * 10;
3406 pthread_mutex_lock(&(*netw)->eventbolt);
3407 nb_running = (*netw)->nb_running_threads;
3408 pthread_mutex_unlock(&(*netw)->eventbolt);
3411 }
while (nb_running > 0 && it < max_iterations);
3413 if (it >= max_iterations && nb_running > 0) {
3419 if ((*netw)->link.sock != INVALID_SOCKET) {
3420 int remaining =
deplete_send_buffer((
int)(*netw)->link.sock, (*netw)->deplete_socket_timeout);
3422 if (remaining > 0) {
3423 n_log(
LOG_ERR,
"socket " SOCKET_SIZE_FORMAT " (%s:%s) %d octets still in send buffer before closing after a wait of %d msecs", (*netw)->link.sock, (*netw)->link.ip, (*netw)->link.port, remaining, (*netw)->deplete_socket_timeout);
3440 pthread_mutex_lock(&(*netw)->eventbolt);
3441 nb_running = (*netw)->nb_running_threads;
3442 pthread_mutex_unlock(&(*netw)->eventbolt);
3444 if ((*netw)->link.sock != INVALID_SOCKET) {
3450 if (nb_running == 0) {
3451 int shutdown_res = SSL_shutdown((*netw)->ssl);
3452 if (shutdown_res == 0) {
3456 shutdown_res = SSL_shutdown((*netw)->ssl);
3458 if (shutdown_res < 0) {
3459 int err = SSL_get_error((*netw)->ssl, shutdown_res);
3460 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
3468 }
else if (err != SSL_ERROR_SYSCALL && err != SSL_ERROR_SSL) {
3476 int shutdown_res = SSL_shutdown((*netw)->ssl);
3477 if (shutdown_res == 0) {
3479 shutdown_res = SSL_shutdown((*netw)->ssl);
3481 if (shutdown_res < 0) {
3482 int err = SSL_get_error((*netw)->ssl, shutdown_res);
3490 SSL_free((*netw)->ssl);
3502 shutdown((*netw)->link.sock, SHUT_WR);
3504 if ((*netw)->wait_close_timeout > 0) {
3507 char buffer[4096] =
"";
3508 int max_iters = (*netw)->wait_close_timeout * 10;
3509 for (
int it = 0; it < max_iters; it++) {
3513#pragma GCC diagnostic push
3514#pragma GCC diagnostic ignored "-Wsign-conversion"
3515 FD_SET((*netw)->link.sock, &rfds);
3516#pragma GCC diagnostic pop
3518 tv.tv_usec = 100000;
3519 int sel = select((
int)(*netw)->link.sock + 1, &rfds, NULL, NULL, &tv);
3521 ssize_t res = recv((*netw)->link.sock, buffer, 4096, NETFLAGS);
3526 if (error != ENOTCONN && error != EINTR && error != ECONNRESET
3528 && error != WSAENOTCONN && error != WSAECONNRESET && error != WSAESHUTDOWN && error != WSAEWOULDBLOCK && error != WSAEINTR
3539 }
else if (sel < 0) {
3544 && error != WSAEINTR
3558 closesocket((*netw)->link.sock);
3565 SSL_CTX_free((*netw)->ctx);
3576 if ((*netw)->link.rhost) {
3577 freeaddrinfo((*netw)->link.rhost);
3583 pthread_mutex_destroy(&(*netw)->recvbolt);
3584 pthread_mutex_destroy(&(*netw)->sendbolt);
3585 pthread_mutex_destroy(&(*netw)->eventbolt);
3586 sem_destroy(&(*netw)->send_blocker);
3606 char* errmsg = NULL;
3609 n_log(
LOG_ERR,
"Cannot use an allocated network. Please pass a NULL network to modify");
3621 (*netw)->link.port = strdup(
port);
3624 (*netw)->link.hints.ai_family = AF_INET;
3626 (*netw)->link.hints.ai_family = AF_INET6;
3629 (*netw)->link.hints.ai_family = AF_UNSPEC;
3632 (*netw)->link.hints.ai_flags = AI_PASSIVE;
3634 (*netw)->link.hints.ai_socktype = SOCK_STREAM;
3635 (*netw)->link.hints.ai_protocol = IPPROTO_TCP;
3636 (*netw)->link.hints.ai_canonname = NULL;
3637 (*netw)->link.hints.ai_next = NULL;
3639 error = getaddrinfo(
addr,
port, &(*netw)->link.hints, &(*netw)->link.rhost);
3646 (*netw)->addr_infos_loaded = 1;
3652 struct addrinfo* rp = NULL;
3653 for (rp = (*netw)->link.rhost; rp != NULL; rp = rp->ai_next) {
3654 (*netw)->link.sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
3655 if ((*netw)->link.sock == INVALID_SOCKET) {
3677 if (bind((*netw)->link.sock, rp->ai_addr, (socklen_t)rp->ai_addrlen) == 0) {
3685 if (!inet_ntop(rp->ai_family,
get_in_addr(rp->ai_addr), ip, 64)) {
3688 n_log(
LOG_ERR,
"inet_ntop: %p , %s", (
void*)&(*netw)->link.raddr,
_str(errmsg));
3692 (*netw)->link.ip = ip;
3700 closesocket((*netw)->link.sock);
3711 (*netw)->nb_pending = nbpending;
3712 if (listen((*netw)->link.sock, (*netw)->nb_pending) != 0) {
3739 char* errmsg = NULL;
3742 n_log(
LOG_ERR,
"Cannot use an allocated network. Please pass a NULL network to modify");
3754 (*netw)->link.port = strdup(
port);
3759 (*netw)->link.hints.ai_family = AF_INET;
3761 (*netw)->link.hints.ai_family = AF_INET6;
3763 (*netw)->link.hints.ai_family = AF_UNSPEC;
3766 (*netw)->link.hints.ai_flags = AI_PASSIVE;
3768 (*netw)->link.hints.ai_socktype = SOCK_DGRAM;
3769 (*netw)->link.hints.ai_protocol = IPPROTO_UDP;
3770 (*netw)->link.hints.ai_canonname = NULL;
3771 (*netw)->link.hints.ai_next = NULL;
3773 error = getaddrinfo(
addr,
port, &(*netw)->link.hints, &(*netw)->link.rhost);
3780 (*netw)->addr_infos_loaded = 1;
3782 struct addrinfo* rp = NULL;
3783 for (rp = (*netw)->link.rhost; rp != NULL; rp = rp->ai_next) {
3784 (*netw)->link.sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
3785 if ((*netw)->link.sock == INVALID_SOCKET) {
3788 n_log(
LOG_ERR,
"Error while trying to make a UDP socket: %s",
_str(errmsg));
3806 if (bind((*netw)->link.sock, rp->ai_addr, (socklen_t)rp->ai_addrlen) == 0) {
3814 if (!inet_ntop(rp->ai_family,
get_in_addr(rp->ai_addr), ip, 64)) {
3817 n_log(
LOG_ERR,
"inet_ntop: %p , %s", (
void*)&(*netw)->link.raddr,
_str(errmsg));
3820 (*netw)->link.ip = ip;
3826 n_log(
LOG_ERR,
"Error from bind() on UDP port %s neterrno: %s",
port, errmsg);
3828 closesocket((*netw)->link.sock);
3858 char* errmsg = NULL;
3861 n_log(
LOG_ERR,
"Unable to allocate (*netw), already existing. You must use empty NETWORK *structs.");
3877 (*netw)->link.hints.ai_family = AF_INET;
3879 (*netw)->link.hints.ai_family = AF_INET6;
3881 (*netw)->link.hints.ai_family = AF_UNSPEC;
3884 (*netw)->link.hints.ai_socktype = SOCK_DGRAM;
3885 (*netw)->link.hints.ai_protocol = IPPROTO_UDP;
3886 (*netw)->link.hints.ai_flags = AI_PASSIVE;
3887 (*netw)->link.hints.ai_canonname = NULL;
3888 (*netw)->link.hints.ai_next = NULL;
3890 error = getaddrinfo(host,
port, &(*netw)->link.hints, &(*netw)->link.rhost);
3893 n_log(
LOG_ERR,
"Error when resolving %s:%s getaddrinfo: %s", host,
port, gai_strerror(error));
3897 (*netw)->addr_infos_loaded = 1;
3898 Malloc((*netw)->link.ip,
char, 64);
3901 struct addrinfo* rp = NULL;
3902 for (rp = (*netw)->link.rhost; rp != NULL; rp = rp->ai_next) {
3903 SOCKET sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
3904 if (sock == INVALID_SOCKET) {
3907 n_log(
LOG_ERR,
"Error while trying to make a UDP socket: %s",
_str(errmsg));
3912 (*netw)->link.sock = sock;
3915 if (connect(sock, rp->ai_addr, (socklen_t)rp->ai_addrlen) == -1) {
3921 (*netw)->link.sock = INVALID_SOCKET;
3924 if (!inet_ntop(rp->ai_family,
get_in_addr(rp->ai_addr), (*netw)->link.ip, 64)) {
3935 n_log(
LOG_ERR,
"Couldn't connect UDP to %s:%s : no address succeeded", host,
port);
3940 (*netw)->link.port = strdup(
port);
3949 n_log(
LOG_DEBUG,
"UDP-Connected to %s:%s", (*netw)->link.ip, (*netw)->link.port);
3967 char* errmsg = NULL;
3975 ssize_t bs = send(s, buf, NETW_BUFLEN_CAST(n), NETFLAGS);
3978 if (error == ECONNRESET || error == ENOTCONN
3980 || error == WSAECONNRESET || error == WSAENOTCONN
4014 ssize_t br = recv(s, buf, NETW_BUFLEN_CAST(n), NETFLAGS);
4049 ssize_t bs = sendto(
netw->link.sock, buf, NETW_BUFLEN_CAST(n), NETFLAGS, dest_addr, dest_len);
4080 ssize_t br = recvfrom(
netw->link.sock, buf, NETW_BUFLEN_CAST(n), NETFLAGS, src_addr, src_len);
4102 SOCKET tmp = INVALID_SOCKET;
4104 char* errmsg = NULL;
4106#if defined(__linux__) || defined(__sun) || defined(_AIX)
4107 socklen_t sin_size = 0;
4124 sin_size =
sizeof(
netw->link.raddr);
4127 int secs = blocking / 1000;
4128 int usecs = (blocking % 1000) * 1000;
4129 struct timeval select_timeout = {secs, usecs};
4132 FD_ZERO(&accept_set);
4133#pragma GCC diagnostic push
4134#pragma GCC diagnostic ignored "-Wsign-conversion"
4135 FD_SET(from->
link.
sock, &accept_set);
4136#pragma GCC diagnostic pop
4138 int ret = select((
int)(from->
link.
sock + 1), &accept_set, NULL, NULL, &select_timeout);
4144 n_log(
LOG_DEBUG,
"error on select with timeout %ds (%d.%ds), neterrno: %s", blocking, secs, usecs,
_str(errmsg));
4148 }
else if (ret == 0) {
4154#pragma GCC diagnostic push
4155#pragma GCC diagnostic ignored "-Wsign-conversion"
4156 if (FD_ISSET(from->
link.
sock, &accept_set)) {
4157#pragma GCC diagnostic pop
4159 tmp = accept(from->
link.
sock, (
struct sockaddr*)&
netw->link.raddr, &sin_size);
4160 if (tmp == INVALID_SOCKET) {
4176 netw->link.is_blocking = 0;
4177 }
else if (blocking == -1) {
4181 tmp = accept(from->
link.
sock, (
struct sockaddr*)&
netw->link.raddr, &sin_size);
4185 if (tmp == INVALID_SOCKET) {
4186 if (error != EINTR && error != EAGAIN
4188 && error != WSAEWOULDBLOCK
4199 netw->link.is_blocking = 0;
4205 tmp = accept(from->
link.
sock, (
struct sockaddr*)&
netw->link.raddr, &sin_size);
4206 if (tmp == INVALID_SOCKET) {
4216 netw->link.is_blocking = 1;
4218 netw->link.sock = tmp;
4225 netw->link.is_blocking = 1;
4229 if (!
netw->link.ip) {
4234 if (!inet_ntop(
netw->link.raddr.ss_family,
get_in_addr(((
struct sockaddr*)&
netw->link.raddr)),
netw->link.ip, 64)) {
4253 netw->ssl = SSL_new(from->
ctx);
4254 SSL_set_fd(
netw->ssl, (
int)
netw->link.sock);
4264 if (SSL_accept(
netw->ssl) <= 0) {
4320 n_log(
LOG_ERR,
"Empty messages are not supported. msg(%p)->length=%zu", msg, msg->
length);
4332 long long bytes_for_counter = (
long long)msg->
written;
4334 pthread_mutex_lock(&
netw->sendbolt);
4337 pthread_mutex_unlock(&
netw->sendbolt);
4341 pthread_mutex_unlock(&
netw->sendbolt);
4357#if N_REACTOR_AVAILABLE
4362 sem_post(&
netw->send_blocker);
4365 sem_post(&
netw->send_blocker);
4393 pthread_mutex_lock(&
netw->sendbolt);
4395 pthread_mutex_unlock(&
netw->sendbolt);
4398 pthread_mutex_unlock(&
netw->sendbolt);
4400 sem_post(&
netw->send_blocker);
4415 pthread_mutex_lock(&
netw->recvbolt);
4419 pthread_mutex_unlock(&
netw->recvbolt);
4434 unsigned int secs = 0;
4435 unsigned int usecs = 0;
4440 if (refresh > 999999) {
4441 secs = refresh / 1000000;
4442 usecs = refresh % 1000000;
4457 if (timed >= refresh)
4459 if (timed == 0 || timed < refresh) {
4487 pthread_mutex_lock(&
netw->eventbolt);
4491 pthread_mutex_unlock(&
netw->eventbolt);
4498 pthread_mutex_unlock(&
netw->eventbolt);
4501 netw->nb_running_threads++;
4509#if defined(__ANDROID__)
4512 pthread_cancel(
netw->recv_thr);
4514 pthread_join(
netw->recv_thr, NULL);
4515 netw->nb_running_threads--;
4516 pthread_mutex_unlock(&
netw->eventbolt);
4519 netw->nb_running_threads++;
4523 pthread_mutex_unlock(&
netw->eventbolt);
4536 ssize_t net_status = 0;
4550 sem_wait(&
netw->send_blocker);
4551 int message_sent = 0;
4552 while (message_sent == 0 && !
DONE) {
4562 memcpy(nboct, &nboctet,
sizeof(uint32_t));
4564 net_status =
netw->send_data(
netw, nboct,
sizeof(int32_t));
4572 pthread_mutex_lock(&
netw->sendbolt);
4574 pthread_mutex_unlock(&
netw->sendbolt);
4579 if (ptr->
written <= UINT_MAX - 2 *
sizeof(uint32_t)) {
4588 uint32_t pkt_state = state;
4591 N_STR* zipped = NULL;
4592 uint32_t flag_bit = 0;
4600 if (zipped && zipped->
written > 0 &&
4606 pkt_state |= flag_bit;
4607 }
else if (zipped) {
4626 size_t frame_len = 2 *
sizeof(uint32_t) + ptr->
written;
4628 Malloc(frame,
char, frame_len);
4630 nboctet = htonl(pkt_state);
4631 memcpy(frame, &nboctet,
sizeof(uint32_t));
4632 nboctet = htonl((uint32_t)ptr->
written);
4633 memcpy(frame +
sizeof(uint32_t), &nboctet,
sizeof(uint32_t));
4634 memcpy(frame + 2 *
sizeof(uint32_t), ptr->
data, ptr->
written);
4635 net_status =
netw->send_data(
netw, frame, (uint32_t)frame_len);
4645 n_log(
LOG_ERR,
"could not allocate %zu byte send frame, packet dropped", frame_len);
4647 if (
netw->send_queue_consecutive_wait >= 0) {
4648 u_sleep((
unsigned int)
netw->send_queue_consecutive_wait);
4654 n_log(
LOG_ERR,
"discarded packet of size %zu which is greater than %" PRIu32, ptr->
written, UINT_MAX);
4677 }
else if (
DONE == 2) {
4680 }
else if (
DONE == 3)
4682 else if (
DONE == 4) {
4685 }
else if (
DONE == 5) {
4699 pthread_mutex_lock(&
netw->eventbolt);
4700 netw->nb_running_threads--;
4701 pthread_mutex_unlock(&
netw->eventbolt);
4706#if !defined(__linux__)
4718 ssize_t net_status = 0;
4726 N_STR* recvdmsg = NULL;
4743 net_status =
netw->recv_data(
netw, nboct,
sizeof(uint32_t));
4744 if (net_status < 0) {
4753 memcpy(&nboctet, nboct,
sizeof(uint32_t));
4754 tmpstate = ntohl(nboctet);
4760 uint32_t pkt_state = tmpstate;
4767 net_status =
netw->recv_data(
netw, nboct,
sizeof(uint32_t));
4768 if (net_status < 0) {
4771 memcpy(&nboctet, nboct,
sizeof(uint32_t));
4772 tmpstate = ntohl(nboctet);
4789 if (!recvdmsg->
data) {
4793 recvdmsg->
length = nboctet + 1;
4797 net_status =
netw->recv_data(
netw, recvdmsg->
data, nboctet);
4798 if (net_status < 0) {
4811 if (want_zlib || want_lz4) {
4812 recvdmsg->
data[nboctet] =
'\0';
4813 N_STR* plain = want_lz4
4821 "socket " SOCKET_SIZE_FORMAT " : failed to decompress payload (%" PRIu32
" bytes, codec=%s); dropping",
4822 netw->link.sock, nboctet,
4823 want_lz4 ?
"lz4" :
"zlib");
4829 pthread_mutex_lock(&
netw->recvbolt);
4832 pthread_mutex_unlock(&
netw->recvbolt);
4847 }
else if (
DONE == 2) {
4850 }
else if (
DONE == 3) {
4853 }
else if (
DONE == 4) {
4856 }
else if (
DONE == 5) {
4859 }
else if (
DONE == 6) {
4862 }
else if (
DONE == 8) {
4876 pthread_mutex_lock(&
netw->eventbolt);
4877 netw->nb_running_threads--;
4878 pthread_mutex_unlock(&
netw->eventbolt);
4883#if !defined(__linux__)
4896 int thr_engine_status = 0;
4904 n_log(
LOG_ERR,
"Thread engine status already stopped for network %p",
netw);
4911 for (
int it = 0; it < 10; it++) {
4912 sem_post(&
netw->send_blocker);
4915 pthread_join(
netw->send_thr, NULL);
4917 pthread_join(
netw->recv_thr, NULL);
4940 char* errmsg = NULL;
4948 char* tmp_buf = buf;
4959 }
else if (error == ECONNRESET || error == ENOTCONN || error == EPIPE
4961 || error == WSAECONNRESET || error == WSAENOTCONN || error == WSAESHUTDOWN
4966 }
else if (bs == -1) {
4973 }
else if (bs == -2) {
4978 }
else if (bs == 0) {
5001 char* errmsg = NULL;
5003#if defined(NETWORK_DISABLE_ZERO_LENGTH_RECV) && (NETWORK_DISABLE_ZERO_LENGTH_RECV == TRUE)
5011 char* tmp_buf = buf;
5022 }
else if (br == 0) {
5026 }
else if (error == ECONNRESET || error == ENOTCONN
5028 || error == WSAECONNRESET || error == WSAENOTCONN || error == WSAESHUTDOWN
5034 }
else if (br == -1) {
5041 }
else if (br == -2) {
5072 ssize_t bs = send(s, buf, NETW_BUFLEN_CAST(n), NETFLAGS);
5074 if (bs > 0)
return bs;
5082 || error == WSAEINTR
5086 if (error == EAGAIN || error == EWOULDBLOCK
5088 || error == WSAEWOULDBLOCK
5092 if (error == ECONNRESET || error == ENOTCONN || error == EPIPE
5094 || error == WSAECONNRESET || error == WSAENOTCONN || error == WSAESHUTDOWN
5128 ssize_t br = recv(s, buf, NETW_BUFLEN_CAST(n), NETFLAGS);
5130 if (br > 0)
return br;
5138 || error == WSAEINTR
5142 if (error == EAGAIN || error == EWOULDBLOCK
5144 || error == WSAEWOULDBLOCK
5148 if (error == ECONNRESET || error == ENOTCONN
5150 || error == WSAECONNRESET || error == WSAENOTCONN
5183 char* errmsg = NULL;
5197#if OPENSSL_VERSION_NUMBER < 0x1010107fL
5198 int status = SSL_write(ssl, buf, (
int)(n - bcount));
5199 bs = (status > 0) ? (
size_t)status : 0;
5201 int status = SSL_write_ex(ssl, buf, (
size_t)(n - bcount), &bs);
5205 bcount += (ssize_t)bs;
5208 int ssl_error = SSL_get_error(ssl, status);
5209 if (ssl_error == SSL_ERROR_WANT_READ || ssl_error == SSL_ERROR_WANT_WRITE) {
5228 if (ssl_error == SSL_ERROR_ZERO_RETURN ||
5229 (ssl_error == SSL_ERROR_SYSCALL &&
5230 (error == 0 || error == EPIPE || error == ECONNRESET || error == ENOTCONN
5232 || error == WSAECONNRESET || error == WSAENOTCONN || error == WSAESHUTDOWN
5239 switch (ssl_error) {
5240 case SSL_ERROR_SYSCALL:
5280 char* errmsg = NULL;
5288 while (bcount < n) {
5292#if OPENSSL_VERSION_NUMBER < 0x10101000L
5293 int status = SSL_read(ssl, buf, (
int)(n - bcount));
5294 br = (status > 0) ? (
size_t)status : 0;
5296 int status = SSL_read_ex(ssl, buf, (
size_t)(n - bcount), &br);
5300 bcount += (ssize_t)br;
5303 int ssl_error = SSL_get_error(ssl, status);
5304 if (ssl_error == SSL_ERROR_WANT_READ || ssl_error == SSL_ERROR_WANT_WRITE) {
5320 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
5325 if (ssl_error == SSL_ERROR_SYSCALL && (error == 0 || error == ECONNRESET || error == ENOTCONN
5327 || error == WSAECONNRESET || error == WSAENOTCONN || error == WSAESHUTDOWN
5338 unsigned long ssl_reason = ERR_get_error();
5340#ifdef SSL_R_UNEXPECTED_EOF_WHILE_READING
5341 if (ssl_error == SSL_ERROR_SSL && ERR_GET_REASON(ssl_reason) == SSL_R_UNEXPECTED_EOF_WHILE_READING) {
5350 switch (ssl_error) {
5351 case SSL_ERROR_SYSCALL:
5411#if OPENSSL_VERSION_NUMBER < 0x1010107fL
5412 int status = SSL_write(ssl, buf, (
int)n);
5413 bs = (status > 0) ? (
size_t)status : 0;
5415 int status = SSL_write_ex(ssl, buf, (
size_t)n, &bs);
5418 if (status > 0)
return (ssize_t)bs;
5420 int ssl_error = SSL_get_error(ssl, status);
5421 switch (ssl_error) {
5422 case SSL_ERROR_WANT_READ:
5424 case SSL_ERROR_WANT_WRITE:
5426 case SSL_ERROR_ZERO_RETURN:
5429 case SSL_ERROR_SYSCALL:
5430 if (error == 0 || error == ECONNRESET || error == EPIPE || error == ENOTCONN
5432 || error == WSAECONNRESET || error == WSAENOTCONN
5438 if (error == EINTR || error == EAGAIN || error == EWOULDBLOCK
5440 || error == WSAEINTR || error == WSAEWOULDBLOCK
5455 unsigned long ssl_reason = ERR_get_error();
5492#if OPENSSL_VERSION_NUMBER < 0x10101000L
5493 int status = SSL_read(ssl, buf, (
int)n);
5494 br = (status > 0) ? (
size_t)status : 0;
5496 int status = SSL_read_ex(ssl, buf, (
size_t)n, &br);
5499 if (status > 0)
return (ssize_t)br;
5501 int ssl_error = SSL_get_error(ssl, status);
5502 switch (ssl_error) {
5503 case SSL_ERROR_WANT_READ:
5505 case SSL_ERROR_WANT_WRITE:
5507 case SSL_ERROR_ZERO_RETURN:
5510 case SSL_ERROR_SYSCALL:
5511 if (error == 0 || error == ECONNRESET || error == ENOTCONN
5513 || error == WSAECONNRESET || error == WSAENOTCONN
5519 if (error == EINTR || error == EAGAIN || error == EWOULDBLOCK
5521 || error == WSAEINTR || error == WSAEWOULDBLOCK
5537 unsigned long ssl_reason = ERR_get_error();
5539#ifdef SSL_R_UNEXPECTED_EOF_WHILE_READING
5540 if (ssl_error == SSL_ERROR_SSL && ERR_GET_REASON(ssl_reason) == SSL_R_UNEXPECTED_EOF_WHILE_READING) {
5566 char* errmsg = NULL;
5582 bs = send(s, ptr, NETW_BUFLEN_CAST(
HEAD_SIZE - bcount), NETFLAGS);
5601 bs = send(s, ptr, NETW_BUFLEN_CAST(
HEAD_CODE - bcount), NETFLAGS);
5618 bs = send(s, buf, NETW_BUFLEN_CAST(n - bcount), NETFLAGS);
5646 long int tmpnb = 0, size = 0;
5650 char* errmsg = NULL;
5660 br = recv(s, ptr, NETW_BUFLEN_CAST(
HEAD_SIZE - bcount), NETFLAGS);
5674 tmpnb = strtol(head, NULL, 10);
5675 if (tmpnb == LONG_MIN || tmpnb == LONG_MAX) {
5687 br = recv(s, ptr, NETW_BUFLEN_CAST(
HEAD_CODE - bcount), NETFLAGS);
5701 tmpnb = strtol(code, NULL, 10);
5702 if (tmpnb <= INT_MIN || tmpnb >= INT_MAX) {
5707 (*_code) = (int)tmpnb;
5714 Malloc((*buf),
char, (
size_t)(size + 1));
5722 while (bcount < size) {
5724 br = recv(s, ptr, NETW_BUFLEN_CAST(size - bcount), NETFLAGS);
5751 pthread_mutex_lock(&
netw->sendbolt);
5752 (*nb_to_send) =
netw->send_buf->nb_items;
5753 pthread_mutex_unlock(&
netw->sendbolt);
5755 pthread_mutex_lock(&
netw->recvbolt);
5756 (*nb_to_read) =
netw->recv_buf->nb_items;
5757 pthread_mutex_unlock(&
netw->recvbolt);
5787 __n_assert(netw_pool && (*netw_pool),
return FALSE);
5790 if ((*netw_pool)->pool)
5792 unlock((*netw_pool)->rwlock);
5840 if ((retval =
list_push(
netw->pools, netw_pool, NULL)) == TRUE) {
5958 N_STR* tmpstr = NULL;
5977 N_STR* tmpstr = NULL;
6001 N_STR* tmpstr = NULL;
6023 N_STR* tmpstr = NULL;
6043 N_STR* tmpstr = NULL;
6061 N_STR* tmpstr = NULL;
6078 size_t encoded_size = 0;
6080 for (
size_t i = 0; i < len; i++) {
6081 unsigned char c = (
unsigned char)str[i];
6082 if (isalnum(c) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
6089 return encoded_size;
6101 static const char* hex =
"0123456789ABCDEF";
6103 char* encoded = (
char*)malloc(encoded_size + 1);
6109 char* pbuf = encoded;
6111 for (
size_t i = 0; i < len; i++) {
6112 unsigned char c = (
unsigned char)str[i];
6113 if (isalnum(c) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
6117 *pbuf++ = hex[c >> 4];
6118 *pbuf++ = hex[c & 0xF];
6134 const char* space = strchr(request,
' ');
6136 if (space == NULL) {
6142 size_t method_length = (size_t)(space - request);
6145 char* method = (
char*)malloc(method_length + 1);
6147 if (method == NULL) {
6153 strncpy(method, request, method_length);
6154 method[method_length] =
'\0';
6165 NETWORK_HTTP_INFO info = {.content_length = 0, .body = NULL, .type = NULL, .content_type = {0}};
6177 const char* content_type_header = strstr(request,
"Content-Type:");
6178 if (content_type_header) {
6179 const char*
start = content_type_header + strlen(
"Content-Type: ");
6180 const char* end = strstr(
start,
"\r\n");
6182 size_t length = (size_t)(end -
start);
6183 if (length > 255) length = 255;
6193 const char* content_length_header = strstr(request,
"Content-Length:");
6194 if (content_length_header) {
6195 const char*
start = content_length_header + strlen(
"Content-Length: ");
6197 unsigned long tmp_cl = strtoul(
start, NULL, 10);
6201#if ULONG_MAX > SIZE_MAX
6202 if (error == ERANGE || tmp_cl > SIZE_MAX) {
6204 if (error == ERANGE) {
6206 n_log(
LOG_ERR,
"could not get content_length for request %p, returned %s", request, strerror(error));
6214 const char* body_start = strstr(request,
"\r\n\r\n");
6228 info.
type = request_type;
6251 __n_assert(request && strlen(request) > 0,
return FALSE);
6255 strncpy(url,
"/", size - 1);
6256 url[size - 1] =
'\0';
6259 const char* first_space = strchr(request,
' ');
6265 const char* second_space = strchr(first_space + 1,
' ');
6266 if (!second_space) {
6271 size_t len = (size_t)(second_space - first_space - 1);
6276 strncpy(url, first_space + 1, len);
6289 char* decoded = malloc(strlen(str) + 1);
6295 if (isxdigit((
unsigned char)str[1]) && isxdigit((
unsigned char)str[2])) {
6297 if (sscanf(str + 1,
"%2x", &value) >= 1) {
6301 n_log(
LOG_ERR,
"sscanf could not parse char *str (%p) for a %%2x", str);
6308 }
else if (*str ==
'+') {
6328 char* data = strdup(post_data);
6335 while (pair != NULL) {
6337 char* ampersand_pos = strchr(pair,
'&');
6340 if (ampersand_pos != NULL) {
6341 *ampersand_pos =
'\0';
6345 char* equal_pos = strchr(pair,
'=');
6346 if (equal_pos != NULL) {
6348 const char*
key = pair;
6349 const char* value = equal_pos + 1;
6355 free(decoded_value);
6358 pair = (ampersand_pos != NULL) ? (ampersand_pos + 1) : NULL;
6362 return post_data_table;
6374 char url_copy[1024];
6375 strncpy(url_copy, url,
sizeof(url_copy) - 1);
6376 url_copy[
sizeof(url_copy) - 1] =
'\0';
6379 char* query_start = strchr(url_copy,
'?');
6381 *query_start =
'\0';
6385 const char* ext = strrchr(url_copy,
'.');
6393 if (strcmp(ext,
".html") == 0 || strcmp(ext,
".htm") == 0) {
6395 }
else if (strcmp(ext,
".txt") == 0) {
6396 return "text/plain";
6397 }
else if (strcmp(ext,
".jpg") == 0 || strcmp(ext,
".jpeg") == 0) {
6398 return "image/jpeg";
6399 }
else if (strcmp(ext,
".png") == 0) {
6401 }
else if (strcmp(ext,
".gif") == 0) {
6403 }
else if (strcmp(ext,
".css") == 0) {
6405 }
else if (strcmp(ext,
".js") == 0) {
6406 return "application/javascript";
6407 }
else if (strcmp(ext,
".json") == 0) {
6408 return "application/json";
6409 }
else if (strcmp(ext,
".xml") == 0) {
6410 return "application/xml";
6411 }
else if (strcmp(ext,
".pdf") == 0) {
6412 return "application/pdf";
6413 }
else if (strcmp(ext,
".zip") == 0) {
6414 return "application/zip";
6415 }
else if (strcmp(ext,
".mp4") == 0) {
6417 }
else if (strcmp(ext,
".mp3") == 0) {
6418 return "audio/mpeg";
6419 }
else if (strcmp(ext,
".wav") == 0) {
6421 }
else if (strcmp(ext,
".ogg") == 0) {
6435 switch (status_code) {
6439 return "No Content";
6441 return "Not Modified";
6445 return "Internal Server Error";
6453 if (status_code < 100 || status_code > 599)
6455 return status_code / 100;
6483 size_t alloc, total = 0;
6487 memset(&strm, 0,
sizeof(strm));
6488 if (inflateInit2(&strm, window_bits) != Z_OK)
6493 buf = malloc(alloc);
6498 strm.next_in = (Bytef*)src;
6499 strm.avail_in = (uInt)len;
6501 if (total >= alloc - 1) {
6504 tmp = realloc(buf, alloc);
6512 strm.next_out = (Bytef*)(buf + total);
6513 strm.avail_out = (uInt)(alloc - total - 1);
6514 ret = inflate(&strm, Z_NO_FLUSH);
6515 if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR || ret == Z_NEED_DICT) {
6520 total = alloc - 1 - strm.avail_out;
6521 }
while (ret != Z_STREAM_END);
6537 int is_gzip = 0, is_deflate = 0;
6545 for (i = 0; i <
sizeof(enc) - 1 && encoding[i]; i++)
6546 enc[i] = (
char)tolower((
unsigned char)encoding[i]);
6549 if (strstr(enc,
"gzip"))
6551 else if (strstr(enc,
"deflate"))
6555 if ((!is_gzip && !is_deflate) || !body->
data || body->
written == 0) {
6572static long mp_find(
const unsigned char* hay,
size_t hlen,
const char* needle,
size_t nlen) {
6574 if (nlen == 0 || nlen > hlen)
6576 for (i = 0; i + nlen <= hlen; i++) {
6577 if (hay[i] == (
unsigned char)needle[0] && memcmp(hay + i, needle, nlen) == 0)
6586 Malloc(r,
char, len + 1);
6598 if (!content_type || !out || outsz == 0)
6601 for (p = content_type; *p; p++) {
6602 if (strncasecmp(p,
"boundary=", 9) == 0)
6611 while (*p && *p !=
'"' && i + 1 < outsz)
6614 while (*p && *p !=
';' && *p !=
' ' && *p !=
'\r' && *p !=
'\n' && i + 1 < outsz)
6618 return i > 0 ? 0 : -1;
6636 if (parts && *parts)
6642static const char*
mp_header_value(
const char* block,
size_t blen,
const char* name,
size_t* vlen) {
6643 size_t nlen = strlen(name);
6647 size_t line_end = i;
6648 while (line_end < blen && block[line_end] !=
'\n')
6651 if (line_end - i > nlen + 1 && strncasecmp(block + i, name, nlen) == 0 && block[i + nlen] ==
':') {
6652 const char* v = block + i + nlen + 1;
6653 size_t vl = line_end - (i + nlen + 1);
6654 while (vl > 0 && (*v ==
' ' || *v ==
'\t')) {
6658 while (vl > 0 && (v[vl - 1] ==
'\r' || v[vl - 1] ==
' ' || v[vl - 1] ==
'\t'))
6673 snprintf(
key,
sizeof(
key),
"%s=", param);
6675 for (i = 0; i + klen <= dlen; i++) {
6676 if (strncasecmp(disp + i,
key, klen) == 0) {
6677 const char* v = disp + i + klen;
6678 size_t avail = dlen - (i + klen);
6680 if (avail > 0 && *v ==
'"') {
6683 while (j < avail && v[j] !=
'"')
6686 while (j < avail && v[j] !=
';' && v[j] !=
' ')
6697 char crlf_dash[260];
6698 const unsigned char* data;
6699 size_t len, dlen, cdlen, cursor;
6703 if (!body || !body->
data || !boundary || !boundary[0])
6705 if (strlen(boundary) + 2 >=
sizeof(dash))
6707 snprintf(dash,
sizeof(dash),
"--%s", boundary);
6708 snprintf(crlf_dash,
sizeof(crlf_dash),
"\r\n--%s", boundary);
6709 dlen = strlen(dash);
6710 cdlen = strlen(crlf_dash);
6716 data = (
const unsigned char*)body->
data;
6718 first =
mp_find(data, len, dash, dlen);
6721 cursor = (size_t)first + dlen;
6725 size_t part_start, part_end;
6729 if (cursor + 2 <= len && data[cursor] ==
'-' && data[cursor + 1] ==
'-')
6732 if (cursor + 2 <= len && data[cursor] ==
'\r' && data[cursor + 1] ==
'\n')
6734 else if (cursor < len && data[cursor] ==
'\n')
6736 part_start = cursor;
6737 rel =
mp_find(data + part_start, len - part_start, crlf_dash, cdlen);
6740 part_end = part_start + (size_t)rel;
6751 long s =
mp_find(data + part_start, part_end - part_start,
"\r\n\r\n", 4);
6753 size_t hlen = (size_t)s;
6754 size_t bbeg = part_start + (size_t)s + 4;
6755 const char* hblock = (
const char*)(data + part_start);
6756 const char* hv =
mp_header_value(hblock, hlen,
"Content-Disposition", &hvl);
6765 size_t blen = part_end - bbeg;
6769 memcpy(mp->
body->
data, data + bbeg, blen);
6786 cursor = part_end + cdlen;
6792static void mp_put(
char* buf,
size_t* pos,
const void* src,
size_t n) {
6794 memcpy(buf + *pos, src, n);
6799static void mp_puts(
char* buf,
size_t* pos,
const char* s) {
6801 mp_put(buf, pos, s, strlen(s));
6805static void mp_emit(
char* buf,
size_t* pos,
LIST* parts,
const char* dash) {
6811 mp_puts(buf, pos,
"\r\nContent-Disposition: form-data; name=\"");
6815 mp_puts(buf, pos,
"; filename=\"");
6823 mp_puts(buf, pos,
"Content-Type: ");
6841 if (!parts || !boundary || !boundary[0])
6843 if (strlen(boundary) + 2 >=
sizeof(dash))
6845 snprintf(dash,
sizeof(dash),
"--%s", boundary);
6847 mp_emit(NULL, &total, parts, dash);
6848 Malloc(buf,
char, total + 1);
6852 mp_emit(buf, &total, parts, dash);
6867 const time_t now = time(NULL);
6870 if (gmtime_s(&gmt, &now) != 0) {
6875 if (!gmtime_r(&now, &gmt)) {
6880 if (strftime(buffer, buffer_size,
"%a, %d %b %Y %H:%M:%S GMT", &gmt) == 0) {
6900 __n_assert(additional_headers,
return FALSE);
6903 const char* connection_type =
"close";
6906 char date_buffer[128] =
"";
6909 if ((*http_response)) {
6910 (*http_response)->written = 0;
6913 if (!body || body->
written == 0) {
6916 "HTTP/1.1 %d %s\r\n"
6919 "Content-Length: 0\r\n"
6921 "Connection: %s\r\n\r\n",
6922 status_code, status_message, date_buffer, server_name, additional_headers, connection_type);
6927 "HTTP/1.1 %d %s\r\n"
6930 "Content-Type: %s\r\n"
6931 "Content-Length: %zu\r\n"
6933 "Connection: %s\r\n\r\n",
6934 status_code, status_message, date_buffer, server_name, content_type, body->
written, additional_headers, connection_type);
6935 nstrcat((*http_response), body);
6952 int ret = SSL_write(conn->
netw->
ssl, buf, (
int)len);
6953 return (ret > 0) ? (ssize_t)ret : -1;
6955 ssize_t ret = send(conn->
netw->
link.
sock, buf, NETW_BUFLEN_CAST(len), NETFLAGS);
6968 char* p = (
char*)buf;
6969 while (total < len) {
6972 ret = SSL_read(conn->
netw->
ssl, p + total, (
int)(len - total));
6974 ret = recv(conn->
netw->
link.
sock, p + total, NETW_BUFLEN_CAST(len - total), 0);
6979 total += (size_t)ret;
6981 return (ssize_t)total;
7006 conn->
host = strdup(host);
7007 conn->
path = strdup(path);
7010 goto ws_connect_fail;
7016 n_log(
LOG_ERR,
"n_ws_connect: TCP+SSL context setup failed for %s:%s", host,
port);
7017 goto ws_connect_fail;
7021 n_log(
LOG_ERR,
"n_ws_connect: SSL handshake failed for %s:%s", host,
port);
7022 goto ws_connect_fail;
7027 goto ws_connect_fail;
7032 unsigned char rand_bytes[16];
7033 if (RAND_bytes(rand_bytes, 16) != 1) {
7036 goto ws_connect_fail;
7041 memcpy(rand_nstr->
data, rand_bytes, 16);
7046 __n_assert(ws_key_nstr,
goto ws_connect_fail);
7048 while (ws_key_nstr->
written > 0 &&
7049 (ws_key_nstr->
data[ws_key_nstr->
written - 1] ==
'\n' ||
7050 ws_key_nstr->
data[ws_key_nstr->
written - 1] ==
'\r' ||
7051 ws_key_nstr->
data[ws_key_nstr->
written - 1] ==
' ')) {
7056 char upgrade_req[2048];
7057 int req_len = snprintf(upgrade_req,
sizeof(upgrade_req),
7058 "GET %s HTTP/1.1\r\n"
7060 "Upgrade: websocket\r\n"
7061 "Connection: Upgrade\r\n"
7062 "Sec-WebSocket-Key: %s\r\n"
7063 "Sec-WebSocket-Version: 13\r\n"
7065 path, host, ws_key_nstr->
data);
7067 if (req_len < 0 || (
size_t)req_len >=
sizeof(upgrade_req)) {
7069 n_log(
LOG_ERR,
"n_ws_connect: upgrade request too large");
7071 goto ws_connect_fail;
7075 if (
_ws_write(conn, upgrade_req, (
size_t)req_len) < 0) {
7077 n_log(
LOG_ERR,
"n_ws_connect: failed to send upgrade request");
7079 goto ws_connect_fail;
7083 char resp_buf[4096];
7084 memset(resp_buf, 0,
sizeof(resp_buf));
7085 size_t resp_len = 0;
7086 while (resp_len <
sizeof(resp_buf) - 1) {
7089 r = SSL_read(conn->
netw->
ssl, resp_buf + resp_len, 1);
7091 r = recv(conn->
netw->
link.
sock, resp_buf + resp_len, 1, 0);
7095 n_log(
LOG_ERR,
"n_ws_connect: failed reading handshake response");
7097 goto ws_connect_fail;
7099 resp_len += (size_t)r;
7100 if (resp_len >= 4 &&
7101 resp_buf[resp_len - 4] ==
'\r' && resp_buf[resp_len - 3] ==
'\n' &&
7102 resp_buf[resp_len - 2] ==
'\r' && resp_buf[resp_len - 1] ==
'\n') {
7106 resp_buf[resp_len] =
'\0';
7109 if (strstr(resp_buf,
"101") == NULL) {
7111 n_log(
LOG_ERR,
"n_ws_connect: server did not return 101: %.128s", resp_buf);
7113 goto ws_connect_fail;
7117 static const char ws_magic[] =
"258EAFA5-E914-47DA-95CA-5AB5FE44F513";
7118 char concat_key[256];
7119 snprintf(concat_key,
sizeof(concat_key),
"%s%s", ws_key_nstr->
data, ws_magic);
7124 unsigned char sha1_hash[SHA_DIGEST_LENGTH];
7125 SHA1((
const unsigned char*)concat_key, strlen(concat_key), sha1_hash);
7129 memcpy(sha1_nstr->
data, sha1_hash, SHA_DIGEST_LENGTH);
7130 sha1_nstr->
written = SHA_DIGEST_LENGTH;
7134 __n_assert(expected_accept,
goto ws_connect_fail);
7136 while (expected_accept->
written > 0 &&
7137 (expected_accept->
data[expected_accept->
written - 1] ==
'\n' ||
7138 expected_accept->
data[expected_accept->
written - 1] ==
'\r' ||
7139 expected_accept->
data[expected_accept->
written - 1] ==
' ')) {
7140 expected_accept->
data[--expected_accept->
written] =
'\0';
7144 const char* accept_hdr = NULL;
7145 char* search_pos = resp_buf;
7146 while (*search_pos) {
7147 if (strncasecmp(search_pos,
"Sec-WebSocket-Accept:", 21) == 0) {
7148 accept_hdr = search_pos + 21;
7155 n_log(
LOG_ERR,
"n_ws_connect: no Sec-WebSocket-Accept header in response");
7157 goto ws_connect_fail;
7160 while (*accept_hdr ==
' ') accept_hdr++;
7162 char accept_val[128];
7165 while (ai < (
int)
sizeof(accept_val) - 1 && accept_hdr[ai] && accept_hdr[ai] !=
'\r' && accept_hdr[ai] !=
'\n') {
7166 accept_val[ai] = accept_hdr[ai];
7169 accept_val[ai] =
'\0';
7171 if (strcmp(accept_val, expected_accept->
data) != 0) {
7174 n_log(
LOG_DEBUG,
"n_ws_connect: Sec-WebSocket-Accept mismatch (proxy?): got [%s] expected [%s]",
7175 accept_val, expected_accept->
data);
7180 n_log(
LOG_INFO,
"n_ws_connect: WebSocket handshake completed with %s:%s%s", host,
port, path);
7204 size_t frame_max = 14 + len;
7205 unsigned char* frame = NULL;
7206 Malloc(frame,
unsigned char, frame_max);
7212 frame[pos++] = (
unsigned char)(0x80 | (opcode & 0x0F));
7216 frame[pos++] = (
unsigned char)(0x80 | len);
7217 }
else if (len <= 65535) {
7218 frame[pos++] = (
unsigned char)(0x80 | 126);
7219 frame[pos++] = (
unsigned char)((len >> 8) & 0xFF);
7220 frame[pos++] = (
unsigned char)(len & 0xFF);
7222 frame[pos++] = (
unsigned char)(0x80 | 127);
7223 for (
int i = 7; i >= 0; i--) {
7224 frame[pos++] = (
unsigned char)((len >> (8 * i)) & 0xFF);
7229 unsigned char mask_key[4];
7230 if (RAND_bytes(mask_key, 4) != 1) {
7232 n_log(
LOG_ERR,
"n_ws_send: RAND_bytes failed for mask key");
7236 memcpy(frame + pos, mask_key, 4);
7240 if (payload && len > 0) {
7241 for (
size_t i = 0; i < len; i++) {
7242 frame[pos + i] = (
unsigned char)((
unsigned char)payload[i] ^ mask_key[i % 4]);
7247 ssize_t written =
_ws_write(conn, frame, pos);
7249 if (written < 0 || (
size_t)written != pos) {
7251 n_log(
LOG_ERR,
"n_ws_send: write failed (wrote %zd of %zu)", written, pos);
7271 memset(msg_out, 0,
sizeof(*msg_out));
7274 unsigned char hdr[2];
7280 int opcode = hdr[0] & 0x0F;
7281 int mask_bit = (hdr[1] >> 7) & 1;
7282 uint64_t payload_len = hdr[1] & 0x7F;
7285 if (payload_len == 126) {
7286 unsigned char ext[2];
7287 if (
_ws_read(conn, ext, 2) < 0)
return -1;
7288 payload_len = ((uint64_t)ext[0] << 8) | (uint64_t)ext[1];
7289 }
else if (payload_len == 127) {
7290 unsigned char ext[8];
7291 if (
_ws_read(conn, ext, 8) < 0)
return -1;
7293 for (
int i = 0; i < 8; i++) {
7294 payload_len = (payload_len << 8) | (uint64_t)ext[i];
7304 n_log(
LOG_ERR,
"n_ws_recv: payload length %llu exceeds N_WS_MAX_PAYLOAD (%zu)", (
unsigned long long)payload_len, (
size_t)
N_WS_MAX_PAYLOAD);
7309 unsigned char mask_key[4] = {0};
7311 if (
_ws_read(conn, mask_key, 4) < 0)
return -1;
7318 if (payload_len > 0) {
7319 if (
_ws_read(conn, payload->
data, (
size_t)payload_len) < 0) {
7325 for (uint64_t i = 0; i < payload_len; i++) {
7326 payload->
data[i] = (char)((
unsigned char)payload->
data[i] ^ mask_key[i % 4]);
7330 payload->
written = (size_t)payload_len;
7331 payload->
data[payload_len] =
'\0';
7333 msg_out->
opcode = opcode;
7335 msg_out->
masked = mask_bit;
7358 memset(&msg, 0,
sizeof(msg));
7416 __atomic_store_n(&conn->
stop_flag, 1, __ATOMIC_RELEASE);
7446 if (stop_flag && __atomic_load_n(stop_flag, __ATOMIC_ACQUIRE)) {
7451 tv.tv_usec = 500000;
7454 FD_SET(
netw->link.sock, &fds);
7459 if (SSL_pending(
netw->ssl) > 0) {
7464 int sel = select((
int)
netw->link.sock + 1, &fds, NULL, NULL, &tv);
7465 if (sel < 0)
return -1;
7466 if (sel == 0)
continue;
7471 ret = SSL_read(
netw->ssl, ch, 1);
7473 ret = recv(
netw->link.sock, ch, 1, 0);
7475 return (ret == 1) ? 1 : -1;
7487 int ret = SSL_write(
netw->ssl, buf, (
int)len);
7488 return (ret > 0) ? (ssize_t)ret : -1;
7490 ssize_t ret = send(
netw->link.sock, buf, NETW_BUFLEN_CAST(len), NETFLAGS);
7526 n_log(
LOG_ERR,
"n_sse_connect: TCP+SSL context setup failed for %s:%s", host,
port);
7527 goto sse_connect_fail;
7531 n_log(
LOG_ERR,
"n_sse_connect: SSL handshake failed for %s:%s", host,
port);
7532 goto sse_connect_fail;
7537 goto sse_connect_fail;
7544 if (user_agent && user_agent[0]) {
7545 req_len = snprintf(request,
sizeof(request),
7546 "GET %s HTTP/1.1\r\n"
7548 "Accept: text/event-stream\r\n"
7549 "Cache-Control: no-cache\r\n"
7550 "Connection: keep-alive\r\n"
7551 "User-Agent: %s\r\n"
7553 path, host, user_agent);
7555 req_len = snprintf(request,
sizeof(request),
7556 "GET %s HTTP/1.1\r\n"
7558 "Accept: text/event-stream\r\n"
7559 "Cache-Control: no-cache\r\n"
7560 "Connection: keep-alive\r\n"
7565 if (req_len < 0 || (
size_t)req_len >=
sizeof(request)) {
7568 goto sse_connect_fail;
7573 n_log(
LOG_ERR,
"n_sse_connect: failed to send HTTP request");
7574 goto sse_connect_fail;
7578 char resp_buf[8192];
7579 size_t resp_len = 0;
7580 while (resp_len <
sizeof(resp_buf) - 1) {
7584 n_log(
LOG_ERR,
"n_sse_connect: failed reading HTTP response");
7585 goto sse_connect_fail;
7587 resp_buf[resp_len++] = ch;
7588 if (resp_len >= 4 &&
7589 resp_buf[resp_len - 4] ==
'\r' && resp_buf[resp_len - 3] ==
'\n' &&
7590 resp_buf[resp_len - 2] ==
'\r' && resp_buf[resp_len - 1] ==
'\n') {
7594 resp_buf[resp_len] =
'\0';
7597 if (strstr(resp_buf,
"200") == NULL) {
7599 n_log(
LOG_ERR,
"n_sse_connect: server did not return 200: %.128s", resp_buf);
7600 goto sse_connect_fail;
7608 memset(¤t, 0,
sizeof(current));
7612 size_t line_len = 0;
7614 while (!__atomic_load_n(&conn->
stop_flag, __ATOMIC_ACQUIRE)) {
7617 n_log(
LOG_DEBUG,
"n_sse_connect: connection closed or read error");
7623 if (line_len > 0 &&
line[line_len - 1] ==
'\r') {
7626 line[line_len] =
'\0';
7628 if (line_len == 0) {
7633 memset(¤t, 0,
sizeof(current));
7635 }
else if (
line[0] ==
':') {
7640 char* colon = strchr(
line,
':');
7641 const char* field =
line;
7642 const char* value =
"";
7647 if (*value ==
' ') value++;
7650 if (strcmp(field,
"data") == 0) {
7657 }
else if (strcmp(field,
"event") == 0) {
7660 }
else if (strcmp(field,
"id") == 0) {
7663 }
else if (strcmp(field,
"retry") == 0) {
7664 current.
retry = atoi(value);
7670 if (line_len <
sizeof(
line) - 1) {
7671 line[line_len++] = ch;
7691#define N_WS_FRAME_MAX_PAYLOAD (16U * 1024U * 1024U)
7706 if (!buf || !frame || !consumed)
7710 frame->
fin = (buf[0] & 0x80) ? 1 : 0;
7711 frame->
rsv = (buf[0] >> 4) & 0x07;
7712 frame->
opcode = buf[0] & 0x0F;
7713 frame->
masked = (buf[1] & 0x80) ? 1 : 0;
7714 len7 = buf[1] & 0x7F;
7716 plen = (uint64_t)len7;
7717 }
else if (len7 == 126) {
7720 plen = ((uint64_t)buf[2] << 8) | (uint64_t)buf[3];
7727 for (i = 0; i < 8; i++)
7728 plen = (plen << 8) | (uint64_t)buf[2 + i];
7736 memcpy(frame->
mask, buf + hdr, 4);
7739 memset(frame->
mask, 0, 4);
7741 if (len < hdr + plen)
7745 *consumed = hdr + (size_t)plen;
7749void n_ws_unmask(
unsigned char* dst,
const unsigned char* src,
size_t len,
const unsigned char mask[4]) {
7751 if (!dst || !src || !mask)
7753 for (i = 0; i < len; i++)
7754 dst[i] = (
unsigned char)(src[i] ^ mask[i & 3]);
7757size_t n_ws_frame_build(
int fin,
int opcode,
int do_mask,
const unsigned char* payload,
size_t len,
const unsigned char mask_key[4],
unsigned char* out,
size_t out_cap) {
7759 unsigned char b1 = (
unsigned char)(do_mask ? 0x80 : 0);
7760 if (!out || out_cap < 2)
7762 out[pos++] = (
unsigned char)((fin ? 0x80 : 0) | (opcode & 0x0F));
7764 out[pos++] = (
unsigned char)(b1 | (
unsigned char)len);
7765 }
else if (len <= 65535) {
7766 if (out_cap < pos + 3)
7768 out[pos++] = (
unsigned char)(b1 | 126);
7769 out[pos++] = (
unsigned char)((len >> 8) & 0xFF);
7770 out[pos++] = (
unsigned char)(len & 0xFF);
7773 if (out_cap < pos + 9)
7775 out[pos++] = (
unsigned char)(b1 | 127);
7776 for (i = 7; i >= 0; i--)
7777 out[pos++] = (
unsigned char)((len >> (8 * i)) & 0xFF);
7780 if (!mask_key || out_cap < pos + 4)
7782 memcpy(out + pos, mask_key, 4);
7785 if (out_cap < pos + len)
7787 if (do_mask && mask_key) {
7789 for (i = 0; i < len; i++)
7790 out[pos + i] = (
unsigned char)((payload ? payload[i] : 0) ^ mask_key[i & 3]);
7791 }
else if (payload && len > 0) {
7792 memcpy(out + pos, payload, len);
7804 if (!buf || !req)
return;
7807 const char* line_end = strstr(buf,
"\r\n");
7808 if (!line_end) line_end = strchr(buf,
'\n');
7809 if (!line_end)
return;
7811 size_t line_len = (size_t)(line_end - buf);
7813 if (line_len >=
sizeof(
line)) line_len =
sizeof(
line) - 1;
7814 memcpy(
line, buf, line_len);
7815 line[line_len] =
'\0';
7818 char* sp1 = strchr(
line,
' ');
7820 size_t mlen = (size_t)(sp1 -
line);
7821 if (mlen >=
sizeof(req->
method)) mlen =
sizeof(req->
method) - 1;
7823 req->
method[mlen] =
'\0';
7827 char* sp2 = strchr(sp1,
' ');
7828 if (sp2) *sp2 =
'\0';
7829 char* qmark = strchr(sp1,
'?');
7832 strncpy(req->
query, qmark + 1,
sizeof(req->
query) - 1);
7835 strncpy(req->
path, sp1,
sizeof(req->
path) - 1);
7836 req->
path[
sizeof(req->
path) - 1] =
'\0';
7839 const char* hdr_start = line_end;
7840 if (*hdr_start ==
'\r') hdr_start++;
7841 if (*hdr_start ==
'\n') hdr_start++;
7845 const char* body_start = NULL;
7846 while (hdr_start && *hdr_start) {
7847 const char* next = strstr(hdr_start,
"\r\n");
7848 if (!next) next = strchr(hdr_start,
'\n');
7851 size_t hlen = (size_t)(next - hdr_start);
7855 if (*body_start ==
'\r') body_start++;
7856 if (*body_start ==
'\n') body_start++;
7860 char* header_line = strndup(hdr_start, hlen);
7866 if (*hdr_start ==
'\r') hdr_start++;
7867 if (*hdr_start ==
'\n') hdr_start++;
7871 if (body_start && *body_start) {
7904 server->user_data = user_data;
7917 server->listener = listener;
7931 while (!__atomic_load_n(&
server->stop_flag, __ATOMIC_ACQUIRE)) {
7935 FD_SET(
server->listener->link.sock, &readfds);
7938 tv.tv_usec = 200000;
7939 int sel = select((
int)(
server->listener->link.sock + 1), &readfds, NULL, NULL, &tv);
7940 if (sel <= 0)
continue;
7949 ssize_t n = recv(
client->link.sock, buf,
sizeof(buf) - 1, 0);
7958 memset(&req, 0,
sizeof(req));
7963 memset(&resp, 0,
sizeof(resp));
7972 if (!status_msg) status_msg =
"Unknown";
7974 size_t body_len = 0;
7975 const char* body_data =
"";
7981 char header_buf[1024];
7982 int hlen = snprintf(header_buf,
sizeof(header_buf),
7983 "HTTP/1.1 %d %s\r\n"
7984 "Content-Type: %s\r\n"
7985 "Content-Length: %zu\r\n"
7986 "Connection: close\r\n"
7993 send(
client->link.sock, header_buf, NETW_BUFLEN_CAST(hlen), NETFLAGS);
7996 send(
client->link.sock, body_data, NETW_BUFLEN_CAST(body_len), NETFLAGS);
8014 __atomic_store_n(&
server->stop_flag, 1, __ATOMIC_RELEASE);
8023 if ((*server)->listener) {
8031 if (!u || !qs || !qs[0])
return;
8032 char* buf = strdup(qs);
8034 char* saveptr = NULL;
8038 char* tok = strtok_r(buf,
"&", &saveptr);
8040 char*
eq = strchr(tok,
'=');
8050 tok = strtok_r(NULL,
"&", &saveptr);
8056 if (!url)
return NULL;
8059 if (!u)
return NULL;
8060 memset(u, 0,
sizeof(*u));
8061 const char* p = url;
8062 const char* scheme_end = strstr(p,
"://");
8064 u->
scheme = strndup(p, (
size_t)(scheme_end - p));
8067 u->
scheme = strdup(
"http");
8069 const char* host_end = p;
8070 while (*host_end && *host_end !=
'/' && *host_end !=
'?' && *host_end !=
':') host_end++;
8071 u->
host = strndup(p, (
size_t)(host_end - p));
8076 while (*p && *p !=
'/' && *p !=
'?') p++;
8080 while (*pe && *pe !=
'?') pe++;
8081 u->
path = strndup(p, (
size_t)(pe - p));
8084 u->
path = strdup(
"/");
8088 u->
query = strdup(p);
8095 if (!u)
return NULL;
8097 if (!result)
return NULL;
8100 int dp = (u->
scheme && strcmp(u->
scheme,
"https") == 0) ? 443 : 80;
8115 const char* seg_starts[256];
8116 size_t seg_lens[256];
8118 const char* p = (path && path[0]) ? path :
"/";
8129 slash = strchr(p,
'/');
8130 slen = slash ? (size_t)(slash - p) : strlen(p);
8131 if (slen == 1 && p[0] ==
'.') {
8133 }
else if (slen == 2 && p[0] ==
'.' && p[1] ==
'.') {
8135 }
else if (nb < 256) {
8137 seg_lens[nb] = slen;
8143 if (outsz == 0)
return;
8145 for (i = 0; i < nb && pos < outsz - 1; i++) {
8147 if (i > 0 && pos < outsz - 1) out[pos++] =
'/';
8148 for (k = 0; k < seg_lens[i] && pos < outsz - 1; k++)
8149 out[pos++] = seg_starts[i][k];
8152 if (path && path[0] && path[strlen(path) - 1] ==
'/' && pos < outsz - 1 && (nb > 0))
8167 N_STR* result = NULL;
8172 if (!u)
return NULL;
8173 snprintf(scheme,
sizeof(scheme),
"%s", u->
scheme ? u->
scheme :
"http");
8174 snprintf(host,
sizeof(host),
"%s", u->
host ? u->
host :
"");
8175 for (i = 0; scheme[i]; i++) scheme[i] = (
char)tolower((
unsigned char)scheme[i]);
8176 for (i = 0; host[i]; i++) host[i] = (
char)tolower((
unsigned char)host[i]);
8179 if (!result)
return NULL;
8182 int dp = (strcmp(scheme,
"https") == 0) ? 443 : 80;
8198 if (!u)
return NULL;
8210 if (!s || !isalpha((
unsigned char)s[0]))
return 0;
8212 while (s[i] && (isalnum((
unsigned char)s[i]) || s[i] ==
'+' || s[i] ==
'-' || s[i] ==
'.')) i++;
8234 if (!base)
return NULL;
8237 char* refbuf = strdup(ref ? ref :
"");
8238 if (!refbuf)
return NULL;
8239 char* frag = strchr(refbuf,
'#');
8240 if (frag) *frag =
'\0';
8257 char* refq = strchr(refbuf,
'?');
8258 const char* ref_query = NULL;
8261 ref_query = refq + 1;
8265 char authority[300];
8267 snprintf(authority,
sizeof(authority),
"%s:%d", b->
host ? b->
host :
"", b->
port);
8269 snprintf(authority,
sizeof(authority),
"%s", b->
host ? b->
host :
"");
8273 const char* t_query = NULL;
8275 if (refbuf[0] ==
'\0') {
8277 snprintf(rawpath,
sizeof(rawpath),
"%s", b->
path ? b->
path :
"/");
8278 t_query = refq ? ref_query : b->
query;
8279 }
else if (refbuf[0] ==
'/' && refbuf[1] ==
'/') {
8281 const char* a = refbuf + 2;
8282 const char* slash = strchr(a,
'/');
8284 snprintf(authority,
sizeof(authority),
"%.*s", (
int)(slash - a), a);
8285 snprintf(rawpath,
sizeof(rawpath),
"%s", slash);
8287 snprintf(authority,
sizeof(authority),
"%s", a);
8288 snprintf(rawpath,
sizeof(rawpath),
"/");
8290 t_query = ref_query;
8291 }
else if (refbuf[0] ==
'/') {
8293 snprintf(rawpath,
sizeof(rawpath),
"%s", refbuf);
8294 t_query = ref_query;
8297 const char* bp = (b->
path && b->
path[0]) ? b->
path :
"/";
8298 const char* lastslash = strrchr(bp,
'/');
8299 size_t prefixlen = lastslash ? (size_t)(lastslash - bp) + 1 : 0;
8300 snprintf(rawpath,
sizeof(rawpath),
"%.*s%s", (
int)prefixlen, bp, refbuf);
8301 t_query = ref_query;
8304 char normpath[4096];
8307 N_STR* out =
new_nstr(strlen(authority) +
sizeof(normpath) + 32);
8309 nstrprintf(out,
"%s://%s%s", t_scheme, authority, normpath);
8322 if (!str)
return NULL;
8323 size_t len = strlen(str);
8325 if (!result)
return NULL;
8326 for (
size_t i = 0; i < len; i++) {
8327 if (str[i] ==
'%' && i + 2 < len && isxdigit((
unsigned char)str[i + 1]) && isxdigit((
unsigned char)str[i + 2])) {
8328 const char hex[3] = {str[i + 1], str[i + 2],
'\0'};
8329 unsigned int val = 0;
8330 sscanf(hex,
"%x", &val);
8333 }
else if (str[i] ==
'+') {
8343 if (!u || !*u)
return;
8349 for (
int i = 0; i < p->
nb_params; i++) {
8367 struct addrinfo hints;
8368 memset(&hints, 0,
sizeof(hints));
8369 hints.ai_family = AF_UNSPEC;
8370 hints.ai_socktype = SOCK_STREAM;
8372 struct addrinfo* res = NULL;
8373 int gai_rc = getaddrinfo(host,
port_str, &hints, &res);
8374 if (gai_rc != 0 || !res) {
8375 n_log(
LOG_ERR,
"n_proxy: DNS lookup failed for %s:%d: %s",
8376 host,
port, gai_strerror(gai_rc));
8377 return INVALID_SOCKET;
8380 SOCKET fd = INVALID_SOCKET;
8381 struct addrinfo* rp = NULL;
8382 for (rp = res; rp; rp = rp->ai_next) {
8383 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
8384 if (fd == INVALID_SOCKET)
continue;
8385 if (connect(fd, rp->ai_addr, (socklen_t)rp->ai_addrlen) == 0)
break;
8387 fd = INVALID_SOCKET;
8391 if (fd == INVALID_SOCKET) {
8398 if (!url)
return NULL;
8401 const char* sep = strstr(url,
"://");
8403 n_log(
LOG_ERR,
"n_proxy_cfg_parse: no scheme in '%s'", url);
8407 size_t scheme_len = (size_t)(sep - url);
8408 if (scheme_len == 0 || scheme_len > 16)
return NULL;
8411 memcpy(scheme, url, scheme_len);
8412 scheme[scheme_len] =
'\0';
8415 if (strcmp(scheme,
"http") != 0 && strcmp(scheme,
"https") != 0 && strcmp(scheme,
"socks5") != 0) {
8416 n_log(
LOG_ERR,
"n_proxy_cfg_parse: unsupported scheme '%s'", scheme);
8420 const char* after = sep + 3;
8421 if (!*after)
return NULL;
8424 const char* at = strchr(after,
'@');
8425 const char* host_start = NULL;
8426 char* username = NULL;
8427 char* password = NULL;
8431 const char* colon = strchr(after,
':');
8432 if (colon && colon < at) {
8433 username = strndup(after, (
size_t)(colon - after));
8434 password = strndup(colon + 1, (
size_t)(at - colon - 1));
8436 username = strndup(after, (
size_t)(at - after));
8438 host_start = at + 1;
8445 char* hostname = NULL;
8448 if (host_start[0] ==
'[') {
8449 const char* bracket = strchr(host_start,
']');
8450 if (!bracket)
goto fail;
8451 hostname = strndup(host_start + 1, (
size_t)(bracket - host_start - 1));
8452 if (bracket[1] ==
':') {
8453 port = atoi(bracket + 2);
8456 const char* colon = strchr(host_start,
':');
8458 hostname = strndup(host_start, (
size_t)(colon - host_start));
8459 port = atoi(colon + 1);
8461 hostname = strdup(host_start);
8465 if (!hostname || !hostname[0])
goto fail;
8468 if (strcmp(scheme,
"http") == 0 || strcmp(scheme,
"https") == 0)
8470 else if (strcmp(scheme,
"socks5") == 0)
8476 if (!cfg)
goto fail;
8477 memset(cfg, 0,
sizeof(*cfg));
8478 cfg->
scheme = strdup(scheme);
8479 cfg->
host = hostname;
8493 if (!cfg || !*cfg)
return;
8504 const char* target_host,
8506 if (!proxy || !target_host)
return -1;
8509 if (fd == INVALID_SOCKET)
return -1;
8512 char connect_req[2048];
8515 len = snprintf(connect_req,
sizeof(connect_req),
8516 "CONNECT %s:%d HTTP/1.1\r\n"
8518 target_host, target_port,
8519 target_host, target_port);
8524 snprintf(cred,
sizeof(cred),
"%s:%s",
8529 if (b64 && b64->
data) {
8530 len += snprintf(connect_req + len,
8531 sizeof(connect_req) - (
size_t)len,
8532 "Proxy-Authorization: Basic %s\r\n",
8540 len += snprintf(connect_req + len,
sizeof(connect_req) - (
size_t)len,
8544 ssize_t sent = send(fd, connect_req, NETW_BUFLEN_CAST(len), 0);
8545 if (sent != (ssize_t)len) {
8546 n_log(
LOG_ERR,
"n_proxy_connect_tunnel: send CONNECT failed");
8552 char resp_buf[4096];
8553 ssize_t nr = recv(fd, resp_buf, NETW_BUFLEN_CAST(
sizeof(resp_buf) - 1), 0);
8555 n_log(
LOG_ERR,
"n_proxy_connect_tunnel: no response from proxy");
8559 resp_buf[nr] =
'\0';
8562 if (strstr(resp_buf,
" 200 ") == NULL) {
8563 n_log(
LOG_ERR,
"n_proxy_connect_tunnel: proxy rejected CONNECT: %.*s",
8564 (
int)(strchr(resp_buf,
'\r') ? strchr(resp_buf,
'\r') - resp_buf : nr),
8570 n_log(
LOG_DEBUG,
"n_proxy_connect_tunnel: tunnel established to %s:%d via %s:%d",
8571 target_host, target_port, proxy->
host, proxy->
port);
8577 const char* target_host,
8579 if (!proxy || !target_host)
return -1;
8582 if (fd == INVALID_SOCKET)
return -1;
8587#if OPENSSL_VERSION_NUMBER >= 0x10100000L
8588 const SSL_METHOD* method = TLS_client_method();
8590 const SSL_METHOD* method = TLSv1_2_client_method();
8592 SSL_CTX* ctx = SSL_CTX_new(method);
8594 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: SSL_CTX_new failed");
8598 SSL_CTX_set_default_verify_paths(ctx);
8600 SSL* ssl = SSL_new(ctx);
8602 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: SSL_new failed");
8607 SSL_set_fd(ssl, (
int)fd);
8608 SSL_set_tlsext_host_name(ssl, proxy->
host);
8610 if (SSL_connect(ssl) <= 0) {
8611 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: TLS handshake to proxy %s:%d failed",
8619 n_log(
LOG_DEBUG,
"n_proxy_connect_tunnel_ssl: TLS established to proxy %s:%d",
8623 char connect_req[2048];
8626 len = snprintf(connect_req,
sizeof(connect_req),
8627 "CONNECT %s:%d HTTP/1.1\r\n"
8629 target_host, target_port,
8630 target_host, target_port);
8635 snprintf(cred,
sizeof(cred),
"%s:%s",
8640 if (b64 && b64->
data) {
8641 len += snprintf(connect_req + len,
8642 sizeof(connect_req) - (
size_t)len,
8643 "Proxy-Authorization: Basic %s\r\n",
8651 len += snprintf(connect_req + len,
sizeof(connect_req) - (
size_t)len,
8655 if (SSL_write(ssl, connect_req, len) != len) {
8656 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: send CONNECT failed");
8664 char resp_buf[4096];
8665 int nr = SSL_read(ssl, resp_buf, (
int)(
sizeof(resp_buf) - 1));
8667 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: no response from proxy");
8673 resp_buf[nr] =
'\0';
8676 if (strstr(resp_buf,
" 200 ") == NULL) {
8677 n_log(
LOG_ERR,
"n_proxy_connect_tunnel_ssl: proxy rejected CONNECT: %.*s",
8678 (
int)(strchr(resp_buf,
'\r') ? strchr(resp_buf,
'\r') - resp_buf : nr),
8691 SSL_set_quiet_shutdown(ssl, 1);
8696 n_log(
LOG_DEBUG,
"n_proxy_connect_tunnel_ssl: tunnel established to %s:%d via https://%s:%d",
8697 target_host, target_port, proxy->
host, proxy->
port);
8703 const char* target_host,
8705 if (!proxy || !target_host)
return -1;
8708 if (fd == INVALID_SOCKET)
return -1;
8719 if (send(fd, greeting, 4, 0) != 4)
goto fail;
8724 if (send(fd, greeting, 3, 0) != 3)
goto fail;
8728 char method_resp[2];
8729 if (recv(fd, method_resp, 2, 0) != 2)
goto fail;
8730 if (method_resp[0] != 0x05)
goto fail;
8732 if (method_resp[1] == 0x02 && use_auth) {
8734 size_t ulen = strlen(proxy->
username);
8736 if (ulen > 255 || plen > 255)
goto fail;
8740 auth_req[pos++] = 0x01;
8741 auth_req[pos++] = (char)ulen;
8742 memcpy(auth_req + pos, proxy->
username, ulen);
8744 auth_req[pos++] = (char)plen;
8746 memcpy(auth_req + pos, proxy->
password, plen);
8753 if (send(fd, auth_req, NETW_BUFLEN_CAST(pos), 0) != (ssize_t)pos)
goto fail;
8756 if (recv(fd, auth_resp, 2, 0) != 2)
goto fail;
8757 if (auth_resp[1] != 0x00) {
8758 n_log(
LOG_ERR,
"n_proxy_connect_socks5: auth rejected");
8761 }
else if (method_resp[1] != 0x00) {
8762 n_log(
LOG_ERR,
"n_proxy_connect_socks5: no acceptable method");
8768 size_t hlen = strlen(target_host);
8769 if (hlen > 255)
goto fail;
8773 conn_req[pos++] = 0x05;
8774 conn_req[pos++] = 0x01;
8775 conn_req[pos++] = 0x00;
8776 conn_req[pos++] = 0x03;
8777 conn_req[pos++] = (char)hlen;
8778 memcpy(conn_req + pos, target_host, hlen);
8780 conn_req[pos++] = (char)((target_port >> 8) & 0xFF);
8781 conn_req[pos++] = (char)(target_port & 0xFF);
8783 if (send(fd, conn_req, NETW_BUFLEN_CAST(pos), 0) != (ssize_t)pos)
goto fail;
8790 ssize_t nr = recv(fd, conn_resp, NETW_BUFLEN_CAST(
sizeof(conn_resp)), 0);
8791 if (nr < 4)
goto fail;
8792 if (conn_resp[0] != 0x05 || conn_resp[1] != 0x00) {
8793 n_log(
LOG_ERR,
"n_proxy_connect_socks5: connect failed, reply=%02x",
8799 if (conn_resp[3] == 0x03 && nr < 5) {
8802 recv(fd, extra, NETW_BUFLEN_CAST(
sizeof(extra)), 0);
8803 }
else if (conn_resp[3] == 0x04 && nr < 10) {
8806 size_t need = 22 - (size_t)nr;
8807 if (need <=
sizeof(extra)) {
8808 recv(fd, extra, NETW_BUFLEN_CAST(need), 0);
8813 n_log(
LOG_DEBUG,
"n_proxy_connect_socks5: tunnel established to %s:%d via %s:%d",
8814 target_host, target_port, proxy->
host, proxy->
port);
8818 n_log(
LOG_ERR,
"n_proxy_connect_socks5: handshake failed");
8843 n_log(
LOG_ERR,
"netw_adopt_client_fd: target NETWORK must be empty");
8846 if (fd == INVALID_SOCKET) {
8847 n_log(
LOG_ERR,
"netw_adopt_client_fd: invalid socket");
8851 n_log(
LOG_ERR,
"netw_adopt_client_fd: unable to load WSA dll's");
8857 (*netw)->link.sock = fd;
8858 (*netw)->link.ip = strdup(host ? host :
"");
8859 if (!(*netw)->link.ip) {
8863 (*netw)->link.port = strdup(
port ?
port :
"");
8864 if (!(*netw)->link.port) {
8870 n_log(
LOG_DEBUG,
"netw_adopt_client_fd: adopted socket %d for %s:%s", (
int)fd, (*netw)->link.ip, (*netw)->link.port);
static NETWORK_POOL * pool
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static int on_request(void *ud, N_H2_SERVER *srv, const N_H2_REQUEST *req)
static N_HTTP_SERVER * start(N_HTTP_SERVER_CONFIG *cfg, char *port, int base)
start a server on the first free port from base
a test client connection, plain or TLS
NETWORK * netw
Network for server mode, accepting incomming.
static int eq(double a, double b)
void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color)
draw a line
volatile char key[227]
TRUE while the key is held.
#define init_lock(__rwlock_mutex)
Macro for initializing a rwlock.
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define rw_lock_destroy(__rwlock_mutex)
Macro to destroy rwlock mutex.
#define unlock(__rwlock_mutex)
Macro for releasing read/write lock a rwlock mutex.
#define endif
close a ifwhatever block
#define write_lock(__rwlock_mutex)
Macro for acquiring a write lock on a rwlock mutex.
#define Free(__ptr)
Free Handler to get errors.
#define read_lock(__rwlock_mutex)
Macro for acquiring a read lock on a rwlock mutex.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
#define N_ENUM_DEFINE(MACRO_DEFINITION, enum_name)
Macro to define an N_ENUM.
#define N_ENUM_ENTRY(class, method)
helper to build an N_ENUM
size_t nb_keys
total number of used keys in the table
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
#define ht_foreach(__ITEM_, __HASH_)
ForEach macro helper (classic / old).
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
int ht_remove(HASH_TABLE *table, const char *key)
remove and delete node at key in table
HASH_TABLE * new_ht(size_t size)
Create a hash table with the given size.
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr), void *(*duplicator)(void *ptr))
put an arbitrary pointer value with given key in the targeted hash table
int ht_put_string(HASH_TABLE *table, const char *key, char *string)
put a string value (copy/dup) with given key in the targeted hash table
#define hash_val(node, type)
Cast a HASH_NODE element.
structure of a hash table node
structure of a hash table
#define list_shift(__LIST_, __TYPE_)
Shift macro helper for void pointer casting.
int list_empty(LIST *list)
Empty a LIST list of pointers.
LIST_NODE * list_search(LIST *list, const void *ptr)
search ptr in list
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
#define remove_list_node(__LIST_, __NODE_, __TYPE_)
Remove macro helper for void pointer casting.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Structure of a generic LIST container.
Structure of a generic list node.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
#define N_PRINTF_FORMAT(fmt, args)
declare a function printf-like; nothing on a compiler without attributes
#define LOG_WARNING
warning conditions
#define LOG_INFO
informational
N_STR * zip4_nstr(N_STR *src)
Compress src with LZ4 block format.
N_STR * unzip4_nstr(N_STR *src)
Decompress an N_STR produced by zip4_nstr.
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
size_t length
total allocation (in bytes) of the data buffer, padding included
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
N_STR * n_str_url_encode(const char *src)
Percent-encode a C string per RFC 3986 (unreserved set kept as-is).
size_t NSTRBYTE
N_STR base unit.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrcat(__nstr_dst, __nstr_src)
Macro to quickly concatenate two N_STR.
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
int char_to_nstr_ex(const char *from, NSTRBYTE nboct, N_STR **to)
Convert a char into a N_STR, extended version.
A box including a string and his lenght.
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
N_STR * netmsg_make_position_msg(int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
make a network NETMSG_POSITION message with given parameters
N_STR * netmsg_make_ident(int type, int id, N_STR *name, N_STR *passwd)
Add a formatted NETWMSG_IDENT message to the specified network.
N_STR * netmsg_make_quit_msg(void)
make a generic network NETMSG_QUIT message
N_STR * netmsg_make_ping(int type, int id_from, int id_to, int time)
Make a ping message to send to a network.
N_STR * netmsg_make_string_msg(int id_from, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
make a network NETMSG_STRING message with given parameters
volatile int stop_flag
atomic stop flag
char query[2048]
query string (or empty)
N_SOCKET link
networking socket
size_t content_length
Store content length.
char * type
Type of request.
char * filename
Content-Disposition filename, or "".
int port
port number (0 if not specified)
int masked
MASK bit (client-to-server frames are masked).
char * host
proxy hostname
pthread_rwlock_t rwlock
thread safety
NETWORK * netw
underlying network connection
char * path
path starting with "/", or "/" if empty
char * content_type
the part's Content-Type, or ""
N_STR * body
response body
char * scheme
"http" or "https"
int opcode
opcode (N_WS_OP_*)
NETWORK * netw
underlying network connection
void(* on_event)(N_SSE_EVENT *event, struct N_SSE_CONN *conn, void *user_data)
callback
N_STR * body
the part's raw body bytes (binary-safe), or NULL
int fin
FIN bit (1 = final fragment).
N_STR * event
event type (or NULL for default)
int connected
1 if handshake completed
N_STR * body
request body (or NULL)
uint64_t payload_len
payload length in bytes
char * password
NULL if no auth.
char * query
raw query string without leading '?
N_URL_PARAM params[64]
parsed key=value pairs
char * body
Pointer to the body data.
LIST * headers
list of char* "Name: Value" strings
char * value
parameter value
char * host
remote hostname
SOCKET sock
a normal socket
char path[2048]
request path
char * scheme
"http", "https", or "socks5"
char * port
port of socket
int nb_params
number of parsed parameters
char * name
Content-Disposition form-field name, or "".
SSL_CTX * ctx
SSL context holder.
const unsigned char * payload
pointer into the input buffer (still masked)
int retry
retry interval in ms (0 if not set)
char content_type[256]
Store content type.
N_STR * id
last event ID (or NULL)
int rsv
the three reserved bits, rsv1<<2 | rsv2<<1 | rsv3
N_STR * payload
message payload
char method[16]
HTTP method.
unsigned char mask[4]
masking key when masked, else zeroed
char * disposition_extra
extra Content-Disposition parameters, appended verbatim after name (and filename) as written,...
char * username
NULL if no auth.
int status_code
HTTP status code.
unsigned long int is_blocking
flag to quickly check socket mode
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
char content_type[128]
Content-Type header value.
HASH_TABLE * pool
table of clients
void * user_data
user data for callback
#define NETW_SOCKET_ERROR
code for a socket error
int netw_send_string_to_all(NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color)
Add a string to the network, aiming all server-side users.
ssize_t send_ssl_data_once(void *netw, char *buf, uint32_t n)
single-attempt TLS send for non-blocking sockets (reactor use).
#define N_URL_MAX_PARAMS
maximum number of parsed query parameters
#define NETW_IO_WANT_READ
single-attempt I/O (send_data_once / recv_data_once): the operation cannot progress until the socket ...
#define NETW_COMPRESS_THRESHOLD
Opportunistic compression policy.
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
N_STR * n_http_build_multipart(LIST *parts, const char *boundary)
build a multipart/form-data body from a LIST of N_HTTP_MULTIPART_PART* using boundary (without "--");...
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
int netw_ssl_get_verify_result(NETWORK *netw, const char *expected_host, char *errbuf, size_t errsz)
evaluate the peer certificate of a completed TLS client connection
const char * n_netw_get_connect_error(int index)
Get pre-connection error message by index.
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
send data onto the socket
char * netw_extract_http_request_type(const char *request)
function to extract the request method from an http request
int netw_set_crypto_pem_ctx(NETWORK *netw, const N_STR *key_pem, const N_STR *cert_pem)
install an in-memory cert/key on a single accepted connection's SSL
#define N_WS_MAX_PAYLOAD
Largest WebSocket payload n_ws_recv() will accept from a peer, in bytes.
int netw_get_queue_status(NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
retrieve network send queue status
int netw_bind_udp(NETWORK **netw, char *addr, char *port, int ip_version)
Create a UDP bound socket for receiving datagrams.
int netw_set_crypto_pem(NETWORK *netw, const char *key_pem, const char *cert_pem)
activate SSL encryption using PEM-formatted key and certificate strings loaded from memory
ssize_t recv_ssl_data_once(void *netw, char *buf, uint32_t n)
single-attempt TLS recv for non-blocking sockets (reactor use).
#define NETW_THR_EXIT_ERROR
Internal DONE value indicating a send/recv thread observed NETW_ERROR without an in-flight local shut...
#define NETW_COMPRESS_MIN_RATIO
int netw_init_wsa(int mode, int v1, int v2)
Do not directly use, internal api.
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable SSL peer certificate verification
ssize_t send_php(SOCKET s, int _code, char *buf, int n)
send data onto the socket
#define NETW_MSG_MAX_FRAME_SIZE
Maximum accepted size, in bytes, of a single received message frame payload.
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receing thread.
void n_ws_close(N_WS_CONN *conn)
Send close frame and close the connection.
char * netw_urlencode(const char *str, size_t len)
function to perform URL encoding
N_STR * n_url_encode(const char *str)
percent-encode a string for use in URLs (delegates to n_str_url_encode())
int n_http_status_class(int status_code)
leading digit of an HTTP status code (1..5), or 0 when outside 100..599
#define SSL_get1_peer_certificate(ssl)
void * netw_send_func(void *NET)
Thread send function.
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
make a normal blocking 'accept' .
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
Helper function to extract the URL from the HTTP request line.
N_PROXY_CFG * n_proxy_cfg_parse(const char *url)
Parse a proxy URL string into an N_PROXY_CFG struct.
#define N_ENUM_netw_code_type(_)
Network codes definition.
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
int netw_ssl_server_handshake_ex(NETWORK *netw, netw_sni_pick_cb pick, void *user_data, const char *const *alpn, size_t alpn_count)
upgrade an already-accepted plaintext connection to a TLS server, selecting the certificate per clien...
int netw_set_crypto_chain_pem(NETWORK *netw, const char *key_pem, const char *cert_pem, const char *ca_pem)
activate SSL encryption using PEM strings for key, certificate, and CA
int netw_adopt_client_fd(NETWORK **netw, SOCKET fd, const char *host, const char *port)
Adopt an already-connected socket fd into a fresh client NETWORK.
int netw_ssl_set_ca(NETWORK *netw, const char *ca_file, const char *ca_path)
set custom CA verify location for SSL context
void n_sse_stop(N_SSE_CONN *conn)
Signal the SSE connection to stop reading.
ssize_t recv_data_once(void *netw, char *buf, uint32_t n)
single-attempt recv for non-blocking sockets (reactor use).
#define NETWORK_IPV6
Flag to force IPV6.
void n_http_multipart_free(LIST **parts)
free a LIST returned by n_http_parse_multipart and NULL the pointer
void n_ws_unmask(unsigned char *dst, const unsigned char *src, size_t len, const unsigned char mask[4])
XOR-unmask len bytes of src into dst using a 4-byte WebSocket mask key.
int netw_connect_ex_to(NETWORK **netw, char *host, char *port, size_t send_list_limit, size_t recv_list_limit, int ip_version, char *ssl_key_file, char *ssl_cert_file, int connect_timeout_ms)
Use this to connect a NETWORK to any listening one.
#define NETWORK_UDP
Flag for UDP transport.
N_STR * n_url_canonicalize_string(const char *url)
parse and canonicalize a URL string in one call
N_STR * n_url_resolve(const char *base, const char *ref)
resolve a (possibly relative) URL reference against an absolute base
ssize_t send_data_once(void *netw, char *buf, uint32_t n)
single-attempt send for non-blocking sockets (reactor use).
int netw_ssl_server_handshake_once(NETWORK *netw, SSL_CTX *ctx)
non-blocking, resumable TLS server handshake against a shared ctx
int netw_get_http_date(char *buffer, size_t buffer_size)
helper function to generate the current date in HTTP format
int netw_connect_nonblock(NETWORK **netw, char *host, char *port, int ip_version)
begin a non-blocking TCP connect without waiting for it to complete
NETWORK_POOL * netw_new_pool(size_t nb_min_element)
return a new network pool of nb_min_element
int netw_set_user_id(NETWORK *netw, int id)
associate an id and a network
void n_mock_server_free(N_MOCK_SERVER **server)
Free a mock server and close the listening socket.
ssize_t recv_data(void *netw, char *buf, uint32_t n)
recv data from the socket
int netw_ssl_handshake_once(NETWORK *netw, const char *sni_hostname)
non-blocking, resumable TLS client handshake
int netw_init_openssl(void)
Do not directly use, internal api.
void n_netw_clear_errors(NETWORK *netw)
Clear captured errors on a NETWORK handle.
ssize_t recv_ssl_data(void *netw, char *buf, uint32_t n)
recv data from the socket
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
int netw_ssl_do_handshake(NETWORK *netw, const char *sni_hostname)
Complete the SSL handshake on an already-connected NETWORK.
#define HEAD_SIZE
Size of a HEAD message.
ssize_t send_udp_data(void *netw, char *buf, uint32_t n)
send data via UDP on a connected socket
#define netw_atomic_write_state(netw, val)
Lock-free atomic write of the network state field.
int netw_set_compression_mode(NETWORK *netw, int mode)
Pick send-side payload compression algorithm.
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
int netw_destroy_pool(NETWORK_POOL **netw_pool)
free a NETWORK_POOL *pool
void n_url_free(N_URL **u)
free a N_URL and all its members
#define NETWORK_IPV4
Flag to force IPV4.
int netw_build_http_response(N_STR **http_response, int status_code, const char *server_name, const char *content_type, char *additional_headers, N_STR *body)
function to dynamically generate an HTTP response
void * netw_recv_func(void *NET)
To Thread Receiving function.
void n_sse_conn_free(N_SSE_CONN **conn)
Free an SSE connection structure.
int n_proxy_connect_tunnel_ssl(const N_PROXY_CFG *proxy, const char *target_host, int target_port)
Open a TCP connection through an HTTPS proxy using CONNECT tunneling.
N_STR * n_url_decode(const char *str)
decode a percent-encoded string (returns N_STR)
int n_netw_get_error_count(const NETWORK *netw)
Get number of captured errors on a NETWORK handle.
int n_ws_frame_parse(const unsigned char *buf, size_t len, N_WS_FRAME *frame, size_t *consumed)
Parse one WebSocket frame from a byte buffer (stateless, no allocation).
N_WS_CONN * n_ws_connect(const char *host, const char *port, const char *path, int use_ssl)
Connect to a WebSocket server (ws:// or wss://).
int n_http_status_is_server_error(int status_code)
1 when status_code is a server error (5xx), else 0
#define SOCKET_SIZE_FORMAT
socket associated printf style
__netw_code_type size_t htonst(size_t value)
host to network size_t
int netw_unload_openssl(void)
Do not directly use, internal api.
const char * netw_ssl_get_sni(NETWORK *netw)
get the SNI server_name the peer requested in its ClientHello
int n_http_status_is_client_error(int status_code)
1 when status_code is a client error (4xx), else 0
#define HEAD_CODE
Code of a HEAD message.
int n_proxy_connect_socks5(const N_PROXY_CFG *proxy, const char *target_host, int target_port)
Open a TCP connection through a SOCKS5 proxy.
size_t ntohst(size_t value)
network to host size_t
ssize_t netw_udp_sendto(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *dest_addr, socklen_t dest_len)
send data via UDP to a specific destination address
#define NETWORK_CONSECUTIVE_SEND_WAIT
Flag to set consecutive send waiting timeout.
#define netw_atomic_read_reactor_mode(netw)
Lock-free atomic read of the reactor_mode flag.
size_t netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
int(* netw_sni_pick_cb)(const char *sni, N_STR **cert_pem, N_STR **key_pem, void *user_data)
SNI cert-pick callback for netw_accept_ssl_with_sni_cb: given the client SNI host (may be NULL),...
int n_proxy_connect_tunnel(const N_PROXY_CFG *proxy, const char *target_host, int target_port)
Open a TCP connection through an HTTP proxy using CONNECT tunneling.
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
void n_netw_clear_connect_errors(void)
Clear pre-connection errors on this thread.
void n_mock_server_stop(N_MOCK_SERVER *server)
Signal the mock server to stop accepting connections.
int netw_connect_ex(NETWORK **netw, char *host, char *port, size_t send_list_limit, size_t recv_list_limit, int ip_version, char *ssl_key_file, char *ssl_cert_file)
Use this to connect a NETWORK to any listening one.
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
int SOCKET
default socket declaration
#define NETW_SOCKET_DISCONNECTED
Code for a disconnected recv.
int netw_pool_broadcast(NETWORK_POOL *netw_pool, const NETWORK *from, N_STR *net_msg)
add net_msg to all network in netork pool
SSL_CTX * netw_ssl_server_ctx_new(const char *key_file, const char *cert_chain_file)
new standalone server SSL_CTX with the library's secure defaults
int n_http_decompress_body(const N_STR *body, const char *encoding, N_STR **out)
decompress an HTTP body per its Content-Encoding ("gzip"/"deflate"; identity/NULL/unknown copies thro...
int netw_ssl_connect_client_to(NETWORK **netw, char *host, char *port, int ip_version, int connect_timeout_ms)
Connect as an SSL client without providing a client certificate.
const char * n_netw_get_error(const NETWORK *netw, int index)
Get captured error message by index (0 = oldest).
void n_sse_event_clean(N_SSE_EVENT *event)
Free the contents of an SSE event (does not free the struct itself).
#define NETWORK_WAIT_CLOSE_TIMEOUT
Flag to set network closing wait timeout.
int netw_setsockopt(NETWORK *netw, int optname, int value)
Modify common socket options on the given netw.
int n_http_multipart_boundary(const char *content_type, char *out, size_t outsz)
copy the boundary token from a Content-Type value into out (without the leading "--").
int netw_ssl_set_alpn(NETWORK *netw, const char *const *protos, size_t count)
offer an ALPN protocol list on a TLS client connection, before its handshake
int netw_set(NETWORK *netw, int flag)
Restart or reset the specified network ability.
ssize_t send_data(void *netw, char *buf, uint32_t n)
send data onto the socket
ssize_t recv_udp_data(void *netw, char *buf, uint32_t n)
recv data via UDP from a connected socket
NETWORK * netw_accept_ssl_with_sni_cb(NETWORK *listen, netw_sni_pick_cb pick, void *user_data)
accept a TLS connection, selecting the server certificate per client SNI
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get the state of a network.
N_SSE_CONN * n_sse_connect(const char *host, const char *port, const char *path, int use_ssl, const char *user_agent, void(*on_event)(N_SSE_EVENT *, N_SSE_CONN *, void *), void *user_data)
Connect to an SSE endpoint and start reading events.
N_URL * n_url_parse(const char *url)
parse a URL string into components
int n_http_status_is_informational(int status_code)
1 when status_code is informational (1xx), else 0
void netw_pool_netw_close(void *netw_ptr)
close a network from a network pool
int netw_connect_check(NETWORK *netw)
advance a non-blocking connect started by netw_connect_nonblock()
int n_netw_get_connect_error_count(void)
Get number of pre-connection errors captured on this thread.
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
function to calculate the required size for the URL-encoded string
#define netw_atomic_read_reactor_handle(netw)
Same contract for the back-pointer to the reactor.
int deplete_send_buffer(int fd, int timeout)
wait until the socket is empty or timeout, checking each 100 msec.
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
static void SSL_set_hostflags(SSL *ssl, unsigned int flags)
1.0.2 stand-in for SSL_set_hostflags(): set the X509_CHECK_FLAG_* host-check flags of ssl
N_STR * n_url_canonicalize(const N_URL *u)
produce a canonical URL string for deduplication
#define NETW_IO_WANT_WRITE
single-attempt I/O: the operation cannot progress until the socket is WRITABLE.
#define NETW_MAX_RETRIES
Send or recv max number of retries.
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
int n_http_status_is_success(int status_code)
1 when status_code is success (2xx), else 0
N_STR * n_url_build(const N_URL *u)
build a URL string from parsed components
void n_mock_server_run(N_MOCK_SERVER *server)
Run the mock server accept loop.
int netw_send_quit(NETWORK *netw)
Add a formatted NETMSG_QUIT message to the specified network.
#define NETW_THR_EXIT_OK
Internal DONE value indicating a send/recv thread reached end-of-life cleanly (peer QUIT or local NET...
int netw_ssl_set_ciphers(NETWORK *netw, const char *cipher_list, const char *ciphersuites)
set the cipher list (TLS 1.2 and below) and/or the ciphersuites (TLS 1.3)
int netw_ssl_connect_client(NETWORK **netw, char *host, char *port, int ip_version)
Connect as an SSL client without providing a client certificate.
int netw_ssl_start_client(NETWORK *netw)
Set up a client SSL_CTX on an already-connected NETWORK.
const char * netw_get_http_status_message(int status_code)
helper function to convert status code to a human-readable message
int netw_set_crypto_chain(NETWORK *netw, char *key, char *certificate, char *ca_file)
activate SSL encryption using key/certificate files and a CA file for chain verification
int n_ws_recv(N_WS_CONN *conn, N_WS_MESSAGE *msg_out)
Receive one WebSocket frame.
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
Modify blocking socket mode.
N_STR * netw_wait_msg(NETWORK *netw, unsigned int refresh, size_t timeout)
Wait a message from aimed NETWORK.
int n_http_status_is_redirect(int status_code)
1 when status_code is a redirect (3xx), else 0
LIST * n_http_parse_multipart(const N_STR *body, const char *boundary)
parse a multipart body (boundary given without "--") into a LIST of N_HTTP_MULTIPART_PART*; free with...
int netw_send_string_to(NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
Add a string to the network, aiming a specific user.
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
Add a ping reply to the network.
int netw_ssl_connect(NETWORK **netw, char *host, char *port, int ip_version, char *ssl_key_file, char *ssl_cert_file)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
size_t n_ws_frame_build(int fin, int opcode, int do_mask, const unsigned char *payload, size_t len, const unsigned char mask_key[4], unsigned char *out, size_t out_cap)
build a WebSocket frame into out (optionally masking the payload).
int netw_ssl_server_handshake(NETWORK *netw, netw_sni_pick_cb pick, void *user_data)
upgrade an already-accepted plaintext connection to a TLS server, selecting the certificate per clien...
#define netw_atomic_read_state(netw)
Lock-free atomic read of the network state field.
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
N_MOCK_SERVER * n_mock_server_start(int port, void(*on_request)(N_HTTP_REQUEST *, N_HTTP_RESPONSE *, void *), void *user_data)
Start a mock HTTP server: set up listener and return immediately.
__netw_code_type
Network codes declaration.
#define NETWORK_DEPLETE_SOCKET_TIMEOUT
Flag to set send buffer depletion timeout.
int netw_connect_to(NETWORK **netw, char *host, char *port, int ip_version, int connect_timeout_ms)
Connect a NETWORK with a bounded connection-establishment time.
int netw_send_ident(NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
Add a formatted NETWMSG_IDENT message to the specified network.
char * netw_urldecode(const char *str)
Function to decode URL-encoded data.
int netw_ssl_set_client_cert(NETWORK *netw, const char *cert_file, const char *key_file)
load a client certificate and private key for mTLS
void n_ws_conn_free(N_WS_CONN **conn)
Free a WebSocket connection structure.
#define NETWORK_DEPLETE_QUEUES_TIMEOUT
Flag to set network queues depletion timeout.
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add a NETWORK *netw to a NETWORK_POOL *pool
int netw_connect_udp(NETWORK **netw, char *host, char *port, int ip_version)
Connect a UDP socket to a remote host.
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
int netw_ssl_get_alpn(NETWORK *netw, char *out, size_t out_len)
get the ALPN protocol the completed handshake selected
int netw_ssl_set_versions(NETWORK *netw, int min_version, int max_version)
set the minimum (and optionally maximum) TLS protocol version on a connection's SSL context
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
int n_ws_send(N_WS_CONN *conn, const char *payload, size_t len, int opcode)
Send a WebSocket frame (client always masks).
ssize_t recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
static int SSL_set1_host(SSL *ssl, const char *host)
1.0.2 stand-in for SSL_set1_host(): set the expected peer name of ssl, returning 1 on success and 0 o...
int netw_send_position(NETWORK *netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
Add a formatted NETWMSG_IDENT message to the specified network.
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove a NETWORK *netw to a NETWORK_POOL *pool
void netw_set_connect_abort_cb(int(*cb)(void *ctx), void *ctx)
Register a process-wide callback polled while a connect is in progress.
int netw_add_msg_ex(NETWORK *netw, char *str, unsigned int length)
Add a message to send in aimed NETWORK.
#define NETWORK_TCP
Flag for TCP transport (default).
ssize_t netw_udp_recvfrom(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *src_addr, socklen_t *src_len)
recv data via UDP and capture the source address
@ NETW_COMPRESS_NONE
no automatic compression on send, still decompresses inbound
@ NETW_COMPRESS_ZLIB
compress on send with zlib, decompress either on recv
@ NETW_COMPRESS_LZ4
compress on send with LZ4, decompress either on recv
@ NETW_THR_ENGINE_STARTED
@ NETW_THR_ENGINE_STOPPED
a single part of a parsed multipart/form-data body
parsed HTTP request for mock server callback
HTTP response to send from mock server callback.
Parsed proxy URL components.
SSE event received from server.
A single parsed WebSocket frame (RFC 6455).
structure for splitting HTTP requests
structure of a network pool
N_STR * unzip_nstr(N_STR *src)
return an uncompressed version of src
N_STR * zip_nstr(N_STR *src)
return a compressed version of src
Base64 encoding and decoding functions using N_STR.
Hash functions and table.
LZ4 block-compression handler.
static char * netstrerror(int code)
BSD style errno string NO WORKING ON REDHAT.
#define NETW_CALL_RETRY(__retvar, __expression, __max_tries)
network-aware retry macro: retries on EINTR and EAGAIN/EWOULDBLOCK
static size_t _netw_alpn_encode(const char *const *protos, size_t count, unsigned char *out, size_t out_cap)
static void _n_url_normalize_path(const char *path, char *out, size_t outsz)
normalize a path by removing "." and ".." segments (RFC 3986 style)
static int _ssl_use_pem(SSL *ssl, const char *key_pem, const char *cert_pem)
static char * mp_strndup(const char *s, size_t len)
static __thread int s_connect_err_next
static int _netw_sni_servername_cb(SSL *ssl, int *al, void *arg)
#define N_WS_FRAME_MAX_PAYLOAD
upper bound on a single parsed WebSocket frame payload, to bound a proxy's reassembly buffer against ...
static ssize_t _ws_read(N_WS_CONN *conn, void *buf, size_t len)
read exactly len bytes from a WebSocket connection
static void mp_put(char *buf, size_t *pos, const void *src, size_t n)
static void netw_init_locks(void)
void netw_ssl_print_errors(SOCKET socket)
print the queued OpenSSL errors for a given socket
static ssize_t _sse_write(NETWORK *netw, const void *buf, size_t len)
write bytes to an SSE connection (SSL or plain)
#define _Thread_local
thread-local pre-connection error buffer (DNS, socket creation)
static int _netw_ssl_server_defaults(SSL_CTX *ctx)
static void _n_parse_query_params(N_URL *u, const char *qs)
static int(* _netw_connect_abort_cb)(void *)
Optional connect-abort callback, or NULL.
static void _n_mock_parse_request(const char *buf, N_HTTP_REQUEST *req)
Parse a raw HTTP request buffer into an N_HTTP_REQUEST.
static ssize_t _ws_write(N_WS_CONN *conn, const void *buf, size_t len)
write bytes to a WebSocket connection (SSL or plain)
static int _netw_ssl_handshake_step(NETWORK *netw)
advance an in-flight TLS handshake by a single non-blocking step
NETWORK * netw_new(size_t send_list_limit, size_t recv_list_limit)
Return an empty allocated network ready to be netw_closed.
static ssize_t _sse_read_byte(NETWORK *netw, char *ch, volatile int *stop_flag)
read one byte from an SSE connection (SSL or plain).
static int _netw_start_nonblock_connect(NETWORK *netw)
create a non-blocking socket for netw->link.connect_rp and start connect()
static SOCKET _proxy_tcp_connect(const char *host, int port)
Helper: connect a plain TCP socket to host:port.
long long g_netw_bytes_sent
Add a message to send in aimed NETWORK.
static void * _netw_connect_abort_ctx
Opaque context handed to _netw_connect_abort_cb on each poll.
static void mp_emit(char *buf, size_t *pos, LIST *parts, const char *dash)
static int n_http_inflate(const unsigned char *src, size_t len, int window_bits, N_STR **out)
static int OPENSSL_IS_INITIALIZED
static __thread int s_connect_err_count
#define neterrno
get last socket error code, linux version
static pthread_mutex_t * netw_ssl_lockarray
static void mp_part_free(void *ptr)
static void _netw_capture_connect_error(const char *fmt,...)
capture a pre-connection error (thread-local)
#define NETW_CONNECT_ABORT_POLL_MS
Poll interval (milliseconds) at which an in-progress connect wakes to check the registered connect-ab...
static long mp_find(const unsigned char *hay, size_t hlen, const char *needle, size_t nlen)
char * get_in_addr(struct sockaddr *sa)
get sockaddr, IPv4 or IPv6
static int _netw_timed_connect(NETWORK *netw, SOCKET sock, struct addrinfo *rp, int connect_timeout_ms)
Connect a socket, optionally bounding the attempt by a timeout.
static const char * mp_header_value(const char *block, size_t blen, const char *name, size_t *vlen)
static void _n_mock_request_clean(N_HTTP_REQUEST *req)
Free the contents of an N_HTTP_REQUEST (does not free the struct itself).
static int _n_url_has_scheme(const char *s)
test whether a reference string begins with its own URI scheme
long long g_netw_bytes_recv
static char * mp_disp_param(const char *disp, size_t dlen, const char *param)
char * netw_get_openssl_error_string()
get the OpenSSL error string
static __thread char s_connect_errors[8][512]
static int _netw_alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
static void _netw_capture_error(NETWORK *netw, const char *fmt,...)
capture an error into a NETWORK handle's ring buffer
static void mp_puts(char *buf, size_t *pos, const char *s)
static void netw_kill_locks(void)
static int netw_is_ip_literal(const char *host)
is host a numeric IP literal rather than a DNS name?
static void _netw_crypto_setup_failed(NETWORK *netw)
Network messages , serialization tools.
void n_reactor_notify_send(NETWORK *netw)
Producer-side wake-up after a netw_add_msg.
void n_reactor_close_netw_sync(NETWORK *netw)
Synchronously close a reactor-registered NETWORK from the game thread.
Single-threaded epoll reactor for n_network connections.
ZLIB compression handler.