tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / res_debug.c
blobfc49630c6d33e069d64a25a8d7cbe8893bc22bb9
1 /*-
2 * Copyright (c) 1985, 1990 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (C) 2005 - 2007 The AROS Dev Team
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)res_debug.c 5.36 (Berkeley) 3/6/91
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)res_debug.c 5.36 (Berkeley) 3/6/91";
39 #endif /* LIBC_SCCS and not lint */
41 #ifdef RES_DEBUG
43 #include <conf.h>
45 #include <sys/param.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 //#include <api/arpa_nameser.h>
49 #include <api/apicalls.h>
50 #include <api/resolv.h>
51 #include <stdio.h>
52 /* #include <string.h> */
54 void __fp_query();
55 char *__p_class(), *__p_time(), *__p_type();
56 static char *p_cdname(), *p_rr();
58 char *_res_opcodes[] = {
59 "QUERY",
60 "IQUERY",
61 "CQUERYM",
62 "CQUERYU",
63 "4",
64 "5",
65 "6",
66 "7",
67 "8",
68 "UPDATEA",
69 "UPDATED",
70 "UPDATEDA",
71 "UPDATEM",
72 "UPDATEMA",
73 "ZONEINIT",
74 "ZONEREF",
77 char *_res_resultcodes[] = {
78 "NOERROR",
79 "FORMERR",
80 "SERVFAIL",
81 "NXDOMAIN",
82 "NOTIMP",
83 "REFUSED",
84 "6",
85 "7",
86 "8",
87 "9",
88 "10",
89 "11",
90 "12",
91 "13",
92 "14",
93 "NOCHANGE",
96 __p_query(msg, libPtr)
97 char *msg;
98 struct SocketBase *libPtr;
100 __fp_query(msg,libPtr);
104 * Print the contents of a query.
105 * This is intended to be primarily a debugging routine.
107 void
108 __fp_query(msg,libPtr)
109 char *msg;
110 struct SocketBase *libPtr;
112 register char *cp;
113 register HEADER *hp;
114 register int n;
117 * Print header fields.
119 hp = (HEADER *)msg;
120 cp = msg + sizeof(HEADER);
121 Printf("HEADER:\n");
122 Printf("\topcode = %s", _res_opcodes[hp->opcode]);
123 Printf(", id = %ld", ntohs(hp->id));
124 Printf(", rcode = %s\n", _res_resultcodes[hp->rcode]);
125 Printf("\theader flags: ");
126 if (hp->qr)
127 Printf(" qr");
128 if (hp->aa)
129 Printf(" aa");
130 if (hp->tc)
131 Printf(" tc");
132 if (hp->rd)
133 Printf(" rd");
134 if (hp->ra)
135 Printf(" ra");
136 if (hp->pr)
137 Printf(" pr");
138 Printf("\n\tqdcount = %ld", ntohs(hp->qdcount));
139 Printf(", ancount = %ld", ntohs(hp->ancount));
140 Printf(", nscount = %ld", ntohs(hp->nscount));
141 Printf(", arcount = %ld\n\n", ntohs(hp->arcount));
143 * Print question records.
145 if (n = ntohs(hp->qdcount)) {
146 Printf("QUESTIONS:\n");
147 while (--n >= 0) {
148 Printf("\t");
149 cp = p_cdname(cp, msg);
150 if (cp == NULL)
151 return;
152 Printf(", type = %s", __p_type(_getshort(cp)));
153 cp += sizeof(u_short);
154 Printf(
155 ", class = %s\n\n", __p_class(_getshort(cp)));
156 cp += sizeof(u_short);
160 * Print authoritative answer records
162 if (n = ntohs(hp->ancount)) {
163 Printf("ANSWERS:\n");
164 while (--n >= 0) {
165 Printf("\t");
166 cp = p_rr(cp, msg, libPtr);
167 if (cp == NULL)
168 return;
172 * print name server records
174 if (n = ntohs(hp->nscount)) {
175 Printf("NAME SERVERS:\n");
176 while (--n >= 0) {
177 Printf("\t");
178 cp = p_rr(cp, msg, libPtr);
179 if (cp == NULL)
180 return;
184 * print additional records
186 if (n = ntohs(hp->arcount)) {
187 Printf("ADDITIONAL RECORDS:\n");
188 while (--n >= 0) {
189 Printf("\t");
190 cp = p_rr(cp, msg, libPtr);
191 if (cp == NULL)
192 return;
197 static char *
198 p_cdname(cp, msg)
199 char *cp, *msg;
201 char name[MAXDNAME];
202 int n;
204 if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
205 (u_char *)name, sizeof(name))) < 0)
206 return (NULL);
207 if (name[0] == '\0') {
208 name[0] = '.';
209 name[1] = '\0';
211 PutStr(name);
212 return (cp + n);
216 * Print resource record fields in human readable form.
218 static char *
219 p_rr(cp, msg, libPtr)
220 char *cp, *msg;
221 struct SocketBase *libPtr;
223 int type, class, dlen, n, c;
224 struct in_addr inaddr;
225 char *cp1, *cp2;
227 if ((cp = p_cdname(cp, msg)) == NULL)
228 return (NULL); /* compression error */
229 Printf("\n\ttype = %s", __p_type(type = _getshort(cp)));
230 cp += sizeof(u_short);
231 Printf(", class = %s", __p_class(class = _getshort(cp)));
232 cp += sizeof(u_short);
233 Printf(", ttl = %s", __p_time(_getlong(cp)));
234 cp += sizeof(u_long);
235 Printf(", dlen = %d\n", dlen = _getshort(cp));
236 cp += sizeof(u_short);
237 cp1 = cp;
239 * Print type specific data, if appropriate
241 switch (type) {
242 case T_A:
243 switch (class) {
244 case C_IN:
245 case C_HS:
246 bcopy(cp, (char *)&inaddr, sizeof(inaddr));
247 if (dlen == 4) {
248 Printf("\tinternet address = %s\n",
249 __Inet_NtoA(inaddr.s_addr, libPtr));
250 cp += dlen;
251 } else if (dlen == 7) {
252 Printf("\tinternet address = %s",
253 __Inet_NtoA(inaddr.s_addr, libPtr));
254 Printf(", protocol = %d", cp[4]);
255 Printf(", port = %d\n",
256 (cp[5] << 8) + cp[6]);
257 cp += dlen;
259 break;
260 default:
261 cp += dlen;
263 break;
264 case T_CNAME:
265 case T_MB:
266 case T_MG:
267 case T_MR:
268 case T_NS:
269 case T_PTR:
270 Printf("\tdomain name = ");
271 cp = p_cdname(cp, msg);
272 Printf("\n");
273 break;
275 case T_HINFO:
276 if (n = *cp++) {
277 Printf("\tCPU=%.*s\n", n, cp);
278 cp += n;
280 if (n = *cp++) {
281 Printf("\tOS=%.*s\n", n, cp);
282 cp += n;
284 break;
286 case T_SOA:
287 Printf("\torigin = ");
288 cp = p_cdname(cp, msg);
289 Printf("\n\tmail addr = ");
290 cp = p_cdname(cp, msg);
291 Printf("\n\tserial = %ld", _getlong(cp));
292 cp += sizeof(u_long);
293 Printf("\n\trefresh = %s", __p_time(_getlong(cp)));
294 cp += sizeof(u_long);
295 Printf("\n\tretry = %s", __p_time(_getlong(cp)));
296 cp += sizeof(u_long);
297 Printf("\n\texpire = %s", __p_time(_getlong(cp)));
298 cp += sizeof(u_long);
299 Printf("\n\tmin = %s\n", __p_time(_getlong(cp)));
300 cp += sizeof(u_long);
301 break;
303 case T_MX:
304 Printf("\tpreference = %ld,",_getshort(cp));
305 cp += sizeof(u_short);
306 Printf(" name = ");
307 cp = p_cdname(cp, msg);
308 break;
310 case T_TXT:
311 PutStr("\t\"");
312 cp2 = cp1 + dlen;
313 while (cp < cp2) {
314 if (n = (unsigned char) *cp++) {
315 for (c = n; c > 0 && cp < cp2; c--)
316 if (*cp == '\n') {
317 PutStr("\\");
318 WriteChars(cp++, 1);
319 } else
320 WriteChars(cp++, 2);
323 PutStr("\"\n");
324 break;
326 case T_MINFO:
327 Printf("\trequests = ");
328 cp = p_cdname(cp, msg);
329 Printf("\n\terrors = ");
330 cp = p_cdname(cp, msg);
331 break;
333 case T_UINFO:
334 Printf("\t%s\n", cp);
335 cp += dlen;
336 break;
338 case T_UID:
339 case T_GID:
340 if (dlen == 4) {
341 Printf("\t%ld\n", _getlong(cp));
342 cp += sizeof(int);
344 break;
346 case T_WKS:
347 if (dlen < sizeof(u_long) + 1)
348 break;
349 bcopy(cp, (char *)&inaddr, sizeof(inaddr));
350 cp += sizeof(u_long);
351 Printf("\tinternet address = %s, protocol = %d\n\t",
352 __Inet_NtoA(inaddr.s_addr, libPtr), *cp++);
353 n = 0;
354 while (cp < cp1 + dlen) {
355 c = *cp++;
356 do {
357 if (c & 0200)
358 Printf(" %ld", n);
359 c <<= 1;
360 } while (++n & 07);
362 PutStr("\n");
363 break;
365 #ifdef ALLOW_T_UNSPEC
366 case T_UNSPEC:
368 int NumBytes = 8;
369 char *DataPtr;
370 int i;
372 if (dlen < NumBytes) NumBytes = dlen;
373 Printf( "\tFirst %ld bytes of hex data:",
374 NumBytes);
375 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
376 Printf( " %x", *DataPtr);
377 PutStr("\n");
378 cp += dlen;
380 break;
381 #endif /* ALLOW_T_UNSPEC */
383 default:
384 Printf("\t???\n");
385 cp += dlen;
387 if (cp != cp1 + dlen) {
388 Printf("packet size error (%#x != %#x)\n", cp, cp1+dlen);
389 cp = NULL;
391 Printf("\n");
392 return (cp);
395 static char nbuf[40];
398 * Return a string for the type
400 char *
401 __p_type(type)
402 int type;
404 switch (type) {
405 case T_A:
406 return("A");
407 case T_NS: /* authoritative server */
408 return("NS");
409 case T_CNAME: /* canonical name */
410 return("CNAME");
411 case T_SOA: /* start of authority zone */
412 return("SOA");
413 case T_MB: /* mailbox domain name */
414 return("MB");
415 case T_MG: /* mail group member */
416 return("MG");
417 case T_MR: /* mail rename name */
418 return("MR");
419 case T_NULL: /* null resource record */
420 return("NULL");
421 case T_WKS: /* well known service */
422 return("WKS");
423 case T_PTR: /* domain name pointer */
424 return("PTR");
425 case T_HINFO: /* host information */
426 return("HINFO");
427 case T_MINFO: /* mailbox information */
428 return("MINFO");
429 case T_MX: /* mail routing info */
430 return("MX");
431 case T_TXT: /* text */
432 return("TXT");
433 case T_AXFR: /* zone transfer */
434 return("AXFR");
435 case T_MAILB: /* mail box */
436 return("MAILB");
437 case T_MAILA: /* mail address */
438 return("MAILA");
439 case T_ANY: /* matches any type */
440 return("ANY");
441 case T_UINFO:
442 return("UINFO");
443 case T_UID:
444 return("UID");
445 case T_GID:
446 return("GID");
447 #ifdef ALLOW_T_UNSPEC
448 case T_UNSPEC:
449 return("UNSPEC");
450 #endif /* ALLOW_T_UNSPEC */
451 default:
452 (void)sprintf(nbuf, "%ld", type);
453 return(nbuf);
458 * Return a mnemonic for class
460 char *
461 __p_class(class)
462 int class;
465 switch (class) {
466 case C_IN: /* internet class */
467 return("IN");
468 case C_HS: /* hesiod class */
469 return("HS");
470 case C_ANY: /* matches any class */
471 return("ANY");
472 default:
473 (void)sprintf(nbuf, "%ld", class);
474 return(nbuf);
479 * Return a mnemonic for a time to live
481 char *
482 __p_time(value)
483 u_long value;
485 int secs, mins, hours;
486 register char *p;
488 if (value == 0) {
489 strcpy(nbuf, "0 secs");
490 return(nbuf);
493 secs = value % 60;
494 value /= 60;
495 mins = value % 60;
496 value /= 60;
497 hours = value % 24;
498 value /= 24;
500 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
501 p = nbuf;
502 if (value) {
503 (void)sprintf(p, "%ld day%s", PLURALIZE(value));
504 while (*++p);
506 if (hours) {
507 if (value)
508 *p++ = ' ';
509 (void)sprintf(p, "%ld hour%s", PLURALIZE(hours));
510 while (*++p);
512 if (mins) {
513 if (value || hours)
514 *p++ = ' ';
515 (void)sprintf(p, "%ld min%s", PLURALIZE(mins));
516 while (*++p);
518 if (secs || ! (value || hours || mins)) {
519 if (value || hours || mins)
520 *p++ = ' ';
521 (void)sprintf(p, "%ld sec%s", PLURALIZE(secs));
523 return(nbuf);
526 #endif /* RES_DEBUG */