don't call curl_multi_add_handle() from outside of the event queue
[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         syslog(LOG_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 pthread_mutex_t DBEventQueueMutex;
85 extern HashList *DBInboundEventQueue;
86 extern struct ev_loop *event_db;
87 extern ev_async DBAddJob;   
88 extern ev_async DBExitEventLoop;
89
90 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
91 {
92         IOAddHandler *h;
93         int i;
94
95         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
96         h->IO = IO;
97         h->EvAttch = CB;
98         ev_cleanup_init(&IO->db_abort_by_shutdown, 
99                         IO_abort_shutdown_callback);
100         IO->db_abort_by_shutdown.data = IO;
101         ev_cleanup_start(event_db, &IO->db_abort_by_shutdown);
102
103         pthread_mutex_lock(&DBEventQueueMutex);
104         syslog(LOG_DEBUG, "DBEVENT Q\n");
105         i = ++evdb_count ;
106         Put(DBInboundEventQueue, IKEY(i), h, NULL);
107         pthread_mutex_unlock(&DBEventQueueMutex);
108
109         ev_async_send (event_db, &DBAddJob);
110         syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
111         return eDBQuery;
112 }
113
114 void ShutDownDBCLient(AsyncIO *IO)
115 {
116         CitContext *Ctx =IO->CitContext;
117         become_session(Ctx);
118
119         syslog(LOG_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         syslog(LOG_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 eSendFile:
149         case eReadMessage: 
150         case eReadMore:
151         case eReadPayload:
152         case eReadFile:
153                 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
154                 break;
155         case eTerminateConnection:
156         case eAbort:
157             ShutDownDBCLient(IO);
158         }
159 }
160
161 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
162 {
163         IO->NextDBOperation = CB;
164         ev_idle_init(&IO->db_unwind_stack,
165                      DB_PerformNext);
166         IO->db_unwind_stack.data = IO;
167         ev_idle_start(event_db, &IO->db_unwind_stack);
168         return eDBQuery;
169 }
170
171 /*--------------------------------------------------------------------------------
172  * Client IO 
173  */
174 extern int evbase_count;
175 extern pthread_mutex_t EventQueueMutex;
176 extern HashList *InboundEventQueue;
177 extern struct ev_loop *event_base;
178 extern ev_async AddJob;   
179 extern ev_async ExitEventLoop;
180
181
182 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
183 {
184         IOAddHandler *h;
185         int i;
186
187         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
188         h->IO = IO;
189         h->EvAttch = CB;
190         ev_cleanup_init(&IO->abort_by_shutdown, 
191                         IO_abort_shutdown_callback);
192         IO->abort_by_shutdown.data = IO;
193         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
194
195         pthread_mutex_lock(&EventQueueMutex);
196         syslog(LOG_DEBUG, "EVENT Q\n");
197         i = ++evbase_count;
198         Put(InboundEventQueue, IKEY(i), h, NULL);
199         pthread_mutex_unlock(&EventQueueMutex);
200
201         ev_async_send (event_base, &AddJob);
202         syslog(LOG_DEBUG, "EVENT Q Done.\n");
203         return eSendReply;
204 }
205
206 extern eNextState evcurl_handle_start(AsyncIO *IO);
207
208 eNextState QueueCurlContext(AsyncIO *IO)
209 {
210         IOAddHandler *h;
211         int i;
212
213         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
214         h->IO = IO;
215         h->EvAttch = evcurl_handle_start;
216
217         pthread_mutex_lock(&EventQueueMutex);
218         syslog(LOG_DEBUG, "EVENT Q\n");
219         i = ++evbase_count;
220         Put(InboundEventQueue, IKEY(i), h, NULL);
221         pthread_mutex_unlock(&EventQueueMutex);
222
223         ev_async_send (event_base, &AddJob);
224         syslog(LOG_DEBUG, "EVENT Q Done.\n");
225         return eSendReply;
226 }
227
228 int ShutDownEventQueue(void)
229 {
230         pthread_mutex_lock(&DBEventQueueMutex);
231         ev_async_send (event_db, &DBExitEventLoop);
232         pthread_mutex_unlock(&DBEventQueueMutex);
233
234         pthread_mutex_lock(&EventQueueMutex);
235         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
236         pthread_mutex_unlock(&EventQueueMutex);
237         return 0;
238 }
239
240 void FreeAsyncIOContents(AsyncIO *IO)
241 {
242         FreeStrBuf(&IO->IOBuf);
243         FreeStrBuf(&IO->SendBuf.Buf);
244         FreeStrBuf(&IO->RecvBuf.Buf);
245 }
246
247
248 void StopClientWatchers(AsyncIO *IO)
249 {
250         ev_timer_stop(event_base, &IO->conn_fail);
251         ev_io_stop(event_base, &IO->conn_event);
252         ev_idle_stop(event_base, &IO->unwind_stack);
253
254         ev_io_stop(event_base, &IO->send_event);
255         ev_io_stop(event_base, &IO->recv_event);
256         ev_timer_stop (event_base, &IO->rw_timeout);
257         close(IO->SendBuf.fd);
258         IO->SendBuf.fd = 0;
259         IO->RecvBuf.fd = 0;
260 }
261
262 void ShutDownCLient(AsyncIO *IO)
263 {
264         CitContext *Ctx =IO->CitContext;
265         become_session(Ctx);
266
267         syslog(LOG_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
268
269         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
270         StopClientWatchers(IO);
271
272         if (IO->DNSChannel != NULL) {
273                 ares_destroy(IO->DNSChannel);
274                 ev_io_stop(event_base, &IO->dns_recv_event);
275                 ev_io_stop(event_base, &IO->dns_send_event);
276                 IO->DNSChannel = NULL;
277         }
278         assert(IO->Terminate);
279         IO->Terminate(IO);
280         Ctx->state = CON_IDLE;
281         Ctx->kill_me = 1;
282 }
283
284
285 eReadState HandleInbound(AsyncIO *IO)
286 {
287         const char *Err = NULL;
288         eReadState Finished = eBufferNotEmpty;
289         
290         become_session(IO->CitContext);
291
292         while ((Finished == eBufferNotEmpty) && 
293                ((IO->NextState == eReadMessage)||
294                 (IO->NextState == eReadMore)||
295                 (IO->NextState == eReadFile)||
296                 (IO->NextState == eReadPayload)))
297         {
298                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
299                                 
300                 }
301                 else { /* Reading lines... */
302 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
303                         if ((IO->NextState == eReadFile) && 
304                             (Finished == eBufferNotEmpty))
305                         {
306                                 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
307                                 if (Finished == eReadSuccess)
308                                 {
309                                         IO->NextState = eSendReply;                             
310                                 }
311                         }
312                         else if (IO->LineReader)
313                                 Finished = IO->LineReader(IO);
314                         else 
315                                 Finished = StrBufChunkSipLine(IO->IOBuf, &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                 }
329                         
330                 if (Finished != eMustReadMore) {
331                         assert(IO->ReadDone);
332                         ev_io_stop(event_base, &IO->recv_event);
333                         IO->NextState = IO->ReadDone(IO);
334                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
335                 }
336         }
337
338         switch (IO->NextState) {
339         case eSendFile:
340                 ev_io_start(event_base, &IO->send_event);
341                 break;
342         case eSendReply:
343         case eSendMore:
344                 assert(IO->SendDone);
345                 IO->NextState = IO->SendDone(IO);
346                 ev_io_start(event_base, &IO->send_event);
347                 break;
348         case eReadPayload:
349         case eReadMore:
350         case eReadFile:
351                 ev_io_start(event_base, &IO->recv_event);
352                 break;
353         case eTerminateConnection:
354 //////TODOxxxx
355                 break;
356         case eAbort:
357                 ShutDownCLient(IO);
358                 break;
359         case eSendDNSQuery:
360         case eReadDNSReply:
361         case eDBQuery:
362         case eConnect:
363         case eReadMessage:
364                 break;
365         }
366         return Finished;
367 }
368
369
370 static void
371 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
372 {
373         int rc;
374         AsyncIO *IO = watcher->data;
375         const char *errmsg = NULL;
376
377         become_session(IO->CitContext);
378 #ifdef BIGBAD_IODBG
379         {
380                 int rv = 0;
381                 char fn [SIZ];
382                 FILE *fd;
383                 const char *pch = ChrPtr(IO->SendBuf.Buf);
384                 const char *pchh = IO->SendBuf.ReadWritePointer;
385                 long nbytes;
386                 
387                 if (pchh == NULL)
388                         pchh = pch;
389                 
390                 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
391                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
392                          ((CitContext*)(IO->CitContext))->ServiceName,
393                          IO->SendBuf.fd);
394                 
395                 fd = fopen(fn, "a+");
396                 fprintf(fd, "Send: BufSize: %ld BufContent: [",
397                         nbytes);
398                 rv = fwrite(pchh, nbytes, 1, fd);
399                 if (!rv) printf("failed to write debug to %s!\n", fn);
400                 fprintf(fd, "]\n");
401 #endif
402                 switch (IO->NextState) {
403                 case eSendFile:
404                         rc = FileSendChunked(&IO->IOB, &errmsg);
405                         if (rc < 0)
406                                 StrBufPlain(IO->ErrMsg, errmsg, -1);
407                         break;
408                 default:
409                         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &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, &IO->send_event);
448                                         break;
449                                 case eReadMessage: 
450                                 case eReadMore:
451                                 case eReadPayload:
452                                 case eReadFile:
453                                         break;
454                                 case eTerminateConnection:
455                                 case eAbort:
456                                         break;
457                                 }
458                         }
459                         break;
460                 case eSendReply:
461                     if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess) 
462                         break;
463                     IO->NextState = eReadMore;
464                 case eReadMore:
465                 case eReadMessage:
466                 case eReadPayload:
467                 case eReadFile:
468                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
469                                 HandleInbound(IO);
470                         }
471                         else {
472                                 ev_io_start(event_base, &IO->recv_event);
473                         }
474
475                         break;
476                 case eDBQuery:
477                         /* we now live in another queue, so we have to unregister. */
478                         ev_cleanup_stop(loop, &IO->abort_by_shutdown);
479                         break;
480                 case eSendDNSQuery:
481                 case eReadDNSReply:
482                 case eConnect:
483                 case eTerminateConnection:
484                 case eAbort:
485                         break;
486                 }
487         }
488         else if (rc < 0) {
489                 assert(IO->Timeout);
490                 IO->Timeout(IO);
491         }
492         /* else : must write more. */
493 }
494 static void
495 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
496 {
497         
498         switch(IO->NextState) {
499         case eReadMore:
500         case eReadMessage:
501         case eReadFile:
502                 ev_io_start(event_base, &IO->recv_event);
503                 break;
504         case eSendReply:
505         case eSendMore:
506         case eReadPayload:
507         case eSendFile:
508                 become_session(IO->CitContext);
509                 IO_send_callback(loop, &IO->send_event, revents);
510                 break;
511         case eDBQuery:
512         case eSendDNSQuery:
513         case eReadDNSReply:
514         case eConnect:
515         case eTerminateConnection:
516         case eAbort:
517                 /// TODO: WHUT?
518                 break;
519         }
520 }
521
522 static void
523 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
524 {
525         AsyncIO *IO = watcher->data;
526
527         ev_timer_stop (event_base, &IO->rw_timeout);
528         become_session(IO->CitContext);
529
530         if (IO->SendBuf.fd != 0)
531         {
532                 ev_io_stop(event_base, &IO->send_event);
533                 ev_io_stop(event_base, &IO->recv_event);
534                 ev_timer_stop (event_base, &IO->rw_timeout);
535                 close(IO->SendBuf.fd);
536                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
537         }
538
539         assert(IO->Timeout);
540         switch (IO->Timeout(IO))
541         {
542         case eAbort:
543                 ShutDownCLient(IO);
544         default:
545                 break;
546         }
547 }
548
549 static void
550 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
551 {
552         AsyncIO *IO = watcher->data;
553
554         ev_timer_stop (event_base, &IO->conn_fail);
555
556         if (IO->SendBuf.fd != 0)
557         {
558                 ev_io_stop(loop, &IO->conn_event);
559                 ev_io_stop(event_base, &IO->send_event);
560                 ev_io_stop(event_base, &IO->recv_event);
561                 ev_timer_stop (event_base, &IO->rw_timeout);
562                 close(IO->SendBuf.fd);
563                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
564         }
565         become_session(IO->CitContext);
566
567         assert(IO->ConnFail);
568         switch (IO->ConnFail(IO))
569         {
570         case eAbort:
571                 ShutDownCLient(IO);
572         default:
573                 break;
574
575         }
576 }
577
578 static void
579 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
580 {
581         AsyncIO *IO = watcher->data;
582
583         ev_idle_stop (event_base, &IO->conn_fail_immediate);
584
585         if (IO->SendBuf.fd != 0)
586         {
587                 close(IO->SendBuf.fd);
588                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
589         }
590         become_session(IO->CitContext);
591
592         assert(IO->ConnFail);
593         switch (IO->ConnFail(IO))
594         {
595         case eAbort:
596                 ShutDownCLient(IO);
597         default:
598                 break;
599
600         }
601 }
602
603 static void
604 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
605 {
606         AsyncIO *IO = watcher->data;
607
608         ev_io_stop(loop, &IO->conn_event);
609         ev_timer_stop (event_base, &IO->conn_fail);
610         set_start_callback(loop, IO, revents);
611 }
612 static void
613 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
614 {
615         const char *errmsg;
616         ssize_t nbytes;
617         AsyncIO *IO = watcher->data;
618
619         switch (IO->NextState) {
620         case eReadFile:
621                 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
622                 if (nbytes < 0)
623                         StrBufPlain(IO->ErrMsg, errmsg, -1);
624                 else 
625                 {
626                         if (IO->IOB.ChunkSendRemain == 0)
627                         {
628                                 IO->NextState = eSendReply;
629                         }
630                         else
631                                 return;
632                 }
633                 break;
634         default:
635                 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
636                 break;
637         }
638
639 #ifdef BIGBAD_IODBG
640         {
641                 int rv = 0;
642                 char fn [SIZ];
643                 FILE *fd;
644                 const char *pch = ChrPtr(IO->RecvBuf.Buf);
645                 const char *pchh = IO->RecvBuf.ReadWritePointer;
646                 long nbytes;
647                 
648                 if (pchh == NULL)
649                         pchh = pch;
650                 
651                 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
652                 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
653                          ((CitContext*)(IO->CitContext))->ServiceName, 
654                          IO->SendBuf.fd);
655                 
656                 fd = fopen(fn, "a+");
657                 fprintf(fd, "Read: BufSize: %ld BufContent: [",
658                         nbytes);
659                 rv = fwrite(pchh, nbytes, 1, fd);
660                 if (!rv) printf("failed to write debug to %s!\n", fn);
661                 fprintf(fd, "]\n");
662                 
663                 
664                 fclose(fd);
665         }
666 #endif
667         if (nbytes > 0) {
668                 HandleInbound(IO);
669         } else if (nbytes == 0) {
670                 assert(IO->Timeout);
671
672                 switch (IO->Timeout(IO))
673                 {
674                 case eAbort:
675                         ShutDownCLient(IO);
676                 default:
677                         break;
678                 }
679                 return;
680         } else if (nbytes == -1) {
681 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
682                 syslog(LOG_DEBUG, 
683                        "EVENT: Socket Invalid! %s \n",
684                        strerror(errno));
685                 ShutDownCLient(IO);
686                 return;
687         }
688 }
689
690 void
691 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
692 {
693         AsyncIO *IO = watcher->data;
694         syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
695         become_session(IO->CitContext);
696         assert(IO->DNSFail);
697         assert(IO->DNSQuery->PostDNS);
698         switch (IO->DNSQuery->PostDNS(IO))
699         {
700         case eAbort:
701                 IO->DNSFail(IO);
702         default:
703             break;
704         }
705 }
706
707 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
708 {
709         int fdflags; 
710         int rc = -1;
711
712         IO->SendBuf.fd = IO->RecvBuf.fd = 
713                 socket(
714                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, 
715                         SOCK_STREAM, 
716                         IPPROTO_TCP);
717
718         if (IO->SendBuf.fd < 0) {
719                 syslog(LOG_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
720                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
721                 return eAbort;
722         }
723         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
724         if (fdflags < 0) {
725                 syslog(LOG_DEBUG, 
726                        "EVENT: unable to get socket flags! %s \n",
727                        strerror(errno));
728                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
729                 return eAbort;
730         }
731         fdflags = fdflags | O_NONBLOCK;
732         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
733                 syslog(LOG_DEBUG, 
734                        "EVENT: unable to set socket nonblocking flags! %s \n",
735                        strerror(errno));
736                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
737                 close(IO->SendBuf.fd);
738                 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
739                 return eAbort;
740         }
741 /* TODO: maye we could use offsetof() to calc the position of data... 
742  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
743  */
744         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
745         IO->recv_event.data = IO;
746         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
747         IO->send_event.data = IO;
748
749         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
750         IO->conn_fail.data = IO;
751         ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout, 0);
752         IO->rw_timeout.data = IO;
753
754
755         /*  Bypass it like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
756 ///     ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = inet_addr("127.0.0.1");
757         if (IO->ConnectMe->IPv6)
758                 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
759         else
760                 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
761
762         if (rc >= 0){
763                 syslog(LOG_DEBUG, "connect() immediate success.\n");
764                 set_start_callback(event_base, IO, 0);
765                 ev_timer_start(event_base, &IO->rw_timeout);
766                 return IO->NextState;
767         }
768         else if (errno == EINPROGRESS) {
769                 syslog(LOG_DEBUG, "connect() have to wait now.\n");
770
771                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
772                 IO->conn_event.data = IO;
773
774                 ev_io_start(event_base, &IO->conn_event);
775                 ev_timer_start(event_base, &IO->conn_fail);
776                 return IO->NextState;
777         }
778         else {
779                 ev_idle_init(&IO->conn_fail_immediate,
780                              IO_connfailimmediate_callback);
781                 IO->conn_fail_immediate.data = IO;
782                 ev_idle_start(event_base, &IO->conn_fail_immediate);
783                 
784                 syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
785                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
786                 return IO->NextState;
787         }
788         return IO->NextState;
789 }
790
791 void SetNextTimeout(AsyncIO *IO, double timeout)
792 {
793         IO->rw_timeout.repeat = timeout;
794         ev_timer_again (event_base,  &IO->rw_timeout);
795 }
796
797 eNextState InitEventIO(AsyncIO *IO, 
798                        void *pData, 
799                        double conn_timeout, 
800                        double first_rw_timeout,
801                        int ReadFirst)
802 {
803         IO->Data = pData;
804         become_session(IO->CitContext);
805         
806         if (ReadFirst) {
807                 IO->NextState = eReadMessage;
808         }
809         else {
810                 IO->NextState = eSendReply;
811         }
812         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
813 }
814
815 eNextState ReAttachIO(AsyncIO *IO, 
816                       void *pData, 
817                       int ReadFirst)
818 {
819         IO->Data = pData;
820         become_session(IO->CitContext);
821         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
822         if (ReadFirst) {
823                 IO->NextState = eReadMessage;
824         }
825         else {
826                 IO->NextState = eSendReply;
827         }
828         set_start_callback(event_base, IO, 0);
829
830         return IO->NextState;
831 }