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