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>
22 #include <libcitadel.h>
24 #include "ctdl_module.h"
25 #include "event_client.h"
27 ConstStr IOStates[] = {
34 {HKEY("DB Terminate")},
37 {HKEY("IO Connect Socket")},
40 {HKEY("IO ConnFail")},
41 {HKEY("IO ConnFail Now")},
42 {HKEY("IO Conn Now")},
43 {HKEY("IO Conn Wait")},
46 {HKEY("Curl Shotdown")},
47 {HKEY("Curl More IO")},
48 {HKEY("Curl Got IO")},
49 {HKEY("Curl Got Data")},
50 {HKEY("Curl Got Status")},
51 {HKEY("C-Ares Start")},
52 {HKEY("C-Ares IO Done")},
53 {HKEY("C-Ares Finished")},
54 {HKEY("C-Ares exit")},
59 void SetEVState(AsyncIO *IO, eIOState State)
62 CitContext* CCC = IO->CitContext;
64 memcpy(CCC->lastcmdname, IOStates[State].Key, IOStates[State].len + 1);
69 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
70 static void IO_abort_shutdown_callback(struct ev_loop *loop,
75 /*------------------------------------------------------------------------------
77 *----------------------------------------------------------------------------*/
78 extern int evdb_count;
79 extern pthread_mutex_t DBEventQueueMutex;
80 extern pthread_mutex_t DBEventExitQueueMutex;
81 extern HashList *DBInboundEventQueue;
82 extern struct ev_loop *event_db;
83 extern ev_async DBAddJob;
84 extern ev_async DBExitEventLoop;
86 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
92 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
95 ev_cleanup_init(&IO->db_abort_by_shutdown,
96 IO_abort_shutdown_callback);
97 IO->db_abort_by_shutdown.data = IO;
99 pthread_mutex_lock(&DBEventQueueMutex);
100 if (DBInboundEventQueue == NULL)
102 /* shutting down... */
104 EVM_syslog(LOG_DEBUG, "DBEVENT Q exiting.\n");
105 pthread_mutex_unlock(&DBEventQueueMutex);
108 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
110 Put(DBInboundEventQueue, IKEY(i), h, NULL);
111 pthread_mutex_unlock(&DBEventQueueMutex);
113 pthread_mutex_lock(&DBEventExitQueueMutex);
114 if (event_db == NULL)
116 pthread_mutex_unlock(&DBEventExitQueueMutex);
119 ev_async_send (event_db, &DBAddJob);
120 pthread_mutex_unlock(&DBEventExitQueueMutex);
122 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
126 void StopDBWatchers(AsyncIO *IO)
128 SetEVState(IO, eDBStop);
129 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
130 ev_idle_stop(event_db, &IO->db_unwind_stack);
133 void ShutDownDBCLient(AsyncIO *IO)
135 CitContext *Ctx =IO->CitContext;
138 SetEVState(IO, eDBTerm);
139 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
142 assert(IO->DBTerminate);
147 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
149 AsyncIO *IO = watcher->data;
151 SetEVState(IO, eDBNext);
152 IO->Now = ev_now(event_db);
153 EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
154 become_session(IO->CitContext);
156 ev_idle_stop(event_db, &IO->db_unwind_stack);
158 assert(IO->NextDBOperation);
159 switch (IO->NextDBOperation(IO))
173 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
175 case eTerminateConnection:
177 ev_idle_stop(event_db, &IO->db_unwind_stack);
178 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
179 ShutDownDBCLient(IO);
183 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
185 SetEVState(IO, eQDBNext);
186 IO->NextDBOperation = CB;
187 ev_idle_init(&IO->db_unwind_stack,
189 IO->db_unwind_stack.data = IO;
190 ev_idle_start(event_db, &IO->db_unwind_stack);
194 /*------------------------------------------------------------------------------
196 *----------------------------------------------------------------------------*/
197 extern int evbase_count;
198 extern pthread_mutex_t EventQueueMutex;
199 extern pthread_mutex_t EventExitQueueMutex;
200 extern HashList *InboundEventQueue;
201 extern struct ev_loop *event_base;
202 extern ev_async AddJob;
203 extern ev_async ExitEventLoop;
205 static void IO_abort_shutdown_callback(struct ev_loop *loop,
209 AsyncIO *IO = watcher->data;
211 SetEVState(IO, eIOAbort);
212 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
213 IO->Now = ev_now(event_base);
214 assert(IO->ShutdownAbort);
215 IO->ShutdownAbort(IO);
219 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
224 SetEVState(IO, eIOQ);
225 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
228 ev_cleanup_init(&IO->abort_by_shutdown,
229 IO_abort_shutdown_callback);
230 IO->abort_by_shutdown.data = IO;
232 pthread_mutex_lock(&EventQueueMutex);
233 if (InboundEventQueue == NULL)
236 /* shutting down... */
237 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
238 pthread_mutex_unlock(&EventQueueMutex);
241 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
243 Put(InboundEventQueue, IKEY(i), h, NULL);
244 pthread_mutex_unlock(&EventQueueMutex);
246 pthread_mutex_lock(&EventExitQueueMutex);
247 if (event_base == NULL) {
248 pthread_mutex_unlock(&EventExitQueueMutex);
251 ev_async_send (event_base, &AddJob);
252 pthread_mutex_unlock(&EventExitQueueMutex);
253 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
257 eNextState EventQueueDBOperation(AsyncIO *IO, IO_CallBack CB)
259 StopClientWatchers(IO, 0);
260 return QueueDBOperation(IO, CB);
262 eNextState DBQueueEventContext(AsyncIO *IO, IO_CallBack CB)
265 return QueueEventContext(IO, CB);
268 extern eNextState evcurl_handle_start(AsyncIO *IO);
270 eNextState QueueCurlContext(AsyncIO *IO)
275 SetEVState(IO, eCurlQ);
276 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
278 h->EvAttch = evcurl_handle_start;
280 pthread_mutex_lock(&EventQueueMutex);
281 if (InboundEventQueue == NULL)
283 /* shutting down... */
285 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
286 pthread_mutex_unlock(&EventQueueMutex);
290 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
292 Put(InboundEventQueue, IKEY(i), h, NULL);
293 pthread_mutex_unlock(&EventQueueMutex);
295 pthread_mutex_lock(&EventExitQueueMutex);
296 if (event_base == NULL) {
297 pthread_mutex_unlock(&EventExitQueueMutex);
300 ev_async_send (event_base, &AddJob);
301 pthread_mutex_unlock(&EventExitQueueMutex);
303 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
307 eNextState CurlQueueDBOperation(AsyncIO *IO, IO_CallBack CB)
309 StopCurlWatchers(IO);
310 return QueueDBOperation(IO, CB);
314 void DestructCAres(AsyncIO *IO);
315 void FreeAsyncIOContents(AsyncIO *IO)
317 CitContext *Ctx = IO->CitContext;
319 FreeStrBuf(&IO->IOBuf);
320 FreeStrBuf(&IO->SendBuf.Buf);
321 FreeStrBuf(&IO->RecvBuf.Buf);
325 FreeURL(&IO->ConnectMe);
326 FreeStrBuf(&IO->HttpReq.ReplyData);
329 Ctx->state = CON_IDLE;
331 IO->CitContext = NULL;
336 void StopClientWatchers(AsyncIO *IO, int CloseFD)
338 ev_timer_stop (event_base, &IO->rw_timeout);
339 ev_timer_stop(event_base, &IO->conn_fail);
340 ev_idle_stop(event_base, &IO->unwind_stack);
341 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
343 ev_io_stop(event_base, &IO->conn_event);
344 ev_io_stop(event_base, &IO->send_event);
345 ev_io_stop(event_base, &IO->recv_event);
347 if (CloseFD && (IO->SendBuf.fd > 0)) {
348 close(IO->SendBuf.fd);
354 void StopCurlWatchers(AsyncIO *IO)
356 EVM_syslog(LOG_DEBUG, "EVENT StopCurlWatchers \n");
358 ev_timer_stop (event_base, &IO->rw_timeout);
359 ev_timer_stop(event_base, &IO->conn_fail);
360 ev_idle_stop(event_base, &IO->unwind_stack);
361 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
363 ev_io_stop(event_base, &IO->conn_event);
364 ev_io_stop(event_base, &IO->send_event);
365 ev_io_stop(event_base, &IO->recv_event);
367 curl_easy_cleanup(IO->HttpReq.chnd);
368 IO->HttpReq.chnd = NULL;
370 if (IO->SendBuf.fd != 0) {
371 close(IO->SendBuf.fd);
377 void ShutDownCLient(AsyncIO *IO)
379 CitContext *Ctx =IO->CitContext;
381 SetEVState(IO, eExit);
384 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
386 StopClientWatchers(IO, 1);
388 if (IO->DNS.Channel != NULL) {
389 ares_destroy(IO->DNS.Channel);
390 EV_DNS_LOG_STOP(DNS.recv_event);
391 EV_DNS_LOG_STOP(DNS.send_event);
392 ev_io_stop(event_base, &IO->DNS.recv_event);
393 ev_io_stop(event_base, &IO->DNS.send_event);
394 IO->DNS.Channel = NULL;
396 assert(IO->Terminate);
400 void PostInbound(AsyncIO *IO)
402 switch (IO->NextState) {
404 ev_io_start(event_base, &IO->send_event);
408 assert(IO->SendDone);
409 IO->NextState = IO->SendDone(IO);
410 switch (IO->NextState)
419 ev_io_start(event_base, &IO->send_event);
422 StopClientWatchers(IO, 0);
430 ev_io_start(event_base, &IO->recv_event);
432 case eTerminateConnection:
446 eReadState HandleInbound(AsyncIO *IO)
448 const char *Err = NULL;
449 eReadState Finished = eBufferNotEmpty;
451 become_session(IO->CitContext);
453 while ((Finished == eBufferNotEmpty) &&
454 ((IO->NextState == eReadMessage)||
455 (IO->NextState == eReadMore)||
456 (IO->NextState == eReadFile)||
457 (IO->NextState == eReadPayload)))
460 * lex line reply in callback,
461 * or do it ourselves.
462 * i.e. as nnn-blabla means continue reading in SMTP
464 if ((IO->NextState == eReadFile) &&
465 (Finished == eBufferNotEmpty))
467 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
468 if (Finished == eReadSuccess)
470 IO->NextState = eSendReply;
473 else if (IO->LineReader)
474 Finished = IO->LineReader(IO);
476 Finished = StrBufChunkSipLine(IO->IOBuf,
480 case eMustReadMore: /// read new from socket...
482 case eBufferNotEmpty: /* shouldn't happen... */
483 case eReadSuccess: /// done for now...
485 case eReadFail: /// WHUT?
490 if (Finished != eMustReadMore) {
491 assert(IO->ReadDone);
492 ev_io_stop(event_base, &IO->recv_event);
493 IO->NextState = IO->ReadDone(IO);
494 Finished = StrBufCheckBuffer(&IO->RecvBuf);
505 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
508 AsyncIO *IO = watcher->data;
509 const char *errmsg = NULL;
511 IO->Now = ev_now(event_base);
512 become_session(IO->CitContext);
518 const char *pch = ChrPtr(IO->SendBuf.Buf);
519 const char *pchh = IO->SendBuf.ReadWritePointer;
525 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
526 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
527 ((CitContext*)(IO->CitContext))->ServiceName,
530 fd = fopen(fn, "a+");
531 fprintf(fd, "Send: BufSize: %ld BufContent: [",
533 rv = fwrite(pchh, nbytes, 1, fd);
534 if (!rv) printf("failed to write debug to %s!\n", fn);
537 switch (IO->NextState) {
539 rc = FileSendChunked(&IO->IOB, &errmsg);
541 StrBufPlain(IO->ErrMsg, errmsg, -1);
544 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
550 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
556 ev_io_stop(event_base, &IO->send_event);
557 switch (IO->NextState) {
559 assert(IO->SendDone);
560 IO->NextState = IO->SendDone(IO);
562 if ((IO->NextState == eTerminateConnection) ||
563 (IO->NextState == eAbort) )
566 ev_io_start(event_base, &IO->send_event);
570 if (IO->IOB.ChunkSendRemain > 0) {
571 ev_io_start(event_base, &IO->recv_event);
572 SetNextTimeout(IO, 100.0);
575 assert(IO->ReadDone);
576 IO->NextState = IO->ReadDone(IO);
577 switch(IO->NextState) {
586 ev_io_start(event_base,
594 case eTerminateConnection:
601 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
603 IO->NextState = eReadMore;
608 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
613 ev_io_start(event_base, &IO->recv_event);
619 * we now live in another queue,
620 * so we have to unregister.
622 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
627 case eTerminateConnection:
633 if (errno != EAGAIN) {
634 StopClientWatchers(IO, 1);
636 "IO_send_callback(): Socket Invalid! [%d] [%s] [%d]\n",
637 errno, strerror(errno), IO->SendBuf.fd);
638 StrBufPrintf(IO->ErrMsg,
639 "Socket Invalid! [%s]",
641 SetNextTimeout(IO, 0.01);
644 /* else : must write more. */
647 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
649 ev_timer_stop(event_base, &IO->conn_fail);
650 ev_timer_start(event_base, &IO->rw_timeout);
652 switch(IO->NextState) {
656 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
657 ev_io_start(event_base, &IO->recv_event);
663 become_session(IO->CitContext);
664 IO_send_callback(loop, &IO->send_event, revents);
670 case eTerminateConnection:
678 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
680 AsyncIO *IO = watcher->data;
682 SetEVState(IO, eIOTimeout);
683 IO->Now = ev_now(event_base);
684 ev_timer_stop (event_base, &IO->rw_timeout);
685 become_session(IO->CitContext);
687 if (IO->SendBuf.fd != 0)
689 ev_io_stop(event_base, &IO->send_event);
690 ev_io_stop(event_base, &IO->recv_event);
691 ev_timer_stop (event_base, &IO->rw_timeout);
692 close(IO->SendBuf.fd);
693 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
697 switch (IO->Timeout(IO))
707 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
709 AsyncIO *IO = watcher->data;
711 SetEVState(IO, eIOConnfail);
712 IO->Now = ev_now(event_base);
713 ev_timer_stop (event_base, &IO->conn_fail);
715 if (IO->SendBuf.fd != 0)
717 ev_io_stop(loop, &IO->conn_event);
718 ev_io_stop(event_base, &IO->send_event);
719 ev_io_stop(event_base, &IO->recv_event);
720 ev_timer_stop (event_base, &IO->rw_timeout);
721 close(IO->SendBuf.fd);
722 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
724 become_session(IO->CitContext);
726 assert(IO->ConnFail);
727 switch (IO->ConnFail(IO))
738 IO_connfailimmediate_callback(struct ev_loop *loop,
742 AsyncIO *IO = watcher->data;
744 SetEVState(IO, eIOConnfailNow);
745 IO->Now = ev_now(event_base);
746 ev_idle_stop (event_base, &IO->conn_fail_immediate);
748 if (IO->SendBuf.fd != 0)
750 close(IO->SendBuf.fd);
751 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
753 become_session(IO->CitContext);
755 assert(IO->ConnFail);
756 switch (IO->ConnFail(IO))
767 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
769 AsyncIO *IO = watcher->data;
771 socklen_t lon = sizeof(so_err);
774 SetEVState(IO, eIOConnNow);
775 IO->Now = ev_now(event_base);
776 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
778 ev_io_stop(loop, &IO->conn_event);
779 ev_timer_stop(event_base, &IO->conn_fail);
781 err = getsockopt(IO->SendBuf.fd,
787 if ((err == 0) && (so_err != 0))
789 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
792 IO_connfail_callback(loop, &IO->conn_fail, revents);
797 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
798 set_start_callback(loop, IO, revents);
803 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
807 AsyncIO *IO = watcher->data;
809 IO->Now = ev_now(event_base);
810 switch (IO->NextState) {
812 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
814 StrBufPlain(IO->ErrMsg, errmsg, -1);
817 if (IO->IOB.ChunkSendRemain == 0)
819 IO->NextState = eSendReply;
820 assert(IO->ReadDone);
821 ev_io_stop(event_base, &IO->recv_event);
830 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
842 const char *pch = ChrPtr(IO->RecvBuf.Buf);
843 const char *pchh = IO->RecvBuf.ReadWritePointer;
848 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
849 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
850 ((CitContext*)(IO->CitContext))->ServiceName,
853 fd = fopen(fn, "a+");
854 fprintf(fd, "Read: BufSize: %ld BufContent: [",
856 rv = fwrite(pchh, nbytes, 1, fd);
857 if (!rv) printf("failed to write debug to %s!\n", fn);
864 } else if (nbytes == 0) {
865 StopClientWatchers(IO, 1);
866 SetNextTimeout(IO, 0.01);
868 } else if (nbytes == -1) {
869 if (errno != EAGAIN) {
870 // FD is gone. kick it.
871 StopClientWatchers(IO, 1);
873 "IO_recv_callback(): Socket Invalid! [%d] [%s] [%d]\n",
874 errno, strerror(errno), IO->SendBuf.fd);
875 StrBufPrintf(IO->ErrMsg,
876 "Socket Invalid! [%s]",
878 SetNextTimeout(IO, 0.01);
885 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
887 AsyncIO *IO = watcher->data;
889 SetEVState(IO, eCaresFinished);
890 IO->Now = ev_now(event_base);
891 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
892 become_session(IO->CitContext);
893 assert(IO->DNS.Query->PostDNS);
894 switch (IO->DNS.Query->PostDNS(IO))
897 assert(IO->DNS.Fail);
898 switch (IO->DNS.Fail(IO)) {
900 //// StopClientWatchers(IO);
911 eNextState EvConnectSock(AsyncIO *IO,
913 double first_rw_timeout,
916 struct sockaddr_in egress_sin;
920 SetEVState(IO, eIOConnectSock);
921 become_session(IO->CitContext);
924 IO->NextState = eReadMessage;
927 IO->NextState = eSendReply;
930 IO->SendBuf.fd = IO->RecvBuf.fd =
932 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
936 if (IO->SendBuf.fd < 0) {
938 "EVENT: socket() failed: %s\n",
941 StrBufPrintf(IO->ErrMsg,
942 "Failed to create socket: %s",
944 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
947 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
950 "EVENT: unable to get socket %d flags! %s \n",
953 StrBufPrintf(IO->ErrMsg,
954 "Failed to get socket %d flags: %s",
957 close(IO->SendBuf.fd);
958 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
961 fdflags = fdflags | O_NONBLOCK;
962 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
965 "EVENT: unable to set socket %d nonblocking flags! %s \n",
968 StrBufPrintf(IO->ErrMsg,
969 "Failed to set socket flags: %s",
971 close(IO->SendBuf.fd);
972 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
975 /* TODO: maye we could use offsetof() to calc the position of data...
976 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
978 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
979 IO->recv_event.data = IO;
980 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
981 IO->send_event.data = IO;
983 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
984 IO->conn_fail.data = IO;
985 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
986 IO->rw_timeout.data = IO;
991 /* for debugging you may bypass it like this:
992 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
993 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
994 * inet_addr("127.0.0.1");
996 if (IO->ConnectMe->IPv6) {
997 rc = connect(IO->SendBuf.fd,
998 &IO->ConnectMe->Addr,
999 sizeof(struct sockaddr_in6));
1002 /* If citserver is bound to a specific IP address on the host, make
1003 * sure we use that address for outbound connections.
1006 memset(&egress_sin, 0, sizeof(egress_sin));
1007 egress_sin.sin_family = AF_INET;
1008 if (!IsEmptyStr(config.c_ip_addr)) {
1009 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
1010 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
1011 egress_sin.sin_addr.s_addr = INADDR_ANY;
1014 /* If this bind fails, no problem; we can still use INADDR_ANY */
1015 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
1017 rc = connect(IO->SendBuf.fd,
1018 (struct sockaddr_in *)&IO->ConnectMe->Addr,
1019 sizeof(struct sockaddr_in));
1023 SetEVState(IO, eIOConnNow);
1024 EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd);
1025 set_start_callback(event_base, IO, 0);
1026 return IO->NextState;
1028 else if (errno == EINPROGRESS) {
1029 SetEVState(IO, eIOConnWait);
1030 EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd);
1032 ev_io_init(&IO->conn_event,
1033 IO_connestd_callback,
1037 IO->conn_event.data = IO;
1039 ev_io_start(event_base, &IO->conn_event);
1040 ev_timer_start(event_base, &IO->conn_fail);
1041 return IO->NextState;
1044 SetEVState(IO, eIOConnfail);
1045 ev_idle_init(&IO->conn_fail_immediate,
1046 IO_connfailimmediate_callback);
1047 IO->conn_fail_immediate.data = IO;
1048 ev_idle_start(event_base, &IO->conn_fail_immediate);
1051 "connect() = %d failed: %s\n",
1055 StrBufPrintf(IO->ErrMsg,
1056 "Failed to connect: %s",
1058 return IO->NextState;
1060 return IO->NextState;
1063 void SetNextTimeout(AsyncIO *IO, double timeout)
1065 IO->rw_timeout.repeat = timeout;
1066 ev_timer_again (event_base, &IO->rw_timeout);
1070 eNextState ReAttachIO(AsyncIO *IO,
1074 SetEVState(IO, eIOAttach);
1076 become_session(IO->CitContext);
1077 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1079 IO->NextState = eReadMessage;
1082 IO->NextState = eSendReply;
1084 set_start_callback(event_base, IO, 0);
1086 return IO->NextState;
1089 void InitIOStruct(AsyncIO *IO,
1091 eNextState NextState,
1092 IO_LineReaderCallback LineReader,
1093 IO_CallBack DNS_Fail,
1094 IO_CallBack SendDone,
1095 IO_CallBack ReadDone,
1096 IO_CallBack Terminate,
1097 IO_CallBack DBTerminate,
1098 IO_CallBack ConnFail,
1099 IO_CallBack Timeout,
1100 IO_CallBack ShutdownAbort)
1104 IO->CitContext = CloneContext(CC);
1105 IO->CitContext->session_specific_data = Data;
1106 IO->CitContext->IO = IO;
1108 IO->NextState = NextState;
1110 IO->SendDone = SendDone;
1111 IO->ReadDone = ReadDone;
1112 IO->Terminate = Terminate;
1113 IO->DBTerminate = DBTerminate;
1114 IO->LineReader = LineReader;
1115 IO->ConnFail = ConnFail;
1116 IO->Timeout = Timeout;
1117 IO->ShutdownAbort = ShutdownAbort;
1119 IO->DNS.Fail = DNS_Fail;
1121 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1122 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1123 IO->IOBuf = NewStrBuf();
1124 EV_syslog(LOG_DEBUG,
1125 "EVENT: Session lives at %p IO at %p \n",
1130 extern int evcurl_init(AsyncIO *IO);
1132 int InitcURLIOStruct(AsyncIO *IO,
1135 IO_CallBack SendDone,
1136 IO_CallBack Terminate,
1137 IO_CallBack DBTerminate,
1138 IO_CallBack ShutdownAbort)
1142 IO->CitContext = CloneContext(CC);
1143 IO->CitContext->session_specific_data = Data;
1144 IO->CitContext->IO = IO;
1146 IO->SendDone = SendDone;
1147 IO->Terminate = Terminate;
1148 IO->DBTerminate = DBTerminate;
1149 IO->ShutdownAbort = ShutdownAbort;
1151 strcpy(IO->HttpReq.errdesc, Desc);
1154 return evcurl_init(IO);
1159 typedef struct KillOtherSessionContext {
1162 }KillOtherSessionContext;
1164 eNextState KillTerminate(AsyncIO *IO)
1167 KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
1168 EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
1170 FreeAsyncIOContents(IO);
1171 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1172 IO->ID = id; /* just for the case we want to analyze it in a coredump */
1178 eNextState KillShutdown(AsyncIO *IO)
1180 return eTerminateConnection;
1183 eNextState KillOtherContextNow(AsyncIO *IO)
1185 KillOtherSessionContext *Ctx = IO->Data;
1187 SetEVState(IO, eKill);
1189 if (Ctx->OtherOne->ShutdownAbort != NULL)
1190 Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
1191 return eTerminateConnection;
1194 void KillAsyncIOContext(AsyncIO *IO)
1196 KillOtherSessionContext *Ctx;
1198 Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
1199 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1201 InitIOStruct(&Ctx->IO,
1216 switch(IO->NextState) {
1229 QueueEventContext(&Ctx->IO, KillOtherContextNow);
1232 QueueDBOperation(&Ctx->IO, KillOtherContextNow);
1234 case eTerminateConnection:
1236 /*hm, its already dying, dunno which Queue its in... */
1242 extern int DebugEventLoopBacktrace;
1243 void EV_backtrace(AsyncIO *IO)
1245 #ifdef HAVE_BACKTRACE
1246 void *stack_frames[50];
1250 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1252 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1253 strings = backtrace_symbols(stack_frames, size);
1254 for (i = 0; i < size; i++) {
1255 if (strings != NULL) {
1256 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1259 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1267 ev_tstamp ctdl_ev_now (void)
1269 return ev_now(event_base);