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