Add code in mountnfs() and nfs_unmount() to hold and release respectively,
[unix-history] / usr / src / lib / libc / net / res_debug.c
CommitLineData
b423e985 1/*
8ea4199d 2 * Copyright (c) 1985 Regents of the University of California.
6b2f9dd0
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
f4f66d2c
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
b423e985
RC
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
90e07ed6 19static char sccsid[] = "@(#)res_debug.c 5.27 (Berkeley) %G%";
6b2f9dd0 20#endif /* LIBC_SCCS and not lint */
8ea4199d 21
0d22afd1
RC
22#include <sys/types.h>
23#include <netinet/in.h>
24#include <stdio.h>
02a51825 25#include <arpa/nameser.h>
0d22afd1 26
7134abf2 27extern char *p_cdname(), *p_rr(), *p_type(), *p_class(), *p_time();
0d22afd1
RC
28extern char *inet_ntoa();
29
cd43bd6a 30char *_res_opcodes[] = {
0d22afd1
RC
31 "QUERY",
32 "IQUERY",
33 "CQUERYM",
34 "CQUERYU",
35 "4",
36 "5",
37 "6",
38 "7",
39 "8",
0d22afd1
RC
40 "UPDATEA",
41 "UPDATED",
3e8361d9 42 "UPDATEDA",
0d22afd1 43 "UPDATEM",
3e8361d9 44 "UPDATEMA",
0d22afd1
RC
45 "ZONEINIT",
46 "ZONEREF",
47};
48
cd43bd6a 49char *_res_resultcodes[] = {
0d22afd1
RC
50 "NOERROR",
51 "FORMERR",
52 "SERVFAIL",
53 "NXDOMAIN",
54 "NOTIMP",
55 "REFUSED",
56 "6",
57 "7",
58 "8",
59 "9",
60 "10",
61 "11",
62 "12",
63 "13",
64 "14",
65 "NOCHANGE",
66};
67
a73d974c
KD
68p_query(msg)
69 char *msg;
70{
71 fp_query(msg,stdout);
72}
73
0d22afd1
RC
74/*
75 * Print the contents of a query.
76 * This is intended to be primarily a debugging routine.
77 */
a73d974c 78fp_query(msg,file)
e515a559 79 char *msg;
a73d974c 80 FILE *file;
0d22afd1
RC
81{
82 register char *cp;
83 register HEADER *hp;
84 register int n;
85
86 /*
87 * Print header fields.
88 */
e515a559
RC
89 hp = (HEADER *)msg;
90 cp = msg + sizeof(HEADER);
a73d974c 91 fprintf(file,"HEADER:\n");
d7070d01 92 fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
a73d974c 93 fprintf(file,", id = %d", ntohs(hp->id));
d7070d01 94 fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
a73d974c 95 fprintf(file,"\theader flags: ");
0d22afd1 96 if (hp->qr)
a73d974c 97 fprintf(file," qr");
0d22afd1 98 if (hp->aa)
a73d974c 99 fprintf(file," aa");
0d22afd1 100 if (hp->tc)
a73d974c 101 fprintf(file," tc");
0d22afd1 102 if (hp->rd)
a73d974c 103 fprintf(file," rd");
0d22afd1 104 if (hp->ra)
a73d974c 105 fprintf(file," ra");
0d22afd1 106 if (hp->pr)
a73d974c
KD
107 fprintf(file," pr");
108 fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
109 fprintf(file,", ancount = %d", ntohs(hp->ancount));
110 fprintf(file,", nscount = %d", ntohs(hp->nscount));
111 fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
0d22afd1
RC
112 /*
113 * Print question records.
114 */
115 if (n = ntohs(hp->qdcount)) {
a73d974c 116 fprintf(file,"QUESTIONS:\n");
0d22afd1 117 while (--n >= 0) {
a73d974c
KD
118 fprintf(file,"\t");
119 cp = p_cdname(cp, msg, file);
0d22afd1
RC
120 if (cp == NULL)
121 return;
20087cad 122 fprintf(file,", type = %s", p_type(_getshort(cp)));
0d22afd1 123 cp += sizeof(u_short);
20087cad 124 fprintf(file,", class = %s\n\n", p_class(_getshort(cp)));
0d22afd1
RC
125 cp += sizeof(u_short);
126 }
127 }
128 /*
129 * Print authoritative answer records
130 */
131 if (n = ntohs(hp->ancount)) {
a73d974c 132 fprintf(file,"ANSWERS:\n");
0d22afd1 133 while (--n >= 0) {
a73d974c
KD
134 fprintf(file,"\t");
135 cp = p_rr(cp, msg, file);
0d22afd1
RC
136 if (cp == NULL)
137 return;
138 }
139 }
140 /*
141 * print name server records
142 */
143 if (n = ntohs(hp->nscount)) {
a73d974c 144 fprintf(file,"NAME SERVERS:\n");
0d22afd1 145 while (--n >= 0) {
a73d974c
KD
146 fprintf(file,"\t");
147 cp = p_rr(cp, msg, file);
0d22afd1
RC
148 if (cp == NULL)
149 return;
150 }
151 }
152 /*
153 * print additional records
154 */
155 if (n = ntohs(hp->arcount)) {
a73d974c 156 fprintf(file,"ADDITIONAL RECORDS:\n");
0d22afd1 157 while (--n >= 0) {
a73d974c
KD
158 fprintf(file,"\t");
159 cp = p_rr(cp, msg, file);
0d22afd1
RC
160 if (cp == NULL)
161 return;
162 }
163 }
164}
165
166char *
a73d974c 167p_cdname(cp, msg, file)
e515a559 168 char *cp, *msg;
a73d974c 169 FILE *file;
0d22afd1
RC
170{
171 char name[MAXDNAME];
172 int n;
173
127b68cb 174 if ((n = dn_expand(msg, msg + 512, cp, name, sizeof(name))) < 0)
0d22afd1
RC
175 return (NULL);
176 if (name[0] == '\0') {
177 name[0] = '.';
178 name[1] = '\0';
179 }
a73d974c 180 fputs(name, file);
0d22afd1
RC
181 return (cp + n);
182}
183
184/*
185 * Print resource record fields in human readable form.
186 */
187char *
a73d974c 188p_rr(cp, msg, file)
e515a559 189 char *cp, *msg;
a73d974c 190 FILE *file;
0d22afd1
RC
191{
192 int type, class, dlen, n, c;
193 struct in_addr inaddr;
194 char *cp1;
195
a73d974c 196 if ((cp = p_cdname(cp, msg, file)) == NULL)
0d22afd1 197 return (NULL); /* compression error */
20087cad 198 fprintf(file,"\n\ttype = %s", p_type(type = _getshort(cp)));
0d22afd1 199 cp += sizeof(u_short);
20087cad 200 fprintf(file,", class = %s", p_class(class = _getshort(cp)));
0d22afd1 201 cp += sizeof(u_short);
606dc84e 202 fprintf(file,", ttl = %s", p_time(_getlong(cp)));
0d22afd1 203 cp += sizeof(u_long);
20087cad 204 fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
0d22afd1
RC
205 cp += sizeof(u_short);
206 cp1 = cp;
207 /*
208 * Print type specific data, if appropriate
209 */
210 switch (type) {
211 case T_A:
212 switch (class) {
213 case C_IN:
11e57e73 214 bcopy(cp, (char *)&inaddr, sizeof(inaddr));
0d22afd1 215 if (dlen == 4) {
a73d974c 216 fprintf(file,"\tinternet address = %s\n",
0d22afd1
RC
217 inet_ntoa(inaddr));
218 cp += dlen;
219 } else if (dlen == 7) {
a73d974c 220 fprintf(file,"\tinternet address = %s",
0d22afd1 221 inet_ntoa(inaddr));
a73d974c
KD
222 fprintf(file,", protocol = %d", cp[4]);
223 fprintf(file,", port = %d\n",
0d22afd1
RC
224 (cp[5] << 8) + cp[6]);
225 cp += dlen;
226 }
227 break;
e0dd8906
KD
228 default:
229 cp += dlen;
0d22afd1
RC
230 }
231 break;
232 case T_CNAME:
233 case T_MB:
0d22afd1
RC
234 case T_MG:
235 case T_MR:
236 case T_NS:
237 case T_PTR:
a73d974c
KD
238 fprintf(file,"\tdomain name = ");
239 cp = p_cdname(cp, msg, file);
240 fprintf(file,"\n");
0d22afd1
RC
241 break;
242
243 case T_HINFO:
244 if (n = *cp++) {
a73d974c 245 fprintf(file,"\tCPU=%.*s\n", n, cp);
0d22afd1
RC
246 cp += n;
247 }
248 if (n = *cp++) {
a73d974c 249 fprintf(file,"\tOS=%.*s\n", n, cp);
0d22afd1
RC
250 cp += n;
251 }
252 break;
253
254 case T_SOA:
a73d974c
KD
255 fprintf(file,"\torigin = ");
256 cp = p_cdname(cp, msg, file);
257 fprintf(file,"\n\tmail addr = ");
258 cp = p_cdname(cp, msg, file);
606dc84e 259 fprintf(file,"\n\tserial = %ld", _getlong(cp));
0d22afd1 260 cp += sizeof(u_long);
606dc84e 261 fprintf(file,"\n\trefresh = %s", p_time(_getlong(cp)));
0d22afd1 262 cp += sizeof(u_long);
606dc84e 263 fprintf(file,"\n\tretry = %s", p_time(_getlong(cp)));
0d22afd1 264 cp += sizeof(u_long);
606dc84e 265 fprintf(file,"\n\texpire = %s", p_time(_getlong(cp)));
0d22afd1 266 cp += sizeof(u_long);
606dc84e 267 fprintf(file,"\n\tmin = %s\n", p_time(_getlong(cp)));
0d22afd1
RC
268 cp += sizeof(u_long);
269 break;
270
73546a32 271 case T_MX:
20087cad 272 fprintf(file,"\tpreference = %ld,",_getshort(cp));
018df4a9
KD
273 cp += sizeof(u_short);
274 fprintf(file," name = ");
73546a32 275 cp = p_cdname(cp, msg, file);
73546a32
KD
276 break;
277
0af159f3
JB
278 case T_TXT:
279 if (n = *cp++) {
280 fprintf(file, "\tTEXT=%.*s\n", n, cp);
281 cp += n;
282 } else
283 fprintf(file, "\tTEXT=\"\"\n");
284 break;
285
e515a559 286 case T_MINFO:
a73d974c
KD
287 fprintf(file,"\trequests = ");
288 cp = p_cdname(cp, msg, file);
289 fprintf(file,"\n\terrors = ");
290 cp = p_cdname(cp, msg, file);
e515a559
RC
291 break;
292
0d22afd1 293 case T_UINFO:
a73d974c 294 fprintf(file,"\t%s\n", cp);
0d22afd1
RC
295 cp += dlen;
296 break;
297
298 case T_UID:
299 case T_GID:
300 if (dlen == 4) {
20087cad 301 fprintf(file,"\t%ld\n", _getlong(cp));
0d22afd1
RC
302 cp += sizeof(int);
303 }
304 break;
305
306 case T_WKS:
307 if (dlen < sizeof(u_long) + 1)
308 break;
11e57e73 309 bcopy(cp, (char *)&inaddr, sizeof(inaddr));
0d22afd1 310 cp += sizeof(u_long);
a73d974c 311 fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
0d22afd1
RC
312 inet_ntoa(inaddr), *cp++);
313 n = 0;
314 while (cp < cp1 + dlen) {
315 c = *cp++;
316 do {
a943feed 317 if (c & 0200)
a73d974c 318 fprintf(file," %d", n);
a943feed 319 c <<= 1;
0d22afd1
RC
320 } while (++n & 07);
321 }
a73d974c 322 putc('\n',file);
0d22afd1
RC
323 break;
324
3bf25746 325#ifdef ALLOW_T_UNSPEC
1e12917d
KB
326 case T_UNSPEC:
327 {
328 int NumBytes = 8;
329 char *DataPtr;
330 int i;
3bf25746 331
1e12917d
KB
332 if (dlen < NumBytes) NumBytes = dlen;
333 fprintf(file, "\tFirst %d bytes of hex data:",
334 NumBytes);
335 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
336 fprintf(file, " %x", *DataPtr);
337 fputs("\n", file);
338 cp += dlen;
339 }
340 break;
341#endif /* ALLOW_T_UNSPEC */
3bf25746 342
0d22afd1 343 default:
a73d974c 344 fprintf(file,"\t???\n");
0d22afd1
RC
345 cp += dlen;
346 }
90e07ed6 347 if (cp != cp1 + dlen) {
a73d974c 348 fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
90e07ed6
JB
349 cp = NULL;
350 }
a73d974c 351 fprintf(file,"\n");
0d22afd1
RC
352 return (cp);
353}
354
7134abf2 355static char nbuf[40];
0d22afd1
RC
356
357/*
358 * Return a string for the type
359 */
360char *
361p_type(type)
362 int type;
363{
0d22afd1
RC
364 switch (type) {
365 case T_A:
366 return("A");
367 case T_NS: /* authoritative server */
368 return("NS");
0af159f3 369 case T_CNAME: /* canonical name */
0d22afd1
RC
370 return("CNAME");
371 case T_SOA: /* start of authority zone */
372 return("SOA");
373 case T_MB: /* mailbox domain name */
374 return("MB");
375 case T_MG: /* mail group member */
376 return("MG");
377 case T_MR: /* mail rename name */
378 return("MR");
379 case T_NULL: /* null resource record */
380 return("NULL");
381 case T_WKS: /* well known service */
382 return("WKS");
383 case T_PTR: /* domain name pointer */
384 return("PTR");
385 case T_HINFO: /* host information */
386 return("HINFO");
387 case T_MINFO: /* mailbox information */
388 return("MINFO");
606dc84e
JB
389 case T_MX: /* mail routing info */
390 return("MX");
391 case T_TXT: /* text */
392 return("TXT");
0d22afd1
RC
393 case T_AXFR: /* zone transfer */
394 return("AXFR");
395 case T_MAILB: /* mail box */
396 return("MAILB");
397 case T_MAILA: /* mail address */
398 return("MAILA");
399 case T_ANY: /* matches any type */
400 return("ANY");
401 case T_UINFO:
402 return("UINFO");
403 case T_UID:
404 return("UID");
405 case T_GID:
406 return("GID");
3bf25746 407#ifdef ALLOW_T_UNSPEC
1e12917d
KB
408 case T_UNSPEC:
409 return("UNSPEC");
410#endif /* ALLOW_T_UNSPEC */
0d22afd1 411 default:
06eef7c4
KB
412 (void)sprintf(nbuf, "%d", type);
413 return(nbuf);
0d22afd1
RC
414 }
415}
416
417/*
418 * Return a mnemonic for class
419 */
420char *
421p_class(class)
422 int class;
423{
424
425 switch (class) {
426 case C_IN: /* internet class */
427 return("IN");
0af159f3
JB
428 case C_HS: /* hesiod class */
429 return("HS");
0d22afd1
RC
430 case C_ANY: /* matches any class */
431 return("ANY");
432 default:
06eef7c4
KB
433 (void)sprintf(nbuf, "%d", class);
434 return(nbuf);
0d22afd1
RC
435 }
436}
7134abf2
MK
437
438/*
439 * Return a mnemonic for a time to live
440 */
0af159f3
JB
441char *
442p_time(value)
7134abf2
MK
443 u_long value;
444{
445 int secs, mins, hours;
446 register char *p;
447
448 secs = value % 60;
449 value /= 60;
450 mins = value % 60;
451 value /= 60;
452 hours = value % 24;
453 value /= 24;
454
455#define PLURALIZE(x) x, (x == 1) ? "" : "s"
456 p = nbuf;
457 if (value) {
458 (void)sprintf(p, "%d day%s", PLURALIZE(value));
459 while (*++p);
460 }
461 if (hours) {
0af159f3 462 if (value)
7134abf2
MK
463 *p++ = ' ';
464 (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
465 while (*++p);
466 }
467 if (mins) {
0af159f3 468 if (value || hours)
7134abf2
MK
469 *p++ = ' ';
470 (void)sprintf(p, "%d min%s", PLURALIZE(mins));
471 while (*++p);
472 }
0af159f3
JB
473 if (secs || ! (value || hours || mins)) {
474 if (value || hours || mins)
7134abf2
MK
475 *p++ = ' ';
476 (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
7134abf2
MK
477 }
478 return(nbuf);
479}