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>
51 #include <libcitadel.h>
54 #include "citserver.h"
61 #include "internet_addressing.h"
64 #include "clientsocket.h"
65 #include "locate_host.h"
66 #include "citadel_dirs.h"
68 #include "event_client.h"
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;
77 IO_abort_shutdown_callback(struct ev_loop *loop, ev_cleanup *watcher, int revents)
79 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
81 AsyncIO *IO = watcher->data;
82 IO->ShutdownAbort(IO);
85 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
90 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
93 ev_cleanup_init(&IO->abort_by_shutdown,
94 IO_abort_shutdown_callback);
95 IO->abort_by_shutdown.data = IO;
96 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
98 citthread_mutex_lock(&EventQueueMutex);
99 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
100 i = GetCount(InboundEventQueue);
101 Put(InboundEventQueue, IKEY(i), h, NULL);
102 citthread_mutex_unlock(&EventQueueMutex);
104 ev_async_send (event_base, &AddJob);
105 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
110 int ShutDownEventQueue(void)
112 citthread_mutex_lock(&EventQueueMutex);
113 ev_async_send (EV_DEFAULT_ &ExitEventLoop);
114 citthread_mutex_unlock(&EventQueueMutex);
118 void FreeAsyncIOContents(AsyncIO *IO)
120 FreeStrBuf(&IO->IOBuf);
121 FreeStrBuf(&IO->SendBuf.Buf);
122 FreeStrBuf(&IO->RecvBuf.Buf);
126 void ShutDownCLient(AsyncIO *IO)
128 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
130 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
132 if (IO->SendBuf.fd != 0)
134 ev_io_stop(event_base, &IO->send_event);
135 ev_io_stop(event_base, &IO->recv_event);
136 ev_timer_stop (event_base, &IO->rw_timeout);
137 close(IO->SendBuf.fd);
141 if (IO->DNSChannel != NULL) {
142 ares_destroy(IO->DNSChannel);
143 ev_io_stop(event_base, &IO->dns_recv_event);
144 ev_io_stop(event_base, &IO->dns_send_event);
145 IO->DNSChannel = NULL;
147 assert(IO->Terminate);
153 eReadState HandleInbound(AsyncIO *IO)
155 eReadState Finished = eBufferNotEmpty;
157 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
158 if (IO->RecvBuf.nBlobBytesWanted != 0) {
161 else { /* Reading lines... */
162 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
164 Finished = IO->LineReader(IO);
166 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
169 case eMustReadMore: /// read new from socket...
172 case eBufferNotEmpty: /* shouldn't happen... */
173 case eReadSuccess: /// done for now...
175 case eReadFail: /// WHUT?
182 if (Finished != eMustReadMore) {
183 assert(IO->ReadDone);
184 ev_io_stop(event_base, &IO->recv_event);
185 IO->NextState = IO->ReadDone(IO);
186 Finished = StrBufCheckBuffer(&IO->RecvBuf);
191 if ((IO->NextState == eSendReply) ||
192 (IO->NextState == eSendMore))
194 assert(IO->SendDone);
195 IO->NextState = IO->SendDone(IO);
196 ev_io_start(event_base, &IO->send_event);
198 else if ((IO->NextState == eTerminateConnection) ||
199 (IO->NextState == eAbort) )
206 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
209 AsyncIO *IO = watcher->data;
211 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
220 const char *pch = ChrPtr(IO->SendBuf.Buf);
221 const char *pchh = IO->SendBuf.ReadWritePointer;
227 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
228 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->SendBuf.fd);
230 fd = fopen(fn, "a+");
231 fprintf(fd, "Read: BufSize: %ld BufContent: [",
233 rv = fwrite(pchh, nbytes, 1, fd);
240 ev_io_stop(event_base, &IO->send_event);
241 switch (IO->NextState) {
245 assert(IO->SendDone);
246 IO->NextState = IO->SendDone(IO);
248 if ((IO->NextState == eTerminateConnection) ||
249 (IO->NextState == eAbort) )
252 ev_io_start(event_base, &IO->send_event);
256 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
260 ev_io_start(event_base, &IO->recv_event);
264 case eTerminateConnection:
273 /* else : must write more. */
276 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
279 switch(IO->NextState) {
281 ev_io_start(event_base, &IO->recv_event);
285 IO_send_callback(loop, &IO->send_event, revents);
287 case eTerminateConnection:
295 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
297 AsyncIO *IO = watcher->data;
299 ev_timer_stop (event_base, &IO->rw_timeout);
304 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
306 AsyncIO *IO = watcher->data;
308 ev_timer_stop (event_base, &IO->conn_fail);
309 ev_io_stop(loop, &IO->conn_event);
310 assert(IO->ConnFail);
314 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
316 AsyncIO *IO = watcher->data;
318 ev_io_stop(loop, &IO->conn_event);
319 ev_timer_stop (event_base, &IO->conn_fail);
320 set_start_callback(loop, IO, revents);
323 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
326 AsyncIO *IO = watcher->data;
328 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
331 } else if (nbytes == 0) {
333 IO->Timeout(IO); /* this is a timeout... */
335 } else if (nbytes == -1) {
336 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
342 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
344 AsyncIO *IO = watcher->data;
345 CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
347 IO->DNSQuery->PostDNS(IO);
350 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
355 IO->SendBuf.fd = IO->RecvBuf.fd =
357 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
361 if (IO->SendBuf.fd < 0) {
362 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
363 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
364 // freeaddrinfo(res);
367 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
369 CtdlLogPrintf(CTDL_DEBUG,
370 "EVENT: unable to get socket flags! %s \n",
372 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
375 fdflags = fdflags | O_NONBLOCK;
376 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
377 CtdlLogPrintf(CTDL_DEBUG,
378 "EVENT: unable to set socket nonblocking flags! %s \n",
380 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
381 close(IO->SendBuf.fd);
382 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
385 /* TODO: maye we could use offsetof() to calc the position of data...
386 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
388 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
389 IO->recv_event.data = IO;
390 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
391 IO->send_event.data = IO;
393 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
394 IO->conn_fail.data = IO;
395 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
396 IO->rw_timeout.data = IO;
398 if (IO->ConnectMe->IPv6)
399 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
401 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
404 //// freeaddrinfo(res);
405 set_start_callback(event_base, IO, 0);
406 ev_timer_start(event_base, &IO->rw_timeout);
407 return IO->NextState;
409 else if (errno == EINPROGRESS) {
411 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
412 IO->conn_event.data = IO;
414 ev_io_start(event_base, &IO->conn_event);
415 ev_timer_start(event_base, &IO->conn_fail);
416 return IO->NextState;
419 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
420 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
421 assert(IO->ConnFail);
425 return IO->NextState;
428 void SetNextTimeout(AsyncIO *IO, double timeout)
430 IO->rw_timeout.repeat = timeout;
431 ev_timer_again (event_base, &IO->rw_timeout);
434 eNextState InitEventIO(AsyncIO *IO,
437 double first_rw_timeout,
443 IO->NextState = eReadMessage;
446 IO->NextState = eSendReply;
448 return event_connect_socket(IO, conn_timeout, first_rw_timeout);