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