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