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>
51 #include <libcitadel.h>
54 #include "citserver.h"
61 #include "internet_addressing.h"
64 #include "clientsocket.h"
65 #include "locate_host.h"
66 #include "citadel_dirs.h"
68 #include "event_client.h"
70 static void IO_abort_shutdown_callback(struct ev_loop *loop,
74 AsyncIO *IO = watcher->data;
75 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
77 assert(IO->ShutdownAbort);
78 IO->ShutdownAbort(IO);
82 /*------------------------------------------------------------------------------
84 *----------------------------------------------------------------------------*/
85 extern int evdb_count;
86 extern pthread_mutex_t DBEventQueueMutex;
87 extern HashList *DBInboundEventQueue;
88 extern struct ev_loop *event_db;
89 extern ev_async DBAddJob;
90 extern ev_async DBExitEventLoop;
92 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
97 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
100 ev_cleanup_init(&IO->db_abort_by_shutdown,
101 IO_abort_shutdown_callback);
102 IO->db_abort_by_shutdown.data = IO;
103 ev_cleanup_start(event_db, &IO->db_abort_by_shutdown);
105 pthread_mutex_lock(&DBEventQueueMutex);
106 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
108 Put(DBInboundEventQueue, IKEY(i), h, NULL);
109 pthread_mutex_unlock(&DBEventQueueMutex);
111 ev_async_send (event_db, &DBAddJob);
112 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
116 void ShutDownDBCLient(AsyncIO *IO)
118 CitContext *Ctx =IO->CitContext;
121 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
122 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
124 assert(IO->Terminate);
129 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
131 AsyncIO *IO = watcher->data;
132 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
133 become_session(IO->CitContext);
135 ev_idle_stop(event_db, &IO->db_unwind_stack);
137 assert(IO->NextDBOperation);
138 switch (IO->NextDBOperation(IO))
152 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
154 case eTerminateConnection:
156 ev_idle_stop(event_db, &IO->db_unwind_stack);
157 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
158 ShutDownDBCLient(IO);
162 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
164 IO->NextDBOperation = CB;
165 ev_idle_init(&IO->db_unwind_stack,
167 IO->db_unwind_stack.data = IO;
168 ev_idle_start(event_db, &IO->db_unwind_stack);
172 /*------------------------------------------------------------------------------
174 *----------------------------------------------------------------------------*/
175 extern int evbase_count;
176 extern pthread_mutex_t EventQueueMutex;
177 extern HashList *InboundEventQueue;
178 extern struct ev_loop *event_base;
179 extern ev_async AddJob;
180 extern ev_async ExitEventLoop;
183 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
188 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
191 ev_cleanup_init(&IO->abort_by_shutdown,
192 IO_abort_shutdown_callback);
193 IO->abort_by_shutdown.data = IO;
194 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
196 pthread_mutex_lock(&EventQueueMutex);
197 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
199 Put(InboundEventQueue, IKEY(i), h, NULL);
200 pthread_mutex_unlock(&EventQueueMutex);
202 ev_async_send (event_base, &AddJob);
203 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
207 extern eNextState evcurl_handle_start(AsyncIO *IO);
209 eNextState QueueCurlContext(AsyncIO *IO)
214 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
216 h->EvAttch = evcurl_handle_start;
218 pthread_mutex_lock(&EventQueueMutex);
219 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
221 Put(InboundEventQueue, IKEY(i), h, NULL);
222 pthread_mutex_unlock(&EventQueueMutex);
224 ev_async_send (event_base, &AddJob);
225 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
229 void DestructCAres(AsyncIO *IO);
230 void FreeAsyncIOContents(AsyncIO *IO)
232 CitContext *Ctx = IO->CitContext;
234 FreeStrBuf(&IO->IOBuf);
235 FreeStrBuf(&IO->SendBuf.Buf);
236 FreeStrBuf(&IO->RecvBuf.Buf);
240 FreeURL(&IO->ConnectMe);
241 FreeStrBuf(&IO->HttpReq.ReplyData);
243 Ctx->state = CON_IDLE;
248 void StopClientWatchers(AsyncIO *IO)
250 ev_timer_stop (event_base, &IO->rw_timeout);
251 ev_timer_stop(event_base, &IO->conn_fail);
252 ev_idle_stop(event_base, &IO->unwind_stack);
254 ev_io_stop(event_base, &IO->conn_event);
255 ev_io_stop(event_base, &IO->send_event);
256 ev_io_stop(event_base, &IO->recv_event);
257 close(IO->SendBuf.fd);
262 void ShutDownCLient(AsyncIO *IO)
264 CitContext *Ctx =IO->CitContext;
267 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
269 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
270 StopClientWatchers(IO);
272 if (IO->DNS.Channel != NULL) {
273 ares_destroy(IO->DNS.Channel);
274 ev_io_stop(event_base, &IO->DNS.recv_event);
275 ev_io_stop(event_base, &IO->DNS.send_event);
276 IO->DNS.Channel = NULL;
278 assert(IO->Terminate);
283 eReadState HandleInbound(AsyncIO *IO)
285 const char *Err = NULL;
286 eReadState Finished = eBufferNotEmpty;
288 become_session(IO->CitContext);
290 while ((Finished == eBufferNotEmpty) &&
291 ((IO->NextState == eReadMessage)||
292 (IO->NextState == eReadMore)||
293 (IO->NextState == eReadFile)||
294 (IO->NextState == eReadPayload)))
297 * lex line reply in callback,
298 * or do it ourselves.
299 * i.e. as nnn-blabla means continue reading in SMTP
301 if ((IO->NextState == eReadFile) &&
302 (Finished == eBufferNotEmpty))
304 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
305 if (Finished == eReadSuccess)
307 IO->NextState = eSendReply;
310 else if (IO->LineReader)
311 Finished = IO->LineReader(IO);
313 Finished = StrBufChunkSipLine(IO->IOBuf,
317 case eMustReadMore: /// read new from socket...
319 case eBufferNotEmpty: /* shouldn't happen... */
320 case eReadSuccess: /// done for now...
322 case eReadFail: /// WHUT?
327 if (Finished != eMustReadMore) {
328 assert(IO->ReadDone);
329 ev_io_stop(event_base, &IO->recv_event);
330 IO->NextState = IO->ReadDone(IO);
331 Finished = StrBufCheckBuffer(&IO->RecvBuf);
335 switch (IO->NextState) {
337 ev_io_start(event_base, &IO->send_event);
341 assert(IO->SendDone);
342 IO->NextState = IO->SendDone(IO);
343 ev_io_start(event_base, &IO->send_event);
348 ev_io_start(event_base, &IO->recv_event);
350 case eTerminateConnection:
368 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
371 AsyncIO *IO = watcher->data;
372 const char *errmsg = NULL;
374 become_session(IO->CitContext);
380 const char *pch = ChrPtr(IO->SendBuf.Buf);
381 const char *pchh = IO->SendBuf.ReadWritePointer;
387 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
388 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
389 ((CitContext*)(IO->CitContext))->ServiceName,
392 fd = fopen(fn, "a+");
393 fprintf(fd, "Send: BufSize: %ld BufContent: [",
395 rv = fwrite(pchh, nbytes, 1, fd);
396 if (!rv) printf("failed to write debug to %s!\n", fn);
399 switch (IO->NextState) {
401 rc = FileSendChunked(&IO->IOB, &errmsg);
403 StrBufPlain(IO->ErrMsg, errmsg, -1);
406 rc = StrBuf_write_one_chunk_callback(watcher->fd,
412 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
418 ev_io_stop(event_base, &IO->send_event);
419 switch (IO->NextState) {
421 assert(IO->SendDone);
422 IO->NextState = IO->SendDone(IO);
424 if ((IO->NextState == eTerminateConnection) ||
425 (IO->NextState == eAbort) )
428 ev_io_start(event_base, &IO->send_event);
432 if (IO->IOB.ChunkSendRemain > 0) {
433 ev_io_start(event_base, &IO->recv_event);
435 assert(IO->ReadDone);
436 IO->NextState = IO->ReadDone(IO);
437 switch(IO->NextState) {
446 ev_io_start(event_base,
454 case eTerminateConnection:
461 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
463 IO->NextState = eReadMore;
468 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
473 ev_io_start(event_base, &IO->recv_event);
479 * we now live in another queue,
480 * so we have to unregister.
482 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
487 case eTerminateConnection:
496 /* else : must write more. */
499 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
501 switch(IO->NextState) {
505 ev_io_start(event_base, &IO->recv_event);
511 become_session(IO->CitContext);
512 IO_send_callback(loop, &IO->send_event, revents);
518 case eTerminateConnection:
526 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
528 AsyncIO *IO = watcher->data;
530 ev_timer_stop (event_base, &IO->rw_timeout);
531 become_session(IO->CitContext);
533 if (IO->SendBuf.fd != 0)
535 ev_io_stop(event_base, &IO->send_event);
536 ev_io_stop(event_base, &IO->recv_event);
537 ev_timer_stop (event_base, &IO->rw_timeout);
538 close(IO->SendBuf.fd);
539 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
543 switch (IO->Timeout(IO))
553 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
555 AsyncIO *IO = watcher->data;
557 ev_timer_stop (event_base, &IO->conn_fail);
559 if (IO->SendBuf.fd != 0)
561 ev_io_stop(loop, &IO->conn_event);
562 ev_io_stop(event_base, &IO->send_event);
563 ev_io_stop(event_base, &IO->recv_event);
564 ev_timer_stop (event_base, &IO->rw_timeout);
565 close(IO->SendBuf.fd);
566 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
568 become_session(IO->CitContext);
570 assert(IO->ConnFail);
571 switch (IO->ConnFail(IO))
582 IO_connfailimmediate_callback(struct ev_loop *loop,
586 AsyncIO *IO = watcher->data;
588 ev_idle_stop (event_base, &IO->conn_fail_immediate);
590 if (IO->SendBuf.fd != 0)
592 close(IO->SendBuf.fd);
593 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
595 become_session(IO->CitContext);
597 assert(IO->ConnFail);
598 switch (IO->ConnFail(IO))
609 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
611 AsyncIO *IO = watcher->data;
613 ev_io_stop(loop, &IO->conn_event);
614 ev_timer_stop (event_base, &IO->conn_fail);
615 set_start_callback(loop, IO, revents);
618 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
622 AsyncIO *IO = watcher->data;
624 switch (IO->NextState) {
626 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
628 StrBufPlain(IO->ErrMsg, errmsg, -1);
631 if (IO->IOB.ChunkSendRemain == 0)
633 IO->NextState = eSendReply;
640 nbytes = StrBuf_read_one_chunk_callback(watcher->fd,
652 const char *pch = ChrPtr(IO->RecvBuf.Buf);
653 const char *pchh = IO->RecvBuf.ReadWritePointer;
658 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
659 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
660 ((CitContext*)(IO->CitContext))->ServiceName,
663 fd = fopen(fn, "a+");
664 fprintf(fd, "Read: BufSize: %ld BufContent: [",
666 rv = fwrite(pchh, nbytes, 1, fd);
667 if (!rv) printf("failed to write debug to %s!\n", fn);
674 } else if (nbytes == 0) {
677 switch (IO->Timeout(IO))
685 } else if (nbytes == -1) {
686 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
688 "EVENT: Socket Invalid! %s \n",
696 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
698 AsyncIO *IO = watcher->data;
699 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
700 become_session(IO->CitContext);
701 assert(IO->DNS.Fail);
702 assert(IO->DNS.Query->PostDNS);
703 switch (IO->DNS.Query->PostDNS(IO))
706 switch (IO->DNS.Fail(IO)) {
718 eNextState EvConnectSock(AsyncIO *IO,
720 double first_rw_timeout,
726 become_session(IO->CitContext);
729 IO->NextState = eReadMessage;
732 IO->NextState = eSendReply;
735 IO->SendBuf.fd = IO->RecvBuf.fd =
737 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
741 if (IO->SendBuf.fd < 0) {
743 "EVENT: socket() failed: %s\n",
746 StrBufPrintf(IO->ErrMsg,
747 "Failed to create socket: %s",
751 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
754 "EVENT: unable to get socket flags! %s \n",
756 StrBufPrintf(IO->ErrMsg,
757 "Failed to get socket flags: %s",
761 fdflags = fdflags | O_NONBLOCK;
762 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
765 "EVENT: unable to set socket nonblocking flags! %s \n",
767 StrBufPrintf(IO->ErrMsg,
768 "Failed to set socket flags: %s",
770 close(IO->SendBuf.fd);
771 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
774 /* TODO: maye we could use offsetof() to calc the position of data...
775 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
777 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
778 IO->recv_event.data = IO;
779 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
780 IO->send_event.data = IO;
782 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
783 IO->conn_fail.data = IO;
784 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
785 IO->rw_timeout.data = IO;
788 /* for debugging you may bypass it like this:
789 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
790 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
791 * inet_addr("127.0.0.1");
793 if (IO->ConnectMe->IPv6)
794 rc = connect(IO->SendBuf.fd,
795 &IO->ConnectMe->Addr,
796 sizeof(struct sockaddr_in6));
798 rc = connect(IO->SendBuf.fd,
799 (struct sockaddr_in *)&IO->ConnectMe->Addr,
800 sizeof(struct sockaddr_in));
803 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
804 set_start_callback(event_base, IO, 0);
805 ev_timer_start(event_base, &IO->rw_timeout);
806 return IO->NextState;
808 else if (errno == EINPROGRESS) {
809 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
811 ev_io_init(&IO->conn_event,
812 IO_connestd_callback,
816 IO->conn_event.data = IO;
818 ev_io_start(event_base, &IO->conn_event);
819 ev_timer_start(event_base, &IO->conn_fail);
820 return IO->NextState;
823 ev_idle_init(&IO->conn_fail_immediate,
824 IO_connfailimmediate_callback);
825 IO->conn_fail_immediate.data = IO;
826 ev_idle_start(event_base, &IO->conn_fail_immediate);
828 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
829 StrBufPrintf(IO->ErrMsg,
830 "Failed to connect: %s",
832 return IO->NextState;
834 return IO->NextState;
837 void SetNextTimeout(AsyncIO *IO, double timeout)
839 IO->rw_timeout.repeat = timeout;
840 ev_timer_again (event_base, &IO->rw_timeout);
844 eNextState ReAttachIO(AsyncIO *IO,
849 become_session(IO->CitContext);
850 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
852 IO->NextState = eReadMessage;
855 IO->NextState = eSendReply;
857 set_start_callback(event_base, IO, 0);
859 return IO->NextState;
862 void InitIOStruct(AsyncIO *IO,
864 eNextState NextState,
865 IO_LineReaderCallback LineReader,
866 IO_CallBack DNS_Fail,
867 IO_CallBack SendDone,
868 IO_CallBack ReadDone,
869 IO_CallBack Terminate,
870 IO_CallBack ConnFail,
872 IO_CallBack ShutdownAbort)
876 IO->CitContext = CloneContext(CC);
877 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
879 IO->NextState = NextState;
881 IO->SendDone = SendDone;
882 IO->ReadDone = ReadDone;
883 IO->Terminate = Terminate;
884 IO->LineReader = LineReader;
885 IO->ConnFail = ConnFail;
886 IO->Timeout = Timeout;
887 IO->ShutdownAbort = ShutdownAbort;
889 IO->DNS.Fail = DNS_Fail;
891 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
892 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
893 IO->IOBuf = NewStrBuf();
897 extern int evcurl_init(AsyncIO *IO);
899 int InitcURLIOStruct(AsyncIO *IO,
902 IO_CallBack SendDone,
903 IO_CallBack Terminate,
904 IO_CallBack ShutdownAbort)
908 IO->CitContext = CloneContext(CC);
909 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
911 IO->SendDone = SendDone;
912 IO->Terminate = Terminate;
913 IO->ShutdownAbort = ShutdownAbort;
915 strcpy(IO->HttpReq.errdesc, Desc);
918 return evcurl_init(IO);