4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Gary Mills
24 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <arpa/nameser.h>
51 void fp_query(char *msg
, FILE *file
);
53 char *_res_opcodes
[] = {
72 char *_res_resultcodes
[] = {
95 fp_query(msg
, stdout
);
99 * Print the contents of a query.
100 * This is intended to be primarily a debugging routine.
112 * Print header fields.
115 cp
= msg
+ sizeof (HEADER
);
116 fprintf(file
, "HEADER:\n");
117 fprintf(file
, "\topcode = %s", _res_opcodes
[hp
->opcode
]);
118 fprintf(file
, ", id = %d", ntohs(hp
->id
));
119 fprintf(file
, ", rcode = %s\n", _res_resultcodes
[hp
->rcode
]);
120 fprintf(file
, "\theader flags: ");
122 fprintf(file
, " qr");
124 fprintf(file
, " aa");
126 fprintf(file
, " tc");
128 fprintf(file
, " rd");
130 fprintf(file
, " ra");
132 fprintf(file
, " pr");
133 fprintf(file
, "\n\tqdcount = %d", ntohs(hp
->qdcount
));
134 fprintf(file
, ", ancount = %d", ntohs(hp
->ancount
));
135 fprintf(file
, ", nscount = %d", ntohs(hp
->nscount
));
136 fprintf(file
, ", arcount = %d\n\n", ntohs(hp
->arcount
));
138 * Print question records.
140 if (n
= ntohs(hp
->qdcount
)) {
141 fprintf(file
, "QUESTIONS:\n");
144 cp
= p_cdname(cp
, msg
, file
);
147 fprintf(file
, ", type = %s", p_type(_getshort(cp
)));
148 cp
+= sizeof (u_short
);
149 fprintf(file
, ", class = %s\n\n",
150 p_class(_getshort(cp
)));
151 cp
+= sizeof (u_short
);
155 * Print authoritative answer records
157 if (n
= ntohs(hp
->ancount
)) {
158 fprintf(file
, "ANSWERS:\n");
161 cp
= p_rr(cp
, msg
, file
);
167 * print name server records
169 if (n
= ntohs(hp
->nscount
)) {
170 fprintf(file
, "NAME SERVERS:\n");
173 cp
= p_rr(cp
, msg
, file
);
179 * print additional records
181 if (n
= ntohs(hp
->arcount
)) {
182 fprintf(file
, "ADDITIONAL RECORDS:\n");
185 cp
= p_rr(cp
, msg
, file
);
193 p_cdname(cp
, msg
, file
)
200 if ((n
= dn_expand((u_char
*)msg
, (u_char
*)(msg
+ 512), (u_char
*)cp
,
201 (u_char
*)name
, sizeof (name
))) < 0)
203 if (name
[0] == '\0') {
212 * Print resource record fields in human readable form.
219 int type
, class, dlen
, n
, c
;
220 struct in_addr inaddr
;
223 if ((cp
= p_cdname(cp
, msg
, file
)) == NULL
)
224 return (NULL
); /* compression error */
225 fprintf(file
, "\n\ttype = %s", p_type(type
= _getshort(cp
)));
226 cp
+= sizeof (u_short
);
227 fprintf(file
, ", class = %s", p_class(class = _getshort(cp
)));
228 cp
+= sizeof (u_short
);
229 fprintf(file
, ", ttl = %s", p_time(_getlong(cp
)));
230 cp
+= sizeof (u_long
);
231 fprintf(file
, ", dlen = %d\n", dlen
= _getshort(cp
));
232 cp
+= sizeof (u_short
);
235 * Print type specific data, if appropriate
243 memcpy((void *)&inaddr
, (void *)cp
, sizeof (inaddr
));
245 bcopy(cp
, (char *)&inaddr
, sizeof (inaddr
));
248 fprintf(file
, "\tinternet address = %s\n",
251 } else if (dlen
== 7) {
252 fprintf(file
, "\tinternet address = %s",
254 fprintf(file
, ", protocol = %d", cp
[4]);
255 fprintf(file
, ", port = %d\n",
256 (cp
[5] << 8) + cp
[6]);
270 fprintf(file
, "\tdomain name = ");
271 cp
= p_cdname(cp
, msg
, file
);
277 fprintf(file
, "\tCPU=%.*s\n", n
, cp
);
281 fprintf(file
, "\tOS=%.*s\n", n
, cp
);
287 fprintf(file
, "\torigin = ");
288 cp
= p_cdname(cp
, msg
, file
);
289 fprintf(file
, "\n\tmail addr = ");
290 cp
= p_cdname(cp
, msg
, file
);
291 fprintf(file
, "\n\tserial = %ld", _getlong(cp
));
292 cp
+= sizeof (u_long
);
293 fprintf(file
, "\n\trefresh = %s", p_time(_getlong(cp
)));
294 cp
+= sizeof (u_long
);
295 fprintf(file
, "\n\tretry = %s", p_time(_getlong(cp
)));
296 cp
+= sizeof (u_long
);
297 fprintf(file
, "\n\texpire = %s", p_time(_getlong(cp
)));
298 cp
+= sizeof (u_long
);
299 fprintf(file
, "\n\tmin = %s\n", p_time(_getlong(cp
)));
300 cp
+= sizeof (u_long
);
304 fprintf(file
, "\tpreference = %ld,", _getshort(cp
));
305 cp
+= sizeof (u_short
);
306 fprintf(file
, " name = ");
307 cp
= p_cdname(cp
, msg
, file
);
311 (void) fputs("\t\"", file
);
314 if (n
= (unsigned char) *cp
++) {
315 for (c
= n
; c
> 0 && cp
< cp2
; c
--)
317 (void) putc('\\', file
);
318 (void) putc(*cp
++, file
);
320 (void) putc(*cp
++, file
);
323 (void) fputs("\"\n", file
);
327 fprintf(file
, "\trequests = ");
328 cp
= p_cdname(cp
, msg
, file
);
329 fprintf(file
, "\n\terrors = ");
330 cp
= p_cdname(cp
, msg
, file
);
334 fprintf(file
, "\t%s\n", cp
);
341 fprintf(file
, "\t%ld\n", _getlong(cp
));
347 if (dlen
< sizeof (u_long
) + 1)
350 memcpy((void *)&inaddr
, (void *)cp
, sizeof (inaddr
));
352 bcopy(cp
, (char *)&inaddr
, sizeof (inaddr
));
354 cp
+= sizeof (u_long
);
355 fprintf(file
, "\tinternet address = %s, protocol = %d\n\t",
356 inet_ntoa(inaddr
), *cp
++);
358 while (cp
< cp1
+ dlen
) {
362 fprintf(file
, " %d", n
);
369 #ifdef ALLOW_T_UNSPEC
376 if (dlen
< NumBytes
) NumBytes
= dlen
;
377 fprintf(file
, "\tFirst %d bytes of hex data:",
379 for (i
= 0, DataPtr
= cp
; i
< NumBytes
; i
++, DataPtr
++)
380 fprintf(file
, " %x", *DataPtr
);
385 #endif /* ALLOW_T_UNSPEC */
388 fprintf(file
, "\t???\n");
391 if (cp
!= cp1
+ dlen
) {
392 fprintf(file
, "packet size error (%#x != %#x)\n", cp
, cp1
+dlen
);
399 static char nbuf
[40];
402 * Return a string for the type
411 case T_NS
: /* authoritative server */
413 case T_CNAME
: /* canonical name */
415 case T_SOA
: /* start of authority zone */
417 case T_MB
: /* mailbox domain name */
419 case T_MG
: /* mail group member */
421 case T_MR
: /* mail rename name */
423 case T_NULL
: /* null resource record */
425 case T_WKS
: /* well known service */
427 case T_PTR
: /* domain name pointer */
429 case T_HINFO
: /* host information */
431 case T_MINFO
: /* mailbox information */
433 case T_MX
: /* mail routing info */
435 case T_TXT
: /* text */
437 case T_AXFR
: /* zone transfer */
439 case T_MAILB
: /* mail box */
441 case T_MAILA
: /* mail address */
443 case T_ANY
: /* matches any type */
451 #ifdef ALLOW_T_UNSPEC
454 #endif /* ALLOW_T_UNSPEC */
456 (void) sprintf(nbuf
, "%d", type
);
462 * Return a mnemonic for class
470 case C_IN
: /* internet class */
472 case C_HS
: /* hesiod class */
474 case C_ANY
: /* matches any class */
477 (void) sprintf(nbuf
, "%d", class);
483 * Return a mnemonic for a time to live
489 int secs
, mins
, hours
;
493 strcpy(nbuf
, "0 secs");
504 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
507 (void) sprintf(p
, "%d day%s", PLURALIZE(value
));
513 (void) sprintf(p
, "%d hour%s", PLURALIZE(hours
));
519 (void) sprintf(p
, "%d min%s", PLURALIZE(mins
));
522 if (secs
|| ! (value
|| hours
|| mins
)) {
523 if (value
|| hours
|| mins
)
525 (void) sprintf(p
, "%d sec%s", PLURALIZE(secs
));