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 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef N_NETWORK
28#define N_NETWORK
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39#include "n_common.h"
40#include "n_str.h"
41#include "n_list.h"
42#include "n_hash.h"
43
45#define NETWORK_DISABLE_ZERO_LENGTH_RECV FALSE
47#define NETWORK_IPALL 0
49#define NETWORK_IPV4 1
51#define NETWORK_IPV6 2
53#define NETWORK_TCP 0
55#define NETWORK_UDP 1
57#define NETWORK_DEPLETE_SOCKET_TIMEOUT 512
59#define NETWORK_DEPLETE_QUEUES_TIMEOUT 1024
61#define NETWORK_CONSECUTIVE_SEND_WAIT 2048
63#define NETWORK_WAIT_CLOSE_TIMEOUT 4096
65#define HEAD_SIZE 10
67#define HEAD_CODE 3
69#define NETW_SOCKET_ERROR -1
71#define NETW_SOCKET_DISCONNECTED -2
73#define NETW_MAX_RETRIES 8
75#define NETW_RETRY_DELAY 1000
76
77#ifndef SOCKET
79#ifdef __windows__
80#ifndef ARCH32BITS
82typedef long long unsigned int SOCKET;
84#define SOCKET_SIZE_FORMAT "%zu"
85#endif
86#else
88typedef int SOCKET;
90#define SOCKET_SIZE_FORMAT "%d"
91#endif
92#endif
93
94#if defined(__linux__) || defined(__sun) || defined(_AIX)
95#include <sys/types.h>
96#include <fcntl.h>
97#include <stdio.h>
98#include <errno.h>
99#include <unistd.h>
100#include <malloc.h>
101#include <string.h>
102#include <netinet/in.h>
103#include <netinet/tcp.h>
104#include <arpa/inet.h>
105#include <netdb.h>
106#include <sys/ioctl.h>
107#include <sys/socket.h>
108#include <sys/select.h>
109#include <semaphore.h>
110
112#define SOCKET_SIZE_FORMAT "%d"
113
114#ifdef __linux__
115#include <linux/sockios.h>
116#include <signal.h>
117#endif
118
119#define netw_unload()
120
122#define closesocket close
124#define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
126#define INVALID_SOCKET -1
127
128#include "n_time.h"
129
130#elif defined __windows__
131
132#define SHUT_WR SD_SEND
133#define SHUT_RD SD_RECEIVE
134#define SHUT_RDWR SD_BOTH
135
136#ifndef ECONNRESET
137#define ECONNRESET 104
138#endif
139
140#include "nilorea/n_windows.h"
141
142#include <errno.h>
143#include <unistd.h>
144#include <malloc.h>
145#include <string.h>
146#include "n_time.h"
147
148#ifndef MSG_EOR
149#define MSG_EOR 0
150#endif
151
152#ifndef MSG_NOSIGNAL
153#define MSG_NOSIGNAL 0
154#endif
155
156#ifndef MSG_WAITALL
157#define MSG_WAITALL 0
158#endif
159
160#ifndef EAI_SYSTEM
161#define EAI_SYSTEM 0
162#endif
163
164#ifndef AI_PASSIVE
165#define AI_PASSIVE 0x00000001
166#endif
167#ifndef AI_CANONNAME
168#define AI_CANONNAME 0x00000002
169#endif
170#ifndef AI_NUMERICHOST
171#define AI_NUMERICHOST 0x00000004
172#endif
173#ifndef AI_NUMERICSERV
174#define AI_NUMERICSERV 0x00000008
175#endif
176#ifndef AI_ALL
177#define AI_ALL 0x00000100
178#endif
179#ifndef AI_ADDRCONFIG
180#define AI_ADDRCONFIG 0x00000400
181#endif
182#ifndef AI_V4MAPPED
183#define AI_V4MAPPED 0x00000800
184#endif
185#ifndef AI_NON_AUTHORITATIVE
186#define AI_NON_AUTHORITATIVE 0x00004000
187#endif
188#ifndef AI_SECURE
189#define AI_SECURE 0x00008000
190#endif
191#ifndef AI_RETURN_PREFERRED_NAMES
192#define AI_RETURN_PREFERRED_NAMES 0x00010000
193#endif
194
195#include <pthread.h>
196#include <semaphore.h>
197
199#define NETFLAGS 0 /* no flags needed for microsoft */
200
202#define netw_unload() netw_init_wsa(0, 2, 2)
203
204#endif
205
206#ifdef HAVE_OPENSSL
207#define _OPEN_SYS_SOCK_IPV6 1
208#include <openssl/err.h>
209#include <openssl/ssl.h>
210#include <openssl/crypto.h>
211#endif
212
214#define N_ENUM_netw_code_type(_) \
215 _(NETW_CLIENT, 2) \
216 _(NETW_SERVER, 4) \
217 _(NETW_RESTART_TIMER, 8) \
218 _(NETW_EMPTY_SENDBUF, 16) \
219 _(NETW_EMPTY_RECVBUF, 32) \
220 _(NETW_RUN, 64) \
221 _(NETW_EXIT_ASKED, 128) \
222 _(NETW_EXITED, 256) \
223 _(NETW_ERROR, 512) \
224 _(NETW_ENCRYPT_NONE, 1024) \
225 _(NETW_ENCRYPT_OPENSSL, 2048) \
226 _(NETW_THR_ENGINE_STARTED, 4096) \
227 _(NETW_THR_ENGINE_STOPPED, 8192) \
228 _(NETW_DESTROY_RECVBUF, 16384) \
229 _(NETW_DESTROY_SENDBUF, 32768)
230
233
235typedef ssize_t (*netw_func)(void*, char*, uint32_t);
236
238typedef struct N_SOCKET {
240 char* port;
244 char* ip;
245
247 unsigned long int is_blocking;
248
250 struct addrinfo hints;
252 struct addrinfo* rhost;
254 struct sockaddr_storage raddr;
255} N_SOCKET;
256
358
363#define netw_atomic_read_state(netw) __atomic_load_n(&(netw)->state, __ATOMIC_ACQUIRE)
364
368#define netw_atomic_write_state(netw, val) __atomic_store_n(&(netw)->state, (val), __ATOMIC_RELEASE)
369
371typedef struct NETWORK_POOL {
374
376 pthread_rwlock_t rwlock;
377
379
381typedef struct NETWORK_HTTP_INFO {
383 char content_type[256];
387 char* body;
389 char* type;
391
393size_t htonst(size_t value);
395size_t ntohst(size_t value);
396#ifdef HAVE_OPENSSL
398int netw_set_crypto(NETWORK* netw, char* key, char* certificate);
400int netw_set_crypto_pem(NETWORK* netw, const char* key_pem, const char* cert_pem);
402int netw_set_crypto_chain(NETWORK* netw, char* key, char* certificate, char* ca_file);
404int netw_set_crypto_chain_pem(NETWORK* netw, const char* key_pem, const char* cert_pem, const char* ca_pem);
406int netw_ssl_set_ca(NETWORK* netw, const char* ca_file, const char* ca_path);
408int netw_ssl_set_verify(NETWORK* netw, int enable);
410int netw_ssl_set_client_cert(NETWORK* netw, const char* cert_file, const char* key_file);
412int netw_init_openssl(void);
414int netw_unload_openssl(void);
416int netw_ssl_connect(NETWORK** netw, char* host, char* port, int ip_version, char* ssl_key_file, char* ssl_cert_file);
418int netw_ssl_connect_client(NETWORK** netw, char* host, char* port, int ip_version);
420int netw_ssl_do_handshake(NETWORK* netw, const char* sni_hostname);
422ssize_t send_ssl_data(void* netw, char* buf, uint32_t n);
424ssize_t recv_ssl_data(void* netw, char* buf, uint32_t n);
425#endif
427int netw_init_wsa(int mode, int v1, int v2);
429int netw_set(NETWORK* netw, int flag);
431int netw_get_state(NETWORK* netw, uint32_t* state, int* thr_engine_status);
433int netw_setsockopt(NETWORK* netw, int optname, int value);
435int netw_set_blocking(NETWORK* netw, unsigned long int is_blocking);
437int 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);
439int netw_connect(NETWORK** netw, char* host, char* port, int ip_version);
441int deplete_send_buffer(int fd, int timeout);
443int netw_bind_udp(NETWORK** netw, char* addr, char* port, int ip_version);
445int netw_connect_udp(NETWORK** netw, char* host, char* port, int ip_version);
447ssize_t send_udp_data(void* netw, char* buf, uint32_t n);
449ssize_t recv_udp_data(void* netw, char* buf, uint32_t n);
451ssize_t netw_udp_sendto(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* dest_addr, socklen_t dest_len);
453ssize_t netw_udp_recvfrom(NETWORK* netw, char* buf, uint32_t n, struct sockaddr* src_addr, socklen_t* src_len);
455int netw_close(NETWORK** netw);
459int netw_wait_close_timed(NETWORK** netw, size_t timeout);
463int netw_make_listening(NETWORK** netw, char* addr, char* port, int nbpending, int ip_version);
465NETWORK* netw_accept_from_ex(NETWORK* from, size_t send_list_limit, size_t recv_list_limit, int blocking, int* retval);
469NETWORK* netw_accept_nonblock_from(NETWORK* from, int blocking);
471int netw_add_msg(NETWORK* netw, N_STR* msg);
473int netw_add_msg_ex(NETWORK* netw, char* str, unsigned int length);
477N_STR* netw_wait_msg(NETWORK* netw, unsigned int refresh, size_t timeout);
481void* netw_send_func(void* NET);
483void* netw_recv_func(void* NET);
485ssize_t send_data(void* netw, char* buf, uint32_t n);
487ssize_t recv_data(void* netw, char* buf, uint32_t n);
489ssize_t send_php(SOCKET s, int _code, char* buf, int n);
491ssize_t recv_php(SOCKET s, int* _code, char** buf);
493int netw_get_queue_status(NETWORK* netw, size_t* nb_to_send, size_t* nb_to_read);
494
496NETWORK_POOL* netw_new_pool(size_t nb_min_element);
498int netw_destroy_pool(NETWORK_POOL** netw_pool);
500void netw_pool_netw_close(void* netw_ptr);
502int netw_pool_add(NETWORK_POOL* netw_pool, NETWORK* netw);
504int netw_pool_remove(NETWORK_POOL* netw_pool, NETWORK* netw);
506int netw_pool_broadcast(NETWORK_POOL* netw_pool, const NETWORK* from, N_STR* net_msg);
508size_t netw_pool_nbclients(NETWORK_POOL* netw_pool);
509
511int netw_set_user_id(NETWORK* netw, int id);
512
514int netw_send_ping(NETWORK* netw, int type, int id_from, int id_to, int time);
516int netw_send_ident(NETWORK* netw, int type, int id, N_STR* name, N_STR* passwd);
518int netw_send_position(NETWORK* netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp);
520int netw_send_string_to(NETWORK* netw, int id_to, N_STR* name, N_STR* chan, N_STR* txt, int color);
522int netw_send_string_to_all(NETWORK* netw, N_STR* name, N_STR* chan, N_STR* txt, int color);
525
527size_t netw_calculate_urlencoded_size(const char* str, size_t len);
529char* netw_extract_http_request_type(const char* request);
533int netw_info_destroy(NETWORK_HTTP_INFO http_request);
535char* netw_urlencode(const char* str, size_t len);
537int netw_get_url_from_http_request(const char* request, char* url, size_t size);
539char* netw_urldecode(const char* str);
541HASH_TABLE* netw_parse_post_data(const char* post_data);
543const char* netw_guess_http_content_type(const char* url);
545const char* netw_get_http_status_message(int status_code);
547int netw_get_http_date(char* buffer, size_t buffer_size);
549int 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);
550
554typedef struct N_PROXY_CFG {
555 char* scheme;
556 char* host;
557 int port;
558 char* username;
559 char* password;
561
577N_PROXY_CFG* n_proxy_cfg_parse(const char* url);
578
583void n_proxy_cfg_free(N_PROXY_CFG** cfg);
584
602int n_proxy_connect_tunnel(const N_PROXY_CFG* proxy,
603 const char* target_host,
604 int target_port);
605
606#ifdef HAVE_OPENSSL
622 const char* target_host,
623 int target_port);
624#endif /* HAVE_OPENSSL */
625
638int n_proxy_connect_socks5(const N_PROXY_CFG* proxy,
639 const char* target_host,
640 int target_port);
641
643#define N_URL_MAX_PARAMS 64
644
646typedef struct N_URL_PARAM {
647 char* key;
648 char* value;
650
652typedef struct N_URL {
653 char* scheme;
654 char* host;
655 int port;
656 char* path;
657 char* query;
660} N_URL;
661
663N_URL* n_url_parse(const char* url);
665N_STR* n_url_build(const N_URL* u);
667N_STR* n_url_encode(const char* str);
669N_STR* n_url_decode(const char* str);
671void n_url_free(N_URL** u);
672
674#define N_WS_OP_TEXT 0x01
675#define N_WS_OP_BINARY 0x02
676#define N_WS_OP_CLOSE 0x08
677#define N_WS_OP_PING 0x09
678#define N_WS_OP_PONG 0x0A
679
686
688typedef struct N_WS_CONN {
691 char* host;
692 char* path;
693} N_WS_CONN;
694
695#ifdef HAVE_OPENSSL
697N_WS_CONN* n_ws_connect(const char* host, const char* port, const char* path, int use_ssl);
698
700int n_ws_send(N_WS_CONN* conn, const char* payload, size_t len, int opcode);
701
703int n_ws_recv(N_WS_CONN* conn, N_WS_MESSAGE* msg_out);
704
706void n_ws_close(N_WS_CONN* conn);
707
709void n_ws_conn_free(N_WS_CONN** conn);
710#endif
711
719
720struct N_SSE_CONN;
721
723typedef struct N_SSE_CONN {
725 volatile int stop_flag;
726 void (*on_event)(N_SSE_EVENT* event, struct N_SSE_CONN* conn, void* user_data);
727 void* user_data;
728} N_SSE_CONN;
729
730#ifdef HAVE_OPENSSL
732N_SSE_CONN* n_sse_connect(const char* host, const char* port, const char* path, int use_ssl, void (*on_event)(N_SSE_EVENT*, N_SSE_CONN*, void*), void* user_data);
733
735void n_sse_stop(N_SSE_CONN* conn);
736
738void n_sse_conn_free(N_SSE_CONN** conn);
739
741void n_sse_event_clean(N_SSE_EVENT* event);
742#endif
743
745typedef struct N_HTTP_REQUEST {
746 char method[16];
747 char path[2048];
748 char query[2048];
752
759
761typedef struct N_MOCK_SERVER {
763 volatile int stop_flag;
765 void* user_data;
766 int port;
768
771 void (*on_request)(N_HTTP_REQUEST*, N_HTTP_RESPONSE*, void*),
772 void* user_data);
773
776
779
782
783/* ---- per-NETWORK error capture ---- */
784
788const char* n_netw_get_error(NETWORK* netw, int index);
791
792/* ---- pre-connection error capture (thread-local, no NETWORK yet) ---- */
793
797const char* n_netw_get_connect_error(int index);
800
805#ifdef __cplusplus
806}
807#endif
808
809#endif /*#ifndef N_NETWORK*/
static int mode
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:38
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:154
structure of a hash table
Definition n_hash.h:137
Structure of a generic LIST container.
Definition n_list.h:58
A box including a string and his lenght.
Definition n_str.h:60
volatile int stop_flag
atomic stop flag
Definition n_network.h:725
char query[2048]
query string (or empty)
Definition n_network.h:748
struct addrinfo * rhost
getaddrinfo results
Definition n_network.h:252
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
Definition n_network.h:288
char * ip
ip of the connected socket
Definition n_network.h:244
N_SOCKET link
networking socket
Definition n_network.h:326
char * certificate
openssl certificate file
Definition n_network.h:320
char netw_errors[8][512]
per-connection error capture ring buffer (max 8 entries, 512 chars each)
Definition n_network.h:351
int threaded_engine_status
Threaded network engine state for this network.
Definition n_network.h:264
size_t content_length
Store content length.
Definition n_network.h:385
char * type
Type of request.
Definition n_network.h:389
pthread_t send_thr
sending thread
Definition n_network.h:334
int nb_pending
Nb pending connection,if listening.
Definition n_network.h:260
int so_reuseaddr
so reuseaddr state
Definition n_network.h:270
pthread_t recv_thr
receiving thread
Definition n_network.h:336
int port
port number (0 if not specified)
Definition n_network.h:655
char * host
proxy hostname
Definition n_network.h:556
pthread_rwlock_t rwlock
thread safety
Definition n_network.h:376
NETWORK * netw
underlying network connection
Definition n_network.h:724
struct sockaddr_storage raddr
connected remote addr
Definition n_network.h:254
char * host
hostname
Definition n_network.h:654
char * path
path starting with "/", or "/" if empty
Definition n_network.h:656
pthread_mutex_t eventbolt
mutex for threaded access of state event
Definition n_network.h:343
int port
listening port
Definition n_network.h:766
const SSL_METHOD * method
SSL method container.
Definition n_network.h:314
N_STR * body
response body
Definition n_network.h:757
int netw_err_next
next write slot in ring buffer
Definition n_network.h:355
int deplete_socket_timeout
deplete socket send buffer timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:298
void * user_data
user data for handler
Definition n_network.h:765
char * scheme
"http" or "https"
Definition n_network.h:653
int deplete_queues_timeout
deplete network queues timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:296
int nb_running_threads
nb running threads, if > 0 thread engine is still running
Definition n_network.h:294
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
Definition n_network.h:341
NETWORK * netw
underlying network connection
Definition n_network.h:689
void(* on_event)(N_SSE_EVENT *event, struct N_SSE_CONN *conn, void *user_data)
callback
Definition n_network.h:726
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
Definition n_network.h:339
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
Definition n_network.h:268
void(* on_request)(N_HTTP_REQUEST *req, N_HTTP_RESPONSE *resp, void *user_data)
request handler
Definition n_network.h:764
N_STR * event
event type (or NULL for default)
Definition n_network.h:714
int connected
1 if handshake completed
Definition n_network.h:690
N_STR * data
event data
Definition n_network.h:715
N_STR * body
request body (or NULL)
Definition n_network.h:750
char * key
parameter name
Definition n_network.h:647
int so_rcvtimeo
send timeout value
Definition n_network.h:284
char * password
NULL if no auth.
Definition n_network.h:559
char * query
raw query string without leading '?', or NULL
Definition n_network.h:657
int port
proxy port
Definition n_network.h:557
N_URL_PARAM params[64]
parsed key=value pairs
Definition n_network.h:658
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
Definition n_network.h:276
char * body
Pointer to the body data.
Definition n_network.h:387
LIST * headers
list of char* "Name: Value" strings
Definition n_network.h:749
char * value
parameter value
Definition n_network.h:648
char * host
remote hostname
Definition n_network.h:691
int netw_err_count
number of captured errors
Definition n_network.h:353
SOCKET sock
a normal socket
Definition n_network.h:242
char path[2048]
request path
Definition n_network.h:747
char * scheme
"http", "https", or "socks5"
Definition n_network.h:555
char * port
port of socket
Definition n_network.h:240
LIST * recv_buf
reveicing buffer (for incomming usage)
Definition n_network.h:331
int transport_type
transport type: NETWORK_TCP (0) or NETWORK_UDP (1)
Definition n_network.h:302
int nb_params
number of parsed parameters
Definition n_network.h:659
int user_id
if part of a user property, id of the user
Definition n_network.h:292
sem_t send_blocker
block sending func
Definition n_network.h:345
SSL_CTX * ctx
SSL context holder.
Definition n_network.h:316
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
Definition n_network.h:278
int so_sndtimeo
send timeout value
Definition n_network.h:282
int retry
retry interval in ms (0 if not set)
Definition n_network.h:717
char content_type[256]
Store content type.
Definition n_network.h:383
N_STR * id
last event ID (or NULL)
Definition n_network.h:716
struct addrinfo hints
address of local machine
Definition n_network.h:250
int so_keepalive
so keepalive state
Definition n_network.h:274
netw_func send_data
send func ptr
Definition n_network.h:308
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
Definition n_network.h:266
char * path
resource path
Definition n_network.h:692
N_STR * payload
message payload
Definition n_network.h:683
char method[16]
HTTP method.
Definition n_network.h:746
int opcode
frame opcode
Definition n_network.h:682
int masked
1 if masked
Definition n_network.h:684
uint32_t state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
Definition n_network.h:305
LIST * pools
pointers to network pools if members of any
Definition n_network.h:348
char * username
NULL if no auth.
Definition n_network.h:558
int status_code
HTTP status code.
Definition n_network.h:755
char * key
openssl key file
Definition n_network.h:322
SSL * ssl
SSL handle.
Definition n_network.h:318
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
Definition n_network.h:286
unsigned long int is_blocking
flag to quickly check socket mode
Definition n_network.h:247
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
Definition n_network.h:290
char content_type[128]
Content-Type header value.
Definition n_network.h:756
HASH_TABLE * pool
table of clients
Definition n_network.h:373
NETWORK * listener
listening network
Definition n_network.h:762
LIST * send_buf
sending buffer (for outgoing queuing )
Definition n_network.h:329
int mode
NETWORK mode , 1 listening, 0 connecting.
Definition n_network.h:262
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
Definition n_network.h:280
void * user_data
user data for callback
Definition n_network.h:727
netw_func recv_data
receive func ptr
Definition n_network.h:310
int wait_close_timeout
network wait close timeout value ( < 1 disabled, >= 1 timeout sec )
Definition n_network.h:300
volatile int stop_flag
atomic stop flag
Definition n_network.h:763
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:4136
#define N_URL_MAX_PARAMS
maximum number of parsed query parameters
Definition n_network.h:643
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:2977
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:2914
const char * n_netw_get_connect_error(int index)
Get pre-connection error message by index.
Definition n_network.c:115
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
SSL Writing to a socket.
Definition n_network.c:3512
char * netw_extract_http_request_type(const char *request)
extract HTTP request type
Definition n_network.c:4225
int netw_get_queue_status(NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
get queue status
Definition n_network.c:3842
int netw_bind_udp(NETWORK **netw, char *addr, char *port, int ip_version)
Create a UDP bound socket.
Definition n_network.c:2360
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:1395
int netw_init_wsa(int mode, int v1, int v2)
Used by Init & Close network.
Definition n_network.c:824
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable peer certificate verification
Definition n_network.c:1606
ssize_t send_php(SOCKET s, int _code, char *buf, int n)
sending to php
Definition n_network.c:3653
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receiving thread.
Definition n_network.c:3346
void n_ws_close(N_WS_CONN *conn)
send close frame and close the connection
Definition n_network.c:5019
char * netw_urlencode(const char *str, size_t len)
URL-encode a string.
Definition n_network.c:4192
N_STR * n_url_encode(const char *str)
percent-encode a string for use in URLs (returns N_STR)
Definition n_network.c:5654
void * netw_send_func(void *NET)
Thread Sending management function.
Definition n_network.c:3091
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
Accepting routine.
Definition n_network.c:2904
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
get URL from HTTP request
Definition n_network.c:4338
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:5745
#define N_ENUM_netw_code_type(_)
Network codes definition.
Definition n_network.h:214
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
set SSL
Definition n_network.c:1321
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:1508
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:1581
void n_sse_stop(N_SSE_CONN *conn)
signal the SSE connection to stop reading
Definition n_network.c:5085
int netw_get_http_date(char *buffer, size_t buffer_size)
get current HTTP date string
Definition n_network.c:4546
NETWORK_POOL * netw_new_pool(size_t nb_min_element)
init pools
Definition n_network.c:3861
int netw_set_user_id(NETWORK *netw, int id)
set user id on a netw
Definition n_network.c:4036
void n_mock_server_free(N_MOCK_SERVER **server)
free a mock server and close the listening socket
Definition n_network.c:5570
ssize_t recv_data(void *netw, char *buf, uint32_t n)
Reading from a socket.
Definition n_network.c:3447
int netw_init_openssl(void)
init ssl helper
Definition n_network.c:1280
void n_netw_clear_errors(NETWORK *netw)
Clear captured errors on a NETWORK handle.
Definition n_network.c:105
ssize_t recv_ssl_data(void *netw, char *buf, uint32_t n)
SSL Reading from a socket.
Definition n_network.c:3582
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Listening network.
Definition n_network.c:2240
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:1888
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:2573
const char * n_netw_get_error(NETWORK *netw, int index)
Get captured error message by index (0 = oldest).
Definition n_network.c:99
int netw_start_thr_engine(NETWORK *netw)
Create the sending and receiving thread of a NETWORK.
Definition n_network.c:3050
int netw_destroy_pool(NETWORK_POOL **netw_pool)
destroy pool
Definition n_network.c:3880
void n_url_free(N_URL **u)
free a N_URL and all its members
Definition n_network.c:5690
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:4578
void * netw_recv_func(void *NET)
Thread Receiving management function.
Definition n_network.c:3219
void n_sse_conn_free(N_SSE_CONN **conn)
free an SSE connection
Definition n_network.c:5094
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:5924
N_STR * n_url_decode(const char *str)
decode a percent-encoded string (returns N_STR)
Definition n_network.c:5669
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:4676
size_t htonst(size_t value)
host to network size_t
Definition n_network.c:134
int netw_unload_openssl(void)
unload ssl helper
Definition n_network.c:1302
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:6050
size_t ntohst(size_t value)
network to host size_t
Definition n_network.c:151
ssize_t(* netw_func)(void *, char *, uint32_t)
send/recv func ptr type
Definition n_network.h:235
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:2650
size_t netw_pool_nbclients(NETWORK_POOL *netw_pool)
get nb clients
Definition n_network.c:4019
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:5851
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:2713
void n_netw_clear_connect_errors(void)
Clear pre-connection errors on this thread.
Definition n_network.c:121
void n_mock_server_stop(N_MOCK_SERVER *server)
signal the mock server to stop accepting connections
Definition n_network.c:5561
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:1670
int SOCKET
default socket declaration
Definition n_network.h:88
int n_netw_get_error_count(NETWORK *netw)
Get number of captured errors on a NETWORK handle.
Definition n_network.c:95
int netw_pool_broadcast(NETWORK_POOL *netw_pool, const NETWORK *from, N_STR *net_msg)
broadcast message to pool
Definition n_network.c:3995
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:5073
int netw_setsockopt(NETWORK *netw, int optname, int value)
Set common socket options (disable naggle, send/recv buf, reuse addr)
Definition n_network.c:929
int netw_set(NETWORK *netw, int flag)
Set flags on network.
Definition n_network.c:1946
ssize_t send_data(void *netw, char *buf, uint32_t n)
Writing to a socket.
Definition n_network.c:3386
ssize_t recv_udp_data(void *netw, char *buf, uint32_t n)
UDP recv data from socket.
Definition n_network.c:2614
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get flags from network.
Definition n_network.c:1924
N_URL * n_url_parse(const char *url)
parse a URL string into components
Definition n_network.c:5601
void netw_pool_netw_close(void *netw_ptr)
close pool
Definition n_network.c:3899
int n_netw_get_connect_error_count(void)
Get number of pre-connection errors captured on this thread.
Definition n_network.c:111
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
calculate URL-encoded size
Definition n_network.c:4169
int deplete_send_buffer(int fd, int timeout)
wait for send buffer to be empty
Definition n_network.c:2010
NETWORK * netw_accept_from(NETWORK *from)
Accepting routine.
Definition n_network.c:2894
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
Definition n_network.c:5840
int netw_close(NETWORK **netw)
Closing.
Definition n_network.c:2041
N_STR * n_url_build(const N_URL *u)
build a URL string from parsed components
Definition n_network.c:5640
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:5476
int netw_send_quit(NETWORK *netw)
send quit message
Definition n_network.c:4152
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:1849
const char * netw_get_http_status_message(int status_code)
get HTTP status message string
Definition n_network.c:4522
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:1473
int n_ws_recv(N_WS_CONN *conn, N_WS_MESSAGE *msg_out)
receive one WebSocket frame
Definition n_network.c:4947
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
set blocking mode
Definition n_network.c:871
N_STR * netw_wait_msg(NETWORK *netw, unsigned int refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition n_network.c:2998
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:4116
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
send ping message
Definition n_network.c:4051
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:1830
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract HTTP info from request
Definition n_network.c:4258
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Connecting.
Definition n_network.c:1814
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:5442
__netw_code_type
Network codes declaration.
Definition n_network.h:232
int netw_send_ident(NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
send ident message
Definition n_network.c:4070
char * netw_urldecode(const char *str)
URL-decode a string.
Definition n_network.c:4375
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:1626
void n_ws_conn_free(N_WS_CONN **conn)
free a WebSocket connection structure
Definition n_network.c:5049
N_SSE_CONN * n_sse_connect(const char *host, const char *port, const char *path, int use_ssl, 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:5179
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add network to pool
Definition n_network.c:3912
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:2468
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy HTTP info structure
Definition n_network.c:4325
HASH_TABLE * netw_parse_post_data(const char *post_data)
parse POST data into hash table
Definition n_network.c:4412
const char * netw_guess_http_content_type(const char *url)
guess HTTP content type from URL
Definition n_network.c:4458
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:4880
ssize_t recv_php(SOCKET s, int *_code, char **buf)
receive from php
Definition n_network.c:3736
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:4094
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove network from pool
Definition n_network.c:3958
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:2946
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:2682
parsed HTTP request for mock server callback
Definition n_network.h:745
HTTP response to send from mock server callback.
Definition n_network.h:754
mock HTTP server handle
Definition n_network.h:761
Parsed proxy URL components.
Definition n_network.h:554
Structure of a N_SOCKET.
Definition n_network.h:238
SSE connection handle.
Definition n_network.h:723
SSE event received from server.
Definition n_network.h:713
parsed URL components
Definition n_network.h:652
a single query parameter key=value pair
Definition n_network.h:646
WebSocket connection.
Definition n_network.h:688
WebSocket message.
Definition n_network.h:681
Structure of a NETWORK.
Definition n_network.h:258
structure for splitting HTTP requests
Definition n_network.h:381
structure of a network pool
Definition n_network.h:371
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
N_STR and string function declaration.
Timing utilities.