GNUnet  0.10.x
gnunet-dns-redirector.c
Go to the documentation of this file.
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 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
31 
35 static struct GNUNET_DNS_Handle *handle;
36 
40 static char *n4;
41 
45 static char *n6;
46 
50 static int ret;
51 
55 static unsigned int verbosity;
56 
57 
63 static void
65 {
66  char buf[INET6_ADDRSTRLEN];
67 
68  switch (record->type)
69  {
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;
84  if (record->data.raw.data_len != sizeof (struct in6_addr))
85  return;
86  if (NULL != n6)
87  {
88  if (verbosity > 1)
89  fprintf (stderr,
90  "Changing AAAA record from `%s' to `%s'\n",
91  inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf)),
92  n6);
93  GNUNET_assert (1 == inet_pton (AF_INET6, n6, record->data.raw.data));
94  }
95  break;
102  break;
103  default:
104  break;
105  }
106 }
107 
108 
132 static void
133 modify_request (void *cls,
135  size_t request_length,
136  const char *request)
137 {
138  struct GNUNET_DNSPARSER_Packet *p;
139  unsigned int i;
140  char *buf;
141  size_t len;
142  int ret;
143 
144  p = GNUNET_DNSPARSER_parse (request, request_length);
145  if (NULL == p)
146  {
147  fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
149  return;
150  }
151  for (i=0;i<p->num_answers;i++)
152  modify_record (&p->answers[i]);
153  buf = NULL;
154  ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len);
156  if (GNUNET_OK != ret)
157  {
158  if (GNUNET_NO == ret)
159  fprintf (stderr,
160  "Modified DNS response did not fit, keeping old response\n");
161  else
162  GNUNET_break (0); /* our modifications should have been sane! */
164  }
165  else
166  {
167  if (verbosity > 0)
168  fprintf (stdout,
169  "Injecting modified DNS response\n");
170  GNUNET_DNS_request_answer (rh, len, buf);
171  }
172  GNUNET_free_non_null (buf);
173 }
174 
175 
179 static void
180 do_disconnect (void *cls)
181 {
182  if (NULL != handle)
183  {
184  GNUNET_DNS_disconnect (handle);
185  handle = NULL;
186  }
187 }
188 
189 
198 static void
199 run (void *cls, char *const *args, const char *cfgfile,
200  const struct GNUNET_CONFIGURATION_Handle *cfg)
201 {
202  struct in_addr i4;
203  struct in6_addr i6;
204  if ( (n4 != NULL) &&
205  (1 != inet_pton (AF_INET, n4, &i4)) )
206  {
207  fprintf (stderr,
208  "`%s' is nto a valid IPv4 address!\n",
209  n4);
210  return;
211  }
212  if ( (n6 != NULL) &&
213  (1 != inet_pton (AF_INET6, n6, &i6)) )
214  {
215  fprintf (stderr,
216  "`%s' is nto a valid IPv6 address!\n",
217  n6);
218  return;
219  }
220 
221  handle =
222  GNUNET_DNS_connect (cfg,
225  NULL);
227 }
228 
229 
230 int
231 main (int argc, char *const *argv)
232 {
233  struct GNUNET_GETOPT_CommandLineOption options[] = {
235  "ipv4",
236  "IPV4",
237  gettext_noop ("set A records"),
238  &n4),
239 
241  "ipv4",
242  "IPV6",
243  gettext_noop ("set AAAA records"),
244  &n6),
245 
248  };
249 
250  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
251  return 2;
252 
253  ret = (GNUNET_OK ==
254  GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector",
256  ("Change DNS replies to point elsewhere."), options,
257  &run, NULL)) ? ret : 1;
258  GNUNET_free ((void*) argv);
259  return ret;
260 }
261 
262 
263 /* end of gnunet-dns-redirector.c */
static struct GNUNET_DNS_Handle * handle
Handle to DNS service.
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
Definition: w32nsp.c:83
#define GNUNET_DNSPARSER_TYPE_CNAME
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the &#39;-V&#39; verbosity option.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p, uint16_t max, char **buf, size_t *buf_length)
Given a DNS packet p, generate the corresponding UDP payload.
Definition: dnsparser.c:1259
This client wants to be called on the results of a DNS resolution (either resolved by PRE-RESOLUTION ...
static char * n6
New target for AAAA records.
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:854
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
#define GNUNET_DNSPARSER_TYPE_MX
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
int main(int argc, char *const *argv)
#define GNUNET_DNSPARSER_TYPE_PTR
static char * n4
New target for A records.
void GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
Disconnect from the DNS service.
Definition: dns_api.c:373
#define GNUNET_DNSPARSER_TYPE_SOA
static void do_disconnect(void *cls)
Shutdown.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
struct GNUNET_DNSPARSER_Packet * GNUNET_DNSPARSER_parse(const char *udp_payload, size_t udp_payload_length)
Parse a UDP payload of a DNS packet in to a nice struct for further processing and manipulation...
Definition: dnsparser.c:662
static int ret
Global return value (0 success).
static char buf[2048]
A DNS response record.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
DNS handle.
Definition: dns_api.c:58
size_t data_len
Number of bytes in data.
void * data
Binary record data.
#define GNUNET_DNSPARSER_TYPE_TXT
struct GNUNET_DNS_Handle * GNUNET_DNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_DNS_Flags flags, GNUNET_DNS_RequestHandler rh, void *rh_cls)
Connect to the service-dns.
Definition: dns_api.c:350
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:74
void GNUNET_DNS_request_answer(struct GNUNET_DNS_RequestHandle *rh, uint16_t reply_length, const char *reply)
If a GNUNET_DNS_RequestHandler calls this function, the request is supposed to be answered with the d...
Definition: dns_api.c:306
static void modify_request(void *cls, struct GNUNET_DNS_RequestHandle *rh, size_t request_length, const char *request)
Signature of a function that is called whenever the DNS service encounters a DNS request and needs to...
static void modify_record(const struct GNUNET_DNSPARSER_Record *record)
Modify the given DNS record.
Handle to identify an individual DNS request.
Definition: dns_api.c:34
#define GNUNET_DNSPARSER_TYPE_AAAA
configuration data
Definition: configuration.c:85
void GNUNET_DNS_request_forward(struct GNUNET_DNS_RequestHandle *rh)
If a GNUNET_DNS_RequestHandler calls this function, the request is given to other clients or the glob...
Definition: dns_api.c:247
#define GNUNET_DNSPARSER_TYPE_NS
Easy-to-process, parsed version of a DNS packet.
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
static unsigned int verbosity
Selected level of verbosity.
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define gettext_noop(String)
Definition: gettext.h:69