6f2c6ad7ad28d264158889c19328ea3d4fb443ef
[citadel.git] / citadel / event_client.h
1 /*
2  *
3  * Copyright (c) 1998-2012 by the citadel.org team
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #ifndef __EVENT_CLIENT_H__
21 #define __EVENT_CLIENT_H__
22 #define EV_COMPAT3 0
23 #include "sysconfig.h"
24 #include <ev.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <arpa/nameser.h>
29 #include <ares.h>
30 #include <curl/curl.h>
31
32 typedef struct AsyncIO AsyncIO;
33
34 typedef enum _eNextState {
35         eSendDNSQuery,
36         eReadDNSReply,
37
38         eDBQuery,
39
40         eConnect,
41         eSendReply,
42         eSendMore,
43         eSendFile,
44
45         eReadMessage,
46         eReadMore,
47         eReadPayload,
48         eReadFile,
49
50         eTerminateConnection,
51         eAbort
52 }eNextState;
53
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);
58
59
60 typedef struct __ReadAsyncMsg {
61         StrBuf *MsgBuf;
62         size_t maxlen;          /* maximum message length */
63
64         const char *terminator; /* token signalling EOT */
65         long tlen;
66         int dodot;
67
68         int flushing;
69 /* if we read maxlen, read until nothing more arives and ignore this. */
70
71         int crlf;               /* CRLF newlines instead of LF */
72 } ReadAsyncMsg;
73
74
75 typedef struct _DNSQueryParts {
76         ParseDNSAnswerCb DNS_CB;
77         IO_CallBack PostDNS;
78
79         int DNSStatus;
80         void *VParsedDNSReply;
81         FreeDNSReply DNSReplyFree;
82         void *Data;
83 } DNSQueryParts;
84
85 typedef struct _evcurl_request_data
86 {
87         CURL                    *chnd;
88         struct curl_slist       *headers;
89         char                     errdesc[CURL_ERROR_SIZE];
90
91         int                      attached;
92
93         char                    *PlainPostData;
94         long                     PlainPostDataLen;
95         StrBuf                  *PostData;
96
97         StrBuf                  *ReplyData;
98         long                     httpcode;
99 } evcurl_request_data;
100
101 /* DNS Related */
102 typedef struct __evcares_data {
103         ev_io recv_event,
104                 send_event;
105         ev_timer timeout;           /* timeout while requesting ips */
106 #ifdef DEBUG_CARES
107         short int SourcePort;
108 #endif
109         struct ares_options Options;
110         ares_channel Channel;
111         DNSQueryParts *Query;
112
113         IO_CallBack Fail;      /* the dns lookup didn't work out. */
114 } evcares_data;
115
116 struct AsyncIO {
117         long ID;
118         eNextState NextState;
119
120         /* connection related */
121         ParsedURL *ConnectMe;
122
123         /* read/send related... */
124         StrBuf *IOBuf;
125         IOBuffer SendBuf,
126                 RecvBuf;
127
128         FDIOBuffer IOB;
129         /* when sending from / reading into files, this is used. */
130
131         /* our events... */
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 */
142
143         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
144
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                 DBTerminate,  /* shutting down... */
150                 Timeout,      /* Timeout handler;may also be conn. timeout */
151                 ConnFail,     /* What to do when one connection failed? */
152                 ShutdownAbort,/* we're going down. make your piece. */
153                 NextDBOperation; /* Perform Database IO */
154
155         /* if we have linereaders, maybe we want to read more lines before
156          * the real application logic is called? */
157         IO_LineReaderCallback LineReader;
158
159         evcares_data DNS;
160
161         evcurl_request_data HttpReq;
162
163         /* Saving / loading a message async from / to disk */
164         ReadAsyncMsg *ReadMsg;
165         struct CtdlMessage *AsyncMsg;
166         struct recptypes *AsyncRcp;
167
168         /* Context specific data; Hint: put AsyncIO in there */
169         void *Data;        /* application specific data */
170         void *CitContext;  /* Citadel Session context... */
171 };
172
173 typedef struct _IOAddHandler {
174         AsyncIO *IO;
175         IO_CallBack EvAttch;
176 } IOAddHandler;
177
178 #define CCID ((CitContext*)IO->CitContext)->cs_pid
179
180 #define EV_syslog(LEVEL, FORMAT, ...) \
181         syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
182
183 #define EVM_syslog(LEVEL, FORMAT) \
184         syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
185
186 #define EVNC_syslog(LEVEL, FORMAT, ...) \
187         syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
188
189 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
190
191 #ifdef DEBUG_CARES
192 #define EV_DNS_LOG_START(a)                                                     \
193         syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
194         EV_backtrace(IO);
195
196 #define EV_DNS_LOG_STOP(a)                                                      \
197         syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
198         EV_backtrace(IO);
199
200 #define EV_DNS_LOG_INIT(a)                                                      \
201         syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %p FD %d", IO->ID, CCID, &IO->a, IO->a.fd); \
202         EV_backtrace(IO);
203
204 #define EV_DNS_LOGT_START(a)                                                    \
205         syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %p", IO->ID, CCID, &IO->a); \
206         EV_backtrace(IO);
207
208 #define EV_DNS_LOGT_STOP(a)                                                     \
209         syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %p", IO->ID, CCID, &IO->a); \
210         EV_backtrace(IO);
211
212 #define EV_DNS_LOGT_INIT(a)                                                     \
213         syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %p", IO->ID, CCID, &IO->a); \
214         EV_backtrace(IO);
215 #else
216 #define EV_DNS_LOG_START(a)
217 #define EV_DNS_LOG_STOP(a)
218 #define EV_DNS_LOG_INIT(a)
219 #define EV_DNS_LOGT_START(a)
220 #define EV_DNS_LOGT_STOP(a)
221 #define EV_DNS_LOGT_INIT(a)
222 #endif
223
224 void FreeAsyncIOContents(AsyncIO *IO);
225
226 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
227 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
228 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
229 eNextState QueueCurlContext(AsyncIO *IO);
230
231 eNextState EvConnectSock(AsyncIO *IO,
232                          double conn_timeout,
233                          double first_rw_timeout,
234                          int ReadFirst);
235 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
236
237 int QueueQuery(ns_type Type,
238                const char *name,
239                AsyncIO *IO,
240                DNSQueryParts *QueryParts,
241                IO_CallBack PostDNS);
242
243 void QueueGetHostByName(AsyncIO *IO,
244                         const char *Hostname,
245                         DNSQueryParts *QueryParts,
246                         IO_CallBack PostDNS);
247
248 void QueryCbDone(AsyncIO *IO);
249
250 void StopClient(AsyncIO *IO);
251
252 void StopClientWatchers(AsyncIO *IO);
253
254 void SetNextTimeout(AsyncIO *IO, double timeout);
255
256 #include <curl/curl.h>
257
258 #define OPT(s, v) \
259         do { \
260                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v));       \
261                 if (sta)  {                                             \
262                         syslog(LOG_ERR,                         \
263                                "error setting option " #s               \
264                                " on curl handle: %s",                   \
265                                curl_easy_strerror(sta));                \
266         } } while (0)
267
268 void InitIOStruct(AsyncIO *IO,
269                   void *Data,
270                   eNextState NextState,
271                   IO_LineReaderCallback LineReader,
272                   IO_CallBack DNS_Fail,
273                   IO_CallBack SendDone,
274                   IO_CallBack ReadDone,
275                   IO_CallBack Terminate,
276                   IO_CallBack DBTerminate,
277                   IO_CallBack ConnFail,
278                   IO_CallBack Timeout,
279                   IO_CallBack ShutdownAbort);
280
281 int InitcURLIOStruct(AsyncIO *IO,
282                      void *Data,
283                      const char* Desc,
284                      IO_CallBack SendDone,
285                      IO_CallBack Terminate,
286                      IO_CallBack DBTerminate,
287                      IO_CallBack ShutdownAbort);
288
289 eNextState ReAttachIO(AsyncIO *IO,
290                       void *pData,
291                       int ReadFirst);
292
293 void EV_backtrace(AsyncIO *IO);
294 ev_tstamp ctdl_ev_now (void);
295
296 #endif /* __EVENT_CLIENT_H__ */