File: example-synchronous.c

package info (click to toggle)
getdns 1.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,752 kB
  • sloc: ansic: 57,351; sh: 407; xml: 38; makefile: 22
file content (66 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <assert.h>
#include <getdns_core_only.h>

int main()
{
	getdns_return_t  r; /* Holder for all function returns */
	getdns_context  *context    = NULL;
	getdns_dict     *response   = NULL;
	getdns_dict     *extensions = NULL;
	getdns_bindata  *address_data;
	char            *first = NULL, *second = NULL;

	/* Create the DNS context for this call */
	if ((r = getdns_context_create(&context, 1)))
		fprintf(stderr, "Trying to create the context failed");

	else if (!(extensions = getdns_dict_create()))
		fprintf(stderr, "Could not create extensions dict.\n");

	else if ((r = getdns_dict_set_int(extensions, "return_both_v4_and_v6", GETDNS_EXTENSION_TRUE)))
		fprintf(stderr, "Trying to set an extension do both IPv4 and IPv6 failed");

	else if ((r = getdns_general_sync(context, "example.com", GETDNS_RRTYPE_A, extensions, &response)))
		fprintf(stderr, "Error scheduling synchronous request");
	
	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 (!(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 (!(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);
	}
	/* Clean up */
	if (response)
		getdns_dict_destroy(response); 

	if (extensions)
		getdns_dict_destroy(extensions);

	if (context)
		getdns_context_destroy(context);

	if (r) {
		assert( r != GETDNS_RETURN_GOOD );

		fprintf(stderr, ": %d\n", r);
		exit(EXIT_FAILURE);
	}
	/* Assuming we get here, leave gracefully */
	exit(EXIT_SUCCESS);
}