Style cleanup
[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                 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 */
153
154         /* if we have linereaders, maybe we want to read more lines before
155          * the real application logic is called? */
156         IO_LineReaderCallback LineReader;
157
158         evcares_data DNS;
159
160         evcurl_request_data HttpReq;
161
162         /* Saving / loading a message async from / to disk */
163         ReadAsyncMsg *ReadMsg;
164         struct CtdlMessage *AsyncMsg;
165         struct recptypes *AsyncRcp;
166
167         /* Context specific data; Hint: put AsyncIO in there */
168         void *Data;        /* application specific data */
169         void *CitContext;  /* Citadel Session context... */
170 };
171
172 typedef struct _IOAddHandler {
173         AsyncIO *IO;
174         IO_CallBack EvAttch;
175 } IOAddHandler;
176
177 #define CCID ((CitContext*)IO->CitContext)->cs_pid
178
179 #define EV_syslog(LEVEL, FORMAT, ...) \
180         syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
181
182 #define EVM_syslog(LEVEL, FORMAT) \
183         syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
184
185 #define EVNC_syslog(LEVEL, FORMAT, ...) \
186         syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
187
188 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
189
190 void FreeAsyncIOContents(AsyncIO *IO);
191
192 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
193 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
194 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
195 eNextState QueueCurlContext(AsyncIO *IO);
196
197 eNextState EvConnectSock(AsyncIO *IO,
198                          double conn_timeout,
199                          double first_rw_timeout,
200                          int ReadFirst);
201 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
202
203 int QueueQuery(ns_type Type,
204                const char *name,
205                AsyncIO *IO,
206                DNSQueryParts *QueryParts,
207                IO_CallBack PostDNS);
208
209 void QueueGetHostByName(AsyncIO *IO,
210                         const char *Hostname,
211                         DNSQueryParts *QueryParts,
212                         IO_CallBack PostDNS);
213
214 void QueryCbDone(AsyncIO *IO);
215
216 void StopClient(AsyncIO *IO);
217
218 void StopClientWatchers(AsyncIO *IO);
219
220 void SetNextTimeout(AsyncIO *IO, double timeout);
221
222 void InitC_ares_dns(AsyncIO *IO);
223
224 #include <curl/curl.h>
225
226 #define OPT(s, v) \
227         do { \
228                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v));       \
229                 if (sta)  {                                             \
230                         syslog(LOG_ERR,                         \
231                                "error setting option " #s               \
232                                " on curl handle: %s",                   \
233                                curl_easy_strerror(sta));                \
234         } } while (0)
235
236 void InitIOStruct(AsyncIO *IO,
237                   void *Data,
238                   eNextState NextState,
239                   IO_LineReaderCallback LineReader,
240                   IO_CallBack DNS_Fail,
241                   IO_CallBack SendDone,
242                   IO_CallBack ReadDone,
243                   IO_CallBack Terminate,
244                   IO_CallBack ConnFail,
245                   IO_CallBack Timeout,
246                   IO_CallBack ShutdownAbort);
247
248 int InitcURLIOStruct(AsyncIO *IO,
249                      void *Data,
250                      const char* Desc,
251                      IO_CallBack SendDone,
252                      IO_CallBack Terminate,
253                      IO_CallBack ShutdownAbort);
254
255 eNextState ReAttachIO(AsyncIO *IO,
256                       void *pData,
257                       int ReadFirst);
258
259 #endif /* __EVENT_CLIENT_H__ */