3 #ifndef ADNS_INTERNAL_H_INCLUDED
4 #define ADNS_INTERNAL_H_INCLUDED
6 #define PRINTFFORMAT(a,b) __attribute__((format(printf,a,b)))
7 typedef unsigned char byte
;
17 /* Configuration and constants */
20 #define UDPMAXRETRIES /*15*/5
21 #define UDPRETRYMS 2000
23 #define LOCALRESOURCEMS 20
26 #define DNS_MAXUDP 512
27 #define DNS_MAXDOMAIN 255
28 #define DNS_HDRSIZE 12
29 #define DNS_CLASS_IN 1
40 /* Shared data structures */
64 adns_status (*parse
)(adns_state ads
, adns_query qu
, int serv
, vbuf
*vb
,
65 const byte
*dgram
, int dglen
, int cbyte
, int max
,
67 /* Parse one RR, in dgram of length dglen, starting at cbyte and
68 * extending until at most max.
70 * The RR should be stored at *store_r, of length qu->typei->rrsz.
72 * If there is an overrun which might indicate truncation, it should set
73 * *rdstart to -1; otherwise it may set it to anything else positive.
75 * This function may use vb (which has been initialised) however it likes.
80 /* FIXME: make sure this is all init'd properly */
81 enum { query_udp
, query_tcpwait
, query_tcpsent
, query_child
, query_done
} state
;
82 adns_query back
, next
, parent
;
83 struct { adns_query head
, tail
; } children
;
84 struct { adns_query back
, next
; } siblings
;
86 const typeinfo
*typei
;
88 #error make sure all this is init'd properly
89 #error make sure all this is freed properly
93 vbuf ansbuf; /* Used for answer RRs */
96 int cname_dglen, cname_begin;
98 int id, flags, udpretries;
100 unsigned long udpsent, tcpfailed; /* bitmap indexed by server */
101 struct timeval timeout;
104 /* After the owner name and nul comes the query message, pointed to by querymsg */
108 * state Queue child id ansbuf nextudpserver sentudp failedtcp
110 * udp NONE null >=0 null 0 zero zero
111 * udp timew null >=0 null any nonzero zero
112 * udp NONE null >=0 null any nonzero zero
114 * tcpwait timew null >=0 null irrelevant zero any
115 * tcpsent timew null >=0 null irrelevant zero any
117 * child childw set >=0 partial irrelevant irrelevant irrelevant
118 * done output null -1 set/null irrelevant irrelevant irrelevant
120 * +------------------------+
121 * START -----> | udp/NONE |
122 * +------------------------+
124 * too big for UDP / UDP timeout \ \ send via UDP
125 * do this ASAP! / more retries \ \ do this ASAP!
127 * +---------------+ +-----------+
128 * | tcpwait/timew | ____ | udp/timew |
129 * +---------------+ \ +-----------+
131 * TCP conn'd; | | TCP died | | |
132 * send via TCP
| | more
| UDP timeout
| |
133 * do this ASAP
! | | servers
| no more
| |
134 * v
| to
try | retries
| |
135 * +---------------+ | desired
| |
136 * | tcpsent
/timew
| ____
| | |
137 * +---------------+ \
| | |
138 * \ \ TCP died
| TCP
| |
139 * \ \ no more
| timeout
/ |
143 * reply \ _
| +------------------+ / reply
144 * \
| done
/output FAIL
| /
145 * \
+------------------+ /
148 * (..... got reply
....)
150 * need child query
/ies
/ \ no child query
153 * +--------------+ +----------------+
154 * | child
/childw
| ----------------> | done
/output OK
|
155 * +--------------+ children done
+----------------+
160 adns_initflags iflags
;
162 struct { adns_query head
, tail
; } timew
, childw
, output
;
163 int nextid
, udpsocket
, tcpsocket
;
164 vbuf rqbuf
, tcpsend
, tcprecv
;
165 int nservers
, tcpserver
;
166 enum adns__tcpstate
{ server_disconnected
, server_connecting
, server_ok
} tcpstate
;
167 struct timeval tcptimeout
;
170 } servers
[MAXSERVERS
];
175 void adns__vdiag(adns_state ads
, const char *pfx
, adns_initflags prevent
,
176 int serv
, const char *fmt
, va_list al
);
178 void adns__debug(adns_state ads
, int serv
, adns_query qu
,
179 const char *fmt
, ...) PRINTFFORMAT(3,4);
180 void adns__warn(adns_state ads
, int serv
, adns_query qu
,
181 const char *fmt
, ...) PRINTFFORMAT(3,4);
182 void adns__diag(adns_state ads
, int serv
, adns_query qu
,
183 const char *fmt
, ...) PRINTFFORMAT(3,4);
185 int adns__vbuf_ensure(vbuf
*vb
, int want
);
186 int adns__vbuf_appendstr(vbuf
*vb
, const char *data
);
187 int adns__vbuf_append(vbuf
*vb
, const byte
*data
, int len
);
188 /* 1=>success, 0=>realloc failed */
189 void adns__vbuf_appendq(vbuf
*vb
, const byte
*data
, int len
);
190 void adns__vbuf_init(vbuf
*vb
);
192 int adns__setnonblock(adns_state ads
, int fd
); /* => errno value */
196 void adns__query_nomem(adns_state ads
, adns_query qu
);
197 void adns__query_finish(adns_state ads
, adns_query qu
, adns_status stat
);
198 void adns__query_fail(adns_state ads
, adns_query qu
, adns_status stat
);
202 void adns__query_udp(adns_state ads
, adns_query qu
, struct timeval now
);
203 void adns__query_tcp(adns_state ads
, adns_query qu
, struct timeval now
);
204 adns_status
adns__mkquery(adns_state ads
, const char *owner
, int ol
, int id
,
205 const typeinfo
*typei
, adns_queryflags flags
);
209 void adns__procdgram(adns_state ads
, const byte
*dgram
, int len
,
210 int serv
, struct timeval now
);
214 const typeinfo
*adns__findtype(adns_rrtype type
);
219 adns_state ads
, int serv
;
221 int dglen
, max
, cbyte
, namelen
;
222 int *dmend_rlater
, *namelen_rlater
;
225 void adns__findlabel_start(findlabel_state
*fls
,
226 adns_state ads
, int serv
,
227 const byte
*dgram
, int dglen
, int max
,
228 int dmbegin
, int *dmend_rlater
);
229 /* Finds labels in a domain in a datagram.
231 * Call this routine first.
232 * endpoint_rlater may be null.
235 adns_status
adns__findlabel_next(findlabel_state
*fls
,
236 int *lablen_r
, int *labstart_r
);
237 /* Then, call this one repeatedly.
239 * It will return adns_s_ok if all is well, and tell you the length
240 * and start of successive labels. labstart_r may be null, but
241 * lablen_r must not be.
243 * After the last label, it will return with *lablen_r zero.
244 * Do not then call it again; instead, just throw away the findlabel_state.
246 * *dmend_rlater will have been set to point to the next part of
247 * the datagram after the label (or after the uncompressed part,
248 * if compression was used). *namelen_rlater will have been set
249 * to the length of the domain name (total length of labels plus
250 * 1 for each intervening dot).
252 * If the datagram appears to be truncated, *lablen_r will be -1.
253 * *dmend_rlater, *labstart_r and *namelen_r may contain garbage.
254 * Do not call _next again.
256 * There may also be errors, in which case *dmend_rlater,
257 * *namelen_rlater, *lablen_r and *labstart_r may contain garbage.
258 * Do not then call findlabel_next again.
261 adns_status
adns__parse_domain(adns_state ads
, int serv
, vbuf
*vb
,
262 const byte
*dgram
, int dglen
,
263 int *cbyte_io
, int max
);
264 /* vb must already have been initialised; it will be reset if necessary.
265 * If there is truncation, vb->used will be set to 0; otherwise
266 * (if there is no error) vb will be null-terminated.
267 * If there is an error vb and *cbyte_io may be left indeterminate.
270 adns_status
adns__findrr(adns_state ads
, int serv
,
271 const byte
*dgram
, int dglen
, int *cbyte_io
,
272 int *type_r
, int *class_r
, int *rdlen_r
, int *rdstart_r
,
273 const byte
*eo_dgram
, int eo_dglen
, int eo_cbyte
,
275 /* Finds the extent and some of the contents of an RR in a datagram
276 * and does some checks. The datagram is *dgram, length dglen, and
277 * the RR starts at *cbyte_io (which is updated afterwards to point
278 * to the end of the RR).
280 * The type, class and RRdata length and start are returned iff
281 * the corresponding pointer variables are not null. type_r and
282 * class_r may not be null.
284 * If the caller thinks they know what the owner of the RR ought to
285 * be they can pass in details in eo_*: this is another (or perhaps
286 * the same datagram), and a pointer to where the putative owner
287 * starts in that datagram. In this case *eo_matched_r will be set
288 * to 1 if the datagram matched or 0 if it did not. Either
289 * both eo_dgram and eo_matched_r must both be non-null, or they
290 * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
291 * The eo datagram and contained owner domain MUST be valid and
294 * If there is truncation then *type_r will be set to -1 and
295 * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
298 * If an error is returned then *type_r will be undefined too.
301 int vbuf__append_quoted1035(vbuf
*vb
, const byte
*buf
, int len
);
305 void adns__tcp_broken(adns_state ads
, const char *what
, const char *why
);
306 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
);
307 void adns__autosys(adns_state ads
, struct timeval now
);
309 /* Useful static inline functions: */
311 static inline void timevaladd(struct timeval
*tv_io
, long ms
) {
315 tmp
.tv_usec
+= (ms
%1000)*1000000;
316 tmp
.tv_sec
+= ms
/1000;
317 if (tmp
.tv_usec
>= 1000000) { tmp
.tv_sec
++; tmp
.tv_usec
-= 1000; }
321 static inline int ctype_whitespace(int c
) { return c
==' ' || c
=='\n' || c
=='\t'; }
322 static inline int ctype_digit(int c
) { return c
>='0' && c
<='9'; }
323 static inline int ctype_alpha(int c
) {
324 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' || c
<= 'Z');
329 #define LIST_INIT(list) ((list).head= (list).tail= 0)
331 #define LIST_UNLINK_PART(list,node,part) \
333 if ((node)->back) (node)->back->part next= (node)->part next; \
334 else (list).head= (node)->part next; \
335 if ((node)->next) (node)->next->part back= (node)->part back; \
336 else (list).tail= (node)->part back; \
339 #define LIST_LINK_TAIL_PART(list,node,part) \
341 (node)->part back= 0; \
342 (node)->part next= (list).tail; \
343 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
344 (list).tail= (node); \
347 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
348 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
350 #define GETIL_B(cb) (dgram[(cb)++])
351 #define GET_B(cb,tv) ((tv)= GETIL_B((cb)))
352 #define GET_W(cb,tv) ((tv)=0, (tv)|=(GETIL_B((cb))<<8), (tv)|=GETIL_B(cb), (tv))