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 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
60 #include "event_client.h"
61 #include "serv_curl.h"
63 int event_add_pipe[2] = {-1, -1};
65 citthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
66 HashList *QueueEvents = NULL;
68 HashList *InboundEventQueue = NULL;
69 HashList *InboundEventQueues[2] = { NULL, NULL };
71 struct ev_loop *event_base;
74 ev_async ExitEventLoop;
77 extern struct ev_loop *event_base;
79 void SockStateCb(void *data, int sock, int read, int write);
83 sta = curl_multi_setopt(mhnd, (CURLMOPT_##s), (v)); \
85 CtdlLogPrintf(CTDL_ERR, "EVCURL: error setting option " #s " on curl multi handle: %s\n", curl_easy_strerror(sta)); \
92 /*****************************************************************************
93 * libevent / curl integration *
94 *****************************************************************************/
95 typedef struct _evcurl_global_data {
100 } evcurl_global_data;
102 typedef struct _sockwatcher_data
104 evcurl_global_data *global;
108 evcurl_global_data global;
111 gotstatus(evcurl_global_data *global, int nnrun)
117 if (EVCURL_GLOBAL_MAGIC != global.magic)
119 CtdlLogPrintf(CTDL_ERR, "internal error: gotstatus on wrong struct");
123 global->nrun = nnrun;
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);
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;
138 sta = msg->data.result;
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));
144 sta = curl_easy_getinfo(chnd, CURLINFO_RESPONSE_CODE, &httpcode);
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);
150 CtdlLogPrintf(CTDL_ERR, "EVCURL: warning problem detaching completed handle from curl multi: %s\n", curl_multi_strerror(msta));
151 handle->attached = 0;
157 stepmulti(evcurl_global_data *global, curl_socket_t fd) {
159 CURLMcode msta = curl_multi_socket_action(global->mhnd, fd, 0, &nnrun);
160 CtdlLogPrintf(CTDL_DEBUG, "EVCURL: stepmulti(): calling gotstatus()\n");
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);
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);
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);
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);
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)
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);
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;
207 CtdlLogPrintf(CTDL_DEBUG,"EVCURL: gotwatchsock called fd=%d action=%d\n", (int)fd, action);
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);
217 if (CURL_POLL_REMOVE == action) {
218 CtdlLogPrintf(CTDL_ERR,"EVCURL: called last time to unregister this sockwatcher\n");
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);
224 ev_io_set(&sockwatcher->ioev, fd, events);
225 ev_io_start(EV_DEFAULT, &sockwatcher->ioev);
231 void curl_init_connectionpool(void)
234 // global.magic = EVCURL_GLOBAL_MAGIC;
236 ev_timer_init(&global.timeev, &gottime, 14.0, 14.0);
237 global.timeev.data = (void *)&global;
239 CURLcode sta = curl_global_init(CURL_GLOBAL_ALL);
243 CtdlLogPrintf(CTDL_ERR,"EVCURL: error initializing curl library: %s\n", curl_easy_strerror(sta));
247 if (!ev_default_loop(EVFLAG_AUTO))
249 CtdlLogPrintf(CTDL_ERR,"error initializing libev\n");
253 mhnd = global.mhnd = curl_multi_init();
256 CtdlLogPrintf(CTDL_ERR,"EVCURL: error initializing curl multi handle\n");
260 MOPT(SOCKETFUNCTION, &gotwatchsock);
261 MOPT(SOCKETDATA, (void *)&global);
262 MOPT(TIMERFUNCTION, &gotwatchtime);
263 MOPT(TIMERDATA, (void *)&global);
265 /* well, just there to fire the sample request?*/
266 /// ev_timer_start(EV_DEFAULT, &global.timeev);
273 int evcurl_init(evcurl_request_data *handle,
281 CtdlLogPrintf(CTDL_DEBUG,"EVCURL: evcurl_init called ms\n");
282 handle->attached = 0;
283 chnd = handle->chnd = curl_easy_init();
286 CtdlLogPrintf(CTDL_ERR, "EVCURL: error initializing curl handle\n");
290 strcpy(handle->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 *)handle);
309 OPT(WRITEFUNCTION, &gotdata);
310 OPT(WRITEDATA, (void *)handle);
311 OPT(ERRORBUFFER, handle->errdesc);
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))
318 OPT(HTTPAUTH, (long)CURLAUTH_BASIC);
319 OPT(USERPWD, ChrPtr(handle->URL->CurlCreds));
321 #ifdef CURLOPT_HTTP_CONTENT_DECODING
322 OPT(HTTP_CONTENT_DECODING, 1);
325 if (StrLength(handle->PostData) > 0)
327 OPT(POSTFIELDS, ChrPtr(handle->PostData));
328 OPT(POSTFIELDSIZE, StrLength(handle->PostData));
331 else if ((handle->PlainPostDataLen != 0) && (handle->PlainPostData != NULL))
333 OPT(POSTFIELDS, handle->PlainPostData);
334 OPT(POSTFIELDSIZE, handle->PlainPostDataLen);
337 if (handle->headers != NULL)
338 OPT(HTTPHEADER, handle->headers);
344 evcurl_handle_start(evcurl_request_data *handle)
348 CtdlLogPrintf(CTDL_DEBUG, "EVCURL: attaching to curl multi handle\n");
349 msta = curl_multi_add_handle(global.mhnd, handle->chnd);
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);
357 static void WakeupCurlCallback(EV_P_ ev_async *w, int revents)
359 /// evcurl_global_data *global = cglobal;
361 CtdlLogPrintf(CTDL_DEBUG, "EVCURL: waking up curl multi handle\n");
363 curl_multi_perform(&global, CURL_POLL_NONE);
366 /*****************************************************************************
367 * libevent integration *
368 *****************************************************************************/
371 static void QueueEventAddCallback(EV_P_ ev_async *w, int revents)
379 /* get the control command... */
380 citthread_mutex_lock(&EventQueueMutex);
382 if (InboundEventQueues[0] == InboundEventQueue) {
383 InboundEventQueue = InboundEventQueues[1];
384 q = InboundEventQueues[0];
387 InboundEventQueue = InboundEventQueues[0];
388 q = InboundEventQueues[1];
390 citthread_mutex_unlock(&EventQueueMutex);
392 It = GetNewHashPos(q, 0);
393 while (GetNextHashPos(q, It, &len, &Key, &v))
399 DeleteHashContent(&q);
400 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Read done.\n");
404 static void EventExitCallback(EV_P_ ev_async *w, int revents)
406 ev_unloop(event_base, EVUNLOOP_ALL);
408 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q exiting.\n");
413 void InitEventQueue(void)
415 struct rlimit LimitSet;
417 citthread_mutex_init(&EventQueueMutex, NULL);
419 if (pipe(event_add_pipe) != 0) {
420 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for libev queueing: %s\n", strerror(errno));
423 LimitSet.rlim_cur = 1;
424 LimitSet.rlim_max = 1;
425 setrlimit(event_add_pipe[1], &LimitSet);
427 QueueEvents = NewHash(1, Flathash);
428 InboundEventQueues[0] = NewHash(1, Flathash);
429 InboundEventQueues[1] = NewHash(1, Flathash);
430 InboundEventQueue = InboundEventQueues[0];
433 * this thread operates the select() etc. via libev.
437 void *client_event_thread(void *arg)
439 struct CitContext libevent_client_CC;
441 CtdlFillSystemContext(&libevent_client_CC, "LibEv Thread");
442 // citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
443 CtdlLogPrintf(CTDL_DEBUG, "client_ev_thread() initializing\n");
445 event_base = ev_default_loop (EVFLAG_AUTO);
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);
454 curl_init_connectionpool();
456 ev_loop (event_base, 0);
459 CtdlClearSystemContext();
460 ev_default_destroy ();
462 DeleteHash(&QueueEvents);
463 InboundEventQueue = NULL;
464 DeleteHash(&InboundEventQueues[0]);
465 DeleteHash(&InboundEventQueues[1]);
466 citthread_mutex_destroy(&EventQueueMutex);
474 CTDL_MODULE_INIT(event_client)
476 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
480 CtdlThreadCreate("Client event", CTDLTHREAD_BIGSTACK, client_event_thread, NULL);
481 /// todo register shutdown callback.