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