3 * Copyright (c) 1998-2009 by the citadel.org team
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.
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.
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
29 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
59 #include "internet_addressing.h"
62 #include "clientsocket.h"
63 #include "locate_host.h"
64 #include "citadel_dirs.h"
66 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
68 #include "event_client.h"
70 extern int event_add_pipe[2];
71 extern citthread_mutex_t EventQueueMutex;
72 extern HashList *InboundEventQueue;
73 extern struct ev_loop *event_base;
79 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
84 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
88 citthread_mutex_lock(&EventQueueMutex);
89 if (event_add_pipe[1] == -1) {
90 citthread_mutex_unlock(&EventQueueMutex);
94 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
95 i = GetCount(InboundEventQueue);
96 Put(InboundEventQueue, IKEY(i), h, NULL);
97 citthread_mutex_unlock(&EventQueueMutex);
99 write(event_add_pipe[1], "+_", 1);
100 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
105 int ShutDownEventQueue(void)
107 citthread_mutex_lock(&EventQueueMutex);
108 if (event_add_pipe[1] == -1) {
109 citthread_mutex_unlock(&EventQueueMutex);
113 write(event_add_pipe[1], "x_", 1);
114 close(event_add_pipe[1]);
115 event_add_pipe[1] = -1;
116 citthread_mutex_unlock(&EventQueueMutex);
120 void FreeAsyncIOContents(AsyncIO *IO)
122 FreeStrBuf(&IO->IOBuf);
123 FreeStrBuf(&IO->SendBuf.Buf);
124 FreeStrBuf(&IO->RecvBuf.Buf);
130 setup_signal_handlers(struct instance *instance)
132 signal(SIGPIPE, SIG_IGN);
134 event_set(&instance->sigterm_event, SIGTERM, EV_SIGNAL|EV_PERSIST,
135 exit_event_callback, instance);
136 event_add(&instance->sigterm_event, NULL);
138 event_set(&instance->sigint_event, SIGINT, EV_SIGNAL|EV_PERSIST,
139 exit_event_callback, instance);
140 event_add(&instance->sigint_event, NULL);
142 event_set(&instance->sigquit_event, SIGQUIT, EV_SIGNAL|EV_PERSIST,
143 exit_event_callback, instance);
144 event_add(&instance->sigquit_event, NULL);
148 void ShutDownCLient(AsyncIO *IO)
150 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
151 switch (IO->active_event) {
153 ev_io_stop(event_base, &IO->send_event);
156 ev_io_stop(event_base, &IO->recv_event);
159 ev_io_stop(event_base, &IO->conn_event);
162 // no event active here; just bail out.
166 IO->active_event = 0;
167 IO->Terminate(IO->Data);
171 eReadState HandleInbound(AsyncIO *IO)
173 eReadState Finished = eBufferNotEmpty;
175 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
176 if (IO->RecvBuf.nBlobBytesWanted != 0) {
179 else { /* Reading lines... */
180 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
182 Finished = IO->LineReader(IO);
184 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
187 case eMustReadMore: /// read new from socket...
190 case eBufferNotEmpty: /* shouldn't happen... */
191 case eReadSuccess: /// done for now...
193 case eReadFail: /// WHUT?
200 if (Finished != eMustReadMore) {
201 ev_io_stop(event_base, &IO->recv_event);
202 IO->active_event = 0;
203 IO->NextState = IO->ReadDone(IO->Data);
204 Finished = StrBufCheckBuffer(&IO->RecvBuf);
209 if ((IO->NextState == eSendReply) ||
210 (IO->NextState == eSendMore))
212 IO->NextState = IO->SendDone(IO->Data);
213 ev_io_start(event_base, &IO->send_event);
215 IO->active_event = SEND_EVENT;
218 else if ((IO->NextState == eTerminateConnection) ||
219 (IO->NextState == eAbort) )
226 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
229 AsyncIO *IO = watcher->data;
231 CtdlLogPrintf(CTDL_DEBUG, "EVENT -> %d : [%s%s%s%s]\n",
233 (event&EV_TIMEOUT) ? " timeout" : "",
234 (event&EV_READ) ? " read" : "",
235 (event&EV_WRITE) ? " write" : "",
236 (event&EV_SIGNAL) ? " signal" : "");
238 /// assert(fd == IO->sock);
240 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
250 const char *pch = ChrPtr(IO->SendBuf.Buf);
251 const char *pchh = IO->SendBuf.ReadWritePointer;
257 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
258 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
260 fd = fopen(fn, "a+");
261 fprintf(fd, "Read: BufSize: %ld BufContent: [",
263 rv = fwrite(pchh, nbytes, 1, fd);
270 ev_io_stop(event_base, &IO->send_event);
271 IO->active_event = 0;
272 switch (IO->NextState) {
276 IO->NextState = IO->SendDone(IO->Data);
278 if ((IO->NextState == eTerminateConnection) ||
279 (IO->NextState == eAbort) )
282 ev_io_start(event_base, &IO->send_event);
284 IO->active_event = SEND_EVENT;
288 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
292 ev_io_start(event_base, &IO->recv_event);
294 IO->active_event = RECV_EVENT;
298 case eTerminateConnection:
311 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
314 AsyncIO *IO = watcher->data;
316 // assert(fd == IO->sock);
318 // assert(fd == sb->fd);
320 CtdlLogPrintf(CTDL_DEBUG, "EVENT <- %d : [%s%s%s%s]\n",
322 (event&EV_TIMEOUT) ? " timeout" : "",
323 (event&EV_READ) ? " read" : "",
324 (event&EV_WRITE) ? " write" : "",
325 (event&EV_SIGNAL) ? " signal" : "");
327 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
330 } else if (nbytes == 0) {
332 /// TODO: this is a timeout??? sock_buff_invoke_free(sb, 0); seems as if socket is gone then?
334 } else if (nbytes == -1) {
335 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
342 set_start_callback(struct ev_loop *loop, ev_io *watcher, int revents)
344 AsyncIO *IO = watcher->data;
346 switch(IO->NextState) {
348 ev_io_start(event_base, &IO->recv_event);
349 IO->active_event = RECV_EVENT;
353 IO_send_callback(loop, watcher, revents);
355 case eTerminateConnection:
363 IO_connect_callback(struct ev_loop *loop, ev_io *watcher, int revents)
365 AsyncIO *IO = watcher->data;
367 ev_io_stop(event_base, &IO->conn_event);
368 IO->active_event = 0;
369 set_start_callback(loop, watcher, revents);
372 int event_connect_socket(AsyncIO *IO)
379 IO->sock = socket(IO->curr_ai->ai_family,
380 IO->curr_ai->ai_socktype,
381 IO->curr_ai->ai_protocol);
384 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
385 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
386 // freeaddrinfo(res);
389 fdflags = fcntl(IO->sock, F_GETFL);
391 CtdlLogPrintf(CTDL_DEBUG,
392 "EVENT: unable to get socket flags! %s \n",
394 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
397 fdflags = fdflags | O_NONBLOCK;
398 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
399 CtdlLogPrintf(CTDL_DEBUG,
400 "EVENT: unable to set socket nonblocking flags! %s \n",
402 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
406 /* TODO: maye we could use offsetof() to calc the position of data...
407 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
409 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
410 IO->recv_event.data = IO;
411 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
412 IO->send_event.data = IO;
414 rc = connect(IO->sock, IO->curr_ai->ai_addr, IO->curr_ai->ai_addrlen);
416 //// freeaddrinfo(res);
417 ev_io_init(&IO->conn_event, IO_send_callback, IO->sock, EV_WRITE);
418 IO->conn_event.data = IO;
419 set_start_callback(event_base, &IO->conn_event, 0);
423 else if (errno == EINPROGRESS) {
424 ev_io_init(&IO->conn_event, IO_connect_callback, IO->sock, EV_READ|EV_WRITE);
425 IO->conn_event.data = IO;
428 event_set(&IO->conn_event,
430 EV_READ|EV_WRITE|EV_PERSIST,
434 ev_io_start(event_base, &IO->conn_event);
436 IO->active_event = CONN_EVENT;
440 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
441 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
443 IO->curr_ai = IO->curr_ai->ai_next;
444 if (IO->curr_ai != NULL)
445 return event_connect_socket(IO);
451 void InitEventIO(AsyncIO *IO,
453 IO_CallBack ReadDone,
454 IO_CallBack SendDone,
455 IO_CallBack Terminate,
457 IO_CallBack ConnFail,
458 IO_CallBack CustomDNS,
459 IO_LineReaderCallback LineReader,
463 IO->SendDone = SendDone;
464 IO->ReadDone = ReadDone;
465 IO->Terminate = Terminate;
466 IO->LineReader = LineReader;
469 IO->NextState = eReadMessage;
472 IO->NextState = eSendReply;
475 event_connect_socket(IO);