Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
NETWORK ENGINE: connect, accept, send and recv wrappers. Network Queue, thread-safe add/get message, ssl/tls secured communications

Data Structures

struct  N_SOCKET
 Structure of a N_SOCKET. More...
 
struct  NETWORK
 Structure of a NETWORK. More...
 
struct  NETWORK_HTTP_INFO
 structure for splitting HTTP requests More...
 
struct  NETWORK_POOL
 structure of a network pool More...
 

Macros

#define HEAD_CODE   3
 Code of a HEAD message.
 
#define HEAD_SIZE   10
 Size of a HEAD message.
 
#define N_ENUM_netw_code_type(_)
 Network codes definition.
 
#define NETWORK_CONSECUTIVE_SEND_TIMEOUT   8192
 Flag to set consecutive send waiting timeout

 
#define NETWORK_DEPLETE_TIMEOUT   4096
 Flag to set send buffer depletion timeout

 
#define NETWORK_IPALL   0
 Flag for auto detection by OS of ip version to use.
 
#define NETWORK_IPV4   1
 Flag to force IPV4

 
#define NETWORK_IPV6   2
 Flag to force IPV6

 
#define SOCKET_SIZE_FORMAT   "%d"
 socket associated printf style
 

Typedefs

typedef int(* netw_func) (void *, char *, NSTRBYTE)
 send/recv func ptr type
 
typedef int SOCKET
 default socket declaration
 

Functions

 N_ENUM_DECLARE (N_ENUM_netw_code_type, __netw_code_type)
 Network codes declaration.
 
NETWORKnetw_accept_from (NETWORK *from)
 make a normal blocking 'accept' .
 
NETWORKnetw_accept_from_ex (NETWORK *from, int send_list_limit, int recv_list_limit, int blocking, int *retval)
 make a normal 'accept' .
 
NETWORKnetw_accept_nonblock_from (NETWORK *from, int blocking)
 make a normal blocking 'accept' .
 
int netw_add_msg (NETWORK *netw, N_STR *msg)
 Add a message to send in aimed NETWORK.
 
int netw_add_msg_ex (NETWORK *netw, char *str, unsigned int length)
 Add a message to send in aimed NETWORK.
 
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
 
size_t netw_calculate_urlencoded_size (const char *str, size_t len)
 function to calculate the required size for the URL-encoded string
 
int netw_close (NETWORK **netw)
 Closing a specified Network, destroy queues, free the 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.
 
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 netw_destroy_pool (NETWORK_POOL **netw_pool)
 free a NETWORK_POOL *pool
 
NETWORK_HTTP_INFO netw_extract_http_info (char *request)
 extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
 
char * netw_extract_http_request_type (const char *request)
 function to extract the request method from an http request
 
void netw_get_http_date (char *buffer, size_t buffer_size)
 helper function to generate the current date in HTTP format
 
const char * netw_get_http_status_message (int status_code)
 helper function to convert status code to a human-readable message
 
N_STRnetw_get_msg (NETWORK *netw)
 Get a message from aimed NETWORK.
 
int netw_get_queue_status (NETWORK *netw, int *nb_to_send, int *nb_to_read)
 retrieve network send queue status
 
int netw_get_state (NETWORK *netw, int *state, int *thr_engine_status)
 Get the state of a network.
 
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.
 
const char * netw_guess_http_content_type (const char *url)
 function to guess the content type based on URL extension
 
int netw_info_destroy (NETWORK_HTTP_INFO http_request)
 destroy a NETWORK_HTTP_INFO loaded informations
 
int netw_init_wsa (int mode, int v1, int v2)
 Do not directly use, internal api.
 
int netw_make_listening (NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
 Make a NETWORK be a Listening network.
 
NETWORK_POOLnetw_new_pool (int nb_min_element)
 return a new network pool of nb_min_element
 
HASH_TABLEnetw_parse_post_data (const char *post_data)
 Function to parse POST data.
 
int netw_pool_add (NETWORK_POOL *netw_pool, NETWORK *netw)
 add a NETWORK *netw to a NETWORK_POOL *pool
 
int netw_pool_broadcast (NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg)
 add net_msg to all network in netork pool
 
int netw_pool_nbclients (NETWORK_POOL *netw_pool)
 return the number of networks in netw_pool
 
void netw_pool_netw_close (void *netw_ptr)
 close a network from a network pool
 
int netw_pool_remove (NETWORK_POOL *netw_pool, NETWORK *netw)
 remove a NETWORK *netw to a NETWORK_POOL *pool
 
void * netw_recv_func (void *NET)
 To Thread Receiving function.
 
void * netw_send_func (void *NET)
 Thread send function.
 
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_send_ping (NETWORK *netw, int type, int id_from, int id_to, int time)
 Add a ping reply to the network.
 
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_send_quit (NETWORK *netw)
 Add a formatted NETMSG_QUIT message to the specified network.
 
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_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_set (NETWORK *netw, int flag)
 Restart or reset the specified network ability.
 
int netw_set_blocking (NETWORK *netw, unsigned long int is_blocking)
 Modify blocking socket mode.
 
int netw_set_user_id (NETWORK *netw, int id)
 associate an id and a network
 
int netw_setsockopt (NETWORK *netw, int optname, int value)
 Modify common socket options on the given netw.
 
int netw_start_thr_engine (NETWORK *netw)
 Start the NETWORK netw Threaded Engine.
 
int netw_stop_thr_engine (NETWORK *netw)
 Stop a NETWORK connection sending and receing thread.
 
char * netw_urldecode (const char *str)
 Function to decode URL-encoded data.
 
char * netw_urlencode (const char *str, size_t len)
 function to perform URL encoding
 
int netw_wait_close (NETWORK **netw)
 Wait for peer closing a specified Network, destroy queues, free the structure.
 
int netw_wait_close_timed (NETWORK **netw, size_t timeout)
 Wait for peer closing a specified Network, destroy queues, free the structure.
 
N_STRnetw_wait_msg (NETWORK *netw, size_t refresh, size_t timeout)
 Wait a message from aimed NETWORK.
 
int recv_data (void *netw, char *buf, NSTRBYTE n)
 recv data from the socket
 
int recv_php (SOCKET s, int *_code, char **buf)
 recv data from the socket
 
int send_data (void *netw, char *buf, NSTRBYTE n)
 send data onto the socket
 
int send_php (SOCKET s, int _code, char *buf, int n)
 send data onto the socket
 

Detailed Description


Data Structure Documentation

◆ N_SOCKET

struct N_SOCKET

Structure of a N_SOCKET.

Definition at line 211 of file n_network.h.

+ Collaboration diagram for N_SOCKET:
Data Fields
struct addrinfo hints address of local machine
char * ip ip of the connected socket
unsigned long int is_blocking flag to quickly check socket mode
char * port port of socket
struct sockaddr_storage raddr connected remote addr
struct addrinfo * rhost getaddrinfo results
SOCKET sock a normal socket

◆ NETWORK

struct NETWORK

Structure of a NETWORK.

Examples
ex_network.c, and ex_network_ssl.c.

Definition at line 232 of file n_network.h.

+ Collaboration diagram for NETWORK:
Data Fields
int addr_infos_loaded Internal flag to know if we have to free addr infos.
int crypto_algo if encryption is on, which one (flags NETW_ENCRYPT_*)
int crypto_mode tell if the socket have to be encrypted (flags NETW_CRYPTO_*)
size_t deplete_timeout deplete send buffer ( 0 disabled, > 0 wait for timeout and check unset/unack datas)
pthread_mutex_t eventbolt mutex for threaded access of state event
N_SOCKET link networking socket
int mode NETWORK mode , 1 listening, 0 connecting.
int nb_pending Nb pending connection,if listening.
int nb_running_threads nb running threads, if > 0 thread engine is still running
LIST * pools pointers to network pools if members of any
LIST * recv_buf reveicing buffer (for incomming usage)
netw_func recv_data receive func ptr
pthread_t recv_thr receiving thread
pthread_mutex_t recvbolt mutex for threaded access of recv buf
sem_t send_blocker block sending func
LIST * send_buf sending buffer (for outgoing queuing )
netw_func send_data send func ptr
int send_queue_consecutive_wait send queue consecutive pool interval, used when there are still items to send, in usec
pthread_t send_thr sending thread
pthread_mutex_t sendbolt mutex for threaded access of send_buf
int so_linger close lingering value (-1 disabled, 0 force close, >0 linger )
int so_rcvbuf size of the socket recv buffer, 0 untouched, else size in bytes
int so_rcvtimeo send timeout value
int so_reuseaddr so reuseaddr state
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
int tcpnodelay state of naggle algorythm, 0 untouched, 1 forcibly disabled
int threaded_engine_status Threaded network engine state for this network.

NETW_THR_ENGINE_STARTED or NETW_THR_ENGINE_STOPPED

int user_id if part of a user property, id of the user

◆ NETWORK_HTTP_INFO

struct NETWORK_HTTP_INFO

structure for splitting HTTP requests

Examples
ex_network_ssl.c.

Definition at line 331 of file n_network.h.

+ Collaboration diagram for NETWORK_HTTP_INFO:
Data Fields
char * body
size_t content_length
char content_type[256]
char * type

◆ NETWORK_POOL

struct NETWORK_POOL

structure of a network pool

Definition at line 320 of file n_network.h.

+ Collaboration diagram for NETWORK_POOL:
Data Fields
HASH_TABLE * pool table of clients
pthread_rwlock_t rwlock thread safety

Macro Definition Documentation

◆ HEAD_CODE

#define HEAD_CODE   3

Code of a HEAD message.

Definition at line 39 of file n_network.h.

◆ HEAD_SIZE

#define HEAD_SIZE   10

Size of a HEAD message.

Definition at line 37 of file n_network.h.

◆ N_ENUM_netw_code_type

#define N_ENUM_netw_code_type (   _)
Value:
_(NETW_CLIENT, 2)\
_(NETW_SERVER, 4)\
_(NETW_RESTART_TIMER, 8)\
_(NETW_EMPTY_SENDBUF, 16)\
_(NETW_EMPTY_RECVBUF, 32)\
_(NETW_RUN, 64)\
_(NETW_EXIT_ASKED, 128)\
_(NETW_EXITED, 256)\
_(NETW_ERROR, 512)\
_(NETW_ENCRYPT_NONE, 1024)\
_(NETW_ENCRYPT_OPENSSL, 2048)\
_(NETW_THR_ENGINE_STARTED, 4096)\
_(NETW_THR_ENGINE_STOPPED, 8192)\
_(NETW_DESTROY_RECVBUF, 16384)\
_(NETW_DESTROY_SENDBUF, 32768)

Network codes definition.

Definition at line 187 of file n_network.h.

◆ NETWORK_CONSECUTIVE_SEND_TIMEOUT

#define NETWORK_CONSECUTIVE_SEND_TIMEOUT   8192

Flag to set consecutive send waiting timeout

Definition at line 35 of file n_network.h.

◆ NETWORK_DEPLETE_TIMEOUT

#define NETWORK_DEPLETE_TIMEOUT   4096

Flag to set send buffer depletion timeout

Definition at line 33 of file n_network.h.

◆ NETWORK_IPALL

#define NETWORK_IPALL   0

Flag for auto detection by OS of ip version to use.

Examples
ex_network.c, and ex_network_ssl.c.

Definition at line 27 of file n_network.h.

◆ NETWORK_IPV4

#define NETWORK_IPV4   1

Flag to force IPV4

Examples
ex_network.c, and ex_network_ssl.c.

Definition at line 29 of file n_network.h.

◆ NETWORK_IPV6

#define NETWORK_IPV6   2

Flag to force IPV6

Examples
ex_network.c, and ex_network_ssl.c.

Definition at line 31 of file n_network.h.

◆ SOCKET_SIZE_FORMAT

#define SOCKET_SIZE_FORMAT   "%d"

socket associated printf style

Examples
ex_network_ssl.c.

Definition at line 54 of file n_network.h.

Typedef Documentation

◆ netw_func

typedef int(* netw_func) (void *, char *, NSTRBYTE)

send/recv func ptr type

Definition at line 208 of file n_network.h.

◆ SOCKET

typedef int SOCKET

default socket declaration

socket type for windows

Definition at line 52 of file n_network.h.

Function Documentation

◆ netw_accept_from()

NETWORK * netw_accept_from ( NETWORK from)

make a normal blocking 'accept' .

Network 'from' must be allocated with netw_make_listening.

Parameters
fromThe network from which to obtaion the connection
Returns
NULL

Definition at line 2093 of file n_network.c.

References netw_accept_from_ex().

+ Here is the call graph for this function:

◆ netw_accept_from_ex()

NETWORK * netw_accept_from_ex ( NETWORK from,
int  send_list_limit,
int  recv_list_limit,
int  blocking,
int *  retval 
)

make a normal 'accept' .

Network 'from' must be allocated with netw_make_listening.

Parameters
fromthe network from where we accept
send_list_limitInternal sending list maximum number of item. 0 or negative for unrestricted
recv_list_limitInternal receiving list maximum number of item. 0 or negative for unrestricted
blockingset to -1 to make it non blocking, to 0 for blocking, else it's the select timeout value in msecs.
retvalEAGAIN ou EWOULDBLOCK or neterrno (use netstrerr( retval) to obtain a string describing the code )
Returns
NULL on failure, if not a pointer to the connected network

Definition at line 1923 of file n_network.c.

References __n_assert, _str, FreeNoLog, get_in_addr(), LOG_DEBUG, LOG_ERR, Malloc, n_log, neterrno, netstrerror, netw_close(), netw_init_wsa(), netw_new(), netw_set(), netw_set_blocking(), netw_setsockopt(), recv_data(), and send_data().

Referenced by netw_accept_from(), and netw_accept_nonblock_from().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_accept_nonblock_from()

NETWORK * netw_accept_nonblock_from ( NETWORK from,
int  blocking 
)

make a normal blocking 'accept' .

Network 'from' must be allocated with netw_make_listening.

Parameters
fromThe network from which to obtaion the connection
blockingset to -1 to make it non blocking, to 0 for blocking, else it's the select timeout value in mseconds.
Returns
NULL

Definition at line 2106 of file n_network.c.

References netw_accept_from_ex().

+ Here is the call graph for this function:

◆ netw_add_msg()

int netw_add_msg ( NETWORK netw,
N_STR msg 
)

Add a message to send in aimed NETWORK.

Parameters
netwNETWORK where add the message
msgthe message to add
Returns
TRUE if success FALSE on error

Definition at line 2118 of file n_network.c.

References __n_assert, free_nstr_ptr(), list_push(), LOG_ERR, and n_log.

Referenced by netw_pool_broadcast(), netw_send_ident(), netw_send_ping(), netw_send_position(), netw_send_quit(), netw_send_string_to(), netw_send_string_to_all(), send_net_datas(), userlist_add_msg_to_ex(), userlist_send_waiting_msgs(), and userlist_user_send_waiting_msgs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_add_msg_ex()

int netw_add_msg_ex ( NETWORK netw,
char *  str,
unsigned int  length 
)

Add a message to send in aimed NETWORK.

Parameters
netwNETWORK where add the message
strthe message to add
lengththe size of the message to add
Returns
TRUE if success FALSE on error

Definition at line 2154 of file n_network.c.

References __n_assert, free_nstr_ptr(), list_push(), LOG_ERR, Malloc, and n_log.

+ Here is the call graph for this function:

◆ netw_build_http_response()

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

Parameters
http_responsepointer to a N_STR *response. Will be set to the response, or NULL
status_coderesponse http status code
server_nameresponse 'Server' in headers
content_typeresponse 'Content-Type' in headers
additional_headersadditional response headers, can be "" if no additional headers, else 'backslash r backslash n' separated key: values
bodyresponse 'Server' in headers
Returns
TRUE if the http_response was built, FALSE if not

Definition at line 3852 of file n_network.c.

References __n_assert, LOG_DEBUG, n_log, netw_get_http_date(), netw_get_http_status_message(), nstrcat(), and nstrprintf.

+ Here is the call graph for this function:

◆ netw_calculate_urlencoded_size()

size_t netw_calculate_urlencoded_size ( const char *  str,
size_t  len 
)

function to calculate the required size for the URL-encoded string

Parameters
strclear string from which we want to know the urlencoded size
lenlength of the input string
Returns
the urlencoded size if the string was urlencoded

Definition at line 3505 of file n_network.c.

Referenced by netw_urlencode().

+ Here is the caller graph for this function:

◆ netw_close()

int netw_close ( NETWORK **  netw)

Closing a specified Network, destroy queues, free the structure.

Parameters
netwA NETWORK *network to close
Returns
TRUE on success , FALSE on failure

Definition at line 1588 of file n_network.c.

References __n_assert, Free, FreeNoLog, list_destroy(), list_foreach, LOG_DEBUG, LOG_ERR, n_log, netw_get_state(), netw_pool_remove(), netw_set(), and netw_stop_thr_engine().

Referenced by netw_accept_from_ex(), netw_connect_ex(), netw_make_listening(), netw_new(), netw_set_blocking(), and netw_wait_close_timed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_connect()

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.

Parameters
netwa NETWORK *object
hostHost or IP to connect to
portPort to use to connect
ip_versionNETWORK_IPALL for both ipv4 and ipv6 , NETWORK_IPV4 or NETWORK_IPV6
Returns
TRUE or FALSE

Definition at line 1449 of file n_network.c.

References _str, LOG_INFO, n_log, and netw_connect_ex().

+ Here is the call graph for this function:

◆ netw_connect_ex()

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.

Parameters
netwa NETWORK *object
hostHost or IP to connect to
portPort to use to connect
send_list_limitInternal sending list maximum number of item. 0 or negative for unrestricted
recv_list_limitInternal receiving list maximum number of item. 0 or negative for unrestricted
ip_versionNETWORK_IPALL for both ipv4 and ipv6 , NETWORK_IPV4 or NETWORK_IPV6
ssl_key_fileNULL or the path to the SSL key file to use. If set, a ssl_cert_file must also be provided
ssl_cert_fileNULL or the path to the SSL certificate file to use. If set, a ssl_key_file must also be provided
Returns
TRUE or FALSE

Definition at line 1289 of file n_network.c.

References __n_assert, _str, Free, FreeNoLog, get_in_addr(), LOG_DEBUG, LOG_ERR, LOG_INFO, Malloc, n_log, neterrno, netstrerror, netw_close(), netw_init_wsa(), netw_new(), netw_set(), NETWORK_IPV4, and NETWORK_IPV6.

Referenced by netw_connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_destroy_pool()

int netw_destroy_pool ( NETWORK_POOL **  netw_pool)

free a NETWORK_POOL *pool

Parameters
netw_poolthe address of a NETWORK_POOL *pointer to free
Returns
TRUE or FALSE

Definition at line 3169 of file n_network.c.

References __n_assert, destroy_ht(), Free, rw_lock_destroy, unlock, and write_lock.

+ Here is the call graph for this function:

◆ netw_extract_http_info()

NETWORK_HTTP_INFO netw_extract_http_info ( char *  request)

extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure

Parameters
requestthe raw http request string
Returns
a NETWORK_HTTP_INFO structure, filled with zeros in case of errors

Definition at line 3592 of file n_network.c.

References netw_extract_http_request_type().

+ Here is the call graph for this function:

◆ netw_extract_http_request_type()

char * netw_extract_http_request_type ( const char *  request)

function to extract the request method from an http request

Parameters
requestthe raw http request
Returns
a char *copy of the request type, or NULL

Definition at line 3558 of file n_network.c.

Referenced by netw_extract_http_info().

+ Here is the caller graph for this function:

◆ netw_get_http_date()

void netw_get_http_date ( char *  buffer,
size_t  buffer_size 
)

helper function to generate the current date in HTTP format

Parameters
bufferthe target buffer, must be big enough to hold the date
buffer_sizethe size of the target buffer

Definition at line 3835 of file n_network.c.

Referenced by netw_build_http_response().

+ Here is the caller graph for this function:

◆ netw_get_http_status_message()

const char * netw_get_http_status_message ( int  status_code)

helper function to convert status code to a human-readable message

Parameters
status_codethe code to convert into a human-readable message
Returns
a pointer to the human readable message. 'Unknown' is returned if the status code is not supported

Definition at line 3818 of file n_network.c.

Referenced by netw_build_http_response().

+ Here is the caller graph for this function:

◆ netw_get_msg()

N_STR * netw_get_msg ( NETWORK netw)

Get a message from aimed NETWORK.

Parameters
netwNETWORK where get the msg
Returns
NULL or a filled N_STR

Definition at line 2197 of file n_network.c.

References __n_assert, and list_shift.

Referenced by manage_client(), and netw_wait_msg().

+ Here is the caller graph for this function:

◆ netw_get_queue_status()

int netw_get_queue_status ( NETWORK netw,
int *  nb_to_send,
int *  nb_to_read 
)

retrieve network send queue status

Parameters
netwNETWORK object
nb_to_sendNumber of messages still in send buffer (not yet submitted to kernel)
nb_to_readNumber of message already read by the kernel, waiting in the local message list
Returns
TRUE or FALSE

Definition at line 3125 of file n_network.c.

References __n_assert.

◆ netw_get_state()

int netw_get_state ( NETWORK netw,
int *  state,
int *  thr_engine_status 
)

Get the state of a network.

Parameters
netwThe NETWORK *connection to query
statepointer to network status storage , NETW_RUN , NETW_EXIT_ASKED , NETW_EXITED
thr_engine_statuspointer to network thread engine status storage ,NETW_THR_ENGINE_STARTED , NETW_THR_ENGINE_STOPPED
Returns
TRUE or FALSE

Definition at line 1484 of file n_network.c.

References LOG_ERR, and n_log.

Referenced by manage_client(), netw_close(), netw_recv_func(), netw_send_func(), netw_stop_thr_engine(), netw_wait_close_timed(), and netw_wait_msg().

+ Here is the caller graph for this function:

◆ netw_get_url_from_http_request()

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.

Parameters
requestraw http request to decode
urlpointer to an allocated char *holder
sizethe size of the url holder

Definition at line 3662 of file n_network.c.

References __n_assert.

◆ netw_guess_http_content_type()

const char * netw_guess_http_content_type ( const char *  url)

function to guess the content type based on URL extension

Parameters
urlthe url from which we want to guess the content type
Returns
a pointer to the detected content type. It will be 'unknown' if no detection worked

Definition at line 3755 of file n_network.c.

◆ netw_info_destroy()

int netw_info_destroy ( NETWORK_HTTP_INFO  http_request)

destroy a NETWORK_HTTP_INFO loaded informations

Parameters
http_requestthe parsed NETWORK_HTTP_INFO request
Returns
TRUE ;

Definition at line 3648 of file n_network.c.

References FreeNoLog.

◆ netw_init_wsa()

int netw_init_wsa ( int  mode,
int  v1,
int  v2 
)

Do not directly use, internal api.

Initialize winsock dll loading on windows if needed.

Parameters
mode1 for opening 0 for close 2 for status
v1First digit of version requested
v2Second digit of version requested
Returns
TRUE on success FALSE on error

Definition at line 718 of file n_network.c.

Referenced by netw_accept_from_ex(), netw_connect_ex(), and netw_make_listening().

+ Here is the caller graph for this function:

◆ netw_make_listening()

int netw_make_listening ( NETWORK **  netw,
char *  addr,
char *  port,
int  nbpending,
int  ip_version 
)

Make a NETWORK be a Listening network.

Parameters
netwA NETWORK **network to make listening
addrAdress to bind, NULL for automatic address filling
portFor choosing a PORT to listen to
nbpendingNumber of pending connection when listening
ip_versionNETWORK_IPALL for both ipv4 and ipv6 , NETWORK_IPV4 or NETWORK_IPV6
Returns
TRUE on success, FALSE on error

Definition at line 1797 of file n_network.c.

References __n_assert, _str, FreeNoLog, get_in_addr(), LOG_ERR, Malloc, n_log, neterrno, netstrerror, netw_close(), netw_init_wsa(), netw_new(), netw_set(), netw_setsockopt(), NETWORK_IPV4, and NETWORK_IPV6.

+ Here is the call graph for this function:

◆ netw_new_pool()

NETWORK_POOL * netw_new_pool ( int  nb_min_element)

return a new network pool of nb_min_element

Parameters
nb_min_elementsize of internal hash table for network pool
Returns
a new NETWORK_POOL or NULL

Definition at line 3147 of file n_network.c.

References __n_assert, Free, init_lock, Malloc, and new_ht().

+ Here is the call graph for this function:

◆ netw_parse_post_data()

HASH_TABLE * netw_parse_post_data ( const char *  post_data)

Function to parse POST data.

Parameters
post_datathe post data to parse
Returns
An allocated HASH_TABLE with post data as keys/values, or NULL

Definition at line 3710 of file n_network.c.

References __n_assert, ht_put_string(), netw_urldecode(), and new_ht().

+ Here is the call graph for this function:

◆ netw_pool_add()

int netw_pool_add ( NETWORK_POOL netw_pool,
NETWORK netw 
)

add a NETWORK *netw to a NETWORK_POOL *pool

Parameters
netw_pooltargeted network pool
netwnetwork to add
Returns
TRUE or FALSE

Definition at line 3207 of file n_network.c.

References __n_assert, _nstr, free_nstr, ht_get_ptr(), ht_put_ptr(), list_push(), LOG_DEBUG, LOG_ERR, n_log, netw_pool_netw_close(), nstrprintf, unlock, and write_lock.

+ Here is the call graph for this function:

◆ netw_pool_broadcast()

int netw_pool_broadcast ( NETWORK_POOL netw_pool,
NETWORK from,
N_STR net_msg 
)

add net_msg to all network in netork pool

Parameters
netw_pooltargeted network pool
fromsource network
net_msgmesage to broadcast
Returns
TRUE or FALSE

Definition at line 3301 of file n_network.c.

References __n_assert, hash_val, ht_foreach, netw_add_msg(), nstrdup(), read_lock, and unlock.

+ Here is the call graph for this function:

◆ netw_pool_nbclients()

int netw_pool_nbclients ( NETWORK_POOL netw_pool)

return the number of networks in netw_pool

Parameters
netw_pooltargeted network pool
Returns
-1 or the number of networks in netw_pool

Definition at line 3332 of file n_network.c.

References __n_assert, read_lock, and unlock.

◆ netw_pool_netw_close()

void netw_pool_netw_close ( void *  netw_ptr)

close a network from a network pool

Parameters
netw_ptrNETWORK *network pointer

Definition at line 3191 of file n_network.c.

References __n_assert, LOG_DEBUG, and n_log.

Referenced by netw_pool_add().

+ Here is the caller graph for this function:

◆ netw_pool_remove()

int netw_pool_remove ( NETWORK_POOL netw_pool,
NETWORK netw 
)

remove a NETWORK *netw to a NETWORK_POOL *pool

Parameters
netw_pooltargeted network pool
netwnetwork to remove
Returns
TRUE or FALSE

Definition at line 3260 of file n_network.c.

References __n_assert, _nstr, free_nstr, ht_remove(), list_search(), LOG_DEBUG, LOG_ERR, n_log, nstrprintf, remove_list_node, unlock, and write_lock.

Referenced by netw_close().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_recv_func()

void * netw_recv_func ( void *  NET)

To Thread Receiving function.

Parameters
NETthe NETWORK connection to use
Returns
NULL ;

Definition at line 2449 of file n_network.c.

References __n_assert, _str, free_nstr_ptr(), list_push(), LOG_DEBUG, LOG_ERR, Malloc, n_log, netw_get_state(), netw_set(), and NETWORK::recv_data.

Referenced by netw_start_thr_engine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_send_func()

void * netw_send_func ( void *  NET)

Thread send function.

Parameters
NETthe NETWORK connection to use, casted into (void*)
Returns
NULL

Definition at line 2319 of file n_network.c.

References __n_assert, _str, free_nstr, list_shift, LOG_DEBUG, LOG_ERR, n_log, netw_get_state(), netw_set(), NETWORK::send_data, and u_sleep().

Referenced by netw_start_thr_engine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_send_ident()

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.

Parameters
netwThe aimed NETWORK where we want to add something to send
typetype of identification ( NETW_IDENT_REQUEST , NETW_IDENT_NEW )
idThe ID of the sending client
nameUsername
passwdPassword
Returns
TRUE or FALSE

Definition at line 3392 of file n_network.c.

References __n_assert, netmsg_make_ident(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_send_ping()

int netw_send_ping ( NETWORK netw,
int  type,
int  id_from,
int  id_to,
int  time 
)

Add a ping reply to the network.

Parameters
netwThe aimed NETWORK where we want to add something to send
id_fromIdentifiant of the sender
id_toIdentifiant of the destination, -1 if the serveur itslef is targetted
timeThe time it was when the ping was sended
typeNETW_PING_REQUEST or NETW_PING_REPLY
Returns
TRUE or FALSE

Definition at line 3370 of file n_network.c.

References __n_assert, netmsg_make_ping(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_send_position()

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.

Parameters
netwThe aimed NETWORK where we want to add something to send
idThe ID of the sending client
XX position inside a big grid
YY position inside a big grid
vxX speed
vyY speed
acc_xY acceleration
acc_yX acceleration
time_stampCurrent Time when sending (for some delta we would want to compute )
Returns
TRUE or FALSE

Definition at line 3419 of file n_network.c.

References __n_assert, netmsg_make_position_msg(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_send_quit()

int netw_send_quit ( NETWORK netw)

Add a formatted NETMSG_QUIT message to the specified network.

Parameters
netwThe aimed NETWORK
Returns
TRUE or FALSE

Definition at line 3486 of file n_network.c.

References __n_assert, netmsg_make_quit_msg(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_send_string_to()

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.

Parameters
netwThe aimed NETWORK where we want to add something to send
id_toThe ID of the targetted client
nameSender Name
chanchannel to use
txtSender text
colorSender text color
Returns
TRUE or FALSE

Definition at line 3444 of file n_network.c.

References __n_assert, netmsg_make_string_msg(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_send_string_to_all()

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.

Parameters
netwThe aimed NETWORK where we want to add something to send
nameName of user
chanTarget Channel, if any. Pass "ALL" to target the default channel
txtThe text to send
colorThe color of the text
Returns
TRUE or FALSE;

Definition at line 3467 of file n_network.c.

References __n_assert, netmsg_make_string_msg(), and netw_add_msg().

+ Here is the call graph for this function:

◆ netw_set()

int netw_set ( NETWORK netw,
int  flag 
)

Restart or reset the specified network ability.

Parameters
netwThe NETWORK *connection to modify
flagNETW_EMPTY_SENDBUF, NETW_EMPTY_RECVBUF, NETW_RUN , NETW_EXIT_ASKED , NETW_EXITED
Returns
TRUE or FALSE

Definition at line 1511 of file n_network.c.

References list_destroy(), and list_empty().

Referenced by netw_accept_from_ex(), netw_close(), netw_connect_ex(), netw_make_listening(), netw_recv_func(), netw_send_func(), and netw_stop_thr_engine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_set_blocking()

int netw_set_blocking ( NETWORK netw,
unsigned long int  is_blocking 
)

Modify blocking socket mode.

Parameters
netwThe network to configure
is_blocking0 NON BLOCk , 1 BLOCK
Returns
TRUE or FALSE

Definition at line 772 of file n_network.c.

References __n_assert, _str, FreeNoLog, LOG_DEBUG, LOG_ERR, n_log, neterrno, netstrerror, and netw_close().

Referenced by netw_accept_from_ex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_set_user_id()

int netw_set_user_id ( NETWORK netw,
int  id 
)

associate an id and a network

Parameters
netwtargeted network
idid we want to associated with
Returns
TRUE or FALSE

Definition at line 3352 of file n_network.c.

References __n_assert.

◆ netw_setsockopt()

int netw_setsockopt ( NETWORK netw,
int  optname,
int  value 
)

Modify common socket options on the given netw.

Parameters
netwThe socket to configure
optnameNETWORK_DEPLETE_TIMEOUT,NETWORK_CONSECUTIVE_SEND_TIMEOUT ,SO_REUSEADDR,TCP_NODELAY,SO_SNDBUF,SO_RCVBUF,SO_LINGER,SO_RCVTIMEO,SO_SNDTIMEO. Please refer to man setsockopt for details
valueThe value of the socket parameter
Returns
TRUE or FALSE

Definition at line 836 of file n_network.c.

References __n_assert, _str, FreeNoLog, LOG_ERR, n_log, neterrno, netstrerror, NETWORK_CONSECUTIVE_SEND_TIMEOUT, and NETWORK_DEPLETE_TIMEOUT.

Referenced by netw_accept_from_ex(), and netw_make_listening().

+ Here is the caller graph for this function:

◆ netw_start_thr_engine()

int netw_start_thr_engine ( NETWORK netw)

Start the NETWORK netw Threaded Engine.

Create a sending & receiving thread.

Parameters
netwThe aimed NETWORK connection to start receiving data
Returns
TRUE on success FALSE on failure

Definition at line 2278 of file n_network.c.

References __n_assert, _str, LOG_ERR, n_log, netw_recv_func(), and netw_send_func().

Referenced by manage_client().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_stop_thr_engine()

int netw_stop_thr_engine ( NETWORK netw)

Stop a NETWORK connection sending and receing thread.

Parameters
netwThe aimed NETWORK conection to stop
Returns
TRUE on success FALSE on failure

Definition at line 2599 of file n_network.c.

References __n_assert, LOG_DEBUG, LOG_ERR, n_log, netw_get_state(), and netw_set().

Referenced by netw_close().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_urldecode()

char * netw_urldecode ( const char *  str)

Function to decode URL-encoded data.

Parameters
strinput string to decode
Returns
url decoded string

Definition at line 3685 of file n_network.c.

Referenced by netw_parse_post_data().

+ Here is the caller graph for this function:

◆ netw_urlencode()

char * netw_urlencode ( const char *  str,
size_t  len 
)

function to perform URL encoding

Parameters
strinput string
leninput string length
Returns
urlencoded char *string or NULL

Definition at line 3526 of file n_network.c.

References netw_calculate_urlencoded_size().

+ Here is the call graph for this function:

◆ netw_wait_close()

int netw_wait_close ( NETWORK **  netw)

Wait for peer closing a specified Network, destroy queues, free the structure.

Default 30 seconds timeout

Warning
Do not use on the accept socket itself (the server socket) as it will display false errors
Parameters
netwA NETWORK *network to close
Returns
TRUE on success , FALSE on failure

Definition at line 1706 of file n_network.c.

References netw_wait_close_timed().

Referenced by manage_client().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_wait_close_timed()

int netw_wait_close_timed ( NETWORK **  netw,
size_t  timeout 
)

Wait for peer closing a specified Network, destroy queues, free the structure.

Warning
Do not use on the accept socket itself (the server socket) as it will display false errors
Parameters
netwA NETWORK *network to close
timeouttimeout in seconds before force close engine , 0 to instantly close (not recommanded)
Returns
TRUE on success , FALSE on failure

Definition at line 1721 of file n_network.c.

References __n_assert, _str, deplete_send_buffer(), FreeNoLog, LOG_ERR, N_ENUM_ENTRY, n_log, neterrno, netstrerror, netw_close(), and netw_get_state().

Referenced by netw_wait_close().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ netw_wait_msg()

N_STR * netw_wait_msg ( NETWORK netw,
size_t  refresh,
size_t  timeout 
)

Wait a message from aimed NETWORK.

Recheck each usec until a valid

Parameters
netwThe link on which we wait a message
refreshThe time in usec between each check until there is a message
timeoutin usecs , maximum amount of time to wait before return. 0 to disable.
Returns
NULL or a filled N_STR

Definition at line 2221 of file n_network.c.

References __n_assert, LOG_ERR, N_ENUM_ENTRY, n_log, netw_get_msg(), netw_get_state(), and u_sleep().

+ Here is the call graph for this function:

◆ recv_data()

int recv_data ( void *  netw,
char *  buf,
NSTRBYTE  n 
)

recv data from the socket

Parameters
netwconnected NETWORK
bufpointer to buffer
nnumber of characters we want
Returns
-1 on error, -2 on disconnection n on success

Definition at line 2694 of file n_network.c.

References __n_assert, _str, CALL_RETRY, FreeNoLog, LOG_DEBUG, LOG_ERR, n_log, neterrno, and netstrerror.

Referenced by netw_accept_from_ex(), and netw_new().

+ Here is the caller graph for this function:

◆ recv_php()

int recv_php ( SOCKET  s,
int *  _code,
char **  buf 
)

recv data from the socket

Parameters
sconnected socket
_codepointer to store the code
bufpointer to buffer
Returns
-1 on error, size on success

Definition at line 2995 of file n_network.c.

References _str, Free, FreeNoLog, HEAD_CODE, HEAD_SIZE, LOG_ERR, Malloc, n_log, neterrno, and netstrerror.

◆ send_data()

int send_data ( void *  netw,
char *  buf,
NSTRBYTE  n 
)

send data onto the socket

Parameters
netwconnected NETWORK
bufpointer to buffer
nnumber of characters we want to send
Returns
-1 on error, -2 on disconnection, n on success

Definition at line 2642 of file n_network.c.

References __n_assert, _str, CALL_RETRY, FreeNoLog, LOG_DEBUG, LOG_ERR, n_log, neterrno, and netstrerror.

Referenced by netw_accept_from_ex(), and netw_new().

+ Here is the caller graph for this function:

◆ send_php()

int send_php ( SOCKET  s,
int  _code,
char *  buf,
int  n 
)

send data onto the socket

Parameters
sconnected socket
bufpointer to buffer
nnumber of characters we want to send
_codeCode for php decoding rule
Returns
-1 on error, n on success

Definition at line 2896 of file n_network.c.

References _str, CALL_RETRY, FreeNoLog, HEAD_CODE, HEAD_SIZE, LOG_ERR, n_log, neterrno, and netstrerror.