Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_network.h
Go to the documentation of this file.
1
8#ifndef N_NETWORK
9#define N_NETWORK
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif
15
21#include "n_common.h"
22#include "n_str.h"
23#include "n_list.h"
24#include "n_hash.h"
25
27#define NETWORK_IPALL 0
29#define NETWORK_IPV4 1
31#define NETWORK_IPV6 2
33#define NETWORK_DEPLETE_TIMEOUT 4096
35#define NETWORK_CONSECUTIVE_SEND_TIMEOUT 8192
37#define HEAD_SIZE 10
39#define HEAD_CODE 3
40
41#ifndef SOCKET
43#ifdef __windows__
44#ifndef ARCH32BITS
46typedef long long unsigned int SOCKET ;
48#define SOCKET_SIZE_FORMAT "%zu"
49#endif
50#else
52typedef int SOCKET ;
54#define SOCKET_SIZE_FORMAT "%d"
55#endif
56#endif
57
58#if defined( __linux__ ) || defined( __sun ) || defined( _AIX )
59#include <sys/types.h>
60#include <fcntl.h>
61#include <stdio.h>
62#include <errno.h>
63#include <unistd.h>
64#include <malloc.h>
65#include <string.h>
66#include <netinet/in.h>
67#include <netinet/tcp.h>
68#include <arpa/inet.h>
69#include <netdb.h>
70#include <sys/ioctl.h>
71#include <sys/socket.h>
72#include <semaphore.h>
73
75#define SOCKET_SIZE_FORMAT "%d"
76
77#ifdef __linux__
78#include <linux/sockios.h>
79#include <signal.h>
80#endif
81
82#define netw_unload()
83
84#ifndef MSG_NOSIGNAL
85#define MSG_NOSIGNAL 0
86#endif
87
89#define closesocket close
91#define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
93#define INVALID_SOCKET -1
94
95#include "n_time.h"
96
97#elif defined __windows__
98
99#define SHUT_WR SD_SEND
100#define SHUT_RD SD_RECEIVE
101#define SHUT_RDWR SD_BOTH
102
103#ifndef ECONNRESET
104#define ECONNRESET 104
105#endif
106
107#if (_WIN32_WINNT < 0x0501)
108#undef _WIN32_WINNT
109#define _WIN32_WINNT 0x0501
110#endif
111
112#include <errno.h>
113#include <unistd.h>
114#include <malloc.h>
115#include <string.h>
116#include <winsock2.h>
117#include <windows.h>
118#include <ws2tcpip.h>
119#include "n_time.h"
120
121#ifndef MSG_EOR
122#define MSG_EOR 0
123#endif
124
125#ifndef MSG_NOSIGNAL
126#define MSG_NOSIGNAL 0
127#endif
128
129#ifndef MSG_WAITALL
130#define MSG_WAITALL 0
131#endif
132
133#ifndef EAI_SYSTEM
134#define EAI_SYSTEM 0
135#endif
136
137#ifndef AI_PASSIVE
138#define AI_PASSIVE 0x00000001
139#endif
140#ifndef AI_CANONNAME
141#define AI_CANONNAME 0x00000002
142#endif
143#ifndef AI_NUMERICHOST
144#define AI_NUMERICHOST 0x00000004
145#endif
146#ifndef AI_NUMERICSERV
147#define AI_NUMERICSERV 0x00000008
148#endif
149#ifndef AI_ALL
150#define AI_ALL 0x00000100
151#endif
152#ifndef AI_ADDRCONFIG
153#define AI_ADDRCONFIG 0x00000400
154#endif
155#ifndef AI_V4MAPPED
156#define AI_V4MAPPED 0x00000800
157#endif
158#ifndef AI_NON_AUTHORITATIVE
159#define AI_NON_AUTHORITATIVE 0x00004000
160#endif
161#ifndef AI_SECURE
162#define AI_SECURE 0x00008000
163#endif
164#ifndef AI_RETURN_PREFERRED_NAMES
165#define AI_RETURN_PREFERRED_NAMES 0x00010000
166#endif
167
168#include <pthread.h>
169#include <semaphore.h>
170
172#define NETFLAGS 0 /* no flags needed for microsoft */
173
175#define netw_unload() netw_init_wsa( 0 , 2 , 2 )
176
177#endif
178
179#ifdef HAVE_OPENSSL
180#define _OPEN_SYS_SOCK_IPV6 1
181#include <openssl/err.h>
182#include <openssl/ssl.h>
183#include <openssl/crypto.h>
184#endif
185
187#define N_ENUM_netw_code_type(_)\
188 _(NETW_CLIENT, 2)\
189 _(NETW_SERVER, 4)\
190 _(NETW_RESTART_TIMER, 8)\
191 _(NETW_EMPTY_SENDBUF, 16)\
192 _(NETW_EMPTY_RECVBUF, 32)\
193 _(NETW_RUN, 64)\
194 _(NETW_EXIT_ASKED, 128)\
195 _(NETW_EXITED, 256)\
196 _(NETW_ERROR, 512)\
197 _(NETW_ENCRYPT_NONE, 1024)\
198 _(NETW_ENCRYPT_OPENSSL, 2048)\
199 _(NETW_THR_ENGINE_STARTED, 4096)\
200 _(NETW_THR_ENGINE_STOPPED, 8192)\
201 _(NETW_DESTROY_RECVBUF, 16384)\
202 _(NETW_DESTROY_SENDBUF, 32768)
203
206
208typedef int (*netw_func)( void *, char *, NSTRBYTE );
209
211typedef struct N_SOCKET
212{
214 char *port;
218 char *ip;
219
221 unsigned long int is_blocking ;
222
224 struct addrinfo hints ;
226 struct addrinfo *rhost ;
228 struct sockaddr_storage raddr ;
229} N_SOCKET;
230
232typedef struct NETWORK
233{
268
271
276
277#ifdef HAVE_OPENSSL
279 const SSL_METHOD *method ;
281 SSL_CTX *ctx ;
283 SSL *ssl ;
285 char *certificate,
287 *key ;
288#endif
289
292
297
299 pthread_t send_thr;
301 pthread_t recv_thr;
302
304 pthread_mutex_t sendbolt;
306 pthread_mutex_t recvbolt;
308 pthread_mutex_t eventbolt;
311
314
315} NETWORK;
316
317
318
320typedef struct NETWORK_POOL
321{
324
326 pthread_rwlock_t rwlock ;
327
328} NETWORK_POOL ;
329
331typedef struct NETWORK_HTTP_INFO {
332 // Store content type
333 char content_type[256];
334 // Store content length
335 size_t content_length;
336 // Pointer to the body data
337 char *body;
338 // Type of request
339 char *type;
341
342#ifdef HAVE_OPENSSL
343/* set SSL */
344int netw_set_crypto( NETWORK *netw, char *key, char *certificate );
345/* init ssl helper */
346int netw_init_openssl( void );
347/* unload ssl helper */
348int netw_unload_openssl( void );
349/* connect ssl helper */
350int netw_ssl_connect( NETWORK **netw, char *host, char *port, int ip_version, char *ssl_key_file , char *ssl_cert_file );
351/* SSL Writting to a socket */
352int send_ssl_data( void *netw, char *buf, NSTRBYTE n );
353/* SSL Reading from a socket */
354int recv_ssl_data( void *netw, char *buf, NSTRBYTE n );
355#endif
356/* Used by Init & Close network */
357int netw_init_wsa( int mode, int v1, int v2 );
358/* Set flags on network */
359int netw_set( NETWORK *netw, int flag );
360/* Get flags from network */
361int netw_get_state( NETWORK *netw, int *state, int *thr_engine_status );
362/* Set common socket options (disable naggle, send/recv buf, reuse addr) */
363int netw_setsockopt( NETWORK *netw, int optname, int value );
364/* set blocking mode */
365int netw_set_blocking( NETWORK *netw, unsigned long int is_blocking );
366/* Connecting, extended */
367int netw_connect_ex( NETWORK **netw, char *host, char *port, int send_list_limit, int recv_list_limit, int ip_version, char *ssl_key_file, char *ssl_cert_file );
368/* Connecting */
369int netw_connect( NETWORK **netw, char *host, char *port, int ip_version );
370/* wait for send buffer to be empty */
371#if defined( __linux__ )
372int deplete_send_buffer( int fd, size_t timeout );
373#endif
374/* Closing */
375int netw_close( NETWORK **netw );
376/* Closing for peer */
377int netw_wait_close( NETWORK **netw );
378/* Closing for peer timeouted*/
379int netw_wait_close_timed( NETWORK **netw, size_t timeout );
380/* Stop a NETWORK connection sending and receing thread */
381int netw_stop_thr_engine( NETWORK *netw );
382/* Listening network */
383int netw_make_listening( NETWORK **netw, char *addr, char *port, int nbpending, int ip_version );
384/* Accepting routine extended */
385NETWORK *netw_accept_from_ex( NETWORK *from, int send_list_limit, int recv_list_limit, int blocking, int *retval );
386/* Accepting routine */
388/* Accepting routine */
389NETWORK *netw_accept_nonblock_from( NETWORK *from, int blocking );
390/* Add a message to send in aimed NETWORK */
391int netw_add_msg( NETWORK *netw, N_STR *msg );
392/* Add a char message to send in the aimed NETWORK */
393int netw_add_msg_ex( NETWORK *netw, char *str, unsigned int length );
394/* Get a message from aimed NETWORK. Instant return to NULL if no MSG */
395N_STR *netw_get_msg( NETWORK *netw );
396/* Wait a message from aimed NETWORK. Recheck each 'refresh' usec until 'timeout' usec */
397N_STR *netw_wait_msg( NETWORK *netw, size_t refresh, size_t timeout );
398/* Create the sending and receiving thread of a NETWORK */
399int netw_start_thr_engine( NETWORK *netw );
400/* Thread Sending management function */
401void *netw_send_func( void *NET );
402/* Thread Receiving management function */
403void *netw_recv_func( void *NET );
404/* Writting to a socket */
405int send_data( void *netw, char *buf, NSTRBYTE n );
406/* Reading from a socket */
407int recv_data( void *netw, char *buf, NSTRBYTE n );
408/* sending to php */
409int send_php( SOCKET s, int _code, char *buf, int n );
410/* receive from php */
411int recv_php( SOCKET s, int *_code, char **buf );
412/* get queue status */
413int netw_get_queue_status( NETWORK *netw, int *nb_to_send, int *nb_to_read );
414
415/* init pools */
416NETWORK_POOL *netw_new_pool( int nb_min_element );
417/* destroy pool */
418int netw_destroy_pool( NETWORK_POOL **netw_pool );
419/* close pool */
420void netw_pool_netw_close( void *netw_ptr );
421/* add network to pool */
422int netw_pool_add( NETWORK_POOL *netw_pool, NETWORK *netw );
423/* add network to pool */
424int netw_pool_remove( NETWORK_POOL *netw_pool, NETWORK *netw );
425/* add message to pool */
426int netw_pool_broadcast( NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg );
427/* get nb clients */
428int netw_pool_nbclients( NETWORK_POOL *netw_pool );
429
430/* set user id on a netw */
431int netw_set_user_id( NETWORK *netw, int id );
432
433/* homemade tcp ip protocol helpers */
434int netw_send_ping( NETWORK *netw, int type, int id_from, int id_to, int time );
435int netw_send_ident( NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd );
436int netw_send_position( NETWORK *netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp );
437int netw_send_string_to( NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color );
438int netw_send_string_to_all( NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color );
439int netw_send_quit( NETWORK *netw );
440
441/* http helpers */
442size_t netw_calculate_urlencoded_size(const char* str, size_t len);
443char* netw_extract_http_request_type(const char *request);
445int netw_info_destroy( NETWORK_HTTP_INFO http_request );
446char *netw_urlencode(const char* str, size_t len);
447void netw_get_url_from_http_request(const char *request, char *url, size_t size);
448char *netw_urldecode(const char *str);
449HASH_TABLE *netw_parse_post_data(const char *post_data);
450const char* netw_guess_http_content_type(const char* url);
451const char *netw_get_http_status_message(int status_code);
452void netw_get_http_date(char *buffer, size_t buffer_size);
453int 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);
454
455
460#ifdef __cplusplus
461}
462#endif
463
464#endif /*#ifndef N_NETWORK*/
#define N_ENUM_DECLARE(MACRO_DEFINITION, enum_name)
Macro to declare a N_ENUM.
Definition n_enum.h:135
structure of a hash table
Definition n_hash.h:108
Structure of a generic LIST container.
Definition n_list.h:45
size_t NSTRBYTE
N_STR base unit.
Definition n_str.h:169
A box including a string and his lenght.
Definition n_str.h:173
struct addrinfo * rhost
getaddrinfo results
Definition n_network.h:226
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
Definition n_network.h:261
char * ip
ip of the connected socket
Definition n_network.h:218
N_SOCKET link
networking socket
Definition n_network.h:291
int threaded_engine_status
Threaded network engine state for this network.
Definition n_network.h:241
pthread_t send_thr
sending thread
Definition n_network.h:299
int nb_pending
Nb pending connection,if listening.
Definition n_network.h:235
int so_reuseaddr
so reuseaddr state
Definition n_network.h:247
pthread_t recv_thr
receiving thread
Definition n_network.h:301
pthread_rwlock_t rwlock
thread safety
Definition n_network.h:326
struct sockaddr_storage raddr
connected remote addr
Definition n_network.h:228
pthread_mutex_t eventbolt
mutex for threaded access of state event
Definition n_network.h:308
int nb_running_threads
nb running threads, if > 0 thread engine is still running
Definition n_network.h:267
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
Definition n_network.h:306
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
Definition n_network.h:304
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
Definition n_network.h:245
int so_rcvtimeo
send timeout value
Definition n_network.h:257
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
Definition n_network.h:249
SOCKET sock
a normal socket
Definition n_network.h:216
char * port
port of socket
Definition n_network.h:214
LIST * recv_buf
reveicing buffer (for incomming usage)
Definition n_network.h:296
int user_id
if part of a user property, id of the user
Definition n_network.h:265
sem_t send_blocker
block sending func
Definition n_network.h:310
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
Definition n_network.h:251
int so_sndtimeo
send timeout value
Definition n_network.h:255
int state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
Definition n_network.h:239
struct addrinfo hints
address of local machine
Definition n_network.h:224
netw_func send_data
send func ptr
Definition n_network.h:273
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
Definition n_network.h:243
LIST * pools
pointers to network pools if members of any
Definition n_network.h:313
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
Definition n_network.h:259
unsigned long int is_blocking
flag to quickly check socket mode
Definition n_network.h:221
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
Definition n_network.h:263
size_t deplete_timeout
deplete send buffer ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition n_network.h:270
HASH_TABLE * pool
table of clients
Definition n_network.h:323
LIST * send_buf
sending buffer (for outgoing queuing )
Definition n_network.h:294
int mode
NETWORK mode , 1 listening, 0 connecting.
Definition n_network.h:237
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
Definition n_network.h:253
netw_func recv_data
receive func ptr
Definition n_network.h:275
int netw_send_string_to_all(NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color)
Add a string to the network, aiming all server-side users.
Definition n_network.c:3467
int netw_pool_broadcast(NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg)
add net_msg to all network in netork pool
Definition n_network.c:3301
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:2197
int send_php(SOCKET s, int _code, char *buf, int n)
send data onto the socket
Definition n_network.c:2896
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:2118
N_STR * netw_wait_msg(NETWORK *netw, size_t refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition n_network.c:2221
char * netw_extract_http_request_type(const char *request)
function to extract the request method from an http request
Definition n_network.c:3558
int netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
Definition n_network.c:3332
int netw_init_wsa(int mode, int v1, int v2)
Do not directly use, internal api.
Definition n_network.c:718
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receing thread.
Definition n_network.c:2599
char * netw_urlencode(const char *str, size_t len)
function to perform URL encoding
Definition n_network.c:3526
NETWORK * netw_accept_from_ex(NETWORK *from, int send_list_limit, int recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
Definition n_network.c:1923
void * netw_send_func(void *NET)
Thread send function.
Definition n_network.c:2319
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
make a normal blocking 'accept' .
Definition n_network.c:2106
#define N_ENUM_netw_code_type(_)
Network codes definition.
Definition n_network.h:187
int netw_set_user_id(NETWORK *netw, int id)
associate an id and a network
Definition n_network.c:3352
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
Definition n_network.c:1797
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition n_network.c:2278
int netw_destroy_pool(NETWORK_POOL **netw_pool)
free a NETWORK_POOL *pool
Definition n_network.c:3169
int netw_wait_close(NETWORK **netw)
Wait for peer closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1706
int netw_build_http_response(N_STR **http_response, int status_code, const char *server_name, const char *content_type, char *additional_headers, N_STR *body)
function to dynamically generate an HTTP response
Definition n_network.c:3852
void * netw_recv_func(void *NET)
To Thread Receiving function.
Definition n_network.c:2449
void netw_get_http_date(char *buffer, size_t buffer_size)
helper function to generate the current date in HTTP format
Definition n_network.c:3835
int netw_get_queue_status(NETWORK *netw, int *nb_to_send, int *nb_to_read)
retrieve network send queue status
Definition n_network.c:3125
int netw_connect_ex(NETWORK **netw, char *host, char *port, int send_list_limit, int recv_list_limit, int ip_version, char *ssl_key_file, char *ssl_cert_file)
Use this to connect a NETWORK to any listening one.
Definition n_network.c:1289
int SOCKET
default socket declaration
Definition n_network.h:52
void netw_get_url_from_http_request(const char *request, char *url, size_t size)
Helper function to extract the URL from the HTTP request line.
Definition n_network.c:3662
int recv_data(void *netw, char *buf, NSTRBYTE n)
recv data from the socket
Definition n_network.c:2694
int netw_setsockopt(NETWORK *netw, int optname, int value)
Modify common socket options on the given netw.
Definition n_network.c:836
int netw_set(NETWORK *netw, int flag)
Restart or reset the specified network ability.
Definition n_network.c:1511
int(* netw_func)(void *, char *, NSTRBYTE)
send/recv func ptr type
Definition n_network.h:208
void netw_pool_netw_close(void *netw_ptr)
close a network from a network pool
Definition n_network.c:3191
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
function to calculate the required size for the URL-encoded string
Definition n_network.c:3505
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
Definition n_network.c:2093
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1588
int netw_send_quit(NETWORK *netw)
Add a formatted NETMSG_QUIT message to the specified network.
Definition n_network.c:3486
const char * netw_get_http_status_message(int status_code)
helper function to convert status code to a human-readable message
Definition n_network.c:3818
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
Modify blocking socket mode.
Definition n_network.c:772
int netw_send_string_to(NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
Add a string to the network, aiming a specific user.
Definition n_network.c:3444
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
Add a ping reply to the network.
Definition n_network.c:3370
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
Definition n_network.c:3592
NETWORK_POOL * netw_new_pool(int nb_min_element)
return a new network pool of nb_min_element
Definition n_network.c:3147
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
Definition n_network.c:1449
int netw_send_ident(NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
Add a formatted NETWMSG_IDENT message to the specified network.
Definition n_network.c:3392
char * netw_urldecode(const char *str)
Function to decode URL-encoded data.
Definition n_network.c:3685
int send_data(void *netw, char *buf, NSTRBYTE n)
send data onto the socket
Definition n_network.c:2642
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add a NETWORK *netw to a NETWORK_POOL *pool
Definition n_network.c:3207
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
Definition n_network.c:3648
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
Definition n_network.c:1484
int recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
Definition n_network.c:2995
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
Definition n_network.c:3710
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
Definition n_network.c:3755
int netw_send_position(NETWORK *netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
Add a formatted NETWMSG_IDENT message to the specified network.
Definition n_network.c:3419
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove a NETWORK *netw to a NETWORK_POOL *pool
Definition n_network.c:3260
int netw_add_msg_ex(NETWORK *netw, char *str, unsigned int length)
Add a message to send in aimed NETWORK.
Definition n_network.c:2154
int netw_wait_close_timed(NETWORK **netw, size_t timeout)
Wait for peer closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1721
Structure of a N_SOCKET.
Definition n_network.h:212
Structure of a NETWORK.
Definition n_network.h:233
structure for splitting HTTP requests
Definition n_network.h:331
structure of a network pool
Definition n_network.h:321
Common headers and low-level hugly functions & define.
Hash functions and table.
List structures and definitions.
int deplete_send_buffer(int fd, size_t timeout)
wait until the socket is empty or timeout, checking each 100 msec.
Definition n_network.c:1673
N_STR and string function declaration.
Timing utilities.