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