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 #include "event_client.h"
68 extern int event_add_pipe[2];
69 extern citthread_mutex_t EventQueueMutex;
70 extern HashList *InboundEventQueue;
71 extern struct ev_loop *event_base;
74 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
79 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
83 citthread_mutex_lock(&EventQueueMutex);
84 if (event_add_pipe[1] == -1) {
85 citthread_mutex_unlock(&EventQueueMutex);
89 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
90 i = GetCount(InboundEventQueue);
91 Put(InboundEventQueue, IKEY(i), h, NULL);
92 citthread_mutex_unlock(&EventQueueMutex);
94 write(event_add_pipe[1], "+_", 1);
95 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
100 int ShutDownEventQueue(void)
102 citthread_mutex_lock(&EventQueueMutex);
103 if (event_add_pipe[1] == -1) {
104 citthread_mutex_unlock(&EventQueueMutex);
108 write(event_add_pipe[1], "x_", 1);
109 close(event_add_pipe[1]);
110 event_add_pipe[1] = -1;
111 citthread_mutex_unlock(&EventQueueMutex);
115 void FreeAsyncIOContents(AsyncIO *IO)
117 FreeStrBuf(&IO->IOBuf);
118 FreeStrBuf(&IO->SendBuf.Buf);
119 FreeStrBuf(&IO->RecvBuf.Buf);
120 ares_destroy(IO->DNSChannel);
124 void ShutDownCLient(AsyncIO *IO)
126 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
130 ev_io_stop(event_base, &IO->send_event);
131 ev_io_stop(event_base, &IO->recv_event);
132 ev_timer_stop (event_base, &IO->rw_timeout);
144 eReadState HandleInbound(AsyncIO *IO)
146 eReadState Finished = eBufferNotEmpty;
148 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
149 if (IO->RecvBuf.nBlobBytesWanted != 0) {
152 else { /* Reading lines... */
153 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
155 Finished = IO->LineReader(IO);
157 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
160 case eMustReadMore: /// read new from socket...
163 case eBufferNotEmpty: /* shouldn't happen... */
164 case eReadSuccess: /// done for now...
166 case eReadFail: /// WHUT?
173 if (Finished != eMustReadMore) {
174 ev_io_stop(event_base, &IO->recv_event);
175 IO->NextState = IO->ReadDone(IO);
176 Finished = StrBufCheckBuffer(&IO->RecvBuf);
181 if ((IO->NextState == eSendReply) ||
182 (IO->NextState == eSendMore))
184 IO->NextState = IO->SendDone(IO);
185 ev_io_start(event_base, &IO->send_event);
187 else if ((IO->NextState == eTerminateConnection) ||
188 (IO->NextState == eAbort) )
195 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
198 AsyncIO *IO = watcher->data;
200 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
209 const char *pch = ChrPtr(IO->SendBuf.Buf);
210 const char *pchh = IO->SendBuf.ReadWritePointer;
216 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
217 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
219 fd = fopen(fn, "a+");
220 fprintf(fd, "Read: BufSize: %ld BufContent: [",
222 rv = fwrite(pchh, nbytes, 1, fd);
229 ev_io_stop(event_base, &IO->send_event);
230 switch (IO->NextState) {
234 IO->NextState = IO->SendDone(IO);
236 if ((IO->NextState == eTerminateConnection) ||
237 (IO->NextState == eAbort) )
240 ev_io_start(event_base, &IO->send_event);
244 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
248 ev_io_start(event_base, &IO->recv_event);
252 case eTerminateConnection:
259 /* else : must write more. */
262 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
265 switch(IO->NextState) {
267 ev_io_start(event_base, &IO->recv_event);
271 IO_send_callback(loop, &IO->send_event, revents);
273 case eTerminateConnection:
281 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
283 AsyncIO *IO = watcher->data;
285 ev_timer_stop (event_base, &IO->rw_timeout);
289 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
291 AsyncIO *IO = watcher->data;
293 ev_timer_stop (event_base, &IO->conn_fail);
294 ev_io_stop(loop, &IO->conn_event);
298 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
300 AsyncIO *IO = watcher->data;
302 ev_io_stop(loop, &IO->conn_event);
303 ev_timer_stop (event_base, &IO->conn_fail);
304 set_start_callback(loop, IO, revents);
307 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
310 AsyncIO *IO = watcher->data;
312 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
315 } else if (nbytes == 0) {
316 IO->Timeout(IO); /* this is a timeout... */
318 } else if (nbytes == -1) {
319 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
326 int event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
331 IO->SendBuf.fd = IO->RecvBuf.fd =
333 (IO->IP6)?PF_INET6:PF_INET,
338 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
339 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
340 // freeaddrinfo(res);
343 fdflags = fcntl(IO->sock, F_GETFL);
345 CtdlLogPrintf(CTDL_DEBUG,
346 "EVENT: unable to get socket flags! %s \n",
348 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
351 fdflags = fdflags | O_NONBLOCK;
352 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
353 CtdlLogPrintf(CTDL_DEBUG,
354 "EVENT: unable to set socket nonblocking flags! %s \n",
356 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
360 /* TODO: maye we could use offsetof() to calc the position of data...
361 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
363 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
364 IO->recv_event.data = IO;
365 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
366 IO->send_event.data = IO;
368 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
369 IO->conn_fail.data = IO;
370 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
371 IO->rw_timeout.data = IO;
373 rc = connect(IO->sock,
374 (struct sockaddr *) &IO->Addr,
375 (IO->IP6)? ///HEnt->h_addrtype == AF_INET6)?
376 sizeof(struct in6_addr):
377 sizeof(struct sockaddr_in));
379 //// freeaddrinfo(res);
380 set_start_callback(event_base, IO, 0);
381 ev_timer_start(event_base, &IO->rw_timeout);
384 else if (errno == EINPROGRESS) {
386 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
387 IO->conn_event.data = IO;
389 ev_io_start(event_base, &IO->conn_event);
390 ev_timer_start(event_base, &IO->conn_fail);
394 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
395 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
401 void SetNextTimeout(AsyncIO *IO, double timeout)
403 IO->rw_timeout.repeat = timeout;
404 ev_timer_again (event_base, &IO->rw_timeout);
407 void InitEventIO(AsyncIO *IO,
410 double first_rw_timeout,
416 IO->NextState = eReadMessage;
419 IO->NextState = eSendReply;
421 IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
422 // IO->res = HEnt->h_addr_list[0];
423 event_connect_socket(IO, conn_timeout, first_rw_timeout);