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