tiny fixes
[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->abort_by_shutdown, 
99                         IO_abort_shutdown_callback);
100         IO->abort_by_shutdown.data = IO;
101         ev_cleanup_start(event_db, &IO->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->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->unwind_stack);
137
138         assert(IO->ReadDone);
139         switch (IO->ReadDone(IO))
140         {
141         case eAbort:
142             ShutDownDBCLient(IO);
143         default:
144             break;
145         }
146 }
147
148 void NextDBOperation(AsyncIO *IO, IO_CallBack CB)
149 {
150         IO->ReadDone = CB;
151         ev_idle_init(&IO->unwind_stack,
152                      DB_PerformNext);
153         IO->unwind_stack.data = IO;
154         ev_idle_start(event_db, &IO->unwind_stack);
155 }
156
157 /*--------------------------------------------------------------------------------
158  * Client IO 
159  */
160 extern int evbase_count;
161 extern citthread_mutex_t EventQueueMutex;
162 extern HashList *InboundEventQueue;
163 extern struct ev_loop *event_base;
164 extern ev_async AddJob;   
165 extern ev_async ExitEventLoop;
166
167
168 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
169 {
170         IOAddHandler *h;
171         int i;
172
173         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
174         h->IO = IO;
175         h->EvAttch = CB;
176         ev_cleanup_init(&IO->abort_by_shutdown, 
177                         IO_abort_shutdown_callback);
178         IO->abort_by_shutdown.data = IO;
179         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
180
181         citthread_mutex_lock(&EventQueueMutex);
182         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
183         i = ++evbase_count;
184         Put(InboundEventQueue, IKEY(i), h, NULL);
185         citthread_mutex_unlock(&EventQueueMutex);
186
187         ev_async_send (event_base, &AddJob);
188         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
189         return 0;
190 }
191
192 int ShutDownEventQueue(void)
193 {
194         citthread_mutex_lock(&DBEventQueueMutex);
195         ev_async_send (event_db, &DBExitEventLoop);
196         citthread_mutex_unlock(&DBEventQueueMutex);
197
198         citthread_mutex_lock(&EventQueueMutex);
199         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
200         citthread_mutex_unlock(&EventQueueMutex);
201         return 0;
202 }
203
204 void FreeAsyncIOContents(AsyncIO *IO)
205 {
206         FreeStrBuf(&IO->IOBuf);
207         FreeStrBuf(&IO->SendBuf.Buf);
208         FreeStrBuf(&IO->RecvBuf.Buf);
209 }
210
211
212 void StopClientWatchers(AsyncIO *IO)
213 {
214         ev_timer_stop(event_base, &IO->conn_fail);
215         ev_io_stop(event_base, &IO->conn_event);
216         ev_idle_stop(event_base, &IO->unwind_stack);
217
218         if (IO->SendBuf.fd != 0)
219         {
220                 ev_io_stop(event_base, &IO->send_event);
221                 ev_io_stop(event_base, &IO->recv_event);
222                 ev_timer_stop (event_base, &IO->rw_timeout);
223                 close(IO->SendBuf.fd);
224                 IO->SendBuf.fd = 0;
225                 IO->RecvBuf.fd = 0;
226         }
227 }
228
229 void ShutDownCLient(AsyncIO *IO)
230 {
231         CitContext *Ctx =IO->CitContext;
232         become_session(Ctx);
233
234         CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
235
236         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
237         StopClientWatchers(IO);
238
239         if (IO->DNSChannel != NULL) {
240                 ares_destroy(IO->DNSChannel);
241                 ev_io_stop(event_base, &IO->dns_recv_event);
242                 ev_io_stop(event_base, &IO->dns_send_event);
243                 IO->DNSChannel = NULL;
244         }
245         assert(IO->Terminate);
246         IO->Terminate(IO);
247         Ctx->state = CON_IDLE;
248         Ctx->kill_me = 1;
249 }
250
251
252 eReadState HandleInbound(AsyncIO *IO)
253 {
254         eReadState Finished = eBufferNotEmpty;
255         
256         become_session(IO->CitContext);
257
258         while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
259                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
260                                 
261                 }
262                 else { /* Reading lines... */
263 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
264                         if (IO->LineReader)
265                                 Finished = IO->LineReader(IO);
266                         else 
267                                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
268                                 
269                         switch (Finished) {
270                         case eMustReadMore: /// read new from socket... 
271                                 return Finished;
272                                 break;
273                         case eBufferNotEmpty: /* shouldn't happen... */
274                         case eReadSuccess: /// done for now...
275                                 break;
276                         case eReadFail: /// WHUT?
277                                 ///todo: shut down! 
278                                 break;
279                         }
280                                         
281                 }
282                         
283                 if (Finished != eMustReadMore) {
284                         assert(IO->ReadDone);
285                         ev_io_stop(event_base, &IO->recv_event);
286                         IO->NextState = IO->ReadDone(IO);
287                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
288                 }
289         }
290
291
292         if ((IO->NextState == eSendReply) ||
293             (IO->NextState == eSendMore))
294         {
295                 assert(IO->SendDone);
296                 IO->NextState = IO->SendDone(IO);
297                 ev_io_start(event_base, &IO->send_event);
298         }
299         else if ((IO->NextState == eTerminateConnection) ||
300                  (IO->NextState == eAbort) )
301                 ShutDownCLient(IO);
302         return Finished;
303 }
304
305
306 static void
307 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
308 {
309         int rc;
310         AsyncIO *IO = watcher->data;
311
312         become_session(IO->CitContext);
313         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
314
315         if (rc == 0)
316         {               
317 #ifdef BIGBAD_IODBG
318                 {
319                         int rv = 0;
320                         char fn [SIZ];
321                         FILE *fd;
322                         const char *pch = ChrPtr(IO->SendBuf.Buf);
323                         const char *pchh = IO->SendBuf.ReadWritePointer;
324                         long nbytes;
325
326                         if (pchh == NULL)
327                                 pchh = pch;
328                         
329                         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
330                         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->SendBuf.fd);
331                 
332                         fd = fopen(fn, "a+");
333                         fprintf(fd, "Read: BufSize: %ld BufContent: [",
334                                 nbytes);
335                         rv = fwrite(pchh, nbytes, 1, fd);
336                         if (!rv) printf("failed to write debug!");
337                         fprintf(fd, "]\n");
338                 
339                         
340                         fclose(fd);
341                 }
342 #endif
343                 ev_io_stop(event_base, &IO->send_event);
344                 switch (IO->NextState) {
345                 case eSendReply:
346                         break;
347                 case eSendMore:
348                         assert(IO->SendDone);
349                         IO->NextState = IO->SendDone(IO);
350
351                         if ((IO->NextState == eTerminateConnection) ||
352                             (IO->NextState == eAbort) )
353                                 ShutDownCLient(IO);
354                         else {
355                                 ev_io_start(event_base, &IO->send_event);
356                         }
357                         break;
358                 case eReadMessage:
359                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
360                                 HandleInbound(IO);
361                         }
362                         else {
363                                 ev_io_start(event_base, &IO->recv_event);
364                         }
365
366                         break;
367                 case eSendDNSQuery:
368                 case eReadDNSReply:
369                 case eConnect:
370                 case eTerminateConnection:
371                 case eAbort:
372                         break;
373                 }
374         }
375         else if (rc < 0) {
376                 assert(IO->Timeout);
377                 IO->Timeout(IO);
378         }
379         /* else : must write more. */
380 }
381 static void
382 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
383 {
384         
385         switch(IO->NextState) {
386         case eReadMessage:
387                 ev_io_start(event_base, &IO->recv_event);
388                 break;
389         case eSendReply:
390         case eSendMore:
391                 become_session(IO->CitContext);
392                 IO_send_callback(loop, &IO->send_event, revents);
393                 break;
394         case eSendDNSQuery:
395         case eReadDNSReply:
396         case eConnect:
397         case eTerminateConnection:
398         case eAbort:
399                 /// TODO: WHUT?
400                 break;
401         }
402 }
403
404 static void
405 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
406 {
407         AsyncIO *IO = watcher->data;
408
409         ev_timer_stop (event_base, &IO->rw_timeout);
410         become_session(IO->CitContext);
411
412         if (IO->SendBuf.fd != 0)
413         {
414                 ev_io_stop(event_base, &IO->send_event);
415                 ev_io_stop(event_base, &IO->recv_event);
416                 ev_timer_stop (event_base, &IO->rw_timeout);
417                 close(IO->SendBuf.fd);
418                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
419         }
420
421         assert(IO->Timeout);
422         switch (IO->Timeout(IO))
423         {
424         case eAbort:
425                 ShutDownCLient(IO);
426         default:
427                 break;
428         }
429 }
430
431 static void
432 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
433 {
434         AsyncIO *IO = watcher->data;
435
436         ev_timer_stop (event_base, &IO->conn_fail);
437
438         if (IO->SendBuf.fd != 0)
439         {
440                 ev_io_stop(loop, &IO->conn_event);
441                 ev_io_stop(event_base, &IO->send_event);
442                 ev_io_stop(event_base, &IO->recv_event);
443                 ev_timer_stop (event_base, &IO->rw_timeout);
444                 close(IO->SendBuf.fd);
445                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
446         }
447         become_session(IO->CitContext);
448
449         assert(IO->ConnFail);
450         switch (IO->ConnFail(IO))
451         {
452         case eAbort:
453                 ShutDownCLient(IO);
454         default:
455                 break;
456
457         }
458 }
459
460 static void
461 IO_connfailimmediate_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
462 {
463         AsyncIO *IO = watcher->data;
464
465         ev_idle_stop (event_base, &IO->conn_fail_immediate);
466
467         if (IO->SendBuf.fd != 0)
468         {
469                 close(IO->SendBuf.fd);
470                 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
471         }
472         become_session(IO->CitContext);
473
474         assert(IO->ConnFail);
475         switch (IO->ConnFail(IO))
476         {
477         case eAbort:
478                 ShutDownCLient(IO);
479         default:
480                 break;
481
482         }
483 }
484
485 static void
486 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
487 {
488         AsyncIO *IO = watcher->data;
489
490         ev_io_stop(loop, &IO->conn_event);
491         ev_timer_stop (event_base, &IO->conn_fail);
492         set_start_callback(loop, IO, revents);
493 }
494 static void
495 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
496 {
497         ssize_t nbytes;
498         AsyncIO *IO = watcher->data;
499
500         nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
501         if (nbytes > 0) {
502                 HandleInbound(IO);
503         } else if (nbytes == 0) {
504                 assert(IO->Timeout);
505
506                 switch (IO->Timeout(IO))
507                 {
508                 case eAbort:
509                         ShutDownCLient(IO);
510                 default:
511                         break;
512                 }
513                 return;
514         } else if (nbytes == -1) {
515 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
516                 CtdlLogPrintf(CTDL_DEBUG,
517                               "EVENT: Socket Invalid! %s \n",
518                               strerror(errno));
519                 return;
520         }
521 }
522
523 void
524 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
525 {
526         AsyncIO *IO = watcher->data;
527         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
528         become_session(IO->CitContext);
529
530         switch (IO->DNSQuery->PostDNS(IO))
531         {
532         case eAbort:
533             ShutDownCLient(IO);
534         default:
535             break;
536         }
537 }
538
539 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
540 {
541         int fdflags; 
542         int rc = -1;
543
544         IO->SendBuf.fd = IO->RecvBuf.fd = 
545                 socket(
546                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, 
547                         SOCK_STREAM, 
548                         IPPROTO_TCP);
549
550         if (IO->SendBuf.fd < 0) {
551                 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
552                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
553                 return eAbort;
554         }
555         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
556         if (fdflags < 0) {
557                 CtdlLogPrintf(CTDL_DEBUG,
558                               "EVENT: unable to get socket flags! %s \n",
559                               strerror(errno));
560                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
561                 return eAbort;
562         }
563         fdflags = fdflags | O_NONBLOCK;
564         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
565                 CtdlLogPrintf(CTDL_DEBUG,
566                               "EVENT: unable to set socket nonblocking flags! %s \n",
567                               strerror(errno));
568                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
569                 close(IO->SendBuf.fd);
570                 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
571                 return eAbort;
572         }
573 /* TODO: maye we could use offsetof() to calc the position of data... 
574  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
575  */
576         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
577         IO->recv_event.data = IO;
578         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
579         IO->send_event.data = IO;
580
581         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
582         IO->conn_fail.data = IO;
583         ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout, 0);
584         IO->rw_timeout.data = IO;
585
586
587         /*  Bypass it like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
588 ///     ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr = inet_addr("127.0.0.1");
589         if (IO->ConnectMe->IPv6)
590                 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
591         else
592                 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
593
594         if (rc >= 0){
595                 CtdlLogPrintf(CTDL_DEBUG, "connect() immediate success.\n");
596                 set_start_callback(event_base, IO, 0);
597                 ev_timer_start(event_base, &IO->rw_timeout);
598                 return IO->NextState;
599         }
600         else if (errno == EINPROGRESS) {
601                 CtdlLogPrintf(CTDL_DEBUG, "connect() have to wait now.\n");
602
603                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
604                 IO->conn_event.data = IO;
605
606                 ev_io_start(event_base, &IO->conn_event);
607                 ev_timer_start(event_base, &IO->conn_fail);
608                 return IO->NextState;
609         }
610         else {
611                 ev_idle_init(&IO->conn_fail_immediate,
612                              IO_connfailimmediate_callback);
613                 IO->conn_fail_immediate.data = IO;
614                 ev_idle_start(event_base, &IO->conn_fail_immediate);
615                 
616                 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
617                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
618                 return IO->NextState;
619         }
620         return IO->NextState;
621 }
622
623 void SetNextTimeout(AsyncIO *IO, double timeout)
624 {
625         IO->rw_timeout.repeat = timeout;
626         ev_timer_again (event_base,  &IO->rw_timeout);
627 }
628
629 eNextState InitEventIO(AsyncIO *IO, 
630                        void *pData, 
631                        double conn_timeout, 
632                        double first_rw_timeout,
633                        int ReadFirst)
634 {
635         IO->Data = pData;
636         become_session(IO->CitContext);
637         
638         if (ReadFirst) {
639                 IO->NextState = eReadMessage;
640         }
641         else {
642                 IO->NextState = eSendReply;
643         }
644         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
645 }