Merge branch 'dnssec'
[strongswan.git] / scripts / dnssec.c
1 /*
2 * Copyright (C) 2011-2012 Reto Guadagnini
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17
18 #include <library.h>
19
20 int main(int argc, char *argv[])
21 {
22 resolver_t *resolver;
23 resolver_response_t *response;
24 enumerator_t *enumerator;
25 rr_set_t *rrset;
26 rr_t *rr;
27 chunk_t chunk;
28
29 library_init(NULL);
30 atexit(library_deinit);
31 if (!lib->plugins->load(lib->plugins, NULL, PLUGINS))
32 {
33 return 1;
34 }
35 if (argc != 2)
36 {
37 fprintf(stderr, "usage: %s <name>\n", argv[0]);
38 return 1;
39 }
40
41 resolver = lib->resolver->create(lib->resolver);
42 if (!resolver)
43 {
44 printf("failed to create a resolver!\n");
45 return 1;
46 }
47
48 response = resolver->query(resolver, argv[1], RR_CLASS_IN, RR_TYPE_A);
49 if (!response)
50 {
51 printf("no response received!\n");
52 resolver->destroy(resolver);
53 return 1;
54 }
55
56 printf("DNS response:\n");
57 if (!response->has_data(response) || !response->query_name_exist(response))
58 {
59 if (!response->has_data(response))
60 {
61 printf(" no data in the response\n");
62 }
63 if (!response->query_name_exist(response))
64 {
65 printf(" query name does not exist\n");
66 }
67 response->destroy(response);
68 resolver->destroy(resolver);
69 return 1;
70 }
71
72 printf(" RRs in the response:\n");
73 rrset = response->get_rr_set(response);
74 if (!rrset)
75 {
76 printf(" response contains no RRset!\n");
77 response->destroy(response);
78 resolver->destroy(resolver);
79 return 1;
80 }
81
82 enumerator = rrset->create_rr_enumerator(rrset);
83 while (enumerator->enumerate(enumerator, &rr))
84 {
85 printf(" name: ");
86 printf(rr->get_name(rr));
87 printf("\n");
88 }
89
90 enumerator = rrset->create_rrsig_enumerator(rrset);
91 if (enumerator)
92 {
93 printf(" RRSIGs for the RRset:\n");
94 while (enumerator->enumerate(enumerator, &rr))
95 {
96 printf(" name: ");
97 printf(rr->get_name(rr));
98 printf("\n RDATA: ");
99 chunk = rr->get_rdata(rr);
100 chunk = chunk_to_hex(chunk, NULL, TRUE);
101 printf(chunk.ptr);
102 printf("\n");
103 }
104 }
105
106 printf(" security status of the response: ");
107 switch (response->get_security_state(response))
108 {
109 case SECURE:
110 printf("SECURE\n\n");
111 break;
112 case INSECURE:
113 printf("INSECURE\n\n");
114 break;
115 case BOGUS:
116 printf("BOGUS\n\n");
117 break;
118 case INDETERMINATE:
119 printf("INDETERMINATE\n\n");
120 break;
121 }
122 response->destroy(response);
123 resolver->destroy(resolver);
124 return 0;
125 }