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"
73 static void IO_abort_shutdown_callback(struct ev_loop *loop,
77 AsyncIO *IO = watcher->data;
78 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
80 assert(IO->ShutdownAbort);
81 IO->ShutdownAbort(IO);
85 /*------------------------------------------------------------------------------
87 *----------------------------------------------------------------------------*/
88 extern int evdb_count;
89 extern pthread_mutex_t DBEventQueueMutex;
90 extern HashList *DBInboundEventQueue;
91 extern struct ev_loop *event_db;
92 extern ev_async DBAddJob;
93 extern ev_async DBExitEventLoop;
95 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
100 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
103 ev_cleanup_init(&IO->db_abort_by_shutdown,
104 IO_abort_shutdown_callback);
105 IO->db_abort_by_shutdown.data = IO;
106 ev_cleanup_start(event_db, &IO->db_abort_by_shutdown);
108 pthread_mutex_lock(&DBEventQueueMutex);
109 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
111 Put(DBInboundEventQueue, IKEY(i), h, NULL);
112 pthread_mutex_unlock(&DBEventQueueMutex);
114 ev_async_send (event_db, &DBAddJob);
115 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
119 void ShutDownDBCLient(AsyncIO *IO)
121 CitContext *Ctx =IO->CitContext;
124 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
125 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
127 assert(IO->Terminate);
132 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
134 AsyncIO *IO = watcher->data;
135 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
136 become_session(IO->CitContext);
138 ev_idle_stop(event_db, &IO->db_unwind_stack);
140 assert(IO->NextDBOperation);
141 switch (IO->NextDBOperation(IO))
155 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
157 case eTerminateConnection:
159 ev_idle_stop(event_db, &IO->db_unwind_stack);
160 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
161 ShutDownDBCLient(IO);
165 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
167 IO->NextDBOperation = CB;
168 ev_idle_init(&IO->db_unwind_stack,
170 IO->db_unwind_stack.data = IO;
171 ev_idle_start(event_db, &IO->db_unwind_stack);
175 /*------------------------------------------------------------------------------
177 *----------------------------------------------------------------------------*/
178 extern int evbase_count;
179 extern pthread_mutex_t EventQueueMutex;
180 extern HashList *InboundEventQueue;
181 extern struct ev_loop *event_base;
182 extern ev_async AddJob;
183 extern ev_async ExitEventLoop;
186 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
191 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
194 ev_cleanup_init(&IO->abort_by_shutdown,
195 IO_abort_shutdown_callback);
196 IO->abort_by_shutdown.data = IO;
197 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
199 pthread_mutex_lock(&EventQueueMutex);
200 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
202 Put(InboundEventQueue, IKEY(i), h, NULL);
203 pthread_mutex_unlock(&EventQueueMutex);
205 ev_async_send (event_base, &AddJob);
206 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
210 extern eNextState evcurl_handle_start(AsyncIO *IO);
212 eNextState QueueCurlContext(AsyncIO *IO)
217 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
219 h->EvAttch = evcurl_handle_start;
221 pthread_mutex_lock(&EventQueueMutex);
222 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
224 Put(InboundEventQueue, IKEY(i), h, NULL);
225 pthread_mutex_unlock(&EventQueueMutex);
227 ev_async_send (event_base, &AddJob);
228 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
232 void DestructCAres(AsyncIO *IO);
233 void FreeAsyncIOContents(AsyncIO *IO)
235 CitContext *Ctx = IO->CitContext;
237 FreeStrBuf(&IO->IOBuf);
238 FreeStrBuf(&IO->SendBuf.Buf);
239 FreeStrBuf(&IO->RecvBuf.Buf);
243 FreeURL(&IO->ConnectMe);
244 FreeStrBuf(&IO->HttpReq.ReplyData);
247 Ctx->state = CON_IDLE;
253 void StopClientWatchers(AsyncIO *IO)
255 ev_timer_stop (event_base, &IO->rw_timeout);
256 ev_timer_stop(event_base, &IO->conn_fail);
257 ev_idle_stop(event_base, &IO->unwind_stack);
259 ev_io_stop(event_base, &IO->conn_event);
260 ev_io_stop(event_base, &IO->send_event);
261 ev_io_stop(event_base, &IO->recv_event);
263 if (IO->SendBuf.fd != 0) {
264 close(IO->SendBuf.fd);
270 void ShutDownCLient(AsyncIO *IO)
272 CitContext *Ctx =IO->CitContext;
275 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
277 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
278 StopClientWatchers(IO);
280 if (IO->DNS.Channel != NULL) {
281 ares_destroy(IO->DNS.Channel);
282 EV_DNS_LOG_STOP(DNS.recv_event);
283 EV_DNS_LOG_STOP(DNS.send_event);
284 ev_io_stop(event_base, &IO->DNS.recv_event);
285 ev_io_stop(event_base, &IO->DNS.send_event);
286 IO->DNS.Channel = NULL;
288 assert(IO->Terminate);
292 eReadState HandleInbound(AsyncIO *IO)
294 const char *Err = NULL;
295 eReadState Finished = eBufferNotEmpty;
297 become_session(IO->CitContext);
299 while ((Finished == eBufferNotEmpty) &&
300 ((IO->NextState == eReadMessage)||
301 (IO->NextState == eReadMore)||
302 (IO->NextState == eReadFile)||
303 (IO->NextState == eReadPayload)))
306 * lex line reply in callback,
307 * or do it ourselves.
308 * i.e. as nnn-blabla means continue reading in SMTP
310 if ((IO->NextState == eReadFile) &&
311 (Finished == eBufferNotEmpty))
313 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
314 if (Finished == eReadSuccess)
316 IO->NextState = eSendReply;
319 else if (IO->LineReader)
320 Finished = IO->LineReader(IO);
322 Finished = StrBufChunkSipLine(IO->IOBuf,
326 case eMustReadMore: /// read new from socket...
328 case eBufferNotEmpty: /* shouldn't happen... */
329 case eReadSuccess: /// done for now...
331 case eReadFail: /// WHUT?
336 if (Finished != eMustReadMore) {
337 assert(IO->ReadDone);
338 ev_io_stop(event_base, &IO->recv_event);
339 IO->NextState = IO->ReadDone(IO);
340 Finished = StrBufCheckBuffer(&IO->RecvBuf);
344 switch (IO->NextState) {
346 ev_io_start(event_base, &IO->send_event);
350 assert(IO->SendDone);
351 IO->NextState = IO->SendDone(IO);
352 ev_io_start(event_base, &IO->send_event);
357 ev_io_start(event_base, &IO->recv_event);
359 case eTerminateConnection:
377 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
380 AsyncIO *IO = watcher->data;
381 const char *errmsg = NULL;
383 become_session(IO->CitContext);
389 const char *pch = ChrPtr(IO->SendBuf.Buf);
390 const char *pchh = IO->SendBuf.ReadWritePointer;
396 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
397 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
398 ((CitContext*)(IO->CitContext))->ServiceName,
401 fd = fopen(fn, "a+");
402 fprintf(fd, "Send: BufSize: %ld BufContent: [",
404 rv = fwrite(pchh, nbytes, 1, fd);
405 if (!rv) printf("failed to write debug to %s!\n", fn);
408 switch (IO->NextState) {
410 rc = FileSendChunked(&IO->IOB, &errmsg);
412 StrBufPlain(IO->ErrMsg, errmsg, -1);
415 rc = StrBuf_write_one_chunk_callback(watcher->fd,
421 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
427 ev_io_stop(event_base, &IO->send_event);
428 switch (IO->NextState) {
430 assert(IO->SendDone);
431 IO->NextState = IO->SendDone(IO);
433 if ((IO->NextState == eTerminateConnection) ||
434 (IO->NextState == eAbort) )
437 ev_io_start(event_base, &IO->send_event);
441 if (IO->IOB.ChunkSendRemain > 0) {
442 ev_io_start(event_base, &IO->recv_event);
444 assert(IO->ReadDone);
445 IO->NextState = IO->ReadDone(IO);
446 switch(IO->NextState) {
455 ev_io_start(event_base,
463 case eTerminateConnection:
470 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
472 IO->NextState = eReadMore;
477 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
482 ev_io_start(event_base, &IO->recv_event);
488 * we now live in another queue,
489 * so we have to unregister.
491 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
496 case eTerminateConnection:
505 /* else : must write more. */
508 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
510 switch(IO->NextState) {
514 ev_io_start(event_base, &IO->recv_event);
520 become_session(IO->CitContext);
521 IO_send_callback(loop, &IO->send_event, revents);
527 case eTerminateConnection:
535 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
537 AsyncIO *IO = watcher->data;
539 ev_timer_stop (event_base, &IO->rw_timeout);
540 become_session(IO->CitContext);
542 if (IO->SendBuf.fd != 0)
544 ev_io_stop(event_base, &IO->send_event);
545 ev_io_stop(event_base, &IO->recv_event);
546 ev_timer_stop (event_base, &IO->rw_timeout);
547 close(IO->SendBuf.fd);
548 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
552 switch (IO->Timeout(IO))
562 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
564 AsyncIO *IO = watcher->data;
566 ev_timer_stop (event_base, &IO->conn_fail);
568 if (IO->SendBuf.fd != 0)
570 ev_io_stop(loop, &IO->conn_event);
571 ev_io_stop(event_base, &IO->send_event);
572 ev_io_stop(event_base, &IO->recv_event);
573 ev_timer_stop (event_base, &IO->rw_timeout);
574 close(IO->SendBuf.fd);
575 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
577 become_session(IO->CitContext);
579 assert(IO->ConnFail);
580 switch (IO->ConnFail(IO))
591 IO_connfailimmediate_callback(struct ev_loop *loop,
595 AsyncIO *IO = watcher->data;
597 ev_idle_stop (event_base, &IO->conn_fail_immediate);
599 if (IO->SendBuf.fd != 0)
601 close(IO->SendBuf.fd);
602 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
604 become_session(IO->CitContext);
606 assert(IO->ConnFail);
607 switch (IO->ConnFail(IO))
618 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
620 AsyncIO *IO = watcher->data;
622 ev_io_stop(loop, &IO->conn_event);
623 ev_timer_stop (event_base, &IO->conn_fail);
624 set_start_callback(loop, IO, revents);
627 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
631 AsyncIO *IO = watcher->data;
633 switch (IO->NextState) {
635 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
637 StrBufPlain(IO->ErrMsg, errmsg, -1);
640 if (IO->IOB.ChunkSendRemain == 0)
642 IO->NextState = eSendReply;
649 nbytes = StrBuf_read_one_chunk_callback(watcher->fd,
661 const char *pch = ChrPtr(IO->RecvBuf.Buf);
662 const char *pchh = IO->RecvBuf.ReadWritePointer;
667 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
668 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
669 ((CitContext*)(IO->CitContext))->ServiceName,
672 fd = fopen(fn, "a+");
673 fprintf(fd, "Read: BufSize: %ld BufContent: [",
675 rv = fwrite(pchh, nbytes, 1, fd);
676 if (!rv) printf("failed to write debug to %s!\n", fn);
683 } else if (nbytes == 0) {
686 switch (IO->Timeout(IO))
694 } else if (nbytes == -1) {
695 // FD is gone. kick it.
696 StopClientWatchers(IO);
698 "EVENT: Socket Invalid! %s \n",
706 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
708 AsyncIO *IO = watcher->data;
709 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
710 become_session(IO->CitContext);
711 assert(IO->DNS.Fail);
712 assert(IO->DNS.Query->PostDNS);
713 switch (IO->DNS.Query->PostDNS(IO))
716 switch (IO->DNS.Fail(IO)) {
718 //// StopClientWatchers(IO);
729 eNextState EvConnectSock(AsyncIO *IO,
731 double first_rw_timeout,
737 become_session(IO->CitContext);
740 IO->NextState = eReadMessage;
743 IO->NextState = eSendReply;
746 IO->SendBuf.fd = IO->RecvBuf.fd =
748 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
752 if (IO->SendBuf.fd < 0) {
754 "EVENT: socket() failed: %s\n",
757 StrBufPrintf(IO->ErrMsg,
758 "Failed to create socket: %s",
760 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
763 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
766 "EVENT: unable to get socket flags! %s \n",
768 StrBufPrintf(IO->ErrMsg,
769 "Failed to get socket flags: %s",
771 close(IO->SendBuf.fd);
772 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
775 fdflags = fdflags | O_NONBLOCK;
776 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
779 "EVENT: unable to set socket nonblocking flags! %s \n",
781 StrBufPrintf(IO->ErrMsg,
782 "Failed to set socket flags: %s",
784 close(IO->SendBuf.fd);
785 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
788 /* TODO: maye we could use offsetof() to calc the position of data...
789 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
791 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
792 IO->recv_event.data = IO;
793 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
794 IO->send_event.data = IO;
796 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
797 IO->conn_fail.data = IO;
798 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
799 IO->rw_timeout.data = IO;
802 /* for debugging you may bypass it like this:
803 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
804 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
805 * inet_addr("127.0.0.1");
807 if (IO->ConnectMe->IPv6)
808 rc = connect(IO->SendBuf.fd,
809 &IO->ConnectMe->Addr,
810 sizeof(struct sockaddr_in6));
812 rc = connect(IO->SendBuf.fd,
813 (struct sockaddr_in *)&IO->ConnectMe->Addr,
814 sizeof(struct sockaddr_in));
817 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
818 set_start_callback(event_base, IO, 0);
819 ev_timer_start(event_base, &IO->rw_timeout);
820 return IO->NextState;
822 else if (errno == EINPROGRESS) {
823 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
825 ev_io_init(&IO->conn_event,
826 IO_connestd_callback,
830 IO->conn_event.data = IO;
832 ev_io_start(event_base, &IO->conn_event);
833 ev_timer_start(event_base, &IO->conn_fail);
834 return IO->NextState;
837 ev_idle_init(&IO->conn_fail_immediate,
838 IO_connfailimmediate_callback);
839 IO->conn_fail_immediate.data = IO;
840 ev_idle_start(event_base, &IO->conn_fail_immediate);
842 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
843 StrBufPrintf(IO->ErrMsg,
844 "Failed to connect: %s",
846 return IO->NextState;
848 return IO->NextState;
851 void SetNextTimeout(AsyncIO *IO, double timeout)
853 IO->rw_timeout.repeat = timeout;
854 ev_timer_again (event_base, &IO->rw_timeout);
858 eNextState ReAttachIO(AsyncIO *IO,
863 become_session(IO->CitContext);
864 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
866 IO->NextState = eReadMessage;
869 IO->NextState = eSendReply;
871 set_start_callback(event_base, IO, 0);
873 return IO->NextState;
876 void InitIOStruct(AsyncIO *IO,
878 eNextState NextState,
879 IO_LineReaderCallback LineReader,
880 IO_CallBack DNS_Fail,
881 IO_CallBack SendDone,
882 IO_CallBack ReadDone,
883 IO_CallBack Terminate,
884 IO_CallBack ConnFail,
886 IO_CallBack ShutdownAbort)
890 IO->CitContext = CloneContext(CC);
891 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
893 IO->NextState = NextState;
895 IO->SendDone = SendDone;
896 IO->ReadDone = ReadDone;
897 IO->Terminate = Terminate;
898 IO->LineReader = LineReader;
899 IO->ConnFail = ConnFail;
900 IO->Timeout = Timeout;
901 IO->ShutdownAbort = ShutdownAbort;
903 IO->DNS.Fail = DNS_Fail;
905 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
906 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
907 IO->IOBuf = NewStrBuf();
909 "EVENT: Session lives at %p IO at %p \n",
914 extern int evcurl_init(AsyncIO *IO);
916 int InitcURLIOStruct(AsyncIO *IO,
919 IO_CallBack SendDone,
920 IO_CallBack Terminate,
921 IO_CallBack ShutdownAbort)
925 IO->CitContext = CloneContext(CC);
926 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
928 IO->SendDone = SendDone;
929 IO->Terminate = Terminate;
930 IO->ShutdownAbort = ShutdownAbort;
932 strcpy(IO->HttpReq.errdesc, Desc);
935 return evcurl_init(IO);
939 void EV_backtrace(AsyncIO *IO)
941 #ifdef HAVE_BACKTRACE
942 void *stack_frames[50];
947 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
948 strings = backtrace_symbols(stack_frames, size);
949 for (i = 0; i < size; i++) {
951 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
953 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);