libasyncd
ad_server.h File Reference

ad_server header file More...

Go to the source code of this file.

Data Structures

struct  ad_server_s
 Server info container. More...
 
struct  ad_conn_s
 Connection structure. More...
 

Macros

#define AD_OK   (0)
 
#define AD_TAKEOVER   (1)
 
#define AD_DONE   (2)
 
#define AD_CLOSE   (3)
 
#define AD_SERVER_OPTIONS
 Server option names and default values. More...
 
#define AD_EVENT_INIT   (1)
 Event types. More...
 
#define AD_EVENT_READ   (1 << 1)
 
#define AD_EVENT_WRITE   (1 << 2)
 
#define AD_EVENT_CLOSE   (1 << 3)
 
#define AD_EVENT_TIMEOUT   (1 << 4)
 
#define AD_EVENT_SHUTDOWN   (1 << 5)
 
#define AD_NUM_USERDATA   (2)
 Defaults. More...
 

Typedefs

typedef struct ad_server_s ad_server_t
 
typedef struct ad_conn_s ad_conn_t
 
typedef int(* ad_callback )(short event, ad_conn_t *conn, void *userdata)
 User callback(hook) prototype. More...
 
typedef void(* ad_userdata_free_cb )(ad_conn_t *conn, void *userdata)
 

Enumerations

enum  ad_log_e {
  AD_LOG_DISABLE = 0, AD_LOG_ERROR, AD_LOG_WARN, AD_LOG_INFO,
  AD_LOG_DEBUG, AD_LOG_DEBUG2
}
 

Functions

enum ad_log_e ad_log_level (enum ad_log_e log_level)
 Set debug output level. More...
 
ad_server_tad_server_new (void)
 Create a server object. More...
 
int ad_server_start (ad_server_t *server)
 Start server. More...
 
void ad_server_stop (ad_server_t *server)
 Stop server. More...
 
void ad_server_free (ad_server_t *server)
 Release server object and all the resources. More...
 
void ad_server_global_free (void)
 Clean up all the global objects. More...
 
void ad_server_set_option (ad_server_t *server, const char *key, const char *value)
 Set server option. More...
 
char * ad_server_get_option (ad_server_t *server, const char *key)
 Retrieve server option. More...
 
int ad_server_get_option_int (ad_server_t *server, const char *key)
 Retrieve server option in integer format. More...
 
SSL_CTX * ad_server_ssl_ctx_create_simple (const char *cert_path, const char *pkey_path)
 Helper method for creating minimal OpenSSL SSL_CTX object. More...
 
void ad_server_set_ssl_ctx (ad_server_t *server, SSL_CTX *sslctx)
 Attach OpenSSL SSL_CTX to the server. More...
 
SSL_CTX * ad_server_get_ssl_ctx (ad_server_t *server)
 Get OpenSSL SSL_CTX object. More...
 
qhashtbl_t * ad_server_get_stats (ad_server_t *server, const char *key)
 Return internal statistic counter map. More...
 
void ad_server_register_hook (ad_server_t *server, ad_callback cb, void *userdata)
 Register user hook. More...
 
void ad_server_register_hook_on_method (ad_server_t *server, const char *method, ad_callback cb, void *userdata)
 Register user hook on method name. More...
 
void * ad_conn_set_userdata (ad_conn_t *conn, const void *userdata, ad_userdata_free_cb free_cb)
 Attach userdata into the connection. More...
 
void * ad_conn_get_userdata (ad_conn_t *conn)
 Get userdata attached in the connection. More...
 
void * ad_conn_set_extra (ad_conn_t *conn, const void *extra, ad_userdata_free_cb free_cb)
 Set extra userdata into the connection. More...
 
void * ad_conn_get_extra (ad_conn_t *conn)
 Get extra userdata attached in this connection. More...
 
void ad_conn_set_method (ad_conn_t *conn, char *method)
 Set method name on this connection. More...
 
int ad_conn_get_socket (ad_conn_t *conn)
 Return socket file descriptor associated with a connection. More...
 

Detailed Description

ad_server header file

Definition in file ad_server.h.

Macro Definition Documentation

#define AD_OK   (0)

I'm done with this request. Escalate to other hooks.

Definition at line 57 of file ad_server.h.

#define AD_TAKEOVER   (1)

I'll handle the buffer directly this time, skip next hook

Definition at line 58 of file ad_server.h.

#define AD_DONE   (2)

We're done with this request but keep the connection open.

Definition at line 59 of file ad_server.h.

#define AD_CLOSE   (3)

We're done with this request. Close as soon as we sent all data out.

Definition at line 60 of file ad_server.h.

#define AD_SERVER_OPTIONS
Value:
{ \
{ "server.port", "8888" }, \
\
/* Addr format IPv4="1.2.3.4", IPv6="1:2:3:4:5:6", Unix="/path" */ \
{ "server.addr", "0.0.0.0" }, \
\
{ "server.backlog", "128" }, \
\
/* Set read timeout seconds. 0 means no timeout. */ \
{ "server.timeout", "0" }, \
\
/* SSL options */ \
{ "server.enable_ssl", "0" }, \
{ "server.ssl_cert", "/usr/local/etc/ad_server/ad_server.crt" }, \
{ "server.ssl_pkey", "/usr/local/etc/ad_server/ad_server.key" }, \
\
/* Enable or disable request pipelining, this change AD_DONE's behavior */ \
{ "server.request_pipelining", "1" }, \
\
/* Run server in a separate thread */ \
{ "server.thread", "0" }, \
\
/* Collect resources after stop */ \
{ "server.free_on_stop", "1" }, \
\
/* End of array marker. Do not remove */ \
{ "", "_END_" } \
};

Server option names and default values.

Definition at line 81 of file ad_server.h.

#define AD_EVENT_INIT   (1)

Event types.

Call once upon new connection.

Definition at line 123 of file ad_server.h.

#define AD_EVENT_READ   (1 << 1)

Call on read

Definition at line 124 of file ad_server.h.

#define AD_EVENT_WRITE   (1 << 2)

Call on write.

Definition at line 125 of file ad_server.h.

#define AD_EVENT_CLOSE   (1 << 3)

Call before closing.

Definition at line 126 of file ad_server.h.

#define AD_EVENT_TIMEOUT   (1 << 4)

Timeout indicator, this flag will be set with AD_EVENT_CLOSE.

Definition at line 127 of file ad_server.h.

#define AD_EVENT_SHUTDOWN   (1 << 5)

Shutdown indicator, this flag will be set with AD_EVENT_CLOSE.

Definition at line 128 of file ad_server.h.

#define AD_NUM_USERDATA   (2)

Defaults.

Number of userdata. Currently 0 is for userdata, 1 is for extra.

Definition at line 133 of file ad_server.h.

Typedef Documentation

typedef struct ad_server_s ad_server_t

Definition at line 51 of file ad_server.h.

typedef struct ad_conn_s ad_conn_t

Definition at line 52 of file ad_server.h.

typedef int(* ad_callback)(short event, ad_conn_t *conn, void *userdata)

User callback(hook) prototype.

Definition at line 117 of file ad_server.h.

typedef void(* ad_userdata_free_cb)(ad_conn_t *conn, void *userdata)

Definition at line 118 of file ad_server.h.

Enumeration Type Documentation

enum ad_log_e
Enumerator
AD_LOG_DISABLE 
AD_LOG_ERROR 
AD_LOG_WARN 
AD_LOG_INFO 
AD_LOG_DEBUG 
AD_LOG_DEBUG2 

Definition at line 65 of file ad_server.h.

Function Documentation

enum ad_log_e ad_log_level ( enum ad_log_e  log_level)

Set debug output level.

Parameters
debug_leveldebug output level. 0 to disable.
Returns
previous debug level.
Note
debug_level: AD_LOG_DISABLE AD_LOG_ERROR AD_LOG_WARN (default) AD_LOG_INFO AD_LOG_DEBUG AD_LOG_DEBUG2

Definition at line 126 of file ad_server.c.

ad_server_t* ad_server_new ( void  )

Create a server object.

Definition at line 135 of file ad_server.c.

int ad_server_start ( ad_server_t server)

Start server.

Returns
0 if successful, otherwise -1.

Definition at line 164 of file ad_server.c.

void ad_server_stop ( ad_server_t server)

Stop server.

This call is be used to stop a server from different thread.

Returns
0 if successful, otherwise -1.

Definition at line 287 of file ad_server.c.

void ad_server_free ( ad_server_t server)

Release server object and all the resources.

Definition at line 303 of file ad_server.c.

void ad_server_global_free ( void  )

Clean up all the global objects.

This will make memory-leak checkers happy. There are globally shared resources in libevent and openssl and it's usually not a problem since they don't grow but having these can confuse some debugging tools into thinking as memory leak. If you need to make sure that libasyncd has released all internal library-global data structures, call this.

Definition at line 352 of file ad_server.c.

void ad_server_set_option ( ad_server_t server,
const char *  key,
const char *  value 
)

Set server option.

See Also
AD_SERVER_OPTIONS

Definition at line 370 of file ad_server.c.

char* ad_server_get_option ( ad_server_t server,
const char *  key 
)

Retrieve server option.

Definition at line 377 of file ad_server.c.

int ad_server_get_option_int ( ad_server_t server,
const char *  key 
)

Retrieve server option in integer format.

Definition at line 384 of file ad_server.c.

SSL_CTX* ad_server_ssl_ctx_create_simple ( const char *  cert_path,
const char *  pkey_path 
)

Helper method for creating minimal OpenSSL SSL_CTX object.

Parameters
cert_pathpath to a PEM encoded certificate file
pkey_pathpath to a PEM encoded private key file
Returns
newly allocated SSL_CTX object or NULL on failure
Note
This function initializes SSL_CTX with minimum default with "SSLv23_server_method" which will make the server understand SSLv2, SSLv3, and TLSv1 protocol.
See Also
ad_server_set_ssl_ctx()

Definition at line 404 of file ad_server.c.

void ad_server_set_ssl_ctx ( ad_server_t server,
SSL_CTX *  sslctx 
)

Attach OpenSSL SSL_CTX to the server.

Parameters
servera valid server instance
sslctxallocated and configured SSL_CTX object
Note
This function attached SSL_CTX object to the server causing it to communicate over SSL. Use ad_server_ssl_ctx_create_simple() to quickly create a simple SSL_CTX object or make your own with OpenSSL directly. This function must never be called when ad_server is running.
See Also
ad_server_ssl_ctx_create_simple()

Definition at line 434 of file ad_server.c.

SSL_CTX* ad_server_get_ssl_ctx ( ad_server_t server)

Get OpenSSL SSL_CTX object.

Parameters
servera valid server instance
Returns
SSL_CTX object, NULL if not enabled.
Note
As a general rule the returned SSL_CTX object must not be modified while server is running as it may cause unpredictable results. However, it is safe to use it for reading SSL statistics.

Definition at line 454 of file ad_server.c.

qhashtbl_t* ad_server_get_stats ( ad_server_t server,
const char *  key 
)

Return internal statistic counter map.

Definition at line 461 of file ad_server.c.

void ad_server_register_hook ( ad_server_t server,
ad_callback  cb,
void *  userdata 
)

Register user hook.

Definition at line 468 of file ad_server.c.

void ad_server_register_hook_on_method ( ad_server_t server,
const char *  method,
ad_callback  cb,
void *  userdata 
)

Register user hook on method name.

Definition at line 475 of file ad_server.c.

void* ad_conn_set_userdata ( ad_conn_t conn,
const void *  userdata,
ad_userdata_free_cb  free_cb 
)

Attach userdata into the connection.

Returns
previous userdata;

Definition at line 490 of file ad_server.c.

void* ad_conn_get_userdata ( ad_conn_t conn)

Get userdata attached in the connection.

Returns
previous userdata;

Definition at line 499 of file ad_server.c.

void* ad_conn_set_extra ( ad_conn_t conn,
const void *  extra,
ad_userdata_free_cb  free_cb 
)

Set extra userdata into the connection.

Returns
previous userdata;
Note
Extra userdata is for default protocol handler such as ad_http_handler to provide higher abstraction. End users should always use only ad_conn_set_userdata() to avoid any conflict with default handlers.

Definition at line 513 of file ad_server.c.

void* ad_conn_get_extra ( ad_conn_t conn)

Get extra userdata attached in this connection.

Definition at line 520 of file ad_server.c.

void ad_conn_set_method ( ad_conn_t conn,
char *  method 
)

Set method name on this connection.

Once the method name is set, hooks registered by ad_server_register_hook_on_method() will be called if method name matches with the registered name.

See Also
ad_server_register_hook_on_method()

Definition at line 532 of file ad_server.c.

int ad_conn_get_socket ( ad_conn_t conn)

Return socket file descriptor associated with a connection.

Definition at line 543 of file ad_server.c.