26#define NETWORK_IPALL 0
32#define NETWORK_DEPLETE_SOCKET_TIMEOUT 512
34#define NETWORK_DEPLETE_QUEUES_TIMEOUT 1024
36#define NETWORK_CONSECUTIVE_SEND_WAIT 2048
38#define NETWORK_WAIT_CLOSE_TIMEOUT 4096
44#define NETW_SOCKET_ERROR -1
46#define NETW_SOCKET_DISCONNECTED -2
48#define NETW_MAX_RETRIES 128
55typedef long long unsigned int SOCKET;
57#define SOCKET_SIZE_FORMAT "%zu"
63#define SOCKET_SIZE_FORMAT "%d"
67#if defined(__linux__) || defined(__sun) || defined(_AIX)
75#include <netinet/in.h>
76#include <netinet/tcp.h>
80#include <sys/socket.h>
84#define SOCKET_SIZE_FORMAT "%d"
87#include <linux/sockios.h>
94#define closesocket close
96#define NETFLAGS MSG_NOSIGNAL
98#define INVALID_SOCKET -1
102#elif defined __windows__
104#define SHUT_WR SD_SEND
105#define SHUT_RD SD_RECEIVE
106#define SHUT_RDWR SD_BOTH
109#define ECONNRESET 104
112#if (_WIN32_WINNT < 0x0501)
114#define _WIN32_WINNT 0x0501
131#define MSG_NOSIGNAL 0
143#define AI_PASSIVE 0x00000001
146#define AI_CANONNAME 0x00000002
148#ifndef AI_NUMERICHOST
149#define AI_NUMERICHOST 0x00000004
151#ifndef AI_NUMERICSERV
152#define AI_NUMERICSERV 0x00000008
155#define AI_ALL 0x00000100
158#define AI_ADDRCONFIG 0x00000400
161#define AI_V4MAPPED 0x00000800
163#ifndef AI_NON_AUTHORITATIVE
164#define AI_NON_AUTHORITATIVE 0x00004000
167#define AI_SECURE 0x00008000
169#ifndef AI_RETURN_PREFERRED_NAMES
170#define AI_RETURN_PREFERRED_NAMES 0x00010000
174#include <semaphore.h>
180#define netw_unload() netw_init_wsa(0, 2, 2)
185#define _OPEN_SYS_SOCK_IPV6 1
186#include <openssl/err.h>
187#include <openssl/ssl.h>
188#include <openssl/crypto.h>
192#define N_ENUM_netw_code_type(_) \
195 _(NETW_RESTART_TIMER, 8) \
196 _(NETW_EMPTY_SENDBUF, 16) \
197 _(NETW_EMPTY_RECVBUF, 32) \
199 _(NETW_EXIT_ASKED, 128) \
200 _(NETW_EXITED, 256) \
202 _(NETW_ENCRYPT_NONE, 1024) \
203 _(NETW_ENCRYPT_OPENSSL, 2048) \
204 _(NETW_THR_ENGINE_STARTED, 4096) \
205 _(NETW_THR_ENGINE_STOPPED, 8192) \
206 _(NETW_DESTROY_RECVBUF, 16384) \
207 _(NETW_DESTROY_SENDBUF, 32768)
341 char content_type[256];
343 size_t content_length;
351size_t htonst(
size_t value);
353size_t ntohst(
size_t value);
362int netw_ssl_connect(
NETWORK** netw,
char* host,
char* port,
int ip_version,
char* ssl_key_file,
char* ssl_cert_file);
379int 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);
387int netw_wait_close(
NETWORK** netw);
389int netw_wait_close_timed(
NETWORK** netw,
size_t timeout);
407N_STR* netw_wait_msg(
NETWORK* netw,
unsigned int refresh,
size_t timeout);
415ssize_t
send_data(
void* netw,
char* buf, uint32_t n);
417ssize_t
recv_data(
void* netw,
char* buf, uint32_t n);
446int 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.
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
char * certificate
openssl certificate file
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
const SSL_METHOD * method
SSL method container.
int deplete_socket_timeout
deplete socket send buffer timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
int deplete_queues_timeout
deplete network queues timeout ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
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
SSL_CTX * ctx
SSL context holder.
int so_sndbuf
size of the socket send buffer, 0 untouched, else size in bytes
int so_sndtimeo
send timeout value
struct addrinfo hints
address of local machine
int so_keepalive
so keepalive state
netw_func send_data
send func ptr
int addr_infos_loaded
Internal flag to know if we have to free addr infos.
uint32_t state
state of the connection , NETW_RUN, NETW_QUIT, NETW_STOP , NETW_ERR
LIST * pools
pointers to network pools if members of any
char * key
openssl key file
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_*)
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 wait_close_timeout
network wait close timeout value ( < 1 disabled, >= 1 timeout sec )
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 netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
send data onto the socket
char * netw_extract_http_request_type(const char *request)
function to extract the request method from an http request
int netw_get_queue_status(NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
retrieve network send queue status
int netw_init_wsa(int mode, int v1, int v2)
Do not directly use, internal api.
ssize_t send_php(SOCKET s, int _code, char *buf, int n)
send data onto the socket
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
void * netw_send_func(void *NET)
Thread send function.
NETWORK * netw_accept_nonblock_from(NETWORK *from, int blocking)
make a normal blocking 'accept' .
int 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.
#define N_ENUM_netw_code_type(_)
Network codes definition.
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
int netw_get_http_date(char *buffer, size_t buffer_size)
helper function to generate the current date in HTTP format
NETWORK_POOL * netw_new_pool(size_t nb_min_element)
return a new network pool of nb_min_element
int netw_set_user_id(NETWORK *netw, int id)
associate an id and a network
ssize_t recv_data(void *netw, char *buf, uint32_t n)
recv data from the socket
int netw_init_openssl(void)
Do not directly use, internal api.
ssize_t recv_ssl_data(void *netw, char *buf, uint32_t n)
recv data from the socket
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_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.
size_t htonst(size_t value)
host to network size_t
int netw_unload_openssl(void)
Do not directly use, internal api.
ssize_t(* netw_func)(void *, char *, uint32_t)
send/recv func ptr type
size_t netw_pool_nbclients(NETWORK_POOL *netw_pool)
return the number of networks in netw_pool
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
int SOCKET
default socket declaration
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.
ssize_t send_data(void *netw, char *buf, uint32_t n)
send data onto the socket
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get the state of a network.
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
int deplete_send_buffer(int fd, int timeout)
wait until the socket is empty or timeout, checking each 100 msec.
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.
int netw_ssl_connect(NETWORK **netw, char *host, char *port, int ip_version, char *ssl_key_file, char *ssl_cert_file)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
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.
__netw_code_type
Network codes declaration.
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 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
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
ssize_t recv_php(SOCKET s, int *_code, char **buf)
recv data from the socket
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.
structure for splitting HTTP requests
structure of a network pool
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
N_STR and string function declaration.