chiark / gitweb /
Experimental: rr types as pointer to struct, not enum.
[adns] / src / internal.h
1 /**/
2
3 #ifndef ADNS_INTERNAL_H_INCLUDED
4 #define ADNS_INTERNAL_H_INCLUDED
5
6 #define PRINTFFORMAT(a,b) __attribute__((format(printf,a,b)))
7 typedef unsigned char byte;
8
9 #include <stdarg.h>
10 #include <assert.h>
11 #include <unistd.h>
12
13 #include <sys/time.h>
14
15 #include "adns.h"
16
17 typedef enum {
18   rtf_deref=     0x1, /* dereference domains and perhaps produce extra data */
19   rtf_mail822=   0x2, /* make mailboxes be in RFC822 rcpt field format */
20 } rrtype_flags;
21
22 struct adns__rrtype {
23   int qtype;
24   rrtype_flags rflags;
25   void (*recv_fn)(void);
26 };
27
28 /* Configuration and constants */
29
30 #define MAXSERVERS 5
31 #define MAXUDPRETRIES /*15*/5
32 #define UDPRETRYMS 2000
33 #define TCPMS 30000
34 #define LOCALRESOURCEMS 20
35 #define MAXUDPDGRAM 512
36 #define NSPORT 53
37 #define MAXDNAME 255
38
39 /* Shared data structures */
40
41 typedef union {
42   adns_status status;
43   char *cp;
44   adns_rrtype type;
45   int i;
46   struct in_addr ia;
47   unsigned long ul;
48 } rr_align;
49
50 typedef struct {
51   int used, avail;
52   byte *buf;
53 } vbuf;
54
55 typedef union {
56   void *ext;
57   int dmaddr_index;
58 } qcontext;
59
60 struct adns__query {
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;
66   adns_rrtype type;
67   vbuf answer;
68   int id, flags, udpretries;
69   int udpnextserver;
70   unsigned long udpsent, tcpfailed; /* bitmap indexed by server */
71   struct timeval timeout;
72   byte *querymsg;
73   int querylen;
74   qcontext context;
75   char owner[1];
76   /* After the owner name and nul comes the query message, pointed to by querymsg */
77
78   /* Possible states:
79    *
80    *  state   Queue   child  id   answer    nextudpserver  sentudp     failedtcp
81    *
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
85    *
86    *  tcpwait timew   null   >=0  null      irrelevant     zero        any
87    *  tcpsent timew   null   >=0  null      irrelevant     zero        any
88    *
89    *  child   childw  set    >=0  partial   irrelevant     irrelevant  irrelevant
90    *  done    output  null   -1   set/null  irrelevant     irrelevant  irrelevant
91    *
92    *                          +------------------------+
93    *             START -----> |      udp/NONE          |
94    *                          +------------------------+
95    *                         /                       |\  \
96    *        too big for UDP /             UDP timeout  \  \ send via UDP
97    *        do this ASAP!  /              more retries  \  \   do this ASAP!
98    *                     |_                  desired     \  _|
99    *              +---------------+                     +-----------+
100    *              | tcpwait/timew | ____                | udp/timew |
101    *              +---------------+     \               +-----------+
102    *                    |  ^             |                 | |
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         / |
112    *                    \   \ servers    |                /  |
113    *                     \   \ to try    |               /   |
114    *                  got \   \          v             |_    / got
115    *                 reply \   _| +------------------+      / reply
116    *                        \     | done/output FAIL |     /
117    *                         \    +------------------+    /
118    *                          \                          /
119    *                           _|                      |_
120    *                             (..... got reply ....)
121    *                              /                   \
122    *        need child query/ies /                     \ no child query
123    *                            /                       \
124    *                          |_                         _|
125    *                +--------------+                   +----------------+
126    *                | child/childw | ----------------> | done/output OK |
127    *                +--------------+  children done    +----------------+
128    */
129 };
130
131 struct adns__state {
132   adns_initflags iflags;
133   FILE *diagfile;
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;
140   struct server {
141     struct in_addr addr;
142   } servers[MAXSERVERS];
143 };
144
145 /* From setup.c: */
146
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);
152
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);
158
159 int adns__setnonblock(adns_state ads, int fd); /* => errno value */
160
161 /* From submit.c: */
162
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);
165
166 /* From query.c: */
167
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);
172
173 /* From reply.c: */
174
175 void adns__procdgram(adns_state ads, const byte *dgram, int len, int serv);
176
177 /* From event.c: */
178
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);
182
183 /* Useful static inline functions: */
184
185 static inline void timevaladd(struct timeval *tv_io, long ms) {
186   struct timeval tmp;
187   assert(ms>=0);
188   tmp= *tv_io;
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; }
192   *tv_io= tmp;
193 }
194
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'; }
197
198 /* Useful macros */
199
200 #define LIST_INIT(list) ((list).head= (list).tail= 0)
201
202 #define LIST_UNLINK_PART(list,node,part) \
203   do { \
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; \
208   } while(0)
209
210 #define LIST_LINK_TAIL_PART(list,node,part) \
211   do { \
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); \
216   } while(0)
217
218 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
219 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
220
221 #endif