Nilorea Library
C utilities for networking, threading, graphics
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_TYPE ;
48#define SOCKET SOCKET_TYPE
49#endif
50#else
52typedef int SOCKET_TYPE ;
54#define SOCKET SOCKET_TYPE
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
74#ifdef __linux__
75#include <linux/sockios.h>
76#include <signal.h>
77#endif
78
79#define netw_unload()
80
81void netw_sigchld_handler( int sig );
82
84#define netw_server_set_signal_handler \
85{ \
86 #ifdef __linux__ \
87 struct sigaction sa; \
88 sa.sa_handler = sigchld_handler; /* reap dead processes */ \
89 sigemptyset(&sa.sa_mask); \
90 sa.sa_flags = SA_RESTART; \
91 if( sigaction( SIGCHLD , &sa , NULL ) == -1 ) \
92 { \
93 n_log( LOG_ERR , "sigaction error %s" , strerror( errno ) ); \
94 exit( 1 ); \
95 } \
96 #endif \
97}
98
99#ifndef MSG_NOSIGNAL
100#define MSG_NOSIGNAL 0
101#endif
102
104#define closesocket close
106#define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
108#define INVALID_SOCKET -1
109
110#include "n_time.h"
111
112#elif defined __windows__
113
114#define SHUT_WR SD_SEND
115#define SHUT_RD SD_RECEIVE
116#define SHUT_RDWR SD_BOTH
117
118#ifndef ECONNRESET
119#define ECONNRESET 104
120#endif
121
122#if (_WIN32_WINNT < 0x0501)
123#undef _WIN32_WINNT
124#define _WIN32_WINNT 0x0501
125#endif
126
127#include <errno.h>
128#include <unistd.h>
129#include <malloc.h>
130#include <string.h>
131#include <winsock2.h>
132#include <windows.h>
133#include <ws2tcpip.h>
134#include "n_time.h"
135
136#ifndef MSG_EOR
137#define MSG_EOR 0
138#endif
139
140#ifndef MSG_NOSIGNAL
141#define MSG_NOSIGNAL 0
142#endif
143
144#ifndef MSG_WAITALL
145#define MSG_WAITALL 0
146#endif
147
148#ifndef EAI_SYSTEM
149#define EAI_SYSTEM 0
150#endif
151
152#ifndef AI_PASSIVE
153#define AI_PASSIVE 0x00000001
154#endif
155#ifndef AI_CANONNAME
156#define AI_CANONNAME 0x00000002
157#endif
158#ifndef AI_NUMERICHOST
159#define AI_NUMERICHOST 0x00000004
160#endif
161#ifndef AI_NUMERICSERV
162#define AI_NUMERICSERV 0x00000008
163#endif
164#ifndef AI_ALL
165#define AI_ALL 0x00000100
166#endif
167#ifndef AI_ADDRCONFIG
168#define AI_ADDRCONFIG 0x00000400
169#endif
170#ifndef AI_V4MAPPED
171#define AI_V4MAPPED 0x00000800
172#endif
173#ifndef AI_NON_AUTHORITATIVE
174#define AI_NON_AUTHORITATIVE 0x00004000
175#endif
176#ifndef AI_SECURE
177#define AI_SECURE 0x00008000
178#endif
179#ifndef AI_RETURN_PREFERRED_NAMES
180#define AI_RETURN_PREFERRED_NAMES 0x00010000
181#endif
182
183#include <pthread.h>
184#include <semaphore.h>
185
187#define NETFLAGS 0 /* no flags needed for microsoft */
188
190#define netw_unload() netw_init_wsa( 0 , 2 , 2 )
191
192#endif
193
194#ifdef HAVE_OPENSSL
195#define _OPEN_SYS_SOCK_IPV6 1
196#include <resolv.h>
197#include <openssl/ssl.h>
198#include <openssl/err.h>
199#endif
200
202#define N_ENUM_netw_code_type(_)\
203 _(NETW_CLIENT, 2)\
204 _(NETW_SERVER, 4)\
205 _(NETW_RESTART_TIMER, 8)\
206 _(NETW_EMPTY_SENDBUF, 16)\
207 _(NETW_EMPTY_RECVBUF, 32)\
208 _(NETW_RUN, 64)\
209 _(NETW_EXIT_ASKED, 128)\
210 _(NETW_EXITED, 256)\
211 _(NETW_ERROR, 512)\
212 _(NETW_CRYPTO_NONE, 1024)\
213 _(NETW_CRYPTO_NEGOCIATE, 2048)\
214 _(NETW_CRYPTO_MANDATORY, 4096)\
215 _(NETW_ENCRYPT_NONE, 8192)\
216 _(NETW_ENCRYPT_VIGENERE, 16384)\
217 _(NETW_ENCRYPT_OPENSSL, 32768)\
218 _(NETW_THR_ENGINE_STARTED, 65536)\
219 _(NETW_THR_ENGINE_STOPPED, 131072)\
220 _(NETW_DESTROY_RECVBUF, 262144)\
221 _(NETW_DESTROY_SENDBUF, 524288)
222
225
227typedef int (*netw_func)( SOCKET, char *, NSTRBYTE );
228
230typedef struct N_SOCKET
231{
233 char *port;
237 char *ip;
238
240 unsigned long int is_blocking ;
241
243 struct addrinfo hints ;
245 struct addrinfo *rhost ;
247 struct sockaddr_storage raddr ;
248} N_SOCKET;
249
251typedef struct NETWORK
252{
287
290
293
298
299#ifdef HAVE_OPENSSL
301 SSL_METHOD *method ;
303 SSL_CTX *ctx ;
305 char *certificat,
307 *key ;
308#endif
309
312
317
319 pthread_t send_thr;
321 pthread_t recv_thr;
322
324 pthread_mutex_t sendbolt;
326 pthread_mutex_t recvbolt;
328 pthread_mutex_t eventbolt;
331
334
335} NETWORK;
336
337
338
340typedef struct NETWORK_POOL
341{
344
346 pthread_rwlock_t rwlock ;
347
348} NETWORK_POOL ;
349
350
351
352#ifdef HAVE_OPENSSL
353/* set the certificat and private key file to use */
354int netw_set_sll_files( NETWORK *netw, char *cert, char *key );
355/* set a vigenere key to use */
356int netw_set_vigenere_key( NETWORK *netw, char *vigenere_key );
357/* negociate and initialize encryptions if any */
358int netw_do_crypto_handshake( NETWORK *netw );
359/* init ssl helper */
360int netw_init_openssl( void );
361#endif
362/* Used by Init & Close network */
363int netw_init_wsa( int mode, int v1, int v2 );
364/* Set flags on network */
365int netw_set( NETWORK *netw, int flag );
366/* Get flags from network */
367int netw_get_state( NETWORK *netw, int *state, int *thr_engine_status );
368/* Set common socket options (disable naggle, send/recv buf, reuse addr) */
369int netw_setsockopt( NETWORK *netw, int optname, int value );
370/* set blocking mode */
371int netw_set_blocking( NETWORK *netw, unsigned long int is_blocking );
372/* Connecting, extended */
373int netw_connect_ex( NETWORK **netw, char *host, char *port, int send_list_limit, int recv_list_limit, int ip_version );
374/* Connecting */
375int netw_connect( NETWORK **netw, char *host, char *port, int ip_version );
376/* wait for send buffer to be empty */
377#if defined( __linux__ )
378int deplete_send_buffer( int fd, size_t timeout );
379#endif
380/* Closing */
381int netw_close( NETWORK **netw );
382/* Closing for peer */
383int netw_wait_close( NETWORK **netw );
384/* Closing for peer timeouted*/
385int netw_wait_close_timed( NETWORK **netw, size_t timeout );
386/* Stop a NETWORK connection sending and receing thread */
387int netw_stop_thr_engine( NETWORK *netw );
388/* Listening network */
389int netw_make_listening( NETWORK **netw, char *addr, char *port, int nbpending, int ip_version );
390/* Accepting routine extended */
391NETWORK *netw_accept_from_ex( NETWORK *from, int send_list_limit, int recv_list_limit, int non_blocking, int *retval );
392/* Accepting routine */
394/* Accepting routine */
395NETWORK *netw_accept_nonblock_from( NETWORK *from, int blocking );
396/* Add a message to send in aimed NETWORK */
397int netw_add_msg( NETWORK *netw, N_STR *msg );
398/* Add a char message to send in the aimed NETWORK */
399int netw_add_msg_ex( NETWORK *netw, char *str, unsigned int length );
400/* Get a message from aimed NETWORK. Instant return to NULL if no MSG */
401N_STR *netw_get_msg( NETWORK *netw );
402/* Wait a message from aimed NETWORK. Recheck each 'refresh' usec until 'timeout' usec */
403N_STR *netw_wait_msg( NETWORK *netw, size_t refresh, size_t timeout );
404/* Create the sending and receiving thread of a NETWORK */
405int netw_start_thr_engine( NETWORK *netw );
406/* Thread Sending management function */
407void *netw_send_func( void *NET );
408/* Thread Receiving management function */
409void *netw_recv_func( void *NET );
410/* Writting to a socket */
411int send_data( SOCKET s, char *buf, NSTRBYTE n );
412/* Reading from a socket */
413int recv_data( SOCKET s, char *buf, NSTRBYTE n );
414/* sending to php */
415int send_php( SOCKET s, int _code, char *buf, int n );
416/* receive from php */
417int recv_php( SOCKET s, int *_code, char **buf );
418/* get queue status */
419int netw_get_queue_status( NETWORK *netw, int *nb_to_send, int *nb_to_read );
420
421/* init pools */
422NETWORK_POOL *netw_new_pool( int nb_min_element );
423/* destroy pool */
424int netw_destroy_pool( NETWORK_POOL **netw_pool );
425/* close pool */
426void netw_pool_netw_close( void *netw_ptr );
427/* add network to pool */
428int netw_pool_add( NETWORK_POOL *netw_pool, NETWORK *netw );
429/* add network to pool */
430int netw_pool_remove( NETWORK_POOL *netw_pool, NETWORK *netw );
431/* add message to pool */
432int netw_pool_broadcast( NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg );
433/* get nb clients */
434int netw_pool_nbclients( NETWORK_POOL *netw_pool );
435
436/* set user id on a netw */
437int netw_set_user_id( NETWORK *netw, int id );
438
439/* homemade tcp ip protocol helpers */
440int netw_send_ping( NETWORK *netw, int type, int id_from, int id_to, int time );
441int netw_send_ident( NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd );
442int netw_send_position( NETWORK *netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp );
443int netw_send_string_to( NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color );
444int netw_send_string_to_all( NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color );
445int netw_send_quit( NETWORK *netw );
446
447
452#ifdef __cplusplus
453}
454#endif
455
456#endif /*#ifndef N_NETWORK*/
structure of a hash table
Definition: n_hash.h:109
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:245
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
Definition: n_network.h:280
char * ip
ip of the connected socket
Definition: n_network.h:237
N_SOCKET link
networking socket
Definition: n_network.h:311
int threaded_engine_status
Threaded network engine state for this network.
Definition: n_network.h:260
pthread_t send_thr
sending thread
Definition: n_network.h:319
int nb_pending
Nb pending connection,if listening.
Definition: n_network.h:254
int so_reuseaddr
so reuseaddr state
Definition: n_network.h:266
pthread_t recv_thr
receiving thread
Definition: n_network.h:321
pthread_rwlock_t rwlock
thread safety
Definition: n_network.h:346
struct sockaddr_storage raddr
connected remote addr
Definition: n_network.h:247
pthread_mutex_t eventbolt
mutex for threaded access of state event
Definition: n_network.h:328
int nb_running_threads
nb running threads, if > 0 thread engine is still running
Definition: n_network.h:286
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
Definition: n_network.h:326
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
Definition: n_network.h:324
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
Definition: n_network.h:264
int so_rcvtimeo
send timeout value
Definition: n_network.h:276
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
Definition: n_network.h:268
SOCKET sock
a normal socket
Definition: n_network.h:235
char * port
port of socket
Definition: n_network.h:233
LIST * recv_buf
reveicing buffer (for incomming usage)
Definition: n_network.h:316
int user_id
if part of a user property, id of the user
Definition: n_network.h:284
sem_t send_blocker
block sending func
Definition: n_network.h:330
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
Definition: n_network.h:270
int so_sndtimeo
send timeout value
Definition: n_network.h:274
char * vigenere_key
vigenere key
Definition: n_network.h:292
int state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
Definition: n_network.h:258
struct addrinfo hints
address of local machine
Definition: n_network.h:243
netw_func send_data
send func ptr
Definition: n_network.h:295
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
Definition: n_network.h:262
LIST * pools
pointers to network pools if members of any
Definition: n_network.h:333
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
Definition: n_network.h:278
unsigned long int is_blocking
flag to quickly check socket mode
Definition: n_network.h:240
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
Definition: n_network.h:282
size_t deplete_timeout
deplete send buffer ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
Definition: n_network.h:289
HASH_TABLE * pool
table of clients
Definition: n_network.h:343
LIST * send_buf
sending buffer (for outgoing queuing )
Definition: n_network.h:314
int mode
NETWORK mode , 1 listening, 0 connecting.
Definition: n_network.h:256
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
Definition: n_network.h:272
netw_func recv_data
receive func ptr
Definition: n_network.h:297
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:3167
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:3001
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition: n_network.c:2044
int send_php(SOCKET s, int _code, char *buf, int n)
send data onto the socket
Definition: n_network.c:2596
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition: n_network.c:1965
N_STR * netw_wait_msg(NETWORK *netw, size_t refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition: n_network.c:2068
int netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
Definition: n_network.c:3032
int(* netw_func)(SOCKET, char *, NSTRBYTE)
send/recv func ptr type
Definition: n_network.h:227
int recv_data(SOCKET s, char *buf, NSTRBYTE n)
recv data from the socket
Definition: n_network.c:2541
int netw_init_wsa(int mode, int v1, int v2)
Do not directly use, internal api.
Definition: n_network.c:719
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receing thread.
Definition: n_network.c:2446
void * netw_send_func(void *NET)
Thread send function.
Definition: n_network.c:2166
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
make a normal blocking 'accept' .
Definition: n_network.c:1953
#define N_ENUM_netw_code_type(_)
Network codes definition.
Definition: n_network.h:202
int netw_connect_ex(NETWORK **netw, char *host, char *port, int send_list_limit, int recv_list_limit, int ip_version)
Use this to connect a NETWORK to any listening one.
Definition: n_network.c:1226
int netw_set_user_id(NETWORK *netw, int id)
associate an id and a network
Definition: n_network.c:3052
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:1665
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition: n_network.c:2125
int netw_destroy_pool(NETWORK_POOL **netw_pool)
free a NETWORK_POOL *pool
Definition: n_network.c:2869
int netw_wait_close(NETWORK **netw)
Wait for peer closing a specified Network, destroy queues, free the structure.
Definition: n_network.c:1574
void * netw_recv_func(void *NET)
To Thread Receiving function.
Definition: n_network.c:2296
int netw_get_queue_status(NETWORK *netw, int *nb_to_send, int *nb_to_read)
retrieve network send queue status
Definition: n_network.c:2825
NETWORK * netw_accept_from_ex(NETWORK *from, int send_list_limit, int recv_list_limit, int non_blocking, int *retval)
make a normal 'accept' .
Definition: n_network.c:1791
int netw_setsockopt(NETWORK *netw, int optname, int value)
Modify common socket options on the given netw.
Definition: n_network.c:830
int netw_set(NETWORK *netw, int flag)
Restart or reset the specified network ability.
Definition: n_network.c:1403
void netw_pool_netw_close(void *netw_ptr)
close a network from a network pool
Definition: n_network.c:2891
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
Definition: n_network.c:1940
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition: n_network.c:1480
int netw_send_quit(NETWORK *netw)
Add a formatted NETMSG_QUIT message to the specified network.
Definition: n_network.c:3186
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
Modify blocking socket mode.
Definition: n_network.c:773
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:3144
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:3070
int SOCKET_TYPE
default socket declaration
Definition: n_network.h:52
NETWORK_POOL * netw_new_pool(int nb_min_element)
return a new network pool of nb_min_element
Definition: n_network.c:2847
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:1361
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:3092
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add a NETWORK *netw to a NETWORK_POOL *pool
Definition: n_network.c:2907
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
Definition: n_network.c:1376
int send_data(SOCKET s, char *buf, NSTRBYTE n)
send data onto the socket
Definition: n_network.c:2489
int recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
Definition: n_network.c:2695
N_ENUM_DECLARE(N_ENUM_netw_code_type, __netw_code_type)
Network codes declaration.
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:3119
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove a NETWORK *netw to a NETWORK_POOL *pool
Definition: n_network.c:2960
int netw_add_msg_ex(NETWORK *netw, char *str, unsigned int length)
Add a message to send in aimed NETWORK.
Definition: n_network.c:2001
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:1589
#define SOCKET
socket macro shortcut
Definition: n_network.h:54
Structure of a N_SOCKET.
Definition: n_network.h:231
Structure of a NETWORK.
Definition: n_network.h:252
structure of a network pool
Definition: n_network.h:341
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:1541
void netw_sigchld_handler(int sig)
signal handler to reap zombies when forking
Definition: n_network.c:581
N_STR and string function declaration.
Timing utilities.