diff options
Diffstat (limited to 'src/lib/libc/net/res_debug.c')
-rw-r--r-- | src/lib/libc/net/res_debug.c | 1565 |
1 files changed, 1565 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c new file mode 100644 index 0000000000..b39e9a3b14 --- /dev/null +++ b/src/lib/libc/net/res_debug.c | |||
@@ -0,0 +1,1565 @@ | |||
1 | /* $OpenBSD: res_debug.c,v 1.16 2003/03/04 00:29:17 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1990, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1990, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. All advertising materials mentioning features or use of this software | ||
18 | * must display the following acknowledgement: | ||
19 | * This product includes software developed by the University of | ||
20 | * California, Berkeley and its contributors. | ||
21 | * 4. Neither the name of the University nor the names of its contributors | ||
22 | * may be used to endorse or promote products derived from this software | ||
23 | * without specific prior written permission. | ||
24 | * | ||
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
35 | * SUCH DAMAGE. | ||
36 | * - | ||
37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
38 | * | ||
39 | * Permission to use, copy, modify, and distribute this software for any | ||
40 | * purpose with or without fee is hereby granted, provided that the above | ||
41 | * copyright notice and this permission notice appear in all copies, and that | ||
42 | * the name of Digital Equipment Corporation not be used in advertising or | ||
43 | * publicity pertaining to distribution of the document or software without | ||
44 | * specific, written prior permission. | ||
45 | * | ||
46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
53 | * SOFTWARE. | ||
54 | * - | ||
55 | * Portions Copyright (c) 1995 by International Business Machines, Inc. | ||
56 | * | ||
57 | * International Business Machines, Inc. (hereinafter called IBM) grants | ||
58 | * permission under its copyrights to use, copy, modify, and distribute this | ||
59 | * Software with or without fee, provided that the above copyright notice and | ||
60 | * all paragraphs of this notice appear in all copies, and that the name of IBM | ||
61 | * not be used in connection with the marketing of any product incorporating | ||
62 | * the Software or modifications thereof, without specific, written prior | ||
63 | * permission. | ||
64 | * | ||
65 | * To the extent it has a right to do so, IBM grants an immunity from suit | ||
66 | * under its patents, if any, for the use, sale or manufacture of products to | ||
67 | * the extent that such products are used for performing Domain Name System | ||
68 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | ||
69 | * granted for any product per se or for any other function of any product. | ||
70 | * | ||
71 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | ||
72 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
73 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | ||
74 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | ||
75 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | ||
76 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
77 | * --Copyright-- | ||
78 | */ | ||
79 | |||
80 | #if defined(LIBC_SCCS) && !defined(lint) | ||
81 | #if 0 | ||
82 | static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; | ||
83 | static char rcsid[] = "$From: res_debug.c,v 8.19 1996/11/26 10:11:23 vixie Exp $"; | ||
84 | #else | ||
85 | static char rcsid[] = "$OpenBSD: res_debug.c,v 1.16 2003/03/04 00:29:17 itojun Exp $"; | ||
86 | #endif | ||
87 | #endif /* LIBC_SCCS and not lint */ | ||
88 | |||
89 | #include <sys/param.h> | ||
90 | #include <sys/types.h> | ||
91 | #include <sys/socket.h> | ||
92 | #include <netinet/in.h> | ||
93 | #include <arpa/inet.h> | ||
94 | #include <arpa/nameser.h> | ||
95 | |||
96 | #include <ctype.h> | ||
97 | #include <netdb.h> | ||
98 | #include <resolv.h> | ||
99 | #include <stdio.h> | ||
100 | #include <time.h> | ||
101 | |||
102 | #include <stdlib.h> | ||
103 | #include <string.h> | ||
104 | |||
105 | #include "thread_private.h" | ||
106 | |||
107 | extern const char *_res_opcodes[]; | ||
108 | extern const char *_res_resultcodes[]; | ||
109 | |||
110 | static const char *loc_ntoal(const u_char *binary, char *ascii, int ascii_len); | ||
111 | |||
112 | /* XXX: we should use getservbyport() instead. */ | ||
113 | static const char * | ||
114 | dewks(wks) | ||
115 | int wks; | ||
116 | { | ||
117 | static char nbuf[20]; | ||
118 | |||
119 | switch (wks) { | ||
120 | case 5: return "rje"; | ||
121 | case 7: return "echo"; | ||
122 | case 9: return "discard"; | ||
123 | case 11: return "systat"; | ||
124 | case 13: return "daytime"; | ||
125 | case 15: return "netstat"; | ||
126 | case 17: return "qotd"; | ||
127 | case 19: return "chargen"; | ||
128 | case 20: return "ftp-data"; | ||
129 | case 21: return "ftp"; | ||
130 | case 23: return "telnet"; | ||
131 | case 25: return "smtp"; | ||
132 | case 37: return "time"; | ||
133 | case 39: return "rlp"; | ||
134 | case 42: return "name"; | ||
135 | case 43: return "whois"; | ||
136 | case 53: return "domain"; | ||
137 | case 57: return "apts"; | ||
138 | case 59: return "apfs"; | ||
139 | case 67: return "bootps"; | ||
140 | case 68: return "bootpc"; | ||
141 | case 69: return "tftp"; | ||
142 | case 77: return "rje"; | ||
143 | case 79: return "finger"; | ||
144 | case 87: return "link"; | ||
145 | case 95: return "supdup"; | ||
146 | case 100: return "newacct"; | ||
147 | case 101: return "hostnames"; | ||
148 | case 102: return "iso-tsap"; | ||
149 | case 103: return "x400"; | ||
150 | case 104: return "x400-snd"; | ||
151 | case 105: return "csnet-ns"; | ||
152 | case 109: return "pop-2"; | ||
153 | case 111: return "sunrpc"; | ||
154 | case 113: return "auth"; | ||
155 | case 115: return "sftp"; | ||
156 | case 117: return "uucp-path"; | ||
157 | case 119: return "nntp"; | ||
158 | case 121: return "erpc"; | ||
159 | case 123: return "ntp"; | ||
160 | case 133: return "statsrv"; | ||
161 | case 136: return "profile"; | ||
162 | case 144: return "NeWS"; | ||
163 | case 161: return "snmp"; | ||
164 | case 162: return "snmp-trap"; | ||
165 | case 170: return "print-srv"; | ||
166 | default: | ||
167 | (void) snprintf(nbuf, sizeof nbuf, "%d", wks); | ||
168 | return (nbuf); | ||
169 | } | ||
170 | } | ||
171 | |||
172 | /* XXX: we should use getprotobynumber() instead. */ | ||
173 | static const char * | ||
174 | deproto(protonum) | ||
175 | int protonum; | ||
176 | { | ||
177 | static char nbuf[20]; | ||
178 | |||
179 | switch (protonum) { | ||
180 | case 1: return "icmp"; | ||
181 | case 2: return "igmp"; | ||
182 | case 3: return "ggp"; | ||
183 | case 5: return "st"; | ||
184 | case 6: return "tcp"; | ||
185 | case 7: return "ucl"; | ||
186 | case 8: return "egp"; | ||
187 | case 9: return "igp"; | ||
188 | case 11: return "nvp-II"; | ||
189 | case 12: return "pup"; | ||
190 | case 16: return "chaos"; | ||
191 | case 17: return "udp"; | ||
192 | default: | ||
193 | (void) snprintf(nbuf, sizeof nbuf, "%d", protonum); | ||
194 | return (nbuf); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | static const u_char * | ||
199 | do_rrset(msg, len, cp, cnt, pflag, file, hs) | ||
200 | int cnt, pflag, len; | ||
201 | const u_char *cp, *msg; | ||
202 | const char *hs; | ||
203 | FILE *file; | ||
204 | { | ||
205 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
206 | int n; | ||
207 | int sflag; | ||
208 | |||
209 | /* | ||
210 | * Print answer records. | ||
211 | */ | ||
212 | sflag = (_resp->pfcode & pflag); | ||
213 | if ((n = ntohs(cnt))) { | ||
214 | if ((!_resp->pfcode) || | ||
215 | ((sflag) && (_resp->pfcode & RES_PRF_HEAD1))) | ||
216 | fprintf(file, "%s", hs); | ||
217 | while (--n >= 0) { | ||
218 | if ((!_resp->pfcode) || sflag) { | ||
219 | cp = p_rr(cp, msg, file); | ||
220 | } else { | ||
221 | unsigned int dlen; | ||
222 | cp += __dn_skipname(cp, cp + MAXCDNAME); | ||
223 | cp += INT16SZ; | ||
224 | cp += INT16SZ; | ||
225 | cp += INT32SZ; | ||
226 | dlen = _getshort((u_char*)cp); | ||
227 | cp += INT16SZ; | ||
228 | cp += dlen; | ||
229 | } | ||
230 | if ((cp - msg) > len) | ||
231 | return (NULL); | ||
232 | } | ||
233 | if ((!_resp->pfcode) || | ||
234 | ((sflag) && (_resp->pfcode & RES_PRF_HEAD1))) | ||
235 | putc('\n', file); | ||
236 | } | ||
237 | return (cp); | ||
238 | } | ||
239 | |||
240 | void | ||
241 | __p_query(msg) | ||
242 | const u_char *msg; | ||
243 | { | ||
244 | __fp_query(msg, stdout); | ||
245 | } | ||
246 | |||
247 | /* | ||
248 | * Print the current options. | ||
249 | * This is intended to be primarily a debugging routine. | ||
250 | */ | ||
251 | void | ||
252 | __fp_resstat(statp, file) | ||
253 | struct __res_state *statp; | ||
254 | FILE *file; | ||
255 | { | ||
256 | register u_long mask; | ||
257 | |||
258 | fprintf(file, ";; res options:"); | ||
259 | if (!statp) | ||
260 | statp = &_res; | ||
261 | for (mask = 1; mask != 0; mask <<= 1) | ||
262 | if (statp->options & mask) | ||
263 | fprintf(file, " %s", p_option(mask)); | ||
264 | putc('\n', file); | ||
265 | } | ||
266 | |||
267 | /* | ||
268 | * Print the contents of a query. | ||
269 | * This is intended to be primarily a debugging routine. | ||
270 | */ | ||
271 | void | ||
272 | __fp_nquery(msg, len, file) | ||
273 | const u_char *msg; | ||
274 | int len; | ||
275 | FILE *file; | ||
276 | { | ||
277 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
278 | register const u_char *cp, *endMark; | ||
279 | register const HEADER *hp; | ||
280 | register int n; | ||
281 | |||
282 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
283 | return; | ||
284 | |||
285 | #define TruncTest(x) if (x > endMark) goto trunc | ||
286 | #define ErrorTest(x) if (x == NULL) goto error | ||
287 | |||
288 | /* | ||
289 | * Print header fields. | ||
290 | */ | ||
291 | hp = (HEADER *)msg; | ||
292 | cp = msg + HFIXEDSZ; | ||
293 | endMark = msg + len; | ||
294 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX) || hp->rcode) { | ||
295 | fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %u", | ||
296 | _res_opcodes[hp->opcode], | ||
297 | _res_resultcodes[hp->rcode], | ||
298 | ntohs(hp->id)); | ||
299 | putc('\n', file); | ||
300 | } | ||
301 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX)) | ||
302 | putc(';', file); | ||
303 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD2)) { | ||
304 | fprintf(file, "; flags:"); | ||
305 | if (hp->qr) | ||
306 | fprintf(file, " qr"); | ||
307 | if (hp->aa) | ||
308 | fprintf(file, " aa"); | ||
309 | if (hp->tc) | ||
310 | fprintf(file, " tc"); | ||
311 | if (hp->rd) | ||
312 | fprintf(file, " rd"); | ||
313 | if (hp->ra) | ||
314 | fprintf(file, " ra"); | ||
315 | if (hp->unused) | ||
316 | fprintf(file, " UNUSED-BIT-ON"); | ||
317 | if (hp->ad) | ||
318 | fprintf(file, " ad"); | ||
319 | if (hp->cd) | ||
320 | fprintf(file, " cd"); | ||
321 | } | ||
322 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD1)) { | ||
323 | fprintf(file, "; Ques: %u", ntohs(hp->qdcount)); | ||
324 | fprintf(file, ", Ans: %u", ntohs(hp->ancount)); | ||
325 | fprintf(file, ", Auth: %u", ntohs(hp->nscount)); | ||
326 | fprintf(file, ", Addit: %u", ntohs(hp->arcount)); | ||
327 | } | ||
328 | if ((!_resp->pfcode) || (_resp->pfcode & | ||
329 | (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { | ||
330 | putc('\n',file); | ||
331 | } | ||
332 | /* | ||
333 | * Print question records. | ||
334 | */ | ||
335 | if ((n = ntohs(hp->qdcount))) { | ||
336 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
337 | fprintf(file, ";; QUESTIONS:\n"); | ||
338 | while (--n >= 0) { | ||
339 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
340 | fprintf(file, ";;\t"); | ||
341 | TruncTest(cp); | ||
342 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
343 | cp = p_cdnname(cp, msg, len, file); | ||
344 | else { | ||
345 | int n; | ||
346 | char name[MAXDNAME]; | ||
347 | |||
348 | if ((n = dn_expand(msg, msg+len, cp, name, | ||
349 | sizeof name)) < 0) | ||
350 | cp = NULL; | ||
351 | else | ||
352 | cp += n; | ||
353 | } | ||
354 | ErrorTest(cp); | ||
355 | TruncTest(cp); | ||
356 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
357 | fprintf(file, ", type = %s", | ||
358 | __p_type(_getshort((u_char*)cp))); | ||
359 | cp += INT16SZ; | ||
360 | TruncTest(cp); | ||
361 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
362 | fprintf(file, ", class = %s\n", | ||
363 | __p_class(_getshort((u_char*)cp))); | ||
364 | cp += INT16SZ; | ||
365 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
366 | putc('\n', file); | ||
367 | } | ||
368 | } | ||
369 | /* | ||
370 | * Print authoritative answer records | ||
371 | */ | ||
372 | TruncTest(cp); | ||
373 | cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file, | ||
374 | ";; ANSWERS:\n"); | ||
375 | ErrorTest(cp); | ||
376 | |||
377 | /* | ||
378 | * print name server records | ||
379 | */ | ||
380 | TruncTest(cp); | ||
381 | cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file, | ||
382 | ";; AUTHORITY RECORDS:\n"); | ||
383 | ErrorTest(cp); | ||
384 | |||
385 | TruncTest(cp); | ||
386 | /* | ||
387 | * print additional records | ||
388 | */ | ||
389 | cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file, | ||
390 | ";; ADDITIONAL RECORDS:\n"); | ||
391 | ErrorTest(cp); | ||
392 | return; | ||
393 | trunc: | ||
394 | fprintf(file, "\n;; ...truncated\n"); | ||
395 | return; | ||
396 | error: | ||
397 | fprintf(file, "\n;; ...malformed\n"); | ||
398 | } | ||
399 | |||
400 | void | ||
401 | __fp_query(msg, file) | ||
402 | const u_char *msg; | ||
403 | FILE *file; | ||
404 | { | ||
405 | fp_nquery(msg, PACKETSZ, file); | ||
406 | } | ||
407 | |||
408 | const u_char * | ||
409 | __p_cdnname(cp, msg, len, file) | ||
410 | const u_char *cp, *msg; | ||
411 | int len; | ||
412 | FILE *file; | ||
413 | { | ||
414 | char name[MAXDNAME]; | ||
415 | int n; | ||
416 | |||
417 | if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0) | ||
418 | return (NULL); | ||
419 | if (name[0] == '\0') | ||
420 | putc('.', file); | ||
421 | else | ||
422 | fputs(name, file); | ||
423 | return (cp + n); | ||
424 | } | ||
425 | |||
426 | const u_char * | ||
427 | __p_cdname(cp, msg, file) | ||
428 | const u_char *cp, *msg; | ||
429 | FILE *file; | ||
430 | { | ||
431 | return (p_cdnname(cp, msg, PACKETSZ, file)); | ||
432 | } | ||
433 | |||
434 | |||
435 | /* Return a fully-qualified domain name from a compressed name (with | ||
436 | length supplied). */ | ||
437 | |||
438 | const u_char * | ||
439 | __p_fqnname(cp, msg, msglen, name, namelen) | ||
440 | const u_char *cp, *msg; | ||
441 | int msglen; | ||
442 | char *name; | ||
443 | int namelen; | ||
444 | { | ||
445 | int n, newlen; | ||
446 | |||
447 | if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) | ||
448 | return (NULL); | ||
449 | newlen = strlen(name); | ||
450 | if (newlen == 0 || name[newlen - 1] != '.') { | ||
451 | if (newlen + 1 >= namelen) /* Lack space for final dot */ | ||
452 | return (NULL); | ||
453 | else | ||
454 | strlcpy(name + newlen, ".", namelen - newlen); | ||
455 | } | ||
456 | return (cp + n); | ||
457 | } | ||
458 | |||
459 | /* XXX: the rest of these functions need to become length-limited, too. (vix) | ||
460 | */ | ||
461 | |||
462 | const u_char * | ||
463 | __p_fqname(cp, msg, file) | ||
464 | const u_char *cp, *msg; | ||
465 | FILE *file; | ||
466 | { | ||
467 | char name[MAXDNAME]; | ||
468 | const u_char *n; | ||
469 | |||
470 | n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name); | ||
471 | if (n == NULL) | ||
472 | return (NULL); | ||
473 | fputs(name, file); | ||
474 | return (n); | ||
475 | } | ||
476 | |||
477 | /* | ||
478 | * Print resource record fields in human readable form. | ||
479 | */ | ||
480 | const u_char * | ||
481 | __p_rr(cp, msg, file) | ||
482 | const u_char *cp, *msg; | ||
483 | FILE *file; | ||
484 | { | ||
485 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
486 | int type, class, dlen, n, c; | ||
487 | struct in_addr inaddr; | ||
488 | const u_char *cp1, *cp2; | ||
489 | u_int32_t tmpttl, t; | ||
490 | int lcnt; | ||
491 | u_int16_t keyflags; | ||
492 | char rrname[MAXDNAME]; /* The fqdn of this RR */ | ||
493 | char base64_key[MAX_KEY_BASE64]; | ||
494 | |||
495 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
496 | h_errno = NETDB_INTERNAL; | ||
497 | return (NULL); | ||
498 | } | ||
499 | cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname); | ||
500 | if (!cp) | ||
501 | return (NULL); /* compression error */ | ||
502 | fputs(rrname, file); | ||
503 | |||
504 | type = _getshort((u_char*)cp); | ||
505 | cp += INT16SZ; | ||
506 | class = _getshort((u_char*)cp); | ||
507 | cp += INT16SZ; | ||
508 | tmpttl = _getlong((u_char*)cp); | ||
509 | cp += INT32SZ; | ||
510 | dlen = _getshort((u_char*)cp); | ||
511 | cp += INT16SZ; | ||
512 | cp1 = cp; | ||
513 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_TTLID)) | ||
514 | fprintf(file, "\t%lu", (u_long)tmpttl); | ||
515 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_CLASS)) | ||
516 | fprintf(file, "\t%s", __p_class(class)); | ||
517 | fprintf(file, "\t%s", __p_type(type)); | ||
518 | /* | ||
519 | * Print type specific data, if appropriate | ||
520 | */ | ||
521 | switch (type) { | ||
522 | case T_A: | ||
523 | switch (class) { | ||
524 | case C_IN: | ||
525 | case C_HS: | ||
526 | bcopy(cp, (char *)&inaddr, INADDRSZ); | ||
527 | if (dlen == 4) { | ||
528 | fprintf(file, "\t%s", inet_ntoa(inaddr)); | ||
529 | cp += dlen; | ||
530 | } else if (dlen == 7) { | ||
531 | char *address; | ||
532 | u_char protocol; | ||
533 | in_port_t port; | ||
534 | |||
535 | address = inet_ntoa(inaddr); | ||
536 | cp += INADDRSZ; | ||
537 | protocol = *(u_char*)cp; | ||
538 | cp += sizeof (u_char); | ||
539 | port = _getshort((u_char*)cp); | ||
540 | cp += INT16SZ; | ||
541 | fprintf(file, "\t%s\t; proto %u, port %u", | ||
542 | address, protocol, port); | ||
543 | } | ||
544 | break; | ||
545 | default: | ||
546 | cp += dlen; | ||
547 | } | ||
548 | break; | ||
549 | case T_CNAME: | ||
550 | case T_MB: | ||
551 | case T_MG: | ||
552 | case T_MR: | ||
553 | case T_NS: | ||
554 | case T_PTR: | ||
555 | putc('\t', file); | ||
556 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
557 | return (NULL); | ||
558 | break; | ||
559 | |||
560 | case T_HINFO: | ||
561 | case T_ISDN: | ||
562 | cp2 = cp + dlen; | ||
563 | (void) fputs("\t\"", file); | ||
564 | if ((n = (unsigned char) *cp++) != 0) { | ||
565 | for (c = n; c > 0 && cp < cp2; c--) { | ||
566 | if (strchr("\n\"\\", *cp)) | ||
567 | (void) putc('\\', file); | ||
568 | (void) putc(*cp++, file); | ||
569 | } | ||
570 | } | ||
571 | putc('"', file); | ||
572 | if (cp < cp2 && (n = (unsigned char) *cp++) != 0) { | ||
573 | (void) fputs ("\t\"", file); | ||
574 | for (c = n; c > 0 && cp < cp2; c--) { | ||
575 | if (strchr("\n\"\\", *cp)) | ||
576 | (void) putc('\\', file); | ||
577 | (void) putc(*cp++, file); | ||
578 | } | ||
579 | putc('"', file); | ||
580 | } else if (type == T_HINFO) { | ||
581 | (void) fputs("\"?\"", file); | ||
582 | fprintf(file, "\n;; *** Warning *** OS-type missing"); | ||
583 | } | ||
584 | break; | ||
585 | |||
586 | case T_SOA: | ||
587 | putc('\t', file); | ||
588 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
589 | return (NULL); | ||
590 | putc(' ', file); | ||
591 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
592 | return (NULL); | ||
593 | fputs(" (\n", file); | ||
594 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
595 | fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t); | ||
596 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
597 | fprintf(file, "\t\t\t%lu\t; refresh (%s)\n", | ||
598 | (u_long)t, __p_time(t)); | ||
599 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
600 | fprintf(file, "\t\t\t%lu\t; retry (%s)\n", | ||
601 | (u_long)t, __p_time(t)); | ||
602 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
603 | fprintf(file, "\t\t\t%lu\t; expire (%s)\n", | ||
604 | (u_long)t, __p_time(t)); | ||
605 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
606 | fprintf(file, "\t\t\t%lu )\t; minimum (%s)", | ||
607 | (u_long)t, __p_time(t)); | ||
608 | break; | ||
609 | |||
610 | case T_MX: | ||
611 | case T_AFSDB: | ||
612 | case T_RT: | ||
613 | fprintf(file, "\t%u ", _getshort((u_char*)cp)); | ||
614 | cp += INT16SZ; | ||
615 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
616 | return (NULL); | ||
617 | break; | ||
618 | |||
619 | case T_PX: | ||
620 | fprintf(file, "\t%u ", _getshort((u_char*)cp)); | ||
621 | cp += INT16SZ; | ||
622 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
623 | return (NULL); | ||
624 | putc(' ', file); | ||
625 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
626 | return (NULL); | ||
627 | break; | ||
628 | |||
629 | case T_X25: | ||
630 | cp2 = cp + dlen; | ||
631 | (void) fputs("\t\"", file); | ||
632 | if ((n = (unsigned char) *cp++) != 0) { | ||
633 | for (c = n; c > 0 && cp < cp2; c--) { | ||
634 | if (strchr("\n\"\\", *cp)) | ||
635 | (void) putc('\\', file); | ||
636 | (void) putc(*cp++, file); | ||
637 | } | ||
638 | } | ||
639 | putc('"', file); | ||
640 | break; | ||
641 | |||
642 | case T_TXT: | ||
643 | (void) putc('\t', file); | ||
644 | cp2 = cp1 + dlen; | ||
645 | while (cp < cp2) { | ||
646 | putc('"', file); | ||
647 | if ((n = (unsigned char) *cp++)) { | ||
648 | for (c = n; c > 0 && cp < cp2; c--) { | ||
649 | if (strchr("\n\"\\", *cp)) | ||
650 | (void) putc('\\', file); | ||
651 | (void) putc(*cp++, file); | ||
652 | } | ||
653 | } | ||
654 | putc('"', file); | ||
655 | if (cp < cp2) | ||
656 | putc(' ', file); | ||
657 | } | ||
658 | break; | ||
659 | |||
660 | case T_NSAP: | ||
661 | (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL)); | ||
662 | cp += dlen; | ||
663 | break; | ||
664 | |||
665 | case T_AAAA: { | ||
666 | char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; | ||
667 | |||
668 | fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t)); | ||
669 | cp += dlen; | ||
670 | break; | ||
671 | } | ||
672 | |||
673 | case T_LOC: { | ||
674 | char t[255]; | ||
675 | |||
676 | fprintf(file, "\t%s", loc_ntoal(cp, t, sizeof t)); | ||
677 | cp += dlen; | ||
678 | break; | ||
679 | } | ||
680 | |||
681 | case T_NAPTR: { | ||
682 | u_int order, preference; | ||
683 | |||
684 | order = _getshort(cp); cp += INT16SZ; | ||
685 | preference = _getshort(cp); cp += INT16SZ; | ||
686 | fprintf(file, "\t%u %u ",order, preference); | ||
687 | /* Flags */ | ||
688 | n = *cp++; | ||
689 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
690 | cp += n; | ||
691 | /* Service */ | ||
692 | n = *cp++; | ||
693 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
694 | cp += n; | ||
695 | /* Regexp */ | ||
696 | n = *cp++; | ||
697 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
698 | cp += n; | ||
699 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
700 | return (NULL); | ||
701 | break; | ||
702 | } | ||
703 | |||
704 | case T_SRV: { | ||
705 | u_int priority, weight, port; | ||
706 | |||
707 | priority = _getshort(cp); cp += INT16SZ; | ||
708 | weight = _getshort(cp); cp += INT16SZ; | ||
709 | port = _getshort(cp); cp += INT16SZ; | ||
710 | fprintf(file, "\t%u %u %u ", priority, weight, port); | ||
711 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
712 | return (NULL); | ||
713 | break; | ||
714 | } | ||
715 | |||
716 | case T_MINFO: | ||
717 | case T_RP: | ||
718 | putc('\t', file); | ||
719 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
720 | return (NULL); | ||
721 | putc(' ', file); | ||
722 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
723 | return (NULL); | ||
724 | break; | ||
725 | |||
726 | case T_UINFO: | ||
727 | putc('\t', file); | ||
728 | fputs((char *)cp, file); | ||
729 | cp += dlen; | ||
730 | break; | ||
731 | |||
732 | case T_UID: | ||
733 | case T_GID: | ||
734 | if (dlen == 4) { | ||
735 | fprintf(file, "\t%u", _getlong((u_char*)cp)); | ||
736 | cp += INT32SZ; | ||
737 | } | ||
738 | break; | ||
739 | |||
740 | case T_WKS: | ||
741 | if (dlen < INT32SZ + 1) | ||
742 | break; | ||
743 | bcopy(cp, (char *)&inaddr, INADDRSZ); | ||
744 | cp += INT32SZ; | ||
745 | fprintf(file, "\t%s %s ( ", | ||
746 | inet_ntoa(inaddr), | ||
747 | deproto((int) *cp)); | ||
748 | cp += sizeof (u_char); | ||
749 | n = 0; | ||
750 | lcnt = 0; | ||
751 | while (cp < cp1 + dlen) { | ||
752 | c = *cp++; | ||
753 | do { | ||
754 | if (c & 0200) { | ||
755 | if (lcnt == 0) { | ||
756 | fputs("\n\t\t\t", file); | ||
757 | lcnt = 5; | ||
758 | } | ||
759 | fputs(dewks(n), file); | ||
760 | putc(' ', file); | ||
761 | lcnt--; | ||
762 | } | ||
763 | c <<= 1; | ||
764 | } while (++n & 07); | ||
765 | } | ||
766 | putc(')', file); | ||
767 | break; | ||
768 | |||
769 | case T_KEY: | ||
770 | putc('\t', file); | ||
771 | keyflags = _getshort(cp); | ||
772 | cp += 2; | ||
773 | fprintf(file,"0x%04x", keyflags ); /* flags */ | ||
774 | fprintf(file," %u", *cp++); /* protocol */ | ||
775 | fprintf(file," %u (", *cp++); /* algorithm */ | ||
776 | |||
777 | n = b64_ntop(cp, (cp1 + dlen) - cp, | ||
778 | base64_key, sizeof base64_key); | ||
779 | for (c = 0; c < n; ++c) { | ||
780 | if (0 == (c & 0x3F)) | ||
781 | fprintf(file, "\n\t"); | ||
782 | putc(base64_key[c], file); /* public key data */ | ||
783 | } | ||
784 | |||
785 | fprintf(file, " )"); | ||
786 | if (n < 0) | ||
787 | fprintf(file, "\t; BAD BASE64"); | ||
788 | fflush(file); | ||
789 | cp = cp1 + dlen; | ||
790 | break; | ||
791 | |||
792 | case T_SIG: | ||
793 | type = _getshort((u_char*)cp); | ||
794 | cp += INT16SZ; | ||
795 | fprintf(file, " %s", p_type(type)); | ||
796 | fprintf(file, "\t%u", *cp++); /* algorithm */ | ||
797 | /* Check label value and print error if wrong. */ | ||
798 | n = *cp++; | ||
799 | c = dn_count_labels (rrname); | ||
800 | if (n != c) | ||
801 | fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t", | ||
802 | n, c); | ||
803 | /* orig ttl */ | ||
804 | n = _getlong((u_char*)cp); | ||
805 | if (n != tmpttl) | ||
806 | fprintf(file, " %u", n); | ||
807 | cp += INT32SZ; | ||
808 | /* sig expire */ | ||
809 | fprintf(file, " (\n\t%s", | ||
810 | __p_secstodate(_getlong((u_char*)cp))); | ||
811 | cp += INT32SZ; | ||
812 | /* time signed */ | ||
813 | fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp))); | ||
814 | cp += INT32SZ; | ||
815 | /* sig footprint */ | ||
816 | fprintf(file," %u ", _getshort((u_char*)cp)); | ||
817 | cp += INT16SZ; | ||
818 | /* signer's name */ | ||
819 | cp = p_fqname(cp, msg, file); | ||
820 | n = b64_ntop(cp, (cp1 + dlen) - cp, | ||
821 | base64_key, sizeof base64_key); | ||
822 | for (c = 0; c < n; c++) { | ||
823 | if (0 == (c & 0x3F)) | ||
824 | fprintf (file, "\n\t"); | ||
825 | putc(base64_key[c], file); /* signature */ | ||
826 | } | ||
827 | /* Clean up... */ | ||
828 | fprintf(file, " )"); | ||
829 | if (n < 0) | ||
830 | fprintf(file, "\t; BAD BASE64"); | ||
831 | fflush(file); | ||
832 | cp = cp1+dlen; | ||
833 | break; | ||
834 | |||
835 | #ifdef ALLOW_T_UNSPEC | ||
836 | case T_UNSPEC: | ||
837 | { | ||
838 | int NumBytes = 8; | ||
839 | u_char *DataPtr; | ||
840 | int i; | ||
841 | |||
842 | if (dlen < NumBytes) NumBytes = dlen; | ||
843 | fprintf(file, "\tFirst %d bytes of hex data:", | ||
844 | NumBytes); | ||
845 | for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++) | ||
846 | fprintf(file, " %x", *DataPtr); | ||
847 | cp += dlen; | ||
848 | } | ||
849 | break; | ||
850 | #endif /* ALLOW_T_UNSPEC */ | ||
851 | |||
852 | default: | ||
853 | fprintf(file, "\t?%d?", type); | ||
854 | cp += dlen; | ||
855 | } | ||
856 | #if 0 | ||
857 | fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl)); | ||
858 | #else | ||
859 | putc('\n', file); | ||
860 | #endif | ||
861 | if (cp - cp1 != dlen) { | ||
862 | fprintf(file, ";; packet size error (found %ld, dlen was %d)\n", | ||
863 | (long)(cp - cp1), dlen); | ||
864 | cp = NULL; | ||
865 | } | ||
866 | return (cp); | ||
867 | } | ||
868 | |||
869 | /* | ||
870 | * Names of RR classes and qclasses. Classes and qclasses are the same, except | ||
871 | * that C_ANY is a qclass but not a class. (You can ask for records of class | ||
872 | * C_ANY, but you can't have any records of that class in the database.) | ||
873 | */ | ||
874 | const struct res_sym __p_class_syms[] = { | ||
875 | {C_IN, "IN"}, | ||
876 | {C_CHAOS, "CHAOS"}, | ||
877 | {C_HS, "HS"}, | ||
878 | {C_HS, "HESIOD"}, | ||
879 | {C_ANY, "ANY"}, | ||
880 | {C_IN, (char *)0} | ||
881 | }; | ||
882 | |||
883 | /* | ||
884 | * Names of RR types and qtypes. Types and qtypes are the same, except | ||
885 | * that T_ANY is a qtype but not a type. (You can ask for records of type | ||
886 | * T_ANY, but you can't have any records of that type in the database.) | ||
887 | */ | ||
888 | const struct res_sym __p_type_syms[] = { | ||
889 | {T_A, "A", "address"}, | ||
890 | {T_NS, "NS", "name server"}, | ||
891 | {T_MD, "MD", "mail destination (deprecated)"}, | ||
892 | {T_MF, "MF", "mail forwarder (deprecated)"}, | ||
893 | {T_CNAME, "CNAME", "canonical name"}, | ||
894 | {T_SOA, "SOA", "start of authority"}, | ||
895 | {T_MB, "MB", "mailbox"}, | ||
896 | {T_MG, "MG", "mail group member"}, | ||
897 | {T_MR, "MR", "mail rename"}, | ||
898 | {T_NULL, "NULL", "null"}, | ||
899 | {T_WKS, "WKS", "well-known service (deprecated)"}, | ||
900 | {T_PTR, "PTR", "domain name pointer"}, | ||
901 | {T_HINFO, "HINFO", "host information"}, | ||
902 | {T_MINFO, "MINFO", "mailbox information"}, | ||
903 | {T_MX, "MX", "mail exchanger"}, | ||
904 | {T_TXT, "TXT", "text"}, | ||
905 | {T_RP, "RP", "responsible person"}, | ||
906 | {T_AFSDB, "AFSDB", "DCE or AFS server"}, | ||
907 | {T_X25, "X25", "X25 address"}, | ||
908 | {T_ISDN, "ISDN", "ISDN address"}, | ||
909 | {T_RT, "RT", "router"}, | ||
910 | {T_NSAP, "NSAP", "nsap address"}, | ||
911 | {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"}, | ||
912 | {T_SIG, "SIG", "signature"}, | ||
913 | {T_KEY, "KEY", "key"}, | ||
914 | {T_PX, "PX", "mapping information"}, | ||
915 | {T_GPOS, "GPOS", "geographical position (withdrawn)"}, | ||
916 | {T_AAAA, "AAAA", "IPv6 address"}, | ||
917 | {T_LOC, "LOC", "location"}, | ||
918 | {T_NXT, "NXT", "next valid name (unimplemented)"}, | ||
919 | {T_EID, "EID", "endpoint identifier (unimplemented)"}, | ||
920 | {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"}, | ||
921 | {T_SRV, "SRV", "server selection"}, | ||
922 | {T_ATMA, "ATMA", "ATM address (unimplemented)"}, | ||
923 | {T_IXFR, "IXFR", "incremental zone transfer"}, | ||
924 | {T_AXFR, "AXFR", "zone transfer"}, | ||
925 | {T_MAILB, "MAILB", "mailbox-related data (deprecated)"}, | ||
926 | {T_MAILA, "MAILA", "mail agent (deprecated)"}, | ||
927 | {T_UINFO, "UINFO", "user information (nonstandard)"}, | ||
928 | {T_UID, "UID", "user ID (nonstandard)"}, | ||
929 | {T_GID, "GID", "group ID (nonstandard)"}, | ||
930 | {T_NAPTR, "NAPTR", "URN Naming Authority"}, | ||
931 | #ifdef ALLOW_T_UNSPEC | ||
932 | {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"}, | ||
933 | #endif /* ALLOW_T_UNSPEC */ | ||
934 | {T_ANY, "ANY", "\"any\""}, | ||
935 | {0, NULL, NULL} | ||
936 | }; | ||
937 | |||
938 | int | ||
939 | __sym_ston(syms, name, success) | ||
940 | const struct res_sym *syms; | ||
941 | char *name; | ||
942 | int *success; | ||
943 | { | ||
944 | for (; syms->name != 0; syms++) { | ||
945 | if (strcasecmp (name, syms->name) == 0) { | ||
946 | if (success) | ||
947 | *success = 1; | ||
948 | return (syms->number); | ||
949 | } | ||
950 | } | ||
951 | if (success) | ||
952 | *success = 0; | ||
953 | return (syms->number); /* The default value. */ | ||
954 | } | ||
955 | |||
956 | const char * | ||
957 | __sym_ntos(syms, number, success) | ||
958 | const struct res_sym *syms; | ||
959 | int number; | ||
960 | int *success; | ||
961 | { | ||
962 | static char unname[20]; | ||
963 | |||
964 | for (; syms->name != 0; syms++) { | ||
965 | if (number == syms->number) { | ||
966 | if (success) | ||
967 | *success = 1; | ||
968 | return (syms->name); | ||
969 | } | ||
970 | } | ||
971 | |||
972 | snprintf(unname, sizeof unname, "%d", number); | ||
973 | if (success) | ||
974 | *success = 0; | ||
975 | return (unname); | ||
976 | } | ||
977 | |||
978 | |||
979 | const char * | ||
980 | __sym_ntop(syms, number, success) | ||
981 | const struct res_sym *syms; | ||
982 | int number; | ||
983 | int *success; | ||
984 | { | ||
985 | static char unname[20]; | ||
986 | |||
987 | for (; syms->name != 0; syms++) { | ||
988 | if (number == syms->number) { | ||
989 | if (success) | ||
990 | *success = 1; | ||
991 | return (syms->humanname); | ||
992 | } | ||
993 | } | ||
994 | snprintf(unname, sizeof unname, "%d", number); | ||
995 | if (success) | ||
996 | *success = 0; | ||
997 | return (unname); | ||
998 | } | ||
999 | |||
1000 | /* | ||
1001 | * Return a string for the type | ||
1002 | */ | ||
1003 | const char * | ||
1004 | __p_type(type) | ||
1005 | int type; | ||
1006 | { | ||
1007 | return (__sym_ntos (__p_type_syms, type, (int *)0)); | ||
1008 | } | ||
1009 | |||
1010 | /* | ||
1011 | * Return a mnemonic for class | ||
1012 | */ | ||
1013 | const char * | ||
1014 | __p_class(class) | ||
1015 | int class; | ||
1016 | { | ||
1017 | return (__sym_ntos (__p_class_syms, class, (int *)0)); | ||
1018 | } | ||
1019 | |||
1020 | /* | ||
1021 | * Return a mnemonic for an option | ||
1022 | */ | ||
1023 | const char * | ||
1024 | __p_option(option) | ||
1025 | u_long option; | ||
1026 | { | ||
1027 | static char nbuf[40]; | ||
1028 | |||
1029 | switch (option) { | ||
1030 | case RES_INIT: return "init"; | ||
1031 | case RES_DEBUG: return "debug"; | ||
1032 | case RES_AAONLY: return "aaonly(unimpl)"; | ||
1033 | case RES_USEVC: return "usevc"; | ||
1034 | case RES_PRIMARY: return "primry(unimpl)"; | ||
1035 | case RES_IGNTC: return "igntc"; | ||
1036 | case RES_RECURSE: return "recurs"; | ||
1037 | case RES_DEFNAMES: return "defnam"; | ||
1038 | case RES_STAYOPEN: return "styopn"; | ||
1039 | case RES_DNSRCH: return "dnsrch"; | ||
1040 | case RES_INSECURE1: return "insecure1"; | ||
1041 | case RES_INSECURE2: return "insecure2"; | ||
1042 | case RES_USE_INET6: return "inet6"; | ||
1043 | case RES_USE_EDNS0: return "edns0"; | ||
1044 | default: | ||
1045 | snprintf(nbuf, sizeof nbuf, "?0x%lx?", (u_long)option); | ||
1046 | return (nbuf); | ||
1047 | } | ||
1048 | } | ||
1049 | |||
1050 | /* | ||
1051 | * Return a mnemonic for a time to live | ||
1052 | */ | ||
1053 | const char * | ||
1054 | p_time(value) | ||
1055 | u_int32_t value; | ||
1056 | { | ||
1057 | static char nbuf[40]; | ||
1058 | char *ebuf; | ||
1059 | int secs, mins, hours, days; | ||
1060 | register char *p; | ||
1061 | int tmp; | ||
1062 | |||
1063 | if (value == 0) { | ||
1064 | strlcpy(nbuf, "0 secs", sizeof nbuf); | ||
1065 | return (nbuf); | ||
1066 | } | ||
1067 | |||
1068 | secs = value % 60; | ||
1069 | value /= 60; | ||
1070 | mins = value % 60; | ||
1071 | value /= 60; | ||
1072 | hours = value % 24; | ||
1073 | value /= 24; | ||
1074 | days = value; | ||
1075 | value = 0; | ||
1076 | |||
1077 | #define PLURALIZE(x) x, (x == 1) ? "" : "s" | ||
1078 | p = nbuf; | ||
1079 | ebuf = nbuf + sizeof(nbuf); | ||
1080 | if (days) { | ||
1081 | if ((tmp = snprintf(p, ebuf - p, "%d day%s", | ||
1082 | PLURALIZE(days))) >= ebuf - nbuf || tmp < 0) | ||
1083 | goto full; | ||
1084 | p += tmp; | ||
1085 | } | ||
1086 | if (hours) { | ||
1087 | if (days) | ||
1088 | *p++ = ' '; | ||
1089 | if (p >= ebuf) | ||
1090 | goto full; | ||
1091 | if ((tmp = snprintf(p, ebuf - p, "%d hour%s", | ||
1092 | PLURALIZE(hours))) >= ebuf - nbuf || tmp < 0) | ||
1093 | goto full; | ||
1094 | p += tmp; | ||
1095 | } | ||
1096 | if (mins) { | ||
1097 | if (days || hours) | ||
1098 | *p++ = ' '; | ||
1099 | if (p >= ebuf) | ||
1100 | goto full; | ||
1101 | if ((tmp = snprintf(p, ebuf - p, "%d min%s", | ||
1102 | PLURALIZE(mins))) >= ebuf - nbuf || tmp < 0) | ||
1103 | goto full; | ||
1104 | p += tmp; | ||
1105 | } | ||
1106 | if (secs || ! (days || hours || mins)) { | ||
1107 | if (days || hours || mins) | ||
1108 | *p++ = ' '; | ||
1109 | if (p >= ebuf) | ||
1110 | goto full; | ||
1111 | if ((tmp = snprintf(p, ebuf - p, "%d sec%s", | ||
1112 | PLURALIZE(secs))) >= ebuf - nbuf || tmp < 0) | ||
1113 | goto full; | ||
1114 | } | ||
1115 | return (nbuf); | ||
1116 | full: | ||
1117 | p = nbuf + sizeof(nbuf) - 4; | ||
1118 | *p++ = '.'; | ||
1119 | *p++ = '.'; | ||
1120 | *p++ = '.'; | ||
1121 | *p++ = '\0'; | ||
1122 | return (nbuf); | ||
1123 | } | ||
1124 | |||
1125 | /* | ||
1126 | * routines to convert between on-the-wire RR format and zone file format. | ||
1127 | * Does not contain conversion to/from decimal degrees; divide or multiply | ||
1128 | * by 60*60*1000 for that. | ||
1129 | */ | ||
1130 | |||
1131 | static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, | ||
1132 | 1000000,10000000,100000000,1000000000}; | ||
1133 | |||
1134 | /* takes an XeY precision/size value, returns a string representation. */ | ||
1135 | static const char * | ||
1136 | precsize_ntoa(prec) | ||
1137 | u_int8_t prec; | ||
1138 | { | ||
1139 | static char retbuf[sizeof "90000000.00"]; | ||
1140 | unsigned long val; | ||
1141 | int mantissa, exponent; | ||
1142 | |||
1143 | mantissa = (int)((prec >> 4) & 0x0f) % 10; | ||
1144 | exponent = (int)((prec >> 0) & 0x0f) % 10; | ||
1145 | |||
1146 | val = mantissa * poweroften[exponent]; | ||
1147 | |||
1148 | (void) snprintf(retbuf, sizeof retbuf, "%ld.%.2ld", val/100, val%100); | ||
1149 | return (retbuf); | ||
1150 | } | ||
1151 | |||
1152 | /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ | ||
1153 | static u_int8_t | ||
1154 | precsize_aton(strptr) | ||
1155 | char **strptr; | ||
1156 | { | ||
1157 | unsigned int mval = 0, cmval = 0; | ||
1158 | u_int8_t retval = 0; | ||
1159 | register char *cp; | ||
1160 | register int exponent; | ||
1161 | register int mantissa; | ||
1162 | |||
1163 | cp = *strptr; | ||
1164 | |||
1165 | while (isdigit(*cp)) | ||
1166 | mval = mval * 10 + (*cp++ - '0'); | ||
1167 | |||
1168 | if (*cp == '.') { /* centimeters */ | ||
1169 | cp++; | ||
1170 | if (isdigit(*cp)) { | ||
1171 | cmval = (*cp++ - '0') * 10; | ||
1172 | if (isdigit(*cp)) { | ||
1173 | cmval += (*cp++ - '0'); | ||
1174 | } | ||
1175 | } | ||
1176 | } | ||
1177 | cmval = (mval * 100) + cmval; | ||
1178 | |||
1179 | for (exponent = 0; exponent < 9; exponent++) | ||
1180 | if (cmval < poweroften[exponent+1]) | ||
1181 | break; | ||
1182 | |||
1183 | mantissa = cmval / poweroften[exponent]; | ||
1184 | if (mantissa > 9) | ||
1185 | mantissa = 9; | ||
1186 | |||
1187 | retval = (mantissa << 4) | exponent; | ||
1188 | |||
1189 | *strptr = cp; | ||
1190 | |||
1191 | return (retval); | ||
1192 | } | ||
1193 | |||
1194 | /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ | ||
1195 | static u_int32_t | ||
1196 | latlon2ul(latlonstrptr,which) | ||
1197 | char **latlonstrptr; | ||
1198 | int *which; | ||
1199 | { | ||
1200 | register char *cp; | ||
1201 | u_int32_t retval; | ||
1202 | int deg = 0, min = 0, secs = 0, secsfrac = 0; | ||
1203 | |||
1204 | cp = *latlonstrptr; | ||
1205 | |||
1206 | while (isdigit(*cp)) | ||
1207 | deg = deg * 10 + (*cp++ - '0'); | ||
1208 | |||
1209 | while (isspace(*cp)) | ||
1210 | cp++; | ||
1211 | |||
1212 | if (!(isdigit(*cp))) | ||
1213 | goto fndhemi; | ||
1214 | |||
1215 | while (isdigit(*cp)) | ||
1216 | min = min * 10 + (*cp++ - '0'); | ||
1217 | |||
1218 | while (isspace(*cp)) | ||
1219 | cp++; | ||
1220 | |||
1221 | if (!(isdigit(*cp))) | ||
1222 | goto fndhemi; | ||
1223 | |||
1224 | while (isdigit(*cp)) | ||
1225 | secs = secs * 10 + (*cp++ - '0'); | ||
1226 | |||
1227 | if (*cp == '.') { /* decimal seconds */ | ||
1228 | cp++; | ||
1229 | if (isdigit(*cp)) { | ||
1230 | secsfrac = (*cp++ - '0') * 100; | ||
1231 | if (isdigit(*cp)) { | ||
1232 | secsfrac += (*cp++ - '0') * 10; | ||
1233 | if (isdigit(*cp)) { | ||
1234 | secsfrac += (*cp++ - '0'); | ||
1235 | } | ||
1236 | } | ||
1237 | } | ||
1238 | } | ||
1239 | |||
1240 | while (!isspace(*cp)) /* if any trailing garbage */ | ||
1241 | cp++; | ||
1242 | |||
1243 | while (isspace(*cp)) | ||
1244 | cp++; | ||
1245 | |||
1246 | fndhemi: | ||
1247 | switch (*cp) { | ||
1248 | case 'N': case 'n': | ||
1249 | case 'E': case 'e': | ||
1250 | retval = ((unsigned)1<<31) | ||
1251 | + (((((deg * 60) + min) * 60) + secs) * 1000) | ||
1252 | + secsfrac; | ||
1253 | break; | ||
1254 | case 'S': case 's': | ||
1255 | case 'W': case 'w': | ||
1256 | retval = ((unsigned)1<<31) | ||
1257 | - (((((deg * 60) + min) * 60) + secs) * 1000) | ||
1258 | - secsfrac; | ||
1259 | break; | ||
1260 | default: | ||
1261 | retval = 0; /* invalid value -- indicates error */ | ||
1262 | break; | ||
1263 | } | ||
1264 | |||
1265 | switch (*cp) { | ||
1266 | case 'N': case 'n': | ||
1267 | case 'S': case 's': | ||
1268 | *which = 1; /* latitude */ | ||
1269 | break; | ||
1270 | case 'E': case 'e': | ||
1271 | case 'W': case 'w': | ||
1272 | *which = 2; /* longitude */ | ||
1273 | break; | ||
1274 | default: | ||
1275 | *which = 0; /* error */ | ||
1276 | break; | ||
1277 | } | ||
1278 | |||
1279 | cp++; /* skip the hemisphere */ | ||
1280 | |||
1281 | while (!isspace(*cp)) /* if any trailing garbage */ | ||
1282 | cp++; | ||
1283 | |||
1284 | while (isspace(*cp)) /* move to next field */ | ||
1285 | cp++; | ||
1286 | |||
1287 | *latlonstrptr = cp; | ||
1288 | |||
1289 | return (retval); | ||
1290 | } | ||
1291 | |||
1292 | /* converts a zone file representation in a string to an RDATA on-the-wire | ||
1293 | * representation. */ | ||
1294 | int | ||
1295 | loc_aton(ascii, binary) | ||
1296 | const char *ascii; | ||
1297 | u_char *binary; | ||
1298 | { | ||
1299 | const char *maxcp; | ||
1300 | u_char *bcp; | ||
1301 | char *cp; | ||
1302 | |||
1303 | u_int32_t latit = 0, longit = 0, alt = 0; | ||
1304 | u_int32_t lltemp1 = 0, lltemp2 = 0; | ||
1305 | int altmeters = 0, altfrac = 0, altsign = 1; | ||
1306 | u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */ | ||
1307 | u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */ | ||
1308 | u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */ | ||
1309 | int which1 = 0, which2 = 0; | ||
1310 | |||
1311 | cp = (char *)ascii; | ||
1312 | maxcp = cp + strlen(ascii); | ||
1313 | |||
1314 | lltemp1 = latlon2ul(&cp, &which1); | ||
1315 | |||
1316 | lltemp2 = latlon2ul(&cp, &which2); | ||
1317 | |||
1318 | switch (which1 + which2) { | ||
1319 | case 3: /* 1 + 2, the only valid combination */ | ||
1320 | if ((which1 == 1) && (which2 == 2)) { /* normal case */ | ||
1321 | latit = lltemp1; | ||
1322 | longit = lltemp2; | ||
1323 | } else if ((which1 == 2) && (which2 == 1)) { /* reversed */ | ||
1324 | longit = lltemp1; | ||
1325 | latit = lltemp2; | ||
1326 | } else { /* some kind of brokenness */ | ||
1327 | return (0); | ||
1328 | } | ||
1329 | break; | ||
1330 | default: /* we didn't get one of each */ | ||
1331 | return (0); | ||
1332 | } | ||
1333 | |||
1334 | /* altitude */ | ||
1335 | if (*cp == '-') { | ||
1336 | altsign = -1; | ||
1337 | cp++; | ||
1338 | } | ||
1339 | |||
1340 | if (*cp == '+') | ||
1341 | cp++; | ||
1342 | |||
1343 | while (isdigit(*cp)) | ||
1344 | altmeters = altmeters * 10 + (*cp++ - '0'); | ||
1345 | |||
1346 | if (*cp == '.') { /* decimal meters */ | ||
1347 | cp++; | ||
1348 | if (isdigit(*cp)) { | ||
1349 | altfrac = (*cp++ - '0') * 10; | ||
1350 | if (isdigit(*cp)) { | ||
1351 | altfrac += (*cp++ - '0'); | ||
1352 | } | ||
1353 | } | ||
1354 | } | ||
1355 | |||
1356 | alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); | ||
1357 | |||
1358 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1359 | cp++; | ||
1360 | |||
1361 | while (isspace(*cp) && (cp < maxcp)) | ||
1362 | cp++; | ||
1363 | |||
1364 | if (cp >= maxcp) | ||
1365 | goto defaults; | ||
1366 | |||
1367 | siz = precsize_aton(&cp); | ||
1368 | |||
1369 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1370 | cp++; | ||
1371 | |||
1372 | while (isspace(*cp) && (cp < maxcp)) | ||
1373 | cp++; | ||
1374 | |||
1375 | if (cp >= maxcp) | ||
1376 | goto defaults; | ||
1377 | |||
1378 | hp = precsize_aton(&cp); | ||
1379 | |||
1380 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1381 | cp++; | ||
1382 | |||
1383 | while (isspace(*cp) && (cp < maxcp)) | ||
1384 | cp++; | ||
1385 | |||
1386 | if (cp >= maxcp) | ||
1387 | goto defaults; | ||
1388 | |||
1389 | vp = precsize_aton(&cp); | ||
1390 | |||
1391 | defaults: | ||
1392 | |||
1393 | bcp = binary; | ||
1394 | *bcp++ = (u_int8_t) 0; /* version byte */ | ||
1395 | *bcp++ = siz; | ||
1396 | *bcp++ = hp; | ||
1397 | *bcp++ = vp; | ||
1398 | PUTLONG(latit,bcp); | ||
1399 | PUTLONG(longit,bcp); | ||
1400 | PUTLONG(alt,bcp); | ||
1401 | |||
1402 | return (16); /* size of RR in octets */ | ||
1403 | } | ||
1404 | |||
1405 | const char * | ||
1406 | loc_ntoa(binary, ascii) | ||
1407 | const u_char *binary; | ||
1408 | char *ascii; | ||
1409 | { | ||
1410 | return loc_ntoal(binary, ascii, 255); | ||
1411 | } | ||
1412 | |||
1413 | /* takes an on-the-wire LOC RR and formats it in a human readable format. */ | ||
1414 | static const char * | ||
1415 | loc_ntoal(binary, ascii, ascii_len) | ||
1416 | const u_char *binary; | ||
1417 | char *ascii; | ||
1418 | int ascii_len; | ||
1419 | { | ||
1420 | static char *error = "?"; | ||
1421 | register const u_char *cp = binary; | ||
1422 | |||
1423 | int latdeg, latmin, latsec, latsecfrac; | ||
1424 | int longdeg, longmin, longsec, longsecfrac; | ||
1425 | char northsouth, eastwest; | ||
1426 | int altmeters, altfrac, altsign; | ||
1427 | |||
1428 | const int referencealt = 100000 * 100; | ||
1429 | |||
1430 | int32_t latval, longval, altval; | ||
1431 | u_int32_t templ; | ||
1432 | u_int8_t sizeval, hpval, vpval, versionval; | ||
1433 | |||
1434 | char *sizestr, *hpstr, *vpstr; | ||
1435 | |||
1436 | versionval = *cp++; | ||
1437 | |||
1438 | if (versionval) { | ||
1439 | snprintf(ascii, ascii_len, "; error: unknown LOC RR version"); | ||
1440 | return (ascii); | ||
1441 | } | ||
1442 | |||
1443 | sizeval = *cp++; | ||
1444 | |||
1445 | hpval = *cp++; | ||
1446 | vpval = *cp++; | ||
1447 | |||
1448 | GETLONG(templ, cp); | ||
1449 | latval = (templ - ((unsigned)1<<31)); | ||
1450 | |||
1451 | GETLONG(templ, cp); | ||
1452 | longval = (templ - ((unsigned)1<<31)); | ||
1453 | |||
1454 | GETLONG(templ, cp); | ||
1455 | if (templ < referencealt) { /* below WGS 84 spheroid */ | ||
1456 | altval = referencealt - templ; | ||
1457 | altsign = -1; | ||
1458 | } else { | ||
1459 | altval = templ - referencealt; | ||
1460 | altsign = 1; | ||
1461 | } | ||
1462 | |||
1463 | if (latval < 0) { | ||
1464 | northsouth = 'S'; | ||
1465 | latval = -latval; | ||
1466 | } else | ||
1467 | northsouth = 'N'; | ||
1468 | |||
1469 | latsecfrac = latval % 1000; | ||
1470 | latval = latval / 1000; | ||
1471 | latsec = latval % 60; | ||
1472 | latval = latval / 60; | ||
1473 | latmin = latval % 60; | ||
1474 | latval = latval / 60; | ||
1475 | latdeg = latval; | ||
1476 | |||
1477 | if (longval < 0) { | ||
1478 | eastwest = 'W'; | ||
1479 | longval = -longval; | ||
1480 | } else | ||
1481 | eastwest = 'E'; | ||
1482 | |||
1483 | longsecfrac = longval % 1000; | ||
1484 | longval = longval / 1000; | ||
1485 | longsec = longval % 60; | ||
1486 | longval = longval / 60; | ||
1487 | longmin = longval % 60; | ||
1488 | longval = longval / 60; | ||
1489 | longdeg = longval; | ||
1490 | |||
1491 | altfrac = altval % 100; | ||
1492 | altmeters = (altval / 100) * altsign; | ||
1493 | |||
1494 | if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL) | ||
1495 | sizestr = error; | ||
1496 | if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL) | ||
1497 | hpstr = error; | ||
1498 | if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL) | ||
1499 | vpstr = error; | ||
1500 | |||
1501 | snprintf(ascii, ascii_len, | ||
1502 | "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm", | ||
1503 | latdeg, latmin, latsec, latsecfrac, northsouth, | ||
1504 | longdeg, longmin, longsec, longsecfrac, eastwest, | ||
1505 | altmeters, altfrac, sizestr, hpstr, vpstr); | ||
1506 | |||
1507 | if (sizestr != error) | ||
1508 | free(sizestr); | ||
1509 | if (hpstr != error) | ||
1510 | free(hpstr); | ||
1511 | if (vpstr != error) | ||
1512 | free(vpstr); | ||
1513 | |||
1514 | return (ascii); | ||
1515 | } | ||
1516 | |||
1517 | |||
1518 | /* Return the number of DNS hierarchy levels in the name. */ | ||
1519 | int | ||
1520 | __dn_count_labels(name) | ||
1521 | char *name; | ||
1522 | { | ||
1523 | int i, len, count; | ||
1524 | |||
1525 | len = strlen(name); | ||
1526 | |||
1527 | for(i = 0, count = 0; i < len; i++) { | ||
1528 | if (name[i] == '.') | ||
1529 | count++; | ||
1530 | } | ||
1531 | |||
1532 | /* don't count initial wildcard */ | ||
1533 | if (name[0] == '*') | ||
1534 | if (count) | ||
1535 | count--; | ||
1536 | |||
1537 | /* don't count the null label for root. */ | ||
1538 | /* if terminating '.' not found, must adjust */ | ||
1539 | /* count to include last label */ | ||
1540 | if (len > 0 && name[len-1] != '.') | ||
1541 | count++; | ||
1542 | return (count); | ||
1543 | } | ||
1544 | |||
1545 | |||
1546 | /* | ||
1547 | * Make dates expressed in seconds-since-Jan-1-1970 easy to read. | ||
1548 | * SIG records are required to be printed like this, by the Secure DNS RFC. | ||
1549 | */ | ||
1550 | char * | ||
1551 | __p_secstodate (secs) | ||
1552 | unsigned long secs; | ||
1553 | { | ||
1554 | static char output[15]; /* YYYYMMDDHHMMSS and null */ | ||
1555 | time_t clock = secs; | ||
1556 | struct tm *time; | ||
1557 | |||
1558 | time = gmtime(&clock); | ||
1559 | time->tm_year += 1900; | ||
1560 | time->tm_mon += 1; | ||
1561 | snprintf(output, sizeof output, "%04d%02d%02d%02d%02d%02d", | ||
1562 | time->tm_year, time->tm_mon, time->tm_mday, | ||
1563 | time->tm_hour, time->tm_min, time->tm_sec); | ||
1564 | return (output); | ||
1565 | } | ||