forked from endlesssoftware/netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.c
executable file
·359 lines (329 loc) · 9.9 KB
/
dns.c
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
349
350
351
352
353
354
355
356
357
358
359
/*[[ CMS REPLACEMENT HISTORY, Element DNS.C]]*/
/*[[ *5 26-NOV-2004 08:32:39 MADISON ""]]*/
/*[[ *4 25-SEP-2004 05:42:47 MADISON "V2.3D fix"]]*/
/*[[ *3 20-JAN-2002 10:01:07 MADISON "Updates for V2.3C"]]*/
/*[[ *2 18-JAN-1999 21:04:44 MADISON "Fix handling of . as mail exchanger"]]*/
/*[[ *1 12-MAR-1998 16:40:56 MADISON "Initial CMS population"]]*/
/*[[ CMS REPLACEMENT HISTORY, Element DNS.C]]*/
/*
**++
** FACILITY: NETLIB
**
** ABSTRACT: DNS resolver support.
**
** MODULE DESCRIPTION:
**
** This module contains support routines for implementing NETLIB's
** Domain Name Service resolver.
**
** AUTHOR: M. Madison
** COPYRIGHT © 1994,1996,1999,2002-2004 MADGOAT SOFTWARE.
** ALL RIGHTS RESERVED.
**
** CREATION DATE: 22-OCT-1994
**
** MODIFICATION HISTORY:
**
** 22-OCT-1994 V1.0 Madison Initial coding.
** 17-NOV-1994 V1.1 Madison Removed socket from DNS context.
** 18-NOV-1994 V1.2 Madison Add retry count.
** 12-FEB-1996 V1.2-1 Madison Fixed rather stupid bug in expandname.
** 04-MAR-1997 V1.2-2 Madison Fixed stupid bug in dns_init (counter/ptr update order).
** 18-JAN-1999 V1.2-3 Madison Fix expands of 0-length names.
** 15-JAN-2002 V1.2-4 Madison Logical name for timeout and increase default timeout.
** 21-SEP-2003 V1.2-5 Madison Fix for skipname routine.
** 26-NOV-2004 V1.2-6 Madison Fix skipname again -- didn't get it right last time.
**--
*/
#include "netlib.h"
#include <lnmdef.h>
/*
** Forward declarations
*/
unsigned int netlib___dns_init(struct CTX *ctx);
void netlib___free_dns_context(struct CTX *ctx);
int netlib_dns_skipname(unsigned char *bufp, unsigned short *buflen);
unsigned int netlib_dns_expandname(unsigned char *buf, unsigned short *buflen,
unsigned char *bufp, struct dsc$descriptor *namdsc,
unsigned short *retlen, unsigned short *bufchrs);
/*
** OWN storage
*/
static $DESCRIPTOR(default_timeout, "0 00:00:10");
/*
** External references
*/
int netlib___get_nameservers(QUEUE *nsq);
int netlib___get_domain(char *, unsigned short, unsigned short *);
/*
**++
** ROUTINE: netlib___dns_init
**
** FUNCTIONAL DESCRIPTION:
**
** Initializes an internal DNS resolver context.
**
** RETURNS: cond_value, longword (unsigned), write only, by value
**
** PROTOTYPE:
**
** netlib___dns_init(struct CTX *ctx)
**
** ctx: NETLIB context, modify, by reference.
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: See code.
**
** SIDE EFFECTS: None.
**
**--
*/
unsigned int netlib___dns_init (struct CTX *ctx) {
struct DOMAIN *dom;
unsigned int status;
ITMLST lnmlst[3];
int i, maxidx;
unsigned int size;
unsigned short buflen;
char buf[256];
int did_tmo;
static unsigned int socktype = NETLIB_K_TYPE_DGRAM;
static unsigned int ctxsize = sizeof(struct DNSCTX);
static $DESCRIPTOR(tabnam, "LNM$FILE_DEV");
static $DESCRIPTOR(lognam, "NETLIB_SEARCH_DOMAIN");
static $DESCRIPTOR(tmolnm, "NETLIB_DNS_QUERY_TIMEOUT");
/*
** Allocate the DNS resolver context
*/
status = lib$get_vm(&ctxsize, &ctx->dnsctx);
if (!OK(status)) return status;
INIT_QUEUE(ctx->dnsctx->nsq);
INIT_QUEUE(ctx->dnsctx->domq);
/*
** Get the list of name servers we're supposed to contact
*/
if (netlib___get_nameservers(&ctx->dnsctx->nsq) == 0) {
lib$free_vm(&ctxsize, &ctx->dnsctx);
ctx->dnsctx = 0;
ctx->flags |= CTX_M_NO_DNS;
return SS$_UNSUPPORTED;
}
/*
** Get the list of NETLIB search domains, or the package-specific
** ones.
*/
ITMLST_INIT(lnmlst[0], LNM$_MAX_INDEX, sizeof(maxidx), &maxidx, 0);
ITMLST_INIT(lnmlst[1], 0, 0, 0, 0);
if (OK(sys$trnlnm(0, &tabnam, &lognam, 0, lnmlst))) {
ITMLST_INIT(lnmlst[0], LNM$_INDEX, sizeof(i), &i, 0);
ITMLST_INIT(lnmlst[1], LNM$_STRING, sizeof(buf), buf, &buflen);
ITMLST_INIT(lnmlst[2], 0, 0, 0, 0);
for (i = 0; i <= maxidx; i++) {
if (OK(sys$trnlnm(0, &tabnam, &lognam, 0, lnmlst)) && buflen != 0) {
size = buflen + sizeof(struct DOMAIN);
if (OK(lib$get_vm(&size, &dom))) {
dom->length = buflen;
memcpy(dom->name, buf, buflen);
dom->name[buflen] = '\0';
queue_insert(dom, ctx->dnsctx->domq.tail);
}
}
}
} else if (netlib___get_domain(buf, sizeof(buf), &buflen)) {
char *cp, *cp1;
int remain;
cp = buf;
remain = buflen;
/*
** A search domain must have at least two parts, to avoid the ".com.edu"
** problem, which is why we check to make sure that the remaining domain
** string has at least one dot.
*/
while (remain > 0) {
cp1 = memchr(cp, '.', remain);
if (cp1 == 0) break;
size = remain + sizeof(struct DOMAIN);
if (OK(lib$get_vm(&size, &dom))) {
dom->length = remain;
memcpy(dom->name, cp, remain);
dom->name[remain] = '\0';
queue_insert(dom, ctx->dnsctx->domq.tail);
}
remain -= (cp1 - cp) + 1;
cp = cp1 + 1;
}
}
did_tmo = 0;
ITMLST_INIT(lnmlst[0], LNM$_STRING, sizeof(buf), buf, &buflen);
ITMLST_INIT(lnmlst[1], 0, 0, 0, 0);
if (OK(sys$trnlnm(0, &tabnam, &tmolnm, 0, lnmlst))) {
struct dsc$descriptor dsc;
INIT_SDESC(dsc, buflen, buf);
/*
* Make sure it's a delta time value
*/
if (OK(sys$bintim(&dsc, &ctx->dnsctx->timeout)))
did_tmo = (int) ctx->dnsctx->timeout.long2 < 0;
}
if (!did_tmo)
sys$bintim(&default_timeout, &ctx->dnsctx->timeout);
ctx->dnsctx->retry_count = 4;
return SS$_NORMAL;
} /* netlib___dns_init */
/*
**++
** ROUTINE: netlib___free_dns_context
**
** FUNCTIONAL DESCRIPTION:
**
** Deallocates the resolver context.
**
** RETURNS: cond_value, longword (unsigned), write only, by value
**
** PROTOTYPE:
**
** netlib___free_dns_context(struct CTX *ctx)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: See code.
**
** SIDE EFFECTS: None.
**
**--
*/
void netlib___free_dns_context (struct CTX *ctx) {
struct NAMESERVER *ns;
struct DOMAIN *d;
unsigned int size;
if (ctx->dnsctx == 0) return;
size = sizeof(struct NAMESERVER);
while (queue_remove(ctx->dnsctx->nsq.head, &ns)) lib$free_vm(&size, &ns);
while (queue_remove(ctx->dnsctx->domq.head, &d)) {
size = sizeof(struct DOMAIN) + d->length;
lib$free_vm(&size, &d);
}
size = sizeof(struct DNSCTX);
lib$free_vm(&size, &ctx->dnsctx);
ctx->dnsctx = 0;
} /* netlib___free_dns_context */
/*
**++
** ROUTINE: netlib_dns_skipname
**
** FUNCTIONAL DESCRIPTION:
**
** Utility routine for skipping over a name in a DNS response.
** Handles the compressed form of domain string that DNS packets use.
**
** RETURNS: longword_signed
**
** The number of bytes to be skipped in the DNS packet is returned.
**
** PROTOTYPE:
**
** NETLIB_DNS_SKIPNAME bufp, buflen
**
** bufp: char_string, character string, read only, by reference
** buflen: word_unsigned, word (unsigned), read only, by reference
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int netlib_dns_skipname (unsigned char *bufp, unsigned short *buflen) {
unsigned char *cp, *eom;
cp = bufp;
eom = cp + *buflen;
while (cp < eom && *cp != 0) {
if (*cp >= 64) {
cp += 2;
return cp - bufp;
} else
cp += *cp + 1;
}
return (cp - bufp) + 1;
} /* netlib_dns_skipname */
/*
**++
** ROUTINE: netlib_dns_expandname
**
** FUNCTIONAL DESCRIPTION:
**
** Expands a domain string from a DNS reply packet into something
** human-readable. The string is returned, along with the number of
** bytes in the DNS packet that composed the name (which won't necessarily
** be the same as the string length because of the compression used in
** DNS packets).
**
** RETURNS: cond_value, condition value, write only, by value
**
** PROTOTYPE:
**
** NETLIB_DNS_EXPANDNAME buf, buflen, bufp, namdsc [,retlen] [,count]
**
** buf: char_string, character string, read only, by reference
** buflen: word_unsigned, word (unsigned), read only, by reference
** bufp: pointer, longword (unsigned), modify, by reference
** namdsc: char_string, character string, write only, by descriptor
** retlen: word_unsigned, word (unsigned), write only, by reference
** count: word_unsigned, word (unsigned), write only, by reference
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: See code.
**
** SIDE EFFECTS: None.
**
**--
*/
unsigned int netlib_dns_expandname (unsigned char *buf, unsigned short *buflen,
unsigned char *bufp, struct dsc$descriptor *namdsc,
unsigned short *retlen, unsigned short *countp) {
int argc, indirect, len;
struct dsc$descriptor dsc;
char name[256], *namp;
unsigned short namlen, count;
unsigned int status;
SETARGCOUNT(argc);
namp = name;
indirect = 0;
count = 0;
while (*bufp != 0) {
if (*bufp < 64) {
if (!indirect) count += *bufp + 1;
if (namp-name+*bufp < sizeof(name)) {
memcpy(namp, bufp+1, *bufp);
namp += *bufp;
*namp++ = '.';
}
bufp += *bufp + 1;
} else {
if (!indirect) count += 2;
indirect = 1;
bufp = buf + (netlib_word_swap(*(unsigned short *)bufp) - 0xc000);
if (bufp - buf > *buflen) return SS$_BADPARAM;
}
}
if (!indirect) count += 1; /* for the extra null byte at the end */
len = namp == name ? 0 : (namp-name)-1;
INIT_SDESC(dsc, len, name);
status = lib$scopy_dxdx(&dsc, namdsc);
if (!OK(status)) return status;
if (argc > 4 && retlen != 0) *retlen = len;
if (argc > 5 && countp != 0) *countp = count;
return SS$_NORMAL;
} /* netlib_dns_expandname */