HomePort
Data Structures | Macros | Typedefs | Enumerations | Functions
hpd_tcpd.h File Reference
Include dependency graph for hpd_tcpd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  hpd_tcpd_settings
 Settings struct for tcpd. More...
 

Macros

#define HPD_TCPD_SETTINGS_DEFAULT
 Default settings for tcpd. More...
 

Typedefs

typedef struct hpd_tcpd_conn hpd_tcpd_conn_t
 
typedef hpd_tcpd_return_t(* hpd_tcpd_data_f) (hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ws_ctx, void **conn_ctx, const char *buf, size_t len)
 
typedef hpd_tcpd_return_t(* hpd_tcpd_nodata_f) (hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ws_ctx, void **conn_ctx)
 
typedef enum hpd_tcpd_return hpd_tcpd_return_t
 
typedef struct hpd_tcpd_settings hpd_tcpd_settings_t
 
typedef struct hpd_tcpd hpd_tcpd_t
 

Enumerations

enum  hpd_tcpd_return { HPD_TCPD_R_CONTINUE = 0, HPD_TCPD_R_STOP = 1 }
 

Functions

hpd_error_t hpd_tcpd_conn_close (hpd_tcpd_conn_t *conn)
 Close a connection, after the remaining data has been sent. More...
 
hpd_error_t hpd_tcpd_conn_get_ip (hpd_tcpd_conn_t *conn, const char **ip)
 Get the IP address of the client. More...
 
hpd_error_t hpd_tcpd_conn_keep_open (hpd_tcpd_conn_t *conn)
 Disable timeout on connection. More...
 
hpd_error_t hpd_tcpd_conn_kill (hpd_tcpd_conn_t *conn)
 Kill and clean up after a connection. More...
 
hpd_error_t hpd_tcpd_conn_sendf (hpd_tcpd_conn_t *conn, const char *fmt,...)
 Send message on connection. More...
 
hpd_error_t hpd_tcpd_conn_vsendf (hpd_tcpd_conn_t *conn, const char *fmt, va_list vp)
 Send message on connection. More...
 
hpd_error_t hpd_tcpd_create (hpd_tcpd_t **tcpd, hpd_tcpd_settings_t *settings, const hpd_module_t *context, hpd_ev_loop_t *loop)
 Create new tcpd instance. More...
 
hpd_error_t hpd_tcpd_destroy (hpd_tcpd_t *tcpd)
 Destroy tcpd and free used memory. More...
 
hpd_error_t hpd_tcpd_start (hpd_tcpd_t *tcpd)
 Start the tcpd. More...
 
hpd_error_t hpd_tcpd_stop (hpd_tcpd_t *tcpd)
 Stop an already running tcpd. More...
 

Data Structure Documentation

struct hpd_tcpd_settings

Settings struct for tcpd.

Please initialise this struct as following, to ensure that all settings have acceptable default values:

The settings hold a series of callbacks of type either data_cb or nodata_cb. Do not expect the string parameter in data callbacks to be null terminated. All data callbacks are called on chunks and therefore may be called multiple times. It is up to the implementer of these callbacks to concatenate the results if needed.

The callbacks are called in the following order:

Notes on the return values of callbacks:

  • on_connect:
    • zero: Accept client and start listening for data.
    • non-zero: Reject client, e.g. kill connection.
  • on_receive:
    • zero: Data accepted, continue to listen for further data.
    • non-zero: Data rejected, kill connection.
  • on_disconnect:
    • any: Ignored as client is to be killed anyways.

Definition at line 94 of file hpd_tcpd.h.

Collaboration diagram for hpd_tcpd_settings:
Data Fields
size_t max_data_size
hpd_tcpd_nodata_f on_connect
hpd_tcpd_nodata_f on_disconnect
hpd_tcpd_data_f on_receive
hpd_tcpd_port_t port Port number.
int retry_delay
void * tcpd_ctx
int timeout

Macro Definition Documentation

#define HPD_TCPD_SETTINGS_DEFAULT
Value:
{ \
.port = HPD_TCPD_P_HTTP, \
.timeout = 15, \
.retry_delay = 5, \
.max_data_size = 1024, \
.on_connect = NULL, \
.on_receive = NULL, \
.on_disconnect = NULL, \
.tcpd_ctx = NULL }

Default settings for tcpd.

Use this as:

1 hpd_tcpd_settings_t settings = HPD_TCPD_SETTINGS_DEFAULT;

Definition at line 113 of file hpd_tcpd.h.

Typedef Documentation

Definition at line 43 of file hpd_tcpd.h.

typedef hpd_tcpd_return_t(* hpd_tcpd_data_f) (hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ws_ctx, void **conn_ctx, const char *buf, size_t len)

Definition at line 56 of file hpd_tcpd.h.

typedef hpd_tcpd_return_t(* hpd_tcpd_nodata_f) (hpd_tcpd_t *instance, hpd_tcpd_conn_t *conn, void *ws_ctx, void **conn_ctx)

Definition at line 54 of file hpd_tcpd.h.

Definition at line 42 of file hpd_tcpd.h.

typedef struct hpd_tcpd hpd_tcpd_t

Definition at line 41 of file hpd_tcpd.h.

Enumeration Type Documentation

Enumerator
HPD_TCPD_R_CONTINUE 
HPD_TCPD_R_STOP 

Definition at line 45 of file hpd_tcpd.h.

Function Documentation

hpd_error_t hpd_tcpd_conn_close ( hpd_tcpd_conn_t conn)

Close a connection, after the remaining data has been sent.

This sets the close flag on a connection. The connection will be closed after the remaining messages has been sent. If there is no waiting messages the connection will be closed instantly.

Parameters
connThe connection to close

Definition at line 581 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_conn_get_ip ( hpd_tcpd_conn_t conn,
const char **  ip 
)

Get the IP address of the client.

Parameters
connThe connection on which the client is connected.
Returns
The IP address in a string.

Definition at line 459 of file tcpd.c.

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_conn_keep_open ( hpd_tcpd_conn_t conn)

Disable timeout on connection.

Every connection have per default a timeout value, which is set in hpd_tcpd_settings_t. If there is no activity on the connection before the timeout run out the connection is killed. This function disables the timeout, so connections will stay open. A connection will still be killed when the client closes the connection, or kill/close is called.

Parameters
connThe connection to keep open

Definition at line 479 of file tcpd.c.

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_conn_kill ( hpd_tcpd_conn_t conn)

Kill and clean up after a connection.

This function stops the LibEV watchers, closes the socket, and frees the data structures used by a connection.

Note that you should use hpd_tcpd_conn_close for a graceful closure of the connection, where the remaining data is sent.

Parameters
connThe connection to kill.

Definition at line 608 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_conn_sendf ( hpd_tcpd_conn_t conn,
const char *  fmt,
  ... 
)

Send message on connection.

This function is used similary to the standard printf function, with a format string and variable arguments. It calls hpd_tcpd_conn_vsendf() to handle the actually sending, see this for more information.

Connection is kept open for further communication, use hpd_tcpd_conn_close to close it.

Parameters
connConnection to send on
fmtFormat string
Returns
The same as hpd_tcpd_conn_vsendf()

Definition at line 503 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_conn_vsendf ( hpd_tcpd_conn_t conn,
const char *  fmt,
va_list  vp 
)

Send message on connection.

This function is simiar to the standard vprintf function, with a format string and a list of variable arguments.

Note that this function only schedules the message to be send. A send watcher on the event loop will trigger the actual sending, when the connection is ready for it.

Connection is kept open for further communication, use hpd_tcpd_conn_close to close it.

Parameters
connConnection to send on
fmtFormat string
argList of arguments
Returns
zero on success, -1 or the return value of vsprintf on failure

Definition at line 534 of file tcpd.c.

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_create ( hpd_tcpd_t **  tcpd,
hpd_tcpd_settings_t settings,
const hpd_module_t context,
hpd_ev_loop_t loop 
)

Create new tcpd instance.

This creates a new tcpd instances, that can be started with hpd_tcpd_start() and stopped with hpd_tcpd_stop(). The instance should be destroyed with hpd_tcpd_destroy when not longer needed.

Parameters
settingsThe settings for the tcpd.
loopThe event loop to run tcpd on.
Returns
The new tcpd instance.

Definition at line 269 of file tcpd.c.

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_destroy ( hpd_tcpd_t tcpd)

Destroy tcpd and free used memory.

This function destroys and frees all connections are instances. The tcpd should be stopped before destroy to properly close all connections and sockets first.

Parameters
instanceThe tcpd instance to destroy

Definition at line 297 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_start ( hpd_tcpd_t tcpd)

Start the tcpd.

The libev-based tcpd is added to an event loop by a call to this function. It is the caller's resposibility to start the event loop.

To stop the tcpd again, one may call hpd_tcpd_stop().

Parameters
instanceThe tcpd instance to start. Created with hpd_tcpd_create();
Returns
0 on success, 1 on error.

Definition at line 318 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_tcpd_stop ( hpd_tcpd_t tcpd)

Stop an already running tcpd.

The tcpd, started with hpd_tcpd_start(), may be stopped by calling this function. It will take the tcpd off the event loop and clean up after it.

This includes killing all connections without waiting for remaining data to be sent.

Parameters
instanceThe tcpd instance to stop.

Definition at line 428 of file tcpd.c.

Here is the call graph for this function:

Here is the caller graph for this function: