Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_network.c File Reference

Network Engine. More...

#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include "nilorea/n_network.h"
#include "nilorea/n_network_msg.h"
#include "nilorea/n_log.h"
#include "nilorea/n_hash.h"
#include <sys/wait.h>
+ Include dependency graph for n_network.c:

Go to the source code of this file.

Macros

#define neterrno   errno
 get last socket error code, linux version
 
#define netstrerror(code)
 BSD style errno string NO WORKING ON REDHAT.
 

Functions

 __attribute__ ((unused))
 
__netw_code_type __netw_code_type_fromString (const char *str_value)
 
__netw_code_type __netw_code_type_fromStringEx (const char *str_value, int index)
 
bool __netw_code_type_isStringValid (const char *str_value)
 
bool __netw_code_type_isValid (__netw_code_type value)
 network error code
 
const char * __netw_code_type_toString (__netw_code_type value)
 
int deplete_send_buffer (int fd, int timeout)
 wait until the socket is empty or timeout, checking each 100 msec.
 
char * get_in_addr (struct sockaddr *sa)
 get sockaddr, IPv4 or IPv6
 
size_t htonst (size_t value)
 host to network size_t
 
NETWORKnetw_accept_from (NETWORK *from)
 make a normal blocking 'accept' .
 
NETWORKnetw_accept_from_ex (NETWORK *from, size_t send_list_limit, size_t 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, size_t send_list_limit, size_t recv_list_limit, int ip_version, char *ssl_key_file, char *ssl_cert_file)
 
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
 
int 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.
 
char * netw_get_openssl_error_string ()
 
int netw_get_queue_status (NETWORK *netw, size_t *nb_to_send, size_t *nb_to_read)
 retrieve network send queue status
 
int netw_get_state (NETWORK *netw, uint32_t *state, int *thr_engine_status)
 Get the state of a network.
 
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.
 
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
 
static void netw_init_locks (void)
 
int netw_init_openssl (void)
 Do not directly use, internal api.
 
int netw_init_wsa (int mode, int v1, int v2)
 Do not directly use, internal api.
 
static void netw_kill_locks (void)
 
int netw_make_listening (NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
 Make a NETWORK be a Listening network.
 
NETWORKnetw_new (size_t send_list_limit, size_t recv_list_limit)
 Return an empty allocated network ready to be netw_closed.
 
NETWORK_POOLnetw_new_pool (size_t 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
 
size_t 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_crypto (NETWORK *netw, char *key, char *certificate)
 activate SSL encryption on selected network, using key and certificate
 
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_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.
 
void netw_ssl_print_errors (SOCKET socket)
 
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.
 
int netw_unload_openssl (void)
 Do not directly use, internal api.
 
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
 
N_STRnetw_wait_msg (NETWORK *netw, unsigned int refresh, size_t timeout)
 
size_t ntohst (size_t value)
 
ssize_t recv_data (void *netw, char *buf, uint32_t n)
 recv data from the socket
 
ssize_t recv_php (SOCKET s, int *_code, char **buf)
 recv data from the socket
 
ssize_t recv_ssl_data (void *netw, char *buf, uint32_t n)
 recv data from the socket
 
ssize_t send_data (void *netw, char *buf, uint32_t n)
 send data onto the socket
 
ssize_t send_php (SOCKET s, int _code, char *buf, int n)
 send data onto the socket
 
ssize_t send_ssl_data (void *netw, char *buf, uint32_t n)
 send data onto the socket
 

Variables

static pthread_mutex_t * netw_ssl_lockarray
 

Detailed Description

Network Engine.

Author
Castagnier Mickael
Version
1.0
Date
10/05/2005

Definition in file n_network.c.

Macro Definition Documentation

◆ neterrno

#define neterrno   errno

get last socket error code, linux version

Definition at line 540 of file n_network.c.

◆ netstrerror

#define netstrerror (   code)
Value:
({ \
char* __errmsg = NULL; \
errno = 0; \
__errmsg = strdup(strerror(code)); \
if (errno == ENOMEM) { \
__errmsg = NULL; \
} \
__errmsg; \
})

BSD style errno string NO WORKING ON REDHAT.

get last socket error code as a string, linux version

Definition at line 555 of file n_network.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (unused)  )

Definition at line 1071 of file n_network.c.

◆ get_in_addr()

char * get_in_addr ( struct sockaddr *  sa)

get sockaddr, IPv4 or IPv6

Parameters
saaddrinfo to get
Returns
socket address

Definition at line 680 of file n_network.c.

Referenced by netw_accept_from_ex(), and netw_make_listening().

+ Here is the caller graph for this function:

◆ netw_get_openssl_error_string()

char * netw_get_openssl_error_string ( )

Definition at line 1002 of file n_network.c.

◆ netw_init_locks()

static void netw_init_locks ( void  )
static

Definition at line 1088 of file n_network.c.

◆ netw_kill_locks()

static void netw_kill_locks ( void  )
static

Definition at line 1102 of file n_network.c.

◆ netw_new()

NETWORK * netw_new ( size_t  send_list_limit,
size_t  recv_list_limit 
)

Return an empty allocated network ready to be netw_closed.

Parameters
send_list_limitThread engine number of tosend message limit. From UNLIMITED_LIST_ITEMS (0) to MAX_LIST_ITEMS (SIZE_MAX).
recv_list_limitThread engine number of received message limit. From UNLIMITED_LIST_ITEMS (0) to MAX_LIST_ITEMS (SIZE_MAX).
Returns
NULL or a new empty network

Definition at line 573 of file n_network.c.

References __n_assert, NETWORK::addr_infos_loaded, NETWORK::certificate, NETWORK::crypto_algo, NETWORK::ctx, NETWORK::deplete_queues_timeout, NETWORK::deplete_socket_timeout, NETWORK::eventbolt, Free, N_SOCKET::hints, N_SOCKET::ip, N_SOCKET::is_blocking, NETWORK::key, NETWORK::link, LOG_ERR, Malloc, MAX_LIST_ITEMS, NETWORK::method, NETWORK::mode, n_log, NETWORK::nb_pending, NETWORK::nb_running_threads, netw_close(), NETWORK::pools, N_SOCKET::port, N_SOCKET::raddr, NETWORK::recv_buf, NETWORK::recv_data, recv_data(), NETWORK::recvbolt, NETWORK::send_blocker, NETWORK::send_buf, NETWORK::send_data, send_data(), NETWORK::send_queue_consecutive_wait, NETWORK::sendbolt, NETWORK::so_keepalive, NETWORK::so_linger, NETWORK::so_rcvbuf, NETWORK::so_rcvtimeo, NETWORK::so_reuseaddr, NETWORK::so_sndbuf, NETWORK::so_sndtimeo, N_SOCKET::sock, NETWORK::ssl, NETWORK::state, NETWORK::tcpnodelay, NETWORK::threaded_engine_status, NETWORK::user_id, and NETWORK::wait_close_timeout.

Referenced by netw_accept_from_ex(), and netw_make_listening().

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

◆ netw_ssl_print_errors()

void netw_ssl_print_errors ( SOCKET  socket)

Definition at line 1025 of file n_network.c.

Variable Documentation

◆ netw_ssl_lockarray

pthread_mutex_t* netw_ssl_lockarray
static

Definition at line 1069 of file n_network.c.