2 * Copyright (c) 1998-2012 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 #include <libcitadel.h>
27 #include "ctdl_module.h"
28 #include "event_client.h"
30 ConstStr IOStates[] = {
37 {HKEY("DB Terminate")},
40 {HKEY("IO Connect Socket")},
43 {HKEY("IO ConnFail")},
44 {HKEY("IO ConnFail Now")},
45 {HKEY("IO Conn Now")},
46 {HKEY("IO Conn Wait")},
49 {HKEY("Curl Shotdown")},
50 {HKEY("Curl More IO")},
51 {HKEY("Curl Got IO")},
52 {HKEY("Curl Got Data")},
53 {HKEY("Curl Got Status")},
54 {HKEY("C-Ares Start")},
55 {HKEY("C-Ares IO Done")},
56 {HKEY("C-Ares Finished")},
57 {HKEY("C-Ares exit")},
62 void SetEVState(AsyncIO *IO, eIOState State)
65 CitContext* CCC = IO->CitContext;
67 memcpy(CCC->lastcmdname, IOStates[State].Key, IOStates[State].len + 1);
72 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
73 static void IO_abort_shutdown_callback(struct ev_loop *loop,
78 /*------------------------------------------------------------------------------
80 *----------------------------------------------------------------------------*/
81 extern int evdb_count;
82 extern pthread_mutex_t DBEventQueueMutex;
83 extern pthread_mutex_t DBEventExitQueueMutex;
84 extern HashList *DBInboundEventQueue;
85 extern struct ev_loop *event_db;
86 extern ev_async DBAddJob;
87 extern ev_async DBExitEventLoop;
89 eNextState QueueAnDBOperation(AsyncIO *IO)
95 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
98 assert(IO->ReAttachCB != NULL);
100 h->EvAttch = IO->ReAttachCB;
101 ev_cleanup_init(&IO->db_abort_by_shutdown,
102 IO_abort_shutdown_callback);
103 IO->db_abort_by_shutdown.data = IO;
105 pthread_mutex_lock(&DBEventQueueMutex);
106 if (DBInboundEventQueue == NULL)
108 /* shutting down... */
110 EVM_syslog(LOG_DEBUG, "DBEVENT Q exiting.\n");
111 pthread_mutex_unlock(&DBEventQueueMutex);
114 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
116 Put(DBInboundEventQueue, IKEY(i), h, NULL);
117 pthread_mutex_unlock(&DBEventQueueMutex);
119 pthread_mutex_lock(&DBEventExitQueueMutex);
120 if (event_db == NULL)
122 pthread_mutex_unlock(&DBEventExitQueueMutex);
125 ev_async_send (event_db, &DBAddJob);
126 pthread_mutex_unlock(&DBEventExitQueueMutex);
128 EVQM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
132 void StopDBWatchers(AsyncIO *IO)
134 SetEVState(IO, eDBStop);
135 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
136 ev_idle_stop(event_db, &IO->db_unwind_stack);
139 void ShutDownDBCLient(AsyncIO *IO)
141 CitContext *Ctx =IO->CitContext;
144 SetEVState(IO, eDBTerm);
145 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
148 assert(IO->DBTerminate);
153 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
155 AsyncIO *IO = watcher->data;
157 SetEVState(IO, eDBNext);
158 IO->Now = ev_now(event_db);
159 EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
160 become_session(IO->CitContext);
162 ev_idle_stop(event_db, &IO->db_unwind_stack);
164 assert(IO->NextDBOperation);
165 switch (IO->NextDBOperation(IO))
179 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
181 case eTerminateConnection:
183 ev_idle_stop(event_db, &IO->db_unwind_stack);
184 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
185 ShutDownDBCLient(IO);
189 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
191 SetEVState(IO, eQDBNext);
192 IO->NextDBOperation = CB;
193 ev_idle_init(&IO->db_unwind_stack,
195 IO->db_unwind_stack.data = IO;
196 ev_idle_start(event_db, &IO->db_unwind_stack);
200 /*------------------------------------------------------------------------------
202 *----------------------------------------------------------------------------*/
203 extern int evbase_count;
204 extern pthread_mutex_t EventQueueMutex;
205 extern pthread_mutex_t EventExitQueueMutex;
206 extern HashList *InboundEventQueue;
207 extern struct ev_loop *event_base;
208 extern ev_async AddJob;
209 extern ev_async ExitEventLoop;
211 static void IO_abort_shutdown_callback(struct ev_loop *loop,
215 AsyncIO *IO = watcher->data;
217 SetEVState(IO, eIOAbort);
218 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
219 IO->Now = ev_now(event_base);
220 assert(IO->ShutdownAbort);
221 IO->ShutdownAbort(IO);
225 eNextState QueueAnEventContext(AsyncIO *IO)
230 SetEVState(IO, eIOQ);
231 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
234 assert(IO->ReAttachCB != NULL);
236 h->EvAttch = IO->ReAttachCB;
238 ev_cleanup_init(&IO->abort_by_shutdown,
239 IO_abort_shutdown_callback);
240 IO->abort_by_shutdown.data = IO;
242 pthread_mutex_lock(&EventQueueMutex);
243 if (InboundEventQueue == NULL)
246 /* shutting down... */
247 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
248 pthread_mutex_unlock(&EventQueueMutex);
251 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
253 Put(InboundEventQueue, IKEY(i), h, NULL);
254 pthread_mutex_unlock(&EventQueueMutex);
256 pthread_mutex_lock(&EventExitQueueMutex);
257 if (event_base == NULL) {
258 pthread_mutex_unlock(&EventExitQueueMutex);
261 ev_async_send (event_base, &AddJob);
262 pthread_mutex_unlock(&EventExitQueueMutex);
263 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
267 eNextState EventQueueDBOperation(AsyncIO *IO, IO_CallBack CB, int CloseFDs)
269 StopClientWatchers(IO, CloseFDs);
273 eNextState DBQueueEventContext(AsyncIO *IO, IO_CallBack CB)
280 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
283 return QueueAnEventContext(IO);
286 extern eNextState evcurl_handle_start(AsyncIO *IO);
288 eNextState QueueCurlContext(AsyncIO *IO)
293 SetEVState(IO, eCurlQ);
294 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
296 h->EvAttch = evcurl_handle_start;
298 pthread_mutex_lock(&EventQueueMutex);
299 if (InboundEventQueue == NULL)
301 /* shutting down... */
303 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
304 pthread_mutex_unlock(&EventQueueMutex);
308 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
310 Put(InboundEventQueue, IKEY(i), h, NULL);
311 pthread_mutex_unlock(&EventQueueMutex);
313 pthread_mutex_lock(&EventExitQueueMutex);
314 if (event_base == NULL) {
315 pthread_mutex_unlock(&EventExitQueueMutex);
318 ev_async_send (event_base, &AddJob);
319 pthread_mutex_unlock(&EventExitQueueMutex);
321 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
325 eNextState CurlQueueDBOperation(AsyncIO *IO, IO_CallBack CB)
327 StopCurlWatchers(IO);
333 void FreeAsyncIOContents(AsyncIO *IO)
335 CitContext *Ctx = IO->CitContext;
337 FreeStrBuf(&IO->IOBuf);
338 FreeStrBuf(&IO->SendBuf.Buf);
339 FreeStrBuf(&IO->RecvBuf.Buf);
341 FreeURL(&IO->ConnectMe);
342 FreeStrBuf(&IO->HttpReq.ReplyData);
345 Ctx->state = CON_IDLE;
347 IO->CitContext = NULL;
352 void DestructCAres(AsyncIO *IO);
353 void StopClientWatchers(AsyncIO *IO, int CloseFD)
355 EVM_syslog(LOG_DEBUG, "EVENT StopClientWatchers");
359 ev_timer_stop (event_base, &IO->rw_timeout);
360 ev_timer_stop(event_base, &IO->conn_fail);
361 ev_idle_stop(event_base, &IO->unwind_stack);
362 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
364 ev_io_stop(event_base, &IO->conn_event);
365 ev_io_stop(event_base, &IO->send_event);
366 ev_io_stop(event_base, &IO->recv_event);
368 if (CloseFD && (IO->SendBuf.fd > 0)) {
369 close(IO->SendBuf.fd);
375 void StopCurlWatchers(AsyncIO *IO)
377 EVM_syslog(LOG_DEBUG, "EVENT StopCurlWatchers \n");
379 ev_timer_stop (event_base, &IO->rw_timeout);
380 ev_timer_stop(event_base, &IO->conn_fail);
381 ev_idle_stop(event_base, &IO->unwind_stack);
382 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
384 ev_io_stop(event_base, &IO->conn_event);
385 ev_io_stop(event_base, &IO->send_event);
386 ev_io_stop(event_base, &IO->recv_event);
388 curl_easy_cleanup(IO->HttpReq.chnd);
389 IO->HttpReq.chnd = NULL;
391 if (IO->SendBuf.fd != 0) {
392 close(IO->SendBuf.fd);
398 void ShutDownCLient(AsyncIO *IO)
400 CitContext *Ctx =IO->CitContext;
402 SetEVState(IO, eExit);
405 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
407 StopClientWatchers(IO, 1);
409 if (IO->DNS.Channel != NULL) {
410 ares_destroy(IO->DNS.Channel);
411 EV_DNS_LOG_STOP(DNS.recv_event);
412 EV_DNS_LOG_STOP(DNS.send_event);
413 ev_io_stop(event_base, &IO->DNS.recv_event);
414 ev_io_stop(event_base, &IO->DNS.send_event);
415 IO->DNS.Channel = NULL;
417 assert(IO->Terminate);
421 void PostInbound(AsyncIO *IO)
423 switch (IO->NextState) {
425 ev_io_start(event_base, &IO->send_event);
429 assert(IO->SendDone);
430 IO->NextState = IO->SendDone(IO);
431 switch (IO->NextState)
440 ev_io_start(event_base, &IO->send_event);
443 StopClientWatchers(IO, 0);
451 ev_io_start(event_base, &IO->recv_event);
453 case eTerminateConnection:
467 eReadState HandleInbound(AsyncIO *IO)
469 const char *Err = NULL;
470 eReadState Finished = eBufferNotEmpty;
472 become_session(IO->CitContext);
474 while ((Finished == eBufferNotEmpty) &&
475 ((IO->NextState == eReadMessage)||
476 (IO->NextState == eReadMore)||
477 (IO->NextState == eReadFile)||
478 (IO->NextState == eReadPayload)))
481 * lex line reply in callback,
482 * or do it ourselves.
483 * i.e. as nnn-blabla means continue reading in SMTP
485 if ((IO->NextState == eReadFile) &&
486 (Finished == eBufferNotEmpty))
488 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
489 if (Finished == eReadSuccess)
491 IO->NextState = eSendReply;
494 else if (IO->LineReader)
495 Finished = IO->LineReader(IO);
497 Finished = StrBufChunkSipLine(IO->IOBuf,
501 case eMustReadMore: /// read new from socket...
503 case eBufferNotEmpty: /* shouldn't happen... */
504 case eReadSuccess: /// done for now...
506 case eReadFail: /// WHUT?
511 if (Finished != eMustReadMore) {
513 assert(IO->ReadDone);
514 ev_io_stop(event_base, &IO->recv_event);
515 rc = IO->ReadDone(IO);
516 if (rc == eDBQuery) {
517 return QueueAnDBOperation(IO);
521 Finished = StrBufCheckBuffer(&IO->RecvBuf);
533 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
536 AsyncIO *IO = watcher->data;
537 const char *errmsg = NULL;
539 IO->Now = ev_now(event_base);
540 become_session(IO->CitContext);
546 const char *pch = ChrPtr(IO->SendBuf.Buf);
547 const char *pchh = IO->SendBuf.ReadWritePointer;
553 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
554 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
555 ((CitContext*)(IO->CitContext))->ServiceName,
558 fd = fopen(fn, "a+");
559 fprintf(fd, "Send: BufSize: %ld BufContent: [",
561 rv = fwrite(pchh, nbytes, 1, fd);
562 if (!rv) printf("failed to write debug to %s!\n", fn);
565 switch (IO->NextState) {
567 rc = FileSendChunked(&IO->IOB, &errmsg);
569 StrBufPlain(IO->ErrMsg, errmsg, -1);
572 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
578 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
584 ev_io_stop(event_base, &IO->send_event);
585 switch (IO->NextState) {
587 assert(IO->SendDone);
588 IO->NextState = IO->SendDone(IO);
590 if ((IO->NextState == eTerminateConnection) ||
591 (IO->NextState == eAbort) )
594 ev_io_start(event_base, &IO->send_event);
598 if (IO->IOB.ChunkSendRemain > 0) {
599 ev_io_start(event_base, &IO->recv_event);
600 SetNextTimeout(IO, 100.0);
603 assert(IO->ReadDone);
604 IO->NextState = IO->ReadDone(IO);
605 switch(IO->NextState) {
614 ev_io_start(event_base,
622 case eTerminateConnection:
629 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
631 IO->NextState = eReadMore;
636 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
641 ev_io_start(event_base, &IO->recv_event);
647 * we now live in another queue,
648 * so we have to unregister.
650 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
655 case eTerminateConnection:
661 if (errno != EAGAIN) {
662 StopClientWatchers(IO, 1);
664 "IO_send_callback(): Socket Invalid! [%d] [%s] [%d]\n",
665 errno, strerror(errno), IO->SendBuf.fd);
666 StrBufPrintf(IO->ErrMsg,
667 "Socket Invalid! [%s]",
669 SetNextTimeout(IO, 0.01);
672 /* else : must write more. */
675 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
677 ev_timer_stop(event_base, &IO->conn_fail);
678 ev_timer_start(event_base, &IO->rw_timeout);
680 switch(IO->NextState) {
684 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
685 ev_io_start(event_base, &IO->recv_event);
691 become_session(IO->CitContext);
692 IO_send_callback(loop, &IO->send_event, revents);
698 case eTerminateConnection:
706 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
708 AsyncIO *IO = watcher->data;
710 SetEVState(IO, eIOTimeout);
711 IO->Now = ev_now(event_base);
712 ev_timer_stop (event_base, &IO->rw_timeout);
713 become_session(IO->CitContext);
715 if (IO->SendBuf.fd != 0)
717 ev_io_stop(event_base, &IO->send_event);
718 ev_io_stop(event_base, &IO->recv_event);
719 ev_timer_stop (event_base, &IO->rw_timeout);
720 close(IO->SendBuf.fd);
721 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
725 switch (IO->Timeout(IO))
735 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
737 AsyncIO *IO = watcher->data;
739 SetEVState(IO, eIOConnfail);
740 IO->Now = ev_now(event_base);
741 ev_timer_stop (event_base, &IO->conn_fail);
743 if (IO->SendBuf.fd != 0)
745 ev_io_stop(loop, &IO->conn_event);
746 ev_io_stop(event_base, &IO->send_event);
747 ev_io_stop(event_base, &IO->recv_event);
748 ev_timer_stop (event_base, &IO->rw_timeout);
749 close(IO->SendBuf.fd);
750 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
752 become_session(IO->CitContext);
754 assert(IO->ConnFail);
755 switch (IO->ConnFail(IO))
766 IO_connfailimmediate_callback(struct ev_loop *loop,
770 AsyncIO *IO = watcher->data;
772 SetEVState(IO, eIOConnfailNow);
773 IO->Now = ev_now(event_base);
774 ev_idle_stop (event_base, &IO->conn_fail_immediate);
776 if (IO->SendBuf.fd != 0)
778 close(IO->SendBuf.fd);
779 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
781 become_session(IO->CitContext);
783 assert(IO->ConnFail);
784 switch (IO->ConnFail(IO))
795 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
797 AsyncIO *IO = watcher->data;
799 socklen_t lon = sizeof(so_err);
802 SetEVState(IO, eIOConnNow);
803 IO->Now = ev_now(event_base);
804 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
806 ev_io_stop(loop, &IO->conn_event);
807 ev_timer_stop(event_base, &IO->conn_fail);
809 err = getsockopt(IO->SendBuf.fd,
815 if ((err == 0) && (so_err != 0))
817 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
820 IO_connfail_callback(loop, &IO->conn_fail, revents);
825 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
826 set_start_callback(loop, IO, revents);
831 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
835 AsyncIO *IO = watcher->data;
837 IO->Now = ev_now(event_base);
838 switch (IO->NextState) {
840 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
842 StrBufPlain(IO->ErrMsg, errmsg, -1);
845 if (IO->IOB.ChunkSendRemain == 0)
847 IO->NextState = eSendReply;
848 assert(IO->ReadDone);
849 ev_io_stop(event_base, &IO->recv_event);
858 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
870 const char *pch = ChrPtr(IO->RecvBuf.Buf);
871 const char *pchh = IO->RecvBuf.ReadWritePointer;
876 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
877 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
878 ((CitContext*)(IO->CitContext))->ServiceName,
881 fd = fopen(fn, "a+");
882 fprintf(fd, "Read: BufSize: %ld BufContent: [",
884 rv = fwrite(pchh, nbytes, 1, fd);
885 if (!rv) printf("failed to write debug to %s!\n", fn);
892 } else if (nbytes == 0) {
893 StopClientWatchers(IO, 1);
894 SetNextTimeout(IO, 0.01);
896 } else if (nbytes == -1) {
897 if (errno != EAGAIN) {
898 // FD is gone. kick it.
899 StopClientWatchers(IO, 1);
901 "IO_recv_callback(): Socket Invalid! [%d] [%s] [%d]\n",
902 errno, strerror(errno), IO->SendBuf.fd);
903 StrBufPrintf(IO->ErrMsg,
904 "Socket Invalid! [%s]",
906 SetNextTimeout(IO, 0.01);
913 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
915 AsyncIO *IO = watcher->data;
917 SetEVState(IO, eCaresFinished);
918 IO->Now = ev_now(event_base);
919 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
920 become_session(IO->CitContext);
921 assert(IO->DNS.Query->PostDNS);
922 switch (IO->DNS.Query->PostDNS(IO))
925 assert(IO->DNS.Fail);
926 switch (IO->DNS.Fail(IO)) {
928 //// StopClientWatchers(IO);
939 eNextState EvConnectSock(AsyncIO *IO,
941 double first_rw_timeout,
944 struct sockaddr_in egress_sin;
948 SetEVState(IO, eIOConnectSock);
949 become_session(IO->CitContext);
952 IO->NextState = eReadMessage;
955 IO->NextState = eSendReply;
958 IO->SendBuf.fd = IO->RecvBuf.fd =
960 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
964 if (IO->SendBuf.fd < 0) {
966 "EVENT: socket() failed: %s\n",
969 StrBufPrintf(IO->ErrMsg,
970 "Failed to create socket: %s",
972 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
975 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
978 "EVENT: unable to get socket %d flags! %s \n",
981 StrBufPrintf(IO->ErrMsg,
982 "Failed to get socket %d flags: %s",
985 close(IO->SendBuf.fd);
986 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
989 fdflags = fdflags | O_NONBLOCK;
990 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
993 "EVENT: unable to set socket %d nonblocking flags! %s \n",
996 StrBufPrintf(IO->ErrMsg,
997 "Failed to set socket flags: %s",
999 close(IO->SendBuf.fd);
1000 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
1003 /* TODO: maye we could use offsetof() to calc the position of data...
1004 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
1006 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
1007 IO->recv_event.data = IO;
1008 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
1009 IO->send_event.data = IO;
1011 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
1012 IO->conn_fail.data = IO;
1013 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
1014 IO->rw_timeout.data = IO;
1019 /* for debugging you may bypass it like this:
1020 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1021 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
1022 * inet_addr("127.0.0.1");
1024 if (IO->ConnectMe->IPv6) {
1025 rc = connect(IO->SendBuf.fd,
1026 &IO->ConnectMe->Addr,
1027 sizeof(struct sockaddr_in6));
1030 /* If citserver is bound to a specific IP address on the host, make
1031 * sure we use that address for outbound connections.
1034 memset(&egress_sin, 0, sizeof(egress_sin));
1035 egress_sin.sin_family = AF_INET;
1036 if (!IsEmptyStr(config.c_ip_addr)) {
1037 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
1038 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
1039 egress_sin.sin_addr.s_addr = INADDR_ANY;
1042 /* If this bind fails, no problem; we can still use INADDR_ANY */
1043 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
1045 rc = connect(IO->SendBuf.fd,
1046 (struct sockaddr_in *)&IO->ConnectMe->Addr,
1047 sizeof(struct sockaddr_in));
1051 SetEVState(IO, eIOConnNow);
1052 EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd);
1053 set_start_callback(event_base, IO, 0);
1054 return IO->NextState;
1056 else if (errno == EINPROGRESS) {
1057 SetEVState(IO, eIOConnWait);
1058 EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd);
1060 ev_io_init(&IO->conn_event,
1061 IO_connestd_callback,
1065 IO->conn_event.data = IO;
1067 ev_io_start(event_base, &IO->conn_event);
1068 ev_timer_start(event_base, &IO->conn_fail);
1069 return IO->NextState;
1072 SetEVState(IO, eIOConnfail);
1073 ev_idle_init(&IO->conn_fail_immediate,
1074 IO_connfailimmediate_callback);
1075 IO->conn_fail_immediate.data = IO;
1076 ev_idle_start(event_base, &IO->conn_fail_immediate);
1079 "connect() = %d failed: %s\n",
1083 StrBufPrintf(IO->ErrMsg,
1084 "Failed to connect: %s",
1086 return IO->NextState;
1088 return IO->NextState;
1091 void SetNextTimeout(AsyncIO *IO, double timeout)
1093 IO->rw_timeout.repeat = timeout;
1094 ev_timer_again (event_base, &IO->rw_timeout);
1098 eNextState ReAttachIO(AsyncIO *IO,
1102 SetEVState(IO, eIOAttach);
1104 become_session(IO->CitContext);
1105 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1107 IO->NextState = eReadMessage;
1110 IO->NextState = eSendReply;
1112 set_start_callback(event_base, IO, 0);
1114 return IO->NextState;
1117 void InitIOStruct(AsyncIO *IO,
1119 eNextState NextState,
1120 IO_LineReaderCallback LineReader,
1121 IO_CallBack DNS_Fail,
1122 IO_CallBack SendDone,
1123 IO_CallBack ReadDone,
1124 IO_CallBack Terminate,
1125 IO_CallBack DBTerminate,
1126 IO_CallBack ConnFail,
1127 IO_CallBack Timeout,
1128 IO_CallBack ShutdownAbort)
1132 IO->CitContext = CloneContext(CC);
1133 IO->CitContext->session_specific_data = Data;
1134 IO->CitContext->IO = IO;
1136 IO->NextState = NextState;
1138 IO->SendDone = SendDone;
1139 IO->ReadDone = ReadDone;
1140 IO->Terminate = Terminate;
1141 IO->DBTerminate = DBTerminate;
1142 IO->LineReader = LineReader;
1143 IO->ConnFail = ConnFail;
1144 IO->Timeout = Timeout;
1145 IO->ShutdownAbort = ShutdownAbort;
1147 IO->DNS.Fail = DNS_Fail;
1149 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1150 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1151 IO->IOBuf = NewStrBuf();
1152 EV_syslog(LOG_DEBUG,
1153 "EVENT: Session lives at %p IO at %p \n",
1158 extern int evcurl_init(AsyncIO *IO);
1160 int InitcURLIOStruct(AsyncIO *IO,
1163 IO_CallBack SendDone,
1164 IO_CallBack Terminate,
1165 IO_CallBack DBTerminate,
1166 IO_CallBack ShutdownAbort)
1170 IO->CitContext = CloneContext(CC);
1171 IO->CitContext->session_specific_data = Data;
1172 IO->CitContext->IO = IO;
1174 IO->SendDone = SendDone;
1175 IO->Terminate = Terminate;
1176 IO->DBTerminate = DBTerminate;
1177 IO->ShutdownAbort = ShutdownAbort;
1179 strcpy(IO->HttpReq.errdesc, Desc);
1182 return evcurl_init(IO);
1187 typedef struct KillOtherSessionContext {
1190 }KillOtherSessionContext;
1192 eNextState KillTerminate(AsyncIO *IO)
1195 KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
1196 EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
1198 FreeAsyncIOContents(IO);
1199 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1200 IO->ID = id; /* just for the case we want to analyze it in a coredump */
1206 eNextState KillShutdown(AsyncIO *IO)
1208 return eTerminateConnection;
1211 eNextState KillOtherContextNow(AsyncIO *IO)
1213 KillOtherSessionContext *Ctx = IO->Data;
1215 SetEVState(IO, eKill);
1217 if (Ctx->OtherOne->ShutdownAbort != NULL)
1218 Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
1219 return eTerminateConnection;
1222 void KillAsyncIOContext(AsyncIO *IO)
1224 KillOtherSessionContext *Ctx;
1226 Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
1227 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1229 InitIOStruct(&Ctx->IO,
1244 switch(IO->NextState) {
1257 IO->ReAttachCB = KillOtherContextNow;
1258 QueueAnEventContext(&Ctx->IO);
1261 IO->ReAttachCB = KillOtherContextNow;
1262 QueueAnDBOperation(&Ctx->IO);
1264 case eTerminateConnection:
1266 /*hm, its already dying, dunno which Queue its in... */
1272 extern int DebugEventLoopBacktrace;
1273 void EV_backtrace(AsyncIO *IO)
1275 #ifdef HAVE_BACKTRACE
1276 void *stack_frames[50];
1280 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1282 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1283 strings = backtrace_symbols(stack_frames, size);
1284 for (i = 0; i < size; i++) {
1285 if (strings != NULL) {
1286 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1289 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1297 ev_tstamp ctdl_ev_now (void)
1299 return ev_now(event_base);