1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/* $OpenBSD: asr_private.h,v 1.6 2012/09/06 13:57:51 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#ifndef ASRNODEBUG
#define DEBUG
#endif
#define QR_MASK (0x1 << 15)
#define OPCODE_MASK (0xf << 11)
#define AA_MASK (0x1 << 10)
#define TC_MASK (0x1 << 9)
#define RD_MASK (0x1 << 8)
#define RA_MASK (0x1 << 7)
#define Z_MASK (0x7 << 4)
#define RCODE_MASK (0xf)
#define OPCODE(v) ((v) & OPCODE_MASK)
#define RCODE(v) ((v) & RCODE_MASK)
struct packed {
char *data;
size_t len;
size_t offset;
const char *err;
};
struct header {
uint16_t id;
uint16_t flags;
uint16_t qdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
struct query {
char q_dname[MAXDNAME];
uint16_t q_type;
uint16_t q_class;
};
struct rr {
char rr_dname[MAXDNAME];
uint16_t rr_type;
uint16_t rr_class;
uint32_t rr_ttl;
union {
struct {
char cname[MAXDNAME];
} cname;
struct {
uint16_t preference;
char exchange[MAXDNAME];
} mx;
struct {
char nsname[MAXDNAME];
} ns;
struct {
char ptrname[MAXDNAME];
} ptr;
struct {
char mname[MAXDNAME];
char rname[MAXDNAME];
uint32_t serial;
uint32_t refresh;
uint32_t retry;
uint32_t expire;
uint32_t minimum;
} soa;
struct {
struct in_addr addr;
} in_a;
struct {
struct in6_addr addr6;
} in_aaaa;
struct {
uint16_t rdlen;
const void *rdata;
} other;
} rr;
};
#define ASR_MAXNS 5
#define ASR_MAXDB 3
#define ASR_MAXDOM 10
enum async_type {
ASR_SEND,
ASR_SEARCH,
ASR_GETRRSETBYNAME,
ASR_GETHOSTBYNAME,
ASR_GETHOSTBYADDR,
ASR_GETNETBYNAME,
ASR_GETNETBYADDR,
ASR_GETADDRINFO,
ASR_GETNAMEINFO,
};
enum asr_db_type {
ASR_DB_FILE,
ASR_DB_DNS,
ASR_DB_YP,
};
struct asr_ctx {
int ac_refcount;
int ac_options;
int ac_ndots;
char *ac_domain;
int ac_domcount;
char *ac_dom[ASR_MAXDOM];
int ac_dbcount;
int ac_db[ASR_MAXDB];
int ac_family[3];
char *ac_hostfile;
int ac_nscount;
int ac_nstimeout;
int ac_nsretries;
struct sockaddr *ac_ns[ASR_MAXNS];
};
struct asr {
char *a_path;
time_t a_mtime;
time_t a_rtime;
struct asr_ctx *a_ctx;
};
#define ASYNC_DOM_FQDN 0x00000001
#define ASYNC_DOM_NDOTS 0x00000002
#define ASYNC_DOM_HOSTALIAS 0x00000004
#define ASYNC_DOM_DOMAIN 0x00000008
#define ASYNC_DOM_ASIS 0x00000010
#define ASYNC_NODATA 0x00000100
#define ASYNC_AGAIN 0x00000200
#define ASYNC_EXTIBUF 0x00001000
#define ASYNC_EXTOBUF 0x00002000
struct async {
int (*as_run)(struct async *, struct async_res *);
struct asr_ctx *as_ctx;
int as_type;
int as_state;
/* cond */
int as_timeout;
int as_fd;
/* loop indices in ctx */
int as_dom_step;
int as_dom_idx;
int as_dom_flags;
int as_family_idx;
int as_db_idx;
int as_ns_idx;
int as_ns_cycles;
int as_count;
union {
struct {
int flags;
uint16_t reqid;
int class;
int type;
char *dname; /* not fqdn! */
int rcode; /* response code */
int ancount; /* answer count */
/* io buffers for query/response */
unsigned char *obuf;
size_t obuflen;
size_t obufsize;
unsigned char *ibuf;
size_t ibuflen;
size_t ibufsize;
size_t bufpos;
size_t datalen; /* for tcp io */
} dns;
struct {
int flags;
int class;
int type;
char *name;
struct async *subq;
int saved_h_errno;
unsigned char *ibuf;
size_t ibuflen;
size_t ibufsize;
} search;
struct {
int flags;
int class;
int type;
char *name;
struct async *subq;
} rrset;
struct {
char *name;
int family;
char *dname;
struct async *subq;
char addr[16];
int addrlen;
} hostnamadr;
struct {
char *name;
int family;
struct async *subq;
in_addr_t addr;
} netnamadr;
struct {
char *hostname;
char *servname;
int port_tcp;
int port_udp;
union {
struct sockaddr sa;
struct sockaddr_in sain;
struct sockaddr_in6 sain6;
} sa;
struct addrinfo hints;
char *fqdn;
struct addrinfo *aifirst;
struct addrinfo *ailast;
struct async *subq;
int flags;
} ai;
struct {
char *hostname;
char *servname;
size_t hostnamelen;
size_t servnamelen;
union {
struct sockaddr sa;
struct sockaddr_in sain;
struct sockaddr_in6 sain6;
} sa;
int flags;
struct async *subq;
} ni;
#define MAXTOKEN 10
} as;
};
#define AS_DB(p) ((p)->as_ctx->ac_db[(p)->as_db_idx - 1])
#define AS_FAMILY(p) ((p)->as_ctx->ac_family[(p)->as_family_idx])
enum asr_state {
ASR_STATE_INIT,
ASR_STATE_NEXT_DOMAIN,
ASR_STATE_NEXT_DB,
ASR_STATE_SAME_DB,
ASR_STATE_NEXT_FAMILY,
ASR_STATE_NEXT_NS,
ASR_STATE_UDP_SEND,
ASR_STATE_UDP_RECV,
ASR_STATE_TCP_WRITE,
ASR_STATE_TCP_READ,
ASR_STATE_PACKET,
ASR_STATE_SUBQUERY,
ASR_STATE_NOT_FOUND,
ASR_STATE_HALT,
};
/* asr_utils.c */
void packed_init(struct packed*, char*, size_t);
int pack_header(struct packed*, const struct header*);
int pack_query(struct packed*, uint16_t, uint16_t, const char*);
int unpack_header(struct packed*, struct header*);
int unpack_query(struct packed*, struct query*);
int unpack_rr(struct packed*, struct rr*);
int sockaddr_from_str(struct sockaddr *, int, const char *);
ssize_t dname_from_fqdn(const char*, char*, size_t);
/* asr.c */
struct asr_ctx *asr_use_resolver(struct asr *);
void asr_ctx_unref(struct asr_ctx *);
struct async *async_new(struct asr_ctx *, int);
void async_free(struct async *);
size_t asr_make_fqdn(const char *, const char *, char *, size_t);
size_t asr_domcat(const char *, const char *, char *, size_t);
char *asr_strdname(const char *, char *, size_t);
int asr_iter_db(struct async *);
int asr_iter_ns(struct async *);
int asr_iter_domain(struct async *, const char *, char *, size_t);
int asr_parse_namedb_line(FILE *, char **, int);
/* <*>_async.h */
struct async *res_query_async_ctx(const char *, int, int, unsigned char *, int,
struct asr_ctx *);
struct async *res_search_async_ctx(const char *, int, int, unsigned char *, int,
struct asr_ctx *);
struct async *gethostbyaddr_async_ctx(const void *, socklen_t, int,
struct asr_ctx *);
#ifdef DEBUG
extern int asr_debug;
/* asr_debug.h */
const char *asr_querystr(int);
const char *asr_transitionstr(int);
void asr_dump(struct asr *);
void asr_dump_async(struct async *);
void asr_dump_packet(FILE *, const void *, size_t, int);
void asr_printf(const char *fmt, ...);
void async_set_state(struct async *, int);
char *asr_print_addr(const struct sockaddr *, char *, size_t);
#else /* DEBUG */
#define async_set_state(a, s) do { (a)->as_state = (s); } while (0)
#endif /* DEBUG */
|