-expanding draft code
[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_dnsparser_lib.h"
31 #include "gnunet_vpn_service.h"
32 #include "gnunet_statistics_service.h"
33
34
35 /**
36  * The handle to the configuration used throughout the process
37  */
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39
40 /**
41  * The handle to the VPN
42  */
43 static struct GNUNET_VPN_Handle *vpn_handle;
44
45 /**
46  * Statistics.
47  */
48 static struct GNUNET_STATISTICS_Handle *stats;
49
50 /**
51  * The handle to DNS
52  */
53 static struct GNUNET_DNS_Handle *dns_handle;
54
55 /**
56  * Are we doing IPv4-pt?
57  */
58 static int ipv4_pt;
59
60 /**
61  * Are we doing IPv6-pt?
62  */
63 static int ipv6_pt;
64
65
66 /**
67  * Test if any of the given records need protocol-translation work.
68  *
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
72  */
73 static int
74 work_test (const struct GNUNET_DNSPARSER_Record *ra,
75            unsigned int ra_len)
76 {
77   unsigned int i;
78
79   for (i=0;i<ra_len;i++)
80   {
81     switch (ra[i].type)
82     {
83     case GNUNET_DNSPARSER_TYPE_A:
84       if (ipv4_pt)
85         return GNUNET_YES;
86       break;
87     case GNUNET_DNSPARSER_TYPE_AAAA:
88       if (ipv6_pt)
89         return GNUNET_YES;
90       break;
91     }
92   }
93   return GNUNET_NO;
94 }
95
96
97 /**
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.
102  *
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.
107  *
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.
114  *
115  * @param cls closure
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
119  */
120 static void 
121 dns_request_handler (void *cls,
122                      struct GNUNET_DNS_RequestHandle *rh,
123                      size_t request_length,
124                      const char *request)
125 {
126   struct GNUNET_DNSPARSER_Packet *dns;
127   int work;
128
129   dns = GNUNET_DNSPARSER_parse (request, request_length);
130   if (NULL == dns)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133                 _("Failed to parse DNS request.  Dropping.\n"));
134     GNUNET_DNS_request_drop (rh);
135     return;
136   }
137   work = GNUNET_NO;
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);
141   if (! work)
142   {
143     GNUNET_DNS_request_forward (rh);
144     return;
145   }
146   /* FIXME: translate A/AAAA records using VPN! */
147 }
148
149
150 /**
151  * Function scheduled as very last function, cleans up after us
152  */
153 static void
154 cleanup (void *cls GNUNET_UNUSED,
155          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
156 {
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Pt service is shutting down now\n");
159   if (vpn_handle != NULL)
160   {
161     GNUNET_VPN_disconnect (vpn_handle);
162     vpn_handle = NULL;
163   }
164   if (dns_handle != NULL)
165   {
166     GNUNET_DNS_disconnect (dns_handle);
167     dns_handle = NULL;
168   }
169   if (stats != NULL)
170   {
171     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
172     stats = NULL;
173   }
174 }
175
176
177 /**
178  * @brief Main function that will be run by the scheduler.
179  *
180  * @param cls closure
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
184  */
185 static void
186 run (void *cls, char *const *args GNUNET_UNUSED,
187      const char *cfgfile GNUNET_UNUSED,
188      const struct GNUNET_CONFIGURATION_Handle *cfg_)
189 {
190   cfg = 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))
195   {
196     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197                 _("No useful service enabled.  Exiting.\n"));
198     GNUNET_SCHEDULER_shutdown ();
199     return;    
200   }
201   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
202   dns_handle 
203     = GNUNET_DNS_connect (cfg, 
204                           GNUNET_DNS_FLAG_POST_RESOLUTION,
205                           &dns_request_handler, NULL);
206   if (NULL == dns_handle)
207   {
208     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209                 _("Failed to connect to %s service.  Exiting.\n"),
210                 "DNS");
211     GNUNET_SCHEDULER_shutdown ();
212     return;
213   }
214   vpn_handle = GNUNET_VPN_connect (cfg);
215   if (NULL == vpn_handle)
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218                 _("Failed to connect to %s service.  Exiting.\n"),
219                 "VPN");
220     GNUNET_SCHEDULER_shutdown ();
221     return;
222   }
223 }
224
225
226 /**
227  * The main function
228  *
229  * @param argc number of arguments from the command line
230  * @param argv command line arguments
231  * @return 0 ok, 1 on error
232  */
233 int
234 main (int argc, char *const *argv)
235 {
236   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
237     GNUNET_GETOPT_OPTION_END
238   };
239
240   return (GNUNET_OK ==
241           GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
242                               gettext_noop
243                               ("Daemon to run to perform IP protocol translation to GNUnet"),
244                               options, &run, NULL)) ? 0 : 1;
245 }
246
247
248 /* end of gnunet-daemon-pt.c */