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