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(PF_INET, SOCK_STREAM, IPPROTO_TCP);
381 IO->curr_ai->ai_family,
382 IO->curr_ai->ai_socktype,
383 IO->curr_ai->ai_protocol);
386 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
387 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
388 // freeaddrinfo(res);
391 fdflags = fcntl(IO->sock, F_GETFL);
393 CtdlLogPrintf(CTDL_DEBUG,
394 "EVENT: unable to get socket flags! %s \n",
396 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
399 fdflags = fdflags | O_NONBLOCK;
400 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
401 CtdlLogPrintf(CTDL_DEBUG,
402 "EVENT: unable to set socket nonblocking flags! %s \n",
404 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
408 /* TODO: maye we could use offsetof() to calc the position of data...
409 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
411 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
412 IO->recv_event.data = IO;
413 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
414 IO->send_event.data = IO;
416 unsigned short dport = atoi("25"); ///todo
417 struct sockaddr_in saddr;
418 memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
420 memcpy(&saddr.sin_addr,
421 IO->HEnt->h_addr_list[0],
422 sizeof(struct in_addr));
424 saddr.sin_addr.s_addr = inet_addr("85.88.5.80");
425 saddr.sin_family = AF_INET;
426 saddr.sin_port = htons(dport);/// TODO
427 rc = connect(IO->sock,
428 (struct sockaddr *) &saddr,
429 /// TODO: ipv6?? (IO->HEnt->h_addrtype == AF_INET6)?
430 /* sizeof(in6_addr):*/
431 sizeof(struct sockaddr_in));
433 //// freeaddrinfo(res);
434 ev_io_init(&IO->conn_event, IO_send_callback, IO->sock, EV_WRITE);
435 IO->conn_event.data = IO;
436 set_start_callback(event_base, &IO->conn_event, 0);
440 else if (errno == EINPROGRESS) {
441 ev_io_init(&IO->conn_event, IO_connect_callback, IO->sock, EV_WRITE);
442 IO->conn_event.data = IO;
445 event_set(&IO->conn_event,
447 EV_READ|EV_WRITE|EV_PERSIST,
451 ev_io_start(event_base, &IO->conn_event);
453 IO->active_event = CONN_EVENT;
457 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
458 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
460 /* IO->curr_ai = IO->curr_ai->ai_next;
461 if (IO->curr_ai != NULL)
462 return event_connect_socket(IO);
468 void InitEventIO(AsyncIO *IO,
470 IO_CallBack ReadDone,
471 IO_CallBack SendDone,
472 IO_CallBack Terminate,
474 IO_CallBack ConnFail,
475 IO_LineReaderCallback LineReader,
479 IO->SendDone = SendDone;
480 IO->ReadDone = ReadDone;
481 IO->Terminate = Terminate;
482 IO->LineReader = LineReader;
485 IO->NextState = eReadMessage;
488 IO->NextState = eSendReply;
490 // IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
491 // IO->res = HEnt->h_addr_list[0];
492 event_connect_socket(IO);