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);
125 Ctx->state = CON_IDLE;
130 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
132 AsyncIO *IO = watcher->data;
133 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
134 become_session(IO->CitContext);
136 ev_idle_stop(event_db, &IO->db_unwind_stack);
138 assert(IO->NextDBOperation);
139 switch (IO->NextDBOperation(IO))
153 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
155 case eTerminateConnection:
157 ev_idle_stop(event_db, &IO->db_unwind_stack);
158 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
159 ShutDownDBCLient(IO);
163 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
165 IO->NextDBOperation = CB;
166 ev_idle_init(&IO->db_unwind_stack,
168 IO->db_unwind_stack.data = IO;
169 ev_idle_start(event_db, &IO->db_unwind_stack);
173 /*--------------------------------------------------------------------------------
176 extern int evbase_count;
177 extern pthread_mutex_t EventQueueMutex;
178 extern HashList *InboundEventQueue;
179 extern struct ev_loop *event_base;
180 extern ev_async AddJob;
181 extern ev_async ExitEventLoop;
184 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
189 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
192 ev_cleanup_init(&IO->abort_by_shutdown,
193 IO_abort_shutdown_callback);
194 IO->abort_by_shutdown.data = IO;
195 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
197 pthread_mutex_lock(&EventQueueMutex);
198 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
200 Put(InboundEventQueue, IKEY(i), h, NULL);
201 pthread_mutex_unlock(&EventQueueMutex);
203 ev_async_send (event_base, &AddJob);
204 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
208 extern eNextState evcurl_handle_start(AsyncIO *IO);
210 eNextState QueueCurlContext(AsyncIO *IO)
215 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
217 h->EvAttch = evcurl_handle_start;
219 pthread_mutex_lock(&EventQueueMutex);
220 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
222 Put(InboundEventQueue, IKEY(i), h, NULL);
223 pthread_mutex_unlock(&EventQueueMutex);
225 ev_async_send (event_base, &AddJob);
226 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
230 void FreeAsyncIOContents(AsyncIO *IO)
232 FreeStrBuf(&IO->IOBuf);
233 FreeStrBuf(&IO->SendBuf.Buf);
234 FreeStrBuf(&IO->RecvBuf.Buf);
238 void StopClientWatchers(AsyncIO *IO)
240 ev_timer_stop(event_base, &IO->conn_fail);
241 ev_io_stop(event_base, &IO->conn_event);
242 ev_idle_stop(event_base, &IO->unwind_stack);
244 ev_io_stop(event_base, &IO->send_event);
245 ev_io_stop(event_base, &IO->recv_event);
246 ev_timer_stop (event_base, &IO->rw_timeout);
247 close(IO->SendBuf.fd);
252 void ShutDownCLient(AsyncIO *IO)
254 CitContext *Ctx =IO->CitContext;
257 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
259 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
260 StopClientWatchers(IO);
262 if (IO->DNS.Channel != NULL) {
263 ares_destroy(IO->DNS.Channel);
264 ev_io_stop(event_base, &IO->DNS.recv_event);
265 ev_io_stop(event_base, &IO->DNS.send_event);
266 IO->DNS.Channel = NULL;
268 assert(IO->Terminate);
270 Ctx->state = CON_IDLE;
275 eReadState HandleInbound(AsyncIO *IO)
277 const char *Err = NULL;
278 eReadState Finished = eBufferNotEmpty;
280 become_session(IO->CitContext);
282 while ((Finished == eBufferNotEmpty) &&
283 ((IO->NextState == eReadMessage)||
284 (IO->NextState == eReadMore)||
285 (IO->NextState == eReadFile)||
286 (IO->NextState == eReadPayload)))
288 if (IO->RecvBuf.nBlobBytesWanted != 0) {
291 else { /* Reading lines... */
292 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
293 if ((IO->NextState == eReadFile) &&
294 (Finished == eBufferNotEmpty))
296 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
297 if (Finished == eReadSuccess)
299 IO->NextState = eSendReply;
302 else if (IO->LineReader)
303 Finished = IO->LineReader(IO);
305 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
308 case eMustReadMore: /// read new from socket...
310 case eBufferNotEmpty: /* shouldn't happen... */
311 case eReadSuccess: /// done for now...
313 case eReadFail: /// WHUT?
320 if (Finished != eMustReadMore) {
321 assert(IO->ReadDone);
322 ev_io_stop(event_base, &IO->recv_event);
323 IO->NextState = IO->ReadDone(IO);
324 Finished = StrBufCheckBuffer(&IO->RecvBuf);
328 switch (IO->NextState) {
330 ev_io_start(event_base, &IO->send_event);
334 assert(IO->SendDone);
335 IO->NextState = IO->SendDone(IO);
336 ev_io_start(event_base, &IO->send_event);
341 ev_io_start(event_base, &IO->recv_event);
343 case eTerminateConnection:
361 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
364 AsyncIO *IO = watcher->data;
365 const char *errmsg = NULL;
367 become_session(IO->CitContext);
373 const char *pch = ChrPtr(IO->SendBuf.Buf);
374 const char *pchh = IO->SendBuf.ReadWritePointer;
380 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
381 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
382 ((CitContext*)(IO->CitContext))->ServiceName,
385 fd = fopen(fn, "a+");
386 fprintf(fd, "Send: BufSize: %ld BufContent: [",
388 rv = fwrite(pchh, nbytes, 1, fd);
389 if (!rv) printf("failed to write debug to %s!\n", fn);
392 switch (IO->NextState) {
394 rc = FileSendChunked(&IO->IOB, &errmsg);
396 StrBufPlain(IO->ErrMsg, errmsg, -1);
399 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
403 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
409 ev_io_stop(event_base, &IO->send_event);
410 switch (IO->NextState) {
412 assert(IO->SendDone);
413 IO->NextState = IO->SendDone(IO);
415 if ((IO->NextState == eTerminateConnection) ||
416 (IO->NextState == eAbort) )
419 ev_io_start(event_base, &IO->send_event);
423 if (IO->IOB.ChunkSendRemain > 0) {
424 ev_io_start(event_base, &IO->recv_event);
426 assert(IO->ReadDone);
427 IO->NextState = IO->ReadDone(IO);
428 switch(IO->NextState) {
437 ev_io_start(event_base, &IO->send_event);
444 case eTerminateConnection:
451 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
453 IO->NextState = eReadMore;
458 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
462 ev_io_start(event_base, &IO->recv_event);
467 /* we now live in another queue, so we have to unregister. */
468 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
473 case eTerminateConnection:
482 /* else : must write more. */
485 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
488 switch(IO->NextState) {
492 ev_io_start(event_base, &IO->recv_event);
498 become_session(IO->CitContext);
499 IO_send_callback(loop, &IO->send_event, revents);
505 case eTerminateConnection:
513 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
515 AsyncIO *IO = watcher->data;
517 ev_timer_stop (event_base, &IO->rw_timeout);
518 become_session(IO->CitContext);
520 if (IO->SendBuf.fd != 0)
522 ev_io_stop(event_base, &IO->send_event);
523 ev_io_stop(event_base, &IO->recv_event);
524 ev_timer_stop (event_base, &IO->rw_timeout);
525 close(IO->SendBuf.fd);
526 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
530 switch (IO->Timeout(IO))
540 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
542 AsyncIO *IO = watcher->data;
544 ev_timer_stop (event_base, &IO->conn_fail);
546 if (IO->SendBuf.fd != 0)
548 ev_io_stop(loop, &IO->conn_event);
549 ev_io_stop(event_base, &IO->send_event);
550 ev_io_stop(event_base, &IO->recv_event);
551 ev_timer_stop (event_base, &IO->rw_timeout);
552 close(IO->SendBuf.fd);
553 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
555 become_session(IO->CitContext);
557 assert(IO->ConnFail);
558 switch (IO->ConnFail(IO))
569 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
571 AsyncIO *IO = watcher->data;
573 ev_idle_stop (event_base, &IO->conn_fail_immediate);
575 if (IO->SendBuf.fd != 0)
577 close(IO->SendBuf.fd);
578 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
580 become_session(IO->CitContext);
582 assert(IO->ConnFail);
583 switch (IO->ConnFail(IO))
594 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
596 AsyncIO *IO = watcher->data;
598 ev_io_stop(loop, &IO->conn_event);
599 ev_timer_stop (event_base, &IO->conn_fail);
600 set_start_callback(loop, IO, revents);
603 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
607 AsyncIO *IO = watcher->data;
609 switch (IO->NextState) {
611 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
613 StrBufPlain(IO->ErrMsg, errmsg, -1);
616 if (IO->IOB.ChunkSendRemain == 0)
618 IO->NextState = eSendReply;
625 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
634 const char *pch = ChrPtr(IO->RecvBuf.Buf);
635 const char *pchh = IO->RecvBuf.ReadWritePointer;
641 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
642 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
643 ((CitContext*)(IO->CitContext))->ServiceName,
646 fd = fopen(fn, "a+");
647 fprintf(fd, "Read: BufSize: %ld BufContent: [",
649 rv = fwrite(pchh, nbytes, 1, fd);
650 if (!rv) printf("failed to write debug to %s!\n", fn);
659 } else if (nbytes == 0) {
662 switch (IO->Timeout(IO))
670 } else if (nbytes == -1) {
671 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
673 "EVENT: Socket Invalid! %s \n",
681 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
683 AsyncIO *IO = watcher->data;
684 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
685 become_session(IO->CitContext);
686 assert(IO->DNS.Fail);
687 assert(IO->DNS.Query->PostDNS);
688 switch (IO->DNS.Query->PostDNS(IO))
691 switch (IO->DNS.Fail(IO)) {
704 eNextState EvConnectSock(AsyncIO *IO,
706 double first_rw_timeout,
712 become_session(IO->CitContext);
715 IO->NextState = eReadMessage;
718 IO->NextState = eSendReply;
721 IO->SendBuf.fd = IO->RecvBuf.fd =
723 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
727 if (IO->SendBuf.fd < 0) {
729 "EVENT: socket() failed: %s\n",
732 StrBufPrintf(IO->ErrMsg,
733 "Failed to create socket: %s",
737 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
740 "EVENT: unable to get socket flags! %s \n",
742 StrBufPrintf(IO->ErrMsg,
743 "Failed to get socket flags: %s",
747 fdflags = fdflags | O_NONBLOCK;
748 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
751 "EVENT: unable to set socket nonblocking flags! %s \n",
753 StrBufPrintf(IO->ErrMsg,
754 "Failed to set socket flags: %s",
756 close(IO->SendBuf.fd);
757 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
760 /* TODO: maye we could use offsetof() to calc the position of data...
761 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
763 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
764 IO->recv_event.data = IO;
765 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
766 IO->send_event.data = IO;
768 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
769 IO->conn_fail.data = IO;
770 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
771 IO->rw_timeout.data = IO;
774 /* Bypass it like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
775 /// ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = inet_addr("127.0.0.1");
776 if (IO->ConnectMe->IPv6)
777 rc = connect(IO->SendBuf.fd,
778 &IO->ConnectMe->Addr,
779 sizeof(struct sockaddr_in6));
781 rc = connect(IO->SendBuf.fd,
782 (struct sockaddr_in *)&IO->ConnectMe->Addr,
783 sizeof(struct sockaddr_in));
786 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
787 set_start_callback(event_base, IO, 0);
788 ev_timer_start(event_base, &IO->rw_timeout);
789 return IO->NextState;
791 else if (errno == EINPROGRESS) {
792 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
794 ev_io_init(&IO->conn_event,
795 IO_connestd_callback,
799 IO->conn_event.data = IO;
801 ev_io_start(event_base, &IO->conn_event);
802 ev_timer_start(event_base, &IO->conn_fail);
803 return IO->NextState;
806 ev_idle_init(&IO->conn_fail_immediate,
807 IO_connfailimmediate_callback);
808 IO->conn_fail_immediate.data = IO;
809 ev_idle_start(event_base, &IO->conn_fail_immediate);
811 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
812 StrBufPrintf(IO->ErrMsg,
813 "Failed to connect: %s",
815 return IO->NextState;
817 return IO->NextState;
820 void SetNextTimeout(AsyncIO *IO, double timeout)
822 IO->rw_timeout.repeat = timeout;
823 ev_timer_again (event_base, &IO->rw_timeout);
827 eNextState ReAttachIO(AsyncIO *IO,
832 become_session(IO->CitContext);
833 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
835 IO->NextState = eReadMessage;
838 IO->NextState = eSendReply;
840 set_start_callback(event_base, IO, 0);
842 return IO->NextState;
845 void InitIOStruct(AsyncIO *IO,
847 eNextState NextState,
848 IO_LineReaderCallback LineReader,
849 IO_CallBack DNS_Fail,
850 IO_CallBack SendDone,
851 IO_CallBack ReadDone,
852 IO_CallBack Terminate,
853 IO_CallBack ConnFail,
855 IO_CallBack ShutdownAbort)
859 IO->CitContext = CloneContext(CC);
860 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
862 IO->NextState = NextState;
864 IO->SendDone = SendDone;
865 IO->ReadDone = ReadDone;
866 IO->Terminate = Terminate;
867 IO->LineReader = LineReader;
868 IO->ConnFail = ConnFail;
869 IO->Timeout = Timeout;
870 IO->ShutdownAbort = ShutdownAbort;
872 IO->DNS.Fail = DNS_Fail;
874 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
875 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
876 IO->IOBuf = NewStrBuf();