unlink global struct and request structs
[citadel.git] / citadel / modules / eventclient / serv_eventclient.c
1 /*
2  * Copyright (c) 1998-2009 by the citadel.org team
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "sysdep.h"
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <termios.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <syslog.h>
30
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # if HAVE_SYS_TIME_H
36 #  include <sys/time.h>
37 # else
38 #  include <time.h>
39 # endif
40 #endif
41 #include <sys/wait.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include <limits.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <libcitadel.h>
49 #include <curl/curl.h>
50 #include <curl/multi.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55
56 #include "ctdl_module.h"
57
58 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
59
60 #include "event_client.h"
61 #include "serv_curl.h"
62
63 int event_add_pipe[2] = {-1, -1};
64
65 citthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
66 HashList *QueueEvents = NULL;
67
68 HashList *InboundEventQueue = NULL;
69 HashList *InboundEventQueues[2] = { NULL, NULL };
70
71 struct ev_loop *event_base;
72
73 ev_async AddJob;   
74 ev_async ExitEventLoop;
75 ev_async WakeupCurl;
76
77 extern struct ev_loop *event_base;
78
79 void SockStateCb(void *data, int sock, int read, int write);
80
81 #define MOPT(s, v)                                                      \
82         do {                                                            \
83                 sta = curl_multi_setopt(mhnd, (CURLMOPT_##s), (v));     \
84                 if (sta) {                                              \
85                         CtdlLogPrintf(CTDL_ERR, "EVCURL: error setting option " #s " on curl multi handle: %s\n", curl_easy_strerror(sta)); \
86                         exit (1);                                       \
87                 }                                                       \
88         } while (0)
89
90
91
92 /*****************************************************************************
93  *                   libevent / curl integration                             *
94  *****************************************************************************/
95 typedef struct _evcurl_global_data {
96         int magic;
97         CURLM *mhnd;
98         ev_timer timeev;
99         int nrun;
100 } evcurl_global_data;
101
102 typedef struct _sockwatcher_data 
103 {
104         evcurl_global_data *global;
105         ev_io ioev;
106 } sockwatcher_data;
107
108 evcurl_global_data global;
109
110 static void
111 gotstatus(evcurl_global_data *global, int nnrun) 
112 {
113         CURLM *mhnd;
114         CURLMsg *msg;
115         int nmsg;
116 /*
117   if (EVCURL_GLOBAL_MAGIC != global.magic)
118   {
119   CtdlLogPrintf(CTDL_ERR, "internal error: gotstatus on wrong struct");
120   return;
121   }
122 */
123         global->nrun = nnrun;
124         mhnd = global->mhnd;
125
126         CtdlLogPrintf(CTDL_DEBUG, "CURLEV: gotstatus(): about to call curl_multi_info_read\n");
127         while ((msg = curl_multi_info_read(mhnd, &nmsg))) {
128                 CtdlLogPrintf(CTDL_ERR, "EVCURL: got curl multi_info message msg=%d\n", msg->msg);
129                 if (CURLMSG_DONE == msg->msg) {
130                         CtdlLogPrintf(CTDL_ERR, "EVCURL: request complete\n");
131                         CURL *chnd = msg->easy_handle;
132                         char *chandle = NULL;;
133                         CURLcode sta = curl_easy_getinfo(chnd, CURLINFO_PRIVATE, &chandle);
134                         if (sta)
135                                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error asking curl for private cookie of curl handle: %s\n", curl_easy_strerror(sta));
136                         evcurl_request_data  *handle = (void *)chandle;
137                         
138                         sta = msg->data.result;
139                         if (sta) {
140                                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error description: %s\n", handle->errdesc);
141                                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error performing request: %s\n", curl_easy_strerror(sta));
142                         }
143                         long httpcode;
144                         sta = curl_easy_getinfo(chnd, CURLINFO_RESPONSE_CODE, &httpcode);
145                         if (sta)
146                                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error asking curl for response code from request: %s\n", curl_easy_strerror(sta));
147                         CtdlLogPrintf(CTDL_ERR, "EVCURL: http response code was %ld\n", (long)httpcode);
148                         CURLMcode msta = curl_multi_remove_handle(mhnd, chnd);
149                         if (msta)
150                                 CtdlLogPrintf(CTDL_ERR, "EVCURL: warning problem detaching completed handle from curl multi: %s\n", curl_multi_strerror(msta));
151                         handle->attached = 0;
152                 }
153         }
154 }
155
156 static void
157 stepmulti(evcurl_global_data *global, curl_socket_t fd) {
158         int nnrun;
159         CURLMcode msta = curl_multi_socket_action(global->mhnd, fd, 0, &nnrun);
160         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: stepmulti(): calling gotstatus()\n");
161         if (msta)
162                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error in curl processing events on multi handle, fd %d: %s\n", (int)fd, curl_multi_strerror(msta));
163         if (global->nrun != nnrun)
164                 gotstatus(global, nnrun);
165 }
166
167 static void
168 gottime(struct ev_loop *loop, ev_timer *timeev, int events) {
169         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: waking up curl for timeout\n");
170         evcurl_global_data *global = (void *)timeev->data;
171         stepmulti(global, CURL_SOCKET_TIMEOUT);
172 }
173
174 static void
175 gotio(struct ev_loop *loop, ev_io *ioev, int events) {
176         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: waking up curl for io on fd %d\n", (int)ioev->fd);
177         sockwatcher_data *sockwatcher = (void *)ioev->data;
178         stepmulti(sockwatcher->global, ioev->fd);
179 }
180
181 static size_t
182 gotdata(void *data, size_t size, size_t nmemb, void *cglobal) {
183         evcurl_request_data *D = (evcurl_request_data*) data;
184         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: gotdata(): calling CurlFillStrBuf_callback()\n");
185         return CurlFillStrBuf_callback(D->ReplyData, size, nmemb, cglobal);
186 }
187
188 static int
189 gotwatchtime(CURLM *multi, long tblock_ms, void *cglobal) {
190         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: gotwatchtime called %ld ms\n", tblock_ms);
191         evcurl_global_data *global = cglobal;
192         ev_timer_stop(EV_DEFAULT, &global->timeev);
193         if (tblock_ms < 0 || 14000 < tblock_ms)
194                 tblock_ms = 14000;
195         ev_timer_set(&global->timeev, 0.5e-3 + 1.0e-3 * tblock_ms, 14.0);
196         ev_timer_start(EV_DEFAULT_UC, &global->timeev);
197         curl_multi_perform(global, CURL_POLL_NONE);
198         return 0;
199 }
200
201 static int
202 gotwatchsock(CURL *easy, curl_socket_t fd, int action, void *cglobal, void *csockwatcher) {
203         evcurl_global_data *global = cglobal;
204         CURLM *mhnd = global->mhnd;
205         sockwatcher_data *sockwatcher = csockwatcher;
206
207         CtdlLogPrintf(CTDL_DEBUG,"EVCURL: gotwatchsock called fd=%d action=%d\n", (int)fd, action);
208
209         if (!sockwatcher) {
210                 CtdlLogPrintf(CTDL_ERR,"EVCURL: called first time to register this sockwatcker\n");
211                 sockwatcher = malloc(sizeof(sockwatcher_data));
212                 sockwatcher->global = global;
213                 ev_init(&sockwatcher->ioev, &gotio);
214                 sockwatcher->ioev.data = (void *)sockwatcher;
215                 curl_multi_assign(mhnd, fd, sockwatcher);
216         }
217         if (CURL_POLL_REMOVE == action) {
218                 CtdlLogPrintf(CTDL_ERR,"EVCURL: called last time to unregister this sockwatcher\n");
219                 free(sockwatcher);
220         } else {
221                 int events = (action & CURL_POLL_IN ? EV_READ : 0) | (action & CURL_POLL_OUT ? EV_WRITE : 0);
222                 ev_io_stop(EV_DEFAULT, &sockwatcher->ioev);
223                 if (events) {
224                         ev_io_set(&sockwatcher->ioev, fd, events);
225                         ev_io_start(EV_DEFAULT, &sockwatcher->ioev);
226                 }
227         }
228         return 0;
229 }
230
231 void curl_init_connectionpool(void) 
232 {
233         CURLM *mhnd ;
234 //      global.magic = EVCURL_GLOBAL_MAGIC;
235
236         ev_timer_init(&global.timeev, &gottime, 14.0, 14.0);
237         global.timeev.data = (void *)&global;
238         global.nrun = -1;
239         CURLcode sta = curl_global_init(CURL_GLOBAL_ALL);
240
241         if (sta) 
242         {
243                 CtdlLogPrintf(CTDL_ERR,"EVCURL: error initializing curl library: %s\n", curl_easy_strerror(sta));
244                 exit(1);
245         }
246 /*
247   if (!ev_default_loop(EVFLAG_AUTO))
248   {
249   CtdlLogPrintf(CTDL_ERR,"error initializing libev\n");
250   exit(2);
251   }
252 */
253         mhnd = global.mhnd = curl_multi_init();
254         if (!mhnd)
255         {
256                 CtdlLogPrintf(CTDL_ERR,"EVCURL: error initializing curl multi handle\n");
257                 exit(3);
258         }
259
260         MOPT(SOCKETFUNCTION, &gotwatchsock);
261         MOPT(SOCKETDATA, (void *)&global);
262         MOPT(TIMERFUNCTION, &gotwatchtime);
263         MOPT(TIMERDATA, (void *)&global);
264
265         /* well, just there to fire the sample request?*/
266 ///     ev_timer_start(EV_DEFAULT, &global.timeev);
267         return;
268 }
269
270
271
272
273 int evcurl_init(evcurl_request_data *handle, 
274                 void *CustomData, 
275                 const char* Desc,
276                 int CallBack) 
277 {
278         CURLcode sta;
279         CURL *chnd;
280
281         CtdlLogPrintf(CTDL_DEBUG,"EVCURL: evcurl_init called ms\n");
282         handle->attached = 0;
283         chnd = handle->chnd = curl_easy_init();
284         if (!chnd)
285         {
286                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error initializing curl handle\n");
287                 return 1;
288         }
289
290         strcpy(handle->errdesc, Desc);
291
292         OPT(VERBOSE, (long)1);
293                 /* unset in production */
294         OPT(NOPROGRESS, (long)1); 
295         OPT(NOSIGNAL, (long)1);
296         OPT(FAILONERROR, (long)1);
297         OPT(ENCODING, "");
298         OPT(FOLLOWLOCATION, (long)1);
299         OPT(MAXREDIRS, (long)7);
300         OPT(USERAGENT, CITADEL);
301
302         OPT(TIMEOUT, (long)1800);
303         OPT(LOW_SPEED_LIMIT, (long)64);
304         OPT(LOW_SPEED_TIME, (long)600);
305         OPT(CONNECTTIMEOUT, (long)600); 
306         OPT(PRIVATE, (void *)handle);
307
308
309         OPT(WRITEFUNCTION, &gotdata); 
310         OPT(WRITEDATA, (void *)handle);
311         OPT(ERRORBUFFER, handle->errdesc);
312
313                 /* point to a structure that points back to the perl structure and stuff */
314         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: Loading URL: %s\n", handle->URL->PlainUrl);
315         OPT(URL, handle->URL->PlainUrl);
316         if (StrLength(handle->URL->CurlCreds))
317         {
318                 OPT(HTTPAUTH, (long)CURLAUTH_BASIC);
319                 OPT(USERPWD, ChrPtr(handle->URL->CurlCreds));
320         }
321 #ifdef CURLOPT_HTTP_CONTENT_DECODING
322         OPT(HTTP_CONTENT_DECODING, 1);
323         OPT(ENCODING, "");
324 #endif
325         if (StrLength(handle->PostData) > 0)
326         { 
327                 OPT(POSTFIELDS, ChrPtr(handle->PostData));
328                 OPT(POSTFIELDSIZE, StrLength(handle->PostData));
329
330         }
331         else if ((handle->PlainPostDataLen != 0) && (handle->PlainPostData != NULL))
332         {
333                 OPT(POSTFIELDS, handle->PlainPostData);
334                 OPT(POSTFIELDSIZE, handle->PlainPostDataLen);
335         }
336
337         if (handle->headers != NULL)
338                 OPT(HTTPHEADER, handle->headers);
339
340         return 1;
341 }
342
343 void
344 evcurl_handle_start(evcurl_request_data *handle) 
345 {
346         CURLMcode msta;
347         
348         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: attaching to curl multi handle\n");
349         msta = curl_multi_add_handle(global.mhnd, handle->chnd);
350         if (msta)
351                 CtdlLogPrintf(CTDL_ERR, "EVCURL: error attaching to curl multi handle: %s\n", curl_multi_strerror(msta));
352         handle->attached = 1;
353 //      ev_timer_start(EV_DEFAULT, &global.timeev);
354         ev_async_send (event_base, &WakeupCurl);
355 }
356
357 static void WakeupCurlCallback(EV_P_ ev_async *w, int revents)
358 {
359 ///     evcurl_global_data *global = cglobal;
360
361         CtdlLogPrintf(CTDL_DEBUG, "EVCURL: waking up curl multi handle\n");
362
363         curl_multi_perform(&global, CURL_POLL_NONE);
364 }
365
366 /*****************************************************************************
367  *                       libevent integration                                *
368  *****************************************************************************/
369
370
371 static void QueueEventAddCallback(EV_P_ ev_async *w, int revents)
372 {
373         HashList *q;
374         void *v;
375         HashPos  *It;
376         long len;
377         const char *Key;
378
379         /* get the control command... */
380         citthread_mutex_lock(&EventQueueMutex);
381
382         if (InboundEventQueues[0] == InboundEventQueue) {
383                 InboundEventQueue = InboundEventQueues[1];
384                 q = InboundEventQueues[0];
385         }
386         else {
387                 InboundEventQueue = InboundEventQueues[0];
388                 q = InboundEventQueues[1];
389         }
390         citthread_mutex_unlock(&EventQueueMutex);
391
392         It = GetNewHashPos(q, 0);
393         while (GetNextHashPos(q, It, &len, &Key, &v))
394         {
395                 IOAddHandler *h = v;
396                 h->EvAttch(h->IO);
397         }
398         DeleteHashPos(&It);
399         DeleteHashContent(&q);
400         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Read done.\n");
401 }
402
403
404 static void EventExitCallback(EV_P_ ev_async *w, int revents)
405 {
406         ev_unloop(event_base, EVUNLOOP_ALL);
407
408         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q exiting.\n");
409 }
410
411
412
413 void InitEventQueue(void)
414 {
415         struct rlimit LimitSet;
416
417         citthread_mutex_init(&EventQueueMutex, NULL);
418
419         if (pipe(event_add_pipe) != 0) {
420                 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for libev queueing: %s\n", strerror(errno));
421                 abort();
422         }
423         LimitSet.rlim_cur = 1;
424         LimitSet.rlim_max = 1;
425         setrlimit(event_add_pipe[1], &LimitSet);
426
427         QueueEvents = NewHash(1, Flathash);
428         InboundEventQueues[0] = NewHash(1, Flathash);
429         InboundEventQueues[1] = NewHash(1, Flathash);
430         InboundEventQueue = InboundEventQueues[0];
431 }
432 /*
433  * this thread operates the select() etc. via libev.
434  * 
435  * 
436  */
437 void *client_event_thread(void *arg) 
438 {
439         struct CitContext libevent_client_CC;
440
441         CtdlFillSystemContext(&libevent_client_CC, "LibEv Thread");
442 //      citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
443         CtdlLogPrintf(CTDL_DEBUG, "client_ev_thread() initializing\n");
444
445         event_base = ev_default_loop (EVFLAG_AUTO);
446
447         ev_async_init(&AddJob, QueueEventAddCallback);
448         ev_async_start(event_base, &AddJob);
449         ev_async_init(&ExitEventLoop, EventExitCallback);
450         ev_async_start(event_base, &ExitEventLoop);
451         ev_async_init(&WakeupCurl, WakeupCurlCallback);
452         ev_async_start(event_base, &WakeupCurl);
453
454         curl_init_connectionpool();
455
456         ev_loop (event_base, 0);
457
458
459         CtdlClearSystemContext();
460         ev_default_destroy ();
461         
462         DeleteHash(&QueueEvents);
463         InboundEventQueue = NULL;
464         DeleteHash(&InboundEventQueues[0]);
465         DeleteHash(&InboundEventQueues[1]);
466         citthread_mutex_destroy(&EventQueueMutex);
467
468
469         return(NULL);
470 }
471
472 #endif
473
474 CTDL_MODULE_INIT(event_client)
475 {
476 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
477         if (!threading)
478         {
479                 InitEventQueue();
480                 CtdlThreadCreate("Client event", CTDLTHREAD_BIGSTACK, client_event_thread, NULL);
481 /// todo register shutdown callback.
482         }
483 #endif
484         return "event";
485 }