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 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
82 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
86 citthread_mutex_lock(&EventQueueMutex);
87 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
88 i = GetCount(InboundEventQueue);
89 Put(InboundEventQueue, IKEY(i), h, NULL);
90 citthread_mutex_unlock(&EventQueueMutex);
92 ev_async_send (event_base, &AddJob);
93 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
98 int ShutDownEventQueue(void)
100 citthread_mutex_lock(&EventQueueMutex);
101 ev_async_send (EV_DEFAULT_ &ExitEventLoop);
102 citthread_mutex_unlock(&EventQueueMutex);
106 void FreeAsyncIOContents(AsyncIO *IO)
108 FreeStrBuf(&IO->IOBuf);
109 FreeStrBuf(&IO->SendBuf.Buf);
110 FreeStrBuf(&IO->RecvBuf.Buf);
114 void ShutDownCLient(AsyncIO *IO)
116 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
120 ev_io_stop(event_base, &IO->send_event);
121 ev_io_stop(event_base, &IO->recv_event);
122 ev_timer_stop (event_base, &IO->rw_timeout);
128 if (IO->DNSChannel != NULL) {
129 ares_destroy(IO->DNSChannel);
130 ev_io_stop(event_base, &IO->dns_recv_event);
131 ev_io_stop(event_base, &IO->dns_send_event);
132 IO->DNSChannel = NULL;
134 assert(IO->Terminate);
140 eReadState HandleInbound(AsyncIO *IO)
142 eReadState Finished = eBufferNotEmpty;
144 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
145 if (IO->RecvBuf.nBlobBytesWanted != 0) {
148 else { /* Reading lines... */
149 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
151 Finished = IO->LineReader(IO);
153 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
156 case eMustReadMore: /// read new from socket...
159 case eBufferNotEmpty: /* shouldn't happen... */
160 case eReadSuccess: /// done for now...
162 case eReadFail: /// WHUT?
169 if (Finished != eMustReadMore) {
170 assert(IO->ReadDone);
171 ev_io_stop(event_base, &IO->recv_event);
172 IO->NextState = IO->ReadDone(IO);
173 Finished = StrBufCheckBuffer(&IO->RecvBuf);
178 if ((IO->NextState == eSendReply) ||
179 (IO->NextState == eSendMore))
181 assert(IO->SendDone);
182 IO->NextState = IO->SendDone(IO);
183 ev_io_start(event_base, &IO->send_event);
185 else if ((IO->NextState == eTerminateConnection) ||
186 (IO->NextState == eAbort) )
193 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
196 AsyncIO *IO = watcher->data;
198 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
207 const char *pch = ChrPtr(IO->SendBuf.Buf);
208 const char *pchh = IO->SendBuf.ReadWritePointer;
214 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
215 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
217 fd = fopen(fn, "a+");
218 fprintf(fd, "Read: BufSize: %ld BufContent: [",
220 rv = fwrite(pchh, nbytes, 1, fd);
227 ev_io_stop(event_base, &IO->send_event);
228 switch (IO->NextState) {
232 assert(IO->SendDone);
233 IO->NextState = IO->SendDone(IO);
235 if ((IO->NextState == eTerminateConnection) ||
236 (IO->NextState == eAbort) )
239 ev_io_start(event_base, &IO->send_event);
243 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
247 ev_io_start(event_base, &IO->recv_event);
251 case eTerminateConnection:
260 /* else : must write more. */
263 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
266 switch(IO->NextState) {
268 ev_io_start(event_base, &IO->recv_event);
272 IO_send_callback(loop, &IO->send_event, revents);
274 case eTerminateConnection:
282 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
284 AsyncIO *IO = watcher->data;
286 ev_timer_stop (event_base, &IO->rw_timeout);
291 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
293 AsyncIO *IO = watcher->data;
295 ev_timer_stop (event_base, &IO->conn_fail);
296 ev_io_stop(loop, &IO->conn_event);
297 assert(IO->ConnFail);
301 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
303 AsyncIO *IO = watcher->data;
305 ev_io_stop(loop, &IO->conn_event);
306 ev_timer_stop (event_base, &IO->conn_fail);
307 set_start_callback(loop, IO, revents);
310 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
313 AsyncIO *IO = watcher->data;
315 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
318 } else if (nbytes == 0) {
320 IO->Timeout(IO); /* this is a timeout... */
322 } else if (nbytes == -1) {
323 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
329 IO_postdns_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
331 AsyncIO *IO = watcher->data;
332 CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
337 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
342 IO->SendBuf.fd = IO->RecvBuf.fd =
344 (IO->IP6)?PF_INET6:PF_INET,
349 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
350 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
351 // freeaddrinfo(res);
354 fdflags = fcntl(IO->sock, F_GETFL);
356 CtdlLogPrintf(CTDL_DEBUG,
357 "EVENT: unable to get socket flags! %s \n",
359 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
362 fdflags = fdflags | O_NONBLOCK;
363 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
364 CtdlLogPrintf(CTDL_DEBUG,
365 "EVENT: unable to set socket nonblocking flags! %s \n",
367 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
371 /* TODO: maye we could use offsetof() to calc the position of data...
372 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
374 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
375 IO->recv_event.data = IO;
376 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
377 IO->send_event.data = IO;
379 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
380 IO->conn_fail.data = IO;
381 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
382 IO->rw_timeout.data = IO;
385 rc = connect(IO->sock, &IO->Addr, sizeof(struct sockaddr_in6));
387 rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
390 //// freeaddrinfo(res);
391 set_start_callback(event_base, IO, 0);
392 ev_timer_start(event_base, &IO->rw_timeout);
393 return IO->NextState;
395 else if (errno == EINPROGRESS) {
397 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
398 IO->conn_event.data = IO;
400 ev_io_start(event_base, &IO->conn_event);
401 ev_timer_start(event_base, &IO->conn_fail);
402 return IO->NextState;
405 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
406 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
407 assert(IO->ConnFail);
411 return IO->NextState;
414 void SetNextTimeout(AsyncIO *IO, double timeout)
416 IO->rw_timeout.repeat = timeout;
417 ev_timer_again (event_base, &IO->rw_timeout);
420 eNextState InitEventIO(AsyncIO *IO,
423 double first_rw_timeout,
429 IO->NextState = eReadMessage;
432 IO->NextState = eSendReply;
434 return event_connect_socket(IO, conn_timeout, first_rw_timeout);