libasyncd
ad_server.h
Go to the documentation of this file.
1 /******************************************************************************
2  * libasyncd
3  *
4  * Copyright (c) 2014 Seungyoung Kim.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
28 
29 /**
30  * ad_server header file
31  *
32  * @file ad_server.h
33  */
34 
35 #ifndef _AD_SERVER_H
36 #define _AD_SERVER_H
37 
38 #include <event2/event.h>
39 #include <event2/buffer.h>
40 #include <event2/bufferevent.h>
41 #include <openssl/ssl.h>
42 #include "qlibc/qlibc.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /*---------------------------------------------------------------------------*\
49 | TYPEDEFS |
50 \*---------------------------------------------------------------------------*/
51 typedef struct ad_server_s ad_server_t;
52 typedef struct ad_conn_s ad_conn_t;
53 
54 /*
55  * Return values of user callback.
56  */
57 #define AD_OK (0) /*!< I'm done with this request. Escalate to other hooks. */
58 #define AD_TAKEOVER (1) /*!< I'll handle the buffer directly this time, skip next hook */
59 #define AD_DONE (2) /*!< We're done with this request but keep the connection open. */
60 #define AD_CLOSE (3) /*!< We're done with this request. Close as soon as we sent all data out. */
61 
62 /*
63  * These flags are used for ad_log_level();
64  */
65 enum ad_log_e {
72 };
73 
74 /*---------------------------------------------------------------------------*\
75 | SERVER OPTIONS |
76 \*---------------------------------------------------------------------------*/
77 
78 /**
79  * Server option names and default values.
80  */
81 #define AD_SERVER_OPTIONS { \
82  { "server.port", "8888" }, \
83  \
84  /* Addr format IPv4="1.2.3.4", IPv6="1:2:3:4:5:6", Unix="/path" */ \
85  { "server.addr", "0.0.0.0" }, \
86  \
87  { "server.backlog", "128" }, \
88  \
89  /* Set read timeout seconds. 0 means no timeout. */ \
90  { "server.timeout", "0" }, \
91  \
92  /* SSL options */ \
93  { "server.enable_ssl", "0" }, \
94  { "server.ssl_cert", "/usr/local/etc/ad_server/ad_server.crt" }, \
95  { "server.ssl_pkey", "/usr/local/etc/ad_server/ad_server.key" }, \
96  \
97  /* Enable or disable request pipelining, this change AD_DONE's behavior */ \
98  { "server.request_pipelining", "1" }, \
99  \
100  /* Run server in a separate thread */ \
101  { "server.thread", "0" }, \
102  \
103  /* Collect resources after stop */ \
104  { "server.free_on_stop", "1" }, \
105  \
106  /* End of array marker. Do not remove */ \
107  { "", "_END_" } \
108 };
109 
110 /*---------------------------------------------------------------------------*\
111 | USER-CALLBACK |
112 \*---------------------------------------------------------------------------*/
113 
114 /**
115  * User callback(hook) prototype.
116  */
117 typedef int (*ad_callback)(short event, ad_conn_t *conn, void *userdata);
118 typedef void (*ad_userdata_free_cb)(ad_conn_t *conn, void *userdata);
119 
120 /**
121  * Event types
122  */
123 #define AD_EVENT_INIT (1) /*!< Call once upon new connection. */
124 #define AD_EVENT_READ (1 << 1) /*!< Call on read */
125 #define AD_EVENT_WRITE (1 << 2) /*!< Call on write. */
126 #define AD_EVENT_CLOSE (1 << 3) /*!< Call before closing. */
127 #define AD_EVENT_TIMEOUT (1 << 4) /*!< Timeout indicator, this flag will be set with AD_EVENT_CLOSE. */
128 #define AD_EVENT_SHUTDOWN (1 << 5) /*!< Shutdown indicator, this flag will be set with AD_EVENT_CLOSE. */
129 
130 /**
131  * Defaults
132  */
133 #define AD_NUM_USERDATA (2) /*!< Number of userdata. Currently 0 is for userdata, 1 is for extra. */
134 
135 /*---------------------------------------------------------------------------*\
136 | DATA STRUCTURES |
137 \*---------------------------------------------------------------------------*/
138 
139 /**
140  * Server info container.
141  */
142 struct ad_server_s {
143  int errcode; /*!< exit status. 0 for normal exit, non zero for error. */
144  pthread_t *thread; /*!< thread object. not null if server runs as a thread */
145 
146  qhashtbl_t *options; /*!< server options */
147  qhashtbl_t *stats; /*!< internal statistics */
148  qlist_t *hooks; /*!< list of registered hooks */
149  struct evconnlistener *listener; /*!< listener */
150  struct event_base *evbase; /*!< event base */
151  SSL_CTX *sslctx; /*!< SSL connection support */
152 
153  struct bufferevent *notify_buffer; /*!< internal notification channel */
154 };
155 
156 /**
157  * Connection structure.
158  */
159 struct ad_conn_s {
160  ad_server_t *server; /*!< reference pointer to server */
161  struct bufferevent *buffer; /*!< reference pointer to buffer */
162  struct evbuffer *in; /*!< in buffer */
163  struct evbuffer *out; /*!< out buffer */
164  int status; /*!< hook status such as AD_OK */
165 
166  void *userdata[2]; /*!< userdata[0] for end user, userdata[1] for extra */
167  ad_userdata_free_cb userdata_free_cb[2]; /*!< callback to release user data */
168  char *method; /*!< request method. set by protocol handler */
169 };
170 
171 /*----------------------------------------------------------------------------*\
172 | PUBLIC FUNCTIONS |
173 \*----------------------------------------------------------------------------*/
174 enum ad_log_e ad_log_level(enum ad_log_e log_level);
175 
176 extern ad_server_t *ad_server_new(void);
177 extern int ad_server_start(ad_server_t *server);
178 extern void ad_server_stop(ad_server_t *server);
179 extern void ad_server_free(ad_server_t *server);
180 extern void ad_server_global_free(void);
181 
182 extern void ad_server_set_option(ad_server_t *server, const char *key, const char *value);
183 extern char *ad_server_get_option(ad_server_t *server, const char *key);
184 extern int ad_server_get_option_int(ad_server_t *server, const char *key);
185 extern SSL_CTX *ad_server_ssl_ctx_create_simple(const char *cert_path, const char *pkey_path);
186 extern void ad_server_set_ssl_ctx(ad_server_t *server, SSL_CTX *sslctx);
187 extern SSL_CTX *ad_server_get_ssl_ctx(ad_server_t *server);
188 extern qhashtbl_t *ad_server_get_stats(ad_server_t *server, const char *key);
189 
190 extern void ad_server_register_hook(ad_server_t *server, ad_callback cb, void *userdata);
191 extern void ad_server_register_hook_on_method(ad_server_t *server, const char *method,
192  ad_callback cb, void *userdata);
193 
194 extern void *ad_conn_set_userdata(ad_conn_t *conn, const void *userdata, ad_userdata_free_cb free_cb);
195 extern void *ad_conn_get_userdata(ad_conn_t *conn);
196 extern void *ad_conn_set_extra(ad_conn_t *conn, const void *extra, ad_userdata_free_cb free_cb);
197 extern void *ad_conn_get_extra(ad_conn_t *conn);
198 extern void ad_conn_set_method(ad_conn_t *conn, char *method);
199 extern int ad_conn_get_socket(ad_conn_t *conn);
200 
201 /*---------------------------------------------------------------------------*\
202 | INTERNAL USE ONLY |
203 \*---------------------------------------------------------------------------*/
204 #ifndef _DOXYGEN_SKIP
205 #endif /* _DOXYGEN_SKIP */
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 
211 #endif /*_AD_SERVER_H */
void ad_server_set_ssl_ctx(ad_server_t *server, SSL_CTX *sslctx)
Attach OpenSSL SSL_CTX to the server.
Definition: ad_server.c:434
int ad_conn_get_socket(ad_conn_t *conn)
Return socket file descriptor associated with a connection.
Definition: ad_server.c:543
enum ad_log_e ad_log_level(enum ad_log_e log_level)
Set debug output level.
Definition: ad_server.c:126
ad_server_t * ad_server_new(void)
Create a server object.
Definition: ad_server.c:135
struct bufferevent * notify_buffer
Definition: ad_server.h:153
void * ad_conn_get_extra(ad_conn_t *conn)
Get extra userdata attached in this connection.
Definition: ad_server.c:520
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: ad_server.c:475
void * ad_conn_set_userdata(ad_conn_t *conn, const void *userdata, ad_userdata_free_cb free_cb)
Attach userdata into the connection.
Definition: ad_server.c:490
qhashtbl_t * ad_server_get_stats(ad_server_t *server, const char *key)
Return internal statistic counter map.
Definition: ad_server.c:461
int status
Definition: ad_server.h:164
int ad_server_get_option_int(ad_server_t *server, const char *key)
Retrieve server option in integer format.
Definition: ad_server.c:384
qhashtbl_t * stats
Definition: ad_server.h:147
struct bufferevent * buffer
Definition: ad_server.h:161
SSL_CTX * sslctx
Definition: ad_server.h:151
void ad_server_stop(ad_server_t *server)
Stop server.
Definition: ad_server.c:287
struct evbuffer * out
Definition: ad_server.h:163
struct evbuffer * in
Definition: ad_server.h:162
ad_log_e
Definition: ad_server.h:65
ad_server_t * server
Definition: ad_server.h:160
struct evconnlistener * listener
Definition: ad_server.h:149
void ad_server_free(ad_server_t *server)
Release server object and all the resources.
Definition: ad_server.c:303
void * ad_conn_set_extra(ad_conn_t *conn, const void *extra, ad_userdata_free_cb free_cb)
Set extra userdata into the connection.
Definition: ad_server.c:513
char * method
Definition: ad_server.h:168
ad_userdata_free_cb userdata_free_cb[2]
Definition: ad_server.h:167
qhashtbl_t * options
Definition: ad_server.h:146
void ad_server_global_free(void)
Clean up all the global objects.
Definition: ad_server.c:352
void(* ad_userdata_free_cb)(ad_conn_t *conn, void *userdata)
Definition: ad_server.h:118
Connection structure.
Definition: ad_server.h:159
char * ad_server_get_option(ad_server_t *server, const char *key)
Retrieve server option.
Definition: ad_server.c:377
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.
Definition: ad_server.c:404
void ad_server_register_hook(ad_server_t *server, ad_callback cb, void *userdata)
Register user hook.
Definition: ad_server.c:468
void * userdata[2]
Definition: ad_server.h:166
void * ad_conn_get_userdata(ad_conn_t *conn)
Get userdata attached in the connection.
Definition: ad_server.c:499
SSL_CTX * ad_server_get_ssl_ctx(ad_server_t *server)
Get OpenSSL SSL_CTX object.
Definition: ad_server.c:454
void ad_server_set_option(ad_server_t *server, const char *key, const char *value)
Set server option.
Definition: ad_server.c:370
void ad_conn_set_method(ad_conn_t *conn, char *method)
Set method name on this connection.
Definition: ad_server.c:532
int(* ad_callback)(short event, ad_conn_t *conn, void *userdata)
User callback(hook) prototype.
Definition: ad_server.h:117
qlist_t * hooks
Definition: ad_server.h:148
int ad_server_start(ad_server_t *server)
Start server.
Definition: ad_server.c:164
pthread_t * thread
Definition: ad_server.h:144
Server info container.
Definition: ad_server.h:142
struct event_base * evbase
Definition: ad_server.h:150