2 This file is part of GNUnet.
3 (C) 2010, 2012 Christian Grothoff
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file pt/gnunet-daemon-pt.c
23 * @brief tool to manipulate DNS and VPN services to perform protocol translation (IPvX over GNUnet)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_vpn_service.h"
32 #include "gnunet_statistics_service.h"
36 * The handle to the configuration used throughout the process
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 * The handle to the VPN
43 static struct GNUNET_VPN_Handle *vpn_handle;
48 static struct GNUNET_STATISTICS_Handle *stats;
53 static struct GNUNET_DNS_Handle *dns_handle;
56 * Are we doing IPv4-pt?
61 * Are we doing IPv6-pt?
67 * Test if any of the given records need protocol-translation work.
69 * @param ra array of records
70 * @param ra_len number of entries in ra
71 * @return GNUNET_YES if any of the given records require protocol-translation
74 work_test (const struct GNUNET_DNSPARSER_Record *ra,
79 for (i=0;i<ra_len;i++)
83 case GNUNET_DNSPARSER_TYPE_A:
87 case GNUNET_DNSPARSER_TYPE_AAAA:
98 * Signature of a function that is called whenever the DNS service
99 * encounters a DNS request and needs to do something with it. The
100 * function has then the chance to generate or modify the response by
101 * calling one of the three "GNUNET_DNS_request_*" continuations.
103 * When a request is intercepted, this function is called first to
104 * give the client a chance to do the complete address resolution;
105 * "rdata" will be NULL for this first call for a DNS request, unless
106 * some other client has already filled in a response.
108 * If multiple clients exist, all of them are called before the global
109 * DNS. The global DNS is only called if all of the clients'
110 * functions call GNUNET_DNS_request_forward. Functions that call
111 * GNUNET_DNS_request_forward will be called again before a final
112 * response is returned to the application. If any of the clients'
113 * functions call GNUNET_DNS_request_drop, the response is dropped.
116 * @param rh request handle to user for reply
117 * @param request_length number of bytes in request
118 * @param request udp payload of the DNS request
121 dns_request_handler (void *cls,
122 struct GNUNET_DNS_RequestHandle *rh,
123 size_t request_length,
126 struct GNUNET_DNSPARSER_Packet *dns;
129 dns = GNUNET_DNSPARSER_parse (request, request_length);
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 _("Failed to parse DNS request. Dropping.\n"));
134 GNUNET_DNS_request_drop (rh);
138 work |= work_test (dns->answers, dns->num_answers);
139 work |= work_test (dns->authority_records, dns->num_authority_records);
140 work |= work_test (dns->additional_records, dns->num_additional_records);
143 GNUNET_DNS_request_forward (rh);
146 /* FIXME: translate A/AAAA records using VPN! */
151 * Function scheduled as very last function, cleans up after us
154 cleanup (void *cls GNUNET_UNUSED,
155 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Pt service is shutting down now\n");
159 if (vpn_handle != NULL)
161 GNUNET_VPN_disconnect (vpn_handle);
164 if (dns_handle != NULL)
166 GNUNET_DNS_disconnect (dns_handle);
171 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
178 * @brief Main function that will be run by the scheduler.
181 * @param args remaining command-line arguments
182 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
183 * @param cfg_ configuration
186 run (void *cls, char *const *args GNUNET_UNUSED,
187 const char *cfgfile GNUNET_UNUSED,
188 const struct GNUNET_CONFIGURATION_Handle *cfg_)
191 stats = GNUNET_STATISTICS_create ("pt", cfg);
192 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV4");
193 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV6");
194 if (! (ipv4_pt || ipv6_pt))
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 _("No useful service enabled. Exiting.\n"));
198 GNUNET_SCHEDULER_shutdown ();
201 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
203 = GNUNET_DNS_connect (cfg,
204 GNUNET_DNS_FLAG_POST_RESOLUTION,
205 &dns_request_handler, NULL);
206 if (NULL == dns_handle)
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 _("Failed to connect to %s service. Exiting.\n"),
211 GNUNET_SCHEDULER_shutdown ();
214 vpn_handle = GNUNET_VPN_connect (cfg);
215 if (NULL == vpn_handle)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Failed to connect to %s service. Exiting.\n"),
220 GNUNET_SCHEDULER_shutdown ();
229 * @param argc number of arguments from the command line
230 * @param argv command line arguments
231 * @return 0 ok, 1 on error
234 main (int argc, char *const *argv)
236 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
237 GNUNET_GETOPT_OPTION_END
241 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
243 ("Daemon to run to perform IP protocol translation to GNUnet"),
244 options, &run, NULL)) ? 0 : 1;
248 /* end of gnunet-daemon-pt.c */