f8d83237a9706c727af7b36e1c6db07d3e1ade2b
[oweals/gnunet.git] / src / pt / gnunet-daemon-pt.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2012 Christian Grothoff
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
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
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_vpn_service.h"
31 #include "gnunet_statistics_service.h"
32
33
34 /**
35  * The handle to the configuration used throughout the process
36  */
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 /**
40  * The handle to the VPN
41  */
42 static struct GNUNET_VPN_Handle *vpn_handle;
43
44 /**
45  * Statistics.
46  */
47 static struct GNUNET_STATISTICS_Handle *stats;
48
49 /**
50  * The handle to DNS
51  */
52 static struct GNUNET_DNS_Handle *dns_handle;
53
54 /**
55  * Are we doing IPv4-pt?
56  */
57 static int ipv4_pt;
58
59 /**
60  * Are we doing IPv6-pt?
61  */
62 static int ipv6_pt;
63
64
65
66 /**
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.
71  *
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.
76  *
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.
83  *
84  * @param cls closure
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
88  */
89 static void 
90 dns_request_handler (void *cls,
91                      struct GNUNET_DNS_RequestHandle *rh,
92                      size_t request_length,
93                      const char *request)
94 {
95 }
96
97
98 /**
99  * Function scheduled as very last function, cleans up after us
100  */
101 static void
102 cleanup (void *cls GNUNET_UNUSED,
103          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
104 {
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106               "Pt service is shutting down now\n");
107   if (vpn_handle != NULL)
108   {
109     GNUNET_VPN_disconnect (vpn_handle);
110     vpn_handle = NULL;
111   }
112   if (dns_handle != NULL)
113   {
114     GNUNET_DNS_disconnect (dns_handle);
115     dns_handle = NULL;
116   }
117   if (stats != NULL)
118   {
119     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
120     stats = NULL;
121   }
122 }
123
124
125 /**
126  * @brief Main function that will be run by the scheduler.
127  *
128  * @param cls closure
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
132  */
133 static void
134 run (void *cls, char *const *args GNUNET_UNUSED,
135      const char *cfgfile GNUNET_UNUSED,
136      const struct GNUNET_CONFIGURATION_Handle *cfg_)
137 {
138   cfg = 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))
143   {
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145                 _("No useful service enabled.  Exiting.\n"));
146     GNUNET_SCHEDULER_shutdown ();
147     return;    
148   }
149   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
150   dns_handle 
151     = GNUNET_DNS_connect (cfg, 
152                           GNUNET_DNS_FLAG_POST_RESOLUTION,
153                           &dns_request_handler, NULL);
154   if (NULL == dns_handle)
155   {
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157                 _("Failed to connect to %s service.  Exiting.\n"),
158                 "DNS");
159     GNUNET_SCHEDULER_shutdown ();
160     return;
161   }
162   vpn_handle = GNUNET_VPN_connect (cfg);
163   if (NULL == vpn_handle)
164   {
165     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166                 _("Failed to connect to %s service.  Exiting.\n"),
167                 "VPN");
168     GNUNET_SCHEDULER_shutdown ();
169     return;
170   }
171 }
172
173
174 /**
175  * The main function
176  *
177  * @param argc number of arguments from the command line
178  * @param argv command line arguments
179  * @return 0 ok, 1 on error
180  */
181 int
182 main (int argc, char *const *argv)
183 {
184   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
185     GNUNET_GETOPT_OPTION_END
186   };
187
188   return (GNUNET_OK ==
189           GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
190                               gettext_noop
191                               ("Daemon to run to perform IP protocol translation to GNUnet"),
192                               options, &run, NULL)) ? 0 : 1;
193 }
194
195
196 /* end of gnunet-daemon-pt.c */