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
;
18 rtf_deref
= 0x1, /* dereference domains and perhaps produce extra data */
19 rtf_mail822
= 0x2, /* make mailboxes be in RFC822 rcpt field format */
25 void (*recv_fn
)(void);
28 /* Configuration and constants */
31 #define MAXUDPRETRIES /*15*/5
32 #define UDPRETRYMS 2000
34 #define LOCALRESOURCEMS 20
35 #define MAXUDPDGRAM 512
39 /* Shared data structures */
61 /* FIXME: make sure this is all init'd properly */
62 enum { query_udp
, query_tcpwait
, query_tcpsent
, query_child
, query_done
} state
;
63 adns_query back
, next
, parent
;
64 struct { adns_query head
, tail
; } children
;
65 struct { adns_query back
, next
; } siblings
;
68 int id
, flags
, udpretries
;
70 unsigned long udpsent
, tcpfailed
; /* bitmap indexed by server */
71 struct timeval timeout
;
76 /* After the owner name and nul comes the query message, pointed to by querymsg */
80 * state Queue child id answer nextudpserver sentudp failedtcp
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
86 * tcpwait timew null >=0 null irrelevant zero any
87 * tcpsent timew null >=0 null irrelevant zero any
89 * child childw set >=0 partial irrelevant irrelevant irrelevant
90 * done output null -1 set/null irrelevant irrelevant irrelevant
92 * +------------------------+
93 * START -----> | udp/NONE |
94 * +------------------------+
96 * too big for UDP / UDP timeout \ \ send via UDP
97 * do this ASAP! / more retries \ \ do this ASAP!
99 * +---------------+ +-----------+
100 * | tcpwait/timew | ____ | udp/timew |
101 * +---------------+ \ +-----------+
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 / |
115 * reply \ _| +------------------+ / reply
116 * \ | done/output FAIL | /
117 * \ +------------------+ /
120 * (..... got reply ....)
122 * need child query/ies / \ no child query
125 * +--------------+ +----------------+
126 * | child/childw | ----------------> | done/output OK |
127 * +--------------+ children done +----------------+
132 adns_initflags iflags
;
134 struct { adns_query head
, tail
; } timew
, childw
, output
;
135 int nextid
, udpsocket
, tcpsocket
;
136 vbuf rqbuf
, tcpsend
, tcprecv
;
137 int nservers
, tcpserver
;
138 enum adns__tcpstate
{ server_disconnected
, server_connecting
, server_ok
} tcpstate
;
139 struct timeval tcptimeout
;
142 } servers
[MAXSERVERS
];
147 void adns__vdiag(adns_state ads
, const char *pfx
, adns_initflags prevent
,
148 int serv
, const char *fmt
, va_list al
);
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);
153 int adns__vbuf_ensure(vbuf
*vb
, int want
);
154 int adns__vbuf_append(vbuf
*vb
, const byte
*data
, int len
);
155 /* 1=>success, 0=>realloc failed */
156 void adns__vbuf_appendq(vbuf
*vb
, const byte
*data
, int len
);
157 void adns__vbuf_init(vbuf
*vb
);
159 int adns__setnonblock(adns_state ads
, int fd
); /* => errno value */
163 void adns__query_nomem(adns_state ads
, adns_query qu
);
164 void adns__query_fail(adns_state ads
, adns_query qu
, adns_status stat
);
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
);
170 adns_status
adns__mkquery(adns_state ads
, const char *owner
, int ol
, int id
,
171 adns_rrtype type
, adns_queryflags flags
);
175 void adns__procdgram(adns_state ads
, const byte
*dgram
, int len
, int serv
);
179 void adns__tcp_broken(adns_state ads
, const char *what
, const char *why
);
180 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
);
181 void adns__autosys(adns_state ads
, struct timeval now
);
183 /* Useful static inline functions: */
185 static inline void timevaladd(struct timeval
*tv_io
, long ms
) {
189 tmp
.tv_usec
+= (ms
%1000)*1000000;
190 tmp
.tv_sec
+= ms
/1000;
191 if (tmp
.tv_usec
>= 1000000) { tmp
.tv_sec
++; tmp
.tv_usec
-= 1000; }
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'; }
200 #define LIST_INIT(list) ((list).head= (list).tail= 0)
202 #define LIST_UNLINK_PART(list,node,part) \
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; \
210 #define LIST_LINK_TAIL_PART(list,node,part) \
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); \
218 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
219 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)