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
28#ifndef N_NETWORK
29#define N_NETWORK
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
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
87#define NETW_THR_EXIT_OK 100
89#define NETW_THR_EXIT_ERROR 666
90
91#ifndef SOCKET
93#ifdef __windows__
94#ifndef ARCH32BITS
96typedef long long unsigned int SOCKET;
98#define SOCKET_SIZE_FORMAT "%zu"
99#endif
100#else
102typedef int SOCKET;
104#define SOCKET_SIZE_FORMAT "%d"
105#endif
106#endif
107
108#if defined(__linux__) || defined(__sun) || defined(_AIX)
109#include <sys/types.h>
110#include <fcntl.h>
111#include <stdio.h>
112#include <errno.h>
113#include <unistd.h>
114#include <malloc.h>
115#include <string.h>
116#include <netinet/in.h>
117#include <netinet/tcp.h>
118#include <arpa/inet.h>
119#include <netdb.h>
120#include <sys/ioctl.h>
121#include <sys/socket.h>
122#include <sys/select.h>
123#include <semaphore.h>
124
126#define SOCKET_SIZE_FORMAT "%d"
127
128#ifdef __linux__
129#include <linux/sockios.h>
130#include <signal.h>
131#endif
132
133#define netw_unload()
134
136#define closesocket close
138#define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
140#define INVALID_SOCKET -1
142#define NETW_BUFLEN_CAST(x) ((size_t)(x))
143
144#include "n_time.h"
145
146#elif defined __windows__
147
148#define SHUT_WR SD_SEND
149#define SHUT_RD SD_RECEIVE
150#define SHUT_RDWR SD_BOTH
151
152#ifndef ECONNRESET
153#define ECONNRESET 104
154#endif
155
156#include "nilorea/n_windows.h"
157
158#include <errno.h>
159#include <unistd.h>
160#include <malloc.h>
161#include <string.h>
162#include "n_time.h"
163
164#ifndef MSG_EOR
165#define MSG_EOR 0
166#endif
167
168#ifndef MSG_NOSIGNAL
169#define MSG_NOSIGNAL 0
170#endif
171
172#ifndef MSG_WAITALL
173#define MSG_WAITALL 0
174#endif
175
176#ifndef EAI_SYSTEM
177#define EAI_SYSTEM 0
178#endif
179
180#ifndef AI_PASSIVE
181#define AI_PASSIVE 0x00000001
182#endif
183#ifndef AI_CANONNAME
184#define AI_CANONNAME 0x00000002
185#endif
186#ifndef AI_NUMERICHOST
187#define AI_NUMERICHOST 0x00000004
188#endif
189#ifndef AI_NUMERICSERV
190#define AI_NUMERICSERV 0x00000008
191#endif
192#ifndef AI_ALL
193#define AI_ALL 0x00000100
194#endif
195#ifndef AI_ADDRCONFIG
196#define AI_ADDRCONFIG 0x00000400
197#endif
198#ifndef AI_V4MAPPED
199#define AI_V4MAPPED 0x00000800
200#endif
201#ifndef AI_NON_AUTHORITATIVE
202#define AI_NON_AUTHORITATIVE 0x00004000
203#endif
204#ifndef AI_SECURE
205#define AI_SECURE 0x00008000
206#endif
207#ifndef AI_RETURN_PREFERRED_NAMES
208#define AI_RETURN_PREFERRED_NAMES 0x00010000
209#endif
210
211#include <pthread.h>
212#include <semaphore.h>
213
215#define NETFLAGS 0 /* no flags needed for microsoft */
216
218#define NETW_BUFLEN_CAST(x) ((int)(x))
219
221#define netw_unload() netw_init_wsa(0, 2, 2)
222
223#endif
224
225#ifdef HAVE_OPENSSL
226#define _OPEN_SYS_SOCK_IPV6 1
227#include <openssl/err.h>
228#include <openssl/ssl.h>
229#include <openssl/crypto.h>
230#endif
231
233#define N_ENUM_netw_code_type(_) \
234 _(NETW_CLIENT, 2) \
235 _(NETW_SERVER, 4) \
236 _(NETW_RESTART_TIMER, 8) \
237 _(NETW_EMPTY_SENDBUF, 16) \
238 _(NETW_EMPTY_RECVBUF, 32) \
239 _(NETW_RUN, 64) \
240 _(NETW_EXIT_ASKED, 128) \
241 _(NETW_EXITED, 256) \
242 _(NETW_ERROR, 512) \
243 _(NETW_ENCRYPT_NONE, 1024) \
244 _(NETW_ENCRYPT_OPENSSL, 2048) \
245 _(NETW_THR_ENGINE_STARTED, 4096) \
246 _(NETW_THR_ENGINE_STOPPED, 8192) \
247 _(NETW_DESTROY_RECVBUF, 16384) \
248 _(NETW_DESTROY_SENDBUF, 32768) \
249 _(NETW_COMPRESSED_ZLIB, 65536) \
250 _(NETW_COMPRESSED_LZ4, 131072)
251
255#define NETW_COMPRESSED NETW_COMPRESSED_ZLIB
256
271
279#define NETW_COMPRESS_THRESHOLD 256u
280#define NETW_COMPRESS_MIN_RATIO 10u
281
284
286typedef ssize_t (*netw_func)(void*, char*, uint32_t);
287
289typedef struct N_SOCKET {
291 char* port;
295 char* ip;
296
298 unsigned long int is_blocking;
299
301 struct addrinfo hints;
303 struct addrinfo* rhost;
305 struct sockaddr_storage raddr;
306} N_SOCKET;
307
309typedef struct NETWORK {
322 /* disabled so reuseport state */
323 // so_reuseport,
354
356 uint32_t state;
357
373
374#ifdef HAVE_OPENSSL
376 const SSL_METHOD* method;
378 SSL_CTX* ctx;
380 SSL* ssl;
385#endif
386
389
394
396 pthread_t send_thr;
398 pthread_t recv_thr;
399
401 pthread_mutex_t sendbolt;
403 pthread_mutex_t recvbolt;
405 pthread_mutex_t eventbolt;
408
411
413 char netw_errors[8][512];
418
427
438
447
448 /* Incremental recv state for reactor mode. Unused by thread
449 * mode (`netw_recv_func` reads blocking). The state machine
450 * has three phases: STATE word (4 B), LENGTH word (4 B),
451 * PAYLOAD (length B), then push to recv_buf and reset. Per-frame
452 * state lives across multiple readable events because TCP can
453 * deliver any prefix of a frame before EAGAIN. */
455 unsigned char reactor_read_hdr_buf[4];
462 /* Incremental send state for reactor mode. Mirrors
463 * `netw_send_func`'s output: each queued N_STR is pre-framed
464 * (4-byte state + 4-byte length + payload, with optional
465 * compression) into a contiguous buffer; the reactor sends as
466 * the kernel buffer accepts bytes, tracking offset across
467 * multiple writable events. `reactor_write_armed` mirrors
468 * whether EPOLLOUT is in the registered events, used to
469 * arm/disarm exactly once per back-pressure cycle. */
483
493
507
521
529
530} NETWORK;
531
536#define netw_atomic_read_state(netw) __atomic_load_n(&(netw)->state, __ATOMIC_ACQUIRE)
537
541#define netw_atomic_write_state(netw, val) __atomic_store_n(&(netw)->state, (val), __ATOMIC_RELEASE)
542
550#define netw_atomic_read_reactor_mode(netw) __atomic_load_n(&(netw)->reactor_mode, __ATOMIC_ACQUIRE)
551#define netw_atomic_write_reactor_mode(netw, val) __atomic_store_n(&(netw)->reactor_mode, (val), __ATOMIC_RELEASE)
555#define netw_atomic_read_reactor_handle(netw) __atomic_load_n(&(netw)->reactor_handle, __ATOMIC_ACQUIRE)
556#define netw_atomic_write_reactor_handle(netw, val) __atomic_store_n(&(netw)->reactor_handle, (val), __ATOMIC_RELEASE)
557
559typedef struct NETWORK_POOL {
562
564 pthread_rwlock_t rwlock;
565
567
569typedef struct NETWORK_HTTP_INFO {
571 char content_type[256];
575 char* body;
577 char* type;
579
581size_t htonst(size_t value);
583size_t ntohst(size_t value);
584#ifdef HAVE_OPENSSL
586int netw_set_crypto(NETWORK* netw, char* key, char* certificate);
588int netw_set_crypto_pem(NETWORK* netw, const char* key_pem, const char* cert_pem);
590int netw_set_crypto_chain(NETWORK* netw, char* key, char* certificate, char* ca_file);
592int netw_set_crypto_chain_pem(NETWORK* netw, const char* key_pem, const char* cert_pem, const char* ca_pem);
594int netw_ssl_set_ca(NETWORK* netw, const char* ca_file, const char* ca_path);
596int netw_ssl_set_verify(NETWORK* netw, int enable);
598int netw_ssl_get_verify_result(NETWORK* netw, const char* expected_host, char* errbuf, size_t errsz);
600int netw_ssl_set_client_cert(NETWORK* netw, const char* cert_file, const char* key_file);
602int netw_init_openssl(void);
607typedef int (*netw_sni_pick_cb)(const char* sni, N_STR** cert_pem, N_STR** key_pem, void* user_data);
609const char* netw_ssl_get_sni(NETWORK* netw);
611int netw_set_crypto_pem_ctx(NETWORK* netw, const N_STR* key_pem, const N_STR* cert_pem);
613NETWORK* netw_accept_ssl_with_sni_cb(NETWORK* listen, netw_sni_pick_cb pick, void* user_data);
615int netw_ssl_server_handshake(NETWORK* netw, netw_sni_pick_cb pick, void* user_data);
617int netw_unload_openssl(void);
619int netw_ssl_connect(NETWORK** netw, char* host, char* port, int ip_version, char* ssl_key_file, char* ssl_cert_file);
621int netw_ssl_connect_client(NETWORK** netw, char* host, char* port, int ip_version);
623int netw_ssl_connect_client_to(NETWORK** netw, char* host, char* port, int ip_version, int connect_timeout_ms);
627int netw_ssl_do_handshake(NETWORK* netw, const char* sni_hostname);
629ssize_t send_ssl_data(void* netw, char* buf, uint32_t n);
631ssize_t recv_ssl_data(void* netw, char* buf, uint32_t n);
633ssize_t send_ssl_data_once(void* netw, char* buf, uint32_t n);
635ssize_t recv_ssl_data_once(void* netw, char* buf, uint32_t n);
636#endif
638int netw_init_wsa(int mode, int v1, int v2);
640int netw_set(NETWORK* netw, int flag);
642int netw_get_state(NETWORK* netw, uint32_t* state, int* thr_engine_status);
644int netw_setsockopt(NETWORK* netw, int optname, int value);
646int netw_set_blocking(NETWORK* netw, unsigned long int is_blocking);
653int 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);
655int 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);
657int netw_connect(NETWORK** netw, char* host, char* port, int ip_version);
659int netw_connect_to(NETWORK** netw, char* host, char* port, int ip_version, int connect_timeout_ms);
661void netw_set_connect_abort_cb(int (*cb)(void* ctx), void* ctx);
663int netw_adopt_client_fd(NETWORK** netw, SOCKET fd, const char* host, const char* port);
665int deplete_send_buffer(int fd, int timeout);
667int netw_bind_udp(NETWORK** netw, char* addr, char* port, int ip_version);
669int netw_connect_udp(NETWORK** netw, char* host, char* port, int ip_version);
671ssize_t send_udp_data(void* netw, char* buf, uint32_t n);
673ssize_t recv_udp_data(void* netw, char* buf, uint32_t n);
675ssize_t netw_udp_sendto(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* dest_addr, socklen_t dest_len);
677ssize_t netw_udp_recvfrom(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* src_addr, socklen_t* src_len);
679int netw_close(NETWORK** netw);
683int netw_wait_close_timed(NETWORK** netw, size_t timeout);
687int netw_make_listening(NETWORK** netw, char* addr, char* port, int nbpending, int ip_version);
689NETWORK* netw_accept_from_ex(NETWORK* from, size_t send_list_limit, size_t recv_list_limit, int blocking, int* retval);
693NETWORK* netw_accept_nonblock_from(NETWORK* from, int blocking);
695int netw_add_msg(NETWORK* netw, N_STR* msg);
697int netw_add_msg_ex(NETWORK* netw, char* str, unsigned int length);
701N_STR* netw_wait_msg(NETWORK* netw, unsigned int refresh, size_t timeout);
705void* netw_send_func(void* NET);
707void* netw_recv_func(void* NET);
709ssize_t send_data(void* netw, char* buf, uint32_t n);
711ssize_t recv_data(void* netw, char* buf, uint32_t n);
713ssize_t send_data_once(void* netw, char* buf, uint32_t n);
715ssize_t recv_data_once(void* netw, char* buf, uint32_t n);
717ssize_t send_php(SOCKET s, int _code, char* buf, int n);
719ssize_t recv_php(SOCKET s, int* _code, char** buf);
721int netw_get_queue_status(NETWORK* netw, size_t* nb_to_send, size_t* nb_to_read);
722
724NETWORK_POOL* netw_new_pool(size_t nb_min_element);
726int netw_destroy_pool(NETWORK_POOL** netw_pool);
728void netw_pool_netw_close(void* netw_ptr);
730int netw_pool_add(NETWORK_POOL* netw_pool, NETWORK* netw);
732int netw_pool_remove(NETWORK_POOL* netw_pool, NETWORK* netw);
734int netw_pool_broadcast(NETWORK_POOL* netw_pool, const NETWORK* from, N_STR* net_msg);
736size_t netw_pool_nbclients(NETWORK_POOL* netw_pool);
737
739int netw_set_user_id(NETWORK* netw, int id);
740
742int netw_send_ping(NETWORK* netw, int type, int id_from, int id_to, int time);
744int netw_send_ident(NETWORK* netw, int type, int id, N_STR* name, N_STR* passwd);
746int netw_send_position(NETWORK* netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp);
748int netw_send_string_to(NETWORK* netw, int id_to, N_STR* name, N_STR* chan, N_STR* txt, int color);
750int netw_send_string_to_all(NETWORK* netw, N_STR* name, N_STR* chan, N_STR* txt, int color);
753
755size_t netw_calculate_urlencoded_size(const char* str, size_t len);
757char* netw_extract_http_request_type(const char* request);
761int netw_info_destroy(NETWORK_HTTP_INFO http_request);
763char* netw_urlencode(const char* str, size_t len);
765int netw_get_url_from_http_request(const char* request, char* url, size_t size);
767char* netw_urldecode(const char* str);
769HASH_TABLE* netw_parse_post_data(const char* post_data);
771const char* netw_guess_http_content_type(const char* url);
773const char* netw_get_http_status_message(int status_code);
775int n_http_status_class(int status_code);
777int n_http_status_is_informational(int status_code);
779int n_http_status_is_success(int status_code);
781int n_http_status_is_redirect(int status_code);
783int n_http_status_is_client_error(int status_code);
785int n_http_status_is_server_error(int status_code);
787int n_http_decompress_body(const N_STR* body, const char* encoding, N_STR** out);
788
796
798int n_http_multipart_boundary(const char* content_type, char* out, size_t outsz);
800LIST* n_http_parse_multipart(const N_STR* body, const char* boundary);
802N_STR* n_http_build_multipart(LIST* parts, const char* boundary);
804void n_http_multipart_free(LIST** parts);
806int netw_get_http_date(char* buffer, size_t buffer_size);
808int 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);
809
813typedef struct N_PROXY_CFG {
814 char* scheme;
815 char* host;
816 int port;
817 char* username;
818 char* password;
820
836N_PROXY_CFG* n_proxy_cfg_parse(const char* url);
837
842void n_proxy_cfg_free(N_PROXY_CFG** cfg);
843
861int n_proxy_connect_tunnel(const N_PROXY_CFG* proxy,
862 const char* target_host,
863 int target_port);
864
865#ifdef HAVE_OPENSSL
881 const char* target_host,
882 int target_port);
883#endif /* HAVE_OPENSSL */
884
897int n_proxy_connect_socks5(const N_PROXY_CFG* proxy,
898 const char* target_host,
899 int target_port);
900
902#define N_URL_MAX_PARAMS 64
903
905typedef struct N_URL_PARAM {
906 char* key;
907 char* value;
909
911typedef struct N_URL {
912 char* scheme;
913 char* host;
914 int port;
915 char* path;
916 char* query;
919} N_URL;
920
922N_URL* n_url_parse(const char* url);
924N_STR* n_url_build(const N_URL* u);
928N_STR* n_url_canonicalize_string(const char* url);
930N_STR* n_url_resolve(const char* base, const char* ref);
932N_STR* n_url_encode(const char* str);
934N_STR* n_url_decode(const char* str);
936void n_url_free(N_URL** u);
937
939#define N_WS_OP_TEXT 0x01
940#define N_WS_OP_BINARY 0x02
941#define N_WS_OP_CLOSE 0x08
942#define N_WS_OP_PING 0x09
943#define N_WS_OP_PONG 0x0A
944
951
953typedef struct N_WS_CONN {
956 char* host;
957 char* path;
958} N_WS_CONN;
959
962typedef struct N_WS_FRAME {
963 int fin;
964 int rsv;
965 int opcode;
966 int masked;
967 unsigned char mask[4];
968 uint64_t payload_len;
969 const unsigned char* payload;
970} N_WS_FRAME;
971
975int n_ws_frame_parse(const unsigned char* buf, size_t len, N_WS_FRAME* frame, size_t* consumed);
976
978void n_ws_unmask(unsigned char* dst, const unsigned char* src, size_t len, const unsigned char mask[4]);
979
982size_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);
983
984#ifdef HAVE_OPENSSL
986N_WS_CONN* n_ws_connect(const char* host, const char* port, const char* path, int use_ssl);
987
989int n_ws_send(N_WS_CONN* conn, const char* payload, size_t len, int opcode);
990
992int n_ws_recv(N_WS_CONN* conn, N_WS_MESSAGE* msg_out);
993
995void n_ws_close(N_WS_CONN* conn);
996
998void n_ws_conn_free(N_WS_CONN** conn);
999#endif
1000
1008
1009struct N_SSE_CONN;
1010
1012typedef struct N_SSE_CONN {
1014 volatile int stop_flag;
1015 void (*on_event)(N_SSE_EVENT* event, struct N_SSE_CONN* conn, void* user_data);
1017} N_SSE_CONN;
1018
1019#ifdef HAVE_OPENSSL
1024N_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);
1025
1027void n_sse_stop(N_SSE_CONN* conn);
1028
1030void n_sse_conn_free(N_SSE_CONN** conn);
1031
1033void n_sse_event_clean(N_SSE_EVENT* event);
1034#endif
1035
1037typedef struct N_HTTP_REQUEST {
1038 char method[16];
1039 char path[2048];
1040 char query[2048];
1044
1051
1060
1063 void (*on_request)(N_HTTP_REQUEST*, N_HTTP_RESPONSE*, void*),
1064 void* user_data);
1065
1068
1071
1074
1075/* per-NETWORK error capture */
1076
1080const char* n_netw_get_error(const NETWORK* netw, int index);
1083
1084/* pre-connection error capture (thread-local, no NETWORK yet) */
1085
1089const char* n_netw_get_connect_error(int index);
1092
1097#ifdef __cplusplus
1098}
1099#endif
1100
1101#endif /*#ifndef N_NETWORK*/
static int mode
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
static void on_request(N_HTTP_REQUEST *req, N_HTTP_RESPONSE *resp, void *user_data)
Request handler: returns JSON for GET /api/test, 404 otherwise.
char * ca_file
NETWORK * server
char * key
int ip_version
char * addr
char * port
#define N_ENUM_DECLARE(MACRO_DEFINITION, enum_name)
Macro to declare a N_ENUM.
Definition n_enum.h:155
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:482
volatile int stop_flag
atomic stop flag
Definition n_network.h:1014
char query[2048]
query string (or empty)
Definition n_network.h:1040
struct addrinfo * rhost
getaddrinfo results
Definition n_network.h:303
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
Definition n_network.h:339
char * ip
ip of the connected socket
Definition n_network.h:295
N_SOCKET link
networking socket
Definition n_network.h:388
char * certificate
openssl certificate file
Definition n_network.h:382
char netw_errors[8][512]
per-connection error capture ring buffer (max 8 entries, 512 chars each)
Definition n_network.h:413
int threaded_engine_status
Threaded network engine state for this network.
Definition n_network.h:315
size_t content_length
Store content length.
Definition n_network.h:573
char * type
Type of request.
Definition n_network.h:577
pthread_t send_thr
sending thread
Definition n_network.h:396
char * reactor_read_payload
malloc'd accumulator for payload bytes
Definition n_network.h:459
int compress_mode
Per-packet compression mode, see NETW_COMPRESS_MODE.
Definition n_network.h:426
int nb_pending
Nb pending connection,if listening.
Definition n_network.h:311
char * filename
Content-Disposition filename, or "".
Definition n_network.h:792
int reactor_write_armed
1 = EPOLLOUT currently registered
Definition n_network.h:473
int so_reuseaddr
so reuseaddr state
Definition n_network.h:321
size_t reactor_send_off
bytes already sent to socket
Definition n_network.h:472
pthread_t recv_thr
receiving thread
Definition n_network.h:398
int port
port number (0 if not specified)
Definition n_network.h:914
int masked
MASK bit (client-to-server frames are masked)
Definition n_network.h:966
char * host
proxy hostname
Definition n_network.h:815
int reactor_read_hdr_have
bytes accumulated in reactor_read_hdr_buf
Definition n_network.h:456
pthread_rwlock_t rwlock
thread safety
Definition n_network.h:564
long long connect_dns_usec
Measured DNS resolution time of the most recent connect, in microseconds (0 if not measured).
Definition n_network.h:525
NETWORK * netw
underlying network connection
Definition n_network.h:1013
struct sockaddr_storage raddr
connected remote addr
Definition n_network.h:305
uint32_t reactor_read_pkt_state
state word for the in-flight frame
Definition n_network.h:457
char * host
hostname
Definition n_network.h:913
char * path
path starting with "/", or "/" if empty
Definition n_network.h:915
int reactor_read_phase
0=STATE, 1=LENGTH, 2=PAYLOAD
Definition n_network.h:454
pthread_mutex_t eventbolt
mutex for threaded access of state event
Definition n_network.h:405
int port
listening port
Definition n_network.h:1058
char * content_type
the part's Content-Type, or ""
Definition n_network.h:793
const SSL_METHOD * method
SSL method container.
Definition n_network.h:376
N_STR * body
response body
Definition n_network.h:1049
int netw_err_next
next write slot in ring buffer
Definition n_network.h:417
int deplete_socket_timeout
deplete socket send buffer timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:349
void * user_data
user data for handler
Definition n_network.h:1057
char * scheme
"http" or "https"
Definition n_network.h:912
int deplete_queues_timeout
deplete network queues timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:347
int nb_running_threads
nb running threads, if > 0 thread engine is still running
Definition n_network.h:345
int in_dirty_list
Dirty-list membership flag.
Definition n_network.h:492
int reactor_close_acked
Close handshake.
Definition n_network.h:506
int opcode
opcode (N_WS_OP_*)
Definition n_network.h:965
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
Definition n_network.h:403
NETWORK * netw
underlying network connection
Definition n_network.h:954
void(* on_event)(N_SSE_EVENT *event, struct N_SSE_CONN *conn, void *user_data)
callback
Definition n_network.h:1015
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
Definition n_network.h:401
N_STR * body
the part's raw body bytes (binary-safe), or NULL
Definition n_network.h:794
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
Definition n_network.h:319
void * reactor_handle
Back-pointer to the registered reactor.
Definition n_network.h:446
int fin
FIN bit (1 = final fragment)
Definition n_network.h:963
void(* on_request)(N_HTTP_REQUEST *req, N_HTTP_RESPONSE *resp, void *user_data)
request handler
Definition n_network.h:1056
N_STR * event
event type (or NULL for default)
Definition n_network.h:1003
int connected
1 if handshake completed
Definition n_network.h:955
N_STR * data
event data
Definition n_network.h:1004
N_STR * body
request body (or NULL)
Definition n_network.h:1042
char * key
parameter name
Definition n_network.h:906
int so_rcvtimeo
send timeout value
Definition n_network.h:335
uint64_t payload_len
payload length in bytes
Definition n_network.h:968
uint32_t reactor_read_pkt_length
payload length for the in-flight frame
Definition n_network.h:458
char * password
NULL if no auth.
Definition n_network.h:818
char * query
raw query string without leading '?', or NULL
Definition n_network.h:916
int port
proxy port
Definition n_network.h:816
N_URL_PARAM params[64]
parsed key=value pairs
Definition n_network.h:917
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
Definition n_network.h:327
char * body
Pointer to the body data.
Definition n_network.h:575
size_t reactor_read_payload_have
bytes accumulated into reactor_read_payload
Definition n_network.h:460
LIST * headers
list of char* "Name: Value" strings
Definition n_network.h:1041
char * value
parameter value
Definition n_network.h:907
char * host
remote hostname
Definition n_network.h:956
int netw_err_count
number of captured errors
Definition n_network.h:415
char * reactor_send_buf
malloc'd framed bytes, NULL when idle
Definition n_network.h:470
SOCKET sock
a normal socket
Definition n_network.h:293
char path[2048]
request path
Definition n_network.h:1039
char * scheme
"http", "https", or "socks5"
Definition n_network.h:814
char * port
port of socket
Definition n_network.h:291
netw_func recv_data_once
single-attempt recv, same contract as send_data_once.
Definition n_network.h:372
LIST * recv_buf
reveicing buffer (for incomming usage)
Definition n_network.h:393
int transport_type
transport type: NETWORK_TCP (0) or NETWORK_UDP (1)
Definition n_network.h:353
int nb_params
number of parsed parameters
Definition n_network.h:918
char * name
Content-Disposition form-field name, or "".
Definition n_network.h:791
int user_id
if part of a user property, id of the user
Definition n_network.h:343
sem_t send_blocker
block sending func
Definition n_network.h:407
SSL_CTX * ctx
SSL context holder.
Definition n_network.h:378
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
Definition n_network.h:329
int so_sndtimeo
send timeout value
Definition n_network.h:333
long long connect_tcp_usec
Measured TCP connect time of the most recent connect, in microseconds (0 if not measured).
Definition n_network.h:528
const unsigned char * payload
pointer into the input buffer (still masked)
Definition n_network.h:969
int retry
retry interval in ms (0 if not set)
Definition n_network.h:1006
char content_type[256]
Store content type.
Definition n_network.h:571
size_t reactor_send_len
total bytes in reactor_send_buf
Definition n_network.h:471
N_STR * id
last event ID (or NULL)
Definition n_network.h:1005
struct addrinfo hints
address of local machine
Definition n_network.h:301
int reactor_registered
Reactor close-handshake latch.
Definition n_network.h:520
int so_keepalive
so keepalive state
Definition n_network.h:325
int reactor_mode
Opt-in epoll-reactor mode flag.
Definition n_network.h:437
netw_func send_data
send func ptr
Definition n_network.h:359
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
Definition n_network.h:317
char * path
resource path
Definition n_network.h:957
int rsv
the three reserved bits, rsv1<<2 | rsv2<<1 | rsv3
Definition n_network.h:964
N_STR * payload
message payload
Definition n_network.h:948
char method[16]
HTTP method.
Definition n_network.h:1038
int opcode
frame opcode
Definition n_network.h:947
unsigned char mask[4]
masking key when masked, else zeroed
Definition n_network.h:967
int masked
1 if masked
Definition n_network.h:949
uint32_t state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
Definition n_network.h:356
LIST * pools
pointers to network pools if members of any
Definition n_network.h:410
char * username
NULL if no auth.
Definition n_network.h:817
netw_func send_data_once
single-attempt send (non-blocking / reactor use).
Definition n_network.h:369
int status_code
HTTP status code.
Definition n_network.h:1047
char * key
openssl key file
Definition n_network.h:384
SSL * ssl
SSL handle.
Definition n_network.h:380
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
Definition n_network.h:337
unsigned long int is_blocking
flag to quickly check socket mode
Definition n_network.h:298
unsigned char reactor_read_hdr_buf[4]
accumulator for header words
Definition n_network.h:455
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:478
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
Definition n_network.h:341
char content_type[128]
Content-Type header value.
Definition n_network.h:1048
HASH_TABLE * pool
table of clients
Definition n_network.h:561
NETWORK * listener
listening network
Definition n_network.h:1054
LIST * send_buf
sending buffer (for outgoing queuing )
Definition n_network.h:391
int mode
NETWORK mode , 1 listening, 0 connecting.
Definition n_network.h:313
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
Definition n_network.h:331
void * user_data
user data for callback
Definition n_network.h:1016
netw_func recv_data
receive func ptr
Definition n_network.h:361
int wait_close_timeout
network wait close timeout value ( < 1 disabled, >= 1 timeout sec )
Definition n_network.h:351
volatile int stop_flag
atomic stop flag
Definition n_network.h:1055
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:5258
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:4620
#define N_URL_MAX_PARAMS
maximum number of parsed query parameters
Definition n_network.h:902
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:3666
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:6049
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:3569
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:1921
const char * n_netw_get_connect_error(int index)
Get pre-connection error message by index.
Definition n_network.c:124
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
SSL Writing to a socket.
Definition n_network.c:4409
char * netw_extract_http_request_type(const char *request)
extract HTTP request type
Definition n_network.c:5347
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:1729
int netw_get_queue_status(NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
get queue status
Definition n_network.c:4964
int netw_bind_udp(NETWORK **netw, char *addr, char *port, int ip_version)
Create a UDP bound socket.
Definition n_network.c:3005
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:1485
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:4694
int netw_init_wsa(int mode, int v1, int v2)
Used by Init & Close network.
Definition n_network.c:839
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable peer certificate verification
Definition n_network.c:1892
ssize_t send_php(SOCKET s, int _code, char *buf, int n)
sending to php
Definition n_network.c:4777
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receiving thread.
Definition n_network.c:4130
void n_ws_close(N_WS_CONN *conn)
send close frame and close the connection
Definition n_network.c:6551
char * netw_urlencode(const char *str, size_t len)
URL-encode a string.
Definition n_network.c:5314
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:7517
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:5668
void * netw_send_func(void *NET)
Thread Sending management function.
Definition n_network.c:3781
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
Accepting routine.
Definition n_network.c:3554
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
get URL from HTTP request
Definition n_network.c:5466
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:7597
#define N_ENUM_netw_code_type(_)
Network codes definition.
Definition n_network.h:233
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
set SSL
Definition n_network.c:1385
NETW_COMPRESS_MODE
Compression mode selector for NETWORK.compress_mode.
Definition n_network.h:263
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:1615
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:8040
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:1867
void n_sse_stop(N_SSE_CONN *conn)
signal the SSE connection to stop reading
Definition n_network.c:6617
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:4354
void n_http_multipart_free(LIST **parts)
free a LIST returned by n_http_parse_multipart and NULL the pointer
Definition n_network.c:5850
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:6952
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:2190
N_STR * n_url_canonicalize_string(const char *url)
parse and canonicalize a URL string in one call
Definition n_network.c:7395
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:7433
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:4298
int netw_get_http_date(char *buffer, size_t buffer_size)
get current HTTP date string
Definition n_network.c:6078
NETWORK_POOL * netw_new_pool(size_t nb_min_element)
init pools
Definition n_network.c:4983
int netw_set_user_id(NETWORK *netw, int id)
set user id on a netw
Definition n_network.c:5158
void n_mock_server_free(N_MOCK_SERVER **server)
free a mock server and close the listening socket
Definition n_network.c:7224
ssize_t recv_data(void *netw, char *buf, uint32_t n)
Reading from a socket.
Definition n_network.c:4231
int netw_init_openssl(void)
init ssl helper
Definition n_network.c:1333
void n_netw_clear_errors(NETWORK *netw)
Clear captured errors on a NETWORK handle.
Definition n_network.c:114
ssize_t recv_ssl_data(void *netw, char *buf, uint32_t n)
SSL Reading from a socket.
Definition n_network.c:4504
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Listening network.
Definition n_network.c:2885
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:2496
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:3218
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:891
int netw_start_thr_engine(NETWORK *netw)
Create the sending and receiving thread of a NETWORK.
Definition n_network.c:3740
int netw_destroy_pool(NETWORK_POOL **netw_pool)
destroy pool
Definition n_network.c:5002
void n_url_free(N_URL **u)
free a N_URL and all its members
Definition n_network.c:7542
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:6110
void * netw_recv_func(void *NET)
Thread Receiving management function.
Definition n_network.c:3964
void n_sse_conn_free(N_SSE_CONN **conn)
free an SSE connection
Definition n_network.c:6626
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:7776
N_STR * n_url_decode(const char *str)
decode a percent-encoded string (returns N_STR)
Definition n_network.c:7521
int n_netw_get_error_count(const NETWORK *netw)
Get number of captured errors on a NETWORK handle.
Definition n_network.c:104
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:6905
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:6208
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:5690
size_t htonst(size_t value)
host to network size_t
Definition n_network.c:143
int netw_unload_openssl(void)
unload ssl helper
Definition n_network.c:1366
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:1715
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:5686
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:7902
size_t ntohst(size_t value)
network to host size_t
Definition n_network.c:160
ssize_t(* netw_func)(void *, char *, uint32_t)
send/recv func ptr type
Definition n_network.h:286
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:3295
size_t netw_pool_nbclients(NETWORK_POOL *netw_pool)
get nb clients
Definition n_network.c:5141
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:607
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:7703
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:3358
void n_netw_clear_connect_errors(void)
Clear pre-connection errors on this thread.
Definition n_network.c:130
void n_mock_server_stop(N_MOCK_SERVER *server)
signal the mock server to stop accepting connections
Definition n_network.c:7215
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:2347
int SOCKET
default socket declaration
Definition n_network.h:102
int netw_pool_broadcast(NETWORK_POOL *netw_pool, const NETWORK *from, N_STR *net_msg)
broadcast message to pool
Definition n_network.c:5117
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:5751
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:2409
const char * n_netw_get_error(const NETWORK *netw, int index)
Get captured error message by index (0 = oldest).
Definition n_network.c:108
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:6605
int netw_setsockopt(NETWORK *netw, int optname, int value)
Set common socket options (disable naggle, send/recv buf, reuse addr)
Definition n_network.c:967
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:5811
int netw_set(NETWORK *netw, int flag)
Set flags on network.
Definition n_network.c:2554
ssize_t send_data(void *netw, char *buf, uint32_t n)
Writing to a socket.
Definition n_network.c:4170
ssize_t recv_udp_data(void *netw, char *buf, uint32_t n)
UDP recv data from socket.
Definition n_network.c:3259
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:1846
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get flags from network.
Definition n_network.c:2532
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:6711
N_URL * n_url_parse(const char *url)
parse a URL string into components
Definition n_network.c:7255
int n_http_status_is_informational(int status_code)
1 when status_code is informational (1xx), else 0
Definition n_network.c:5674
void netw_pool_netw_close(void *netw_ptr)
close pool
Definition n_network.c:5021
int n_netw_get_connect_error_count(void)
Get number of pre-connection errors captured on this thread.
Definition n_network.c:120
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
calculate URL-encoded size
Definition n_network.c:5291
int deplete_send_buffer(int fd, int timeout)
wait for send buffer to be empty
Definition n_network.c:2631
NETWORK * netw_accept_from(NETWORK *from)
Accepting routine.
Definition n_network.c:3544
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:7366
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
Definition n_network.c:7692
int netw_close(NETWORK **netw)
Closing.
Definition n_network.c:2662
int n_http_status_is_success(int status_code)
1 when status_code is success (2xx), else 0
Definition n_network.c:5678
N_STR * n_url_build(const N_URL *u)
build a URL string from parsed components
Definition n_network.c:7294
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:7130
int netw_send_quit(NETWORK *netw)
send quit message
Definition n_network.c:5274
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:2481
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:2438
const char * netw_get_http_status_message(int status_code)
get HTTP status message string
Definition n_network.c:5650
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:1580
int n_ws_recv(N_WS_CONN *conn, N_WS_MESSAGE *msg_out)
receive one WebSocket frame
Definition n_network.c:6479
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
set blocking mode
Definition n_network.c:909
N_STR * netw_wait_msg(NETWORK *netw, unsigned int refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition n_network.c:3688
int n_http_status_is_redirect(int status_code)
1 when status_code is a redirect (3xx), else 0
Definition n_network.c:5682
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:5910
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:5238
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
send ping message
Definition n_network.c:5173
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:2389
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract HTTP info from request
Definition n_network.c:5380
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:6960
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:1790
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Connecting.
Definition n_network.c:2359
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:7096
__netw_code_type
Network codes declaration.
Definition n_network.h:283
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:2373
int netw_send_ident(NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
send ident message
Definition n_network.c:5192
char * netw_urldecode(const char *str)
URL-decode a string.
Definition n_network.c:5503
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:1988
void n_ws_conn_free(N_WS_CONN **conn)
free a WebSocket connection structure
Definition n_network.c:6581
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add network to pool
Definition n_network.c:5034
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:3113
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy HTTP info structure
Definition n_network.c:5453
HASH_TABLE * netw_parse_post_data(const char *post_data)
parse POST data into hash table
Definition n_network.c:5540
const char * netw_guess_http_content_type(const char *url)
guess HTTP content type from URL
Definition n_network.c:5586
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:6412
ssize_t recv_php(SOCKET s, int *_code, char **buf)
receive from php
Definition n_network.c:4859
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:5216
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove network from pool
Definition n_network.c:5080
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:2172
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:3635
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:3327
@ NETW_COMPRESS_NONE
no automatic compression on send, still decompresses inbound
Definition n_network.h:265
@ NETW_COMPRESS_ZLIB
compress on send with zlib, decompress either on recv
Definition n_network.h:267
@ NETW_COMPRESS_LZ4
compress on send with LZ4, decompress either on recv
Definition n_network.h:269
a single part of a parsed multipart/form-data body
Definition n_network.h:790
parsed HTTP request for mock server callback
Definition n_network.h:1037
HTTP response to send from mock server callback.
Definition n_network.h:1046
mock HTTP server handle
Definition n_network.h:1053
Parsed proxy URL components.
Definition n_network.h:813
Structure of a N_SOCKET.
Definition n_network.h:289
SSE connection handle.
Definition n_network.h:1012
SSE event received from server.
Definition n_network.h:1002
parsed URL components
Definition n_network.h:911
a single query parameter key=value pair
Definition n_network.h:905
WebSocket connection.
Definition n_network.h:953
A single parsed WebSocket frame (RFC 6455).
Definition n_network.h:962
WebSocket message.
Definition n_network.h:946
Structure of a NETWORK.
Definition n_network.h:309
structure for splitting HTTP requests
Definition n_network.h:569
structure of a network pool
Definition n_network.h:559
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
N_STR and string function declaration.
Timing utilities.