3 * Copyright (c) 1998-2012 by the citadel.org team
5 * This program is open source software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
54 #include <libcitadel.h>
57 #include "citserver.h"
64 #include "internet_addressing.h"
67 #include "clientsocket.h"
68 #include "locate_host.h"
69 #include "citadel_dirs.h"
71 #include "event_client.h"
72 #include "ctdl_module.h"
74 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
75 static void IO_abort_shutdown_callback(struct ev_loop *loop,
80 /*------------------------------------------------------------------------------
82 *----------------------------------------------------------------------------*/
83 extern int evdb_count;
84 extern pthread_mutex_t DBEventQueueMutex;
85 extern HashList *DBInboundEventQueue;
86 extern struct ev_loop *event_db;
87 extern ev_async DBAddJob;
88 extern ev_async DBExitEventLoop;
90 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
95 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
98 ev_cleanup_init(&IO->db_abort_by_shutdown,
99 IO_abort_shutdown_callback);
100 IO->db_abort_by_shutdown.data = IO;
101 ev_cleanup_start(event_db, &IO->db_abort_by_shutdown);
103 pthread_mutex_lock(&DBEventQueueMutex);
104 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
106 Put(DBInboundEventQueue, IKEY(i), h, NULL);
107 pthread_mutex_unlock(&DBEventQueueMutex);
109 ev_async_send (event_db, &DBAddJob);
110 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
114 void ShutDownDBCLient(AsyncIO *IO)
116 CitContext *Ctx =IO->CitContext;
119 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
120 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
122 assert(IO->DBTerminate);
127 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
129 AsyncIO *IO = watcher->data;
130 IO->Now = ev_now(event_db);
131 EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
132 become_session(IO->CitContext);
134 ev_idle_stop(event_db, &IO->db_unwind_stack);
136 assert(IO->NextDBOperation);
137 switch (IO->NextDBOperation(IO))
151 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
153 case eTerminateConnection:
155 ev_idle_stop(event_db, &IO->db_unwind_stack);
156 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
157 ShutDownDBCLient(IO);
161 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
163 IO->NextDBOperation = CB;
164 ev_idle_init(&IO->db_unwind_stack,
166 IO->db_unwind_stack.data = IO;
167 ev_idle_start(event_db, &IO->db_unwind_stack);
171 /*------------------------------------------------------------------------------
173 *----------------------------------------------------------------------------*/
174 extern int evbase_count;
175 extern pthread_mutex_t EventQueueMutex;
176 extern HashList *InboundEventQueue;
177 extern struct ev_loop *event_base;
178 extern ev_async AddJob;
179 extern ev_async ExitEventLoop;
181 static void IO_abort_shutdown_callback(struct ev_loop *loop,
185 AsyncIO *IO = watcher->data;
186 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
187 IO->Now = ev_now(event_base);
188 assert(IO->ShutdownAbort);
189 IO->ShutdownAbort(IO);
193 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
198 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
201 ev_cleanup_init(&IO->abort_by_shutdown,
202 IO_abort_shutdown_callback);
203 IO->abort_by_shutdown.data = IO;
204 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
206 pthread_mutex_lock(&EventQueueMutex);
207 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
209 Put(InboundEventQueue, IKEY(i), h, NULL);
210 pthread_mutex_unlock(&EventQueueMutex);
212 ev_async_send (event_base, &AddJob);
213 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
217 extern eNextState evcurl_handle_start(AsyncIO *IO);
219 eNextState QueueCurlContext(AsyncIO *IO)
224 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
226 h->EvAttch = evcurl_handle_start;
228 pthread_mutex_lock(&EventQueueMutex);
229 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
231 Put(InboundEventQueue, IKEY(i), h, NULL);
232 pthread_mutex_unlock(&EventQueueMutex);
234 ev_async_send (event_base, &AddJob);
235 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
239 void DestructCAres(AsyncIO *IO);
240 void FreeAsyncIOContents(AsyncIO *IO)
242 CitContext *Ctx = IO->CitContext;
244 FreeStrBuf(&IO->IOBuf);
245 FreeStrBuf(&IO->SendBuf.Buf);
246 FreeStrBuf(&IO->RecvBuf.Buf);
250 FreeURL(&IO->ConnectMe);
251 FreeStrBuf(&IO->HttpReq.ReplyData);
254 Ctx->state = CON_IDLE;
260 void StopClientWatchers(AsyncIO *IO)
262 ev_timer_stop (event_base, &IO->rw_timeout);
263 ev_timer_stop(event_base, &IO->conn_fail);
264 ev_idle_stop(event_base, &IO->unwind_stack);
266 ev_io_stop(event_base, &IO->conn_event);
267 ev_io_stop(event_base, &IO->send_event);
268 ev_io_stop(event_base, &IO->recv_event);
270 if (IO->SendBuf.fd != 0) {
271 close(IO->SendBuf.fd);
277 void ShutDownCLient(AsyncIO *IO)
279 CitContext *Ctx =IO->CitContext;
282 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
284 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
285 StopClientWatchers(IO);
287 if (IO->DNS.Channel != NULL) {
288 ares_destroy(IO->DNS.Channel);
289 EV_DNS_LOG_STOP(DNS.recv_event);
290 EV_DNS_LOG_STOP(DNS.send_event);
291 ev_io_stop(event_base, &IO->DNS.recv_event);
292 ev_io_stop(event_base, &IO->DNS.send_event);
293 IO->DNS.Channel = NULL;
295 assert(IO->Terminate);
299 void PostInbound(AsyncIO *IO)
301 switch (IO->NextState) {
303 ev_io_start(event_base, &IO->send_event);
307 assert(IO->SendDone);
308 IO->NextState = IO->SendDone(IO);
309 ev_io_start(event_base, &IO->send_event);
314 ev_io_start(event_base, &IO->recv_event);
316 case eTerminateConnection:
330 eReadState HandleInbound(AsyncIO *IO)
332 const char *Err = NULL;
333 eReadState Finished = eBufferNotEmpty;
335 become_session(IO->CitContext);
337 while ((Finished == eBufferNotEmpty) &&
338 ((IO->NextState == eReadMessage)||
339 (IO->NextState == eReadMore)||
340 (IO->NextState == eReadFile)||
341 (IO->NextState == eReadPayload)))
344 * lex line reply in callback,
345 * or do it ourselves.
346 * i.e. as nnn-blabla means continue reading in SMTP
348 if ((IO->NextState == eReadFile) &&
349 (Finished == eBufferNotEmpty))
351 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
352 if (Finished == eReadSuccess)
354 IO->NextState = eSendReply;
357 else if (IO->LineReader)
358 Finished = IO->LineReader(IO);
360 Finished = StrBufChunkSipLine(IO->IOBuf,
364 case eMustReadMore: /// read new from socket...
366 case eBufferNotEmpty: /* shouldn't happen... */
367 case eReadSuccess: /// done for now...
369 case eReadFail: /// WHUT?
374 if (Finished != eMustReadMore) {
375 assert(IO->ReadDone);
376 ev_io_stop(event_base, &IO->recv_event);
377 IO->NextState = IO->ReadDone(IO);
378 Finished = StrBufCheckBuffer(&IO->RecvBuf);
389 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
392 AsyncIO *IO = watcher->data;
393 const char *errmsg = NULL;
395 IO->Now = ev_now(event_base);
396 become_session(IO->CitContext);
402 const char *pch = ChrPtr(IO->SendBuf.Buf);
403 const char *pchh = IO->SendBuf.ReadWritePointer;
409 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
410 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
411 ((CitContext*)(IO->CitContext))->ServiceName,
414 fd = fopen(fn, "a+");
415 fprintf(fd, "Send: BufSize: %ld BufContent: [",
417 rv = fwrite(pchh, nbytes, 1, fd);
418 if (!rv) printf("failed to write debug to %s!\n", fn);
421 switch (IO->NextState) {
423 rc = FileSendChunked(&IO->IOB, &errmsg);
425 StrBufPlain(IO->ErrMsg, errmsg, -1);
428 rc = StrBuf_write_one_chunk_callback(watcher->fd,
434 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
440 ev_io_stop(event_base, &IO->send_event);
441 switch (IO->NextState) {
443 assert(IO->SendDone);
444 IO->NextState = IO->SendDone(IO);
446 if ((IO->NextState == eTerminateConnection) ||
447 (IO->NextState == eAbort) )
450 ev_io_start(event_base, &IO->send_event);
454 if (IO->IOB.ChunkSendRemain > 0) {
455 ev_io_start(event_base, &IO->recv_event);
456 SetNextTimeout(IO, 100.0);
459 assert(IO->ReadDone);
460 IO->NextState = IO->ReadDone(IO);
461 switch(IO->NextState) {
470 ev_io_start(event_base,
478 case eTerminateConnection:
485 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
487 IO->NextState = eReadMore;
492 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
497 ev_io_start(event_base, &IO->recv_event);
503 * we now live in another queue,
504 * so we have to unregister.
506 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
511 case eTerminateConnection:
517 IO_Timeout_callback(loop, &IO->rw_timeout, revents);
519 /* else : must write more. */
522 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
524 switch(IO->NextState) {
528 ev_io_start(event_base, &IO->recv_event);
534 become_session(IO->CitContext);
535 IO_send_callback(loop, &IO->send_event, revents);
541 case eTerminateConnection:
549 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
551 AsyncIO *IO = watcher->data;
553 IO->Now = ev_now(event_base);
554 ev_timer_stop (event_base, &IO->rw_timeout);
555 become_session(IO->CitContext);
557 if (IO->SendBuf.fd != 0)
559 ev_io_stop(event_base, &IO->send_event);
560 ev_io_stop(event_base, &IO->recv_event);
561 ev_timer_stop (event_base, &IO->rw_timeout);
562 close(IO->SendBuf.fd);
563 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
567 switch (IO->Timeout(IO))
577 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
579 AsyncIO *IO = watcher->data;
581 IO->Now = ev_now(event_base);
582 ev_timer_stop (event_base, &IO->conn_fail);
584 if (IO->SendBuf.fd != 0)
586 ev_io_stop(loop, &IO->conn_event);
587 ev_io_stop(event_base, &IO->send_event);
588 ev_io_stop(event_base, &IO->recv_event);
589 ev_timer_stop (event_base, &IO->rw_timeout);
590 close(IO->SendBuf.fd);
591 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
593 become_session(IO->CitContext);
595 assert(IO->ConnFail);
596 switch (IO->ConnFail(IO))
607 IO_connfailimmediate_callback(struct ev_loop *loop,
611 AsyncIO *IO = watcher->data;
613 IO->Now = ev_now(event_base);
614 ev_idle_stop (event_base, &IO->conn_fail_immediate);
616 if (IO->SendBuf.fd != 0)
618 close(IO->SendBuf.fd);
619 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
621 become_session(IO->CitContext);
623 assert(IO->ConnFail);
624 switch (IO->ConnFail(IO))
635 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
637 AsyncIO *IO = watcher->data;
639 IO->Now = ev_now(event_base);
640 ev_io_stop(loop, &IO->conn_event);
641 ev_timer_stop (event_base, &IO->conn_fail);
642 set_start_callback(loop, IO, revents);
645 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
649 AsyncIO *IO = watcher->data;
651 IO->Now = ev_now(event_base);
652 switch (IO->NextState) {
654 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
656 StrBufPlain(IO->ErrMsg, errmsg, -1);
659 if (IO->IOB.ChunkSendRemain == 0)
661 IO->NextState = eSendReply;
662 assert(IO->ReadDone);
663 ev_io_stop(event_base, &IO->recv_event);
672 nbytes = StrBuf_read_one_chunk_callback(watcher->fd,
684 const char *pch = ChrPtr(IO->RecvBuf.Buf);
685 const char *pchh = IO->RecvBuf.ReadWritePointer;
690 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
691 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
692 ((CitContext*)(IO->CitContext))->ServiceName,
695 fd = fopen(fn, "a+");
696 fprintf(fd, "Read: BufSize: %ld BufContent: [",
698 rv = fwrite(pchh, nbytes, 1, fd);
699 if (!rv) printf("failed to write debug to %s!\n", fn);
706 } else if (nbytes == 0) {
707 IO_Timeout_callback(loop, &IO->rw_timeout, revents);
709 } else if (nbytes == -1) {
710 // FD is gone. kick it.
711 StopClientWatchers(IO);
713 "EVENT: Socket Invalid! %s \n",
721 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
723 AsyncIO *IO = watcher->data;
724 IO->Now = ev_now(event_base);
725 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
726 become_session(IO->CitContext);
727 assert(IO->DNS.Query->PostDNS);
728 switch (IO->DNS.Query->PostDNS(IO))
731 assert(IO->DNS.Fail);
732 switch (IO->DNS.Fail(IO)) {
734 //// StopClientWatchers(IO);
745 eNextState EvConnectSock(AsyncIO *IO,
747 double first_rw_timeout,
750 struct sockaddr_in egress_sin;
754 become_session(IO->CitContext);
757 IO->NextState = eReadMessage;
760 IO->NextState = eSendReply;
763 IO->SendBuf.fd = IO->RecvBuf.fd =
765 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
769 if (IO->SendBuf.fd < 0) {
771 "EVENT: socket() failed: %s\n",
774 StrBufPrintf(IO->ErrMsg,
775 "Failed to create socket: %s",
777 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
780 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
783 "EVENT: unable to get socket flags! %s \n",
785 StrBufPrintf(IO->ErrMsg,
786 "Failed to get socket flags: %s",
788 close(IO->SendBuf.fd);
789 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
792 fdflags = fdflags | O_NONBLOCK;
793 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
796 "EVENT: unable to set socket nonblocking flags! %s \n",
798 StrBufPrintf(IO->ErrMsg,
799 "Failed to set socket flags: %s",
801 close(IO->SendBuf.fd);
802 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
805 /* TODO: maye we could use offsetof() to calc the position of data...
806 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
808 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
809 IO->recv_event.data = IO;
810 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
811 IO->send_event.data = IO;
813 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
814 IO->conn_fail.data = IO;
815 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
816 IO->rw_timeout.data = IO;
821 /* for debugging you may bypass it like this:
822 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
823 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
824 * inet_addr("127.0.0.1");
826 if (IO->ConnectMe->IPv6) {
827 rc = connect(IO->SendBuf.fd,
828 &IO->ConnectMe->Addr,
829 sizeof(struct sockaddr_in6));
832 /* If citserver is bound to a specific IP address on the host, make
833 * sure we use that address for outbound connections.
836 memset(&egress_sin, 0, sizeof(egress_sin));
837 egress_sin.sin_family = AF_INET;
838 if (!IsEmptyStr(config.c_ip_addr)) {
839 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
840 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
841 egress_sin.sin_addr.s_addr = INADDR_ANY;
844 /* If this bind fails, no problem; we can still use INADDR_ANY */
845 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
847 rc = connect(IO->SendBuf.fd,
848 (struct sockaddr_in *)&IO->ConnectMe->Addr,
849 sizeof(struct sockaddr_in));
853 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
854 set_start_callback(event_base, IO, 0);
855 ev_timer_start(event_base, &IO->rw_timeout);
856 return IO->NextState;
858 else if (errno == EINPROGRESS) {
859 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
861 ev_io_init(&IO->conn_event,
862 IO_connestd_callback,
866 IO->conn_event.data = IO;
868 ev_io_start(event_base, &IO->conn_event);
869 ev_timer_start(event_base, &IO->conn_fail);
870 return IO->NextState;
873 ev_idle_init(&IO->conn_fail_immediate,
874 IO_connfailimmediate_callback);
875 IO->conn_fail_immediate.data = IO;
876 ev_idle_start(event_base, &IO->conn_fail_immediate);
878 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
879 StrBufPrintf(IO->ErrMsg,
880 "Failed to connect: %s",
882 return IO->NextState;
884 return IO->NextState;
887 void SetNextTimeout(AsyncIO *IO, double timeout)
889 IO->rw_timeout.repeat = timeout;
890 ev_timer_again (event_base, &IO->rw_timeout);
894 eNextState ReAttachIO(AsyncIO *IO,
899 become_session(IO->CitContext);
900 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
902 IO->NextState = eReadMessage;
905 IO->NextState = eSendReply;
907 set_start_callback(event_base, IO, 0);
909 return IO->NextState;
912 void InitIOStruct(AsyncIO *IO,
914 eNextState NextState,
915 IO_LineReaderCallback LineReader,
916 IO_CallBack DNS_Fail,
917 IO_CallBack SendDone,
918 IO_CallBack ReadDone,
919 IO_CallBack Terminate,
920 IO_CallBack DBTerminate,
921 IO_CallBack ConnFail,
923 IO_CallBack ShutdownAbort)
927 IO->CitContext = CloneContext(CC);
928 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
930 IO->NextState = NextState;
932 IO->SendDone = SendDone;
933 IO->ReadDone = ReadDone;
934 IO->Terminate = Terminate;
935 IO->DBTerminate = DBTerminate;
936 IO->LineReader = LineReader;
937 IO->ConnFail = ConnFail;
938 IO->Timeout = Timeout;
939 IO->ShutdownAbort = ShutdownAbort;
941 IO->DNS.Fail = DNS_Fail;
943 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
944 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
945 IO->IOBuf = NewStrBuf();
947 "EVENT: Session lives at %p IO at %p \n",
952 extern int evcurl_init(AsyncIO *IO);
954 int InitcURLIOStruct(AsyncIO *IO,
957 IO_CallBack SendDone,
958 IO_CallBack Terminate,
959 IO_CallBack DBTerminate,
960 IO_CallBack ShutdownAbort)
964 IO->CitContext = CloneContext(CC);
965 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
967 IO->SendDone = SendDone;
968 IO->Terminate = Terminate;
969 IO->DBTerminate = DBTerminate;
970 IO->ShutdownAbort = ShutdownAbort;
972 strcpy(IO->HttpReq.errdesc, Desc);
975 return evcurl_init(IO);
979 void EV_backtrace(AsyncIO *IO)
981 #ifdef HAVE_BACKTRACE
982 void *stack_frames[50];
987 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
988 strings = backtrace_symbols(stack_frames, size);
989 for (i = 0; i < size; i++) {
990 if (strings != NULL) {
991 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
994 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1002 ev_tstamp ctdl_ev_now (void)
1004 return ev_now(event_base);