diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-05 13:04:39 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-05 13:04:39 +0000 |
commit | de86c6645d4c6c757ef50701d5d873000449b04e (patch) | |
tree | 06130c4ff65072929b524382c80814935acdfca0 /src/dns/gnunet-dns-redirector.c | |
parent | 1f971ba86a238c1046587889efd92f62cc9afb9f (diff) | |
download | gnunet-de86c6645d4c6c757ef50701d5d873000449b04e.tar.gz gnunet-de86c6645d4c6c757ef50701d5d873000449b04e.zip |
-adding tool to test dnsparser.c
Diffstat (limited to 'src/dns/gnunet-dns-redirector.c')
-rw-r--r-- | src/dns/gnunet-dns-redirector.c | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/src/dns/gnunet-dns-redirector.c b/src/dns/gnunet-dns-redirector.c new file mode 100644 index 000000000..bde869816 --- /dev/null +++ b/src/dns/gnunet-dns-redirector.c | |||
@@ -0,0 +1,207 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/dns/gnunet-dns-redirector.c | ||
23 | * @brief Tool to change DNS replies (for testing) | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_dns_service-new.h" | ||
30 | #include "gnunet_dnsparser_lib.h" | ||
31 | |||
32 | /** | ||
33 | * Handle to transport service. | ||
34 | */ | ||
35 | static struct GNUNET_DNS_Handle *handle; | ||
36 | |||
37 | /** | ||
38 | * New target for A records. | ||
39 | */ | ||
40 | static char *n4; | ||
41 | |||
42 | /** | ||
43 | * New target for AAAA records. | ||
44 | */ | ||
45 | static char *n6; | ||
46 | |||
47 | /** | ||
48 | * Global return value (0 success). | ||
49 | */ | ||
50 | static int ret; | ||
51 | |||
52 | /** | ||
53 | * Selected level of verbosity. | ||
54 | */ | ||
55 | static int verbosity; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Output the given DNS query to stdout. | ||
60 | * | ||
61 | * @param query query to display. | ||
62 | */ | ||
63 | static void | ||
64 | modify_record (const struct GNUNET_DNSPARSER_Record *record) | ||
65 | { | ||
66 | switch (record->type) | ||
67 | { | ||
68 | case GNUNET_DNSPARSER_TYPE_A: | ||
69 | if (record->data.raw.data_len != sizeof (struct in_addr)) | ||
70 | return; | ||
71 | if (NULL != n4) | ||
72 | inet_pton (AF_INET, n4, record->data.raw.data); | ||
73 | break; | ||
74 | case GNUNET_DNSPARSER_TYPE_AAAA: | ||
75 | if (record->data.raw.data_len != sizeof (struct in6_addr)) | ||
76 | return; | ||
77 | if (NULL != n6) | ||
78 | inet_pton (AF_INET6, n6, record->data.raw.data); | ||
79 | break; | ||
80 | case GNUNET_DNSPARSER_TYPE_NS: | ||
81 | case GNUNET_DNSPARSER_TYPE_CNAME: | ||
82 | case GNUNET_DNSPARSER_TYPE_PTR: | ||
83 | case GNUNET_DNSPARSER_TYPE_SOA: | ||
84 | case GNUNET_DNSPARSER_TYPE_MX: | ||
85 | case GNUNET_DNSPARSER_TYPE_TXT: | ||
86 | break; | ||
87 | default: | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | |||
92 | |||
93 | /** | ||
94 | * Signature of a function that is called whenever the DNS service | ||
95 | * encounters a DNS request and needs to do something with it. The | ||
96 | * function has then the chance to generate or modify the response by | ||
97 | * calling one of the three "GNUNET_DNS_request_*" continuations. | ||
98 | * | ||
99 | * When a request is intercepted, this function is called first to | ||
100 | * give the client a chance to do the complete address resolution; | ||
101 | * "rdata" will be NULL for this first call for a DNS request, unless | ||
102 | * some other client has already filled in a response. | ||
103 | * | ||
104 | * If multiple clients exist, all of them are called before the global | ||
105 | * DNS. The global DNS is only called if all of the clients' | ||
106 | * functions call GNUNET_DNS_request_forward. Functions that call | ||
107 | * GNUNET_DNS_request_forward will be called again before a final | ||
108 | * response is returned to the application. If any of the clients' | ||
109 | * functions call GNUNET_DNS_request_drop, the response is dropped. | ||
110 | * | ||
111 | * @param cls closure | ||
112 | * @param rh request handle to user for reply | ||
113 | * @param request_length number of bytes in request | ||
114 | * @param request udp payload of the DNS request | ||
115 | */ | ||
116 | static void | ||
117 | modify_request (void *cls, | ||
118 | struct GNUNET_DNS_RequestHandle *rh, | ||
119 | size_t request_length, | ||
120 | const char *request) | ||
121 | { | ||
122 | struct GNUNET_DNSPARSER_Packet *p; | ||
123 | unsigned int i; | ||
124 | char *buf; | ||
125 | size_t len; | ||
126 | int ret; | ||
127 | |||
128 | p = GNUNET_DNSPARSER_parse (request, request_length); | ||
129 | if (NULL == p) | ||
130 | { | ||
131 | fprintf (stderr, "Received malformed DNS packet!\n"); | ||
132 | // FIXME: drop instead? | ||
133 | GNUNET_DNS_request_forward (rh); | ||
134 | return; | ||
135 | } | ||
136 | for (i=0;i<p->num_answers;i++) | ||
137 | modify_record (&p->answers[i]); | ||
138 | buf = NULL; | ||
139 | ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len); | ||
140 | GNUNET_DNSPARSER_free_packet (p); | ||
141 | fprintf (stderr, "PACK: %d\n", ret); | ||
142 | if (GNUNET_OK != ret) | ||
143 | GNUNET_DNS_request_forward (rh); | ||
144 | else | ||
145 | GNUNET_DNS_request_answer (rh, len, buf); | ||
146 | GNUNET_free_non_null (buf); | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Shutdown. | ||
152 | */ | ||
153 | static void | ||
154 | do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
155 | { | ||
156 | if (NULL != handle) | ||
157 | { | ||
158 | GNUNET_DNS_disconnect (handle); | ||
159 | handle = NULL; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * Main function that will be run by the scheduler. | ||
166 | * | ||
167 | * @param cls closure | ||
168 | * @param args remaining command-line arguments | ||
169 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
170 | * @param cfg configuration | ||
171 | */ | ||
172 | static void | ||
173 | run (void *cls, char *const *args, const char *cfgfile, | ||
174 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
175 | { | ||
176 | handle = | ||
177 | GNUNET_DNS_connect (cfg, | ||
178 | GNUNET_DNS_FLAG_POST_RESOLUTION, | ||
179 | &modify_request, | ||
180 | NULL); | ||
181 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
182 | &do_disconnect, NULL); | ||
183 | } | ||
184 | |||
185 | |||
186 | int | ||
187 | main (int argc, char *const *argv) | ||
188 | { | ||
189 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
190 | {'4', "ipv4", "IPV4", | ||
191 | gettext_noop ("set A records"), | ||
192 | 1, &GNUNET_GETOPT_set_string, &n4}, | ||
193 | {'6', "ipv4", "IPV6", | ||
194 | gettext_noop ("set AAAA records"), | ||
195 | 1, &GNUNET_GETOPT_set_string, &n6}, | ||
196 | GNUNET_GETOPT_OPTION_VERBOSE (&verbosity), | ||
197 | GNUNET_GETOPT_OPTION_END | ||
198 | }; | ||
199 | return (GNUNET_OK == | ||
200 | GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector", | ||
201 | gettext_noop | ||
202 | ("Change DNS replies to point elsewhere."), options, | ||
203 | &run, NULL)) ? ret : 1; | ||
204 | } | ||
205 | |||
206 | |||
207 | /* end of gnunet-dns-redirector.c */ | ||