Fixed binding
[mistral.git] / mistral.c
blobb1f08be1fdd249039e645803651ec655a4620864
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include "php.h"
6 #include "ext/standard/info.h"
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <netdb.h>
13 #include <sys/time.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <err.h>
21 #include <stddef.h>
23 #include <ev.h>
25 #include "mistral.h"
28 * We did use some ideas from:
29 * https://sie.isc.org/svn/nmsg-httpk/trunk/nmsg-httpk.c
30 * http://github.com/ry/libebb/blob/master/ebb.c
33 struct client {
34 int fd;
36 ev_io ev_read;
37 ev_io ev_write;
38 ev_timer ev_timeout;
39 ev_timer ev_goodbye;
41 zval retval;
44 ev_io ev_accept;
45 double timeout = 300;
47 typedef struct _php_event_callback_t { /* {{{ */
48 zval *func;
49 zval *arg;
50 } php_event_callback_t;
51 /* }}} */
53 php_event_callback_t *callback = NULL;
55 static int setnonblock(int fd)
57 int flags;
59 flags = fcntl(fd, F_GETFL);
60 if (flags < 0)
61 return flags;
62 flags |= O_NONBLOCK;
63 if (fcntl(fd, F_SETFL, flags) < 0)
64 return -1;
66 return 0;
69 static inline void _php_event_callback_free(php_event_callback_t *this_callback) /* {{{ */
71 if (!this_callback) {
72 return;
75 zval_ptr_dtor(&this_callback->func);
76 if (this_callback->arg) {
77 zval_ptr_dtor(&this_callback->arg);
79 efree(this_callback);
82 static void timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents) {
83 struct client *cli = (struct client *) w->data;
84 assert(w == &cli->ev_timeout);
86 ev_timer_start(loop, &cli->ev_goodbye);
89 static void goodbye_cb(struct ev_loop *loop, struct ev_timer *w, int revents) {
90 struct client *cli = (struct client *) w->data;
91 assert(w == &cli->ev_goodbye);
93 ev_io_stop(loop, &cli->ev_read);
94 ev_io_stop(loop, &cli->ev_write);
95 ev_timer_stop(loop, &cli->ev_timeout);
97 close(cli->fd);
100 static void write_cb(struct ev_loop *loop, struct ev_io *w, int revents)
102 struct client *cli = (struct client *) w->data;
104 if(EV_ERROR & revents) {
105 ev_timer_start(loop, &cli->ev_goodbye);
106 return;
109 if (revents & EV_WRITE) {
110 if (Z_TYPE_P(&cli->retval) == IS_STRING) {
111 char *test = (char *) malloc((256 + Z_STRLEN_P(&cli->retval)) * sizeof(char));
112 int i = snprintf(test, 1024, "HTTP/1.1 200 OK\r\nConnection: %s\r\nContent-Type: text/plain\r\nServer: mistral/0.1\r\nContent-Length: %u\r\n\r\n%s",
113 (timeout == 0.0 ? "close" : "keep-alive"), Z_STRLEN_P(&cli->retval), Z_STRVAL_P(&cli->retval));
114 write(cli->fd, test, i);
116 free(test);
117 ev_io_stop(EV_A_ w);
121 zval_dtor(&cli->retval);
123 if (timeout == 0.0) {
124 ev_timer_start(loop, &cli->ev_goodbye);
125 } else {
126 ev_timer_again(loop, &cli->ev_timeout);
127 ev_io_start(loop, &cli->ev_read);
131 static void read_cb(struct ev_loop *loop, struct ev_io *w, int revents)
133 if ((revents & EV_READ) && callback) {
134 char *rbuff = emalloc(1024);
135 if (rbuff) {
136 struct client *cli = (struct client *) w->data;
137 int r = 0;
138 r = read(cli->fd,rbuff,1023);
140 if (r > 0) {
141 zval *http_headers[1];
142 ev_io_stop(EV_A_ w);
143 rbuff[r] = '\0';
145 MAKE_STD_ZVAL(http_headers[0]);
146 ZVAL_STRINGL (http_headers[0], rbuff, r, 0);
148 if (call_user_function(EG(function_table), NULL, callback->func, &cli->retval, 1, http_headers TSRMLS_CC) == SUCCESS) {
149 ev_io_start(loop,&cli->ev_write);
150 zval_dtor(http_headers[0]); /* Free's the string */
151 FREE_ZVAL(http_headers[0]); /* Free's the object itself */
152 return; /* NO PROCESSING ANYMORE!! */
153 } else {
154 zval_dtor(http_headers[0]);
155 FREE_ZVAL(http_headers[0]);
156 zval_dtor(&cli->retval);
159 efree(rbuff);
163 struct client *cli = (struct client *) w->data;
164 ev_io_stop(EV_A_ w);
165 ev_timer_start(loop, &cli->ev_goodbye);
168 static void accept_cb(struct ev_loop *loop, struct ev_io *w, int revents)
170 int client_fd;
171 struct client *cli;
172 struct sockaddr_in client_addr;
173 socklen_t client_len = sizeof(client_addr);
174 client_fd = accept(w->fd, (struct sockaddr *)&client_addr, &client_len);
175 if (client_fd == -1)
176 return;
178 cli = calloc(1,sizeof(struct client));
179 cli->fd = client_fd;
180 if (setnonblock(cli->fd) < 0)
181 err(1, "failed to set client socket to non-blocking");
183 ev_io_init(&cli->ev_read, read_cb, cli->fd, EV_READ);
184 cli->ev_read.data = cli;
186 ev_io_init(&cli->ev_write, write_cb, cli->fd, EV_WRITE);
187 cli->ev_write.data = cli;
189 ev_timer_init(&cli->ev_goodbye, goodbye_cb, 0., 0.);
190 cli->ev_goodbye.data = cli;
192 ev_timer_init(&cli->ev_timeout, timeout_cb, 0., (timeout == 0.0 ? 30.0 : timeout));
193 cli->ev_timeout.data = cli;
195 ev_io_start(loop,&cli->ev_read);
198 /* {{{ mistral_functions[] */
199 zend_function_entry mistral_functions[] = {
200 PHP_FE(mistral_init, NULL)
201 PHP_FE(mistral_register_callback, NULL)
202 PHP_FE(mistral_start, NULL)
203 { NULL, NULL, NULL }
205 /* }}} */
207 /* {{{ mistral_module_entry
208 * */
209 zend_module_entry mistral_module_entry = {
210 STANDARD_MODULE_HEADER,
211 "mistral",
212 mistral_functions,
213 NULL, /* Replace with NULL if there is nothing to do at php startup */
214 PHP_MSHUTDOWN(mistral),
215 NULL, /* Replace with NULL if there is nothing to do at request start */
216 NULL, /* Replace with NULL if there is nothing to do at request end */
217 PHP_MINFO(mistral),
218 PHP_MISTRAL_VERSION,
219 STANDARD_MODULE_PROPERTIES
221 /* }}} */
223 #ifdef COMPILE_DL_MISTRAL
224 ZEND_GET_MODULE(mistral)
225 #endif
227 /* {{{ PHP_MINFO_FUNCTION */
228 PHP_MINFO_FUNCTION(mistral)
230 php_info_print_table_start();
231 php_info_print_table_header(2, "Mistral support", "enabled");
232 php_info_print_table_row(2, "Version", PHP_MISTRAL_VERSION);
233 php_info_print_table_end();
235 /* }}} */
237 /* {{{ PHP_MSHUTDOWN_FUNCTION */
238 PHP_MSHUTDOWN_FUNCTION (mistral)
240 _php_event_callback_free(callback);
242 /* }}} */
244 PHP_FUNCTION(mistral_init)
246 struct addrinfo *ai = NULL;
247 struct addrinfo hints;
248 struct addrinfo *runp;
249 int listen_sock = -1;
251 char *listen_addr;
252 int listen_addr_len;
253 long listen_port;
255 struct sockaddr_storage sin;
256 struct sockaddr_in * sa4 = (struct sockaddr_in *) &sin;
257 struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *) &sin;
259 int reuseaddr_on = 1;
260 int keepalive_on = 1;
262 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sld", &listen_addr, &listen_addr_len, &listen_port, &timeout) == FAILURE) {
263 RETURN_NULL();
266 if (listen_port < 1 || listen_port > 65535) {
267 RETURN_NULL();
270 bzero(&hints, sizeof (hints));
271 hints.ai_flags = AI_ADDRCONFIG || AI_NUMERICSERV;
272 hints.ai_socktype = SOCK_STREAM;
274 if (getaddrinfo (listen_addr, "", &hints, &ai) != 0) {
275 RETURN_NULL();
278 runp = ai;
280 while (runp != NULL && listen_sock < 0) {
281 listen_sock = socket(runp->ai_family, SOCK_STREAM, 0);
282 if (listen_sock < 0)
283 runp = runp->ai_next;
286 if (listen_sock < 0) {
287 err(1, "listen failed");
290 if (timeout <= 0.0) {
291 timeout = 0.0;
292 } else {
293 if (setsockopt(listen_sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive_on, sizeof(keepalive_on)) == -1)
294 err(1, "setsockopt failed");
297 if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1)
298 err(1, "setsockopt failed");
300 if (runp->ai_family == AF_INET6) {
301 int null = 0;
302 setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &null, sizeof(null));
305 memset(&sin, 0, sizeof(sin));
307 sin.ss_family = runp->ai_family;
308 switch (sin.ss_family) {
309 case AF_INET6:
310 inet_pton(sin.ss_family, listen_addr, &(sa6->sin6_addr));
311 sa6->sin6_port = htons(listen_port);
312 break;
313 case AF_INET:
314 if ( strchr(listen_addr, ':') )
315 /* Invalid IPv4-string. Use the wildcard address. */
316 listen_addr = strdup("0.0.0.0");
318 inet_pton(sin.ss_family, listen_addr, &(sa4->sin_addr));
319 sa4->sin_port = htons(listen_port);
322 if (ai)
323 freeaddrinfo (ai);
325 if (bind(listen_sock, (struct sockaddr *) &sin, sizeof(sin)) < 0)
326 err(1, "bind failed");
328 if (listen(listen_sock, 5) < 0)
329 err(1, "listen failed");
331 if (setnonblock(listen_sock) < 0)
332 err(1, "failed to set server socket to non-blocking");
334 ev_io_init(&ev_accept, accept_cb, listen_sock, EV_READ);
337 /* {{{ proto bool event_set(resource event, resource fd, int events, mixed callback[, mixed arg])
338 * Taken from pecl::libevent event_set
340 PHP_FUNCTION(mistral_register_callback)
342 zval *zcallback, *zarg = NULL;
343 php_event_callback_t *new_callback, *old_callback;
344 char *func_name;
346 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcallback) != SUCCESS) {
347 return;
350 if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) {
351 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name);
352 efree(func_name);
353 RETURN_FALSE;
355 efree(func_name);
357 zval_add_ref(&zcallback);
359 new_callback = emalloc(sizeof(php_event_callback_t));
360 new_callback->func = zcallback;
361 new_callback->arg = zarg;
363 old_callback = callback;
364 callback = new_callback;
366 if (old_callback) {
367 _php_event_callback_free(old_callback);
369 RETURN_TRUE;
371 /* }}} */
373 PHP_FUNCTION(mistral_start)
375 struct ev_loop *loop = ev_default_loop (0);
377 ev_io_start(loop, &ev_accept);
378 ev_loop(loop, 0);