3 #ifndef ADNS_INTERNAL_H_INCLUDED
4 #define ADNS_INTERNAL_H_INCLUDED
10 /* Configuration and constants */
13 #define MAXUDPRETRIES 15
14 #define UDPRETRYMS 2000
16 #define LOCALRESOURCEMS 20
17 #define UDPMAXDGRAM 512
20 /* Shared data structures */
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;
36 struct { adns_query head, tail; } children;
37 struct { adns_query back, next; } siblings;
40 size_t ansalloc; ansused;
41 int id, flags, udpretries;
43 unsigned long sentudp, failedtcp; /* bitmap indexed by server */
44 struct timeval timeout;
46 unsigned char *querymsg;
49 /* After the owner name and nul comes the query message */
53 * state Queue child id answer nextudpserver sentudp failedtcp
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
59 * tcpwait timew null >=0 null irrelevant zero any
60 * tcpsent timew null >=0 null irrelevant zero any
62 * child childw set >=0 partial irrelevant irrelevant irrelevant
63 * done output null -1 set/null irrelevant irrelevant irrelevant
65 * +------------------------+
66 * START -----> | udp/NONE |
67 * +------------------------+
69 * too big for UDP / UDP timeout \ \ send via UDP
70 * do this ASAP! / more retries \ \ do this ASAP!
72 * +---------------+ +-----------+
73 * | tcpwait/timew | ____ | udp/timew |
74 * +---------------+ \ +-----------+
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 / |
88 * reply \ _| +------------------+ / reply
89 * \ | done/output FAIL | /
90 * \ +------------------+ /
93 * (..... got reply ....)
95 * need child query/ies / \ no child query
98 * +--------------+ +----------------+
99 * | child/childw | ----------------> | done/output OK |
100 * +--------------+ children done +----------------+
110 /* FIXME: make sure this is all init'd properly */
111 adns_initflags iflags;
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;
119 struct timeval tcptimeout;
122 } servers[MAXSERVERS];
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);
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 */
140 void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
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);
150 void adns__tcp_broken(adns_state ads, const char *what, const char *why);
151 void adns__tcp_tryconnect(adns_state ads);
153 /* Useful static inline functions: */
155 static inline void timevaladd(struct timeval *tv_io, long ms) {
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; }
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'; }
170 #define LIST_UNLINK_PART(list,node,part) \
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; \
178 #define LIST_LINK_TAIL_PART(list,node,part) \
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); \
186 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
187 #define LIST_LINK_TAIL_PART(list,node) LIST_LINK_TAIL(list,node,)