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);
125 void ShutDownCLient(AsyncIO *IO)
127 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
131 ev_io_stop(event_base, &IO->send_event);
132 ev_io_stop(event_base, &IO->recv_event);
133 ev_timer_stop (event_base, &IO->rw_timeout);
139 if (IO->DNSChannel != NULL) {
140 ares_destroy(IO->DNSChannel);
141 ev_io_stop(event_base, &IO->dns_recv_event);
142 ev_io_stop(event_base, &IO->dns_send_event);
143 IO->DNSChannel = NULL;
145 assert(IO->Terminate);
151 eReadState HandleInbound(AsyncIO *IO)
153 eReadState Finished = eBufferNotEmpty;
155 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
156 if (IO->RecvBuf.nBlobBytesWanted != 0) {
159 else { /* Reading lines... */
160 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
162 Finished = IO->LineReader(IO);
164 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
167 case eMustReadMore: /// read new from socket...
170 case eBufferNotEmpty: /* shouldn't happen... */
171 case eReadSuccess: /// done for now...
173 case eReadFail: /// WHUT?
180 if (Finished != eMustReadMore) {
181 assert(IO->ReadDone);
182 ev_io_stop(event_base, &IO->recv_event);
183 IO->NextState = IO->ReadDone(IO);
184 Finished = StrBufCheckBuffer(&IO->RecvBuf);
189 if ((IO->NextState == eSendReply) ||
190 (IO->NextState == eSendMore))
192 assert(IO->SendDone);
193 IO->NextState = IO->SendDone(IO);
194 ev_io_start(event_base, &IO->send_event);
196 else if ((IO->NextState == eTerminateConnection) ||
197 (IO->NextState == eAbort) )
204 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
207 AsyncIO *IO = watcher->data;
209 rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
218 const char *pch = ChrPtr(IO->SendBuf.Buf);
219 const char *pchh = IO->SendBuf.ReadWritePointer;
225 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
226 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
228 fd = fopen(fn, "a+");
229 fprintf(fd, "Read: BufSize: %ld BufContent: [",
231 rv = fwrite(pchh, nbytes, 1, fd);
238 ev_io_stop(event_base, &IO->send_event);
239 switch (IO->NextState) {
243 assert(IO->SendDone);
244 IO->NextState = IO->SendDone(IO);
246 if ((IO->NextState == eTerminateConnection) ||
247 (IO->NextState == eAbort) )
250 ev_io_start(event_base, &IO->send_event);
254 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
258 ev_io_start(event_base, &IO->recv_event);
262 case eTerminateConnection:
271 /* else : must write more. */
274 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
277 switch(IO->NextState) {
279 ev_io_start(event_base, &IO->recv_event);
283 IO_send_callback(loop, &IO->send_event, revents);
285 case eTerminateConnection:
293 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
295 AsyncIO *IO = watcher->data;
297 ev_timer_stop (event_base, &IO->rw_timeout);
302 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
304 AsyncIO *IO = watcher->data;
306 ev_timer_stop (event_base, &IO->conn_fail);
307 ev_io_stop(loop, &IO->conn_event);
308 assert(IO->ConnFail);
312 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
314 AsyncIO *IO = watcher->data;
316 ev_io_stop(loop, &IO->conn_event);
317 ev_timer_stop (event_base, &IO->conn_fail);
318 set_start_callback(loop, IO, revents);
321 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
324 AsyncIO *IO = watcher->data;
326 nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
329 } else if (nbytes == 0) {
331 IO->Timeout(IO); /* this is a timeout... */
333 } else if (nbytes == -1) {
334 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
340 IO_postdns_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
342 AsyncIO *IO = watcher->data;
346 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
351 IO->SendBuf.fd = IO->RecvBuf.fd =
353 (IO->IP6)?PF_INET6:PF_INET,
358 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
359 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
360 // freeaddrinfo(res);
363 fdflags = fcntl(IO->sock, F_GETFL);
365 CtdlLogPrintf(CTDL_DEBUG,
366 "EVENT: unable to get socket flags! %s \n",
368 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
371 fdflags = fdflags | O_NONBLOCK;
372 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
373 CtdlLogPrintf(CTDL_DEBUG,
374 "EVENT: unable to set socket nonblocking flags! %s \n",
376 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
380 /* TODO: maye we could use offsetof() to calc the position of data...
381 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
383 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
384 IO->recv_event.data = IO;
385 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
386 IO->send_event.data = IO;
388 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
389 IO->conn_fail.data = IO;
390 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
391 IO->rw_timeout.data = IO;
394 rc = connect(IO->sock, &IO->Addr, sizeof(struct sockaddr_in6));
396 rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
399 //// freeaddrinfo(res);
400 set_start_callback(event_base, IO, 0);
401 ev_timer_start(event_base, &IO->rw_timeout);
402 return IO->NextState;
404 else if (errno == EINPROGRESS) {
406 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
407 IO->conn_event.data = IO;
409 ev_io_start(event_base, &IO->conn_event);
410 ev_timer_start(event_base, &IO->conn_fail);
411 return IO->NextState;
414 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
415 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
416 assert(IO->ConnFail);
420 return IO->NextState;
423 void SetNextTimeout(AsyncIO *IO, double timeout)
425 IO->rw_timeout.repeat = timeout;
426 ev_timer_again (event_base, &IO->rw_timeout);
429 eNextState InitEventIO(AsyncIO *IO,
432 double first_rw_timeout,
438 IO->NextState = eReadMessage;
441 IO->NextState = eSendReply;
443 return event_connect_socket(IO, conn_timeout, first_rw_timeout);