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
25 #define DNS_UDPPORT 53
26 #define DNS_MAXUDP 512
27 #define DNS_MAXDOMAIN 255
28 #define DNS_HDRSIZE 12
29 #define DNS_CLASS_IN 1
31 /* Shared data structures */
53 unsigned long searchkey
; /* flags and typecode swapped */
58 /* FIXME: make sure this is all init'd properly */
59 enum { query_udp
, query_tcpwait
, query_tcpsent
, query_child
, query_done
} state
;
60 adns_query back
, next
, parent
;
61 struct { adns_query head
, tail
; } children
;
62 struct { adns_query back
, next
; } siblings
;
64 const typeinfo
*typei
;
66 int id
, flags
, udpretries
;
68 unsigned long udpsent
, tcpfailed
; /* bitmap indexed by server */
69 struct timeval timeout
;
74 /* After the owner name and nul comes the query message, pointed to by querymsg */
78 * state Queue child id answer nextudpserver sentudp failedtcp
80 * udp NONE null >=0 null 0 zero zero
81 * udp timew null >=0 null any nonzero zero
82 * udp NONE null >=0 null any nonzero zero
84 * tcpwait timew null >=0 null irrelevant zero any
85 * tcpsent timew null >=0 null irrelevant zero any
87 * child childw set >=0 partial irrelevant irrelevant irrelevant
88 * done output null -1 set/null irrelevant irrelevant irrelevant
90 * +------------------------+
91 * START -----> | udp/NONE |
92 * +------------------------+
94 * too big for UDP / UDP timeout \ \ send via UDP
95 * do this ASAP! / more retries \ \ do this ASAP!
97 * +---------------+ +-----------+
98 * | tcpwait/timew | ____ | udp/timew |
99 * +---------------+ \ +-----------+
101 * TCP conn'd; | | TCP died | | |
102 * send via TCP | | more | UDP timeout | |
103 * do this ASAP! | | servers | no more | |
104 * v | to try | retries | |
105 * +---------------+ | desired | |
106 * | tcpsent/timew | ____ | | |
107 * +---------------+ \| | |
108 * \ \ TCP died | TCP | |
109 * \ \ no more | timeout / |
113 * reply \ _| +------------------+ / reply
114 * \ | done/output FAIL | /
115 * \ +------------------+ /
118 * (..... got reply ....)
120 * need child query/ies / \ no child query
123 * +--------------+ +----------------+
124 * | child/childw | ----------------> | done/output OK |
125 * +--------------+ children done +----------------+
130 adns_initflags iflags
;
132 struct { adns_query head
, tail
; } timew
, childw
, output
;
133 int nextid
, udpsocket
, tcpsocket
;
134 vbuf rqbuf
, tcpsend
, tcprecv
;
135 int nservers
, tcpserver
;
136 enum adns__tcpstate
{ server_disconnected
, server_connecting
, server_ok
} tcpstate
;
137 struct timeval tcptimeout
;
140 } servers
[MAXSERVERS
];
145 void adns__vdiag(adns_state ads
, const char *pfx
, adns_initflags prevent
,
146 int serv
, const char *fmt
, va_list al
);
147 void adns__debug(adns_state ads
, int serv
, const char *fmt
, ...) PRINTFFORMAT(3,4);
148 void adns__warn(adns_state ads
, int serv
, const char *fmt
, ...) PRINTFFORMAT(3,4);
149 void adns__diag(adns_state ads
, int serv
, const char *fmt
, ...) PRINTFFORMAT(3,4);
151 int adns__vbuf_ensure(vbuf
*vb
, int want
);
152 int adns__vbuf_append(vbuf
*vb
, const byte
*data
, int len
);
153 /* 1=>success, 0=>realloc failed */
154 void adns__vbuf_appendq(vbuf
*vb
, const byte
*data
, int len
);
155 void adns__vbuf_init(vbuf
*vb
);
157 int adns__setnonblock(adns_state ads
, int fd
); /* => errno value */
161 void adns__query_nomem(adns_state ads
, adns_query qu
);
162 void adns__query_fail(adns_state ads
, adns_query qu
, adns_status stat
);
166 void adns__query_udp(adns_state ads
, adns_query qu
, struct timeval now
);
167 void adns__query_tcp(adns_state ads
, adns_query qu
, struct timeval now
);
168 adns_status
adns__mkquery(adns_state ads
, const char *owner
, int ol
, int id
,
169 adns_rrtype type
, adns_queryflags flags
);
173 void adns__procdgram(adns_state ads
, const byte
*dgram
, int len
, int serv
);
177 void adns__tcp_broken(adns_state ads
, const char *what
, const char *why
);
178 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
);
179 void adns__autosys(adns_state ads
, struct timeval now
);
181 /* Useful static inline functions: */
183 static inline void timevaladd(struct timeval
*tv_io
, long ms
) {
187 tmp
.tv_usec
+= (ms
%1000)*1000000;
188 tmp
.tv_sec
+= ms
/1000;
189 if (tmp
.tv_usec
>= 1000000) { tmp
.tv_sec
++; tmp
.tv_usec
-= 1000; }
193 static inline int ctype_whitespace(int c
) { return c
==' ' || c
=='\n' || c
=='\t'; }
194 static inline int ctype_digit(int c
) { return c
>='0' && c
<='9'; }
198 #define LIST_INIT(list) ((list).head= (list).tail= 0)
200 #define LIST_UNLINK_PART(list,node,part) \
202 if ((node)->back) (node)->back->part next= (node)->part next; \
203 else (list).head= (node)->part next; \
204 if ((node)->next) (node)->next->part back= (node)->part back; \
205 else (list).tail= (node)->part back; \
208 #define LIST_LINK_TAIL_PART(list,node,part) \
210 (node)->part back= 0; \
211 (node)->part next= (list).tail; \
212 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
213 (list).tail= (node); \
216 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
217 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)