2 * Copyright (c) 1998-2009 by the citadel.org team
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.
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.
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
28 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.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>
53 #include "citserver.h"
56 #include "ctdl_module.h"
58 #include "event_client.h"
59 #include "serv_curl.h"
63 /*****************************************************************************
64 * libevent / curl integration *
65 *****************************************************************************/
68 sta = curl_multi_setopt(mhnd, (CURLMOPT_##s), (v)); \
70 syslog(LOG_ERR, "EVCURL: error setting option " #s " on curl multi handle: %s\n", curl_easy_strerror(sta)); \
75 typedef struct _evcurl_global_data {
82 typedef struct _sockwatcher_data
84 evcurl_global_data *global;
89 evcurl_global_data global;
92 gotstatus(evcurl_global_data *global, int nnrun)
98 if (EVCURL_GLOBAL_MAGIC != global.magic)
100 CtdlLogPrintf(CTDL_ERR, "internal error: gotstatus on wrong struct");
104 global->nrun = nnrun;
107 syslog(LOG_DEBUG, "CURLEV: gotstatus(): about to call curl_multi_info_read\n");
108 while ((msg = curl_multi_info_read(mhnd, &nmsg))) {
109 syslog(LOG_ERR, "EVCURL: got curl multi_info message msg=%d\n", msg->msg);
110 if (CURLMSG_DONE == msg->msg) {
118 chnd = msg->easy_handle;
119 sta = curl_easy_getinfo(chnd, CURLINFO_PRIVATE, &chandle);
120 syslog(LOG_ERR, "EVCURL: request complete\n");
122 syslog(LOG_ERR, "EVCURL: error asking curl for private cookie of curl handle: %s\n", curl_easy_strerror(sta));
123 IO = (AsyncIO *)chandle;
125 sta = msg->data.result;
127 syslog(LOG_ERR, "EVCURL: error description: %s\n", IO->HttpReq.errdesc);
128 syslog(LOG_ERR, "EVCURL: error performing request: %s\n", curl_easy_strerror(sta));
130 sta = curl_easy_getinfo(chnd, CURLINFO_RESPONSE_CODE, &IO->HttpReq.httpcode);
132 syslog(LOG_ERR, "EVCURL: error asking curl for response code from request: %s\n", curl_easy_strerror(sta));
133 syslog(LOG_ERR, "EVCURL: http response code was %ld\n", (long)IO->HttpReq.httpcode);
134 msta = curl_multi_remove_handle(mhnd, chnd);
136 syslog(LOG_ERR, "EVCURL: warning problem detaching completed handle from curl multi: %s\n", curl_multi_strerror(msta));
138 IO->HttpReq.attached = 0;
140 curl_easy_cleanup(IO->HttpReq.chnd);
141 curl_slist_free_all(IO->HttpReq.headers);
142 FreeStrBuf(&IO->HttpReq.ReplyData);
143 FreeURL(&IO->ConnectMe);
144 RemoveContext(IO->CitContext);
151 stepmulti(evcurl_global_data *global, curl_socket_t fd) {
155 if (global == NULL) {
156 syslog(LOG_DEBUG, "EVCURL: stepmulti(NULL): wtf?\n");
159 msta = curl_multi_socket_action(global->mhnd, fd, 0, &nnrun);
160 syslog(LOG_DEBUG, "EVCURL: stepmulti(): calling gotstatus()\n");
162 syslog(LOG_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);
168 gottime(struct ev_loop *loop, ev_timer *timeev, int events) {
169 syslog(LOG_DEBUG, "EVCURL: waking up curl for timeout\n");
170 evcurl_global_data *global = (void *)timeev->data;
171 stepmulti(global, CURL_SOCKET_TIMEOUT);
175 gotio(struct ev_loop *loop, ev_io *ioev, int events) {
176 syslog(LOG_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);
182 gotdata(void *data, size_t size, size_t nmemb, void *cglobal) {
183 AsyncIO *IO = (AsyncIO*) cglobal;
184 //evcurl_request_data *D = (evcurl_request_data*) data;
185 syslog(LOG_DEBUG, "EVCURL: gotdata(): calling CurlFillStrBuf_callback()\n");
187 if (IO->HttpReq.ReplyData == NULL)
189 IO->HttpReq.ReplyData = NewStrBufPlain(NULL, SIZ);
191 return CurlFillStrBuf_callback(data, size, nmemb, IO->HttpReq.ReplyData);
195 gotwatchtime(CURLM *multi, long tblock_ms, void *cglobal) {
196 syslog(LOG_DEBUG, "EVCURL: gotwatchtime called %ld ms\n", tblock_ms);
197 evcurl_global_data *global = cglobal;
198 ev_timer_stop(EV_DEFAULT, &global->timeev);
199 if (tblock_ms < 0 || 14000 < tblock_ms)
201 ev_timer_set(&global->timeev, 0.5e-3 + 1.0e-3 * tblock_ms, 14.0);
202 ev_timer_start(EV_DEFAULT_UC, &global->timeev);
203 curl_multi_perform(global, CURL_POLL_NONE);
208 gotwatchsock(CURL *easy, curl_socket_t fd, int action, void *cglobal, void *csockwatcher) {
209 evcurl_global_data *global = cglobal;
210 CURLM *mhnd = global->mhnd;
211 sockwatcher_data *sockwatcher = csockwatcher;
213 syslog(LOG_DEBUG, "EVCURL: gotwatchsock called fd=%d action=%d\n", (int)fd, action);
216 syslog(LOG_ERR,"EVCURL: called first time to register this sockwatcker\n");
217 sockwatcher = malloc(sizeof(sockwatcher_data));
218 sockwatcher->global = global;
219 ev_init(&sockwatcher->ioev, &gotio);
220 sockwatcher->ioev.data = (void *)sockwatcher;
221 curl_multi_assign(mhnd, fd, sockwatcher);
223 if (CURL_POLL_REMOVE == action) {
224 syslog(LOG_ERR,"EVCURL: called last time to unregister this sockwatcher\n");
225 ev_io_stop(event_base, &sockwatcher->ioev);
228 int events = (action & CURL_POLL_IN ? EV_READ : 0) | (action & CURL_POLL_OUT ? EV_WRITE : 0);
229 ev_io_stop(EV_DEFAULT, &sockwatcher->ioev);
231 ev_io_set(&sockwatcher->ioev, fd, events);
232 ev_io_start(EV_DEFAULT, &sockwatcher->ioev);
238 void curl_init_connectionpool(void)
242 ev_timer_init(&global.timeev, &gottime, 14.0, 14.0);
243 global.timeev.data = (void *)&global;
245 CURLcode sta = curl_global_init(CURL_GLOBAL_ALL);
249 syslog(LOG_ERR,"EVCURL: error initializing curl library: %s\n", curl_easy_strerror(sta));
252 mhnd = global.mhnd = curl_multi_init();
255 syslog(LOG_ERR,"EVCURL: error initializing curl multi handle\n");
259 MOPT(SOCKETFUNCTION, &gotwatchsock);
260 MOPT(SOCKETDATA, (void *)&global);
261 MOPT(TIMERFUNCTION, &gotwatchtime);
262 MOPT(TIMERDATA, (void *)&global);
270 int evcurl_init(AsyncIO *IO,
273 IO_CallBack CallBack,
274 IO_CallBack Terminate)
279 syslog(LOG_DEBUG, "EVCURL: evcurl_init called ms\n");
280 IO->HttpReq.attached = 0;
281 IO->SendDone = CallBack;
282 IO->Terminate = Terminate;
283 chnd = IO->HttpReq.chnd = curl_easy_init();
286 syslog(LOG_ERR, "EVCURL: error initializing curl handle\n");
290 strcpy(IO->HttpReq.errdesc, Desc);
292 OPT(VERBOSE, (long)1);
293 /* unset in production */
294 OPT(NOPROGRESS, (long)1);
295 OPT(NOSIGNAL, (long)1);
296 OPT(FAILONERROR, (long)1);
298 OPT(FOLLOWLOCATION, (long)1);
299 OPT(MAXREDIRS, (long)7);
300 OPT(USERAGENT, CITADEL);
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 *)IO);
309 OPT(WRITEFUNCTION, &gotdata);
310 OPT(WRITEDATA, (void *)IO);
311 OPT(ERRORBUFFER, IO->HttpReq.errdesc);
314 (!IsEmptyStr(config.c_ip_addr))
315 && (strcmp(config.c_ip_addr, "*"))
316 && (strcmp(config.c_ip_addr, "::"))
317 && (strcmp(config.c_ip_addr, "0.0.0.0"))
319 OPT(INTERFACE, config.c_ip_addr);
321 /* point to a structure that points back to the perl structure and stuff */
322 syslog(LOG_DEBUG, "EVCURL: Loading URL: %s\n", IO->ConnectMe->PlainUrl);
323 OPT(URL, IO->ConnectMe->PlainUrl);
324 if (StrLength(IO->ConnectMe->CurlCreds))
326 OPT(HTTPAUTH, (long)CURLAUTH_BASIC);
327 OPT(USERPWD, ChrPtr(IO->ConnectMe->CurlCreds));
329 #ifdef CURLOPT_HTTP_CONTENT_DECODING
330 OPT(HTTP_CONTENT_DECODING, 1);
333 if (StrLength(IO->HttpReq.PostData) > 0)
335 OPT(POSTFIELDS, ChrPtr(IO->HttpReq.PostData));
336 OPT(POSTFIELDSIZE, StrLength(IO->HttpReq.PostData));
339 else if ((IO->HttpReq.PlainPostDataLen != 0) && (IO->HttpReq.PlainPostData != NULL))
341 OPT(POSTFIELDS, IO->HttpReq.PlainPostData);
342 OPT(POSTFIELDSIZE, IO->HttpReq.PlainPostDataLen);
345 if (IO->HttpReq.headers != NULL)
346 OPT(HTTPHEADER, IO->HttpReq.headers);
352 evcurl_handle_start(AsyncIO *IO)
356 syslog(LOG_DEBUG, "EVCURL: attaching to curl multi handle\n");
357 msta = curl_multi_add_handle(global.mhnd, IO->HttpReq.chnd);
359 syslog(LOG_ERR, "EVCURL: error attaching to curl multi handle: %s\n", curl_multi_strerror(msta));
360 IO->HttpReq.attached = 1;
361 ev_async_send (event_base, &WakeupCurl);
364 static void WakeupCurlCallback(EV_P_ ev_async *w, int revents)
366 syslog(LOG_DEBUG, "EVCURL: waking up curl multi handle\n");
368 curl_multi_perform(&global, CURL_POLL_NONE);
371 static void evcurl_shutdown (void)
373 curl_multi_cleanup(global.mhnd);
375 /*****************************************************************************
376 * libevent integration *
377 *****************************************************************************/
379 * client event queue plus its methods.
380 * this currently is the main loop (which may change in some future?)
382 int evbase_count = 0;
383 int event_add_pipe[2] = {-1, -1};
384 pthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
385 HashList *QueueEvents = NULL;
386 HashList *InboundEventQueue = NULL;
387 HashList *InboundEventQueues[2] = { NULL, NULL };
390 ev_async ExitEventLoop;
392 static void QueueEventAddCallback(EV_P_ ev_async *w, int revents)
400 /* get the control command... */
401 pthread_mutex_lock(&EventQueueMutex);
403 if (InboundEventQueues[0] == InboundEventQueue) {
404 InboundEventQueue = InboundEventQueues[1];
405 q = InboundEventQueues[0];
408 InboundEventQueue = InboundEventQueues[0];
409 q = InboundEventQueues[1];
411 pthread_mutex_unlock(&EventQueueMutex);
413 It = GetNewHashPos(q, 0);
414 while (GetNextHashPos(q, It, &len, &Key, &v))
420 DeleteHashContent(&q);
421 syslog(LOG_DEBUG, "EVENT Q Read done.\n");
425 static void EventExitCallback(EV_P_ ev_async *w, int revents)
427 ev_break(event_base, EVBREAK_ALL);
429 syslog(LOG_DEBUG, "EVENT Q exiting.\n");
434 void InitEventQueue(void)
436 struct rlimit LimitSet;
438 pthread_mutex_init(&EventQueueMutex, NULL);
440 if (pipe(event_add_pipe) != 0) {
441 syslog(LOG_EMERG, "Unable to create pipe for libev queueing: %s\n", strerror(errno));
444 LimitSet.rlim_cur = 1;
445 LimitSet.rlim_max = 1;
446 setrlimit(event_add_pipe[1], &LimitSet);
448 QueueEvents = NewHash(1, Flathash);
449 InboundEventQueues[0] = NewHash(1, Flathash);
450 InboundEventQueues[1] = NewHash(1, Flathash);
451 InboundEventQueue = InboundEventQueues[0];
454 * this thread operates the select() etc. via libev.
458 void *client_event_thread(void *arg)
460 struct CitContext libev_client_CC;
462 CtdlFillSystemContext(&libev_client_CC, "LibEv Thread");
463 // citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
464 syslog(LOG_DEBUG, "client_ev_thread() initializing\n");
466 event_base = ev_default_loop (EVFLAG_AUTO);
468 ev_async_init(&AddJob, QueueEventAddCallback);
469 ev_async_start(event_base, &AddJob);
470 ev_async_init(&ExitEventLoop, EventExitCallback);
471 ev_async_start(event_base, &ExitEventLoop);
472 ev_async_init(&WakeupCurl, WakeupCurlCallback);
473 ev_async_start(event_base, &WakeupCurl);
475 curl_init_connectionpool();
477 ev_run (event_base, 0);
480 ///what todo here? CtdlClearSystemContext();
481 ev_loop_destroy (EV_DEFAULT_UC);
483 DeleteHash(&QueueEvents);
484 InboundEventQueue = NULL;
485 DeleteHash(&InboundEventQueues[0]);
486 DeleteHash(&InboundEventQueues[1]);
487 /* citthread_mutex_destroy(&EventQueueMutex); TODO */
492 /*------------------------------------------------------------------------------*/
494 * DB-Queue; does async bdb operations.
495 * has its own set of handlers.
499 int evdb_add_pipe[2] = {-1, -1};
500 pthread_mutex_t DBEventQueueMutex; /* locks the access to the following vars: */
501 HashList *DBQueueEvents = NULL;
502 HashList *DBInboundEventQueue = NULL;
503 HashList *DBInboundEventQueues[2] = { NULL, NULL };
506 ev_async DBExitEventLoop;
508 extern void ShutDownDBCLient(AsyncIO *IO);
510 static void DBQueueEventAddCallback(EV_P_ ev_async *w, int revents)
518 /* get the control command... */
519 pthread_mutex_lock(&DBEventQueueMutex);
521 if (DBInboundEventQueues[0] == DBInboundEventQueue) {
522 DBInboundEventQueue = DBInboundEventQueues[1];
523 q = DBInboundEventQueues[0];
526 DBInboundEventQueue = DBInboundEventQueues[0];
527 q = DBInboundEventQueues[1];
529 pthread_mutex_unlock(&DBEventQueueMutex);
531 It = GetNewHashPos(q, 0);
532 while (GetNextHashPos(q, It, &len, &Key, &v))
536 rc = h->EvAttch(h->IO);
540 ShutDownDBCLient(h->IO);
546 DeleteHashContent(&q);
547 syslog(LOG_DEBUG, "DBEVENT Q Read done.\n");
551 static void DBEventExitCallback(EV_P_ ev_async *w, int revents)
553 syslog(LOG_DEBUG, "EVENT Q exiting.\n");
554 ev_break(event_db, EVBREAK_ALL);
559 void DBInitEventQueue(void)
561 struct rlimit LimitSet;
563 pthread_mutex_init(&DBEventQueueMutex, NULL);
565 if (pipe(evdb_add_pipe) != 0) {
566 syslog(LOG_EMERG, "Unable to create pipe for libev queueing: %s\n", strerror(errno));
569 LimitSet.rlim_cur = 1;
570 LimitSet.rlim_max = 1;
571 setrlimit(evdb_add_pipe[1], &LimitSet);
573 DBQueueEvents = NewHash(1, Flathash);
574 DBInboundEventQueues[0] = NewHash(1, Flathash);
575 DBInboundEventQueues[1] = NewHash(1, Flathash);
576 DBInboundEventQueue = DBInboundEventQueues[0];
580 * this thread operates writing to the message database via libev.
584 void *db_event_thread(void *arg)
586 struct CitContext libev_msg_CC;
588 CtdlFillSystemContext(&libev_msg_CC, "LibEv DB IO Thread");
589 // citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
590 syslog(LOG_DEBUG, "client_msgev_thread() initializing\n");
592 event_db = ev_loop_new (EVFLAG_AUTO);
594 ev_async_init(&DBAddJob, DBQueueEventAddCallback);
595 ev_async_start(event_db, &DBAddJob);
596 ev_async_init(&DBExitEventLoop, DBEventExitCallback);
597 ev_async_start(event_db, &DBExitEventLoop);
599 ev_run (event_db, 0);
602 //// what to do here? CtdlClearSystemContext();
603 ev_loop_destroy (event_db);
605 DeleteHash(&DBQueueEvents);
606 DBInboundEventQueue = NULL;
607 DeleteHash(&DBInboundEventQueues[0]);
608 DeleteHash(&DBInboundEventQueues[1]);
609 /* citthread_mutex_destroy(&DBEventQueueMutex); TODO */
614 CTDL_MODULE_INIT(event_client)
620 CtdlThreadCreate(/*"Client event", */ client_event_thread);
621 CtdlThreadCreate(/*"DB event", */db_event_thread);
622 /// todo register shutdown callback.