Event loop stuff complete ?
[adns] / src / internal.h
1 /**/
2
3 #ifndef ADNS_INTERNAL_H_INCLUDED
4 #define ADNS_INTERNAL_H_INCLUDED
5
6 #include <sys/time.h>
7
8 #include "adns.h"
9
10 /* Configuration and constants */
11
12 #define MAXSERVERS 5
13 #define MAXUDPRETRIES 15
14 #define UDPRETRYMS 2000
15 #define TCPMS 30000
16 #define LOCALRESOURCEMS 20
17 #define UDPMAXDGRAM 512
18 #define NSPORT 53
19
20 /* Shared data structures */
21
22 union adns__align {
23 adns_status status;
24 char *cp;
25 adns_rrtype type;
26 int int;
27 struct in_addr ia;
28 unsigned long ul;
29 };
30
31 struct adns__query {
32 /* FIXME: make sure this is all init'd properly */
33 enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state;
34 adns_query back, next;
35 adns_query parent;
36 struct { adns_query head, tail; } children;
37 struct { adns_query back, next; } siblings;
38 adns_rrtype type;
39 adns_answer *answer;
40 size_t ansalloc; ansused;
41 int id, flags, udpretries;
42 int nextudpserver;
43 unsigned long sentudp, failedtcp; /* bitmap indexed by server */
44 struct timeval timeout;
45 void *context;
46 unsigned char *querymsg;
47 int querylen;
48 char owner[1];
49 /* After the owner name and nul comes the query message */
50
51 /* Possible states:
52 *
53 * state Queue child id answer nextudpserver sentudp failedtcp
54 *
55 * udp NONE null >=0 null 0 zero zero
56 * udp timew null >=0 null any nonzero zero
57 * udp NONE null >=0 null any nonzero zero
58 *
59 * tcpwait timew null >=0 null irrelevant zero any
60 * tcpsent timew null >=0 null irrelevant zero any
61 *
62 * child childw set >=0 partial irrelevant irrelevant irrelevant
63 * done output null -1 set/null irrelevant irrelevant irrelevant
64 *
65 * +------------------------+
66 * START -----> | udp/NONE |
67 * +------------------------+
68 * / |\ \
69 * too big for UDP / UDP timeout \ \ send via UDP
70 * do this ASAP! / more retries \ \ do this ASAP!
71 * |_ desired \ _|
72 * +---------------+ +-----------+
73 * | tcpwait/timew | ____ | udp/timew |
74 * +---------------+ \ +-----------+
75 * | ^ | | |
76 * TCP conn'd; | | TCP died | | |
77 * send via TCP | | more | UDP timeout | |
78 * do this ASAP! | | servers | no more | |
79 * v | to try | retries | |
80 * +---------------+ | desired | |
81 * | tcpsent/timew | ____ | | |
82 * +---------------+ \| | |
83 * \ \ TCP died | TCP | |
84 * \ \ no more | timeout / |
85 * \ \ servers | / |
86 * \ \ to try | / |
87 * got \ \ v |_ / got
88 * reply \ _| +------------------+ / reply
89 * \ | done/output FAIL | /
90 * \ +------------------+ /
91 * \ /
92 * _| |_
93 * (..... got reply ....)
94 * / \
95 * need child query/ies / \ no child query
96 * / \
97 * |_ _|
98 * +--------------+ +----------------+
99 * | child/childw | ----------------> | done/output OK |
100 * +--------------+ children done +----------------+
101 */
102 };
103
104 typedef struct {
105 size_t used, avail;
106 unsigned char *buf;
107 } adns__vbuf;
108
109 struct adns__state {
110 /* FIXME: make sure this is all init'd properly */
111 adns_initflags iflags;
112 FILE *diagfile;
113 struct { adns_query head, tail; } timew, childw, output;
114 int nextid, udpsocket;
115 adns_vbuf rqbuf, tcpsend, tcprecv;
116 int nservers, tcpserver;
117 enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
118 int tcpsocket;
119 struct timeval tcptimeout;
120 struct server {
121 struct in_addr addr;
122 } servers[MAXSERVERS];
123 };
124
125 /* From setup.c: */
126
127 void adns__vdiag(adns_state ads, adns_initflags prevent, const char *pfx,
128 int serv, const char *fmt, va_list al);
129 void adns__debug(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
130 void adns__warn(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
131 void adns__diag(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
132
133 static inline int adns__vbuf_ensure(adns__vbuf *vb, size_t want);
134 int adns__vbuf_append(adns__vbuf *vb, const byte *data, size_t len);
135 int adns__vbuf_appendq(adns__vbuf *vb, const byte *data, size_t len);
136 /* 1=>success, 0=>realloc failed */
137
138 /* From submit.c: */
139
140 void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
141
142 /* From query.c: */
143
144 void adns__query_udp(adns_state ads, adns_query qu, struct timeval now);
145 void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now);
146 adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id,
147 adns_rrtype type, adns_queryflags flags, int *qml_r);
148
149 /* From event.c: */
150 void adns__tcp_broken(adns_state ads, const char *what, const char *why);
151 void adns__tcp_tryconnect(adns_state ads);
152
153 /* Useful static inline functions: */
154
155 static inline void timevaladd(struct timeval *tv_io, long ms) {
156 struct timeval tmp;
157 assert(ms>=0);
158 tmp= *tv_io;
159 tmp.tv_usec += (ms%1000)*1000;
160 tmp.tv_sec += ms/1000;
161 if (tmp.tv_usec >= 1000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
162 *tv_io= tmp;
163 }
164
165 static inline int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
166 static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
167
168 /* Useful macros */
169
170 #define LIST_UNLINK_PART(list,node,part) \
171 do { \
172 if ((node)->back) (node)->back->part next= (node)->part next; \
173 else (list).head= (node)->part next; \
174 if ((node)->next) (node)->next->part back= (node)->part back; \
175 else (list).tail= (node)->part back; \
176 } while(0)
177
178 #define LIST_LINK_TAIL_PART(list,node,part) \
179 do { \
180 (node)->part back= 0; \
181 (node)->part next= (list).tail; \
182 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
183 (list).tail= (node); \
184 } while(0)
185
186 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
187 #define LIST_LINK_TAIL_PART(list,node) LIST_LINK_TAIL(list,node,)
188
189 #endif