libev migration: use async methods to schedule jobs. thanks to Marc Lehmann for the...
[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         
77 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
78 {
79         IOAddHandler *h;
80         int i;
81
82         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
83         h->IO = IO;
84         h->EvAttch = CB;
85
86         citthread_mutex_lock(&EventQueueMutex);
87         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
88         i = GetCount(InboundEventQueue);
89         Put(InboundEventQueue, IKEY(i), h, NULL);
90         citthread_mutex_unlock(&EventQueueMutex);
91
92         ev_async_send (event_base, &AddJob);
93         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
94         return 0;
95 }
96
97
98 int ShutDownEventQueue(void)
99 {
100         citthread_mutex_lock(&EventQueueMutex);
101         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
102         citthread_mutex_unlock(&EventQueueMutex);
103         return 0;
104 }
105
106 void FreeAsyncIOContents(AsyncIO *IO)
107 {
108         FreeStrBuf(&IO->IOBuf);
109         FreeStrBuf(&IO->SendBuf.Buf);
110         FreeStrBuf(&IO->RecvBuf.Buf);
111 }
112
113
114 void ShutDownCLient(AsyncIO *IO)
115 {
116         CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
117
118         if (IO->sock != 0)
119         {
120                 ev_io_stop(event_base, &IO->send_event);
121                 ev_io_stop(event_base, &IO->recv_event);
122                 ev_timer_stop (event_base, &IO->rw_timeout);
123                 close(IO->sock);
124                 IO->sock = 0;
125                 IO->SendBuf.fd = 0;
126                 IO->RecvBuf.fd = 0;
127         }
128         if (IO->DNSChannel != NULL) {
129                 ares_destroy(IO->DNSChannel);
130                 ev_io_stop(event_base, &IO->dns_recv_event);
131                 ev_io_stop(event_base, &IO->dns_send_event);
132                 IO->DNSChannel = NULL;
133         }
134         assert(IO->Terminate);
135         IO->Terminate(IO);
136         
137 }
138
139
140 eReadState HandleInbound(AsyncIO *IO)
141 {
142         eReadState Finished = eBufferNotEmpty;
143         
144         while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
145                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
146                                 
147                 }
148                 else { /* Reading lines... */
149 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
150                         if (IO->LineReader)
151                                 Finished = IO->LineReader(IO);
152                         else 
153                                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
154                                 
155                         switch (Finished) {
156                         case eMustReadMore: /// read new from socket... 
157                                 return Finished;
158                                 break;
159                         case eBufferNotEmpty: /* shouldn't happen... */
160                         case eReadSuccess: /// done for now...
161                                 break;
162                         case eReadFail: /// WHUT?
163                                 ///todo: shut down! 
164                                 break;
165                         }
166                                         
167                 }
168                         
169                 if (Finished != eMustReadMore) {
170                         assert(IO->ReadDone);
171                         ev_io_stop(event_base, &IO->recv_event);
172                         IO->NextState = IO->ReadDone(IO);
173                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
174                 }
175         }
176
177
178         if ((IO->NextState == eSendReply) ||
179             (IO->NextState == eSendMore))
180         {
181                 assert(IO->SendDone);
182                 IO->NextState = IO->SendDone(IO);
183                 ev_io_start(event_base, &IO->send_event);
184         }
185         else if ((IO->NextState == eTerminateConnection) ||
186                  (IO->NextState == eAbort) )
187                 ShutDownCLient(IO);
188         return Finished;
189 }
190
191
192 static void
193 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
194 {
195         int rc;
196         AsyncIO *IO = watcher->data;
197
198         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
199
200         if (rc == 0)
201         {               
202 #ifdef BIGBAD_IODBG
203                 {
204                         int rv = 0;
205                         char fn [SIZ];
206                         FILE *fd;
207                         const char *pch = ChrPtr(IO->SendBuf.Buf);
208                         const char *pchh = IO->SendBuf.ReadWritePointer;
209                         long nbytes;
210
211                         if (pchh == NULL)
212                                 pchh = pch;
213                         
214                         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
215                         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
216                 
217                         fd = fopen(fn, "a+");
218                         fprintf(fd, "Read: BufSize: %ld BufContent: [",
219                                 nbytes);
220                         rv = fwrite(pchh, nbytes, 1, fd);
221                         fprintf(fd, "]\n");
222                 
223                         
224                         fclose(fd);
225                 }
226 #endif
227                 ev_io_stop(event_base, &IO->send_event);
228                 switch (IO->NextState) {
229                 case eSendReply:
230                         break;
231                 case eSendMore:
232                         assert(IO->SendDone);
233                         IO->NextState = IO->SendDone(IO);
234
235                         if ((IO->NextState == eTerminateConnection) ||
236                             (IO->NextState == eAbort) )
237                                 ShutDownCLient(IO);
238                         else {
239                                 ev_io_start(event_base, &IO->send_event);
240                         }
241                         break;
242                 case eReadMessage:
243                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
244                                 HandleInbound(IO);
245                         }
246                         else {
247                                 ev_io_start(event_base, &IO->recv_event);
248                         }
249
250                         break;
251                 case eTerminateConnection:
252                 case eAbort:
253                         break;
254                 }
255         }
256         else if (rc < 0) {
257                 assert(IO->Timeout);
258                 IO->Timeout(IO);
259         }
260         /* else : must write more. */
261 }
262 static void
263 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
264 {
265         
266         switch(IO->NextState) {
267         case eReadMessage:
268                 ev_io_start(event_base, &IO->recv_event);
269                 break;
270         case eSendReply:
271         case eSendMore:
272                 IO_send_callback(loop, &IO->send_event, revents);
273                 break;
274         case eTerminateConnection:
275         case eAbort:
276                 /// TODO: WHUT?
277                 break;
278         }
279 }
280
281 static void
282 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
283 {
284         AsyncIO *IO = watcher->data;
285
286         ev_timer_stop (event_base, &IO->rw_timeout);
287         assert(IO->Timeout);
288         IO->Timeout(IO);
289 }
290 static void
291 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
292 {
293         AsyncIO *IO = watcher->data;
294
295         ev_timer_stop (event_base, &IO->conn_fail);
296         ev_io_stop(loop, &IO->conn_event);
297         assert(IO->ConnFail);
298         IO->ConnFail(IO);
299 }
300 static void
301 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
302 {
303         AsyncIO *IO = watcher->data;
304
305         ev_io_stop(loop, &IO->conn_event);
306         ev_timer_stop (event_base, &IO->conn_fail);
307         set_start_callback(loop, IO, revents);
308 }
309 static void
310 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
311 {
312         ssize_t nbytes;
313         AsyncIO *IO = watcher->data;
314
315         nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
316         if (nbytes > 0) {
317                 HandleInbound(IO);
318         } else if (nbytes == 0) {
319                 assert(IO->Timeout);
320                 IO->Timeout(IO); /* this is a timeout... */
321                 return;
322         } else if (nbytes == -1) {
323 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
324                 return;
325         }
326 }
327
328 void
329 IO_postdns_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
330 {
331         AsyncIO *IO = watcher->data;
332         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
333
334         IO->PostDNS(IO);
335 }
336
337 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
338 {
339         int fdflags; 
340         int rc = -1;
341
342         IO->SendBuf.fd = IO->RecvBuf.fd = 
343                 IO->sock = socket(
344                         (IO->IP6)?PF_INET6:PF_INET, 
345                         SOCK_STREAM, 
346                         IPPROTO_TCP);
347
348         if (IO->sock < 0) {
349                 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
350                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
351 //              freeaddrinfo(res);
352                 return eAbort;
353         }
354         fdflags = fcntl(IO->sock, F_GETFL);
355         if (fdflags < 0) {
356                 CtdlLogPrintf(CTDL_DEBUG,
357                               "EVENT: unable to get socket flags! %s \n",
358                               strerror(errno));
359                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
360                 return eAbort;
361         }
362         fdflags = fdflags | O_NONBLOCK;
363         if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
364                 CtdlLogPrintf(CTDL_DEBUG,
365                               "EVENT: unable to set socket nonblocking flags! %s \n",
366                               strerror(errno));
367                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
368                 close(IO->sock);
369                 return eAbort;
370         }
371 /* TODO: maye we could use offsetof() to calc the position of data... 
372  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
373  */
374         ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
375         IO->recv_event.data = IO;
376         ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
377         IO->send_event.data = IO;
378
379         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
380         IO->conn_fail.data = IO;
381         ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
382         IO->rw_timeout.data = IO;
383
384         if (IO->IP6)
385                 rc = connect(IO->sock, &IO->Addr, sizeof(struct sockaddr_in6));
386         else
387                 rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
388
389         if (rc >= 0){
390 ////            freeaddrinfo(res);
391                 set_start_callback(event_base, IO, 0);
392                 ev_timer_start(event_base, &IO->rw_timeout);
393                 return IO->NextState;
394         }
395         else if (errno == EINPROGRESS) {
396
397                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
398                 IO->conn_event.data = IO;
399
400                 ev_io_start(event_base, &IO->conn_event);
401                 ev_timer_start(event_base, &IO->conn_fail);
402                 return IO->NextState;
403         }
404         else {
405                 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
406                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
407                 assert(IO->ConnFail);
408                 IO->ConnFail(IO);
409                 return eAbort;
410         }
411         return IO->NextState;
412 }
413
414 void SetNextTimeout(AsyncIO *IO, double timeout)
415 {
416         IO->rw_timeout.repeat = timeout;
417         ev_timer_again (event_base,  &IO->rw_timeout);
418 }
419
420 eNextState InitEventIO(AsyncIO *IO, 
421                        void *pData, 
422                        double conn_timeout, 
423                        double first_rw_timeout,
424                        int ReadFirst)
425 {
426         IO->Data = pData;
427         
428         if (ReadFirst) {
429                 IO->NextState = eReadMessage;
430         }
431         else {
432                 IO->NextState = eSendReply;
433         }
434         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
435 }