GNUnet  0.19.4
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 
34 static struct GNUNET_DNS_Handle *handle;
35 
39 static char *n4;
40 
44 static char *n6;
45 
49 static int ret;
50 
54 static unsigned int verbosity;
55 
56 
62 static void
64 {
65  char buf[INET6_ADDRSTRLEN];
66 
67  switch (record->type)
68  {
70  if (record->data.raw.data_len != sizeof(struct in_addr))
71  return;
72  if (NULL != n4)
73  {
74  if (verbosity > 1)
75  fprintf (stderr,
76  "Changing A record from `%s' to `%s'\n",
77  inet_ntop (AF_INET, record->data.raw.data, buf, sizeof(buf)),
78  n4);
79  GNUNET_assert (1 == inet_pton (AF_INET, n4, record->data.raw.data));
80  }
81  break;
82 
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;
96 
103  break;
104 
105  default:
106  break;
107  }
108 }
109 
110 
134 static void
135 modify_request (void *cls,
137  size_t request_length,
138  const char *request)
139 {
140  struct GNUNET_DNSPARSER_Packet *p;
141  unsigned int i;
142  char *buf;
143  size_t len;
144  int ret;
145 
146  p = GNUNET_DNSPARSER_parse (request, request_length);
147  if (NULL == p)
148  {
149  fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
151  return;
152  }
153  for (i = 0; i < p->num_answers; i++)
154  modify_record (&p->answers[i]);
155  buf = NULL;
156  ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len);
158  if (GNUNET_OK != ret)
159  {
160  if (GNUNET_NO == ret)
161  fprintf (stderr,
162  "Modified DNS response did not fit, keeping old response\n");
163  else
164  GNUNET_break (0); /* our modifications should have been sane! */
166  }
167  else
168  {
169  if (verbosity > 0)
170  fprintf (stdout,
171  "Injecting modified DNS response\n");
173  }
174  GNUNET_free (buf);
175 }
176 
177 
181 static void
182 do_disconnect (void *cls)
183 {
184  if (NULL != handle)
185  {
187  handle = NULL;
188  }
189 }
190 
191 
200 static void
201 run (void *cls, char *const *args, const char *cfgfile,
202  const struct GNUNET_CONFIGURATION_Handle *cfg)
203 {
204  struct in_addr i4;
205  struct in6_addr i6;
206 
207  if ((n4 != NULL) &&
208  (1 != inet_pton (AF_INET, n4, &i4)))
209  {
210  fprintf (stderr,
211  "`%s' is nto a valid IPv4 address!\n",
212  n4);
213  return;
214  }
215  if ((n6 != NULL) &&
216  (1 != inet_pton (AF_INET6, n6, &i6)))
217  {
218  fprintf (stderr,
219  "`%s' is nto a valid IPv6 address!\n",
220  n6);
221  return;
222  }
223 
224  handle =
228  NULL);
230 }
231 
232 
233 int
234 main (int argc, char *const *argv)
235 {
238  "ipv4",
239  "IPV4",
240  gettext_noop ("set A records"),
241  &n4),
242 
244  "ipv4",
245  "IPV6",
246  gettext_noop ("set AAAA records"),
247  &n6),
248 
251  };
252 
253  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
254  return 2;
255 
256  ret = (GNUNET_OK ==
257  GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector",
259  ("Change DNS replies to point elsewhere."),
260  options,
261  &run, NULL)) ? ret : 1;
262  GNUNET_free_nz ((void *) argv);
263  return ret;
264 }
265 
266 
267 /* end of gnunet-dns-redirector.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static void do_disconnect(void *cls)
Shutdown.
static unsigned int verbosity
Selected level of verbosity.
static char * n4
New target for A records.
static char * n6
New target for AAAA records.
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.
static int ret
Global return value (0 success).
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.
int main(int argc, char *const *argv)
static struct GNUNET_DNS_Handle * handle
Handle to DNS service.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static char buf[2048]
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:40
API to access the DNS service.
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:854
#define GNUNET_DNSPARSER_TYPE_SOA
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_DNSPARSER_TYPE_PTR
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:656
#define GNUNET_DNSPARSER_TYPE_NS
#define GNUNET_DNSPARSER_TYPE_CNAME
#define GNUNET_DNSPARSER_TYPE_AAAA
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
#define GNUNET_DNSPARSER_TYPE_MX
#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:340
void GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
Disconnect from the DNS service.
Definition: dns_api.c:363
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:237
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:296
@ GNUNET_DNS_FLAG_POST_RESOLUTION
This client wants to be called on the results of a DNS resolution (either resolved by PRE-RESOLUTION ...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the '-V' verbosity option.
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.
@ GNUNET_OK
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue 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,...
Definition: program.c:400
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,...
Definition: scheduler.c:1334
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
Easy-to-process, parsed version of a DNS packet.
A DNS response record.
DNS handle.
Definition: dns_api.c:57
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:71
Handle to identify an individual DNS request.
Definition: dns_api.c:35
Definition of a command line option.