do DNS exit lookup in DHT
[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 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_mesh_service.h"
31 #include "gnunet_tun_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_vpn_service.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_applications.h"
36 #include "block_dns.h"
37
38 #define PORT_PT 4242 // FIXME
39
40
41 /**
42  * After how long do we time out if we could not get an IP from VPN or MESH?
43  */
44 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
45
46
47 /**
48  * How many bytes of payload do we allow at most for a DNS reply?
49  * Given that this is pretty much limited to loopback, we can be
50  * pretty high (Linux loopback defaults to 16k, most local UDP packets
51  * should survive up to 9k (NFS), so 8k should be pretty safe in
52  * general).
53  */
54 #define MAX_DNS_SIZE (8 * 1024)
55
56
57 /**
58  * Which group of DNS records are we currently processing?
59  */
60 enum RequestGroup
61 {
62   /**
63    * DNS answers
64    */
65   ANSWERS = 0, 
66
67   /**
68    * DNS authority records
69    */
70   AUTHORITY_RECORDS = 1,
71
72   /**
73    * DNS additional records
74    */
75   ADDITIONAL_RECORDS = 2,
76
77   /**
78    * We're done processing.
79    */
80   END = 3
81 };
82
83
84 /**
85  * Information tracked per DNS reply that we are processing.
86  */
87 struct ReplyContext
88 {
89   /**
90    * Handle to submit the final result.
91    */
92   struct GNUNET_DNS_RequestHandle *rh;
93   
94   /**
95    * DNS packet that is being modified.
96    */
97   struct GNUNET_DNSPARSER_Packet *dns;
98
99   /**
100    * Active redirection request with the VPN.
101    */
102   struct GNUNET_VPN_RedirectionRequest *rr;
103
104   /**
105    * Record for which we have an active redirection request.
106    */
107   struct GNUNET_DNSPARSER_Record *rec;
108
109   /**
110    * Offset in the current record group that is being modified.
111    */
112   unsigned int offset;
113
114   /**
115    * Group that is being modified
116    */
117   enum RequestGroup group;
118   
119 };
120
121
122 /**
123  * State we keep for a request that is going out via MESH.
124  */
125 struct RequestContext
126 {
127   /**
128    * We keep these in a DLL.
129    */
130   struct RequestContext *next;
131
132   /**
133    * We keep these in a DLL.
134    */
135   struct RequestContext *prev;
136
137   /**
138    * Handle for interaction with DNS service.
139    */
140   struct GNUNET_DNS_RequestHandle *rh;
141   
142   /**
143    * Message we're sending out via MESH, allocated at the
144    * end of this struct.
145    */
146   const struct GNUNET_MessageHeader *mesh_message;
147
148   /**
149    * Task used to abort this operation with timeout.
150    */
151   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
152
153   /**
154    * ID of the original DNS request (used to match the reply).
155    */
156   uint16_t dns_id;
157
158   /**
159    * #GNUNET_NO if this request is still in the transmit_queue,
160    * #GNUNET_YES if we are in the receive_queue.
161    */ 
162   int16_t was_transmitted;
163
164 };
165
166
167 /**
168  * The handle to the configuration used throughout the process
169  */
170 static const struct GNUNET_CONFIGURATION_Handle *cfg;
171
172 /**
173  * The handle to the VPN
174  */
175 static struct GNUNET_VPN_Handle *vpn_handle;
176
177 /**
178  * The handle to the MESH service
179  */
180 static struct GNUNET_MESH_Handle *mesh_handle;
181
182 /**
183  * Tunnel we use for DNS requests over MESH.
184  * FIXME: we might want to keep multiple tunnels open
185  * at all times...
186  */
187 static struct GNUNET_MESH_Tunnel *mesh_tunnel;
188
189 /**
190  * Active transmission request with MESH (or NULL).
191  */
192 static struct GNUNET_MESH_TransmitHandle *mesh_th;
193
194 /**
195  * Head of DLL of requests to be transmitted to mesh_tunnel.
196  */
197 static struct RequestContext *transmit_queue_head;
198
199 /**
200  * Tail of DLL of requests to be transmitted to mesh_tunnel.
201  */
202 static struct RequestContext *transmit_queue_tail;
203
204 /**
205  * Head of DLL of requests waiting for a response.
206  */
207 static struct RequestContext *receive_queue_head;
208
209 /**
210  * Tail of DLL of requests waiting for a response.
211  */
212 static struct RequestContext *receive_queue_tail;
213
214 /**
215  * Statistics.
216  */
217 static struct GNUNET_STATISTICS_Handle *stats;
218
219 /**
220  * The handle to DNS post-resolution modifications.
221  */
222 static struct GNUNET_DNS_Handle *dns_post_handle;
223
224 /**
225  * The handle to DNS pre-resolution modifications.
226  */
227 static struct GNUNET_DNS_Handle *dns_pre_handle;
228
229 /**
230  * Handle to access the DHT.
231  */
232 static struct GNUNET_DHT_Handle *dht;
233
234 /**
235  * Our DHT GET operation to find DNS exits.
236  */
237 static struct GNUNET_DHT_GetHandle *dht_get;
238
239 /**
240  * Are we doing IPv4-pt?
241  */
242 static int ipv4_pt;
243
244 /**
245  * Are we doing IPv6-pt?
246  */
247 static int ipv6_pt;
248
249 /**
250  * Are we tunneling DNS queries?
251  */
252 static int dns_tunnel;
253
254 /**
255  * Number of DNS exit peers we currently have in the mesh tunnel.
256  * Used to see if using the mesh tunnel makes any sense right now.
257  */
258 static unsigned int dns_exit_available;
259
260
261 /**
262  * We're done modifying all records in the response.  Submit the reply
263  * and free the resources of the rc.
264  *
265  * @param rc context to process
266  */
267 static void
268 finish_request (struct ReplyContext *rc)
269 {
270   char *buf;
271   size_t buf_len;
272
273   if (GNUNET_SYSERR ==
274       GNUNET_DNSPARSER_pack (rc->dns,
275                              MAX_DNS_SIZE,
276                              &buf,
277                              &buf_len))
278   {
279     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280                 _("Failed to pack DNS request.  Dropping.\n"));
281     GNUNET_DNS_request_drop (rc->rh);
282   }
283   else
284   {
285     GNUNET_STATISTICS_update (stats,
286                               gettext_noop ("# DNS requests mapped to VPN"),
287                               1, GNUNET_NO);
288     GNUNET_DNS_request_answer (rc->rh,
289                                buf_len, buf);
290     GNUNET_free (buf);
291   }
292   GNUNET_DNSPARSER_free_packet (rc->dns);
293   GNUNET_free (rc);
294 }
295
296
297 /**
298  * Process the next record of the given request context.
299  * When done, submit the reply and free the resources of
300  * the rc.
301  *
302  * @param rc context to process
303  */
304 static void
305 submit_request (struct ReplyContext *rc);
306
307
308 /**
309  * Callback invoked from the VPN service once a redirection is
310  * available.  Provides the IP address that can now be used to
311  * reach the requested destination.  We substitute the active
312  * record and then continue with 'submit_request' to look at
313  * the other records.
314  *
315  * @param cls our 'struct ReplyContext'
316  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
317  *                will match 'result_af' from the request
318  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
319  *                that the VPN allocated for the redirection;
320  *                traffic to this IP will now be redirected to the 
321  *                specified target peer; NULL on error
322  */
323 static void
324 vpn_allocation_callback (void *cls,
325                          int af,
326                          const void *address)
327 {
328   struct ReplyContext *rc = cls;
329
330   rc->rr = NULL;
331   if (af == AF_UNSPEC)
332   {
333     GNUNET_DNS_request_drop (rc->rh);
334     GNUNET_DNSPARSER_free_packet (rc->dns);
335     GNUNET_free (rc);
336     return;
337   }
338   GNUNET_STATISTICS_update (stats,
339                             gettext_noop ("# DNS records modified"),
340                             1, GNUNET_NO);
341   switch (rc->rec->type)
342   {
343   case GNUNET_DNSPARSER_TYPE_A:
344     GNUNET_assert (AF_INET == af);
345     memcpy (rc->rec->data.raw.data, address, sizeof (struct in_addr));
346     break;
347   case GNUNET_DNSPARSER_TYPE_AAAA:
348     GNUNET_assert (AF_INET6 == af);
349     memcpy (rc->rec->data.raw.data, address, sizeof (struct in6_addr));
350     break;
351   default:
352     GNUNET_assert (0);
353     return;
354   }
355   rc->rec = NULL;
356   submit_request (rc);
357 }
358
359
360 /**
361  * Modify the given DNS record by asking VPN to create a tunnel
362  * to the given address.  When done, continue with submitting
363  * other records from the request context ('submit_request' is
364  * our continuation).
365  *
366  * @param rc context to process
367  * @param rec record to modify
368  */
369 static void
370 modify_address (struct ReplyContext *rc,
371                 struct GNUNET_DNSPARSER_Record *rec)
372 {
373   int af;
374
375   switch (rec->type)
376   {
377   case GNUNET_DNSPARSER_TYPE_A:
378     af = AF_INET;
379     GNUNET_assert (rec->data.raw.data_len == sizeof (struct in_addr));
380     break;
381   case GNUNET_DNSPARSER_TYPE_AAAA:
382     af = AF_INET6;
383     GNUNET_assert (rec->data.raw.data_len == sizeof (struct in6_addr));
384     break;
385   default:
386     GNUNET_assert (0);
387     return;
388   }
389   rc->rec = rec;
390   rc->rr = GNUNET_VPN_redirect_to_ip (vpn_handle,
391                                       af, af,
392                                       rec->data.raw.data,
393                                       GNUNET_NO /* nac */,
394                                       GNUNET_TIME_relative_to_absolute (TIMEOUT),
395                                       &vpn_allocation_callback,
396                                       rc);
397 }
398
399
400 /**
401  * Process the next record of the given request context.
402  * When done, submit the reply and free the resources of
403  * the rc.
404  *
405  * @param rc context to process
406  */
407 static void
408 submit_request (struct ReplyContext *rc)
409 {
410   struct GNUNET_DNSPARSER_Record *ra;
411   unsigned int ra_len;
412   unsigned int i;
413
414   while (1)
415   {
416     switch (rc->group)
417     {
418     case ANSWERS:
419       ra = rc->dns->answers;
420       ra_len = rc->dns->num_answers;
421       break;
422     case AUTHORITY_RECORDS:
423       ra = rc->dns->authority_records;
424       ra_len = rc->dns->num_authority_records;
425       break;
426     case ADDITIONAL_RECORDS:
427       ra = rc->dns->additional_records;
428       ra_len = rc->dns->num_additional_records;
429       break;
430     case END:
431       finish_request (rc);
432       return;
433     default:
434       GNUNET_assert (0);      
435     }
436     for (i=rc->offset;i<ra_len;i++)
437     {
438       switch (ra[i].type)
439       {
440       case GNUNET_DNSPARSER_TYPE_A:
441         if (ipv4_pt)
442         {
443           rc->offset = i + 1;
444           modify_address (rc, &ra[i]);
445           return;
446         }
447         break;
448       case GNUNET_DNSPARSER_TYPE_AAAA:
449         if (ipv6_pt)
450         {
451           rc->offset = i + 1;
452           modify_address (rc, &ra[i]);
453           return;
454         }
455         break;
456       }
457     }
458     rc->group++;
459   }
460 }
461
462
463 /**
464  * Test if any of the given records need protocol-translation work.
465  *
466  * @param ra array of records
467  * @param ra_len number of entries in ra
468  * @return GNUNET_YES if any of the given records require protocol-translation
469  */
470 static int
471 work_test (const struct GNUNET_DNSPARSER_Record *ra,
472            unsigned int ra_len)
473 {
474   unsigned int i;
475
476   for (i=0;i<ra_len;i++)
477   {
478     switch (ra[i].type)
479     {
480     case GNUNET_DNSPARSER_TYPE_A:
481       if (ipv4_pt)
482         return GNUNET_YES;
483       break;
484     case GNUNET_DNSPARSER_TYPE_AAAA:
485       if (ipv6_pt)
486         return GNUNET_YES;
487       break;
488     }
489   }
490   return GNUNET_NO;
491 }
492
493
494 /**
495  * This function is called AFTER we got an IP address for a 
496  * DNS request.  Now, the PT daemon has the chance to substitute
497  * the IP address with one from the VPN range to tunnel requests
498  * destined for this IP address via VPN and MESH.
499  *
500  * @param cls closure
501  * @param rh request handle to user for reply
502  * @param request_length number of bytes in request
503  * @param request udp payload of the DNS request
504  */
505 static void 
506 dns_post_request_handler (void *cls,
507                           struct GNUNET_DNS_RequestHandle *rh,
508                           size_t request_length,
509                           const char *request)
510 {
511   struct GNUNET_DNSPARSER_Packet *dns;
512   struct ReplyContext *rc;
513   int work;
514
515   GNUNET_STATISTICS_update (stats,
516                             gettext_noop ("# DNS replies intercepted"),
517                             1, GNUNET_NO);
518   dns = GNUNET_DNSPARSER_parse (request, request_length);
519   if (NULL == dns)
520   {
521     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522                 _("Failed to parse DNS request.  Dropping.\n"));
523     GNUNET_DNS_request_drop (rh);
524     return;
525   }
526   work = GNUNET_NO;
527   work |= work_test (dns->answers, dns->num_answers);
528   work |= work_test (dns->authority_records, dns->num_authority_records);
529   work |= work_test (dns->additional_records, dns->num_additional_records);
530   if (! work)
531   {
532     GNUNET_DNS_request_forward (rh);
533     GNUNET_DNSPARSER_free_packet (dns);
534     return;
535   }
536   rc = GNUNET_new (struct ReplyContext);
537   rc->rh = rh;
538   rc->dns = dns;
539   rc->offset = 0;
540   rc->group = ANSWERS;
541   submit_request (rc);
542 }
543
544
545 /**
546  * Transmit a DNS request via MESH and move the request
547  * handle to the receive queue.
548  *
549  * @param cls NULL
550  * @param size number of bytes available in buf
551  * @param buf where to copy the message
552  * @return number of bytes written to buf
553  */
554 static size_t
555 transmit_dns_request_to_mesh (void *cls,
556                               size_t size,
557                               void *buf)
558 {
559   struct RequestContext *rc;
560   size_t mlen;
561
562   mesh_th = NULL;
563   if (NULL == (rc = transmit_queue_head))
564     return 0;
565   mlen = ntohs (rc->mesh_message->size);
566   if (mlen > size)
567   {
568     mesh_th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel,
569                                                  GNUNET_NO,
570                                                  TIMEOUT,
571                                                  mlen,
572                                                  &transmit_dns_request_to_mesh,
573                                                  NULL);
574     return 0;
575   }
576   GNUNET_assert (GNUNET_NO == rc->was_transmitted);
577   memcpy (buf, rc->mesh_message, mlen);
578   GNUNET_CONTAINER_DLL_remove (transmit_queue_head,
579                                transmit_queue_tail,
580                                rc);
581   rc->was_transmitted = GNUNET_YES;
582   GNUNET_CONTAINER_DLL_insert (receive_queue_head,
583                                receive_queue_tail,
584                                rc);
585   rc = transmit_queue_head;
586   if (NULL != rc)
587     mesh_th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel,
588                                                  GNUNET_NO,
589                                                  TIMEOUT,
590                                                  ntohs (rc->mesh_message->size),
591                                                  &transmit_dns_request_to_mesh,
592                                                  NULL);
593   return mlen;
594 }
595
596
597 /**
598  * Task run if the time to answer a DNS request via MESH is over.
599  *
600  * @param cls the 'struct RequestContext' to abort
601  * @param tc scheduler context
602  */
603 static void
604 timeout_request (void *cls,
605                  const struct GNUNET_SCHEDULER_TaskContext *tc)
606 {
607   struct RequestContext *rc = cls;
608   
609   if (rc->was_transmitted)
610     GNUNET_CONTAINER_DLL_remove (receive_queue_head,
611                                  receive_queue_tail,
612                                  rc);
613   else
614     GNUNET_CONTAINER_DLL_remove (transmit_queue_head,
615                                  transmit_queue_tail,
616                                  rc);
617   GNUNET_STATISTICS_update (stats,
618                             gettext_noop ("# DNS requests dropped (timeout)"),
619                             1, GNUNET_NO);
620   GNUNET_DNS_request_drop (rc->rh);
621   GNUNET_free (rc);
622 }
623
624
625 /**
626  * This function is called *before* the DNS request has been 
627  * given to a "local" DNS resolver.  Tunneling for DNS requests
628  * was enabled, so we now need to send the request via some MESH
629  * tunnel to a DNS EXIT for resolution.
630  *
631  * @param cls closure
632  * @param rh request handle to user for reply
633  * @param request_length number of bytes in request
634  * @param request udp payload of the DNS request
635  */
636 static void 
637 dns_pre_request_handler (void *cls,
638                          struct GNUNET_DNS_RequestHandle *rh,
639                          size_t request_length,
640                          const char *request)
641 {
642   struct RequestContext *rc;
643   size_t mlen;
644   struct GNUNET_MessageHeader hdr;
645   struct GNUNET_TUN_DnsHeader dns;
646
647   GNUNET_STATISTICS_update (stats,
648                             gettext_noop ("# DNS requests intercepted"),
649                             1, GNUNET_NO);
650   if (0 == dns_exit_available)
651   {
652     GNUNET_STATISTICS_update (stats,
653                               gettext_noop ("# DNS requests dropped (DNS mesh tunnel down)"),
654                               1, GNUNET_NO);
655     GNUNET_DNS_request_drop (rh);
656     return;
657   }
658   if (request_length < sizeof (dns))
659   {
660     GNUNET_STATISTICS_update (stats,
661                               gettext_noop ("# DNS requests dropped (malformed)"),
662                               1, GNUNET_NO);
663     GNUNET_DNS_request_drop (rh);
664     return;
665   }
666   memcpy (&dns, request, sizeof (dns));
667   GNUNET_assert (NULL != mesh_tunnel);
668   mlen = sizeof (struct GNUNET_MessageHeader) + request_length;
669   rc = GNUNET_malloc (sizeof (struct RequestContext) + mlen);
670   rc->rh = rh;
671   rc->mesh_message = (const struct GNUNET_MessageHeader*) &rc[1];
672   rc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
673                                                    &timeout_request,
674                                                    rc);
675   rc->dns_id = dns.id;
676   hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET);
677   hdr.size = htons (mlen);
678   memcpy (&rc[1], &hdr, sizeof (struct GNUNET_MessageHeader));
679   memcpy (&(((char*)&rc[1])[sizeof (struct GNUNET_MessageHeader)]),
680           request,
681           request_length);
682   GNUNET_CONTAINER_DLL_insert_tail (transmit_queue_head,
683                                     transmit_queue_tail,
684                                     rc);
685   if (NULL == mesh_th)
686     mesh_th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel,
687                                                  GNUNET_NO,
688                                                  TIMEOUT,
689                                                  mlen,
690                                                  &transmit_dns_request_to_mesh,
691                                                  NULL);
692 }
693
694
695 /**
696  * Process a request via mesh to perform a DNS query.
697  *
698  * @param cls closure, NULL
699  * @param tunnel connection to the other end
700  * @param tunnel_ctx pointer to our 'struct TunnelState *'
701  * @param message the actual message
702  * 
703  * @return GNUNET_OK to keep the connection open,
704  *         GNUNET_SYSERR to close it (signal serious error)
705  */
706 static int
707 receive_dns_response (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
708                       void **tunnel_ctx,
709                       const struct GNUNET_MessageHeader *message)
710 {
711   struct GNUNET_TUN_DnsHeader dns;
712   size_t mlen;
713   struct RequestContext *rc;
714
715   mlen = ntohs (message->size);
716   mlen -= sizeof (struct GNUNET_MessageHeader);
717   if (mlen < sizeof (struct GNUNET_TUN_DnsHeader))
718   {
719     GNUNET_break_op (0);
720     return GNUNET_SYSERR;
721   }
722   memcpy (&dns, &message[1], sizeof (dns));
723   for (rc = receive_queue_head; NULL != rc; rc = rc->next)
724   {
725     GNUNET_assert (GNUNET_YES == rc->was_transmitted);
726     if (dns.id == rc->dns_id)
727     {
728       GNUNET_STATISTICS_update (stats,
729                                 gettext_noop ("# DNS replies received"),
730                                 1, GNUNET_NO);
731       GNUNET_DNS_request_answer (rc->rh,
732                                  mlen,
733                                  (const void*) &message[1]);
734       GNUNET_CONTAINER_DLL_remove (receive_queue_head,
735                                    receive_queue_tail,
736                                    rc);
737       GNUNET_SCHEDULER_cancel (rc->timeout_task);
738       GNUNET_free (rc);
739       return GNUNET_OK;      
740     }
741   }
742   GNUNET_STATISTICS_update (stats,
743                             gettext_noop ("# DNS replies dropped (too late?)"),
744                             1, GNUNET_NO);
745   return GNUNET_OK;
746 }
747
748
749 /**
750  * The MESH DNS tunnel went down.  Abort all pending DNS
751  * requests (we're unlikely to get an answer in time).
752  */ 
753 static void
754 abort_all_requests ()
755 {
756   struct RequestContext *rc;
757
758   while (NULL != (rc = receive_queue_head))
759   {
760     GNUNET_STATISTICS_update (stats,
761                               gettext_noop ("# DNS requests aborted (tunnel down)"),
762                               1, GNUNET_NO);
763     GNUNET_CONTAINER_DLL_remove (receive_queue_head,
764                                  receive_queue_tail,
765                                  rc);
766     GNUNET_DNS_request_drop (rc->rh);
767     GNUNET_SCHEDULER_cancel (rc->timeout_task);
768     GNUNET_free (rc);    
769   }
770   while (NULL != (rc = transmit_queue_head))
771   {
772     GNUNET_STATISTICS_update (stats,
773                               gettext_noop ("# DNS requests aborted (tunnel down)"),
774                               1, GNUNET_NO);
775     GNUNET_CONTAINER_DLL_remove (transmit_queue_head,
776                                  transmit_queue_tail,
777                                  rc);
778     GNUNET_DNS_request_drop (rc->rh);
779     GNUNET_SCHEDULER_cancel (rc->timeout_task);
780     GNUNET_free (rc);    
781   }
782 }
783
784
785 /**
786  * Function scheduled as very last function, cleans up after us
787  *
788  * @param cls closure, NULL
789  * @param tskctx scheduler context, unused
790  */
791 static void
792 cleanup (void *cls,
793          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
794 {
795   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796               "Protocol translation daemon is shutting down now\n");
797   if (NULL != vpn_handle)
798   {
799     GNUNET_VPN_disconnect (vpn_handle);
800     vpn_handle = NULL;
801   }
802   if (NULL != mesh_th)
803   {
804     GNUNET_MESH_notify_transmit_ready_cancel (mesh_th);
805     mesh_th = NULL;
806   }
807   if (NULL != mesh_tunnel)
808   {
809     GNUNET_MESH_tunnel_destroy (mesh_tunnel);
810     mesh_tunnel = NULL;
811   }
812   if (NULL != mesh_handle)
813   {
814     GNUNET_MESH_disconnect (mesh_handle);
815     mesh_handle = NULL;
816   }
817   abort_all_requests ();
818   if (NULL != dns_post_handle)
819   {
820     GNUNET_DNS_disconnect (dns_post_handle);
821     dns_post_handle = NULL;
822   }
823   if (NULL != dns_pre_handle)
824   {
825     GNUNET_DNS_disconnect (dns_pre_handle);
826     dns_pre_handle = NULL;
827   }
828   if (NULL != stats)
829   {
830     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
831     stats = NULL;
832   }
833   if (NULL != dht_get)
834   {
835     GNUNET_DHT_get_stop (dht_get);
836     dht_get = NULL;
837   }
838   if (NULL != dht)
839   {
840     GNUNET_DHT_disconnect (dht);
841     dht = NULL;
842   }
843 }
844
845
846
847 /**
848  * Function called whenever a tunnel is destroyed.  Should clean up
849  * the associated state and attempt to build a new one.
850  * 
851  * It must NOT call #GNUNET_MESH_tunnel_destroy on the tunnel.
852  *
853  * @param cls closure (set from #GNUNET_MESH_connect)
854  * @param tunnel connection to the other end (henceforth invalid)
855  * @param tunnel_ctx place where local state associated
856  *                   with the tunnel is stored
857  */
858 static void
859 mesh_tunnel_end_cb (void *cls,
860                     const struct GNUNET_MESH_Tunnel *tunnel, 
861                     void *tunnel_ctx)
862 {
863   // FIXME: do cleanup here!
864 }
865
866
867 /**
868  * Function called whenever we find an advertisement for a 
869  * DNS exit in the DHT.  If we don't have a mesh tunnel,
870  * we should build one; otherwise, we should save the
871  * advertisement for later use.
872  *
873  * @param cls closure
874  * @param exp when will this value expire
875  * @param key key of the result
876  * @param get_path peers on reply path (or NULL if not recorded)
877  *                 [0] = datastore's first neighbor, [length - 1] = local peer
878  * @param get_path_length number of entries in @a get_path
879  * @param put_path peers on the PUT path (or NULL if not recorded)
880  *                 [0] = origin, [length - 1] = datastore
881  * @param put_path_length number of entries in @a put_path
882  * @param type type of the result
883  * @param size number of bytes in @a data
884  * @param data pointer to the result data
885  */
886 static void
887 handle_dht_result (void *cls,
888                    struct GNUNET_TIME_Absolute exp,
889                    const struct GNUNET_HashCode *key,
890                    const struct GNUNET_PeerIdentity *get_path, 
891                    unsigned int get_path_length,
892                    const struct GNUNET_PeerIdentity *put_path,
893                    unsigned int put_path_length,
894                    enum GNUNET_BLOCK_Type type,
895                    size_t size, const void *data)
896 {
897   const struct GNUNET_DNS_Advertisement *ad;
898   struct GNUNET_PeerIdentity pid;
899
900   if (sizeof (struct GNUNET_DNS_Advertisement) != size)
901   {
902     GNUNET_break (0);
903     return;
904   }
905   ad = data;
906   GNUNET_CRYPTO_hash (&ad->peer,
907                       sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
908                       &pid.hashPubKey);
909   /* FIXME: decide between creating more mesh tunnels and
910      just remembering the peer ID */
911   mesh_tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
912                                            NULL /* FIXME: tunnel ctx */,
913                                            &pid,
914                                            PORT_PT, /* FIXME: DNS port, right? */
915                                            GNUNET_YES /* no buffer */,
916                                            GNUNET_NO /* reliable */);
917
918 }
919
920
921 /**
922  * @brief Main function that will be run by the scheduler.
923  *
924  * @param cls closure
925  * @param args remaining command-line arguments
926  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
927  * @param cfg_ configuration
928  */
929 static void
930 run (void *cls, char *const *args GNUNET_UNUSED,
931      const char *cfgfile GNUNET_UNUSED,
932      const struct GNUNET_CONFIGURATION_Handle *cfg_)
933 {
934   struct GNUNET_HashCode dns_key;
935
936   cfg = cfg_;
937   stats = GNUNET_STATISTICS_create ("pt", cfg);
938   ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV4");
939   ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV6"); 
940   dns_tunnel = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_DNS"); 
941   if (! (ipv4_pt || ipv6_pt || dns_tunnel))
942   {
943     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
944                 _("No useful service enabled.  Exiting.\n"));
945     GNUNET_SCHEDULER_shutdown ();
946     return;    
947   }
948   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
949   if (ipv4_pt || ipv6_pt)
950   {
951     dns_post_handle 
952       = GNUNET_DNS_connect (cfg, 
953                             GNUNET_DNS_FLAG_POST_RESOLUTION,
954                             &dns_post_request_handler, NULL);
955     if (NULL == dns_post_handle)       
956     {
957       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
958                   _("Failed to connect to %s service.  Exiting.\n"),
959                   "DNS");
960       GNUNET_SCHEDULER_shutdown ();
961       return;
962     }
963     vpn_handle = GNUNET_VPN_connect (cfg);
964     if (NULL == vpn_handle)
965     {
966       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
967                   _("Failed to connect to %s service.  Exiting.\n"),
968                   "VPN");
969       GNUNET_SCHEDULER_shutdown ();
970       return;
971     }
972   }
973   if (dns_tunnel)
974   {
975     static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
976       {&receive_dns_response, GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET, 0},
977       {NULL, 0, 0}
978     };
979
980     dns_pre_handle 
981       = GNUNET_DNS_connect (cfg, 
982                             GNUNET_DNS_FLAG_PRE_RESOLUTION,
983                             &dns_pre_request_handler, NULL);
984     if (NULL == dns_pre_handle)       
985     {
986       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
987                   _("Failed to connect to %s service.  Exiting.\n"),
988                   "DNS");
989       GNUNET_SCHEDULER_shutdown ();
990       return;
991     }
992     mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL,
993                                        &mesh_tunnel_end_cb,
994                                        mesh_handlers, NULL);
995     if (NULL == mesh_handle)
996     {
997       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
998                   _("Failed to connect to %s service.  Exiting.\n"),
999                   "MESH");
1000       GNUNET_SCHEDULER_shutdown ();
1001       return;
1002     }
1003     dht = GNUNET_DHT_connect (cfg, 1);
1004     if (NULL == dht)
1005     {
1006       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1007                   _("Failed to connect to %s service.  Exiting.\n"),
1008                   "DHT");
1009       GNUNET_SCHEDULER_shutdown ();
1010       return;
1011     }
1012     GNUNET_CRYPTO_hash ("dns", strlen ("dns"), &dns_key);
1013     dht_get = GNUNET_DHT_get_start (dht,
1014                                     GNUNET_BLOCK_TYPE_DNS,
1015                                     &dns_key,
1016                                     1,
1017                                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1018                                     NULL, 0,
1019                                     &handle_dht_result, NULL);
1020   }
1021 }
1022
1023
1024 /**
1025  * The main function
1026  *
1027  * @param argc number of arguments from the command line
1028  * @param argv command line arguments
1029  * @return 0 ok, 1 on error
1030  */
1031 int
1032 main (int argc, char *const *argv)
1033 {
1034   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1035     GNUNET_GETOPT_OPTION_END
1036   };
1037   int ret;
1038
1039   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1040     return 2;
1041   ret = (GNUNET_OK ==
1042          GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
1043                              gettext_noop
1044                              ("Daemon to run to perform IP protocol translation to GNUnet"),
1045                              options, &run, NULL)) ? 0 : 1;
1046   GNUNET_free ((void*) argv);
1047   return ret;
1048 }
1049
1050
1051 /* end of gnunet-daemon-pt.c */