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 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(AsyncIO *IO, IO_CallBack 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->rw_timeout);
140 assert(IO->Terminate);
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 assert(IO->ReadDone);
177 ev_io_stop(event_base, &IO->recv_event);
178 IO->NextState = IO->ReadDone(IO);
179 Finished = StrBufCheckBuffer(&IO->RecvBuf);
184 if ((IO->NextState == eSendReply) ||
185 (IO->NextState == eSendMore))
187 assert(IO->SendDone);
188 IO->NextState = IO->SendDone(IO);
189 ev_io_start(event_base, &IO->send_event);
191 else if ((IO->NextState == eTerminateConnection) ||
192 (IO->NextState == eAbort) )
199 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
202 AsyncIO *IO = watcher->data;
204 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
213 const char *pch = ChrPtr(IO->SendBuf.Buf);
214 const char *pchh = IO->SendBuf.ReadWritePointer;
220 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
221 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
223 fd = fopen(fn, "a+");
224 fprintf(fd, "Read: BufSize: %ld BufContent: [",
226 rv = fwrite(pchh, nbytes, 1, fd);
233 ev_io_stop(event_base, &IO->send_event);
234 switch (IO->NextState) {
238 assert(IO->SendDone);
239 IO->NextState = IO->SendDone(IO);
241 if ((IO->NextState == eTerminateConnection) ||
242 (IO->NextState == eAbort) )
245 ev_io_start(event_base, &IO->send_event);
249 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
253 ev_io_start(event_base, &IO->recv_event);
257 case eTerminateConnection:
266 /* else : must write more. */
269 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
272 switch(IO->NextState) {
274 ev_io_start(event_base, &IO->recv_event);
278 IO_send_callback(loop, &IO->send_event, revents);
280 case eTerminateConnection:
288 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
290 AsyncIO *IO = watcher->data;
292 ev_timer_stop (event_base, &IO->rw_timeout);
297 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
299 AsyncIO *IO = watcher->data;
301 ev_timer_stop (event_base, &IO->conn_fail);
302 ev_io_stop(loop, &IO->conn_event);
303 assert(IO->ConnFail);
307 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
309 AsyncIO *IO = watcher->data;
311 ev_io_stop(loop, &IO->conn_event);
312 ev_timer_stop (event_base, &IO->conn_fail);
313 set_start_callback(loop, IO, revents);
316 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
319 AsyncIO *IO = watcher->data;
321 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
324 } else if (nbytes == 0) {
326 IO->Timeout(IO); /* this is a timeout... */
328 } else if (nbytes == -1) {
329 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
336 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
341 IO->SendBuf.fd = IO->RecvBuf.fd =
343 (IO->IP6)?PF_INET6:PF_INET,
348 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
349 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
350 // freeaddrinfo(res);
353 fdflags = fcntl(IO->sock, F_GETFL);
355 CtdlLogPrintf(CTDL_DEBUG,
356 "EVENT: unable to get socket flags! %s \n",
358 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
361 fdflags = fdflags | O_NONBLOCK;
362 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
363 CtdlLogPrintf(CTDL_DEBUG,
364 "EVENT: unable to set socket nonblocking flags! %s \n",
366 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
370 /* TODO: maye we could use offsetof() to calc the position of data...
371 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
373 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
374 IO->recv_event.data = IO;
375 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
376 IO->send_event.data = IO;
378 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
379 IO->conn_fail.data = IO;
380 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
381 IO->rw_timeout.data = IO;
382 ///struct sockaddr_in *addr = &IO->Addr;
384 rc = connect(IO->sock, &IO->Addr, sizeof(struct in6_addr));
386 rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
389 //// freeaddrinfo(res);
390 set_start_callback(event_base, IO, 0);
391 ev_timer_start(event_base, &IO->rw_timeout);
392 return IO->NextState;
394 else if (errno == EINPROGRESS) {
396 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
397 IO->conn_event.data = IO;
399 ev_io_start(event_base, &IO->conn_event);
400 ev_timer_start(event_base, &IO->conn_fail);
401 return IO->NextState;
404 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
405 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
406 assert(IO->ConnFail);
410 return IO->NextState;
413 void SetNextTimeout(AsyncIO *IO, double timeout)
415 IO->rw_timeout.repeat = timeout;
416 ev_timer_again (event_base, &IO->rw_timeout);
419 eNextState InitEventIO(AsyncIO *IO,
422 double first_rw_timeout,
428 IO->NextState = eReadMessage;
431 IO->NextState = eSendReply;
433 IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
434 // IO->res = HEnt->h_addr_list[0];
435 return event_connect_socket(IO, conn_timeout, first_rw_timeout);