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_vpn_service.h"
31 #include "gnunet_statistics_service.h"
35 * The handle to the configuration used throughout the process
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40 * The handle to the VPN
42 static struct GNUNET_VPN_Handle *vpn_handle;
47 static struct GNUNET_STATISTICS_Handle *stats;
52 static struct GNUNET_DNS_Handle *dns_handle;
55 * Are we doing IPv4-pt?
60 * Are we doing IPv6-pt?
67 * Signature of a function that is called whenever the DNS service
68 * encounters a DNS request and needs to do something with it. The
69 * function has then the chance to generate or modify the response by
70 * calling one of the three "GNUNET_DNS_request_*" continuations.
72 * When a request is intercepted, this function is called first to
73 * give the client a chance to do the complete address resolution;
74 * "rdata" will be NULL for this first call for a DNS request, unless
75 * some other client has already filled in a response.
77 * If multiple clients exist, all of them are called before the global
78 * DNS. The global DNS is only called if all of the clients'
79 * functions call GNUNET_DNS_request_forward. Functions that call
80 * GNUNET_DNS_request_forward will be called again before a final
81 * response is returned to the application. If any of the clients'
82 * functions call GNUNET_DNS_request_drop, the response is dropped.
85 * @param rh request handle to user for reply
86 * @param request_length number of bytes in request
87 * @param request udp payload of the DNS request
90 dns_request_handler (void *cls,
91 struct GNUNET_DNS_RequestHandle *rh,
92 size_t request_length,
99 * Function scheduled as very last function, cleans up after us
102 cleanup (void *cls GNUNET_UNUSED,
103 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106 "Pt service is shutting down now\n");
107 if (vpn_handle != NULL)
109 GNUNET_VPN_disconnect (vpn_handle);
112 if (dns_handle != NULL)
114 GNUNET_DNS_disconnect (dns_handle);
119 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
126 * @brief Main function that will be run by the scheduler.
129 * @param args remaining command-line arguments
130 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
131 * @param cfg_ configuration
134 run (void *cls, char *const *args GNUNET_UNUSED,
135 const char *cfgfile GNUNET_UNUSED,
136 const struct GNUNET_CONFIGURATION_Handle *cfg_)
139 stats = GNUNET_STATISTICS_create ("pt", cfg);
140 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV4");
141 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV6");
142 if (! (ipv4_pt || ipv6_pt))
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 _("No useful service enabled. Exiting.\n"));
146 GNUNET_SCHEDULER_shutdown ();
149 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
151 = GNUNET_DNS_connect (cfg,
152 GNUNET_DNS_FLAG_POST_RESOLUTION,
153 &dns_request_handler, NULL);
154 if (NULL == dns_handle)
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 _("Failed to connect to %s service. Exiting.\n"),
159 GNUNET_SCHEDULER_shutdown ();
162 vpn_handle = GNUNET_VPN_connect (cfg);
163 if (NULL == vpn_handle)
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 _("Failed to connect to %s service. Exiting.\n"),
168 GNUNET_SCHEDULER_shutdown ();
177 * @param argc number of arguments from the command line
178 * @param argv command line arguments
179 * @return 0 ok, 1 on error
182 main (int argc, char *const *argv)
184 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
185 GNUNET_GETOPT_OPTION_END
189 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
191 ("Daemon to run to perform IP protocol translation to GNUnet"),
192 options, &run, NULL)) ? 0 : 1;
196 /* end of gnunet-daemon-pt.c */