"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/bin/tools/named-rrchecker.c" (4 Sep 2020, 8198 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "named-rrchecker.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 #include <stdbool.h>
   13 #include <stdlib.h>
   14 
   15 #include <isc/attributes.h>
   16 #include <isc/buffer.h>
   17 #include <isc/commandline.h>
   18 #include <isc/lex.h>
   19 #include <isc/mem.h>
   20 #include <isc/print.h>
   21 #include <isc/string.h>
   22 #include <isc/util.h>
   23 
   24 #include <dns/fixedname.h>
   25 #include <dns/name.h>
   26 #include <dns/rdata.h>
   27 #include <dns/rdataclass.h>
   28 #include <dns/rdatatype.h>
   29 #include <dns/result.h>
   30 
   31 static isc_mem_t *mctx;
   32 static isc_lex_t *lex;
   33 
   34 static isc_lexspecials_t specials;
   35 
   36 ISC_NORETURN static void
   37 usage(void);
   38 
   39 static void
   40 usage(void) {
   41     fprintf(stderr, "usage: named-rrchecker [-o origin] [-hpCPTu]\n");
   42     fprintf(stderr, "\t-h: print this help message\n");
   43     fprintf(stderr, "\t-o origin: set origin to be used when "
   44             "interpreting the record\n");
   45     fprintf(stderr, "\t-p: print the record in canonical format\n");
   46     fprintf(stderr, "\t-C: list the supported class names\n");
   47     fprintf(stderr, "\t-P: list the supported private type names\n");
   48     fprintf(stderr, "\t-T: list the supported standard type names\n");
   49     fprintf(stderr, "\t-u: print the record in unknown record format\n");
   50     exit(0);
   51 }
   52 
   53 ISC_NORETURN static void
   54 fatal(const char *format, ...);
   55 
   56 static void
   57 fatal(const char *format, ...) {
   58     va_list args;
   59 
   60     fprintf(stderr, "named-rrchecker: ");
   61     va_start(args, format);
   62     vfprintf(stderr, format, args);
   63     va_end(args);
   64     fputc('\n', stderr);
   65     exit(1);
   66 }
   67 
   68 int
   69 main(int argc, char *argv[]) {
   70     isc_token_t token;
   71     isc_result_t result;
   72     int c;
   73     unsigned int options = 0;
   74     dns_rdatatype_t rdtype;
   75     dns_rdataclass_t rdclass;
   76     char text[256 * 1024];
   77     char data[64 * 1024];
   78     isc_buffer_t tbuf;
   79     isc_buffer_t dbuf;
   80     dns_rdata_t rdata = DNS_RDATA_INIT;
   81     bool doexit = false;
   82     bool once = false;
   83     bool print = false;
   84     bool unknown = false;
   85     unsigned int t;
   86     char *origin = NULL;
   87     dns_fixedname_t fixed;
   88     dns_name_t *name = NULL;
   89 
   90     while ((c = isc_commandline_parse(argc, argv, "ho:puCPT")) != -1) {
   91         switch (c) {
   92         case 'o':
   93             origin = isc_commandline_argument;
   94             break;
   95 
   96         case 'p':
   97             print = true;
   98             break;
   99 
  100         case 'u':
  101             unknown = true;
  102             break;
  103 
  104         case 'C':
  105             for (t = 1; t <= 0xfeffu; t++) {
  106                 if (dns_rdataclass_ismeta(t)) {
  107                     continue;
  108                 }
  109                 dns_rdataclass_format(t, text, sizeof(text));
  110                 if (strncmp(text, "CLASS", 4) != 0) {
  111                     fprintf(stdout, "%s\n", text);
  112                 }
  113             }
  114             exit(0);
  115 
  116         case 'P':
  117             for (t = 0xff00; t <= 0xfffeu; t++) {
  118                 if (dns_rdatatype_ismeta(t)) {
  119                     continue;
  120                 }
  121                 dns_rdatatype_format(t, text, sizeof(text));
  122                 if (strncmp(text, "TYPE", 4) != 0) {
  123                     fprintf(stdout, "%s\n", text);
  124                 }
  125             }
  126             doexit = true;
  127             break;
  128 
  129         case 'T':
  130             for (t = 1; t <= 0xfeffu; t++) {
  131                 if (dns_rdatatype_ismeta(t)) {
  132                     continue;
  133                 }
  134                 dns_rdatatype_format(t, text, sizeof(text));
  135                 if (strncmp(text, "TYPE", 4) != 0) {
  136                     fprintf(stdout, "%s\n", text);
  137                 }
  138             }
  139             doexit = true;
  140             break;
  141 
  142         case '?':
  143         case 'h':
  144             /* Does not return. */
  145             usage();
  146 
  147         default:
  148             fprintf(stderr, "%s: unhandled option -%c\n", argv[0],
  149                 isc_commandline_option);
  150             exit(1);
  151         }
  152     }
  153     if (doexit) {
  154         exit(0);
  155     }
  156 
  157     isc_mem_create(&mctx);
  158     RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
  159 
  160     /*
  161      * Set up to lex DNS master file.
  162      */
  163 
  164     specials['('] = 1;
  165     specials[')'] = 1;
  166     specials['"'] = 1;
  167     isc_lex_setspecials(lex, specials);
  168     options = ISC_LEXOPT_EOL;
  169     isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
  170 
  171     RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
  172 
  173     if (origin != NULL) {
  174         name = dns_fixedname_initname(&fixed);
  175         result = dns_name_fromstring(name, origin, 0, NULL);
  176         if (result != ISC_R_SUCCESS) {
  177             fatal("dns_name_fromstring: %s",
  178                   dns_result_totext(result));
  179         }
  180     }
  181 
  182     while ((result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER,
  183                       &token)) == ISC_R_SUCCESS)
  184     {
  185         if (token.type == isc_tokentype_eof) {
  186             break;
  187         }
  188         if (token.type == isc_tokentype_eol) {
  189             continue;
  190         }
  191         if (once) {
  192             fatal("extra data");
  193         }
  194         /*
  195          * Get class.
  196          */
  197         if (token.type == isc_tokentype_number) {
  198             rdclass = (dns_rdataclass_t)token.value.as_ulong;
  199             if (token.value.as_ulong > 0xffffu) {
  200                 fatal("class value too big %lu",
  201                       token.value.as_ulong);
  202             }
  203             if (dns_rdataclass_ismeta(rdclass)) {
  204                 fatal("class %lu is a meta value",
  205                       token.value.as_ulong);
  206             }
  207         } else if (token.type == isc_tokentype_string) {
  208             result = dns_rdataclass_fromtext(
  209                 &rdclass, &token.value.as_textregion);
  210             if (result != ISC_R_SUCCESS) {
  211                 fatal("dns_rdataclass_fromtext: %s",
  212                       dns_result_totext(result));
  213             }
  214             if (dns_rdataclass_ismeta(rdclass)) {
  215                 fatal("class %.*s(%d) is a meta value",
  216                       (int)token.value.as_textregion.length,
  217                       token.value.as_textregion.base, rdclass);
  218             }
  219         } else {
  220             fatal("unexpected token %u", token.type);
  221         }
  222 
  223         result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER,
  224                       &token);
  225         if (result != ISC_R_SUCCESS) {
  226             break;
  227         }
  228         if (token.type == isc_tokentype_eol) {
  229             continue;
  230         }
  231         if (token.type == isc_tokentype_eof) {
  232             break;
  233         }
  234 
  235         /*
  236          * Get type.
  237          */
  238         if (token.type == isc_tokentype_number) {
  239             rdtype = (dns_rdatatype_t)token.value.as_ulong;
  240             if (token.value.as_ulong > 0xffffu) {
  241                 fatal("type value too big %lu",
  242                       token.value.as_ulong);
  243             }
  244             if (dns_rdatatype_ismeta(rdtype)) {
  245                 fatal("type %lu is a meta value",
  246                       token.value.as_ulong);
  247             }
  248         } else if (token.type == isc_tokentype_string) {
  249             result = dns_rdatatype_fromtext(
  250                 &rdtype, &token.value.as_textregion);
  251             if (result != ISC_R_SUCCESS) {
  252                 fatal("dns_rdatatype_fromtext: %s",
  253                       dns_result_totext(result));
  254             }
  255             if (dns_rdatatype_ismeta(rdtype)) {
  256                 fatal("type %.*s(%d) is a meta value",
  257                       (int)token.value.as_textregion.length,
  258                       token.value.as_textregion.base, rdtype);
  259             }
  260         } else {
  261             fatal("unexpected token %u", token.type);
  262         }
  263 
  264         isc_buffer_init(&dbuf, data, sizeof(data));
  265         result = dns_rdata_fromtext(&rdata, rdclass, rdtype, lex, name,
  266                         0, mctx, &dbuf, NULL);
  267         if (result != ISC_R_SUCCESS) {
  268             fatal("dns_rdata_fromtext: %s",
  269                   dns_result_totext(result));
  270         }
  271         once = true;
  272     }
  273     if (result != ISC_R_EOF) {
  274         fatal("eof not found");
  275     }
  276     if (!once) {
  277         fatal("no records found");
  278     }
  279 
  280     if (print) {
  281         isc_buffer_init(&tbuf, text, sizeof(text));
  282         result = dns_rdataclass_totext(rdclass, &tbuf);
  283         if (result != ISC_R_SUCCESS) {
  284             fatal("dns_rdataclass_totext: %s",
  285                   dns_result_totext(result));
  286         }
  287         isc_buffer_putstr(&tbuf, "\t");
  288         result = dns_rdatatype_totext(rdtype, &tbuf);
  289         if (result != ISC_R_SUCCESS) {
  290             fatal("dns_rdatatype_totext: %s",
  291                   dns_result_totext(result));
  292         }
  293         isc_buffer_putstr(&tbuf, "\t");
  294         result = dns_rdata_totext(&rdata, NULL, &tbuf);
  295         if (result != ISC_R_SUCCESS) {
  296             fatal("dns_rdata_totext: %s",
  297                   dns_result_totext(result));
  298         }
  299 
  300         printf("%.*s\n", (int)tbuf.used, (char *)tbuf.base);
  301         fflush(stdout);
  302     }
  303 
  304     if (unknown) {
  305         isc_buffer_init(&tbuf, text, sizeof(text));
  306         result = dns_rdataclass_tounknowntext(rdclass, &tbuf);
  307         if (result != ISC_R_SUCCESS) {
  308             fatal("dns_rdataclass_tounknowntext: %s",
  309                   dns_result_totext(result));
  310         }
  311         isc_buffer_putstr(&tbuf, "\t");
  312         result = dns_rdatatype_tounknowntext(rdtype, &tbuf);
  313         if (result != ISC_R_SUCCESS) {
  314             fatal("dns_rdatatype_tounknowntext: %s",
  315                   dns_result_totext(result));
  316         }
  317         isc_buffer_putstr(&tbuf, "\t");
  318         result = dns_rdata_tofmttext(&rdata, NULL,
  319                          DNS_STYLEFLAG_UNKNOWNFORMAT, 0, 0,
  320                          "", &tbuf);
  321         if (result != ISC_R_SUCCESS) {
  322             fatal("dns_rdata_tofmttext: %sn",
  323                   dns_result_totext(result));
  324         }
  325 
  326         printf("%.*s\n", (int)tbuf.used, (char *)tbuf.base);
  327         fflush(stdout);
  328     }
  329 
  330     isc_lex_close(lex);
  331     isc_lex_destroy(&lex);
  332     isc_mem_destroy(&mctx);
  333     return (0);
  334 }