]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/res_debug.c
Fix send to properly wait while long transmits are in progress
[mirror_edk2.git] / StdLib / BsdSocketLib / res_debug.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1985\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * Portions copyright (c) 1999, 2000\r
6 * Intel Corporation.\r
7 * All rights reserved.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 *\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 *\r
16 * 2. Redistributions in binary form must reproduce the above copyright\r
17 * notice, this list of conditions and the following disclaimer in the\r
18 * documentation and/or other materials provided with the distribution.\r
19 *\r
20 * 3. All advertising materials mentioning features or use of this software\r
21 * must display the following acknowledgement:\r
22 *\r
23 * This product includes software developed by the University of\r
24 * California, Berkeley, Intel Corporation, and its contributors.\r
25 *\r
26 * 4. Neither the name of University, Intel Corporation, or their respective\r
27 * contributors may be used to endorse or promote products derived from\r
28 * this software without specific prior written permission.\r
29 *\r
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
41 *\r
42 */\r
43\r
44/*\r
45 * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
46 *\r
47 * Permission to use, copy, modify, and distribute this software for any\r
48 * purpose with or without fee is hereby granted, provided that the above\r
49 * copyright notice and this permission notice appear in all copies, and that\r
50 * the name of Digital Equipment Corporation not be used in advertising or\r
51 * publicity pertaining to distribution of the document or software without\r
52 * specific, written prior permission.\r
53 *\r
54 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
55 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
57 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
61 * SOFTWARE.\r
62 */\r
63\r
64/*\r
65 * Portions Copyright (c) 1995 by International Business Machines, Inc.\r
66 *\r
67 * International Business Machines, Inc. (hereinafter called IBM) grants\r
68 * permission under its copyrights to use, copy, modify, and distribute this\r
69 * Software with or without fee, provided that the above copyright notice and\r
70 * all paragraphs of this notice appear in all copies, and that the name of IBM\r
71 * not be used in connection with the marketing of any product incorporating\r
72 * the Software or modifications thereof, without specific, written prior\r
73 * permission.\r
74 *\r
75 * To the extent it has a right to do so, IBM grants an immunity from suit\r
76 * under its patents, if any, for the use, sale or manufacture of products to\r
77 * the extent that such products are used for performing Domain Name System\r
78 * dynamic updates in TCP/IP networks by means of the Software. No immunity is\r
79 * granted for any product per se or for any other function of any product.\r
80 *\r
81 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,\r
82 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\r
83 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,\r
84 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING\r
85 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN\r
86 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
87 */\r
88\r
89/*\r
90 * Portions Copyright (c) 1996 by Internet Software Consortium.\r
91 *\r
92 * Permission to use, copy, modify, and distribute this software for any\r
93 * purpose with or without fee is hereby granted, provided that the above\r
94 * copyright notice and this permission notice appear in all copies.\r
95 *\r
96 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
97 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
98 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
99 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
100 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
101 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
102 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
103 * SOFTWARE.\r
104 */\r
105\r
106#if defined(LIBC_SCCS) && !defined(lint)\r
107static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";\r
108static char rcsid[] = "$Id: res_debug.c,v 1.1.1.1 2003/11/19 01:51:36 kyu3 Exp $";\r
109#endif /* LIBC_SCCS and not lint */\r
110\r
111#include <sys/types.h>\r
112#include <sys/param.h>\r
113#include <sys/socket.h>\r
114\r
115#include <netinet/in.h>\r
116#include <arpa/inet.h>\r
117#include <arpa/nameser.h>\r
118\r
119#include <ctype.h>\r
120#include <errno.h>\r
121#include <math.h>\r
122#include <netdb.h>\r
123#include <resolv.h>\r
124#include <stdio.h>\r
125#include <stdlib.h>\r
126#include <string.h>\r
127#include <time.h>\r
128\r
129#define SPRINTF(x) sprintf x\r
130\r
131extern const char *_res_opcodes[];\r
132extern const char *_res_resultcodes[];\r
133extern const char *_res_sectioncodes[];\r
134\r
135/*\r
136 * Print the current options.\r
137 */\r
138void\r
139fp_resstat(struct __res_state *statp, FILE *file) {\r
140 u_long mask;\r
141\r
142 fprintf(file, ";; res options:");\r
143 if (!statp)\r
144 statp = &_res;\r
145 for (mask = 1; mask != 0; mask <<= 1)\r
146 if (statp->options & mask)\r
147 fprintf(file, " %s", p_option(mask));\r
148 putc('\n', file);\r
149}\r
150\r
151static void\r
152do_section(ns_msg *handle, ns_sect section, int pflag, FILE *file) {\r
153 int n, sflag, rrnum;\r
154 ns_opcode opcode;\r
155 ns_rr rr;\r
156\r
157 /*\r
158 * Print answer records.\r
159 */\r
160 sflag = (int)(_res.pfcode & pflag);\r
161 if (_res.pfcode && !sflag)\r
162 return;\r
163\r
164 opcode = ns_msg_getflag(*handle, ns_f_opcode);\r
165 rrnum = 0;\r
166 for (;;) {\r
167 if (ns_parserr(handle, section, rrnum, &rr)) {\r
168 if (errno != ENODEV)\r
169 fprintf(file, ";; ns_parserr: %s\n",\r
170 strerror(errno));\r
171 else if (rrnum > 0 && sflag != 0 &&\r
172 (_res.pfcode & RES_PRF_HEAD1))\r
173 putc('\n', file);\r
174 return;\r
175 }\r
176 if (rrnum == 0 && sflag != 0 && (_res.pfcode & RES_PRF_HEAD1))\r
177 fprintf(file, ";; %s SECTION:\n",\r
178 p_section(section, opcode));\r
179 if (section == ns_s_qd)\r
180 fprintf(file, ";;\t%s, type = %s, class = %s\n",\r
181 ns_rr_name(rr),\r
182 p_type(ns_rr_type(rr)),\r
183 p_class(ns_rr_class(rr)));\r
184 else {\r
185 char *buf;\r
186 buf = (char*)malloc(2024);\r
187 if (buf) {\r
188 n = ns_sprintrr(handle, &rr, NULL, NULL,\r
189 buf, sizeof buf);\r
190 if (n < 0) {\r
191 fprintf(file, ";; ns_sprintrr: %s\n",\r
192 strerror(errno));\r
193 free(buf);\r
194 return;\r
195 }\r
196 fputs(buf, file);\r
197 fputc('\n', file);\r
198 free(buf);\r
199 }\r
200 }\r
201 rrnum++;\r
202 }\r
203}\r
204\r
205void\r
206p_query(const u_char *msg) {\r
207 fp_query(msg, stdout);\r
208}\r
209\r
210void\r
211fp_query(const u_char *msg, FILE *file) {\r
212 fp_nquery(msg, PACKETSZ, file);\r
213}\r
214\r
215/*\r
216 * Print the contents of a query.\r
217 * This is intended to be primarily a debugging routine.\r
218 */\r
219void\r
220fp_nquery(const u_char *msg, int len, FILE *file) {\r
221 ns_msg handle;\r
222 int qdcount, ancount, nscount, arcount;\r
223 u_int opcode, rcode, id;\r
224\r
225 if ((_res.options & RES_INIT) == 0 && res_init() == -1)\r
226 return;\r
227\r
228 if (ns_initparse(msg, len, &handle) < 0) {\r
229 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));\r
230 return;\r
231 }\r
232 opcode = ns_msg_getflag(handle, ns_f_opcode);\r
233 rcode = ns_msg_getflag(handle, ns_f_rcode);\r
234 id = ns_msg_id(handle);\r
235 qdcount = ns_msg_count(handle, ns_s_qd);\r
236 ancount = ns_msg_count(handle, ns_s_an);\r
237 nscount = ns_msg_count(handle, ns_s_ns);\r
238 arcount = ns_msg_count(handle, ns_s_ar);\r
239\r
240 /*\r
241 * Print header fields.\r
242 */\r
243 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || rcode)\r
244 fprintf(file,\r
245 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",\r
246 _res_opcodes[opcode], _res_resultcodes[rcode], id);\r
247 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))\r
248 putc(';', file);\r
249 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {\r
250 fprintf(file, "; flags:");\r
251 if (ns_msg_getflag(handle, ns_f_qr))\r
252 fprintf(file, " qr");\r
253 if (ns_msg_getflag(handle, ns_f_aa))\r
254 fprintf(file, " aa");\r
255 if (ns_msg_getflag(handle, ns_f_tc))\r
256 fprintf(file, " tc");\r
257 if (ns_msg_getflag(handle, ns_f_rd))\r
258 fprintf(file, " rd");\r
259 if (ns_msg_getflag(handle, ns_f_ra))\r
260 fprintf(file, " ra");\r
261 if (ns_msg_getflag(handle, ns_f_z))\r
262 fprintf(file, " ??");\r
263 if (ns_msg_getflag(handle, ns_f_ad))\r
264 fprintf(file, " ad");\r
265 if (ns_msg_getflag(handle, ns_f_cd))\r
266 fprintf(file, " cd");\r
267 }\r
268 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {\r
269 fprintf(file, "; %s: %d",\r
270 p_section(ns_s_qd, (int)opcode), qdcount);\r
271 fprintf(file, ", %s: %d",\r
272 p_section(ns_s_an, (int)opcode), ancount);\r
273 fprintf(file, ", %s: %d",\r
274 p_section(ns_s_ns, (int)opcode), nscount);\r
275 fprintf(file, ", %s: %d",\r
276 p_section(ns_s_ar, (int)opcode), arcount);\r
277 }\r
278 if ((!_res.pfcode) || (_res.pfcode &\r
279 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {\r
280 putc('\n',file);\r
281 }\r
282 /*\r
283 * Print the various sections.\r
284 */\r
285 do_section(&handle, ns_s_qd, RES_PRF_QUES, file);\r
286 do_section(&handle, ns_s_an, RES_PRF_ANS, file);\r
287 do_section(&handle, ns_s_ns, RES_PRF_AUTH, file);\r
288 do_section(&handle, ns_s_ar, RES_PRF_ADD, file);\r
289 if (qdcount == 0 && ancount == 0 &&\r
290 nscount == 0 && arcount == 0)\r
291 putc('\n', file);\r
292}\r
293\r
294const u_char *\r
295p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {\r
296 char name[MAXDNAME];\r
297 int n;\r
298\r
299 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)\r
300 return (NULL);\r
301 if (name[0] == '\0')\r
302 putc('.', file);\r
303 else\r
304 fputs(name, file);\r
305 return (cp + n);\r
306}\r
307\r
308const u_char *\r
309p_cdname(const u_char *cp, const u_char *msg, FILE *file) {\r
310 return (p_cdnname(cp, msg, PACKETSZ, file));\r
311}\r
312\r
313/* Return a fully-qualified domain name from a compressed name (with\r
314 length supplied). */\r
315\r
316const u_char *\r
317p_fqnname(\r
318 const u_char *cp,\r
319 const u_char *msg,\r
320 int msglen,\r
321 char *name,\r
322 int namelen\r
323 )\r
324{\r
325 int n, newlen;\r
326\r
327 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)\r
328 return (NULL);\r
329 newlen = (int)strlen(name);\r
330 if (newlen == 0 || name[newlen - 1] != '.') {\r
331 if (newlen + 1 >= namelen) /* Lack space for final dot */\r
332 return (NULL);\r
333 else\r
334 strcpy(name + newlen, ".");\r
335 }\r
336 return (cp + n);\r
337}\r
338\r
339/* XXX: the rest of these functions need to become length-limited, too. */\r
340\r
341const u_char *\r
342p_fqname(const u_char *cp, const u_char *msg, FILE *file) {\r
343 char name[MAXDNAME];\r
344 const u_char *n;\r
345\r
346 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);\r
347 if (n == NULL)\r
348 return (NULL);\r
349 fputs(name, file);\r
350 return (n);\r
351}\r
352\r
353/*\r
354 * Names of RR classes and qclasses. Classes and qclasses are the same, except\r
355 * that C_ANY is a qclass but not a class. (You can ask for records of class\r
356 * C_ANY, but you can't have any records of that class in the database.)\r
357 */\r
358const struct res_sym __p_class_syms[] = {\r
359 {C_IN, "IN"},\r
360 {C_CHAOS, "CHAOS"},\r
361 {C_HS, "HS"},\r
362 {C_HS, "HESIOD"},\r
363 {C_ANY, "ANY"},\r
364 {C_NONE, "NONE"},\r
365 {C_IN, (char *)0}\r
366};\r
367\r
368/*\r
369 * Names of message sections.\r
370 */\r
371const struct res_sym __p_default_section_syms[] = {\r
372 {ns_s_qd, "QUERY"},\r
373 {ns_s_an, "ANSWER"},\r
374 {ns_s_ns, "AUTHORITY"},\r
375 {ns_s_ar, "ADDITIONAL"},\r
376 {0, (char *)0}\r
377};\r
378\r
379const struct res_sym __p_update_section_syms[] = {\r
380 {S_ZONE, "ZONE"},\r
381 {S_PREREQ, "PREREQUISITE"},\r
382 {S_UPDATE, "UPDATE"},\r
383 {S_ADDT, "ADDITIONAL"},\r
384 {0, (char *)0}\r
385};\r
386\r
387/*\r
388 * Names of RR types and qtypes. Types and qtypes are the same, except\r
389 * that T_ANY is a qtype but not a type. (You can ask for records of type\r
390 * T_ANY, but you can't have any records of that type in the database.)\r
391 */\r
392const struct res_sym __p_type_syms[] = {\r
393 {T_A, "A", "address"},\r
394 {T_NS, "NS", "name server"},\r
395 {T_MD, "MD", "mail destination (deprecated)"},\r
396 {T_MF, "MF", "mail forwarder (deprecated)"},\r
397 {T_CNAME, "CNAME", "canonical name"},\r
398 {T_SOA, "SOA", "start of authority"},\r
399 {T_MB, "MB", "mailbox"},\r
400 {T_MG, "MG", "mail group member"},\r
401 {T_MR, "MR", "mail rename"},\r
402 {T_NULL, "NULL", "null"},\r
403 {T_WKS, "WKS", "well-known service (deprecated)"},\r
404 {T_PTR, "PTR", "domain name pointer"},\r
405 {T_HINFO, "HINFO", "host information"},\r
406 {T_MINFO, "MINFO", "mailbox information"},\r
407 {T_MX, "MX", "mail exchanger"},\r
408 {T_TXT, "TXT", "text"},\r
409 {T_RP, "RP", "responsible person"},\r
410 {T_AFSDB, "AFSDB", "DCE or AFS server"},\r
411 {T_X25, "X25", "X25 address"},\r
412 {T_ISDN, "ISDN", "ISDN address"},\r
413 {T_RT, "RT", "router"},\r
414 {T_NSAP, "NSAP", "nsap address"},\r
415 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},\r
416 {T_SIG, "SIG", "signature"},\r
417 {T_KEY, "KEY", "key"},\r
418 {T_PX, "PX", "mapping information"},\r
419 {T_GPOS, "GPOS", "geographical position (withdrawn)"},\r
420 {T_AAAA, "AAAA", "IPv6 address"},\r
421 {T_LOC, "LOC", "location"},\r
422 {T_NXT, "NXT", "next valid name (unimplemented)"},\r
423 {T_EID, "EID", "endpoint identifier (unimplemented)"},\r
424 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},\r
425 {T_SRV, "SRV", "server selection"},\r
426 {T_ATMA, "ATMA", "ATM address (unimplemented)"},\r
427 {T_IXFR, "IXFR", "incremental zone transfer"},\r
428 {T_AXFR, "AXFR", "zone transfer"},\r
429 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},\r
430 {T_MAILA, "MAILA", "mail agent (deprecated)"},\r
431 {T_NAPTR, "NAPTR", "URN Naming Authority"},\r
432 {T_ANY, "ANY", "\"any\""},\r
433 {0, NULL, NULL}\r
434};\r
435\r
436int\r
437sym_ston(const struct res_sym *syms, const char *name, int *success) {\r
438 for ((void)NULL; syms->name != 0; syms++) {\r
439 if (strcasecmp (name, syms->name) == 0) {\r
440 if (success)\r
441 *success = 1;\r
442 return (syms->number);\r
443 }\r
444 }\r
445 if (success)\r
446 *success = 0;\r
447 return (syms->number); /* The default value. */\r
448}\r
449\r
450const char *\r
451sym_ntos(const struct res_sym *syms, int number, int *success) {\r
452 static char unname[20];\r
453\r
454 for ((void)NULL; syms->name != 0; syms++) {\r
455 if (number == syms->number) {\r
456 if (success)\r
457 *success = 1;\r
458 return (syms->name);\r
459 }\r
460 }\r
461\r
462 sprintf(unname, "%d", number);\r
463 if (success)\r
464 *success = 0;\r
465 return (unname);\r
466}\r
467\r
468const char *\r
469sym_ntop(const struct res_sym *syms, int number, int *success) {\r
470 static char unname[20];\r
471\r
472 for ((void)NULL; syms->name != 0; syms++) {\r
473 if (number == syms->number) {\r
474 if (success)\r
475 *success = 1;\r
476 return (syms->humanname);\r
477 }\r
478 }\r
479 sprintf(unname, "%d", number);\r
480 if (success)\r
481 *success = 0;\r
482 return (unname);\r
483}\r
484\r
485/*\r
486 * Return a string for the type.\r
487 */\r
488const char *\r
489p_type(int type) {\r
490 return (sym_ntos(__p_type_syms, type, (int *)0));\r
491}\r
492\r
493/*\r
494 * Return a string for the type.\r
495 */\r
496const char *\r
497p_section(int section, int opcode) {\r
498 const struct res_sym *symbols;\r
499\r
500 switch (opcode) {\r
501 case ns_o_update:\r
502 symbols = __p_update_section_syms;\r
503 break;\r
504 default:\r
505 symbols = __p_default_section_syms;\r
506 break;\r
507 }\r
508 return (sym_ntos(symbols, section, (int *)0));\r
509}\r
510\r
511/*\r
512 * Return a mnemonic for class.\r
513 */\r
514const char *\r
515p_class(int class) {\r
516 return (sym_ntos(__p_class_syms, class, (int *)0));\r
517}\r
518\r
519/*\r
520 * Return a mnemonic for an option\r
521 */\r
522const char *\r
523p_option(u_long option) {\r
524 static char nbuf[40];\r
525\r
526 switch (option) {\r
527 case RES_INIT: return "init";\r
528 case RES_DEBUG: return "debug";\r
529 case RES_AAONLY: return "aaonly(unimpl)";\r
530 case RES_USEVC: return "usevc";\r
531 case RES_PRIMARY: return "primry(unimpl)";\r
532 case RES_IGNTC: return "igntc";\r
533 case RES_RECURSE: return "recurs";\r
534 case RES_DEFNAMES: return "defnam";\r
535 case RES_STAYOPEN: return "styopn";\r
536 case RES_DNSRCH: return "dnsrch";\r
537 case RES_INSECURE1: return "insecure1";\r
538 case RES_INSECURE2: return "insecure2";\r
539 default: sprintf(nbuf, "?0x%lx?", (u_long)option);\r
540 return (nbuf);\r
541 }\r
542}\r
543\r
544/*\r
545 * Return a mnemonic for a time to live.\r
546 */\r
547const char *\r
548p_time(u_int32_t value) {\r
549 static char nbuf[40];\r
550\r
551 if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0)\r
552 sprintf(nbuf, "%u", value);\r
553 return (nbuf);\r
554}\r
555\r
556\r
557/*\r
558 * routines to convert between on-the-wire RR format and zone file format.\r
559 * Does not contain conversion to/from decimal degrees; divide or multiply\r
560 * by 60*60*1000 for that.\r
561 */\r
562\r
563static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,\r
564 1000000,10000000,100000000,1000000000};\r
565\r
566/* takes an XeY precision/size value, returns a string representation. */\r
567static const char *\r
568precsize_ntoa(\r
569 u_int8_t prec\r
570 )\r
571{\r
572 static char retbuf[sizeof "90000000.00"];\r
573 unsigned long val;\r
574 int mantissa, exponent;\r
575\r
576 mantissa = (int)((prec >> 4) & 0x0f) % 10;\r
577 exponent = (int)((prec >> 0) & 0x0f) % 10;\r
578\r
579 val = mantissa * poweroften[exponent];\r
580\r
581 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);\r
582 return (retbuf);\r
583}\r
584\r
585/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */\r
586static u_int8_t\r
587precsize_aton(\r
588 char **strptr\r
589 )\r
590{\r
591 unsigned int mval = 0, cmval = 0;\r
592 u_int8_t retval = 0;\r
593 char *cp;\r
594 int exponent;\r
595 int mantissa;\r
596\r
597 cp = *strptr;\r
598\r
599 while (isdigit(*cp))\r
600 mval = mval * 10 + (*cp++ - '0');\r
601\r
602 if (*cp == '.') { /* centimeters */\r
603 cp++;\r
604 if (isdigit(*cp)) {\r
605 cmval = (*cp++ - '0') * 10;\r
606 if (isdigit(*cp)) {\r
607 cmval += (*cp++ - '0');\r
608 }\r
609 }\r
610 }\r
611 cmval = (mval * 100) + cmval;\r
612\r
613 for (exponent = 0; exponent < 9; exponent++)\r
614 if (cmval < poweroften[exponent+1])\r
615 break;\r
616\r
617 mantissa = cmval / poweroften[exponent];\r
618 if (mantissa > 9)\r
619 mantissa = 9;\r
620\r
621 retval = (u_int8_t)((mantissa << 4) | exponent);\r
622\r
623 *strptr = cp;\r
624\r
625 return (retval);\r
626}\r
627\r
628/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */\r
629static u_int32_t\r
630latlon2ul(\r
631 char **latlonstrptr,\r
632 int *which\r
633 )\r
634{\r
635 char *cp;\r
636 u_int32_t retval;\r
637 int deg = 0, min = 0, secs = 0, secsfrac = 0;\r
638\r
639 cp = *latlonstrptr;\r
640\r
641 while (isdigit(*cp))\r
642 deg = deg * 10 + (*cp++ - '0');\r
643\r
644 while (isspace(*cp))\r
645 cp++;\r
646\r
647 if (!(isdigit(*cp)))\r
648 goto fndhemi;\r
649\r
650 while (isdigit(*cp))\r
651 min = min * 10 + (*cp++ - '0');\r
652\r
653 while (isspace(*cp))\r
654 cp++;\r
655\r
656 if (!(isdigit(*cp)))\r
657 goto fndhemi;\r
658\r
659 while (isdigit(*cp))\r
660 secs = secs * 10 + (*cp++ - '0');\r
661\r
662 if (*cp == '.') { /* decimal seconds */\r
663 cp++;\r
664 if (isdigit(*cp)) {\r
665 secsfrac = (*cp++ - '0') * 100;\r
666 if (isdigit(*cp)) {\r
667 secsfrac += (*cp++ - '0') * 10;\r
668 if (isdigit(*cp)) {\r
669 secsfrac += (*cp++ - '0');\r
670 }\r
671 }\r
672 }\r
673 }\r
674\r
675 while (!isspace(*cp)) /* if any trailing garbage */\r
676 cp++;\r
677\r
678 while (isspace(*cp))\r
679 cp++;\r
680\r
681 fndhemi:\r
682 switch (*cp) {\r
683 case 'N': case 'n':\r
684 case 'E': case 'e':\r
685 retval = ((unsigned)1<<31)\r
686 + (((((deg * 60) + min) * 60) + secs) * 1000)\r
687 + secsfrac;\r
688 break;\r
689 case 'S': case 's':\r
690 case 'W': case 'w':\r
691 retval = ((unsigned)1<<31)\r
692 - (((((deg * 60) + min) * 60) + secs) * 1000)\r
693 - secsfrac;\r
694 break;\r
695 default:\r
696 retval = 0; /* invalid value -- indicates error */\r
697 break;\r
698 }\r
699\r
700 switch (*cp) {\r
701 case 'N': case 'n':\r
702 case 'S': case 's':\r
703 *which = 1; /* latitude */\r
704 break;\r
705 case 'E': case 'e':\r
706 case 'W': case 'w':\r
707 *which = 2; /* longitude */\r
708 break;\r
709 default:\r
710 *which = 0; /* error */\r
711 break;\r
712 }\r
713\r
714 cp++; /* skip the hemisphere */\r
715\r
716 while (!isspace(*cp)) /* if any trailing garbage */\r
717 cp++;\r
718\r
719 while (isspace(*cp)) /* move to next field */\r
720 cp++;\r
721\r
722 *latlonstrptr = cp;\r
723\r
724 return (retval);\r
725}\r
726\r
727/* converts a zone file representation in a string to an RDATA on-the-wire\r
728 * representation. */\r
729int\r
730loc_aton(\r
731 const char *ascii,\r
732 u_char *binary\r
733 )\r
734{\r
735 const char *cp, *maxcp;\r
736 u_char *bcp;\r
737\r
738 u_int32_t latit = 0, longit = 0, alt = 0;\r
739 u_int32_t lltemp1 = 0, lltemp2 = 0;\r
740 int altmeters = 0, altfrac = 0, altsign = 1;\r
741 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */\r
742 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */\r
743 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */\r
744 int which1 = 0, which2 = 0;\r
745\r
746 cp = ascii;\r
747 maxcp = cp + strlen(ascii);\r
748\r
749 lltemp1 = latlon2ul((char **)&cp, &which1);\r
750\r
751 lltemp2 = latlon2ul((char **)&cp, &which2);\r
752\r
753 switch (which1 + which2) {\r
754 case 3: /* 1 + 2, the only valid combination */\r
755 if ((which1 == 1) && (which2 == 2)) { /* normal case */\r
756 latit = lltemp1;\r
757 longit = lltemp2;\r
758 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */\r
759 longit = lltemp1;\r
760 latit = lltemp2;\r
761 } else { /* some kind of brokenness */\r
762 return (0);\r
763 }\r
764 break;\r
765 default: /* we didn't get one of each */\r
766 return (0);\r
767 }\r
768\r
769 /* altitude */\r
770 if (*cp == '-') {\r
771 altsign = -1;\r
772 cp++;\r
773 }\r
774\r
775 if (*cp == '+')\r
776 cp++;\r
777\r
778 while (isdigit(*cp))\r
779 altmeters = altmeters * 10 + (*cp++ - '0');\r
780\r
781 if (*cp == '.') { /* decimal meters */\r
782 cp++;\r
783 if (isdigit(*cp)) {\r
784 altfrac = (*cp++ - '0') * 10;\r
785 if (isdigit(*cp)) {\r
786 altfrac += (*cp++ - '0');\r
787 }\r
788 }\r
789 }\r
790\r
791 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));\r
792\r
793 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */\r
794 cp++;\r
795\r
796 while (isspace(*cp) && (cp < maxcp))\r
797 cp++;\r
798\r
799 if (cp >= maxcp)\r
800 goto defaults;\r
801\r
802 siz = precsize_aton((char **)&cp);\r
803\r
804 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */\r
805 cp++;\r
806\r
807 while (isspace(*cp) && (cp < maxcp))\r
808 cp++;\r
809\r
810 if (cp >= maxcp)\r
811 goto defaults;\r
812\r
813 hp = precsize_aton((char **)&cp);\r
814\r
815 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */\r
816 cp++;\r
817\r
818 while (isspace(*cp) && (cp < maxcp))\r
819 cp++;\r
820\r
821 if (cp >= maxcp)\r
822 goto defaults;\r
823\r
824 vp = precsize_aton((char **)&cp);\r
825\r
826 defaults:\r
827\r
828 bcp = binary;\r
829 *bcp++ = (u_int8_t) 0; /* version byte */\r
830 *bcp++ = siz;\r
831 *bcp++ = hp;\r
832 *bcp++ = vp;\r
833 PUTLONG(latit,bcp);\r
834 PUTLONG(longit,bcp);\r
835 PUTLONG(alt,bcp);\r
836\r
837 return (16); /* size of RR in octets */\r
838}\r
839\r
840/* takes an on-the-wire LOC RR and formats it in a human readable format. */\r
841const char *\r
842loc_ntoa(\r
843 const u_char *binary,\r
844 char *ascii\r
845 )\r
846{\r
847 static char *error = "?";\r
848 const u_char *cp = binary;\r
849\r
850 int latdeg, latmin, latsec, latsecfrac;\r
851 int longdeg, longmin, longsec, longsecfrac;\r
852 char northsouth, eastwest;\r
853 int altmeters, altfrac, altsign;\r
854\r
855 const u_int32_t referencealt = 100000 * 100;\r
856\r
857 int32_t latval, longval, altval;\r
858 u_int32_t templ;\r
859 u_int8_t sizeval, hpval, vpval, versionval;\r
860\r
861 char *sizestr, *hpstr, *vpstr;\r
862\r
863 versionval = *cp++;\r
864\r
865 if (versionval) {\r
866 (void) sprintf(ascii, "; error: unknown LOC RR version");\r
867 return (ascii);\r
868 }\r
869\r
870 sizeval = *cp++;\r
871\r
872 hpval = *cp++;\r
873 vpval = *cp++;\r
874\r
875 GETLONG(templ, cp);\r
876 latval = (templ - ((unsigned)1<<31));\r
877\r
878 GETLONG(templ, cp);\r
879 longval = (templ - ((unsigned)1<<31));\r
880\r
881 GETLONG(templ, cp);\r
882 if (templ < referencealt) { /* below WGS 84 spheroid */\r
883 altval = referencealt - templ;\r
884 altsign = -1;\r
885 } else {\r
886 altval = templ - referencealt;\r
887 altsign = 1;\r
888 }\r
889\r
890 if (latval < 0) {\r
891 northsouth = 'S';\r
892 latval = -latval;\r
893 } else\r
894 northsouth = 'N';\r
895\r
896 latsecfrac = latval % 1000;\r
897 latval = latval / 1000;\r
898 latsec = latval % 60;\r
899 latval = latval / 60;\r
900 latmin = latval % 60;\r
901 latval = latval / 60;\r
902 latdeg = latval;\r
903\r
904 if (longval < 0) {\r
905 eastwest = 'W';\r
906 longval = -longval;\r
907 } else\r
908 eastwest = 'E';\r
909\r
910 longsecfrac = longval % 1000;\r
911 longval = longval / 1000;\r
912 longsec = longval % 60;\r
913 longval = longval / 60;\r
914 longmin = longval % 60;\r
915 longval = longval / 60;\r
916 longdeg = longval;\r
917\r
918 altfrac = altval % 100;\r
919 altmeters = (altval / 100) * altsign;\r
920\r
921 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)\r
922 sizestr = error;\r
923 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)\r
924 hpstr = error;\r
925 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)\r
926 vpstr = error;\r
927\r
928 sprintf(ascii,\r
929 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",\r
930 latdeg, latmin, latsec, latsecfrac, northsouth,\r
931 longdeg, longmin, longsec, longsecfrac, eastwest,\r
932 altmeters, altfrac, sizestr, hpstr, vpstr);\r
933\r
934 if (sizestr != error)\r
935 free(sizestr);\r
936 if (hpstr != error)\r
937 free(hpstr);\r
938 if (vpstr != error)\r
939 free(vpstr);\r
940\r
941 return (ascii);\r
942}\r
943\r
944\r
945/* Return the number of DNS hierarchy levels in the name. */\r
946int\r
947dn_count_labels(const char *name) {\r
948 int i, len, count;\r
949\r
950 len = (int)strlen(name);\r
951 for (i = 0, count = 0; i < len; i++) {\r
952 /* XXX need to check for \. or use named's nlabels(). */\r
953 if (name[i] == '.')\r
954 count++;\r
955 }\r
956\r
957 /* don't count initial wildcard */\r
958 if (name[0] == '*')\r
959 if (count)\r
960 count--;\r
961\r
962 /* don't count the null label for root. */\r
963 /* if terminating '.' not found, must adjust */\r
964 /* count to include last label */\r
965 if (len > 0 && name[len-1] != '.')\r
966 count++;\r
967 return (count);\r
968}\r
969\r
970\r
971/*\r
972 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.\r
973 * SIG records are required to be printed like this, by the Secure DNS RFC.\r
974 */\r
975char *\r
976p_secstodate (u_long secs) {\r
977 static char output[15]; /* YYYYMMDDHHMMSS and null */\r
978 time_t clock = (time_t)secs;\r
979 struct tm *time;\r
980\r
981 time = gmtime(&clock);\r
982 time->tm_year += 1900;\r
983 time->tm_mon += 1;\r
984 sprintf(output, "%04d%02d%02d%02d%02d%02d",\r
985 time->tm_year, time->tm_mon, time->tm_mday,\r
986 time->tm_hour, time->tm_min, time->tm_sec);\r
987 return (output);\r
988}\r