1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92: #if defined(LIBC_SCCS) && !defined(lint)
93: static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
94: static const char rcsid[] = "$BINDId: res_debug.c,v 8.34 2000/02/29 05:30:55 vixie Exp $";
95: #endif
96:
97: #include <sys/types.h>
98: #include <sys/param.h>
99: #include <sys/socket.h>
100:
101: #include <netinet/in.h>
102: #include <arpa/inet.h>
103: #include <arpa/nameser.h>
104:
105: #include <ctype.h>
106: #include <errno.h>
107: #include <math.h>
108: #include <netdb.h>
109: #include <resolv.h>
110: #include <stdio.h>
111: #include <stdlib.h>
112: #include <string.h>
113: #include <time.h>
114:
115: #ifdef SPRINTF_CHAR
116: # define SPRINTF(x) strlen(sprintfx)
117: #else
118: # define SPRINTF(x) sprintf x
119: #endif
120:
121: extern const char *_res_sectioncodes[] attribute_hidden;
122:
123:
124:
125:
126: void
127: fp_resstat(const res_state statp, FILE *file) {
128: u_long mask;
129:
130: fprintf(file, ";; res options:");
131: for (mask = 1; mask != 0; mask <<= 1)
132: if (statp->options & mask)
133: fprintf(file, " %s", p_option(mask));
134: putc('\n', file);
135: }
136:
137: static void
138: do_section(const res_state statp,
139: ns_msg *handle, ns_sect section,
140: int pflag, FILE *file)
141: {
142: int n, sflag, rrnum;
143: static int buflen = 2048;
144: char *buf;
145: ns_opcode opcode;
146: ns_rr rr;
147:
148:
149:
150:
151: sflag = (statp->pfcode & pflag);
152: if (statp->pfcode && !sflag)
153: return;
154:
155: buf = malloc(buflen);
156: if (buf == NULL) {
157: fprintf(file, ";; memory allocation failure\n");
158: return;
159: }
160:
161: opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
162: rrnum = 0;
163: for (;;) {
164: if (ns_parserr(handle, section, rrnum, &rr)) {
165: if (errno != ENODEV)
166: fprintf(file, ";; ns_parserr: %s\n",
167: strerror(errno));
168: else if (rrnum > 0 && sflag != 0 &&
169: (statp->pfcode & RES_PRF_HEAD1))
170: putc('\n', file);
171: goto cleanup;
172: }
173: if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
174: fprintf(file, ";; %s SECTION:\n",
175: p_section(section, opcode));
176: if (section == ns_s_qd)
177: fprintf(file, ";;\t%s, type = %s, class = %s\n",
178: ns_rr_name(rr),
179: p_type(ns_rr_type(rr)),
180: p_class(ns_rr_class(rr)));
181: else {
182: n = ns_sprintrr(handle, &rr, NULL, NULL,
183: buf, buflen);
184: if (n < 0) {
185: if (errno == ENOSPC) {
186: free(buf);
187: buf = NULL;
188: if (buflen < 131072)
189: buf = malloc(buflen += 1024);
190: if (buf == NULL) {
191: fprintf(file,
192: ";; memory allocation failure\n");
193: return;
194: }
195: continue;
196: }
197: fprintf(file, ";; ns_sprintrr: %s\n",
198: strerror(errno));
199: goto cleanup;
200: }
201: fputs(buf, file);
202: fputc('\n', file);
203: }
204: rrnum++;
205: }
206: cleanup:
207: if (buf != NULL)
208: free(buf);
209: }
210:
211:
212:
213:
214:
215: void
216: res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
217: ns_msg handle;
218: int qdcount, ancount, nscount, arcount;
219: u_int opcode, rcode, id;
220:
221: if (ns_initparse(msg, len, &handle) < 0) {
222: fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
223: return;
224: }
225: opcode = ns_msg_getflag(handle, ns_f_opcode);
226: rcode = ns_msg_getflag(handle, ns_f_rcode);
227: id = ns_msg_id(handle);
228: qdcount = ns_msg_count(handle, ns_s_qd);
229: ancount = ns_msg_count(handle, ns_s_an);
230: nscount = ns_msg_count(handle, ns_s_ns);
231: arcount = ns_msg_count(handle, ns_s_ar);
232:
233:
234:
235:
236: if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
237: fprintf(file,
238: ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
239: _res_opcodes[opcode], p_rcode(rcode), id);
240: if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
241: putc(';', file);
242: if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
243: fprintf(file, "; flags:");
244: if (ns_msg_getflag(handle, ns_f_qr))
245: fprintf(file, " qr");
246: if (ns_msg_getflag(handle, ns_f_aa))
247: fprintf(file, " aa");
248: if (ns_msg_getflag(handle, ns_f_tc))
249: fprintf(file, " tc");
250: if (ns_msg_getflag(handle, ns_f_rd))
251: fprintf(file, " rd");
252: if (ns_msg_getflag(handle, ns_f_ra))
253: fprintf(file, " ra");
254: if (ns_msg_getflag(handle, ns_f_z))
255: fprintf(file, " ??");
256: if (ns_msg_getflag(handle, ns_f_ad))
257: fprintf(file, " ad");
258: if (ns_msg_getflag(handle, ns_f_cd))
259: fprintf(file, " cd");
260: }
261: if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
262: fprintf(file, "; %s: %d",
263: p_section(ns_s_qd, opcode), qdcount);
264: fprintf(file, ", %s: %d",
265: p_section(ns_s_an, opcode), ancount);
266: fprintf(file, ", %s: %d",
267: p_section(ns_s_ns, opcode), nscount);
268: fprintf(file, ", %s: %d",
269: p_section(ns_s_ar, opcode), arcount);
270: }
271: if ((!statp->pfcode) || (statp->pfcode &
272: (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
273: putc('\n',file);
274: }
275:
276:
277:
278: do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
279: do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
280: do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
281: do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
282: if (qdcount == 0 && ancount == 0 &&
283: nscount == 0 && arcount == 0)
284: putc('\n', file);
285: }
286:
287: const u_char *
288: p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
289: char name[MAXDNAME];
290: int n;
291:
292: if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
293: return (NULL);
294: if (name[0] == '\0')
295: putc('.', file);
296: else
297: fputs(name, file);
298: return (cp + n);
299: }
300: libresolv_hidden_def (p_cdnname)
301:
302: const u_char *
303: p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
304: return (p_cdnname(cp, msg, PACKETSZ, file));
305: }
306:
307:
308:
309:
310: const u_char *
311: p_fqnname(cp, msg, msglen, name, namelen)
312: const u_char *cp, *msg;
313: int msglen;
314: char *name;
315: int namelen;
316: {
317: int n, newlen;
318:
319: if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
320: return (NULL);
321: newlen = strlen(name);
322: if (newlen == 0 || name[newlen - 1] != '.') {
323: if (newlen + 1 >= namelen)
324: return (NULL);
325: else
326: strcpy(name + newlen, ".");
327: }
328: return (cp + n);
329: }
330: libresolv_hidden_def (p_fqnname)
331:
332:
333:
334: const u_char *
335: p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
336: char name[MAXDNAME];
337: const u_char *n;
338:
339: n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
340: if (n == NULL)
341: return (NULL);
342: fputs(name, file);
343: return (n);
344: }
345:
346:
347:
348:
349:
350:
351: extern const struct res_sym __p_class_syms[];
352: libresolv_hidden_proto (__p_class_syms)
353: const struct res_sym __p_class_syms[] = {
354: {C_IN, "IN"},
355: {C_CHAOS, "CHAOS"},
356: {C_HS, "HS"},
357: {C_HS, "HESIOD"},
358: {C_ANY, "ANY"},
359: {C_NONE, "NONE"},
360: {C_IN, (char *)0}
361: };
362: libresolv_hidden_data_def (__p_class_syms)
363:
364:
365:
366:
367: const struct res_sym __p_default_section_syms[] attribute_hidden = {
368: {ns_s_qd, "QUERY"},
369: {ns_s_an, "ANSWER"},
370: {ns_s_ns, "AUTHORITY"},
371: {ns_s_ar, "ADDITIONAL"},
372: {0, (char *)0}
373: };
374:
375: const struct res_sym __p_update_section_syms[] attribute_hidden = {
376: {S_ZONE, "ZONE"},
377: {S_PREREQ, "PREREQUISITE"},
378: {S_UPDATE, "UPDATE"},
379: {S_ADDT, "ADDITIONAL"},
380: {0, (char *)0}
381: };
382:
383: const struct res_sym __p_key_syms[] attribute_hidden = {
384: {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
385: {NS_ALG_DH, "DH", "Diffie Hellman"},
386: {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
387: {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
388: {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
389: {0, NULL, NULL}
390: };
391:
392: const struct res_sym __p_cert_syms[] attribute_hidden = {
393: {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
394: {cert_t_spki, "SPKI", "SPKI certificate"},
395: {cert_t_pgp, "PGP", "PGP certificate"},
396: {cert_t_url, "URL", "URL Private"},
397: {cert_t_oid, "OID", "OID Private"},
398: {0, NULL, NULL}
399: };
400:
401:
402:
403:
404:
405:
406: extern const struct res_sym __p_type_syms[];
407: libresolv_hidden_proto (__p_type_syms)
408: const struct res_sym __p_type_syms[] = {
409: {ns_t_a, "A", "address"},
410: {ns_t_ns, "NS", "name server"},
411: {ns_t_md, "MD", "mail destination (deprecated)"},
412: {ns_t_mf, "MF", "mail forwarder (deprecated)"},
413: {ns_t_cname, "CNAME", "canonical name"},
414: {ns_t_soa, "SOA", "start of authority"},
415: {ns_t_mb, "MB", "mailbox"},
416: {ns_t_mg, "MG", "mail group member"},
417: {ns_t_mr, "MR", "mail rename"},
418: {ns_t_null, "NULL", "null"},
419: {ns_t_wks, "WKS", "well-known service (deprecated)"},
420: {ns_t_ptr, "PTR", "domain name pointer"},
421: {ns_t_hinfo, "HINFO", "host information"},
422: {ns_t_minfo, "MINFO", "mailbox information"},
423: {ns_t_mx, "MX", "mail exchanger"},
424: {ns_t_txt, "TXT", "text"},
425: {ns_t_rp, "RP", "responsible person"},
426: {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
427: {ns_t_x25, "X25", "X25 address"},
428: {ns_t_isdn, "ISDN", "ISDN address"},
429: {ns_t_rt, "RT", "router"},
430: {ns_t_nsap, "NSAP", "nsap address"},
431: {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
432: {ns_t_sig, "SIG", "signature"},
433: {ns_t_key, "KEY", "key"},
434: {ns_t_px, "PX", "mapping information"},
435: {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
436: {ns_t_aaaa, "AAAA", "IPv6 address"},
437: {ns_t_loc, "LOC", "location"},
438: {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
439: {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
440: {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
441: {ns_t_srv, "SRV", "server selection"},
442: {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
443: {ns_t_tsig, "TSIG", "transaction signature"},
444: {ns_t_ixfr, "IXFR", "incremental zone transfer"},
445: {ns_t_axfr, "AXFR", "zone transfer"},
446: {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
447: {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
448: {ns_t_maila, "MAILA", "mail agent (deprecated)"},
449: {ns_t_naptr, "NAPTR", "URN Naming Authority"},
450: {ns_t_kx, "KX", "Key Exchange"},
451: {ns_t_cert, "CERT", "Certificate"},
452: {ns_t_any, "ANY", "\"any\""},
453: {0, NULL, NULL}
454: };
455: libresolv_hidden_data_def (__p_type_syms)
456:
457:
458:
459:
460: const struct res_sym __p_rcode_syms[] attribute_hidden = {
461: {ns_r_noerror, "NOERROR", "no error"},
462: {ns_r_formerr, "FORMERR", "format error"},
463: {ns_r_servfail, "SERVFAIL", "server failed"},
464: {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
465: {ns_r_notimpl, "NOTIMP", "not implemented"},
466: {ns_r_refused, "REFUSED", "refused"},
467: {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
468: {ns_r_yxrrset, "YXRRSET", "rrset exists"},
469: {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
470: {ns_r_notauth, "NOTAUTH", "not authoritative"},
471: {ns_r_notzone, "NOTZONE", "Not in zone"},
472: {ns_r_max, "", ""},
473: {ns_r_badsig, "BADSIG", "bad signature"},
474: {ns_r_badkey, "BADKEY", "bad key"},
475: {ns_r_badtime, "BADTIME", "bad time"},
476: {0, NULL, NULL}
477: };
478:
479: int
480: sym_ston(const struct res_sym *syms, const char *name, int *success) {
481: for ((void)NULL; syms->name != 0; syms++) {
482: if (strcasecmp (name, syms->name) == 0) {
483: if (success)
484: *success = 1;
485: return (syms->number);
486: }
487: }
488: if (success)
489: *success = 0;
490: return (syms->number);
491: }
492:
493: const char *
494: sym_ntos(const struct res_sym *syms, int number, int *success) {
495: static char unname[20];
496:
497: for ((void)NULL; syms->name != 0; syms++) {
498: if (number == syms->number) {
499: if (success)
500: *success = 1;
501: return (syms->name);
502: }
503: }
504:
505: sprintf(unname, "%d", number);
506: if (success)
507: *success = 0;
508: return (unname);
509: }
510: libresolv_hidden_def (sym_ntos)
511:
512: const char *
513: sym_ntop(const struct res_sym *syms, int number, int *success) {
514: static char unname[20];
515:
516: for ((void)NULL; syms->name != 0; syms++) {
517: if (number == syms->number) {
518: if (success)
519: *success = 1;
520: return (syms->humanname);
521: }
522: }
523: sprintf(unname, "%d", number);
524: if (success)
525: *success = 0;
526: return (unname);
527: }
528:
529:
530:
531:
532: const char *
533: p_type(int type) {
534: return (sym_ntos(