3 * Copyright (c) 1998-2012 by the citadel.org team
5 * This program is open source 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
20 #ifndef __EVENT_CLIENT_H__
21 #define __EVENT_CLIENT_H__
23 #include "sysconfig.h"
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <arpa/nameser.h>
30 #include <curl/curl.h>
32 typedef struct AsyncIO AsyncIO;
34 typedef enum _eNextState {
54 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
55 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
56 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
57 typedef void (*FreeDNSReply)(void *DNSData);
60 typedef struct __ReadAsyncMsg {
62 size_t maxlen; /* maximum message length */
64 const char *terminator; /* token signalling EOT */
69 /* if we read maxlen, read until nothing more arives and ignore this. */
71 int crlf; /* CRLF newlines instead of LF */
75 typedef struct _DNSQueryParts {
76 ParseDNSAnswerCb DNS_CB;
80 void *VParsedDNSReply;
81 FreeDNSReply DNSReplyFree;
85 typedef struct _evcurl_request_data
88 struct curl_slist *headers;
89 char errdesc[CURL_ERROR_SIZE];
94 long PlainPostDataLen;
99 } evcurl_request_data;
102 typedef struct __evcares_data {
105 ev_timer timeout; /* timeout while requesting ips */
107 short int SourcePort;
109 struct ares_options Options;
110 ares_channel Channel;
111 DNSQueryParts *Query;
113 IO_CallBack Fail; /* the dns lookup didn't work out. */
118 eNextState NextState;
120 /* connection related */
121 ParsedURL *ConnectMe;
123 /* read/send related... */
129 /* when sending from / reading into files, this is used. */
132 ev_cleanup abort_by_shutdown, /* server wants to go down... */
133 db_abort_by_shutdown; /* server wants to go down... */
134 ev_timer conn_fail, /* connection establishing timed out */
135 rw_timeout; /* timeout while sending data */
136 ev_idle unwind_stack, /* get c-ares out of the stack */
137 db_unwind_stack, /* wait for next db operation... */
138 conn_fail_immediate; /* unwind stack, but fail immediately. */
139 ev_io recv_event, /* receive data from the client */
140 send_event, /* send more data to the client */
141 conn_event; /* Connection successfully established */
143 StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
145 /* Citadel application callbacks... */
146 IO_CallBack ReadDone, /* Theres new data to read... */
147 SendDone, /* we may send more data */
148 Terminate, /* shutting down... */
149 Timeout, /* Timeout handler;may also be conn. timeout */
150 ConnFail, /* What to do when one connection failed? */
151 ShutdownAbort,/* we're going down. make your piece. */
152 NextDBOperation; /* Perform Database IO */
154 /* if we have linereaders, maybe we want to read more lines before
155 * the real application logic is called? */
156 IO_LineReaderCallback LineReader;
160 evcurl_request_data HttpReq;
162 /* Saving / loading a message async from / to disk */
163 ReadAsyncMsg *ReadMsg;
164 struct CtdlMessage *AsyncMsg;
165 struct recptypes *AsyncRcp;
167 /* Context specific data; Hint: put AsyncIO in there */
168 void *Data; /* application specific data */
169 void *CitContext; /* Citadel Session context... */
172 typedef struct _IOAddHandler {
177 #define CCID ((CitContext*)IO->CitContext)->cs_pid
179 #define EV_syslog(LEVEL, FORMAT, ...) \
180 syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
182 #define EVM_syslog(LEVEL, FORMAT) \
183 syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
185 #define EVNC_syslog(LEVEL, FORMAT, ...) \
186 syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
188 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
191 #define EV_DNS_LOG_START(a) \
192 syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
195 #define EV_DNS_LOG_STOP(a) \
196 syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
199 #define EV_DNS_LOG_INIT(a) \
200 syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
203 #define EV_DNS_LOGT_START(a) \
204 syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %p", IO->ID, CCID, &IO->a); \
207 #define EV_DNS_LOGT_STOP(a) \
208 syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %p", IO->ID, CCID, &IO->a); \
211 #define EV_DNS_LOGT_INIT(a) \
212 syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %p", IO->ID, CCID, &IO->a); \
215 #define EV_DNS_LOG_START(a)
216 #define EV_DNS_LOG_STOP(a)
217 #define EV_DNS_LOG_INIT(a)
218 #define EV_DNS_LOGT_START(a)
219 #define EV_DNS_LOGT_STOP(a)
220 #define EV_DNS_LOGT_INIT(a)
223 void FreeAsyncIOContents(AsyncIO *IO);
225 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
226 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
227 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
228 eNextState QueueCurlContext(AsyncIO *IO);
230 eNextState EvConnectSock(AsyncIO *IO,
232 double first_rw_timeout,
234 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
236 int QueueQuery(ns_type Type,
239 DNSQueryParts *QueryParts,
240 IO_CallBack PostDNS);
242 void QueueGetHostByName(AsyncIO *IO,
243 const char *Hostname,
244 DNSQueryParts *QueryParts,
245 IO_CallBack PostDNS);
247 void QueryCbDone(AsyncIO *IO);
249 void StopClient(AsyncIO *IO);
251 void StopClientWatchers(AsyncIO *IO);
253 void SetNextTimeout(AsyncIO *IO, double timeout);
255 void InitC_ares_dns(AsyncIO *IO);
257 #include <curl/curl.h>
261 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
264 "error setting option " #s \
265 " on curl handle: %s", \
266 curl_easy_strerror(sta)); \
269 void InitIOStruct(AsyncIO *IO,
271 eNextState NextState,
272 IO_LineReaderCallback LineReader,
273 IO_CallBack DNS_Fail,
274 IO_CallBack SendDone,
275 IO_CallBack ReadDone,
276 IO_CallBack Terminate,
277 IO_CallBack ConnFail,
279 IO_CallBack ShutdownAbort);
281 int InitcURLIOStruct(AsyncIO *IO,
284 IO_CallBack SendDone,
285 IO_CallBack Terminate,
286 IO_CallBack ShutdownAbort);
288 eNextState ReAttachIO(AsyncIO *IO,
292 void EV_backtrace(AsyncIO *IO);
294 #endif /* __EVENT_CLIENT_H__ */