+ if (IO->SendBuf.fd != 0)
+ {
+ ev_io_stop(event_base, &IO->recv_event);
+ ev_io_stop(event_base, &IO->send_event);
+ }
+ IO->SendBuf.fd = fd;
+ ev_io_init(&IO->recv_event, &got_in, fd, EV_READ);
+ ev_io_init(&IO->send_event, &got_out, fd, EV_WRITE);
+ curl_multi_assign(mhnd, fd, IO);
+ }
+
+ IO->Now = ev_now(event_base);
+
+ Action = "";
+ switch (action)
+ {
+ case CURL_POLL_NONE:
+ Action = "CURL_POLL_NONE";
+ break;
+ case CURL_POLL_REMOVE:
+ Action = "CURL_POLL_REMOVE";
+ break;
+ case CURL_POLL_IN:
+ Action = "CURL_POLL_IN";
+ break;
+ case CURL_POLL_OUT:
+ Action = "CURL_POLL_OUT";
+ break;
+ case CURL_POLL_INOUT:
+ Action = "CURL_POLL_INOUT";
+ break;
+ }
+
+
+ EVCURL_syslog(LOG_DEBUG,
+ "EVCURL: gotwatchsock called fd=%d action=%s[%d]\n",
+ (int)fd, Action, action);
+
+ switch (action)
+ {
+ case CURL_POLL_NONE:
+ EVCURLM_syslog(LOG_ERR,
+ "called first time "
+ "to register this sockwatcker\n");
+ break;
+ case CURL_POLL_REMOVE:
+ EVCURLM_syslog(LOG_ERR,
+ "called last time to unregister "
+ "this sockwatcher\n");
+ ev_io_stop(event_base, &IO->recv_event);
+ ev_io_stop(event_base, &IO->send_event);
+ break;
+ case CURL_POLL_IN:
+ ev_io_start(event_base, &IO->recv_event);
+ ev_io_stop(event_base, &IO->send_event);
+ break;
+ case CURL_POLL_OUT:
+ ev_io_start(event_base, &IO->send_event);
+ ev_io_stop(event_base, &IO->recv_event);
+ break;
+ case CURL_POLL_INOUT:
+ ev_io_start(event_base, &IO->send_event);
+ ev_io_start(event_base, &IO->recv_event);
+ break;
+ }
+ return 0;
+}
+
+void curl_init_connectionpool(void)
+{
+ CURLM *mhnd ;
+
+ ev_timer_init(&global.timeev, &gottime, 14.0, 14.0);
+ global.timeev.data = (void *)&global;
+ global.nrun = -1;
+ CURLcode sta = curl_global_init(CURL_GLOBAL_ALL);
+
+ if (sta)
+ {
+ CURL_syslog(LOG_ERR,
+ "error initializing curl library: %s\n",
+ curl_easy_strerror(sta));
+
+ exit(1);
+ }
+ mhnd = global.mhnd = curl_multi_init();
+ if (!mhnd)
+ {
+ CURLM_syslog(LOG_ERR,
+ "error initializing curl multi handle\n");
+ exit(3);
+ }
+
+ MOPT(SOCKETFUNCTION, &gotwatchsock);
+ MOPT(SOCKETDATA, (void *)&global);
+ MOPT(TIMERFUNCTION, &gotwatchtime);
+ MOPT(TIMERDATA, (void *)&global);
+
+ return;
+}
+
+int evcurl_init(AsyncIO *IO)
+{
+ CURLcode sta;
+ CURL *chnd;
+
+ EVCURLM_syslog(LOG_DEBUG, "EVCURL: evcurl_init called ms\n");
+ IO->HttpReq.attached = 0;
+ chnd = IO->HttpReq.chnd = curl_easy_init();
+ if (!chnd)
+ {
+ EVCURLM_syslog(LOG_ERR, "EVCURL: error initializing curl handle\n");
+ return 0;
+ }
+
+#if DEBUG
+ OPT(VERBOSE, (long)1);
+#endif
+ OPT(NOPROGRESS, 1L);
+
+ OPT(NOSIGNAL, 1L);
+ OPT(FAILONERROR, (long)1);
+ OPT(ENCODING, "");
+ OPT(FOLLOWLOCATION, (long)0);
+ OPT(MAXREDIRS, (long)0);
+ OPT(USERAGENT, CITADEL);
+
+ OPT(TIMEOUT, (long)1800);
+ OPT(LOW_SPEED_LIMIT, (long)64);
+ OPT(LOW_SPEED_TIME, (long)600);
+ OPT(CONNECTTIMEOUT, (long)600);
+ OPT(PRIVATE, (void *)IO);
+
+ OPT(FORBID_REUSE, 1);
+ OPT(WRITEFUNCTION, &gotdata);
+ OPT(WRITEDATA, (void *)IO);
+ OPT(ERRORBUFFER, IO->HttpReq.errdesc);
+
+ if ((!IsEmptyStr(config.c_ip_addr))
+ && (strcmp(config.c_ip_addr, "*"))
+ && (strcmp(config.c_ip_addr, "::"))
+ && (strcmp(config.c_ip_addr, "0.0.0.0"))
+ )
+ {
+ OPT(INTERFACE, config.c_ip_addr);
+ }
+
+#ifdef CURLOPT_HTTP_CONTENT_DECODING
+ OPT(HTTP_CONTENT_DECODING, 1);
+ OPT(ENCODING, "");
+#endif
+
+ IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers,
+ "Connection: close");
+
+ return 1;
+}
+
+
+static void IOcurl_abort_shutdown_callback(struct ev_loop *loop,
+ ev_cleanup *watcher,
+ int revents)
+{
+ CURLMcode msta;
+ AsyncIO *IO = watcher->data;
+ IO->Now = ev_now(event_base);
+ EVCURL_syslog(LOG_DEBUG, "EVENT Curl: %s\n", __FUNCTION__);
+
+ curl_slist_free_all(IO->HttpReq.headers);
+ msta = curl_multi_remove_handle(global.mhnd, IO->HttpReq.chnd);
+ if (msta)
+ {
+ EVCURL_syslog(LOG_ERR,
+ "EVCURL: warning problem detaching completed handle "
+ "from curl multi: %s\n",
+ curl_multi_strerror(msta));
+ }
+
+ curl_easy_cleanup(IO->HttpReq.chnd);
+ IO->HttpReq.chnd = NULL;
+ ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
+ ev_io_stop(event_base, &IO->recv_event);
+ ev_io_stop(event_base, &IO->send_event);
+ assert(IO->ShutdownAbort);
+ IO->ShutdownAbort(IO);
+}
+eNextState
+evcurl_handle_start(AsyncIO *IO)
+{
+ CURLMcode msta;
+ CURLcode sta;
+ CURL *chnd;
+
+ chnd = IO->HttpReq.chnd;
+ EVCURL_syslog(LOG_DEBUG,
+ "EVCURL: Loading URL: %s\n", IO->ConnectMe->PlainUrl);
+ OPT(URL, IO->ConnectMe->PlainUrl);
+ if (StrLength(IO->ConnectMe->CurlCreds))
+ {
+ OPT(HTTPAUTH, (long)CURLAUTH_BASIC);
+ OPT(USERPWD, ChrPtr(IO->ConnectMe->CurlCreds));
+ }
+ if (StrLength(IO->HttpReq.PostData) > 0)
+ {
+ OPT(POSTFIELDS, ChrPtr(IO->HttpReq.PostData));
+ OPT(POSTFIELDSIZE, StrLength(IO->HttpReq.PostData));
+
+ }
+ else if ((IO->HttpReq.PlainPostDataLen != 0) &&
+ (IO->HttpReq.PlainPostData != NULL))
+ {
+ OPT(POSTFIELDS, IO->HttpReq.PlainPostData);
+ OPT(POSTFIELDSIZE, IO->HttpReq.PlainPostDataLen);
+ }
+ OPT(HTTPHEADER, IO->HttpReq.headers);
+
+ IO->NextState = eConnect;
+ EVCURLM_syslog(LOG_DEBUG, "EVCURL: attaching to curl multi handle\n");
+ msta = curl_multi_add_handle(global.mhnd, IO->HttpReq.chnd);
+ if (msta)
+ {
+ EVCURL_syslog(LOG_ERR,
+ "EVCURL: error attaching to curl multi handle: %s\n",
+ curl_multi_strerror(msta));
+ }
+
+ IO->HttpReq.attached = 1;
+ ev_async_send (event_base, &WakeupCurl);
+ ev_cleanup_init(&IO->abort_by_shutdown,
+ IOcurl_abort_shutdown_callback);
+
+ ev_cleanup_start(event_base, &IO->abort_by_shutdown);
+ return eReadMessage;
+}
+
+static void WakeupCurlCallback(EV_P_ ev_async *w, int revents)
+{
+ CURLM_syslog(LOG_DEBUG, "waking up curl multi handle\n");
+
+ curl_multi_perform(&global, CURL_POLL_NONE);
+}
+
+static void evcurl_shutdown (void)
+{
+ curl_global_cleanup();
+ curl_multi_cleanup(global.mhnd);
+ CURLM_syslog(LOG_DEBUG, "exiting\n");
+}
+/*****************************************************************************
+ * libevent integration *
+ *****************************************************************************/
+/*
+ * client event queue plus its methods.
+ * this currently is the main loop (which may change in some future?)
+ */
+int evbase_count = 0;
+int event_add_pipe[2] = {-1, -1};
+pthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
+HashList *QueueEvents = NULL;