2 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: sample-request.c,v 1.5 2009/09/29 15:06:07 fdupont Exp $ */
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
26 #include <arpa/inet.h>
34 #include <isc/base64.h>
35 #include <isc/buffer.h>
38 #include <isc/sockaddr.h>
41 #include <dns/client.h>
42 #include <dns/fixedname.h>
43 #include <dns/keyvalues.h>
45 #include <dns/masterdump.h>
46 #include <dns/message.h>
48 #include <dns/rdata.h>
49 #include <dns/rdataset.h>
50 #include <dns/rdatastruct.h>
51 #include <dns/rdatatype.h>
52 #include <dns/result.h>
53 #include <dns/secalg.h>
57 static isc_mem_t *mctx;
58 static dns_fixedname_t fixedqname;
60 ISC_PLATFORM_NORETURN_PRE static void
61 usage(void) ISC_PLATFORM_NORETURN_POST;
65 fprintf(stderr, "sample-request [-t RRtype] server_address hostname\n");
71 make_querymessage(dns_message_t *message, const char *namestr,
72 dns_rdatatype_t rdtype)
74 dns_name_t *qname = NULL, *qname0;
75 dns_rdataset_t *qrdataset = NULL;
81 namelen = strlen(namestr);
82 isc_buffer_init(&b, namestr, namelen);
83 isc_buffer_add(&b, namelen);
84 dns_fixedname_init(&fixedqname);
85 qname0 = dns_fixedname_name(&fixedqname);
86 result = dns_name_fromtext(qname0, &b, dns_rootname, 0, NULL);
87 if (result != ISC_R_SUCCESS) {
88 fprintf(stderr, "failed to convert qname: %d\n", result);
92 /* Construct query message */
93 message->opcode = dns_opcode_query;
94 message->rdclass = dns_rdataclass_in;
96 result = dns_message_gettempname(message, &qname);
97 if (result != ISC_R_SUCCESS)
100 result = dns_message_gettemprdataset(message, &qrdataset);
101 if (result != ISC_R_SUCCESS)
104 dns_name_init(qname, NULL);
105 dns_name_clone(qname0, qname);
106 dns_rdataset_init(qrdataset);
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);
111 return (ISC_R_SUCCESS);
115 dns_message_puttempname(message, &qname);
116 if (qrdataset != NULL)
117 dns_message_puttemprdataset(message, &qrdataset);
119 dns_message_destroy(&message);
124 print_section(dns_message_t *message, int section, isc_buffer_t *buf) {
128 result = dns_message_sectiontotext(message, section,
129 &dns_master_style_full, 0, buf);
130 if (result != ISC_R_SUCCESS)
133 isc_buffer_usedregion(buf, &r);
134 printf("%.*s", (int)r.length, (char *)r.base);
139 fprintf(stderr, "failed to convert a section\n");
143 main(int argc, char *argv[]) {
144 int ch, i, gai_error;
145 struct addrinfo hints, *res;
147 dns_client_t *client = NULL;
150 dns_message_t *qmessage, *rmessage;
151 dns_rdatatype_t type = dns_rdatatype_a;
152 isc_buffer_t *outputbuf;
154 while ((ch = getopt(argc, argv, "t:")) != -1) {
158 tr.length = strlen(optarg);
159 result = dns_rdatatype_fromtext(&type, &tr);
160 if (result != ISC_R_SUCCESS) {
162 "invalid RRtype: %s\n", optarg);
177 result = dns_lib_init();
178 if (result != ISC_R_SUCCESS) {
179 fprintf(stderr, "dns_lib_init failed: %d\n", result);
183 result = dns_client_create(&client, 0);
184 if (result != ISC_R_SUCCESS) {
185 fprintf(stderr, "dns_client_create failed: %d\n", result);
189 /* Prepare message structures */
194 result = isc_mem_create(0, 0, &mctx);
195 if (result != ISC_R_SUCCESS) {
196 fprintf(stderr, "failed to create a memory context\n");
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,
204 if (result != ISC_R_SUCCESS) {
205 fprintf(stderr, "failed to create messages\n");
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 hints.ai_flags = AI_NUMERICHOST;
215 gai_error = getaddrinfo(argv[0], "53", &hints, &res);
216 if (gai_error != 0) {
217 fprintf(stderr, "getaddrinfo failed: %s\n",
218 gai_strerror(gai_error));
221 INSIST(res->ai_addrlen <= sizeof(sa.type));
222 memcpy(&sa.type, res->ai_addr, res->ai_addrlen);
224 sa.length = res->ai_addrlen;
225 ISC_LINK_INIT(&sa, link);
227 /* Construct qname */
228 result = make_querymessage(qmessage, argv[1], type);
229 if (result != ISC_R_SUCCESS) {
230 fprintf(stderr, "failed to create a query\n");
234 /* Send request and wait for a response */
235 result = dns_client_request(client, qmessage, rmessage, &sa, 0, 0,
237 if (result != ISC_R_SUCCESS) {
238 fprintf(stderr, "failed to get a response: %s\n",
239 dns_result_totext(result));
242 /* Dump the response */
244 result = isc_buffer_allocate(mctx, &outputbuf, 65535);
245 if (result != ISC_R_SUCCESS) {
246 fprintf(stderr, "failed to allocate a result buffer\n");
249 for (i = 0; i < DNS_SECTION_MAX; i++) {
250 print_section(rmessage, i, outputbuf);
251 isc_buffer_clear(outputbuf);
253 isc_buffer_free(&outputbuf);
256 dns_message_destroy(&qmessage);
257 dns_message_destroy(&rmessage);
258 isc_mem_destroy(&mctx);
259 dns_client_destroy(&client);