"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/samples/sample-request.c" (7 Sep 2020, 6406 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


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 and the last Fossies "Diffs" side-by-side code changes report: 9.17.1_vs_9.17.2.

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