error handling
[oweals/gnunet.git] / src / dns / gnunet-dns-redirector.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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.h"
30 #include "gnunet_dnsparser_lib.h"
31
32 /**
33  * Handle to DNS 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 unsigned int verbosity;
56
57
58 /**
59  * Modify the given DNS record.
60  *
61  * @param record record to modify
62  */
63 static void
64 modify_record (const struct GNUNET_DNSPARSER_Record *record)
65 {
66   char buf[INET6_ADDRSTRLEN];
67
68   switch (record->type)
69   {
70   case GNUNET_DNSPARSER_TYPE_A:
71     if (record->data.raw.data_len != sizeof(struct in_addr))
72       return;
73     if (NULL != n4)
74     {
75       if (verbosity > 1)
76         fprintf (stderr,
77                  "Changing A record from `%s' to `%s'\n",
78                  inet_ntop (AF_INET, record->data.raw.data, buf, sizeof(buf)),
79                  n4);
80       GNUNET_assert (1 == inet_pton (AF_INET, n4, record->data.raw.data));
81     }
82     break;
83
84   case GNUNET_DNSPARSER_TYPE_AAAA:
85     if (record->data.raw.data_len != sizeof(struct in6_addr))
86       return;
87     if (NULL != n6)
88     {
89       if (verbosity > 1)
90         fprintf (stderr,
91                  "Changing AAAA record from `%s' to `%s'\n",
92                  inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof(buf)),
93                  n6);
94       GNUNET_assert (1 == inet_pton (AF_INET6, n6, record->data.raw.data));
95     }
96     break;
97
98   case GNUNET_DNSPARSER_TYPE_NS:
99   case GNUNET_DNSPARSER_TYPE_CNAME:
100   case GNUNET_DNSPARSER_TYPE_PTR:
101   case GNUNET_DNSPARSER_TYPE_SOA:
102   case GNUNET_DNSPARSER_TYPE_MX:
103   case GNUNET_DNSPARSER_TYPE_TXT:
104     break;
105
106   default:
107     break;
108   }
109 }
110
111
112 /**
113  * Signature of a function that is called whenever the DNS service
114  * encounters a DNS request and needs to do something with it.  The
115  * function has then the chance to generate or modify the response by
116  * calling one of the three "GNUNET_DNS_request_*" continuations.
117  *
118  * When a request is intercepted, this function is called first to
119  * give the client a chance to do the complete address resolution;
120  * "rdata" will be NULL for this first call for a DNS request, unless
121  * some other client has already filled in a response.
122  *
123  * If multiple clients exist, all of them are called before the global
124  * DNS.  The global DNS is only called if all of the clients'
125  * functions call GNUNET_DNS_request_forward.  Functions that call
126  * GNUNET_DNS_request_forward will be called again before a final
127  * response is returned to the application.  If any of the clients'
128  * functions call GNUNET_DNS_request_drop, the response is dropped.
129  *
130  * @param cls closure
131  * @param rh request handle to user for reply
132  * @param request_length number of bytes in request
133  * @param request udp payload of the DNS request
134  */
135 static void
136 modify_request (void *cls,
137                 struct GNUNET_DNS_RequestHandle *rh,
138                 size_t request_length,
139                 const char *request)
140 {
141   struct GNUNET_DNSPARSER_Packet *p;
142   unsigned int i;
143   char *buf;
144   size_t len;
145   int ret;
146
147   p = GNUNET_DNSPARSER_parse (request, request_length);
148   if (NULL == p)
149   {
150     fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
151     GNUNET_DNS_request_forward (rh);
152     return;
153   }
154   for (i = 0; i < p->num_answers; i++)
155     modify_record (&p->answers[i]);
156   buf = NULL;
157   ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len);
158   GNUNET_DNSPARSER_free_packet (p);
159   if (GNUNET_OK != ret)
160   {
161     if (GNUNET_NO == ret)
162       fprintf (stderr,
163                "Modified DNS response did not fit, keeping old response\n");
164     else
165       GNUNET_break (0);  /* our modifications should have been sane! */
166     GNUNET_DNS_request_forward (rh);
167   }
168   else
169   {
170     if (verbosity > 0)
171       fprintf (stdout,
172                "Injecting modified DNS response\n");
173     GNUNET_DNS_request_answer (rh, len, buf);
174   }
175   GNUNET_free_non_null (buf);
176 }
177
178
179 /**
180  * Shutdown.
181  */
182 static void
183 do_disconnect (void *cls)
184 {
185   if (NULL != handle)
186   {
187     GNUNET_DNS_disconnect (handle);
188     handle = NULL;
189   }
190 }
191
192
193 /**
194  * Main function that will be run by the scheduler.
195  *
196  * @param cls closure
197  * @param args remaining command-line arguments
198  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
199  * @param cfg configuration
200  */
201 static void
202 run (void *cls, char *const *args, const char *cfgfile,
203      const struct GNUNET_CONFIGURATION_Handle *cfg)
204 {
205   struct in_addr i4;
206   struct in6_addr i6;
207
208   if ((n4 != NULL) &&
209       (1 != inet_pton (AF_INET, n4, &i4)))
210   {
211     fprintf (stderr,
212              "`%s' is nto a valid IPv4 address!\n",
213              n4);
214     return;
215   }
216   if ((n6 != NULL) &&
217       (1 != inet_pton (AF_INET6, n6, &i6)))
218   {
219     fprintf (stderr,
220              "`%s' is nto a valid IPv6 address!\n",
221              n6);
222     return;
223   }
224
225   handle =
226     GNUNET_DNS_connect (cfg,
227                         GNUNET_DNS_FLAG_POST_RESOLUTION,
228                         &modify_request,
229                         NULL);
230   GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
231 }
232
233
234 int
235 main (int argc, char *const *argv)
236 {
237   struct GNUNET_GETOPT_CommandLineOption options[] = {
238     GNUNET_GETOPT_option_string ('4',
239                                  "ipv4",
240                                  "IPV4",
241                                  gettext_noop ("set A records"),
242                                  &n4),
243
244     GNUNET_GETOPT_option_string ('6',
245                                  "ipv4",
246                                  "IPV6",
247                                  gettext_noop ("set AAAA records"),
248                                  &n6),
249
250     GNUNET_GETOPT_option_verbose (&verbosity),
251     GNUNET_GETOPT_OPTION_END
252   };
253
254   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
255     return 2;
256
257   ret = (GNUNET_OK ==
258          GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector",
259                              gettext_noop
260                                ("Change DNS replies to point elsewhere."),
261                              options,
262                              &run, NULL)) ? ret : 1;
263   GNUNET_free ((void *) argv);
264   return ret;
265 }
266
267
268 /* end of gnunet-dns-redirector.c */