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 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
69 #include "event_client.h"
71 extern int event_add_pipe[2];
72 extern citthread_mutex_t EventQueueMutex;
73 extern HashList *InboundEventQueue;
78 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
83 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
87 citthread_mutex_lock(&EventQueueMutex);
88 if (event_add_pipe[1] == -1) {
89 citthread_mutex_unlock(&EventQueueMutex);
93 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
94 i = GetCount(InboundEventQueue);
95 Put(InboundEventQueue, IKEY(i), h, NULL);
96 citthread_mutex_unlock(&EventQueueMutex);
98 write(event_add_pipe[1], "+_", 1);
99 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
104 int ShutDownEventQueue(void)
106 citthread_mutex_lock(&EventQueueMutex);
107 if (event_add_pipe[1] == -1) {
108 citthread_mutex_unlock(&EventQueueMutex);
112 write(event_add_pipe[1], "x_", 1);
113 close(event_add_pipe[1]);
114 event_add_pipe[1] = -1;
115 citthread_mutex_unlock(&EventQueueMutex);
119 void FreeAsyncIOContents(AsyncIO *IO)
121 FreeStrBuf(&IO->IOBuf);
122 FreeStrBuf(&IO->SendBuf.Buf);
123 FreeStrBuf(&IO->RecvBuf.Buf);
129 setup_signal_handlers(struct instance *instance)
131 signal(SIGPIPE, SIG_IGN);
133 event_set(&instance->sigterm_event, SIGTERM, EV_SIGNAL|EV_PERSIST,
134 exit_event_callback, instance);
135 event_add(&instance->sigterm_event, NULL);
137 event_set(&instance->sigint_event, SIGINT, EV_SIGNAL|EV_PERSIST,
138 exit_event_callback, instance);
139 event_add(&instance->sigint_event, NULL);
141 event_set(&instance->sigquit_event, SIGQUIT, EV_SIGNAL|EV_PERSIST,
142 exit_event_callback, instance);
143 event_add(&instance->sigquit_event, NULL);
147 void ShutDownCLient(AsyncIO *IO)
149 CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
150 switch (IO->active_event) {
152 event_del(&IO->send_event);
155 event_del(&IO->recv_event);
158 // no event active here; just bail out.
161 IO->active_event = 0;
162 IO->Terminate(IO->Data);
164 // citthread_mutex_lock(&EventQueueMutex);
166 ///QueueEvents /// todo remove from hash.
168 // citthread_mutex_unlock(&EventQueueMutex);
171 eReadState HandleInbound(AsyncIO *IO)
173 eReadState Finished = eBufferNotEmpty;
175 while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
176 if (IO->RecvBuf.nBlobBytesWanted != 0) {
179 else { /* Reading lines... */
180 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
182 Finished = IO->LineReader(IO);
184 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
187 case eMustReadMore: /// read new from socket...
190 case eBufferNotEmpty: /* shouldn't happen... */
191 case eReadSuccess: /// done for now...
193 case eReadFail: /// WHUT?
200 if (Finished != eMustReadMore) {
201 event_del(&IO->recv_event);
202 IO->active_event = 0;
203 IO->NextState = IO->ReadDone(IO->Data);
204 Finished = StrBufCheckBuffer(&IO->RecvBuf);
209 if ((IO->NextState == eSendReply) ||
210 (IO->NextState == eSendMore))
212 IO->NextState = IO->SendDone(IO->Data);
213 event_add(&IO->send_event, NULL);
214 IO->active_event = SEND_EVENT;
217 else if ((IO->NextState == eTerminateConnection) ||
218 (IO->NextState == eAbort) )
225 IO_send_callback(int fd, short event, void *ctx)
232 CtdlLogPrintf(CTDL_DEBUG, "EVENT -> %d : [%s%s%s%s]\n",
234 (event&EV_TIMEOUT) ? " timeout" : "",
235 (event&EV_READ) ? " read" : "",
236 (event&EV_WRITE) ? " write" : "",
237 (event&EV_SIGNAL) ? " signal" : "");
239 /// assert(fd == IO->sock);
241 rc = StrBuf_write_one_chunk_callback(fd, event, &IO->SendBuf);
251 const char *pch = ChrPtr(IO->SendBuf.Buf);
252 const char *pchh = IO->SendBuf.ReadWritePointer;
258 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
259 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
261 fd = fopen(fn, "a+");
262 fprintf(fd, "Read: BufSize: %ld BufContent: [",
264 rv = fwrite(pchh, nbytes, 1, fd);
271 event_del(&IO->send_event);
272 IO->active_event = 0;
273 switch (IO->NextState) {
277 IO->NextState = IO->SendDone(IO->Data);
279 if ((IO->NextState == eTerminateConnection) ||
280 (IO->NextState == eAbort) )
283 event_add(&IO->send_event, NULL);
284 IO->active_event = SEND_EVENT;
288 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
292 event_add(&IO->recv_event, NULL);
293 IO->active_event = RECV_EVENT;
309 IO_recv_callback(int fd, short event, void *ctx)
314 // assert(fd == IO->sock);
316 // assert(fd == sb->fd);
318 CtdlLogPrintf(CTDL_DEBUG, "EVENT <- %d : [%s%s%s%s]\n",
320 (event&EV_TIMEOUT) ? " timeout" : "",
321 (event&EV_READ) ? " read" : "",
322 (event&EV_WRITE) ? " write" : "",
323 (event&EV_SIGNAL) ? " signal" : "");
324 nbytes = StrBuf_read_one_chunk_callback(fd, event, &IO->RecvBuf);
327 } else if (nbytes == 0) {
329 /// TODO: this is a timeout??? sock_buff_invoke_free(sb, 0); seems as if socket is gone then?
331 } else if (nbytes == -1) {
332 /// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
337 void InitEventIO(AsyncIO *IO,
339 IO_CallBack ReadDone,
340 IO_CallBack SendDone,
341 IO_CallBack Terminate,
342 IO_LineReaderCallback LineReader,
346 IO->SendDone = SendDone;
347 IO->ReadDone = ReadDone;
348 IO->Terminate = Terminate;
349 IO->LineReader = LineReader;
351 event_set(&IO->recv_event,
357 event_set(&IO->send_event,
364 IO->NextState = eReadMessage;
365 event_add(&IO->recv_event, NULL);
366 IO->active_event = RECV_EVENT;
369 IO->NextState = eSendReply;