source: rtems/cpukit/libnetworking/libc/res_debug.c @ 39f13cb

4.104.115
Last change on this file since 39f13cb was 39f13cb, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/27/10 at 04:00:53

Add HAVE_STRINGS_H for better POSIX compliance.

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