Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_network.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27
28#ifndef N_NETWORK
29#define N_NETWORK
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
39
40#include "n_common.h"
41#include "n_str.h"
42#include "n_list.h"
43#include "n_hash.h"
44
46#define NETWORK_DISABLE_ZERO_LENGTH_RECV FALSE
48#define NETWORK_IPALL 0
50#define NETWORK_IPV4 1
52#define NETWORK_IPV6 2
54#define NETWORK_TCP 0
56#define NETWORK_UDP 1
58#define NETWORK_DEPLETE_SOCKET_TIMEOUT 512
60#define NETWORK_DEPLETE_QUEUES_TIMEOUT 1024
62#define NETWORK_CONSECUTIVE_SEND_WAIT 2048
64#define NETWORK_WAIT_CLOSE_TIMEOUT 4096
66#define HEAD_SIZE 10
68#define HEAD_CODE 3
70#define NETW_SOCKET_ERROR -1
72#define NETW_SOCKET_DISCONNECTED -2
77#define NETW_IO_WANT_READ -10
81#define NETW_IO_WANT_WRITE -11
83#define NETW_MAX_RETRIES 8
85#define NETW_RETRY_DELAY 1000
86#ifndef NETW_MSG_MAX_FRAME_SIZE
93#define NETW_MSG_MAX_FRAME_SIZE (1024UL * 1024UL * 1024UL)
94#endif
96#define NETW_THR_EXIT_OK 100
98#define NETW_THR_EXIT_ERROR 666
99
100#ifndef SOCKET
102#ifdef __windows__
103#ifndef ARCH32BITS
105typedef long long unsigned int SOCKET;
107#define SOCKET_SIZE_FORMAT "%zu"
108#endif
109#else
111typedef int SOCKET;
113#define SOCKET_SIZE_FORMAT "%d"
114#endif
115#endif
116
117#if defined(__linux__) || defined(__sun) || defined(_AIX)
118#include <sys/types.h>
119#include <fcntl.h>
120#include <stdio.h>
121#include <errno.h>
122#include <unistd.h>
123#include <malloc.h>
124#include <string.h>
125#include <netinet/in.h>
126#include <netinet/tcp.h>
127#include <arpa/inet.h>
128#include <netdb.h>
129#include <sys/ioctl.h>
130#include <sys/socket.h>
131#include <sys/select.h>
132#include <semaphore.h>
133
135#define SOCKET_SIZE_FORMAT "%d"
136
137#ifdef __linux__
138#include <linux/sockios.h>
139#include <signal.h>
140#endif
141
142#define netw_unload()
143
145#define closesocket close
147#define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
149#define INVALID_SOCKET -1
151#define NETW_BUFLEN_CAST(x) ((size_t)(x))
152
153#include "n_time.h"
154
155#elif defined __windows__
156
157#define SHUT_WR SD_SEND
158#define SHUT_RD SD_RECEIVE
159#define SHUT_RDWR SD_BOTH
160
161#ifndef ECONNRESET
162#define ECONNRESET 104
163#endif
164
165#include "nilorea/n_windows.h"
166
167#include <errno.h>
168#include <unistd.h>
169#include <malloc.h>
170#include <string.h>
171#include "n_time.h"
172
173#ifndef MSG_EOR
174#define MSG_EOR 0
175#endif
176
177#ifndef MSG_NOSIGNAL
178#define MSG_NOSIGNAL 0
179#endif
180
181#ifndef MSG_WAITALL
182#define MSG_WAITALL 0
183#endif
184
185#ifndef EAI_SYSTEM
186#define EAI_SYSTEM 0
187#endif
188
189#ifndef AI_PASSIVE
190#define AI_PASSIVE 0x00000001
191#endif
192#ifndef AI_CANONNAME
193#define AI_CANONNAME 0x00000002
194#endif
195#ifndef AI_NUMERICHOST
196#define AI_NUMERICHOST 0x00000004
197#endif
198#ifndef AI_NUMERICSERV
199#define AI_NUMERICSERV 0x00000008
200#endif
201#ifndef AI_ALL
202#define AI_ALL 0x00000100
203#endif
204#ifndef AI_ADDRCONFIG
205#define AI_ADDRCONFIG 0x00000400
206#endif
207#ifndef AI_V4MAPPED
208#define AI_V4MAPPED 0x00000800
209#endif
210#ifndef AI_NON_AUTHORITATIVE
211#define AI_NON_AUTHORITATIVE 0x00004000
212#endif
213#ifndef AI_SECURE
214#define AI_SECURE 0x00008000
215#endif
216#ifndef AI_RETURN_PREFERRED_NAMES
217#define AI_RETURN_PREFERRED_NAMES 0x00010000
218#endif
219
220#include <pthread.h>
221#include <semaphore.h>
222
224#define NETFLAGS 0 /* no flags needed for microsoft */
225
227#define NETW_BUFLEN_CAST(x) ((int)(x))
228
230#define netw_unload() netw_init_wsa(0, 2, 2)
231
232#endif
233
234#ifdef HAVE_OPENSSL
235#define _OPEN_SYS_SOCK_IPV6 1
237#include <openssl/err.h>
238#include <openssl/ssl.h>
239#include <openssl/crypto.h>
240/* OpenSSL 3.0 renamed SSL_get_peer_certificate() to SSL_get1_peer_certificate()
241 * and deprecated the old spelling. Both hand back a certificate whose reference
242 * count is already incremented, so the caller X509_free()s it either way. Use
243 * the 3.0 name everywhere and map it back on OpenSSL 1.x and LibreSSL, which
244 * report an OPENSSL_VERSION_NUMBER below 0x30000000L. */
245#if OPENSSL_VERSION_NUMBER < 0x30000000L
246#ifndef SSL_get1_peer_certificate
247#define SSL_get1_peer_certificate(ssl) SSL_get_peer_certificate(ssl)
248#endif
249#endif
250
257#define NETW_HAVE_TLS13 N_OSSL_HAVE_TLS13
258
263#if NETW_HAVE_TLS13
264#define NETW_TLS_MAX_VERSION TLS1_3_VERSION
265#else
266#define NETW_TLS_MAX_VERSION TLS1_2_VERSION
267#endif
268
269#if !N_OSSL_HAVE_SSL_SET1_HOST
270/* SSL_set_hostflags() and SSL_set1_host() arrived in OpenSSL 1.1.0 (LibreSSL
271 * 2.9). Before that the same thing is reached through the connection's
272 * X509_VERIFY_PARAM, which is all the newer calls are: thin wrappers over it.
273 * Both the parameter object and the two X509_VERIFY_PARAM_* entry points below
274 * exist in OpenSSL 1.0.2 (declared in <openssl/x509_vfy.h>, which the
275 * <openssl/ssl.h> above pulls in), so the shims are exact, and call sites need
276 * no version guard of their own. */
277
279static inline void SSL_set_hostflags(SSL* ssl, unsigned int flags) {
280 X509_VERIFY_PARAM* param = SSL_get0_param(ssl);
281 if (param)
282 X509_VERIFY_PARAM_set_hostflags(param, flags);
283}
284
286static inline int SSL_set1_host(SSL* ssl, const char* host) {
287 X509_VERIFY_PARAM* param = SSL_get0_param(ssl);
288 if (!param)
289 return 0;
290 return X509_VERIFY_PARAM_set1_host(param, host, 0);
291}
292#endif
293#endif
294
296#define N_ENUM_netw_code_type(_) \
297 _(NETW_CLIENT, 2) \
298 _(NETW_SERVER, 4) \
299 _(NETW_RESTART_TIMER, 8) \
300 _(NETW_EMPTY_SENDBUF, 16) \
301 _(NETW_EMPTY_RECVBUF, 32) \
302 _(NETW_RUN, 64) \
303 _(NETW_EXIT_ASKED, 128) \
304 _(NETW_EXITED, 256) \
305 _(NETW_ERROR, 512) \
306 _(NETW_ENCRYPT_NONE, 1024) \
307 _(NETW_ENCRYPT_OPENSSL, 2048) \
308 _(NETW_THR_ENGINE_STARTED, 4096) \
309 _(NETW_THR_ENGINE_STOPPED, 8192) \
310 _(NETW_DESTROY_RECVBUF, 16384) \
311 _(NETW_DESTROY_SENDBUF, 32768) \
312 _(NETW_COMPRESSED_ZLIB, 65536) \
313 _(NETW_COMPRESSED_LZ4, 131072)
314
318#define NETW_COMPRESSED NETW_COMPRESSED_ZLIB
319
334
342#define NETW_COMPRESS_THRESHOLD 256u
343#define NETW_COMPRESS_MIN_RATIO 10u
344
347
349typedef ssize_t (*netw_func)(void*, char*, uint32_t);
350
352typedef struct N_SOCKET {
354 char* port;
358 char* ip;
359
361 unsigned long int is_blocking;
362
364 struct addrinfo hints;
366 struct addrinfo* rhost;
369 struct addrinfo* connect_rp;
371 struct sockaddr_storage raddr;
372} N_SOCKET;
373
375typedef struct NETWORK {
388 /* disabled so reuseport state */
389 // so_reuseport,
420
422 uint32_t state;
423
439
440#ifdef HAVE_OPENSSL
442 const SSL_METHOD* method;
444 SSL_CTX* ctx;
446 SSL* ssl;
451#endif
452
455
460
462 pthread_t send_thr;
464 pthread_t recv_thr;
465
467 pthread_mutex_t sendbolt;
469 pthread_mutex_t recvbolt;
471 pthread_mutex_t eventbolt;
474
477
479 char netw_errors[8][512];
484
493
504
513
514 /* Incremental recv state for reactor mode. Unused by thread
515 * mode (`netw_recv_func` reads blocking). The state machine
516 * has three phases: STATE word (4 B), LENGTH word (4 B),
517 * PAYLOAD (length B), then push to recv_buf and reset. Per-frame
518 * state lives across multiple readable events because TCP can
519 * deliver any prefix of a frame before EAGAIN. */
521 unsigned char reactor_read_hdr_buf[4];
527
528 /* Incremental send state for reactor mode. Mirrors
529 * `netw_send_func`'s output: each queued N_STR is pre-framed
530 * (4-byte state + 4-byte length + payload, with optional
531 * compression) into a contiguous buffer; the reactor sends as
532 * the kernel buffer accepts bytes, tracking offset across
533 * multiple writable events. `reactor_write_armed` mirrors
534 * whether EPOLLOUT is in the registered events, used to
535 * arm/disarm exactly once per back-pressure cycle. */
549
559
573
587
595
596} NETWORK;
597
602#define netw_atomic_read_state(netw) __atomic_load_n(&(netw)->state, __ATOMIC_ACQUIRE)
603
607#define netw_atomic_write_state(netw, val) __atomic_store_n(&(netw)->state, (val), __ATOMIC_RELEASE)
608
616#define netw_atomic_read_reactor_mode(netw) __atomic_load_n(&(netw)->reactor_mode, __ATOMIC_ACQUIRE)
617#define netw_atomic_write_reactor_mode(netw, val) __atomic_store_n(&(netw)->reactor_mode, (val), __ATOMIC_RELEASE)
621#define netw_atomic_read_reactor_handle(netw) __atomic_load_n(&(netw)->reactor_handle, __ATOMIC_ACQUIRE)
622#define netw_atomic_write_reactor_handle(netw, val) __atomic_store_n(&(netw)->reactor_handle, (val), __ATOMIC_RELEASE)
623
625typedef struct NETWORK_POOL {
628
630 pthread_rwlock_t rwlock;
631
633
635typedef struct NETWORK_HTTP_INFO {
637 char content_type[256];
641 char* body;
643 char* type;
645
647size_t htonst(size_t value);
649size_t ntohst(size_t value);
650#ifdef HAVE_OPENSSL
652int netw_set_crypto(NETWORK* netw, char* key, char* certificate);
654int netw_set_crypto_pem(NETWORK* netw, const char* key_pem, const char* cert_pem);
656int netw_set_crypto_chain(NETWORK* netw, char* key, char* certificate, char* ca_file);
658int netw_set_crypto_chain_pem(NETWORK* netw, const char* key_pem, const char* cert_pem, const char* ca_pem);
660int netw_ssl_set_ca(NETWORK* netw, const char* ca_file, const char* ca_path);
662int netw_ssl_set_verify(NETWORK* netw, int enable);
664int netw_ssl_set_versions(NETWORK* netw, int min_version, int max_version);
666int netw_ssl_set_ciphers(NETWORK* netw, const char* cipher_list, const char* ciphersuites);
672SSL_CTX* netw_ssl_server_ctx_new(const char* key_file, const char* cert_chain_file);
674int netw_ssl_get_verify_result(NETWORK* netw, const char* expected_host, char* errbuf, size_t errsz);
676int netw_ssl_set_client_cert(NETWORK* netw, const char* cert_file, const char* key_file);
678int netw_init_openssl(void);
683typedef int (*netw_sni_pick_cb)(const char* sni, N_STR** cert_pem, N_STR** key_pem, void* user_data);
685const char* netw_ssl_get_sni(NETWORK* netw);
687int netw_set_crypto_pem_ctx(NETWORK* netw, const N_STR* key_pem, const N_STR* cert_pem);
689NETWORK* netw_accept_ssl_with_sni_cb(NETWORK* listen, netw_sni_pick_cb pick, void* user_data);
691int netw_ssl_server_handshake(NETWORK* netw, netw_sni_pick_cb pick, void* user_data);
693int netw_ssl_server_handshake_ex(NETWORK* netw, netw_sni_pick_cb pick, void* user_data, const char* const* alpn, size_t alpn_count);
695int netw_ssl_set_alpn(NETWORK* netw, const char* const* protos, size_t count);
697int netw_ssl_get_alpn(NETWORK* netw, char* out, size_t out_len);
699int netw_unload_openssl(void);
701int netw_ssl_connect(NETWORK** netw, char* host, char* port, int ip_version, char* ssl_key_file, char* ssl_cert_file);
703int netw_ssl_connect_client(NETWORK** netw, char* host, char* port, int ip_version);
705int netw_ssl_connect_client_to(NETWORK** netw, char* host, char* port, int ip_version, int connect_timeout_ms);
722int netw_ssl_do_handshake(NETWORK* netw, const char* sni_hostname);
724int netw_ssl_handshake_once(NETWORK* netw, const char* sni_hostname);
728ssize_t send_ssl_data(void* netw, char* buf, uint32_t n);
730ssize_t recv_ssl_data(void* netw, char* buf, uint32_t n);
732ssize_t send_ssl_data_once(void* netw, char* buf, uint32_t n);
734ssize_t recv_ssl_data_once(void* netw, char* buf, uint32_t n);
735#endif
737int netw_init_wsa(int mode, int v1, int v2);
739int netw_set(NETWORK* netw, int flag);
741int netw_get_state(NETWORK* netw, uint32_t* state, int* thr_engine_status);
743int netw_setsockopt(NETWORK* netw, int optname, int value);
745int netw_set_blocking(NETWORK* netw, unsigned long int is_blocking);
752int 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);
754int 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);
756int netw_connect(NETWORK** netw, char* host, char* port, int ip_version);
758int netw_connect_to(NETWORK** netw, char* host, char* port, int ip_version, int connect_timeout_ms);
760int netw_connect_nonblock(NETWORK** netw, char* host, char* port, int ip_version);
764void netw_set_connect_abort_cb(int (*cb)(void* ctx), void* ctx);
766int netw_adopt_client_fd(NETWORK** netw, SOCKET fd, const char* host, const char* port);
768int deplete_send_buffer(int fd, int timeout);
772int netw_bind_udp(NETWORK** netw, char* addr, char* port, int ip_version);
774int netw_connect_udp(NETWORK** netw, char* host, char* port, int ip_version);
776ssize_t send_udp_data(void* netw, char* buf, uint32_t n);
778ssize_t recv_udp_data(void* netw, char* buf, uint32_t n);
780ssize_t netw_udp_sendto(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* dest_addr, socklen_t dest_len);
782ssize_t netw_udp_recvfrom(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* src_addr, socklen_t* src_len);
784int netw_close(NETWORK** netw);
788int netw_wait_close_timed(NETWORK** netw, size_t timeout);
797int netw_make_listening(NETWORK** netw, char* addr, char* port, int nbpending, int ip_version);
799NETWORK* netw_accept_from_ex(NETWORK* from, size_t send_list_limit, size_t recv_list_limit, int blocking, int* retval);
803NETWORK* netw_accept_nonblock_from(NETWORK* from, int blocking);
805int netw_add_msg(NETWORK* netw, N_STR* msg);
807int netw_add_msg_ex(NETWORK* netw, char* str, unsigned int length);
811N_STR* netw_wait_msg(NETWORK* netw, unsigned int refresh, size_t timeout);
815void* netw_send_func(void* NET);
817void* netw_recv_func(void* NET);
819ssize_t send_data(void* netw, char* buf, uint32_t n);
821ssize_t recv_data(void* netw, char* buf, uint32_t n);
823ssize_t send_data_once(void* netw, char* buf, uint32_t n);
825ssize_t recv_data_once(void* netw, char* buf, uint32_t n);
827ssize_t send_php(SOCKET s, int _code, char* buf, int n);
829ssize_t recv_php(SOCKET s, int* _code, char** buf);
831int netw_get_queue_status(NETWORK* netw, size_t* nb_to_send, size_t* nb_to_read);
832
834NETWORK_POOL* netw_new_pool(size_t nb_min_element);
836int netw_destroy_pool(NETWORK_POOL** netw_pool);
838void netw_pool_netw_close(void* netw_ptr);
840int netw_pool_add(NETWORK_POOL* netw_pool, NETWORK* netw);
842int netw_pool_remove(NETWORK_POOL* netw_pool, NETWORK* netw);
844int netw_pool_broadcast(NETWORK_POOL* netw_pool, const NETWORK* from, N_STR* net_msg);
846size_t netw_pool_nbclients(NETWORK_POOL* netw_pool);
847
849int netw_set_user_id(NETWORK* netw, int id);
850
852int netw_send_ping(NETWORK* netw, int type, int id_from, int id_to, int time);
854int netw_send_ident(NETWORK* netw, int type, int id, N_STR* name, N_STR* passwd);
856int netw_send_position(NETWORK* netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp);
858int netw_send_string_to(NETWORK* netw, int id_to, N_STR* name, N_STR* chan, N_STR* txt, int color);
860int netw_send_string_to_all(NETWORK* netw, N_STR* name, N_STR* chan, N_STR* txt, int color);
863
865size_t netw_calculate_urlencoded_size(const char* str, size_t len);
867char* netw_extract_http_request_type(const char* request);
871int netw_info_destroy(NETWORK_HTTP_INFO http_request);
873char* netw_urlencode(const char* str, size_t len);
875int netw_get_url_from_http_request(const char* request, char* url, size_t size);
877char* netw_urldecode(const char* str);
879HASH_TABLE* netw_parse_post_data(const char* post_data);
881const char* netw_guess_http_content_type(const char* url);
883const char* netw_get_http_status_message(int status_code);
885int n_http_status_class(int status_code);
887int n_http_status_is_informational(int status_code);
889int n_http_status_is_success(int status_code);
891int n_http_status_is_redirect(int status_code);
893int n_http_status_is_client_error(int status_code);
895int n_http_status_is_server_error(int status_code);
897int n_http_decompress_body(const N_STR* body, const char* encoding, N_STR** out);
898
915
917int n_http_multipart_boundary(const char* content_type, char* out, size_t outsz);
919LIST* n_http_parse_multipart(const N_STR* body, const char* boundary);
921N_STR* n_http_build_multipart(LIST* parts, const char* boundary);
923void n_http_multipart_free(LIST** parts);
925int netw_get_http_date(char* buffer, size_t buffer_size);
927int 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);
928
932typedef struct N_PROXY_CFG {
933 char* scheme;
934 char* host;
935 int port;
936 char* username;
937 char* password;
939
955N_PROXY_CFG* n_proxy_cfg_parse(const char* url);
956
961void n_proxy_cfg_free(N_PROXY_CFG** cfg);
962
980int n_proxy_connect_tunnel(const N_PROXY_CFG* proxy,
981 const char* target_host,
982 int target_port);
983
984#ifdef HAVE_OPENSSL
1000 const char* target_host,
1001 int target_port);
1002#endif /* HAVE_OPENSSL */
1003
1016int n_proxy_connect_socks5(const N_PROXY_CFG* proxy,
1017 const char* target_host,
1018 int target_port);
1019
1021#define N_URL_MAX_PARAMS 64
1022
1024typedef struct N_URL_PARAM {
1025 char* key;
1026 char* value;
1027} N_URL_PARAM;
1028
1039
1041N_URL* n_url_parse(const char* url);
1043N_STR* n_url_build(const N_URL* u);
1045N_STR* n_url_canonicalize(const N_URL* u);
1047N_STR* n_url_canonicalize_string(const char* url);
1049N_STR* n_url_resolve(const char* base, const char* ref);
1051N_STR* n_url_encode(const char* str);
1053N_STR* n_url_decode(const char* str);
1055void n_url_free(N_URL** u);
1056
1058#define N_WS_OP_TEXT 0x01
1059#define N_WS_OP_BINARY 0x02
1060#define N_WS_OP_CLOSE 0x08
1061#define N_WS_OP_PING 0x09
1062#define N_WS_OP_PONG 0x0A
1063
1068#define N_WS_MAX_PAYLOAD (64 * 1024 * 1024)
1069
1076
1078typedef struct N_WS_CONN {
1081 char* host;
1082 char* path;
1083} N_WS_CONN;
1084
1087typedef struct N_WS_FRAME {
1088 int fin;
1089 int rsv;
1092 unsigned char mask[4];
1093 uint64_t payload_len;
1094 const unsigned char* payload;
1095} N_WS_FRAME;
1096
1100int n_ws_frame_parse(const unsigned char* buf, size_t len, N_WS_FRAME* frame, size_t* consumed);
1101
1103void n_ws_unmask(unsigned char* dst, const unsigned char* src, size_t len, const unsigned char mask[4]);
1104
1107size_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);
1108
1109#ifdef HAVE_OPENSSL
1111N_WS_CONN* n_ws_connect(const char* host, const char* port, const char* path, int use_ssl);
1112
1114int n_ws_send(N_WS_CONN* conn, const char* payload, size_t len, int opcode);
1115
1117int n_ws_recv(N_WS_CONN* conn, N_WS_MESSAGE* msg_out);
1118
1120void n_ws_close(N_WS_CONN* conn);
1121
1123void n_ws_conn_free(N_WS_CONN** conn);
1124#endif
1125
1133
1134struct N_SSE_CONN;
1135
1137typedef struct N_SSE_CONN {
1139 volatile int stop_flag;
1140 void (*on_event)(N_SSE_EVENT* event, struct N_SSE_CONN* conn, void* user_data);
1142} N_SSE_CONN;
1143
1144#ifdef HAVE_OPENSSL
1149N_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);
1150
1152void n_sse_stop(N_SSE_CONN* conn);
1153
1155void n_sse_conn_free(N_SSE_CONN** conn);
1156
1158void n_sse_event_clean(N_SSE_EVENT* event);
1159#endif
1160
1162typedef struct N_HTTP_REQUEST {
1163 char method[16];
1164 char path[2048];
1165 char query[2048];
1169
1176
1185
1188 void (*on_request)(N_HTTP_REQUEST*, N_HTTP_RESPONSE*, void*),
1189 void* user_data);
1190
1193
1196
1199
1200/* per-NETWORK error capture */
1201
1205const char* n_netw_get_error(const NETWORK* netw, int index);
1208
1209/* pre-connection error capture (thread-local, no NETWORK yet) */
1210
1214const char* n_netw_get_connect_error(int index);
1217
1221
1222#ifdef __cplusplus
1223}
1224#endif
1225
1226#endif /*#ifndef N_NETWORK*/
static int mode
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)
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
char * ca_file
NETWORK * server
int ip_version
char * addr
char * port
volatile char key[227]
TRUE while the key is held.
#define N_ENUM_DECLARE(MACRO_DEFINITION, enum_name)
Macro to declare a N_ENUM.
Definition n_enum.h:158
structure of a hash table
Definition n_hash.h:138
Structure of a generic LIST container.
Definition n_list.h:59
A box including a string and his lenght.
Definition n_str.h:61
int reactor_recv_wants_write
TLS-over-reactor: recv returned NETW_IO_WANT_WRITE, the reactor re-runs the readable drain after the ...
Definition n_network.h:548
volatile int stop_flag
atomic stop flag
Definition n_network.h:1139
char query[2048]
query string (or empty)
Definition n_network.h:1165
struct addrinfo * rhost
getaddrinfo results
Definition n_network.h:366
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
Definition n_network.h:405
char * ip
ip of the connected socket
Definition n_network.h:358
N_SOCKET link
networking socket
Definition n_network.h:454
char * certificate
openssl certificate file
Definition n_network.h:448
char netw_errors[8][512]
per-connection error capture ring buffer (max 8 entries, 512 chars each)
Definition n_network.h:479
int threaded_engine_status
Threaded network engine state for this network.
Definition n_network.h:381
size_t content_length
Store content length.
Definition n_network.h:639
char * type
Type of request.
Definition n_network.h:643
pthread_t send_thr
sending thread
Definition n_network.h:462
char * reactor_read_payload
malloc'd accumulator for payload bytes
Definition n_network.h:525
int compress_mode
Per-packet compression mode, see NETW_COMPRESS_MODE.
Definition n_network.h:492
int nb_pending
Nb pending connection,if listening.
Definition n_network.h:377
char * filename
Content-Disposition filename, or "".
Definition n_network.h:902
int reactor_write_armed
1 = EPOLLOUT currently registered
Definition n_network.h:539
int so_reuseaddr
so reuseaddr state
Definition n_network.h:387
size_t reactor_send_off
bytes already sent to socket
Definition n_network.h:538
pthread_t recv_thr
receiving thread
Definition n_network.h:464
int port
port number (0 if not specified)
Definition n_network.h:1033
int masked
MASK bit (client-to-server frames are masked).
Definition n_network.h:1091
char * host
proxy hostname
Definition n_network.h:934
int reactor_read_hdr_have
bytes accumulated in reactor_read_hdr_buf
Definition n_network.h:522
pthread_rwlock_t rwlock
thread safety
Definition n_network.h:630
long long connect_dns_usec
Measured DNS resolution time of the most recent connect, in microseconds (0 if not measured).
Definition n_network.h:591
NETWORK * netw
underlying network connection
Definition n_network.h:1138
struct sockaddr_storage raddr
connected remote addr
Definition n_network.h:371
uint32_t reactor_read_pkt_state
state word for the in-flight frame
Definition n_network.h:523
char * host
hostname
Definition n_network.h:1032
char * path
path starting with "/", or "/" if empty
Definition n_network.h:1034
int reactor_read_phase
0=STATE, 1=LENGTH, 2=PAYLOAD
Definition n_network.h:520
pthread_mutex_t eventbolt
mutex for threaded access of state event
Definition n_network.h:471
int port
listening port
Definition n_network.h:1183
char * content_type
the part's Content-Type, or ""
Definition n_network.h:903
const SSL_METHOD * method
SSL method container.
Definition n_network.h:442
N_STR * body
response body
Definition n_network.h:1174
int netw_err_next
next write slot in ring buffer
Definition n_network.h:483
int deplete_socket_timeout
deplete socket send buffer timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:415
void * user_data
user data for handler
Definition n_network.h:1182
char * scheme
"http" or "https"
Definition n_network.h:1031
int deplete_queues_timeout
deplete network queues timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:413
int nb_running_threads
nb running threads, if > 0 thread engine is still running
Definition n_network.h:411
int in_dirty_list
Dirty-list membership flag.
Definition n_network.h:558
int reactor_close_acked
Close handshake.
Definition n_network.h:572
int opcode
opcode (N_WS_OP_*)
Definition n_network.h:1090
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
Definition n_network.h:469
NETWORK * netw
underlying network connection
Definition n_network.h:1079
void(* on_event)(N_SSE_EVENT *event, struct N_SSE_CONN *conn, void *user_data)
callback
Definition n_network.h:1140
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
Definition n_network.h:467
N_STR * body
the part's raw body bytes (binary-safe), or NULL
Definition n_network.h:904
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
Definition n_network.h:385
void * reactor_handle
Back-pointer to the registered reactor.
Definition n_network.h:512
int fin
FIN bit (1 = final fragment).
Definition n_network.h:1088
void(* on_request)(N_HTTP_REQUEST *req, N_HTTP_RESPONSE *resp, void *user_data)
request handler
Definition n_network.h:1181
N_STR * event
event type (or NULL for default)
Definition n_network.h:1128
int connected
1 if handshake completed
Definition n_network.h:1080
N_STR * data
event data
Definition n_network.h:1129
N_STR * body
request body (or NULL)
Definition n_network.h:1167
char * key
parameter name
Definition n_network.h:1025
int so_rcvtimeo
send timeout value
Definition n_network.h:401
uint64_t payload_len
payload length in bytes
Definition n_network.h:1093
uint32_t reactor_read_pkt_length
payload length for the in-flight frame
Definition n_network.h:524
char * password
NULL if no auth.
Definition n_network.h:937
char * query
raw query string without leading '?
Definition n_network.h:1035
int port
proxy port
Definition n_network.h:935
N_URL_PARAM params[64]
parsed key=value pairs
Definition n_network.h:1036
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
Definition n_network.h:393
char * body
Pointer to the body data.
Definition n_network.h:641
size_t reactor_read_payload_have
bytes accumulated into reactor_read_payload
Definition n_network.h:526
LIST * headers
list of char* "Name: Value" strings
Definition n_network.h:1166
char * value
parameter value
Definition n_network.h:1026
char * host
remote hostname
Definition n_network.h:1081
int netw_err_count
number of captured errors
Definition n_network.h:481
char * reactor_send_buf
malloc'd framed bytes, NULL when idle
Definition n_network.h:536
SOCKET sock
a normal socket
Definition n_network.h:356
char path[2048]
request path
Definition n_network.h:1164
char * scheme
"http", "https", or "socks5"
Definition n_network.h:933
char * port
port of socket
Definition n_network.h:354
netw_func recv_data_once
single-attempt recv, same contract as send_data_once.
Definition n_network.h:438
LIST * recv_buf
reveicing buffer (for incomming usage)
Definition n_network.h:459
int transport_type
transport type: NETWORK_TCP (0) or NETWORK_UDP (1)
Definition n_network.h:419
int nb_params
number of parsed parameters
Definition n_network.h:1037
char * name
Content-Disposition form-field name, or "".
Definition n_network.h:901
int user_id
if part of a user property, id of the user
Definition n_network.h:409
sem_t send_blocker
block sending func
Definition n_network.h:473
SSL_CTX * ctx
SSL context holder.
Definition n_network.h:444
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
Definition n_network.h:395
int so_sndtimeo
send timeout value
Definition n_network.h:399
long long connect_tcp_usec
Measured TCP connect time of the most recent connect, in microseconds (0 if not measured).
Definition n_network.h:594
const unsigned char * payload
pointer into the input buffer (still masked)
Definition n_network.h:1094
int retry
retry interval in ms (0 if not set)
Definition n_network.h:1131
char content_type[256]
Store content type.
Definition n_network.h:637
size_t reactor_send_len
total bytes in reactor_send_buf
Definition n_network.h:537
N_STR * id
last event ID (or NULL)
Definition n_network.h:1130
struct addrinfo hints
address of local machine
Definition n_network.h:364
int reactor_registered
Reactor close-handshake latch.
Definition n_network.h:586
int so_keepalive
so keepalive state
Definition n_network.h:391
int reactor_mode
Opt-in epoll-reactor mode flag.
Definition n_network.h:503
netw_func send_data
send func ptr
Definition n_network.h:425
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
Definition n_network.h:383
char * path
resource path
Definition n_network.h:1082
int rsv
the three reserved bits, rsv1<<2 | rsv2<<1 | rsv3
Definition n_network.h:1089
N_STR * payload
message payload
Definition n_network.h:1073
char method[16]
HTTP method.
Definition n_network.h:1163
int opcode
frame opcode
Definition n_network.h:1072
unsigned char mask[4]
masking key when masked, else zeroed
Definition n_network.h:1092
int masked
1 if masked
Definition n_network.h:1074
char * disposition_extra
extra Content-Disposition parameters, appended verbatim after name (and filename) as written,...
Definition n_network.h:905
uint32_t state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
Definition n_network.h:422
LIST * pools
pointers to network pools if members of any
Definition n_network.h:476
char * username
NULL if no auth.
Definition n_network.h:936
netw_func send_data_once
single-attempt send (non-blocking / reactor use).
Definition n_network.h:435
int status_code
HTTP status code.
Definition n_network.h:1172
char * key
openssl key file
Definition n_network.h:450
SSL * ssl
SSL handle.
Definition n_network.h:446
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
Definition n_network.h:403
unsigned long int is_blocking
flag to quickly check socket mode
Definition n_network.h:361
unsigned char reactor_read_hdr_buf[4]
accumulator for header words
Definition n_network.h:521
int reactor_send_wants_read
TLS-over-reactor: the in-flight send returned NETW_IO_WANT_READ (renegotiation), the reactor retries ...
Definition n_network.h:544
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
Definition n_network.h:407
char content_type[128]
Content-Type header value.
Definition n_network.h:1173
HASH_TABLE * pool
table of clients
Definition n_network.h:627
NETWORK * listener
listening network
Definition n_network.h:1179
struct addrinfo * connect_rp
addrinfo currently being tried by a non-blocking connect (netw_connect_nonblock / netw_connect_check)...
Definition n_network.h:369
LIST * send_buf
sending buffer (for outgoing queuing )
Definition n_network.h:457
int mode
NETWORK mode , 1 listening, 0 connecting.
Definition n_network.h:379
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
Definition n_network.h:397
void * user_data
user data for callback
Definition n_network.h:1141
netw_func recv_data
receive func ptr
Definition n_network.h:427
int wait_close_timeout
network wait close timeout value ( < 1 disabled, >= 1 timeout sec )
Definition n_network.h:417
volatile int stop_flag
atomic stop flag
Definition n_network.h:1180
int netw_send_string_to_all(NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color)
send string to all targets
Definition n_network.c:6042
ssize_t send_ssl_data_once(void *netw, char *buf, uint32_t n)
SSL single-attempt write (see send_data_once contract).
Definition n_network.c:5388
#define N_URL_MAX_PARAMS
maximum number of parsed query parameters
Definition n_network.h:1021
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:4410
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 "--");...
Definition n_network.c:6836
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:4313
int netw_ssl_get_verify_result(NETWORK *netw, const char *expected_host, char *errbuf, size_t errsz)
evaluate the peer certificate (chain trust + host match) against the system trust store,...
Definition n_network.c:2288
const char * n_netw_get_connect_error(int index)
Get pre-connection error message by index.
Definition n_network.c:126
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
SSL Writing to a socket.
Definition n_network.c:5172
char * netw_extract_http_request_type(const char *request)
extract HTTP request type
Definition n_network.c:6131
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 THIS connection's SSL only (per-connection, unlike the context-wide ...
Definition n_network.c:1853
int netw_get_queue_status(NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
get queue status
Definition n_network.c:5748
int netw_bind_udp(NETWORK **netw, char *addr, char *port, int ip_version)
Create a UDP bound socket.
Definition n_network.c:3735
int netw_set_crypto_pem(NETWORK *netw, const char *key_pem, const char *cert_pem)
set SSL from PEM strings in memory
Definition n_network.c:1597
ssize_t recv_ssl_data_once(void *netw, char *buf, uint32_t n)
SSL single-attempt read (see recv_data_once contract).
Definition n_network.c:5476
int netw_init_wsa(int mode, int v1, int v2)
Used by Init & Close network.
Definition n_network.c:841
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable peer certificate verification
Definition n_network.c:2162
ssize_t send_php(SOCKET s, int _code, char *buf, int n)
sending to php
Definition n_network.c:5561
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receiving thread.
Definition n_network.c:4893
void n_ws_close(N_WS_CONN *conn)
send close frame and close the connection
Definition n_network.c:7348
char * netw_urlencode(const char *str, size_t len)
URL-encode a string.
Definition n_network.c:6098
N_STR * n_url_encode(const char *str)
percent-encode a string for use in URLs (delegates to n_str_url_encode())
Definition n_network.c:8317
int n_http_status_class(int status_code)
leading digit of an HTTP status code (1..5), or 0 when outside 100..599
Definition n_network.c:6452
void * netw_send_func(void *NET)
Thread Sending management function.
Definition n_network.c:4533
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
Accepting routine.
Definition n_network.c:4297
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
get URL from HTTP request
Definition n_network.c:6250
N_PROXY_CFG * n_proxy_cfg_parse(const char *url)
Parse a proxy URL string into an N_PROXY_CFG struct.
Definition n_network.c:8397
#define N_ENUM_netw_code_type(_)
Network codes definition.
Definition n_network.h:296
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
set SSL
Definition n_network.c:1489
int netw_ssl_server_handshake_ex(NETWORK *netw, netw_sni_pick_cb pick, void *user_data, const char *const *alpn, size_t alpn_count)
same, plus an ALPN offer: the first protocol of alpn the client also offered is selected (none when t...
Definition n_network.c:2047
NETW_COMPRESS_MODE
Compression mode selector for NETWORK.compress_mode.
Definition n_network.h:326
int netw_set_crypto_chain_pem(NETWORK *netw, const char *key_pem, const char *cert_pem, const char *ca_pem)
set SSL certificate chain from PEM strings in memory
Definition n_network.c:1739
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 (plain send/recv)
Definition n_network.c:8840
int netw_ssl_set_ca(NETWORK *netw, const char *ca_file, const char *ca_path)
set custom CA verify path or file
Definition n_network.c:2137
void n_sse_stop(N_SSE_CONN *conn)
signal the SSE connection to stop reading
Definition n_network.c:7414
ssize_t recv_data_once(void *netw, char *buf, uint32_t n)
Single-attempt read (see NETWORK.recv_data_once contract).
Definition n_network.c:5117
void n_http_multipart_free(LIST **parts)
free a LIST returned by n_http_parse_multipart and NULL the pointer
Definition n_network.c:6635
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.
Definition n_network.c:7749
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)
Connecting, extended, with a bounded connection-establishment time (connect_timeout_ms,...
Definition n_network.c:2557
N_STR * n_url_canonicalize_string(const char *url)
parse and canonicalize a URL string in one call
Definition n_network.c:8195
N_STR * n_url_resolve(const char *base, const char *ref)
resolve a (possibly relative) reference against an absolute base URL (RFC 3986 section 5)
Definition n_network.c:8233
ssize_t send_data_once(void *netw, char *buf, uint32_t n)
Single-attempt write (see NETWORK.send_data_once contract).
Definition n_network.c:5061
int netw_ssl_server_handshake_once(NETWORK *netw, SSL_CTX *ctx)
non-blocking (resumable) TLS server handshake against a caller-owned shared ctx: TRUE done,...
Definition n_network.c:3202
int netw_get_http_date(char *buffer, size_t buffer_size)
get current HTTP date string
Definition n_network.c:6865
int netw_connect_nonblock(NETWORK **netw, char *host, char *port, int ip_version)
begin a non-blocking TCP connect: resolve and initiate connect() without waiting; drive to completion...
Definition n_network.c:2760
NETWORK_POOL * netw_new_pool(size_t nb_min_element)
init pools
Definition n_network.c:5767
int netw_set_user_id(NETWORK *netw, int id)
set user id on a netw
Definition n_network.c:5942
void n_mock_server_free(N_MOCK_SERVER **server)
free a mock server and close the listening socket
Definition n_network.c:8021
ssize_t recv_data(void *netw, char *buf, uint32_t n)
Reading from a socket.
Definition n_network.c:4994
int netw_ssl_handshake_once(NETWORK *netw, const char *sni_hostname)
non-blocking (resumable) TLS client handshake: TRUE done, NETW_IO_WANT_READ/WRITE to retry,...
Definition n_network.c:3159
int netw_init_openssl(void)
init ssl helper
Definition n_network.c:1335
void n_netw_clear_errors(NETWORK *netw)
Clear captured errors on a NETWORK handle.
Definition n_network.c:116
ssize_t recv_ssl_data(void *netw, char *buf, uint32_t n)
SSL Reading from a socket.
Definition n_network.c:5270
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Listening network.
Definition n_network.c:3602
int netw_ssl_do_handshake(NETWORK *netw, const char *sni_hostname)
complete the SSL handshake on a NETWORK whose ctx was already created.
Definition n_network.c:3057
ssize_t send_udp_data(void *netw, char *buf, uint32_t n)
UDP send data to connected peer or specified addr.
Definition n_network.c:3961
int netw_set_compression_mode(NETWORK *netw, int mode)
Pick send-side compression algorithm for this NETWORK: NETW_COMPRESS_NONE, _ZLIB, _LZ4.
Definition n_network.c:893
int netw_start_thr_engine(NETWORK *netw)
Create the sending and receiving thread of a NETWORK.
Definition n_network.c:4484
int netw_destroy_pool(NETWORK_POOL **netw_pool)
destroy pool
Definition n_network.c:5786
void n_url_free(N_URL **u)
free a N_URL and all its members
Definition n_network.c:8342
int netw_wait_close(NETWORK **netw)
Closing for peer.
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)
build HTTP response
Definition n_network.c:6897
void * netw_recv_func(void *NET)
Thread Receiving management function.
Definition n_network.c:4716
void n_sse_conn_free(N_SSE_CONN **conn)
free an SSE connection
Definition n_network.c:7423
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.
Definition n_network.c:8576
N_STR * n_url_decode(const char *str)
decode a percent-encoded string (returns N_STR)
Definition n_network.c:8321
int n_netw_get_error_count(const NETWORK *netw)
Get number of captured errors on a NETWORK handle.
Definition n_network.c:106
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).
Definition n_network.c:7702
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://) and perform the HTTP/1.1 upgrade handshake
Definition n_network.c:6995
int n_http_status_is_server_error(int status_code)
1 when status_code is a server error (5xx), else 0
Definition n_network.c:6474
size_t htonst(size_t value)
host to network size_t
Definition n_network.c:145
int netw_unload_openssl(void)
unload ssl helper
Definition n_network.c:1368
const char * netw_ssl_get_sni(NETWORK *netw)
get the SNI server_name the peer requested in its ClientHello, or NULL
Definition n_network.c:1839
int n_http_status_is_client_error(int status_code)
1 when status_code is a client error (4xx), else 0
Definition n_network.c:6470
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.
Definition n_network.c:8702
size_t ntohst(size_t value)
network to host size_t
Definition n_network.c:162
ssize_t(* netw_func)(void *, char *, uint32_t)
send/recv func ptr type
Definition n_network.h:349
ssize_t netw_udp_sendto(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *dest_addr, socklen_t dest_len)
UDP sendto with explicit destination.
Definition n_network.c:4038
size_t netw_pool_nbclients(NETWORK_POOL *netw_pool)
get nb clients
Definition n_network.c:5925
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),...
Definition n_network.h:683
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.
Definition n_network.c:8503
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
Accepting routine extended.
Definition n_network.c:4101
void n_netw_clear_connect_errors(void)
Clear pre-connection errors on this thread.
Definition n_network.c:132
void n_mock_server_stop(N_MOCK_SERVER *server)
signal the mock server to stop accepting connections
Definition n_network.c:8012
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)
Connecting, extended.
Definition n_network.c:2886
int SOCKET
default socket declaration
Definition n_network.h:111
int netw_pool_broadcast(NETWORK_POOL *netw_pool, const NETWORK *from, N_STR *net_msg)
broadcast message to pool
Definition n_network.c:5901
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 (TLS 1.2+, forward-secret AEAD,...
Definition n_network.c:1449
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...
Definition n_network.c:6535
int netw_ssl_connect_client_to(NETWORK **netw, char *host, char *port, int ip_version, int connect_timeout_ms)
like netw_ssl_connect_client but bounds the TCP connect by connect_timeout_ms (0 = OS default)
Definition n_network.c:2948
const char * n_netw_get_error(const NETWORK *netw, int index)
Get captured error message by index (0 = oldest).
Definition n_network.c:110
void n_sse_event_clean(N_SSE_EVENT *event)
free an SSE event's contents (does not free the event struct itself)
Definition n_network.c:7402
int netw_setsockopt(NETWORK *netw, int optname, int value)
Set common socket options (disable naggle, send/recv buf, reuse addr).
Definition n_network.c:969
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 "--").
Definition n_network.c:6595
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
Definition n_network.c:1934
int netw_set(NETWORK *netw, int flag)
Set flags on network.
Definition n_network.c:3259
ssize_t send_data(void *netw, char *buf, uint32_t n)
Writing to a socket.
Definition n_network.c:4933
ssize_t recv_udp_data(void *netw, char *buf, uint32_t n)
UDP recv data from socket.
Definition n_network.c:4002
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 via pick() during the handsh...
Definition n_network.c:2116
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get flags from network.
Definition n_network.c:3237
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 (blocks until stopped).
Definition n_network.c:7508
N_URL * n_url_parse(const char *url)
parse a URL string into components
Definition n_network.c:8055
int n_http_status_is_informational(int status_code)
1 when status_code is informational (1xx), else 0
Definition n_network.c:6458
void netw_pool_netw_close(void *netw_ptr)
close pool
Definition n_network.c:5805
int netw_connect_check(NETWORK *netw)
advance a non-blocking connect: TRUE connected, NETW_IO_WANT_WRITE keep polling for writability,...
Definition n_network.c:2822
int n_netw_get_connect_error_count(void)
Get number of pre-connection errors captured on this thread.
Definition n_network.c:122
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
calculate URL-encoded size
Definition n_network.c:6075
int deplete_send_buffer(int fd, int timeout)
wait for send buffer to be empty
Definition n_network.c:3336
NETWORK * netw_accept_from(NETWORK *from)
Accepting routine.
Definition n_network.c:4287
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
Definition n_network.h:279
N_STR * n_url_canonicalize(const N_URL *u)
produce a canonical URL string for dedup (lowercase scheme/host, drop default port,...
Definition n_network.c:8166
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
Definition n_network.c:8492
int netw_close(NETWORK **netw)
Closing.
Definition n_network.c:3367
int n_http_status_is_success(int status_code)
1 when status_code is success (2xx), else 0
Definition n_network.c:6462
N_STR * n_url_build(const N_URL *u)
build a URL string from parsed components
Definition n_network.c:8094
void n_mock_server_run(N_MOCK_SERVER *server)
run the mock server accept loop (blocks until stop_flag is set)
Definition n_network.c:7927
int netw_send_quit(NETWORK *netw)
send quit message
Definition n_network.c:6058
int netw_ssl_set_ciphers(NETWORK *netw, const char *cipher_list, const char *ciphersuites)
set the TLS<=1.2 cipher list and/or the TLS1.3 ciphersuites (NULL leaves a default).
Definition n_network.c:2244
int netw_ssl_connect_client(NETWORK **netw, char *host, char *port, int ip_version)
connect as SSL client without client certificate (TCP + SSL_CTX, no handshake yet)
Definition n_network.c:3020
int netw_ssl_start_client(NETWORK *netw)
set up a client SSL_CTX on an already-connected NETWORK (no handshake yet)
Definition n_network.c:2977
const char * netw_get_http_status_message(int status_code)
get HTTP status message string
Definition n_network.c:6434
int netw_set_crypto_chain(NETWORK *netw, char *key, char *certificate, char *ca_file)
set SSL certificate chain from PEM file
Definition n_network.c:1704
int n_ws_recv(N_WS_CONN *conn, N_WS_MESSAGE *msg_out)
receive one WebSocket frame
Definition n_network.c:7266
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
set blocking mode
Definition n_network.c:911
N_STR * netw_wait_msg(NETWORK *netw, unsigned int refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition n_network.c:4432
int n_http_status_is_redirect(int status_code)
1 when status_code is a redirect (3xx), else 0
Definition n_network.c:6466
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...
Definition n_network.c:6695
int netw_send_string_to(NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
send string to a specific target
Definition n_network.c:6022
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
send ping message
Definition n_network.c:5957
int netw_ssl_connect(NETWORK **netw, char *host, char *port, int ip_version, char *ssl_key_file, char *ssl_cert_file)
connect ssl helper
Definition n_network.c:2928
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract HTTP info from request
Definition n_network.c:6164
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).
Definition n_network.c:7757
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 cert per client SNI v...
Definition n_network.c:2023
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Connecting.
Definition n_network.c:2898
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 on localhost:port (returns immediately)
Definition n_network.c:7893
__netw_code_type
Network codes declaration.
Definition n_network.h:346
int netw_connect_to(NETWORK **netw, char *host, char *port, int ip_version, int connect_timeout_ms)
Connecting, with a bounded connection-establishment time (connect_timeout_ms, 0 = OS default).
Definition n_network.c:2912
int netw_send_ident(NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
send ident message
Definition n_network.c:5976
char * netw_urldecode(const char *str)
URL-decode a string.
Definition n_network.c:6287
int netw_ssl_set_client_cert(NETWORK *netw, const char *cert_file, const char *key_file)
load client certificate and private key for mTLS
Definition n_network.c:2355
void n_ws_conn_free(N_WS_CONN **conn)
free a WebSocket connection structure
Definition n_network.c:7378
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add network to pool
Definition n_network.c:5818
int netw_connect_udp(NETWORK **netw, char *host, char *port, int ip_version)
Connect a UDP socket to a remote host.
Definition n_network.c:3856
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy HTTP info structure
Definition n_network.c:6237
int netw_ssl_get_alpn(NETWORK *netw, char *out, size_t out_len)
get the ALPN protocol the completed handshake selected, on either side of the connection
Definition n_network.c:1964
int netw_ssl_set_versions(NETWORK *netw, int min_version, int max_version)
set the minimum/maximum TLS protocol version on the connection's SSL context (0 leaves a default).
Definition n_network.c:2194
HASH_TABLE * netw_parse_post_data(const char *post_data)
parse POST data into hash table
Definition n_network.c:6324
const char * netw_guess_http_content_type(const char *url)
guess HTTP content type from URL
Definition n_network.c:6370
int n_ws_send(N_WS_CONN *conn, const char *payload, size_t len, int opcode)
send a WebSocket frame (client always masks)
Definition n_network.c:7199
ssize_t recv_php(SOCKET s, int *_code, char **buf)
receive from php
Definition n_network.c:5643
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...
Definition n_network.h:286
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)
send position message
Definition n_network.c:6000
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove network from pool
Definition n_network.c:5864
void netw_set_connect_abort_cb(int(*cb)(void *ctx), void *ctx)
register a process-wide callback polled during connect; non-zero aborts the in-progress connect (NULL...
Definition n_network.c:2539
int netw_add_msg_ex(NETWORK *netw, char *str, unsigned int length)
Add a char message to send in the aimed NETWORK.
Definition n_network.c:4379
int netw_wait_close_timed(NETWORK **netw, size_t timeout)
Closing for peer with timeout.
ssize_t netw_udp_recvfrom(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *src_addr, socklen_t *src_len)
UDP recvfrom with source address capture.
Definition n_network.c:4070
@ NETW_COMPRESS_NONE
no automatic compression on send, still decompresses inbound
Definition n_network.h:328
@ NETW_COMPRESS_ZLIB
compress on send with zlib, decompress either on recv
Definition n_network.h:330
@ NETW_COMPRESS_LZ4
compress on send with LZ4, decompress either on recv
Definition n_network.h:332
a single part of a parsed multipart/form-data body
Definition n_network.h:900
parsed HTTP request for mock server callback
Definition n_network.h:1162
HTTP response to send from mock server callback.
Definition n_network.h:1171
mock HTTP server handle
Definition n_network.h:1178
Parsed proxy URL components.
Definition n_network.h:932
Structure of a N_SOCKET.
Definition n_network.h:352
SSE connection handle.
Definition n_network.h:1137
SSE event received from server.
Definition n_network.h:1127
parsed URL components
Definition n_network.h:1030
a single query parameter key=value pair
Definition n_network.h:1024
WebSocket connection.
Definition n_network.h:1078
A single parsed WebSocket frame (RFC 6455).
Definition n_network.h:1087
WebSocket message.
Definition n_network.h:1071
Structure of a NETWORK.
Definition n_network.h:375
structure for splitting HTTP requests
Definition n_network.h:635
structure of a network pool
Definition n_network.h:625
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
One place deciding which OpenSSL/LibreSSL features the build can use.
N_STR and string function declaration.
Timing utilities.