3 * Copyright (c) 1998-2009 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, ev_cleanup *watcher, int revents)
72 AsyncIO *IO = watcher->data;
73 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
75 assert(IO->ShutdownAbort);
76 IO->ShutdownAbort(IO);
80 /*--------------------------------------------------------------------------------
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->Terminate);
127 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
129 AsyncIO *IO = watcher->data;
130 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
131 become_session(IO->CitContext);
133 ev_idle_stop(event_db, &IO->db_unwind_stack);
135 assert(IO->NextDBOperation);
136 switch (IO->NextDBOperation(IO))
150 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
152 case eTerminateConnection:
154 ev_idle_stop(event_db, &IO->db_unwind_stack);
155 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
156 ShutDownDBCLient(IO);
160 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
162 IO->NextDBOperation = CB;
163 ev_idle_init(&IO->db_unwind_stack,
165 IO->db_unwind_stack.data = IO;
166 ev_idle_start(event_db, &IO->db_unwind_stack);
170 /*--------------------------------------------------------------------------------
173 extern int evbase_count;
174 extern pthread_mutex_t EventQueueMutex;
175 extern HashList *InboundEventQueue;
176 extern struct ev_loop *event_base;
177 extern ev_async AddJob;
178 extern ev_async ExitEventLoop;
181 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
186 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
189 ev_cleanup_init(&IO->abort_by_shutdown,
190 IO_abort_shutdown_callback);
191 IO->abort_by_shutdown.data = IO;
192 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
194 pthread_mutex_lock(&EventQueueMutex);
195 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
197 Put(InboundEventQueue, IKEY(i), h, NULL);
198 pthread_mutex_unlock(&EventQueueMutex);
200 ev_async_send (event_base, &AddJob);
201 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
205 extern eNextState evcurl_handle_start(AsyncIO *IO);
207 eNextState QueueCurlContext(AsyncIO *IO)
212 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
214 h->EvAttch = evcurl_handle_start;
216 pthread_mutex_lock(&EventQueueMutex);
217 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
219 Put(InboundEventQueue, IKEY(i), h, NULL);
220 pthread_mutex_unlock(&EventQueueMutex);
222 ev_async_send (event_base, &AddJob);
223 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
227 void DestructCAres(AsyncIO *IO);
228 void FreeAsyncIOContents(AsyncIO *IO)
230 CitContext *Ctx = IO->CitContext;
232 FreeStrBuf(&IO->IOBuf);
233 FreeStrBuf(&IO->SendBuf.Buf);
234 FreeStrBuf(&IO->RecvBuf.Buf);
238 FreeURL(&IO->ConnectMe);
239 FreeStrBuf(&IO->HttpReq.ReplyData);
241 Ctx->state = CON_IDLE;
246 void StopClientWatchers(AsyncIO *IO)
248 ev_timer_stop (event_base, &IO->rw_timeout);
249 ev_timer_stop(event_base, &IO->conn_fail);
250 ev_idle_stop(event_base, &IO->unwind_stack);
252 ev_io_stop(event_base, &IO->conn_event);
253 ev_io_stop(event_base, &IO->send_event);
254 ev_io_stop(event_base, &IO->recv_event);
255 close(IO->SendBuf.fd);
260 void ShutDownCLient(AsyncIO *IO)
262 CitContext *Ctx =IO->CitContext;
265 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
267 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
268 StopClientWatchers(IO);
270 if (IO->DNS.Channel != NULL) {
271 ares_destroy(IO->DNS.Channel);
272 ev_io_stop(event_base, &IO->DNS.recv_event);
273 ev_io_stop(event_base, &IO->DNS.send_event);
274 IO->DNS.Channel = NULL;
276 assert(IO->Terminate);
281 eReadState HandleInbound(AsyncIO *IO)
283 const char *Err = NULL;
284 eReadState Finished = eBufferNotEmpty;
286 become_session(IO->CitContext);
288 while ((Finished == eBufferNotEmpty) &&
289 ((IO->NextState == eReadMessage)||
290 (IO->NextState == eReadMore)||
291 (IO->NextState == eReadFile)||
292 (IO->NextState == eReadPayload)))
294 if (IO->RecvBuf.nBlobBytesWanted != 0) {
297 else { /* Reading lines... */
298 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
299 if ((IO->NextState == eReadFile) &&
300 (Finished == eBufferNotEmpty))
302 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
303 if (Finished == eReadSuccess)
305 IO->NextState = eSendReply;
308 else if (IO->LineReader)
309 Finished = IO->LineReader(IO);
311 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
314 case eMustReadMore: /// read new from socket...
316 case eBufferNotEmpty: /* shouldn't happen... */
317 case eReadSuccess: /// done for now...
319 case eReadFail: /// WHUT?
326 if (Finished != eMustReadMore) {
327 assert(IO->ReadDone);
328 ev_io_stop(event_base, &IO->recv_event);
329 IO->NextState = IO->ReadDone(IO);
330 Finished = StrBufCheckBuffer(&IO->RecvBuf);
334 switch (IO->NextState) {
336 ev_io_start(event_base, &IO->send_event);
340 assert(IO->SendDone);
341 IO->NextState = IO->SendDone(IO);
342 ev_io_start(event_base, &IO->send_event);
347 ev_io_start(event_base, &IO->recv_event);
349 case eTerminateConnection:
367 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
370 AsyncIO *IO = watcher->data;
371 const char *errmsg = NULL;
373 become_session(IO->CitContext);
379 const char *pch = ChrPtr(IO->SendBuf.Buf);
380 const char *pchh = IO->SendBuf.ReadWritePointer;
386 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
387 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
388 ((CitContext*)(IO->CitContext))->ServiceName,
391 fd = fopen(fn, "a+");
392 fprintf(fd, "Send: BufSize: %ld BufContent: [",
394 rv = fwrite(pchh, nbytes, 1, fd);
395 if (!rv) printf("failed to write debug to %s!\n", fn);
398 switch (IO->NextState) {
400 rc = FileSendChunked(&IO->IOB, &errmsg);
402 StrBufPlain(IO->ErrMsg, errmsg, -1);
405 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
409 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
415 ev_io_stop(event_base, &IO->send_event);
416 switch (IO->NextState) {
418 assert(IO->SendDone);
419 IO->NextState = IO->SendDone(IO);
421 if ((IO->NextState == eTerminateConnection) ||
422 (IO->NextState == eAbort) )
425 ev_io_start(event_base, &IO->send_event);
429 if (IO->IOB.ChunkSendRemain > 0) {
430 ev_io_start(event_base, &IO->recv_event);
432 assert(IO->ReadDone);
433 IO->NextState = IO->ReadDone(IO);
434 switch(IO->NextState) {
443 ev_io_start(event_base, &IO->send_event);
450 case eTerminateConnection:
457 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
459 IO->NextState = eReadMore;
464 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
468 ev_io_start(event_base, &IO->recv_event);
473 /* we now live in another queue, so we have to unregister. */
474 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
479 case eTerminateConnection:
488 /* else : must write more. */
491 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
494 switch(IO->NextState) {
498 ev_io_start(event_base, &IO->recv_event);
504 become_session(IO->CitContext);
505 IO_send_callback(loop, &IO->send_event, revents);
511 case eTerminateConnection:
519 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
521 AsyncIO *IO = watcher->data;
523 ev_timer_stop (event_base, &IO->rw_timeout);
524 become_session(IO->CitContext);
526 if (IO->SendBuf.fd != 0)
528 ev_io_stop(event_base, &IO->send_event);
529 ev_io_stop(event_base, &IO->recv_event);
530 ev_timer_stop (event_base, &IO->rw_timeout);
531 close(IO->SendBuf.fd);
532 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
536 switch (IO->Timeout(IO))
546 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
548 AsyncIO *IO = watcher->data;
550 ev_timer_stop (event_base, &IO->conn_fail);
552 if (IO->SendBuf.fd != 0)
554 ev_io_stop(loop, &IO->conn_event);
555 ev_io_stop(event_base, &IO->send_event);
556 ev_io_stop(event_base, &IO->recv_event);
557 ev_timer_stop (event_base, &IO->rw_timeout);
558 close(IO->SendBuf.fd);
559 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
561 become_session(IO->CitContext);
563 assert(IO->ConnFail);
564 switch (IO->ConnFail(IO))
575 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
577 AsyncIO *IO = watcher->data;
579 ev_idle_stop (event_base, &IO->conn_fail_immediate);
581 if (IO->SendBuf.fd != 0)
583 close(IO->SendBuf.fd);
584 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
586 become_session(IO->CitContext);
588 assert(IO->ConnFail);
589 switch (IO->ConnFail(IO))
600 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
602 AsyncIO *IO = watcher->data;
604 ev_io_stop(loop, &IO->conn_event);
605 ev_timer_stop (event_base, &IO->conn_fail);
606 set_start_callback(loop, IO, revents);
609 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
613 AsyncIO *IO = watcher->data;
615 switch (IO->NextState) {
617 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
619 StrBufPlain(IO->ErrMsg, errmsg, -1);
622 if (IO->IOB.ChunkSendRemain == 0)
624 IO->NextState = eSendReply;
631 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
640 const char *pch = ChrPtr(IO->RecvBuf.Buf);
641 const char *pchh = IO->RecvBuf.ReadWritePointer;
647 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
648 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
649 ((CitContext*)(IO->CitContext))->ServiceName,
652 fd = fopen(fn, "a+");
653 fprintf(fd, "Read: BufSize: %ld BufContent: [",
655 rv = fwrite(pchh, nbytes, 1, fd);
656 if (!rv) printf("failed to write debug to %s!\n", fn);
665 } else if (nbytes == 0) {
668 switch (IO->Timeout(IO))
676 } else if (nbytes == -1) {
677 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
679 "EVENT: Socket Invalid! %s \n",
687 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
689 AsyncIO *IO = watcher->data;
690 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
691 become_session(IO->CitContext);
692 assert(IO->DNS.Fail);
693 assert(IO->DNS.Query->PostDNS);
694 switch (IO->DNS.Query->PostDNS(IO))
697 switch (IO->DNS.Fail(IO)) {
709 eNextState EvConnectSock(AsyncIO *IO,
711 double first_rw_timeout,
717 become_session(IO->CitContext);
720 IO->NextState = eReadMessage;
723 IO->NextState = eSendReply;
726 IO->SendBuf.fd = IO->RecvBuf.fd =
728 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
732 if (IO->SendBuf.fd < 0) {
734 "EVENT: socket() failed: %s\n",
737 StrBufPrintf(IO->ErrMsg,
738 "Failed to create socket: %s",
742 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
745 "EVENT: unable to get socket flags! %s \n",
747 StrBufPrintf(IO->ErrMsg,
748 "Failed to get socket flags: %s",
752 fdflags = fdflags | O_NONBLOCK;
753 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
756 "EVENT: unable to set socket nonblocking flags! %s \n",
758 StrBufPrintf(IO->ErrMsg,
759 "Failed to set socket flags: %s",
761 close(IO->SendBuf.fd);
762 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
765 /* TODO: maye we could use offsetof() to calc the position of data...
766 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
768 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
769 IO->recv_event.data = IO;
770 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
771 IO->send_event.data = IO;
773 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
774 IO->conn_fail.data = IO;
775 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
776 IO->rw_timeout.data = IO;
779 /* Bypass it like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
780 /// ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = inet_addr("127.0.0.1");
781 if (IO->ConnectMe->IPv6)
782 rc = connect(IO->SendBuf.fd,
783 &IO->ConnectMe->Addr,
784 sizeof(struct sockaddr_in6));
786 rc = connect(IO->SendBuf.fd,
787 (struct sockaddr_in *)&IO->ConnectMe->Addr,
788 sizeof(struct sockaddr_in));
791 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
792 set_start_callback(event_base, IO, 0);
793 ev_timer_start(event_base, &IO->rw_timeout);
794 return IO->NextState;
796 else if (errno == EINPROGRESS) {
797 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
799 ev_io_init(&IO->conn_event,
800 IO_connestd_callback,
804 IO->conn_event.data = IO;
806 ev_io_start(event_base, &IO->conn_event);
807 ev_timer_start(event_base, &IO->conn_fail);
808 return IO->NextState;
811 ev_idle_init(&IO->conn_fail_immediate,
812 IO_connfailimmediate_callback);
813 IO->conn_fail_immediate.data = IO;
814 ev_idle_start(event_base, &IO->conn_fail_immediate);
816 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
817 StrBufPrintf(IO->ErrMsg,
818 "Failed to connect: %s",
820 return IO->NextState;
822 return IO->NextState;
825 void SetNextTimeout(AsyncIO *IO, double timeout)
827 IO->rw_timeout.repeat = timeout;
828 ev_timer_again (event_base, &IO->rw_timeout);
832 eNextState ReAttachIO(AsyncIO *IO,
837 become_session(IO->CitContext);
838 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
840 IO->NextState = eReadMessage;
843 IO->NextState = eSendReply;
845 set_start_callback(event_base, IO, 0);
847 return IO->NextState;
850 void InitIOStruct(AsyncIO *IO,
852 eNextState NextState,
853 IO_LineReaderCallback LineReader,
854 IO_CallBack DNS_Fail,
855 IO_CallBack SendDone,
856 IO_CallBack ReadDone,
857 IO_CallBack Terminate,
858 IO_CallBack ConnFail,
860 IO_CallBack ShutdownAbort)
864 IO->CitContext = CloneContext(CC);
865 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
867 IO->NextState = NextState;
869 IO->SendDone = SendDone;
870 IO->ReadDone = ReadDone;
871 IO->Terminate = Terminate;
872 IO->LineReader = LineReader;
873 IO->ConnFail = ConnFail;
874 IO->Timeout = Timeout;
875 IO->ShutdownAbort = ShutdownAbort;
877 IO->DNS.Fail = DNS_Fail;
879 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
880 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
881 IO->IOBuf = NewStrBuf();
885 extern int evcurl_init(AsyncIO *IO);
887 int InitcURLIOStruct(AsyncIO *IO,
890 IO_CallBack SendDone,
891 IO_CallBack Terminate,
892 IO_CallBack ShutdownAbort)
896 IO->CitContext = CloneContext(CC);
897 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
899 IO->SendDone = SendDone;
900 IO->Terminate = Terminate;
901 IO->ShutdownAbort = ShutdownAbort;
903 strcpy(IO->HttpReq.errdesc, Desc);
906 return evcurl_init(IO);