2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/dns/gnunet-dns-redirector.c
23 * @brief Tool to change DNS replies (for testing)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
33 * Handle to DNS service.
35 static struct GNUNET_DNS_Handle *handle;
38 * New target for A records.
43 * New target for AAAA records.
48 * Global return value (0 success).
53 * Selected level of verbosity.
59 * Modify the given DNS record.
61 * @param record record to modify
64 modify_record (const struct GNUNET_DNSPARSER_Record *record)
66 char buf[INET6_ADDRSTRLEN];
70 case GNUNET_DNSPARSER_TYPE_A:
71 if (record->data.raw.data_len != sizeof (struct in_addr))
77 "Changing A record from `%s' to `%s'\n",
78 inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf)),
80 GNUNET_assert (1 == inet_pton (AF_INET, n4, record->data.raw.data));
83 case GNUNET_DNSPARSER_TYPE_AAAA:
84 if (record->data.raw.data_len != sizeof (struct in6_addr))
90 "Changing AAAA record from `%s' to `%s'\n",
91 inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf)),
93 GNUNET_assert (1 == inet_pton (AF_INET6, n6, record->data.raw.data));
96 case GNUNET_DNSPARSER_TYPE_NS:
97 case GNUNET_DNSPARSER_TYPE_CNAME:
98 case GNUNET_DNSPARSER_TYPE_PTR:
99 case GNUNET_DNSPARSER_TYPE_SOA:
100 case GNUNET_DNSPARSER_TYPE_MX:
101 case GNUNET_DNSPARSER_TYPE_TXT:
110 * Signature of a function that is called whenever the DNS service
111 * encounters a DNS request and needs to do something with it. The
112 * function has then the chance to generate or modify the response by
113 * calling one of the three "GNUNET_DNS_request_*" continuations.
115 * When a request is intercepted, this function is called first to
116 * give the client a chance to do the complete address resolution;
117 * "rdata" will be NULL for this first call for a DNS request, unless
118 * some other client has already filled in a response.
120 * If multiple clients exist, all of them are called before the global
121 * DNS. The global DNS is only called if all of the clients'
122 * functions call GNUNET_DNS_request_forward. Functions that call
123 * GNUNET_DNS_request_forward will be called again before a final
124 * response is returned to the application. If any of the clients'
125 * functions call GNUNET_DNS_request_drop, the response is dropped.
128 * @param rh request handle to user for reply
129 * @param request_length number of bytes in request
130 * @param request udp payload of the DNS request
133 modify_request (void *cls,
134 struct GNUNET_DNS_RequestHandle *rh,
135 size_t request_length,
138 struct GNUNET_DNSPARSER_Packet *p;
144 p = GNUNET_DNSPARSER_parse (request, request_length);
147 fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
148 GNUNET_DNS_request_forward (rh);
151 for (i=0;i<p->num_answers;i++)
152 modify_record (&p->answers[i]);
154 ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len);
155 GNUNET_DNSPARSER_free_packet (p);
156 if (GNUNET_OK != ret)
158 if (GNUNET_NO == ret)
160 "Modified DNS response did not fit, keeping old response\n");
162 GNUNET_break (0); /* our modifications should have been sane! */
163 GNUNET_DNS_request_forward (rh);
169 "Injecting modified DNS response\n");
170 GNUNET_DNS_request_answer (rh, len, buf);
172 GNUNET_free_non_null (buf);
180 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 GNUNET_DNS_disconnect (handle);
191 * Main function that will be run by the scheduler.
194 * @param args remaining command-line arguments
195 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
196 * @param cfg configuration
199 run (void *cls, char *const *args, const char *cfgfile,
200 const struct GNUNET_CONFIGURATION_Handle *cfg)
205 (1 != inet_pton (AF_INET, n4, &i4)) )
208 "`%s' is nto a valid IPv4 address!\n",
213 (1 != inet_pton (AF_INET6, n6, &i6)) )
216 "`%s' is nto a valid IPv6 address!\n",
222 GNUNET_DNS_connect (cfg,
223 GNUNET_DNS_FLAG_POST_RESOLUTION,
226 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
227 &do_disconnect, NULL);
232 main (int argc, char *const *argv)
234 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
235 {'4', "ipv4", "IPV4",
236 gettext_noop ("set A records"),
237 1, &GNUNET_GETOPT_set_string, &n4},
238 {'6', "ipv4", "IPV6",
239 gettext_noop ("set AAAA records"),
240 1, &GNUNET_GETOPT_set_string, &n6},
241 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
242 GNUNET_GETOPT_OPTION_END
245 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
249 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector",
251 ("Change DNS replies to point elsewhere."), options,
252 &run, NULL)) ? ret : 1;
253 GNUNET_free ((void*) argv);
258 /* end of gnunet-dns-redirector.c */