27#define NETWORK_IPALL 0
33#define NETWORK_DEPLETE_TIMEOUT 4096
35#define NETWORK_CONSECUTIVE_SEND_TIMEOUT 8192
48#define SOCKET SOCKET_TYPE
54#define SOCKET SOCKET_TYPE
58#if defined( __linux__ ) || defined( __sun ) || defined( _AIX )
66#include <netinet/in.h>
67#include <netinet/tcp.h>
71#include <sys/socket.h>
75#include <linux/sockios.h>
84#define netw_server_set_signal_handler \
87 struct sigaction sa; \
88 sa.sa_handler = sigchld_handler; \
89 sigemptyset(&sa.sa_mask); \
90 sa.sa_flags = SA_RESTART; \
91 if( sigaction( SIGCHLD , &sa , NULL ) == -1 ) \
93 n_log( LOG_ERR , "sigaction error %s" , strerror( errno ) ); \
100#define MSG_NOSIGNAL 0
104#define closesocket close
106#define NETFLAGS MSG_NOSIGNAL
108#define INVALID_SOCKET -1
112#elif defined __windows__
114#define SHUT_WR SD_SEND
115#define SHUT_RD SD_RECEIVE
116#define SHUT_RDWR SD_BOTH
119#define ECONNRESET 104
122#if (_WIN32_WINNT < 0x0501)
124#define _WIN32_WINNT 0x0501
141#define MSG_NOSIGNAL 0
153#define AI_PASSIVE 0x00000001
156#define AI_CANONNAME 0x00000002
158#ifndef AI_NUMERICHOST
159#define AI_NUMERICHOST 0x00000004
161#ifndef AI_NUMERICSERV
162#define AI_NUMERICSERV 0x00000008
165#define AI_ALL 0x00000100
168#define AI_ADDRCONFIG 0x00000400
171#define AI_V4MAPPED 0x00000800
173#ifndef AI_NON_AUTHORITATIVE
174#define AI_NON_AUTHORITATIVE 0x00004000
177#define AI_SECURE 0x00008000
179#ifndef AI_RETURN_PREFERRED_NAMES
180#define AI_RETURN_PREFERRED_NAMES 0x00010000
184#include <semaphore.h>
190#define netw_unload() netw_init_wsa( 0 , 2 , 2 )
195#define _OPEN_SYS_SOCK_IPV6 1
197#include <openssl/ssl.h>
198#include <openssl/err.h>
202#define N_ENUM_netw_code_type(_)\
205 _(NETW_RESTART_TIMER, 8)\
206 _(NETW_EMPTY_SENDBUF, 16)\
207 _(NETW_EMPTY_RECVBUF, 32)\
209 _(NETW_EXIT_ASKED, 128)\
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)
354int netw_set_sll_files(
NETWORK *netw,
char *cert,
char *key );
356int netw_set_vigenere_key(
NETWORK *netw,
char *vigenere_key );
358int netw_do_crypto_handshake(
NETWORK *netw );
360int netw_init_openssl(
void );
373int netw_connect_ex(
NETWORK **netw,
char *host,
char *port,
int send_list_limit,
int recv_list_limit,
int ip_version );
377#if defined( __linux__ )
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 );
structure of a hash table
Structure of a generic LIST container.
size_t NSTRBYTE
N_STR base unit.
A box including a string and his lenght.
struct addrinfo * rhost
getaddrinfo results
int crypto_mode
tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
char * ip
ip of the connected socket
N_SOCKET link
networking socket
int threaded_engine_status
Threaded network engine state for this network.
pthread_t send_thr
sending thread
int nb_pending
Nb pending connection,if listening.
int so_reuseaddr
so reuseaddr state
pthread_t recv_thr
receiving thread
pthread_rwlock_t rwlock
thread safety
struct sockaddr_storage raddr
connected remote addr
pthread_mutex_t eventbolt
mutex for threaded access of state event
int nb_running_threads
nb running threads, if > 0 thread engine is still running
pthread_mutex_t recvbolt
mutex for threaded access of recv buf
pthread_mutex_t sendbolt
mutex for threaded access of send_buf
int send_queue_consecutive_wait
send queue consecutive pool interval, used when there are still items to send, in usec
int so_rcvtimeo
send timeout value
int tcpnodelay
state of naggle algorythm, 0 untouched, 1 forcibly disabled
SOCKET sock
a normal socket
char * port
port of socket
LIST * recv_buf
reveicing buffer (for incomming usage)
int user_id
if part of a user property, id of the user
sem_t send_blocker
block sending func
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
int so_sndtimeo
send timeout value
char * vigenere_key
vigenere key
int state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
struct addrinfo hints
address of local machine
netw_func send_data
send func ptr
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
LIST * pools
pointers to network pools if members of any
int so_linger
close lingering value (-1 disabled, 0 force close, >0 linger )
unsigned long int is_blocking
flag to quickly check socket mode
int crypto_algo
if encryption is on, which one (flags NETW_ENCRYPT_*)
size_t deplete_timeout
deplete send buffer ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
HASH_TABLE * pool
table of clients
LIST * send_buf
sending buffer (for outgoing queuing )
int mode
NETWORK mode , 1 listening, 0 connecting.
int so_rcvbuf
size of the socket recv buffer, 0 untouched, else size in bytes
netw_func recv_data
receive func ptr
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.
int netw_pool_broadcast(NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg)
add net_msg to all network in netork pool
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
int send_php(SOCKET s, int _code, char *buf, int n)
send data onto the socket
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
N_STR * netw_wait_msg(NETWORK *netw, size_t refresh, size_t timeout)
Wait a message from aimed NETWORK.
int netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
int(* netw_func)(SOCKET, char *, NSTRBYTE)
send/recv func ptr type
int recv_data(SOCKET s, char *buf, NSTRBYTE n)
recv data from the socket
int netw_init_wsa(int mode, int v1, int v2)
Do not directly use, internal api.
int netw_stop_thr_engine(NETWORK *netw)
Stop a NETWORK connection sending and receing thread.
void * netw_send_func(void *NET)
Thread send function.
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
make a normal blocking 'accept' .
#define N_ENUM_netw_code_type(_)
Network codes definition.
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.
int netw_set_user_id(NETWORK *netw, int id)
associate an id and a network
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
int netw_destroy_pool(NETWORK_POOL **netw_pool)
free a NETWORK_POOL *pool
int netw_wait_close(NETWORK **netw)
Wait for peer closing a specified Network, destroy queues, free the structure.
void * netw_recv_func(void *NET)
To Thread Receiving function.
int netw_get_queue_status(NETWORK *netw, int *nb_to_send, int *nb_to_read)
retrieve network send queue status
NETWORK * netw_accept_from_ex(NETWORK *from, int send_list_limit, int recv_list_limit, int non_blocking, int *retval)
make a normal 'accept' .
int netw_setsockopt(NETWORK *netw, int optname, int value)
Modify common socket options on the given netw.
int netw_set(NETWORK *netw, int flag)
Restart or reset the specified network ability.
void netw_pool_netw_close(void *netw_ptr)
close a network from a network pool
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
int netw_send_quit(NETWORK *netw)
Add a formatted NETMSG_QUIT message to the specified network.
int netw_set_blocking(NETWORK *netw, unsigned long int is_blocking)
Modify blocking socket mode.
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.
int netw_send_ping(NETWORK *netw, int type, int id_from, int id_to, int time)
Add a ping reply to the network.
int SOCKET_TYPE
default socket declaration
NETWORK_POOL * netw_new_pool(int nb_min_element)
return a new network pool of nb_min_element
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.
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.
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add a NETWORK *netw to a NETWORK_POOL *pool
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
int send_data(SOCKET s, char *buf, NSTRBYTE n)
send data onto the socket
int recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
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.
int netw_pool_remove(NETWORK_POOL *netw_pool, NETWORK *netw)
remove a NETWORK *netw to a NETWORK_POOL *pool
int netw_add_msg_ex(NETWORK *netw, char *str, unsigned int length)
Add a message to send in aimed NETWORK.
int netw_wait_close_timed(NETWORK **netw, size_t timeout)
Wait for peer closing a specified Network, destroy queues, free the structure.
#define SOCKET
socket macro shortcut
structure of a network pool
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.
void netw_sigchld_handler(int sig)
signal handler to reap zombies when forking
N_STR and string function declaration.