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