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