"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/samples/sample-request.c" (4 Sep 2020, 6205 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 "sample-request.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 #ifndef WIN32
   13 #include <arpa/inet.h>
   14 #include <netdb.h>
   15 #include <netinet/in.h>
   16 #include <sys/socket.h>
   17 #include <sys/types.h>
   18 #include <unistd.h>
   19 #endif /* ifndef WIN32 */
   20 
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <string.h>
   24 
   25 #include <isc/attributes.h>
   26 #include <isc/base64.h>
   27 #include <isc/buffer.h>
   28 #include <isc/commandline.h>
   29 #include <isc/lib.h>
   30 #include <isc/mem.h>
   31 #include <isc/print.h>
   32 #include <isc/sockaddr.h>
   33 #include <isc/util.h>
   34 
   35 #include <dns/client.h>
   36 #include <dns/fixedname.h>
   37 #include <dns/keyvalues.h>
   38 #include <dns/lib.h>
   39 #include <dns/masterdump.h>
   40 #include <dns/message.h>
   41 #include <dns/name.h>
   42 #include <dns/rdata.h>
   43 #include <dns/rdataset.h>
   44 #include <dns/rdatastruct.h>
   45 #include <dns/rdatatype.h>
   46 #include <dns/result.h>
   47 #include <dns/secalg.h>
   48 
   49 #include <dst/dst.h>
   50 
   51 static isc_mem_t *mctx;
   52 static dns_fixedname_t fixedqname;
   53 
   54 ISC_NORETURN static void
   55 usage(void);
   56 
   57 static void
   58 usage(void) {
   59     fprintf(stderr, "sample-request [-t RRtype] server_address hostname\n");
   60 
   61     exit(1);
   62 }
   63 
   64 static isc_result_t
   65 make_querymessage(dns_message_t *message, const char *namestr,
   66           dns_rdatatype_t rdtype) {
   67     dns_name_t *qname = NULL, *qname0;
   68     dns_rdataset_t *qrdataset = NULL;
   69     isc_result_t result;
   70     isc_buffer_t b;
   71     unsigned int namelen;
   72 
   73     REQUIRE(message != NULL);
   74     REQUIRE(namestr != NULL);
   75 
   76     /* Construct qname */
   77     namelen = strlen(namestr);
   78     isc_buffer_constinit(&b, namestr, namelen);
   79     isc_buffer_add(&b, namelen);
   80     qname0 = dns_fixedname_initname(&fixedqname);
   81     result = dns_name_fromtext(qname0, &b, dns_rootname, 0, NULL);
   82     if (result != ISC_R_SUCCESS) {
   83         fprintf(stderr, "failed to convert qname: %u\n", result);
   84         return (result);
   85     }
   86 
   87     /* Construct query message */
   88     message->opcode = dns_opcode_query;
   89     message->rdclass = dns_rdataclass_in;
   90 
   91     result = dns_message_gettempname(message, &qname);
   92     if (result != ISC_R_SUCCESS) {
   93         goto cleanup;
   94     }
   95 
   96     result = dns_message_gettemprdataset(message, &qrdataset);
   97     if (result != ISC_R_SUCCESS) {
   98         goto cleanup;
   99     }
  100 
  101     dns_name_init(qname, NULL);
  102     dns_name_clone(qname0, qname);
  103     dns_rdataset_makequestion(qrdataset, message->rdclass, rdtype);
  104     ISC_LIST_APPEND(qname->list, qrdataset, link);
  105     dns_message_addname(message, qname, DNS_SECTION_QUESTION);
  106 
  107     return (ISC_R_SUCCESS);
  108 
  109 cleanup:
  110     if (qname != NULL) {
  111         dns_message_puttempname(message, &qname);
  112     }
  113     if (qrdataset != NULL) {
  114         dns_message_puttemprdataset(message, &qrdataset);
  115     }
  116     dns_message_destroy(&message);
  117     return (result);
  118 }
  119 
  120 static void
  121 print_section(dns_message_t *message, int section, isc_buffer_t *buf) {
  122     isc_result_t result;
  123     isc_region_t r;
  124 
  125     result = dns_message_sectiontotext(message, section,
  126                        &dns_master_style_full, 0, buf);
  127     if (result != ISC_R_SUCCESS) {
  128         goto fail;
  129     }
  130 
  131     isc_buffer_usedregion(buf, &r);
  132     printf("%.*s", (int)r.length, (char *)r.base);
  133 
  134     return;
  135 
  136 fail:
  137     fprintf(stderr, "failed to convert a section\n");
  138 }
  139 
  140 int
  141 main(int argc, char *argv[]) {
  142     int ch, i, gaierror;
  143     struct addrinfo hints, *res;
  144     isc_textregion_t tr;
  145     dns_client_t *client = NULL;
  146     isc_result_t result;
  147     isc_sockaddr_t sa;
  148     dns_message_t *qmessage, *rmessage;
  149     dns_rdatatype_t type = dns_rdatatype_a;
  150     isc_buffer_t *outputbuf;
  151 
  152     while ((ch = isc_commandline_parse(argc, argv, "t:")) != -1) {
  153         switch (ch) {
  154         case 't':
  155             tr.base = isc_commandline_argument;
  156             tr.length = strlen(isc_commandline_argument);
  157             result = dns_rdatatype_fromtext(&type, &tr);
  158             if (result != ISC_R_SUCCESS) {
  159                 fprintf(stderr, "invalid RRtype: %s\n",
  160                     isc_commandline_argument);
  161                 exit(1);
  162             }
  163             break;
  164         default:
  165             usage();
  166         }
  167     }
  168 
  169     argc -= isc_commandline_index;
  170     argv += isc_commandline_index;
  171     if (argc < 2) {
  172         usage();
  173     }
  174 
  175     isc_lib_register();
  176     result = dns_lib_init();
  177     if (result != ISC_R_SUCCESS) {
  178         fprintf(stderr, "dns_lib_init failed: %u\n", result);
  179         exit(1);
  180     }
  181 
  182     result = dns_client_create(&client, 0);
  183     if (result != ISC_R_SUCCESS) {
  184         fprintf(stderr, "dns_client_create failed: %u\n", result);
  185         exit(1);
  186     }
  187 
  188     /* Prepare message structures */
  189     mctx = NULL;
  190     qmessage = NULL;
  191     rmessage = NULL;
  192 
  193     isc_mem_create(&mctx);
  194     result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &qmessage);
  195     if (result == ISC_R_SUCCESS) {
  196         result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
  197                         &rmessage);
  198     }
  199     if (result != ISC_R_SUCCESS) {
  200         fprintf(stderr, "failed to create messages\n");
  201         exit(1);
  202     }
  203 
  204     /* Initialize the nameserver address */
  205     memset(&hints, 0, sizeof(hints));
  206     hints.ai_family = AF_UNSPEC;
  207     hints.ai_socktype = SOCK_DGRAM;
  208     hints.ai_protocol = IPPROTO_UDP;
  209 #ifdef AI_NUMERICHOST
  210     hints.ai_flags = AI_NUMERICHOST;
  211 #endif /* ifdef AI_NUMERICHOST */
  212     gaierror = getaddrinfo(argv[0], "53", &hints, &res);
  213     if (gaierror != 0) {
  214         fprintf(stderr, "getaddrinfo failed: %s\n",
  215             gai_strerror(gaierror));
  216         exit(1);
  217     }
  218     INSIST(res->ai_addrlen <= sizeof(sa.type));
  219     memmove(&sa.type, res->ai_addr, res->ai_addrlen);
  220     freeaddrinfo(res);
  221     sa.length = (unsigned int)res->ai_addrlen;
  222     ISC_LINK_INIT(&sa, link);
  223 
  224     /* Construct qname */
  225     result = make_querymessage(qmessage, argv[1], type);
  226     if (result != ISC_R_SUCCESS) {
  227         fprintf(stderr, "failed to create a query\n");
  228         exit(1);
  229     }
  230 
  231     /* Send request and wait for a response */
  232     result = dns_client_request(client, qmessage, rmessage, &sa, 0, 0, NULL,
  233                     60, 0, 3);
  234     if (result != ISC_R_SUCCESS) {
  235         fprintf(stderr, "failed to get a response: %s\n",
  236             dns_result_totext(result));
  237     }
  238 
  239     /* Dump the response */
  240     outputbuf = NULL;
  241     isc_buffer_allocate(mctx, &outputbuf, 65535);
  242     for (i = 0; i < DNS_SECTION_MAX; i++) {
  243         print_section(rmessage, i, outputbuf);
  244         isc_buffer_clear(outputbuf);
  245     }
  246     isc_buffer_free(&outputbuf);
  247 
  248     /* Cleanup */
  249     dns_message_destroy(&qmessage);
  250     dns_message_destroy(&rmessage);
  251     isc_mem_destroy(&mctx);
  252     dns_client_destroy(&client);
  253     dns_lib_shutdown();
  254 
  255     return (0);
  256 }