move URL parsing / struct over into libcitadel
[citadel.git] / citadel / event_client.h
1 #include <ev.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netdb.h>
5 #include <arpa/nameser.h>
6 #include <ares.h>
7
8 typedef struct AsyncIO AsyncIO;
9
10 typedef enum _eNextState {
11         eSendReply, 
12         eSendMore,
13         eReadMessage, 
14         eTerminateConnection,
15         eAbort
16 }eNextState;
17
18 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
19 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
20 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
21 typedef void (*FreeDNSReply)(void *DNSData);
22
23 typedef struct _DNSQueryParts {
24         ParseDNSAnswerCb DNS_CB;
25         IO_CallBack PostDNS;
26
27         int DNSStatus;
28         void *VParsedDNSReply;
29         FreeDNSReply DNSReplyFree;
30         void *Data;
31 } DNSQueryParts;
32
33
34 struct AsyncIO {
35         StrBuf *Host;
36         char service[32];
37
38         /* To cycle through several possible services... * /
39         struct addrinfo *res;
40         struct addrinfo *curr_ai;
41         */
42
43         /* connection related */
44         int IP6;
45         struct sockaddr_in6 Addr;
46
47         int sock;
48         unsigned short dport;
49         eNextState NextState;
50         
51         ev_cleanup abort_by_shutdown;
52
53         ev_timer conn_fail, 
54                 rw_timeout;
55         ev_idle unwind_stack;
56         ev_io recv_event, 
57                 send_event, 
58                 conn_event;
59         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
60
61         /* read/send related... */
62         StrBuf *IOBuf;
63         IOBuffer SendBuf, 
64                 RecvBuf;
65
66         /* Citadel application callbacks... */
67         IO_CallBack ReadDone, /* Theres new data to read... */
68                 SendDone,     /* we may send more data */
69                 Terminate,    /* shutting down... */
70                 Timeout,      /* Timeout handler; may also be connection timeout */
71                 ConnFail,     /* What to do when one connection failed? */
72                 ShutdownAbort;/* we're going down. make your piece. */ 
73
74         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
75
76         ev_io dns_recv_event, 
77                 dns_send_event;
78         struct ares_options DNSOptions;
79         ares_channel DNSChannel;
80         DNSQueryParts *DNSQuery;
81
82         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
83         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
84         void *Data; /* application specific data */
85 };
86
87 typedef struct _IOAddHandler {
88         AsyncIO *IO;
89         IO_CallBack EvAttch;
90 }IOAddHandler; 
91
92 void FreeAsyncIOContents(AsyncIO *IO);
93
94 int QueueEventContext(AsyncIO *IO, IO_CallBack CB);
95 int ShutDownEventQueue(void);
96
97 eNextState InitEventIO(AsyncIO *IO, 
98                        void *pData, 
99                        double conn_timeout, 
100                        double first_rw_timeout,
101                        int ReadFirst);
102 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
103
104 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
105 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
106
107 void QueryCbDone(AsyncIO *IO);
108
109 void StopClient(AsyncIO *IO);
110
111 void SetNextTimeout(AsyncIO *IO, double timeout);
112
113 void InitC_ares_dns(AsyncIO *IO);