add infrastructure to cleanly swap inbetween the DB and the IO queue; add infrastruct...
[citadel.git] / citadel / event_client.c
1 /*
2  *
3  * Copyright (c) 1998-2009 by the citadel.org team
4  *
5  *  This program is free 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, ev_cleanup *watcher, int revents)
71 {
72         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
73
74         AsyncIO *IO = watcher->data;
75         assert(IO->ShutdownAbort);
76         IO->ShutdownAbort(IO);
77 }
78
79
80 /*--------------------------------------------------------------------------------
81  * Server DB IO 
82  */
83 extern int evdb_count;
84 extern citthread_mutex_t DBEventQueueMutex;
85 extern HashList *DBInboundEventQueue;
86 extern struct ev_loop *event_db;
87 extern ev_async DBAddJob;   
88 extern ev_async DBExitEventLoop;
89
90 int QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
91 {
92         IOAddHandler *h;
93         int i;
94
95         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
96         h->IO = IO;
97         h->EvAttch = CB;
98         ev_cleanup_init(&IO->db_abort_by_shutdown, 
99                         IO_abort_shutdown_callback);
100         IO->db_abort_by_shutdown.data = IO;
101         ev_cleanup_start(event_db, &IO->db_abort_by_shutdown);
102
103         citthread_mutex_lock(&DBEventQueueMutex);
104         CtdlLogPrintf(CTDL_DEBUG, "DBEVENT Q\n");
105         i = ++evdb_count ;
106         Put(DBInboundEventQueue, IKEY(i), h, NULL);
107         citthread_mutex_unlock(&DBEventQueueMutex);
108
109         ev_async_send (event_db, &DBAddJob);
110         CtdlLogPrintf(CTDL_DEBUG, "DBEVENT Q Done.\n");
111         return 0;
112 }
113
114 void ShutDownDBCLient(AsyncIO *IO)
115 {
116         CitContext *Ctx =IO->CitContext;
117         become_session(Ctx);
118
119         CtdlLogPrintf(CTDL_DEBUG, "DBEVENT\n");
120         ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
121
122         assert(IO->Terminate);
123         IO->Terminate(IO);      
124
125         Ctx->state = CON_IDLE;
126         Ctx->kill_me = 1;
127 }
128
129 void
130 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
131 {
132         AsyncIO *IO = watcher->data;
133         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
134         become_session(IO->CitContext);
135         
136         ev_idle_stop(event_db, &IO->db_unwind_stack);
137
138         assert(IO->NextDBOperation);
139         switch (IO->NextDBOperation(IO))
140         {
141         case eDBQuery:
142                 break;
143         case eSendDNSQuery:
144         case eReadDNSReply:
145         case eConnect:
146         case eSendReply: 
147         case eSendMore:
148         case eReadMessage: 
149         case eReadMore:
150         case eReadPayload:
151                 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
152                 break;
153         case eTerminateConnection:
154         case eAbort:
155             ShutDownDBCLient(IO);
156         }
157 }
158
159 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
160 {
161         IO->NextDBOperation = CB;
162         ev_idle_init(&IO->db_unwind_stack,
163                      DB_PerformNext);
164         IO->db_unwind_stack.data = IO;
165         ev_idle_start(event_db, &IO->db_unwind_stack);
166         return eDBQuery;
167 }
168
169 /*--------------------------------------------------------------------------------
170  * Client IO 
171  */
172 extern int evbase_count;
173 extern citthread_mutex_t EventQueueMutex;
174 extern HashList *InboundEventQueue;
175 extern struct ev_loop *event_base;
176 extern ev_async AddJob;   
177 extern ev_async ExitEventLoop;
178
179
180 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
181 {
182         IOAddHandler *h;
183         int i;
184
185         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
186         h->IO = IO;
187         h->EvAttch = CB;
188         ev_cleanup_init(&IO->abort_by_shutdown, 
189                         IO_abort_shutdown_callback);
190         IO->abort_by_shutdown.data = IO;
191         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
192
193         citthread_mutex_lock(&EventQueueMutex);
194         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
195         i = ++evbase_count;
196         Put(InboundEventQueue, IKEY(i), h, NULL);
197         citthread_mutex_unlock(&EventQueueMutex);
198
199         ev_async_send (event_base, &AddJob);
200         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
201         return 0;
202 }
203
204 int ShutDownEventQueue(void)
205 {
206         citthread_mutex_lock(&DBEventQueueMutex);
207         ev_async_send (event_db, &DBExitEventLoop);
208         citthread_mutex_unlock(&DBEventQueueMutex);
209
210         citthread_mutex_lock(&EventQueueMutex);
211         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
212         citthread_mutex_unlock(&EventQueueMutex);
213         return 0;
214 }
215
216 void FreeAsyncIOContents(AsyncIO *IO)
217 {
218         FreeStrBuf(&IO->IOBuf);
219         FreeStrBuf(&IO->SendBuf.Buf);
220         FreeStrBuf(&IO->RecvBuf.Buf);
221 }
222
223
224 void StopClientWatchers(AsyncIO *IO)
225 {
226         ev_timer_stop(event_base, &IO->conn_fail);
227         ev_io_stop(event_base, &IO->conn_event);
228         ev_idle_stop(event_base, &IO->unwind_stack);
229
230         if (IO->SendBuf.fd != 0)
231         {
232                 ev_io_stop(event_base, &IO->send_event);
233                 ev_io_stop(event_base, &IO->recv_event);
234                 ev_timer_stop (event_base, &IO->rw_timeout);
235                 close(IO->SendBuf.fd);
236                 IO->SendBuf.fd = 0;
237                 IO->RecvBuf.fd = 0;
238         }
239 }
240
241 void ShutDownCLient(AsyncIO *IO)
242 {
243         CitContext *Ctx =IO->CitContext;
244         become_session(Ctx);
245
246         CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
247
248         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
249         StopClientWatchers(IO);
250
251         if (IO->DNSChannel != NULL) {
252                 ares_destroy(IO->DNSChannel);
253                 ev_io_stop(event_base, &IO->dns_recv_event);
254                 ev_io_stop(event_base, &IO->dns_send_event);
255                 IO->DNSChannel = NULL;
256         }
257         assert(IO->Terminate);
258         IO->Terminate(IO);
259         Ctx->state = CON_IDLE;
260         Ctx->kill_me = 1;
261 }
262
263
264 eReadState HandleInbound(AsyncIO *IO)
265 {
266         eReadState Finished = eBufferNotEmpty;
267         
268         become_session(IO->CitContext);
269
270         while ((Finished == eBufferNotEmpty) && 
271                ((IO->NextState == eReadMessage)||
272                 (IO->NextState == eReadMore)||
273                 (IO->NextState == eReadPayload)))
274         {
275                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
276                                 
277                 }
278                 else { /* Reading lines... */
279 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
280                         if (IO->LineReader)
281                                 Finished = IO->LineReader(IO);
282                         else 
283                                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
284                                 
285                         switch (Finished) {
286                         case eMustReadMore: /// read new from socket... 
287                                 return Finished;
288                                 break;
289                         case eBufferNotEmpty: /* shouldn't happen... */
290                         case eReadSuccess: /// done for now...
291                                 break;
292                         case eReadFail: /// WHUT?
293                                 ///todo: shut down! 
294                                 break;
295                         }
296                                         
297                 }
298                         
299                 if (Finished != eMustReadMore) {
300                         assert(IO->ReadDone);
301                         ev_io_stop(event_base, &IO->recv_event);
302                         IO->NextState = IO->ReadDone(IO);
303                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
304                 }
305         }
306
307         switch (IO->NextState) {
308         case eSendReply:
309         case eSendMore:
310                 assert(IO->SendDone);
311                 IO->NextState = IO->SendDone(IO);
312                 ev_io_start(event_base, &IO->send_event);
313                 break;
314         case eReadPayload:
315         case eReadMore:
316                 ev_io_start(event_base, &IO->recv_event);
317                 break;
318         case eTerminateConnection:
319         case eAbort:
320                 ShutDownCLient(IO);
321                 break;
322         case eSendDNSQuery:
323         case eReadDNSReply:
324         case eDBQuery:
325         case eConnect:
326         case eReadMessage:
327                 break;
328         }
329         return Finished;
330 }
331
332
333 static void
334 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
335 {
336         int rc;
337         AsyncIO *IO = watcher->data;
338
339         become_session(IO->CitContext);
340 #ifdef BIGBAD_IODBG
341         {
342                 int rv = 0;
343                 char fn [SIZ];
344                 FILE *fd;
345                 const char *pch = ChrPtr(IO->SendBuf.Buf);
346                 const char *pchh = IO->SendBuf.ReadWritePointer;
347                 long nbytes;
348                 
349                 if (pchh == NULL)
350                         pchh = pch;
351                 
352                 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
353                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
354                          ((CitContext*)(IO->CitContext))->ServiceName,
355                          IO->SendBuf.fd);
356                 
357                 fd = fopen(fn, "a+");
358                 fprintf(fd, "Read: BufSize: %ld BufContent: [",
359                         nbytes);
360                 rv = fwrite(pchh, nbytes, 1, fd);
361                 if (!rv) printf("failed to write debug to %s!\n", fn);
362                 fprintf(fd, "]\n");
363                 
364                 
365                 fclose(fd);
366         }
367 #endif
368         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
369
370         if (rc == 0)
371         {               
372 #ifdef BIGBAD_IODBG
373                 {
374                         int rv = 0;
375                         char fn [SIZ];
376                         FILE *fd;
377                         const char *pch = ChrPtr(IO->SendBuf.Buf);
378                         const char *pchh = IO->SendBuf.ReadWritePointer;
379                         long nbytes;
380
381                         if (pchh == NULL)
382                                 pchh = pch;
383                         
384                         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
385                         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", 
386                                  ((CitContext*)(IO->CitContext))->ServiceName, 
387                                  IO->SendBuf.fd);
388                 
389                         fd = fopen(fn, "a+");
390                         fprintf(fd, "Read: BufSize: %ld BufContent: [",
391                                 nbytes);
392                         rv = fwrite(pchh, nbytes, 1, fd);
393                         if (!rv) printf("failed to write debug to %s!\n", fn);
394                         fprintf(fd, "]\n");
395                 
396                         
397                         fclose(fd);
398                 }
399 #endif
400                 ev_io_stop(event_base, &IO->send_event);
401                 switch (IO->NextState) {
402                 case eSendMore:
403                         assert(IO->SendDone);
404                         IO->NextState = IO->SendDone(IO);
405
406                         if ((IO->NextState == eTerminateConnection) ||
407                             (IO->NextState == eAbort) )
408                                 ShutDownCLient(IO);
409                         else {
410                                 ev_io_start(event_base, &IO->send_event);
411                         }
412                         break;
413                 case eSendReply:
414                     if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess) 
415                         break;
416                     IO->NextState = eReadMore;
417                 case eReadMore:
418                 case eReadMessage:
419                 case eReadPayload:
420                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
421                                 HandleInbound(IO);
422                         }
423                         else {
424                                 ev_io_start(event_base, &IO->recv_event);
425                         }
426
427                         break;
428                 case eDBQuery:
429                         /* we now live in another queue, so we have to unregister. */
430                         ev_cleanup_stop(loop, &IO->abort_by_shutdown);
431                         break;
432                 case eSendDNSQuery:
433                 case eReadDNSReply:
434                 case eConnect:
435                 case eTerminateConnection:
436                 case eAbort:
437                         break;
438                 }
439         }
440         else if (rc < 0) {
441                 assert(IO->Timeout);
442                 IO->Timeout(IO);
443         }
444         /* else : must write more. */
445 }
446 static void
447 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
448 {
449         
450         switch(IO->NextState) {
451         case eReadMore:
452         case eReadMessage:
453                 ev_io_start(event_base, &IO->recv_event);
454                 break;
455         case eSendReply:
456         case eSendMore:
457         case eReadPayload:
458                 become_session(IO->CitContext);
459                 IO_send_callback(loop, &IO->send_event, revents);
460                 break;
461         case eDBQuery:
462         case eSendDNSQuery:
463         case eReadDNSReply:
464         case eConnect:
465         case eTerminateConnection:
466         case eAbort:
467                 /// TODO: WHUT?
468                 break;
469         }
470 }
471
472 static void
473 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
474 {
475         AsyncIO *IO = watcher->data;
476
477         ev_timer_stop (event_base, &IO->rw_timeout);
478         become_session(IO->CitContext);
479
480         if (IO->SendBuf.fd != 0)
481         {
482                 ev_io_stop(event_base, &IO->send_event);
483                 ev_io_stop(event_base, &IO->recv_event);
484                 ev_timer_stop (event_base, &IO->rw_timeout);
485                 close(IO->SendBuf.fd);
486                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
487         }
488
489         assert(IO->Timeout);
490         switch (IO->Timeout(IO))
491         {
492         case eAbort:
493                 ShutDownCLient(IO);
494         default:
495                 break;
496         }
497 }
498
499 static void
500 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
501 {
502         AsyncIO *IO = watcher->data;
503
504         ev_timer_stop (event_base, &IO->conn_fail);
505
506         if (IO->SendBuf.fd != 0)
507         {
508                 ev_io_stop(loop, &IO->conn_event);
509                 ev_io_stop(event_base, &IO->send_event);
510                 ev_io_stop(event_base, &IO->recv_event);
511                 ev_timer_stop (event_base, &IO->rw_timeout);
512                 close(IO->SendBuf.fd);
513                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
514         }
515         become_session(IO->CitContext);
516
517         assert(IO->ConnFail);
518         switch (IO->ConnFail(IO))
519         {
520         case eAbort:
521                 ShutDownCLient(IO);
522         default:
523                 break;
524
525         }
526 }
527
528 static void
529 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
530 {
531         AsyncIO *IO = watcher->data;
532
533         ev_idle_stop (event_base, &IO->conn_fail_immediate);
534
535         if (IO->SendBuf.fd != 0)
536         {
537                 close(IO->SendBuf.fd);
538                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
539         }
540         become_session(IO->CitContext);
541
542         assert(IO->ConnFail);
543         switch (IO->ConnFail(IO))
544         {
545         case eAbort:
546                 ShutDownCLient(IO);
547         default:
548                 break;
549
550         }
551 }
552
553 static void
554 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
555 {
556         AsyncIO *IO = watcher->data;
557
558         ev_io_stop(loop, &IO->conn_event);
559         ev_timer_stop (event_base, &IO->conn_fail);
560         set_start_callback(loop, IO, revents);
561 }
562 static void
563 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
564 {
565         ssize_t nbytes;
566         AsyncIO *IO = watcher->data;
567
568         nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
569 #ifdef BIGBAD_IODBG
570         {
571                 int rv = 0;
572                 char fn [SIZ];
573                 FILE *fd;
574                 const char *pch = ChrPtr(IO->RecvBuf.Buf);
575                 const char *pchh = IO->RecvBuf.ReadWritePointer;
576                 long nbytes;
577                 
578                 if (pchh == NULL)
579                         pchh = pch;
580                 
581                 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
582                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
583                          ((CitContext*)(IO->CitContext))->ServiceName, 
584                          IO->SendBuf.fd);
585                 
586                 fd = fopen(fn, "a+");
587                 fprintf(fd, "Read: BufSize: %ld BufContent: [",
588                         nbytes);
589                 rv = fwrite(pchh, nbytes, 1, fd);
590                 if (!rv) printf("failed to write debug to %s!\n", fn);
591                 fprintf(fd, "]\n");
592                 
593                 
594                 fclose(fd);
595         }
596 #endif
597         if (nbytes > 0) {
598                 HandleInbound(IO);
599         } else if (nbytes == 0) {
600                 assert(IO->Timeout);
601
602                 switch (IO->Timeout(IO))
603                 {
604                 case eAbort:
605                         ShutDownCLient(IO);
606                 default:
607                         break;
608                 }
609                 return;
610         } else if (nbytes == -1) {
611 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
612                 CtdlLogPrintf(CTDL_DEBUG,
613                               "EVENT: Socket Invalid! %s \n",
614                               strerror(errno));
615                 return;
616         }
617 }
618
619 void
620 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
621 {
622         AsyncIO *IO = watcher->data;
623         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
624         become_session(IO->CitContext);
625
626         switch (IO->DNSQuery->PostDNS(IO))
627         {
628         case eAbort:
629             ShutDownCLient(IO);
630         default:
631             break;
632         }
633 }
634
635 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
636 {
637         int fdflags; 
638         int rc = -1;
639
640         IO->SendBuf.fd = IO->RecvBuf.fd = 
641                 socket(
642                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, 
643                         SOCK_STREAM, 
644                         IPPROTO_TCP);
645
646         if (IO->SendBuf.fd < 0) {
647                 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
648                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
649                 return eAbort;
650         }
651         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
652         if (fdflags < 0) {
653                 CtdlLogPrintf(CTDL_DEBUG,
654                               "EVENT: unable to get socket flags! %s \n",
655                               strerror(errno));
656                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
657                 return eAbort;
658         }
659         fdflags = fdflags | O_NONBLOCK;
660         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
661                 CtdlLogPrintf(CTDL_DEBUG,
662                               "EVENT: unable to set socket nonblocking flags! %s \n",
663                               strerror(errno));
664                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
665                 close(IO->SendBuf.fd);
666                 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
667                 return eAbort;
668         }
669 /* TODO: maye we could use offsetof() to calc the position of data... 
670  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
671  */
672         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
673         IO->recv_event.data = IO;
674         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
675         IO->send_event.data = IO;
676
677         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
678         IO->conn_fail.data = IO;
679         ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout, 0);
680         IO->rw_timeout.data = IO;
681
682
683         /*  Bypass it like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
684 ///     ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = inet_addr("127.0.0.1");
685         if (IO->ConnectMe->IPv6)
686                 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
687         else
688                 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
689
690         if (rc >= 0){
691                 CtdlLogPrintf(CTDL_DEBUG, "connect() immediate success.\n");
692                 set_start_callback(event_base, IO, 0);
693                 ev_timer_start(event_base, &IO->rw_timeout);
694                 return IO->NextState;
695         }
696         else if (errno == EINPROGRESS) {
697                 CtdlLogPrintf(CTDL_DEBUG, "connect() have to wait now.\n");
698
699                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
700                 IO->conn_event.data = IO;
701
702                 ev_io_start(event_base, &IO->conn_event);
703                 ev_timer_start(event_base, &IO->conn_fail);
704                 return IO->NextState;
705         }
706         else {
707                 ev_idle_init(&IO->conn_fail_immediate,
708                              IO_connfailimmediate_callback);
709                 IO->conn_fail_immediate.data = IO;
710                 ev_idle_start(event_base, &IO->conn_fail_immediate);
711                 
712                 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
713                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
714                 return IO->NextState;
715         }
716         return IO->NextState;
717 }
718
719 void SetNextTimeout(AsyncIO *IO, double timeout)
720 {
721         IO->rw_timeout.repeat = timeout;
722         ev_timer_again (event_base,  &IO->rw_timeout);
723 }
724
725 eNextState InitEventIO(AsyncIO *IO, 
726                        void *pData, 
727                        double conn_timeout, 
728                        double first_rw_timeout,
729                        int ReadFirst)
730 {
731         IO->Data = pData;
732         become_session(IO->CitContext);
733         
734         if (ReadFirst) {
735                 IO->NextState = eReadMessage;
736         }
737         else {
738                 IO->NextState = eSendReply;
739         }
740         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
741 }
742
743 eNextState ReAttachIO(AsyncIO *IO, 
744                       void *pData, 
745                       int ReadFirst)
746 {
747         IO->Data = pData;
748         become_session(IO->CitContext);
749         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
750         if (ReadFirst) {
751                 IO->NextState = eReadMessage;
752         }
753         else {
754                 IO->NextState = eSendReply;
755         }
756         set_start_callback(event_base, IO, 0);
757
758         return IO->NextState;
759 }