27#define NETWORK_IPALL 0
33#define NETWORK_DEPLETE_TIMEOUT 4096
35#define NETWORK_CONSECUTIVE_SEND_TIMEOUT 8192
46typedef long long unsigned int SOCKET ;
48#define SOCKET_SIZE_FORMAT "%zu"
54#define SOCKET_SIZE_FORMAT "%d"
58#if defined( __linux__ ) || defined( __sun ) || defined( _AIX )
66#include <netinet/in.h>
67#include <netinet/tcp.h>
71#include <sys/socket.h>
75#define SOCKET_SIZE_FORMAT "%d"
78#include <linux/sockios.h>
89#define closesocket close
91#define NETFLAGS MSG_NOSIGNAL
93#define INVALID_SOCKET -1
97#elif defined __windows__
99#define SHUT_WR SD_SEND
100#define SHUT_RD SD_RECEIVE
101#define SHUT_RDWR SD_BOTH
104#define ECONNRESET 104
107#if (_WIN32_WINNT < 0x0501)
109#define _WIN32_WINNT 0x0501
126#define MSG_NOSIGNAL 0
138#define AI_PASSIVE 0x00000001
141#define AI_CANONNAME 0x00000002
143#ifndef AI_NUMERICHOST
144#define AI_NUMERICHOST 0x00000004
146#ifndef AI_NUMERICSERV
147#define AI_NUMERICSERV 0x00000008
150#define AI_ALL 0x00000100
153#define AI_ADDRCONFIG 0x00000400
156#define AI_V4MAPPED 0x00000800
158#ifndef AI_NON_AUTHORITATIVE
159#define AI_NON_AUTHORITATIVE 0x00004000
162#define AI_SECURE 0x00008000
164#ifndef AI_RETURN_PREFERRED_NAMES
165#define AI_RETURN_PREFERRED_NAMES 0x00010000
169#include <semaphore.h>
175#define netw_unload() netw_init_wsa( 0 , 2 , 2 )
180#define _OPEN_SYS_SOCK_IPV6 1
181#include <openssl/err.h>
182#include <openssl/ssl.h>
183#include <openssl/crypto.h>
187#define N_ENUM_netw_code_type(_)\
190 _(NETW_RESTART_TIMER, 8)\
191 _(NETW_EMPTY_SENDBUF, 16)\
192 _(NETW_EMPTY_RECVBUF, 32)\
194 _(NETW_EXIT_ASKED, 128)\
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)
279 const SSL_METHOD *method ;
333 char content_type[256];
335 size_t content_length;
344int netw_set_crypto(
NETWORK *netw,
char *key,
char *certificate );
346int netw_init_openssl(
void );
348int netw_unload_openssl(
void );
350int netw_ssl_connect(
NETWORK **netw,
char *host,
char *port,
int ip_version,
char *ssl_key_file ,
char *ssl_cert_file );
352int send_ssl_data(
void *netw,
char *buf,
NSTRBYTE n );
354int recv_ssl_data(
void *netw,
char *buf,
NSTRBYTE n );
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 );
371#if defined( __linux__ )
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 );
#define N_ENUM_DECLARE(MACRO_DEFINITION, enum_name)
Macro to declare a N_ENUM.
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
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.
char * netw_extract_http_request_type(const char *request)
function to extract the request method from an http request
int netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
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.
char * netw_urlencode(const char *str, size_t len)
function to perform URL encoding
NETWORK * netw_accept_from_ex(NETWORK *from, int send_list_limit, int recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
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_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.
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
void * netw_recv_func(void *NET)
To Thread Receiving function.
void netw_get_http_date(char *buffer, size_t buffer_size)
helper function to generate the current date in HTTP format
int netw_get_queue_status(NETWORK *netw, int *nb_to_send, int *nb_to_read)
retrieve network send queue status
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.
int SOCKET
default socket declaration
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.
int recv_data(void *netw, char *buf, NSTRBYTE n)
recv data from the socket
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.
int(* netw_func)(void *, char *, NSTRBYTE)
send/recv func ptr type
void netw_pool_netw_close(void *netw_ptr)
close a network from a network pool
size_t netw_calculate_urlencoded_size(const char *str, size_t len)
function to calculate the required size for the URL-encoded string
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.
const char * netw_get_http_status_message(int status_code)
helper function to convert status code to a human-readable message
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.
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
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.
char * netw_urldecode(const char *str)
Function to decode URL-encoded data.
int send_data(void *netw, char *buf, NSTRBYTE n)
send data onto the socket
int netw_pool_add(NETWORK_POOL *netw_pool, NETWORK *netw)
add a NETWORK *netw to a NETWORK_POOL *pool
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
int recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
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.
structure for splitting HTTP requests
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.
N_STR and string function declaration.