summaryrefslogtreecommitdiff
path: root/tools/dnssec/net2wire.c
blob: f97483f53578a5fa68d9f5724cd6571acc570a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* This program is based on (IIRC)
 * getdns/spec/example/example-simple-answers.c.
 *
 * Resolve DNS name argv[1] of type argv[2] using libgetdns and write
 * the resulting RRs to two files, in DNS binary wire format:
 *
 * - binout: /just_address_answers/0/address_data and
 *           /just_address_answers/1/address_data
 *
 * - treeout: output from getdns_rr_dict2wire for all RR's in the
 *            answer section
 *
 * Based on getdns/spec/example/example-simple-answers.c.
 */

#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <getdns/getdns.h>
#include <getdns/getdns_ext_libevent.h>
#include <event2/event.h>

static int debug = 1;

static getdns_return_t
dump_reply(FILE *fp, getdns_dict *reply, const char *section_name)
{
	getdns_list *section = NULL;
	size_t section_len = -1;
	getdns_return_t r;
	uint8_t res_buf[4096], *res = NULL;
	size_t res_len = sizeof(res_buf);

	r = getdns_dict_get_list(reply, section_name, &section);
	if (r) {
		fprintf(stderr,
			"unable to get section \"%s\" from reply\n",
			section_name);
		return r;
	}

	r = getdns_list_get_length(section, &section_len);
	if (r) {
		fprintf(stderr, "unable to get length of section\n");
		return r;
	}

	for (size_t j = 0; j < section_len; j++) {
		getdns_dict *rr = NULL;

		r = getdns_list_get_dict(section, j , &rr);
		if (r) {
			fprintf(stderr, "unable to get rr from entry "
				"%d: %d\n", j, r);
			return r;
		}

		r = getdns_rr_dict2wire(rr, &res, &res_len);
		if (r) {
			fprintf(stderr,
				"unable to convert entry %d "
				"to wire format: %d\n", j, r);
			return r;
		}

		if (0 && debug) {
			char *s = getdns_pretty_print_dict(rr);
			puts(s);
			free(s);
		}

		if (fwrite(res, 1, res_len, fp) != res_len) {
			fprintf(stderr,
				"unable to write buffer to file: %s\n",
				strerror(errno));
			return -errno;
		}

		free(res);
	}

	return 0;
}

int
dump_tree(FILE *fp, const getdns_dict *response, const char *tree_name)
{
	getdns_return_t r;
	getdns_list *tree = NULL;
	size_t n_replies = -1;

        r = getdns_dict_get_list(response, tree_name, &tree);
	if (r) {
		fprintf(stderr, "unable to get tree %s\n", tree_name);
		return r;
	}

	r = getdns_list_get_length(tree, &n_replies);
	if (r) {
		fprintf(stderr, "unable to get number of replies\n");
		return r;
	}

	for (size_t i = 0; i < n_replies; i++) {
		getdns_dict *reply = NULL;

		r = getdns_list_get_dict(tree, i, &reply);
		if (r) {
			fprintf(stderr, "unable to get reply %d from tree\n", i);
			return r;
		}

		if (debug) {
			char *s = getdns_pretty_print_dict(reply);
			printf("Pretty-printing reply #%d:%s\n", i, s);
			free(s);
		}

		dump_reply(fp, reply, "answer");
	}

	return 0;
}


/* Set up the callback function, which will also do the processing of the results */
void callback(getdns_context        *context,
              getdns_callback_type_t callback_type,
              getdns_dict           *response,
              void                  *userarg,
              getdns_transaction_t   transaction_id)
{
	getdns_return_t r;  /* Holder for all function returns */
	uint32_t        status;
	getdns_bindata  *address_data;
	char            *first = NULL, *second = NULL;
	FILE *binoutfp = NULL, *treeoutfp = NULL;

	(void) context; /* unused parameter */

	binoutfp = fopen("binout", "w");
	assert(binoutfp != NULL);
	treeoutfp = fopen("treeout", "w");
	assert(treeoutfp != NULL);

	printf( "Callback for query \"%s\" with request ID %"PRIu64".\n"
	      , (char *)userarg, transaction_id );

	switch(callback_type) {
	case GETDNS_CALLBACK_CANCEL:
		printf("Transaction with ID %"PRIu64" was cancelled.\n", transaction_id);
		return;
	case GETDNS_CALLBACK_TIMEOUT:
		printf("Transaction with ID %"PRIu64" timed out.\n", transaction_id);
		return;
	case GETDNS_CALLBACK_ERROR:
		printf("An error occurred for transaction ID %"PRIu64".\n", transaction_id);
		return;
	default: break;
	}
	assert( callback_type == GETDNS_CALLBACK_COMPLETE );

	if ((r = getdns_dict_get_int(response, "status", &status)))
		fprintf(stderr, "Could not get \"status\" from reponse");

	else if (status != GETDNS_RESPSTATUS_GOOD)
		fprintf(stderr, "The search had no results, and a return value of %"PRIu32".\n", status);

	static char *TREES[] = {"replies_tree", "validation_chain"};
	static const int TREES_LEN = 2;
	for (int i = 0; i < TREES_LEN; i++) {
		if (dump_tree(treeoutfp, response, TREES[i]))
			fprintf(stderr, "Could not dump %s to file\n", TREES[i]);

		else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/0/address_data", &address_data)))
			fprintf(stderr, "Could not get first address");

		else if (fwrite(address_data->data, 1, address_data->size, binoutfp) !=
			 address_data->size)
			fprintf(stderr, "first fwrite: %s\n", strerror(errno));

		else if (!(first = getdns_display_ip_address(address_data)))
			fprintf(stderr, "Could not convert first address to string\n");

		else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/1/address_data", &address_data)))
			fprintf(stderr, "Could not get second address");

		else if (fwrite(address_data->data, 1, address_data->size, binoutfp) !=
			 address_data->size)
			fprintf(stderr, "second fwrite: %s\n", strerror(errno));

		else if (!(second = getdns_display_ip_address(address_data)))
			fprintf(stderr, "Could not convert second address to string\n");
		if (first) {
			printf("The address is %s\n", first);
			free(first);
		}
		if (second) {
			printf("The address is %s\n", second);
			free(second);
		}
		if (r) {
			assert( r != GETDNS_RETURN_GOOD );
			fprintf(stderr, ": %d\n", r);
		}
	}
	getdns_dict_destroy(response);
	if (binoutfp) fclose(binoutfp);
	if (treeoutfp) fclose(treeoutfp);
}

static getdns_return_t
send_query(getdns_context *context,
	   char *query_name,
	   const uint16_t query_type,
	   getdns_transaction_t *transaction_id)
{
	getdns_return_t r = GETDNS_RETURN_GENERIC_ERROR;
	getdns_dict *extensions = getdns_dict_create();
	char *userarg = query_name;

	assert(extensions != NULL);

	r = getdns_dict_set_int(extensions,
				"dnssec_return_validation_chain",
				GETDNS_EXTENSION_TRUE);
	if (r) return r;

#if 0
	r = getdns_dict_set_int(extensions,
				"dnssec_return_status",
				GETDNS_EXTENSION_TRUE);
	if (r) return r;
#endif

	if (query_type == 0)
		r = getdns_address(context, query_name, extensions, userarg,
				   transaction_id, callback);
	else
		r = getdns_general(context, query_name, query_type,
				   extensions, userarg,
				   transaction_id, callback);
	if (r) return r;

	return r;
}

#define OWN_ROOT

#ifdef OWN_ROOT
getdns_list *
read_own_rootfile()
{
  FILE *fp = fopen("root.txt", "r");
  getdns_list *list = NULL;

  if (fp && !getdns_fp2rr_list(fp, &list, ".", 0))
    return list;
  return NULL;
}
#endif

int main(int argc, char *argv[])
{
	getdns_return_t      r;  /* Holder for all function returns */
	getdns_context      *context    = NULL;
	struct event_base   *event_base = NULL;
	char                *query_name = "www.example.com";
	uint16_t            query_type = 0;
	getdns_transaction_t transaction_id;
	getdns_list *trust_anchors;
        getdns_list *root_servers = NULL;


	if (argc > 1)
		query_name = argv[1];
	if (argc > 2)
		query_type = (uint16_t) atoi(argv[2]);

	if ((r = getdns_context_create(&context, 0)))
		fprintf(stderr, "Trying to create the context failed");

#ifdef OWN_ROOT
        else if (!(root_servers = read_own_rootfile()))
                fprintf(stderr, "Reading root hints file failed.\n");
#endif

        else if ((r = getdns_context_set_dns_root_servers(context,
                                                          root_servers)))
                fprintf(stderr, "Setting root name servers failed.\n");

	else if ((r = getdns_context_get_dnssec_trust_anchors(
			  context, &trust_anchors)) ||
		 trust_anchors == NULL)
		fprintf(stderr, "No trust anchor.\n");

	else if (!(event_base = event_base_new()))
		fprintf(stderr, "Trying to create the event base failed.\n");

	else if ((r = getdns_extension_set_libevent_base(context, event_base)))
		fprintf(stderr, "Setting the event base failed");

	else if ((r = send_query(context, query_name, query_type,
				 &transaction_id)))
		fprintf(stderr, "Queueing query failed: %d\n", r);

	else {
		printf("Request with transaction ID %"PRIu64" scheduled.\n",
		       transaction_id);
		if (event_base_dispatch(event_base) < 0)
			fprintf(stderr, "Error dispatching events\n");
	}

	/* Clean up */
	if (event_base)
		event_base_free(event_base);

	if (context)
		getdns_context_destroy(context);

	/* Assuming we get here, leave gracefully */
	exit(EXIT_SUCCESS);
}