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;
76 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
81 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
85 citthread_mutex_lock(&EventQueueMutex);
86 if (event_add_pipe[1] == -1) {
87 citthread_mutex_unlock(&EventQueueMutex);
91 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
92 i = GetCount(InboundEventQueue);
93 Put(InboundEventQueue, IKEY(i), h, NULL);
94 citthread_mutex_unlock(&EventQueueMutex);
96 write(event_add_pipe[1], "+_", 1);
97 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
102 int ShutDownEventQueue(void)
104 citthread_mutex_lock(&EventQueueMutex);
105 if (event_add_pipe[1] == -1) {
106 citthread_mutex_unlock(&EventQueueMutex);
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);
117 void FreeAsyncIOContents(AsyncIO *IO)
119 FreeStrBuf(&IO->IOBuf);
120 FreeStrBuf(&IO->SendBuf.Buf);
121 FreeStrBuf(&IO->RecvBuf.Buf);
122 ares_destroy(IO->DNSChannel);
126 void ShutDownCLient(AsyncIO *IO)
128 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
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);
146 eReadState HandleInbound(AsyncIO *IO)
148 eReadState Finished = eBufferNotEmpty;
150 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
151 if (IO->RecvBuf.nBlobBytesWanted != 0) {
154 else { /* Reading lines... */
155 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
157 Finished = IO->LineReader(IO);
159 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
162 case eMustReadMore: /// read new from socket...
165 case eBufferNotEmpty: /* shouldn't happen... */
166 case eReadSuccess: /// done for now...
168 case eReadFail: /// WHUT?
175 if (Finished != eMustReadMore) {
176 ev_io_stop(event_base, &IO->recv_event);
177 IO->NextState = IO->ReadDone(IO);
178 Finished = StrBufCheckBuffer(&IO->RecvBuf);
183 if ((IO->NextState == eSendReply) ||
184 (IO->NextState == eSendMore))
186 IO->NextState = IO->SendDone(IO);
187 ev_io_start(event_base, &IO->send_event);
189 else if ((IO->NextState == eTerminateConnection) ||
190 (IO->NextState == eAbort) )
197 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
200 AsyncIO *IO = watcher->data;
202 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
211 const char *pch = ChrPtr(IO->SendBuf.Buf);
212 const char *pchh = IO->SendBuf.ReadWritePointer;
218 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
219 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
221 fd = fopen(fn, "a+");
222 fprintf(fd, "Read: BufSize: %ld BufContent: [",
224 rv = fwrite(pchh, nbytes, 1, fd);
231 ev_io_stop(event_base, &IO->send_event);
232 switch (IO->NextState) {
236 IO->NextState = IO->SendDone(IO);
238 if ((IO->NextState == eTerminateConnection) ||
239 (IO->NextState == eAbort) )
242 ev_io_start(event_base, &IO->send_event);
246 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
250 ev_io_start(event_base, &IO->recv_event);
254 case eTerminateConnection:
261 /* else : must write more. */
265 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
267 AsyncIO *IO = watcher->data;
272 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
274 AsyncIO *IO = watcher->data;
279 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
282 AsyncIO *IO = watcher->data;
284 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
287 } else if (nbytes == 0) {
288 IO->Timeout(IO); /* this is a timeout... */
290 } else if (nbytes == -1) {
291 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
298 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
301 switch(IO->NextState) {
303 ev_io_start(event_base, &IO->recv_event);
307 IO_send_callback(loop, &IO->send_event, revents);
309 case eTerminateConnection:
316 int event_connect_socket(AsyncIO *IO, int conn_timeout, int first_rw_timeout)
318 struct sockaddr_in saddr;
322 IO->SendBuf.fd = IO->RecvBuf.fd =
323 IO->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
325 IO->curr_ai->ai_family,
326 IO->curr_ai->ai_socktype,
327 IO->curr_ai->ai_protocol);
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);
335 fdflags = fcntl(IO->sock, F_GETFL);
337 CtdlLogPrintf(CTDL_DEBUG,
338 "EVENT: unable to get socket flags! %s \n",
340 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
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",
348 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
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
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;
362 memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
364 memcpy(&saddr.sin_addr,
365 IO->HEnt->h_addr_list[0],
366 sizeof(struct in_addr));
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));
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);
383 else if (errno == EINPROGRESS) {
384 set_start_callback(event_base, IO, 0);
385 ev_timer_init(&IO->conn_timeout,
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);
394 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
395 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
397 /* IO->curr_ai = IO->curr_ai->ai_next;
398 if (IO->curr_ai != NULL)
399 return event_connect_socket(IO);
405 void SetNextTimeout(AsyncIO *IO, int timeout)
407 ev_timer_init(&IO->conn_timeout,
410 ev_timer_again (event_base, &IO->conn_timeout);
413 void InitEventIO(AsyncIO *IO,
415 IO_CallBack ReadDone,
416 IO_CallBack SendDone,
417 IO_CallBack Terminate,
419 IO_CallBack ConnFail,
420 IO_LineReaderCallback LineReader,
422 int first_rw_timeout,
426 IO->SendDone = SendDone;
427 IO->ReadDone = ReadDone;
428 IO->Terminate = Terminate;
429 IO->LineReader = LineReader;
430 IO->ConnFail = ConnFail;
433 IO->NextState = eReadMessage;
436 IO->NextState = eSendReply;
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);