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(void *Ctx, AsyncIO *IO, EventContextAttach 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)
328 struct sockaddr_in saddr;
332 IO->SendBuf.fd = IO->RecvBuf.fd =
333 IO->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
335 IO->curr_ai->ai_family,
336 IO->curr_ai->ai_socktype,
337 IO->curr_ai->ai_protocol);
340 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
341 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
342 // freeaddrinfo(res);
345 fdflags = fcntl(IO->sock, F_GETFL);
347 CtdlLogPrintf(CTDL_DEBUG,
348 "EVENT: unable to get socket flags! %s \n",
350 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
353 fdflags = fdflags | O_NONBLOCK;
354 if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
355 CtdlLogPrintf(CTDL_DEBUG,
356 "EVENT: unable to set socket nonblocking flags! %s \n",
358 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
362 /* TODO: maye we could use offsetof() to calc the position of data...
363 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
365 ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
366 IO->recv_event.data = IO;
367 ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
368 IO->send_event.data = IO;
370 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
371 IO->conn_fail.data = IO;
372 ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
373 IO->rw_timeout.data = IO;
375 memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
377 memcpy(&saddr.sin_addr,
378 IO->HEnt->h_addr_list[0],
379 sizeof(struct in_addr));
380 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
382 saddr.sin_family = AF_INET;
383 saddr.sin_port = htons(IO->dport);
384 rc = connect(IO->sock,
385 (struct sockaddr *) &saddr,
386 /// TODO: ipv6?? (IO->HEnt->h_addrtype == AF_INET6)?
387 /* sizeof(in6_addr):*/
388 sizeof(struct sockaddr_in));
390 //// freeaddrinfo(res);
391 set_start_callback(event_base, IO, 0);
392 ev_timer_start(event_base, &IO->rw_timeout);
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);
405 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
406 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
412 void SetNextTimeout(AsyncIO *IO, double timeout)
414 IO->rw_timeout.repeat = timeout;
415 ev_timer_again (event_base, &IO->rw_timeout);
418 void InitEventIO(AsyncIO *IO,
420 IO_CallBack ReadDone,
421 IO_CallBack SendDone,
422 IO_CallBack Terminate,
424 IO_CallBack ConnFail,
425 IO_LineReaderCallback LineReader,
427 double first_rw_timeout,
431 IO->SendDone = SendDone;
432 IO->ReadDone = ReadDone;
433 IO->Terminate = Terminate;
434 IO->LineReader = LineReader;
435 IO->ConnFail = ConnFail;
436 IO->Timeout = Timeout;
439 IO->NextState = eReadMessage;
442 IO->NextState = eSendReply;
444 IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
445 // IO->res = HEnt->h_addr_list[0];
446 event_connect_socket(IO, conn_timeout, first_rw_timeout);