[PATCH] Added list.h with the needed definitions to manage double linked list. Added...
[MonkeyD.git] / src / include / plugin.h
blobd0be1bb5895582b9f485465f86279c4026c5e103
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "monkey.h"
23 #include "config.h"
24 #include "request.h"
25 #include "memory.h"
26 #include "iov.h"
27 #include "socket.h"
28 #include "epoll.h"
29 #include "http_status.h"
30 #include "utils.h"
31 #include "list.h"
33 #ifndef MK_PLUGIN_H
34 #define MK_PLUGIN_H
36 #define MK_PLUGIN_LOAD "plugins.load"
38 #define MK_PLUGIN_ERROR -1 /* plugin execution error */
39 #define MK_PLUGIN_
41 /* Plugin: Core types */
42 #define MK_PLUGIN_CORE_PRCTX (1)
43 #define MK_PLUGIN_CORE_THCTX (2)
45 /* Plugin: Stages */
46 #define MK_PLUGIN_STAGE_10 (4) /* Connection just accept()ed */
47 #define MK_PLUGIN_STAGE_20 (8) /* HTTP Request arrived */
48 #define MK_PLUGIN_STAGE_30 (16) /* Object handler */
49 #define MK_PLUGIN_STAGE_40 (32) /* Content served */
50 #define MK_PLUGIN_STAGE_50 (64) /* Conection ended */
52 /* Plugin: Network type */
53 #define MK_PLUGIN_NETWORK_IO (128)
54 #define MK_PLUGIN_NETWORK_IP (256)
56 /* Return values */
57 #define MK_PLUGIN_RET_NOT_ME -1
58 #define MK_PLUGIN_RET_CONTINUE 100
59 #define MK_PLUGIN_RET_END 200
60 #define MK_PLUGIN_RET_CLOSE_CONX 300
62 /* Event return values */
63 #define MK_PLUGIN_RET_EVENT_NOT_ME -300
65 /* NEW PROPOSAL */
66 struct plugin_core
68 int (*prctx) ();
69 int (*thctx) ();
72 struct plugin_stage
74 int (*s10) (int, struct sched_connection *);
75 int (*s20) (struct client_request *, struct request *);
76 int (*s30) (struct plugin *, struct client_request *, struct request *);
77 int (*s40) (struct client_request *, struct request *);
78 int (*s50) (int);
81 struct plugin_network_io
83 int (*accept) (int, struct sockaddr_in);
84 int (*read) (int, void *, int);
85 int (*write) (int, const void *, size_t);
86 int (*writev) (int, struct mk_iov *);
87 int (*close) (int);
88 int (*connect) (int, char *, int);
89 int (*send_file) (int, int, off_t *, size_t);
90 int (*create_socket) (int, int, int);
91 int (*bind) (int, const struct sockaddr *addr, socklen_t, int);
92 int (*server) (int, char *);
95 struct plugin_network_ip
97 int (*addr) (int);
98 int (*maxlen) ();
101 struct plugin
103 char *shortname;
104 char *name;
105 char *version;
106 char *path;
107 void *handler;
108 int *hooks;
110 /* Mandatory calls */
111 int (*init) (void *, char *);
112 int (*exit) ();
114 /* Hook functions by type */
115 struct plugin_core core;
116 struct plugin_stage stage;
117 struct plugin_network_io net_io;
118 struct plugin_network_ip net_ip;
120 /* Epoll Events */
121 int (*event_read) (int);
122 int (*event_write) (int);
123 int (*event_error) (int);
124 int (*event_close) (int);
125 int (*event_timeout) (int);
127 /* Each plugin has a thread key for it's global data */
128 pthread_key_t *thread_key;
130 /* Next! */
131 struct plugin *next;
135 /* Multiple plugins can work on multiple stages, we don't want
136 * Monkey be comparing each plugin looking for a specific stage,
137 * so we create a Map of direct stage calls
139 struct plugin_stagem
141 struct plugin *p;
142 struct plugin_stagem *next;
145 struct plugin_stagemap
147 struct plugin_stagem *stage_10;
148 struct plugin_stagem *stage_20;
149 struct plugin_stagem *stage_30;
150 struct plugin_stagem *stage_40;
151 struct plugin_stagem *stage_50;
154 struct plugin_stagemap *plg_stagemap;
156 /* Network map calls */
157 struct plugin_network_io *plg_netiomap;
158 struct plugin_network_ip *plg_netipmap;
160 /* API functions exported to plugins */
161 struct plugin_api
163 struct server_config *config;
164 struct plugin *plugins;
165 struct sched_list_node **sched_list;
167 /* HTTP request function */
168 int *(*http_request_end) (int);
170 /* memory functions */
171 void *(*mem_alloc) (int);
172 void *(*mem_alloc_z) (int);
173 void (*mem_free) (void *);
174 void (*pointer_set) (mk_pointer *, char *);
175 void (*pointer_print) (mk_pointer);
177 /* string functions */
178 char *(*str_build) (char **, unsigned long *, const char *, ...);
179 char *(*str_dup) (const char *);
180 int (*str_search) (char *, char *);
181 int (*str_search_n) (char *, char *, int);
182 char *(*str_copy_substr) (const char *, int, int);
183 int (*str_itop) (int, mk_pointer *);
184 struct mk_string_line *(*str_split_line) (const char *);
186 /* file functions */
187 char *(*file_to_buffer) (char *);
188 struct file_info *(*file_get_info) (char *);
189 int (*header_send) (int,
190 struct client_request *, struct request *);
191 void (*header_set_http_status) (struct request *, int);
193 /* iov functions */
194 struct mk_iov *(*iov_create) (int, int);
195 void (*iov_free) (struct mk_iov *);
196 int (*iov_add_entry) (struct mk_iov *, char *, int, mk_pointer, int);
197 int (*iov_set_entry) (struct mk_iov *, char *, int, int, int);
198 ssize_t (*iov_send) (int, struct mk_iov *, int);
199 void (*iov_print) (struct mk_iov *);
201 /* plugin functions */
202 void *(*plugin_load_symbol) (void *, char *);
204 /* epoll functions */
205 int (*epoll_create) (int);
206 void *(*epoll_init) (int, mk_epoll_handlers *, int);
207 int (*epoll_add) (int, int, int, int);
208 int (*epoll_del) (int, int);
209 int (*epoll_change_mode) (int, int, int);
211 /* socket functions */
212 int (*socket_cork_flag) (int, int);
213 int (*socket_reset) (int);
214 int (*socket_set_tcp_nodelay) (int);
215 int (*socket_connect) (int, char *, int);
216 int (*socket_set_nonblocking) (int);
217 int (*socket_create) ();
218 int (*socket_close) (int);
219 int (*socket_sendv) (int, struct mk_iov *, int);
220 int (*socket_send) (int, const void *, size_t);
221 int (*socket_read) (int, void *, int);
222 int (*socket_send_file) (int, int, off_t, size_t);
224 /* configuration reader functions */
225 struct mk_config *(*config_create) (char *);
226 void (*config_free) (struct mk_config *);
227 struct mk_config_section *(*config_section_get) (struct mk_config *,
228 char *);
229 void *(*config_section_getval) (struct mk_config_section *, char *, int);
230 struct sched_connection *(*sched_get_connection) (struct sched_list_node *,
231 int);
233 /* worker's functions */
234 int (*worker_spawn) (void (*func) (void *));
236 /* event's functions */
237 int (*event_add) (int, int, struct plugin *, struct client_request *,
238 struct request *);
239 int (*event_del) (int);
241 int (*event_socket_change_mode) (int, int);
243 /* system specific functions */
244 int (*sys_get_somaxconn)();
246 /* Time utils functions */
247 int (*time_unix)();
248 mk_pointer *(*time_human)();
250 #ifdef TRACE
251 void (*trace)();
252 #endif
256 typedef char mk_plugin_data_t[];
257 typedef int mk_plugin_hook_t;
258 typedef pthread_key_t mk_plugin_key_t;
260 /* Plugin events thread key */
261 pthread_key_t mk_plugin_event_k;
263 struct plugin_event {
264 int socket;
266 struct plugin *handler;
267 struct client_request *cr;
268 struct request *sr;
270 struct plugin_event *next;
273 void mk_plugin_init();
274 int mk_plugin_stage_run(mk_plugin_hook_t stage,
275 unsigned int socket,
276 struct sched_connection *conx,
277 struct client_request *cr, struct request *sr);
279 void mk_plugin_core_process();
280 void mk_plugin_core_thread();
282 void mk_plugin_request_handler_add(struct request *sr, struct plugin *p);
283 void mk_plugin_request_handler_del(struct request *sr, struct plugin *p);
285 void mk_plugin_preworker_calls();
287 /* Plugins events interface */
288 int mk_plugin_event_add(int socket, int mode,
289 struct plugin *handler,
290 struct client_request *cr,
291 struct request *sr);
292 int mk_plugin_event_del(int socket);
294 int mk_plugin_event_set_list(struct plugin_event *event);
295 struct plugin_event *mk_plugin_event_get_list();
296 int mk_plugin_event_socket_change_mode(int socket, int mode);
298 /* Plugins event handlers */
299 int mk_plugin_event_read(int socket);
300 int mk_plugin_event_write(int socket);
301 int mk_plugin_event_error(int socket);
302 int mk_plugin_event_close(int socket);
303 int mk_plugin_event_timeout(int socket);
305 void mk_plugin_register_to(struct plugin **st, struct plugin *p);
306 void *mk_plugin_load_symbol(void *handler, const char *symbol);
307 int mk_plugin_http_request_end(int socket);
309 /* Register functions */
310 struct plugin *mk_plugin_register(struct plugin *p);
311 void mk_plugin_unregister(struct plugin *p);
313 struct plugin *mk_plugin_alloc(void *handler, char *path);
314 void mk_plugin_free(struct plugin *p);
316 int mk_plugin_time_now_unix();
317 mk_pointer *mk_plugin_time_now_human();
319 #endif