EVENT_CLIENT: when handing over to the DB-Queue, don't re-start IO-Watchers.
[citadel.git] / citadel / event_client.c
1 /*
2  * Copyright (c) 1998-2012 by the citadel.org team
3  *
4  * This program is open source software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <termios.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <syslog.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35 #include <sys/wait.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <assert.h>
43 #if HAVE_BACKTRACE
44 #include <execinfo.h>
45 #endif
46
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "user_ops.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58 #include "genstamp.h"
59 #include "domain.h"
60 #include "clientsocket.h"
61 #include "locate_host.h"
62 #include "citadel_dirs.h"
63
64 #include "event_client.h"
65 #include "ctdl_module.h"
66
67 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
68 static void IO_abort_shutdown_callback(struct ev_loop *loop,
69                                        ev_cleanup *watcher,
70                                        int revents);
71
72
73 /*------------------------------------------------------------------------------
74  *                              Server DB IO
75  *----------------------------------------------------------------------------*/
76 extern int evdb_count;
77 extern pthread_mutex_t DBEventQueueMutex;
78 extern pthread_mutex_t DBEventExitQueueMutex;
79 extern HashList *DBInboundEventQueue;
80 extern struct ev_loop *event_db;
81 extern ev_async DBAddJob;
82 extern ev_async DBExitEventLoop;
83
84 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
85 {
86         IOAddHandler *h;
87         int i;
88
89         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
90         h->IO = IO;
91         h->EvAttch = CB;
92         ev_cleanup_init(&IO->db_abort_by_shutdown,
93                         IO_abort_shutdown_callback);
94         IO->db_abort_by_shutdown.data = IO;
95
96         pthread_mutex_lock(&DBEventQueueMutex);
97         if (DBInboundEventQueue == NULL)
98         {
99                 /* shutting down... */
100                 free(h);
101                 EVM_syslog(LOG_DEBUG, "DBEVENT Q exiting.\n");
102                 pthread_mutex_unlock(&DBEventQueueMutex);
103                 return eAbort;
104         }
105         EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
106         i = ++evdb_count ;
107         Put(DBInboundEventQueue, IKEY(i), h, NULL);
108         pthread_mutex_unlock(&DBEventQueueMutex);
109
110         pthread_mutex_lock(&DBEventExitQueueMutex);
111         if (event_db == NULL)
112         {
113                 pthread_mutex_unlock(&DBEventExitQueueMutex);
114                 return eAbort;
115         }
116         ev_async_send (event_db, &DBAddJob);
117         pthread_mutex_unlock(&DBEventExitQueueMutex);
118
119         EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
120         return eDBQuery;
121 }
122
123 void StopDBWatchers(AsyncIO *IO)
124 {
125         ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
126         ev_idle_stop(event_db, &IO->db_unwind_stack);
127 }
128
129 void ShutDownDBCLient(AsyncIO *IO)
130 {
131         CitContext *Ctx =IO->CitContext;
132         become_session(Ctx);
133
134         EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
135         StopDBWatchers(IO);
136
137         assert(IO->DBTerminate);
138         IO->DBTerminate(IO);
139 }
140
141 void
142 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
143 {
144         AsyncIO *IO = watcher->data;
145         IO->Now = ev_now(event_db);
146         EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
147         become_session(IO->CitContext);
148
149         ev_idle_stop(event_db, &IO->db_unwind_stack);
150
151         assert(IO->NextDBOperation);
152         switch (IO->NextDBOperation(IO))
153         {
154         case eDBQuery:
155                 break;
156         case eSendDNSQuery:
157         case eReadDNSReply:
158         case eConnect:
159         case eSendReply:
160         case eSendMore:
161         case eSendFile:
162         case eReadMessage:
163         case eReadMore:
164         case eReadPayload:
165         case eReadFile:
166                 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
167                 break;
168         case eTerminateConnection:
169         case eAbort:
170                 ev_idle_stop(event_db, &IO->db_unwind_stack);
171                 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
172                 ShutDownDBCLient(IO);
173         }
174 }
175
176 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
177 {
178         IO->NextDBOperation = CB;
179         ev_idle_init(&IO->db_unwind_stack,
180                      DB_PerformNext);
181         IO->db_unwind_stack.data = IO;
182         ev_idle_start(event_db, &IO->db_unwind_stack);
183         return eDBQuery;
184 }
185
186 /*------------------------------------------------------------------------------
187  *                      Client IO
188  *----------------------------------------------------------------------------*/
189 extern int evbase_count;
190 extern pthread_mutex_t EventQueueMutex;
191 extern pthread_mutex_t EventExitQueueMutex; 
192 extern HashList *InboundEventQueue;
193 extern struct ev_loop *event_base;
194 extern ev_async AddJob;
195 extern ev_async ExitEventLoop;
196
197 static void IO_abort_shutdown_callback(struct ev_loop *loop,
198                                        ev_cleanup *watcher,
199                                        int revents)
200 {
201         AsyncIO *IO = watcher->data;
202         EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
203         IO->Now = ev_now(event_base);
204         assert(IO->ShutdownAbort);
205         IO->ShutdownAbort(IO);
206 }
207
208
209 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
210 {
211         IOAddHandler *h;
212         int i;
213
214         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
215         h->IO = IO;
216         h->EvAttch = CB;
217         ev_cleanup_init(&IO->abort_by_shutdown,
218                         IO_abort_shutdown_callback);
219         IO->abort_by_shutdown.data = IO;
220
221         pthread_mutex_lock(&EventQueueMutex);
222         if (InboundEventQueue == NULL)
223         {
224                 free(h);
225                 /* shutting down... */
226                 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
227                 pthread_mutex_unlock(&EventQueueMutex);
228                 return eAbort;
229         }
230         EVM_syslog(LOG_DEBUG, "EVENT Q\n");
231         i = ++evbase_count;
232         Put(InboundEventQueue, IKEY(i), h, NULL);
233         pthread_mutex_unlock(&EventQueueMutex);
234
235         pthread_mutex_lock(&EventExitQueueMutex);
236         if (event_base == NULL) {
237                 pthread_mutex_unlock(&EventExitQueueMutex);
238                 return eAbort;
239         }
240         ev_async_send (event_base, &AddJob);
241         pthread_mutex_unlock(&EventExitQueueMutex);
242         EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
243         return eSendReply;
244 }
245
246 extern eNextState evcurl_handle_start(AsyncIO *IO);
247
248 eNextState QueueCurlContext(AsyncIO *IO)
249 {
250         IOAddHandler *h;
251         int i;
252
253         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
254         h->IO = IO;
255         h->EvAttch = evcurl_handle_start;
256
257         pthread_mutex_lock(&EventQueueMutex);
258         if (InboundEventQueue == NULL)
259         {
260                 /* shutting down... */
261                 free(h);
262                 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
263                 pthread_mutex_unlock(&EventQueueMutex);
264                 return eAbort;
265         }
266
267         EVM_syslog(LOG_DEBUG, "EVENT Q\n");
268         i = ++evbase_count;
269         Put(InboundEventQueue, IKEY(i), h, NULL);
270         pthread_mutex_unlock(&EventQueueMutex);
271
272         pthread_mutex_lock(&EventExitQueueMutex);
273         if (event_base == NULL) {
274                 pthread_mutex_unlock(&EventExitQueueMutex);
275                 return eAbort;
276         }
277         ev_async_send (event_base, &AddJob);
278         pthread_mutex_unlock(&EventExitQueueMutex);
279
280         EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
281         return eSendReply;
282 }
283
284 void DestructCAres(AsyncIO *IO);
285 void FreeAsyncIOContents(AsyncIO *IO)
286 {
287         CitContext *Ctx = IO->CitContext;
288
289         FreeStrBuf(&IO->IOBuf);
290         FreeStrBuf(&IO->SendBuf.Buf);
291         FreeStrBuf(&IO->RecvBuf.Buf);
292
293         DestructCAres(IO);
294
295         FreeURL(&IO->ConnectMe);
296         FreeStrBuf(&IO->HttpReq.ReplyData);
297
298         if (Ctx) {
299                 Ctx->state = CON_IDLE;
300                 Ctx->kill_me = 1;
301         }
302 }
303
304
305 void StopClientWatchers(AsyncIO *IO, int CloseFD)
306 {
307         ev_timer_stop (event_base, &IO->rw_timeout);
308         ev_timer_stop(event_base, &IO->conn_fail);
309         ev_idle_stop(event_base, &IO->unwind_stack);
310         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
311
312         ev_io_stop(event_base, &IO->conn_event);
313         ev_io_stop(event_base, &IO->send_event);
314         ev_io_stop(event_base, &IO->recv_event);
315
316         if (CloseFD && (IO->SendBuf.fd > 0)) {
317                 close(IO->SendBuf.fd);
318                 IO->SendBuf.fd = -1;
319                 IO->RecvBuf.fd = -1;
320         }
321 }
322
323 void StopCurlWatchers(AsyncIO *IO)
324 {
325         ev_timer_stop (event_base, &IO->rw_timeout);
326         ev_timer_stop(event_base, &IO->conn_fail);
327         ev_idle_stop(event_base, &IO->unwind_stack);
328         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
329
330         ev_io_stop(event_base, &IO->conn_event);
331         ev_io_stop(event_base, &IO->send_event);
332         ev_io_stop(event_base, &IO->recv_event);
333
334         if (IO->SendBuf.fd != 0) {
335                 close(IO->SendBuf.fd);
336         }
337         IO->SendBuf.fd = 0;
338         IO->RecvBuf.fd = 0;
339 }
340
341 void ShutDownCLient(AsyncIO *IO)
342 {
343         CitContext *Ctx =IO->CitContext;
344         become_session(Ctx);
345
346         EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
347
348         StopClientWatchers(IO, 1);
349
350         if (IO->DNS.Channel != NULL) {
351                 ares_destroy(IO->DNS.Channel);
352                 EV_DNS_LOG_STOP(DNS.recv_event);
353                 EV_DNS_LOG_STOP(DNS.send_event);
354                 ev_io_stop(event_base, &IO->DNS.recv_event);
355                 ev_io_stop(event_base, &IO->DNS.send_event);
356                 IO->DNS.Channel = NULL;
357         }
358         assert(IO->Terminate);
359         IO->Terminate(IO);
360 }
361
362 void PostInbound(AsyncIO *IO)
363 {
364         switch (IO->NextState) {
365         case eSendFile:
366                 ev_io_start(event_base, &IO->send_event);
367                 break;
368         case eSendReply:
369         case eSendMore:
370                 assert(IO->SendDone);
371                 IO->NextState = IO->SendDone(IO);
372                 switch (IO->NextState)
373                 {
374                 case eSendFile:
375                 case eSendReply:
376                 case eSendMore:
377                 case eReadMessage:
378                 case eReadPayload:
379                 case eReadMore:
380                 case eReadFile:
381                         ev_io_start(event_base, &IO->send_event);
382                         break;
383                 case eDBQuery:
384                         StopClientWatchers(IO, 0);
385                 default:
386                         break;
387                 }
388                 break;
389         case eReadPayload:
390         case eReadMore:
391         case eReadFile:
392                 ev_io_start(event_base, &IO->recv_event);
393                 break;
394         case eTerminateConnection:
395                 ShutDownCLient(IO);
396                 break;
397         case eAbort:
398                 ShutDownCLient(IO);
399                 break;
400         case eSendDNSQuery:
401         case eReadDNSReply:
402         case eDBQuery:
403         case eConnect:
404         case eReadMessage:
405                 break;
406         }
407 }
408 eReadState HandleInbound(AsyncIO *IO)
409 {
410         const char *Err = NULL;
411         eReadState Finished = eBufferNotEmpty;
412
413         become_session(IO->CitContext);
414
415         while ((Finished == eBufferNotEmpty) &&
416                ((IO->NextState == eReadMessage)||
417                 (IO->NextState == eReadMore)||
418                 (IO->NextState == eReadFile)||
419                 (IO->NextState == eReadPayload)))
420         {
421                 /* Reading lines...
422                  * lex line reply in callback,
423                  * or do it ourselves.
424                  * i.e. as nnn-blabla means continue reading in SMTP
425                  */
426                 if ((IO->NextState == eReadFile) &&
427                     (Finished == eBufferNotEmpty))
428                 {
429                         Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
430                         if (Finished == eReadSuccess)
431                         {
432                                 IO->NextState = eSendReply;
433                         }
434                 }
435                 else if (IO->LineReader)
436                         Finished = IO->LineReader(IO);
437                 else
438                         Finished = StrBufChunkSipLine(IO->IOBuf,
439                                                       &IO->RecvBuf);
440
441                 switch (Finished) {
442                 case eMustReadMore: /// read new from socket...
443                         break;
444                 case eBufferNotEmpty: /* shouldn't happen... */
445                 case eReadSuccess: /// done for now...
446                         break;
447                 case eReadFail: /// WHUT?
448                                 ///todo: shut down!
449                         break;
450                 }
451
452                 if (Finished != eMustReadMore) {
453                         assert(IO->ReadDone);
454                         ev_io_stop(event_base, &IO->recv_event);
455                         IO->NextState = IO->ReadDone(IO);
456                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
457                 }
458         }
459
460         PostInbound(IO);
461
462         return Finished;
463 }
464
465
466 static void
467 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
468 {
469         int rc;
470         AsyncIO *IO = watcher->data;
471         const char *errmsg = NULL;
472
473         IO->Now = ev_now(event_base);
474         become_session(IO->CitContext);
475 #ifdef BIGBAD_IODBG
476         {
477                 int rv = 0;
478                 char fn [SIZ];
479                 FILE *fd;
480                 const char *pch = ChrPtr(IO->SendBuf.Buf);
481                 const char *pchh = IO->SendBuf.ReadWritePointer;
482                 long nbytes;
483
484                 if (pchh == NULL)
485                         pchh = pch;
486
487                 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
488                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
489                          ((CitContext*)(IO->CitContext))->ServiceName,
490                          IO->SendBuf.fd);
491
492                 fd = fopen(fn, "a+");
493                 fprintf(fd, "Send: BufSize: %ld BufContent: [",
494                         nbytes);
495                 rv = fwrite(pchh, nbytes, 1, fd);
496                 if (!rv) printf("failed to write debug to %s!\n", fn);
497                 fprintf(fd, "]\n");
498 #endif
499                 switch (IO->NextState) {
500                 case eSendFile:
501                         rc = FileSendChunked(&IO->IOB, &errmsg);
502                         if (rc < 0)
503                                 StrBufPlain(IO->ErrMsg, errmsg, -1);
504                         break;
505                 default:
506                         rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
507                                                              0,
508                                                              &IO->SendBuf);
509                 }
510
511 #ifdef BIGBAD_IODBG
512                 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
513                 fclose(fd);
514         }
515 #endif
516         if (rc == 0)
517         {
518                 ev_io_stop(event_base, &IO->send_event);
519                 switch (IO->NextState) {
520                 case eSendMore:
521                         assert(IO->SendDone);
522                         IO->NextState = IO->SendDone(IO);
523
524                         if ((IO->NextState == eTerminateConnection) ||
525                             (IO->NextState == eAbort) )
526                                 ShutDownCLient(IO);
527                         else {
528                                 ev_io_start(event_base, &IO->send_event);
529                         }
530                         break;
531                 case eSendFile:
532                         if (IO->IOB.ChunkSendRemain > 0) {
533                                 ev_io_start(event_base, &IO->recv_event);
534                                 SetNextTimeout(IO, 100.0);
535
536                         } else {
537                                 assert(IO->ReadDone);
538                                 IO->NextState = IO->ReadDone(IO);
539                                 switch(IO->NextState) {
540                                 case eSendDNSQuery:
541                                 case eReadDNSReply:
542                                 case eDBQuery:
543                                 case eConnect:
544                                         break;
545                                 case eSendReply:
546                                 case eSendMore:
547                                 case eSendFile:
548                                         ev_io_start(event_base,
549                                                     &IO->send_event);
550                                         break;
551                                 case eReadMessage:
552                                 case eReadMore:
553                                 case eReadPayload:
554                                 case eReadFile:
555                                         break;
556                                 case eTerminateConnection:
557                                 case eAbort:
558                                         break;
559                                 }
560                         }
561                         break;
562                 case eSendReply:
563                     if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
564                         break;
565                     IO->NextState = eReadMore;
566                 case eReadMore:
567                 case eReadMessage:
568                 case eReadPayload:
569                 case eReadFile:
570                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
571                         {
572                                 HandleInbound(IO);
573                         }
574                         else {
575                                 ev_io_start(event_base, &IO->recv_event);
576                         }
577
578                         break;
579                 case eDBQuery:
580                         /*
581                          * we now live in another queue,
582                          * so we have to unregister.
583                          */
584                         ev_cleanup_stop(loop, &IO->abort_by_shutdown);
585                         break;
586                 case eSendDNSQuery:
587                 case eReadDNSReply:
588                 case eConnect:
589                 case eTerminateConnection:
590                 case eAbort:
591                         break;
592                 }
593         }
594         else if (rc < 0) {
595                 if (errno != EAGAIN) {
596                         StopClientWatchers(IO, 1);
597                         EV_syslog(LOG_DEBUG,
598                                   "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
599                                   errno, strerror(errno), IO->SendBuf.fd);
600                         StrBufPrintf(IO->ErrMsg,
601                                      "Socket Invalid! [%s]",
602                                      strerror(errno));
603                         SetNextTimeout(IO, 0.0);
604                 }
605         }
606         /* else : must write more. */
607 }
608 static void
609 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
610 {
611         ev_timer_stop(event_base, &IO->conn_fail);
612         ev_timer_start(event_base, &IO->rw_timeout);
613
614         switch(IO->NextState) {
615         case eReadMore:
616         case eReadMessage:
617         case eReadFile:
618                 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
619                 ev_io_start(event_base, &IO->recv_event);
620                 break;
621         case eSendReply:
622         case eSendMore:
623         case eReadPayload:
624         case eSendFile:
625                 become_session(IO->CitContext);
626                 IO_send_callback(loop, &IO->send_event, revents);
627                 break;
628         case eDBQuery:
629         case eSendDNSQuery:
630         case eReadDNSReply:
631         case eConnect:
632         case eTerminateConnection:
633         case eAbort:
634                 /// TODO: WHUT?
635                 break;
636         }
637 }
638
639 static void
640 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
641 {
642         AsyncIO *IO = watcher->data;
643
644         IO->Now = ev_now(event_base);
645         ev_timer_stop (event_base, &IO->rw_timeout);
646         become_session(IO->CitContext);
647
648         if (IO->SendBuf.fd != 0)
649         {
650                 ev_io_stop(event_base, &IO->send_event);
651                 ev_io_stop(event_base, &IO->recv_event);
652                 ev_timer_stop (event_base, &IO->rw_timeout);
653                 close(IO->SendBuf.fd);
654                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
655         }
656
657         assert(IO->Timeout);
658         switch (IO->Timeout(IO))
659         {
660         case eAbort:
661                 ShutDownCLient(IO);
662         default:
663                 break;
664         }
665 }
666
667 static void
668 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
669 {
670         AsyncIO *IO = watcher->data;
671
672         IO->Now = ev_now(event_base);
673         ev_timer_stop (event_base, &IO->conn_fail);
674
675         if (IO->SendBuf.fd != 0)
676         {
677                 ev_io_stop(loop, &IO->conn_event);
678                 ev_io_stop(event_base, &IO->send_event);
679                 ev_io_stop(event_base, &IO->recv_event);
680                 ev_timer_stop (event_base, &IO->rw_timeout);
681                 close(IO->SendBuf.fd);
682                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
683         }
684         become_session(IO->CitContext);
685
686         assert(IO->ConnFail);
687         switch (IO->ConnFail(IO))
688         {
689         case eAbort:
690                 ShutDownCLient(IO);
691         default:
692                 break;
693
694         }
695 }
696
697 static void
698 IO_connfailimmediate_callback(struct ev_loop *loop,
699                               ev_idle *watcher,
700                               int revents)
701 {
702         AsyncIO *IO = watcher->data;
703
704         IO->Now = ev_now(event_base);
705         ev_idle_stop (event_base, &IO->conn_fail_immediate);
706
707         if (IO->SendBuf.fd != 0)
708         {
709                 close(IO->SendBuf.fd);
710                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
711         }
712         become_session(IO->CitContext);
713
714         assert(IO->ConnFail);
715         switch (IO->ConnFail(IO))
716         {
717         case eAbort:
718                 ShutDownCLient(IO);
719         default:
720                 break;
721
722         }
723 }
724
725 static void
726 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
727 {
728         AsyncIO *IO = watcher->data;
729         int             so_err = 0;
730         socklen_t       lon = sizeof(so_err);
731         int             err;
732
733         IO->Now = ev_now(event_base);
734         EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
735
736         ev_io_stop(loop, &IO->conn_event);
737         ev_timer_stop(event_base, &IO->conn_fail);
738
739         err = getsockopt(IO->SendBuf.fd,
740                          SOL_SOCKET,
741                          SO_ERROR,
742                          (void*)&so_err,
743                          &lon);
744
745         if ((err == 0) && (so_err != 0))
746         {
747                 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
748                           so_err,
749                           strerror(so_err));
750                 IO_connfail_callback(loop, &IO->conn_fail, revents);
751
752         }
753         else
754         {
755                 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
756                 set_start_callback(loop, IO, revents);
757         }
758 }
759
760 static void
761 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
762 {
763         const char *errmsg;
764         ssize_t nbytes;
765         AsyncIO *IO = watcher->data;
766
767         IO->Now = ev_now(event_base);
768         switch (IO->NextState) {
769         case eReadFile:
770                 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
771                 if (nbytes < 0)
772                         StrBufPlain(IO->ErrMsg, errmsg, -1);
773                 else
774                 {
775                         if (IO->IOB.ChunkSendRemain == 0)
776                         {
777                                 IO->NextState = eSendReply;
778                                 assert(IO->ReadDone);
779                                 ev_io_stop(event_base, &IO->recv_event);
780                                 PostInbound(IO);
781                                 return;
782                         }
783                         else
784                                 return;
785                 }
786                 break;
787         default:
788                 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
789                                                         0,
790                                                         &IO->RecvBuf);
791                 break;
792         }
793
794 #ifdef BIGBAD_IODBG
795         {
796                 long nbytes;
797                 int rv = 0;
798                 char fn [SIZ];
799                 FILE *fd;
800                 const char *pch = ChrPtr(IO->RecvBuf.Buf);
801                 const char *pchh = IO->RecvBuf.ReadWritePointer;
802
803                 if (pchh == NULL)
804                         pchh = pch;
805
806                 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
807                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
808                          ((CitContext*)(IO->CitContext))->ServiceName,
809                          IO->SendBuf.fd);
810
811                 fd = fopen(fn, "a+");
812                 fprintf(fd, "Read: BufSize: %ld BufContent: [",
813                         nbytes);
814                 rv = fwrite(pchh, nbytes, 1, fd);
815                 if (!rv) printf("failed to write debug to %s!\n", fn);
816                 fprintf(fd, "]\n");
817                 fclose(fd);
818         }
819 #endif
820         if (nbytes > 0) {
821                 HandleInbound(IO);
822         } else if (nbytes == 0) {
823                 StopClientWatchers(IO, 1);
824                 SetNextTimeout(IO, 0.01);
825                 return;
826         } else if (nbytes == -1) {
827                 if (errno != EAGAIN) {
828                         // FD is gone. kick it. 
829                         StopClientWatchers(IO, 1);
830                         EV_syslog(LOG_DEBUG,
831                                   "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
832                                   errno, strerror(errno), IO->SendBuf.fd);
833                         StrBufPrintf(IO->ErrMsg,
834                                      "Socket Invalid! [%s]",
835                                      strerror(errno));
836                         SetNextTimeout(IO, 0.0);
837                 }
838                 return;
839         }
840 }
841
842 void
843 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
844 {
845         AsyncIO *IO = watcher->data;
846         IO->Now = ev_now(event_base);
847         EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
848         become_session(IO->CitContext);
849         assert(IO->DNS.Query->PostDNS);
850         switch (IO->DNS.Query->PostDNS(IO))
851         {
852         case eAbort:
853                 assert(IO->DNS.Fail);
854                 switch (IO->DNS.Fail(IO)) {
855                 case eAbort:
856 ////                    StopClientWatchers(IO);
857                         ShutDownCLient(IO);
858                 default:
859                         break;
860                 }
861         default:
862                 break;
863         }
864 }
865
866
867 eNextState EvConnectSock(AsyncIO *IO,
868                          double conn_timeout,
869                          double first_rw_timeout,
870                          int ReadFirst)
871 {
872         struct sockaddr_in egress_sin;
873         int fdflags;
874         int rc = -1;
875
876         become_session(IO->CitContext);
877
878         if (ReadFirst) {
879                 IO->NextState = eReadMessage;
880         }
881         else {
882                 IO->NextState = eSendReply;
883         }
884
885         IO->SendBuf.fd = IO->RecvBuf.fd =
886                 socket(
887                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
888                         SOCK_STREAM,
889                         IPPROTO_TCP);
890
891         if (IO->SendBuf.fd < 0) {
892                 EV_syslog(LOG_ERR,
893                           "EVENT: socket() failed: %s\n",
894                           strerror(errno));
895
896                 StrBufPrintf(IO->ErrMsg,
897                              "Failed to create socket: %s",
898                              strerror(errno));
899                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
900                 return eAbort;
901         }
902         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
903         if (fdflags < 0) {
904                 EV_syslog(LOG_ERR,
905                           "EVENT: unable to get socket flags! %s \n",
906                           strerror(errno));
907                 StrBufPrintf(IO->ErrMsg,
908                              "Failed to get socket flags: %s",
909                              strerror(errno));
910                 close(IO->SendBuf.fd);
911                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
912                 return eAbort;
913         }
914         fdflags = fdflags | O_NONBLOCK;
915         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
916                 EV_syslog(
917                         LOG_ERR,
918                         "EVENT: unable to set socket nonblocking flags! %s \n",
919                         strerror(errno));
920                 StrBufPrintf(IO->ErrMsg,
921                              "Failed to set socket flags: %s",
922                              strerror(errno));
923                 close(IO->SendBuf.fd);
924                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
925                 return eAbort;
926         }
927 /* TODO: maye we could use offsetof() to calc the position of data...
928  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
929  */
930         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
931         IO->recv_event.data = IO;
932         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
933         IO->send_event.data = IO;
934
935         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
936         IO->conn_fail.data = IO;
937         ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
938         IO->rw_timeout.data = IO;
939
940
941
942
943         /* for debugging you may bypass it like this:
944          * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
945          * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
946          *   inet_addr("127.0.0.1");
947          */
948         if (IO->ConnectMe->IPv6) {
949                 rc = connect(IO->SendBuf.fd,
950                              &IO->ConnectMe->Addr,
951                              sizeof(struct sockaddr_in6));
952         }
953         else {
954                 /* If citserver is bound to a specific IP address on the host, make
955                  * sure we use that address for outbound connections.
956                  */
957         
958                 memset(&egress_sin, 0, sizeof(egress_sin));
959                 egress_sin.sin_family = AF_INET;
960                 if (!IsEmptyStr(config.c_ip_addr)) {
961                         egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
962                         if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
963                                 egress_sin.sin_addr.s_addr = INADDR_ANY;
964                         }
965
966                         /* If this bind fails, no problem; we can still use INADDR_ANY */
967                         bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
968                 }
969                 rc = connect(IO->SendBuf.fd,
970                              (struct sockaddr_in *)&IO->ConnectMe->Addr,
971                              sizeof(struct sockaddr_in));
972         }
973
974         if (rc >= 0){
975                 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
976                 set_start_callback(event_base, IO, 0);
977                 return IO->NextState;
978         }
979         else if (errno == EINPROGRESS) {
980                 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
981
982                 ev_io_init(&IO->conn_event,
983                            IO_connestd_callback,
984                            IO->SendBuf.fd,
985                            EV_READ|EV_WRITE);
986
987                 IO->conn_event.data = IO;
988
989                 ev_io_start(event_base, &IO->conn_event);
990                 ev_timer_start(event_base, &IO->conn_fail);
991                 return IO->NextState;
992         }
993         else {
994                 ev_idle_init(&IO->conn_fail_immediate,
995                              IO_connfailimmediate_callback);
996                 IO->conn_fail_immediate.data = IO;
997                 ev_idle_start(event_base, &IO->conn_fail_immediate);
998
999                 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
1000                 StrBufPrintf(IO->ErrMsg,
1001                              "Failed to connect: %s",
1002                              strerror(errno));
1003                 return IO->NextState;
1004         }
1005         return IO->NextState;
1006 }
1007
1008 void SetNextTimeout(AsyncIO *IO, double timeout)
1009 {
1010         IO->rw_timeout.repeat = timeout;
1011         ev_timer_again (event_base,  &IO->rw_timeout);
1012 }
1013
1014
1015 eNextState ReAttachIO(AsyncIO *IO,
1016                       void *pData,
1017                       int ReadFirst)
1018 {
1019         IO->Data = pData;
1020         become_session(IO->CitContext);
1021         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1022         if (ReadFirst) {
1023                 IO->NextState = eReadMessage;
1024         }
1025         else {
1026                 IO->NextState = eSendReply;
1027         }
1028         set_start_callback(event_base, IO, 0);
1029
1030         return IO->NextState;
1031 }
1032
1033 void InitIOStruct(AsyncIO *IO,
1034                   void *Data,
1035                   eNextState NextState,
1036                   IO_LineReaderCallback LineReader,
1037                   IO_CallBack DNS_Fail,
1038                   IO_CallBack SendDone,
1039                   IO_CallBack ReadDone,
1040                   IO_CallBack Terminate,
1041                   IO_CallBack DBTerminate,
1042                   IO_CallBack ConnFail,
1043                   IO_CallBack Timeout,
1044                   IO_CallBack ShutdownAbort)
1045 {
1046         IO->Data          = Data;
1047
1048         IO->CitContext    = CloneContext(CC);
1049         ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1050
1051         IO->NextState     = NextState;
1052
1053         IO->SendDone      = SendDone;
1054         IO->ReadDone      = ReadDone;
1055         IO->Terminate     = Terminate;
1056         IO->DBTerminate   = DBTerminate;
1057         IO->LineReader    = LineReader;
1058         IO->ConnFail      = ConnFail;
1059         IO->Timeout       = Timeout;
1060         IO->ShutdownAbort = ShutdownAbort;
1061
1062         IO->DNS.Fail      = DNS_Fail;
1063
1064         IO->SendBuf.Buf   = NewStrBufPlain(NULL, 1024);
1065         IO->RecvBuf.Buf   = NewStrBufPlain(NULL, 1024);
1066         IO->IOBuf         = NewStrBuf();
1067         EV_syslog(LOG_DEBUG,
1068                   "EVENT: Session lives at %p IO at %p \n",
1069                   Data, IO);
1070
1071 }
1072
1073 extern int evcurl_init(AsyncIO *IO);
1074
1075 int InitcURLIOStruct(AsyncIO *IO,
1076                      void *Data,
1077                      const char* Desc,
1078                      IO_CallBack SendDone,
1079                      IO_CallBack Terminate,
1080                      IO_CallBack DBTerminate,
1081                      IO_CallBack ShutdownAbort)
1082 {
1083         IO->Data          = Data;
1084
1085         IO->CitContext    = CloneContext(CC);
1086         ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1087
1088         IO->SendDone      = SendDone;
1089         IO->Terminate     = Terminate;
1090         IO->DBTerminate   = DBTerminate;
1091         IO->ShutdownAbort = ShutdownAbort;
1092
1093         strcpy(IO->HttpReq.errdesc, Desc);
1094
1095
1096         return  evcurl_init(IO);
1097
1098 }
1099
1100 extern int DebugEventLoopBacktrace;
1101 void EV_backtrace(AsyncIO *IO)
1102 {
1103 #ifdef HAVE_BACKTRACE
1104         void *stack_frames[50];
1105         size_t size, i;
1106         char **strings;
1107
1108         if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1109                 return;
1110         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1111         strings = backtrace_symbols(stack_frames, size);
1112         for (i = 0; i < size; i++) {
1113                 if (strings != NULL) {
1114                         EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1115                 }
1116                 else {
1117                         EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1118                 }
1119         }
1120         free(strings);
1121 #endif
1122 }
1123
1124
1125 ev_tstamp ctdl_ev_now (void)
1126 {
1127         return ev_now(event_base);
1128 }