fix loop / handling error when connecting fails immediately
[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 extern citthread_mutex_t EventQueueMutex;
71 extern HashList *InboundEventQueue;
72 extern struct ev_loop *event_base;
73 extern ev_async AddJob;   
74 extern ev_async ExitEventLoop;
75
76 static void
77 IO_abort_shutdown_callback(struct ev_loop *loop, ev_cleanup *watcher, int revents)
78 {
79         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
80
81         AsyncIO *IO = watcher->data;
82         IO->ShutdownAbort(IO);
83 }
84         
85 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
86 {
87         IOAddHandler *h;
88         int i;
89
90         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
91         h->IO = IO;
92         h->EvAttch = CB;
93         ev_cleanup_init(&IO->abort_by_shutdown, 
94                         IO_abort_shutdown_callback);
95         IO->abort_by_shutdown.data = IO;
96         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
97
98         citthread_mutex_lock(&EventQueueMutex);
99         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
100         i = GetCount(InboundEventQueue);
101         Put(InboundEventQueue, IKEY(i), h, NULL);
102         citthread_mutex_unlock(&EventQueueMutex);
103
104         ev_async_send (event_base, &AddJob);
105         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
106         return 0;
107 }
108
109
110 int ShutDownEventQueue(void)
111 {
112         citthread_mutex_lock(&EventQueueMutex);
113         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
114         citthread_mutex_unlock(&EventQueueMutex);
115         return 0;
116 }
117
118 void FreeAsyncIOContents(AsyncIO *IO)
119 {
120         FreeStrBuf(&IO->IOBuf);
121         FreeStrBuf(&IO->SendBuf.Buf);
122         FreeStrBuf(&IO->RecvBuf.Buf);
123 }
124
125
126 void StopClientWatchers(AsyncIO *IO)
127 {
128         ev_timer_stop(event_base, &IO->conn_fail);
129         ev_io_stop(event_base, &IO->conn_event);
130         ev_idle_stop(event_base, &IO->unwind_stack);
131
132         if (IO->SendBuf.fd != 0)
133         {
134                 ev_io_stop(event_base, &IO->send_event);
135                 ev_io_stop(event_base, &IO->recv_event);
136                 ev_timer_stop (event_base, &IO->rw_timeout);
137                 close(IO->SendBuf.fd);
138                 IO->SendBuf.fd = 0;
139                 IO->RecvBuf.fd = 0;
140         }
141 }
142
143 void ShutDownCLient(AsyncIO *IO)
144 {
145         CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
146         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
147
148         StopClientWatchers(IO);
149
150         if (IO->DNSChannel != NULL) {
151                 ares_destroy(IO->DNSChannel);
152                 ev_io_stop(event_base, &IO->dns_recv_event);
153                 ev_io_stop(event_base, &IO->dns_send_event);
154                 IO->DNSChannel = NULL;
155         }
156         assert(IO->Terminate);
157         become_session(IO->CitContext);
158         IO->Terminate(IO);
159         
160 }
161
162
163 eReadState HandleInbound(AsyncIO *IO)
164 {
165         eReadState Finished = eBufferNotEmpty;
166         
167         become_session(IO->CitContext);
168
169         while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
170                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
171                                 
172                 }
173                 else { /* Reading lines... */
174 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
175                         if (IO->LineReader)
176                                 Finished = IO->LineReader(IO);
177                         else 
178                                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
179                                 
180                         switch (Finished) {
181                         case eMustReadMore: /// read new from socket... 
182                                 return Finished;
183                                 break;
184                         case eBufferNotEmpty: /* shouldn't happen... */
185                         case eReadSuccess: /// done for now...
186                                 break;
187                         case eReadFail: /// WHUT?
188                                 ///todo: shut down! 
189                                 break;
190                         }
191                                         
192                 }
193                         
194                 if (Finished != eMustReadMore) {
195                         assert(IO->ReadDone);
196                         ev_io_stop(event_base, &IO->recv_event);
197                         IO->NextState = IO->ReadDone(IO);
198                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
199                 }
200         }
201
202
203         if ((IO->NextState == eSendReply) ||
204             (IO->NextState == eSendMore))
205         {
206                 assert(IO->SendDone);
207                 IO->NextState = IO->SendDone(IO);
208                 ev_io_start(event_base, &IO->send_event);
209         }
210         else if ((IO->NextState == eTerminateConnection) ||
211                  (IO->NextState == eAbort) )
212                 ShutDownCLient(IO);
213         return Finished;
214 }
215
216
217 static void
218 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
219 {
220         int rc;
221         AsyncIO *IO = watcher->data;
222
223         become_session(IO->CitContext);
224         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
225
226         if (rc == 0)
227         {               
228 #ifdef BIGBAD_IODBG
229                 {
230                         int rv = 0;
231                         char fn [SIZ];
232                         FILE *fd;
233                         const char *pch = ChrPtr(IO->SendBuf.Buf);
234                         const char *pchh = IO->SendBuf.ReadWritePointer;
235                         long nbytes;
236
237                         if (pchh == NULL)
238                                 pchh = pch;
239                         
240                         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
241                         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->SendBuf.fd);
242                 
243                         fd = fopen(fn, "a+");
244                         fprintf(fd, "Read: BufSize: %ld BufContent: [",
245                                 nbytes);
246                         rv = fwrite(pchh, nbytes, 1, fd);
247                         fprintf(fd, "]\n");
248                 
249                         
250                         fclose(fd);
251                 }
252 #endif
253                 ev_io_stop(event_base, &IO->send_event);
254                 switch (IO->NextState) {
255                 case eSendReply:
256                         break;
257                 case eSendMore:
258                         assert(IO->SendDone);
259                         IO->NextState = IO->SendDone(IO);
260
261                         if ((IO->NextState == eTerminateConnection) ||
262                             (IO->NextState == eAbort) )
263                                 ShutDownCLient(IO);
264                         else {
265                                 ev_io_start(event_base, &IO->send_event);
266                         }
267                         break;
268                 case eReadMessage:
269                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
270                                 HandleInbound(IO);
271                         }
272                         else {
273                                 ev_io_start(event_base, &IO->recv_event);
274                         }
275
276                         break;
277                 case eSendDNSQuery:
278                 case eReadDNSReply:
279                 case eConnect:
280                 case eTerminateConnection:
281                 case eAbort:
282                         break;
283                 }
284         }
285         else if (rc < 0) {
286                 assert(IO->Timeout);
287                 IO->Timeout(IO);
288         }
289         /* else : must write more. */
290 }
291 static void
292 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
293 {
294         
295         switch(IO->NextState) {
296         case eReadMessage:
297                 ev_io_start(event_base, &IO->recv_event);
298                 break;
299         case eSendReply:
300         case eSendMore:
301                 become_session(IO->CitContext);
302                 IO_send_callback(loop, &IO->send_event, revents);
303                 break;
304         case eSendDNSQuery:
305         case eReadDNSReply:
306         case eConnect:
307         case eTerminateConnection:
308         case eAbort:
309                 /// TODO: WHUT?
310                 break;
311         }
312 }
313
314 static void
315 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
316 {
317         AsyncIO *IO = watcher->data;
318
319         ev_timer_stop (event_base, &IO->rw_timeout);
320         become_session(IO->CitContext);
321
322         if (IO->SendBuf.fd != 0)
323         {
324                 ev_io_stop(event_base, &IO->send_event);
325                 ev_io_stop(event_base, &IO->recv_event);
326                 ev_timer_stop (event_base, &IO->rw_timeout);
327                 close(IO->SendBuf.fd);
328                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
329         }
330
331         assert(IO->Timeout);
332         switch (IO->Timeout(IO))
333         {
334         case eAbort:
335                 ShutDownCLient(IO);
336         default:
337                 break;
338         }
339 }
340
341 static void
342 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
343 {
344         AsyncIO *IO = watcher->data;
345
346         ev_timer_stop (event_base, &IO->conn_fail);
347
348         if (IO->SendBuf.fd != 0)
349         {
350                 ev_io_stop(loop, &IO->conn_event);
351                 ev_io_stop(event_base, &IO->send_event);
352                 ev_io_stop(event_base, &IO->recv_event);
353                 ev_timer_stop (event_base, &IO->rw_timeout);
354                 close(IO->SendBuf.fd);
355                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
356         }
357         become_session(IO->CitContext);
358
359         assert(IO->ConnFail);
360         switch (IO->ConnFail(IO))
361         {
362         case eAbort:
363                 ShutDownCLient(IO);
364         default:
365                 break;
366
367         }
368 }
369
370 static void
371 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
372 {
373         AsyncIO *IO = watcher->data;
374
375         ev_idle_stop (event_base, &IO->conn_fail_immediate);
376
377         if (IO->SendBuf.fd != 0)
378         {
379                 close(IO->SendBuf.fd);
380                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
381         }
382         become_session(IO->CitContext);
383
384         assert(IO->ConnFail);
385         switch (IO->ConnFail(IO))
386         {
387         case eAbort:
388                 ShutDownCLient(IO);
389         default:
390                 break;
391
392         }
393 }
394
395 static void
396 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
397 {
398         AsyncIO *IO = watcher->data;
399
400         ev_io_stop(loop, &IO->conn_event);
401         ev_timer_stop (event_base, &IO->conn_fail);
402         set_start_callback(loop, IO, revents);
403 }
404 static void
405 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
406 {
407         ssize_t nbytes;
408         AsyncIO *IO = watcher->data;
409
410         nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
411         if (nbytes > 0) {
412                 HandleInbound(IO);
413         } else if (nbytes == 0) {
414                 assert(IO->Timeout);
415
416                 switch (IO->Timeout(IO))
417                 {
418                 case eAbort:
419                         ShutDownCLient(IO);
420                 default:
421                         break;
422                 }
423                 return;
424         } else if (nbytes == -1) {
425 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
426                 return;
427         }
428 }
429
430 void
431 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
432 {
433         AsyncIO *IO = watcher->data;
434         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
435         become_session(IO->CitContext);
436
437         switch (IO->DNSQuery->PostDNS(IO))
438         {
439         case eAbort:
440             ShutDownCLient(IO);
441         default:
442             break;
443         }
444 }
445
446 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
447 {
448         int fdflags; 
449         int rc = -1;
450
451         IO->SendBuf.fd = IO->RecvBuf.fd = 
452                 socket(
453                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, 
454                         SOCK_STREAM, 
455                         IPPROTO_TCP);
456
457         if (IO->SendBuf.fd < 0) {
458                 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
459                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
460 //              freeaddrinfo(res);
461                 return eAbort;
462         }
463         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
464         if (fdflags < 0) {
465                 CtdlLogPrintf(CTDL_DEBUG,
466                               "EVENT: unable to get socket flags! %s \n",
467                               strerror(errno));
468                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
469                 return eAbort;
470         }
471         fdflags = fdflags | O_NONBLOCK;
472         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
473                 CtdlLogPrintf(CTDL_DEBUG,
474                               "EVENT: unable to set socket nonblocking flags! %s \n",
475                               strerror(errno));
476                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
477                 close(IO->SendBuf.fd);
478                 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
479                 return eAbort;
480         }
481 /* TODO: maye we could use offsetof() to calc the position of data... 
482  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
483  */
484         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
485         IO->recv_event.data = IO;
486         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
487         IO->send_event.data = IO;
488
489         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
490         IO->conn_fail.data = IO;
491         ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout, 0);
492         IO->rw_timeout.data = IO;
493
494         if (IO->ConnectMe->IPv6)
495                 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
496         else
497                 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
498
499         if (rc >= 0){
500                 CtdlLogPrintf(CTDL_DEBUG, "connect() immediate success.\n");
501 ////            freeaddrinfo(res);
502                 set_start_callback(event_base, IO, 0);
503                 ev_timer_start(event_base, &IO->rw_timeout);
504                 return IO->NextState;
505         }
506         else if (errno == EINPROGRESS) {
507                 CtdlLogPrintf(CTDL_DEBUG, "connect() have to wait now.\n");
508
509                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
510                 IO->conn_event.data = IO;
511
512                 ev_io_start(event_base, &IO->conn_event);
513                 ev_timer_start(event_base, &IO->conn_fail);
514                 return IO->NextState;
515         }
516         else {
517                 ev_idle_init(&IO->conn_fail_immediate,
518                              IO_connfailimmediate_callback);
519                 IO->conn_fail_immediate.data = IO;
520                 ev_idle_start(event_base, &IO->conn_fail_immediate);
521                 
522                 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
523                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
524                 return IO->NextState;
525         }
526         return IO->NextState;
527 }
528
529 void SetNextTimeout(AsyncIO *IO, double timeout)
530 {
531         IO->rw_timeout.repeat = timeout;
532         ev_timer_again (event_base,  &IO->rw_timeout);
533 }
534
535 eNextState InitEventIO(AsyncIO *IO, 
536                        void *pData, 
537                        double conn_timeout, 
538                        double first_rw_timeout,
539                        int ReadFirst)
540 {
541         IO->Data = pData;
542         become_session(IO->CitContext);
543         
544         if (ReadFirst) {
545                 IO->NextState = eReadMessage;
546         }
547         else {
548                 IO->NextState = eSendReply;
549         }
550         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
551 }