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