aboutsummaryrefslogtreecommitdiff
path: root/src/pt/gnunet-daemon-pt.c
blob: 72fef144a8a707619e9cca7d83c637d6253ce86c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
/*
     This file is part of GNUnet.
     (C) 2010, 2012 Christian Grothoff

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file pt/gnunet-daemon-pt.c
 * @brief tool to manipulate DNS and VPN services to perform protocol translation (IPvX over GNUnet)
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_dns_service.h"
#include "gnunet_dnsparser_lib.h"
#include "gnunet_cadet_service.h"
#include "gnunet_tun_lib.h"
#include "gnunet_dht_service.h"
#include "gnunet_vpn_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet_applications.h"
#include "block_dns.h"


/**
 * After how long do we time out if we could not get an IP from VPN or CADET?
 */
#define TIMEOUT GNUNET_TIME_UNIT_MINUTES

/**
 * How many bytes of payload do we allow at most for a DNS reply?
 * Given that this is pretty much limited to loopback, we can be
 * pretty high (Linux loopback defaults to 16k, most local UDP packets
 * should survive up to 9k (NFS), so 8k should be pretty safe in
 * general).
 */
#define MAX_DNS_SIZE (8 * 1024)

/**
 * How many channels do we open at most at the same time?
 */
#define MAX_OPEN_TUNNELS 4


/**
 * Which group of DNS records are we currently processing?
 */
enum RequestGroup
{
  /**
   * DNS answers
   */
  ANSWERS = 0,

  /**
   * DNS authority records
   */
  AUTHORITY_RECORDS = 1,

  /**
   * DNS additional records
   */
  ADDITIONAL_RECORDS = 2,

  /**
   * We're done processing.
   */
  END = 3
};


/**
 * Information tracked per DNS reply that we are processing.
 */
struct ReplyContext
{
  /**
   * Handle to submit the final result.
   */
  struct GNUNET_DNS_RequestHandle *rh;

  /**
   * DNS packet that is being modified.
   */
  struct GNUNET_DNSPARSER_Packet *dns;

  /**
   * Active redirection request with the VPN.
   */
  struct GNUNET_VPN_RedirectionRequest *rr;

  /**
   * Record for which we have an active redirection request.
   */
  struct GNUNET_DNSPARSER_Record *rec;

  /**
   * Offset in the current record group that is being modified.
   */
  unsigned int offset;

  /**
   * Group that is being modified
   */
  enum RequestGroup group;

};


/**
 * Handle to a peer that advertised that it is willing to serve
 * as a DNS exit.  We try to keep a few channels open and a few
 * peers in reserve.
 */
struct CadetExit
{

  /**
   * Kept in a DLL.
   */
  struct CadetExit *next;

  /**
   * Kept in a DLL.
   */
  struct CadetExit *prev;

  /**
   * Channel we use for DNS requests over CADET, NULL if we did
   * not initialze a channel to this peer yet.
   */
  struct GNUNET_CADET_Channel *cadet_channel;

  /**
   * At what time did the peer's advertisement expire?
   */
  struct GNUNET_TIME_Absolute expiration;

  /**
   * Head of DLL of requests waiting for a response.
   */
  struct RequestContext *receive_queue_head;

  /**
   * Tail of DLL of requests waiting for a response.
   */
  struct RequestContext *receive_queue_tail;

  /**
   * Head of DLL of requests to be transmitted to a cadet_channel.
   */
  struct RequestContext *transmit_queue_head;

  /**
   * Tail of DLL of requests to be transmitted to a cadet_channel.
   */
  struct RequestContext *transmit_queue_tail;

  /**
   * Active transmission request for this channel (or NULL).
   */
  struct GNUNET_CADET_TransmitHandle *cadet_th;

  /**
   * Identity of the peer that is providing the exit for us.
   */
  struct GNUNET_PeerIdentity peer;

  /**
   * How many DNS requests did we transmit via this channel?
   */
  unsigned int num_transmitted;

  /**
   * How many DNS requests were answered via this channel?
   */
  unsigned int num_answered;

};



/**
 * State we keep for a request that is going out via CADET.
 */
struct RequestContext
{
  /**
   * We keep these in a DLL.
   */
  struct RequestContext *next;

  /**
   * We keep these in a DLL.
   */
  struct RequestContext *prev;

  /**
   * Exit that was chosen for this request.
   */
  struct CadetExit *exit;

  /**
   * Handle for interaction with DNS service.
   */
  struct GNUNET_DNS_RequestHandle *rh;

  /**
   * Message we're sending out via CADET, allocated at the
   * end of this struct.
   */
  const struct GNUNET_MessageHeader *cadet_message;

  /**
   * Task used to abort this operation with timeout.
   */
  struct GNUNET_SCHEDULER_Task * timeout_task;

  /**
   * Length of the request message that follows this struct.
   */
  uint16_t mlen;

  /**
   * ID of the original DNS request (used to match the reply).
   */
  uint16_t dns_id;

  /**
   * #GNUNET_NO if this request is still in the transmit_queue,
   * #GNUNET_YES if we are in the receive_queue.
   */
  int16_t was_transmitted;

};


/**
 * Head of DLL of cadet exits.  Cadet exits with an open channel are
 * always at the beginning (so we do not have to traverse the entire
 * list to find them).
 */
static struct CadetExit *exit_head;

/**
 * Tail of DLL of cadet exits.
 */
static struct CadetExit *exit_tail;

/**
 * The handle to the configuration used throughout the process
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * The handle to the VPN
 */
static struct GNUNET_VPN_Handle *vpn_handle;

/**
 * The handle to the CADET service
 */
static struct GNUNET_CADET_Handle *cadet_handle;

/**
 * Statistics.
 */
static struct GNUNET_STATISTICS_Handle *stats;

/**
 * The handle to DNS post-resolution modifications.
 */
static struct GNUNET_DNS_Handle *dns_post_handle;

/**
 * The handle to DNS pre-resolution modifications.
 */
static struct GNUNET_DNS_Handle *dns_pre_handle;

/**
 * Handle to access the DHT.
 */
static struct GNUNET_DHT_Handle *dht;

/**
 * Our DHT GET operation to find DNS exits.
 */
static struct GNUNET_DHT_GetHandle *dht_get;

/**
 * Are we doing IPv4-pt?
 */
static int ipv4_pt;

/**
 * Are we doing IPv6-pt?
 */
static int ipv6_pt;

/**
 * Are we channeling DNS queries?
 */
static int dns_channel;

/**
 * Number of DNS exit peers we currently have in the cadet channel.
 * Used to see if using the cadet channel makes any sense right now,
 * as well as to decide if we should open new channels.
 */
static unsigned int dns_exit_available;


/**
 * We are short on cadet exits, try to open another one.
 */
static void
try_open_exit ()
{
  struct CadetExit *pos;
  uint32_t candidate_count;
  uint32_t candidate_selected;

  candidate_count = 0;
  for (pos = exit_head; NULL != pos; pos = pos->next)
    if (NULL == pos->cadet_channel)
      candidate_count++;
  candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
						 candidate_count);
  candidate_count = 0;
  for (pos = exit_head; NULL != pos; pos = pos->next)
    if (NULL == pos->cadet_channel)
    {
      candidate_count++;
      if (candidate_selected < candidate_count)
      {
	/* move to the head of the DLL */
	pos->cadet_channel = GNUNET_CADET_channel_create (cadet_handle,
						      pos,
						      &pos->peer,
						      GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
						      GNUNET_CADET_OPTION_DEFAULT);
	if (NULL == pos->cadet_channel)
	{
	  GNUNET_break (0);
	  continue;
	}
	GNUNET_CONTAINER_DLL_remove (exit_head,
				     exit_tail,
				     pos);
	GNUNET_CONTAINER_DLL_insert (exit_head,
				     exit_tail,
				     pos);
	dns_exit_available++;
	return;
      }
    }
  GNUNET_assert (NULL == exit_head);
}


/**
 * Compute the weight of the given exit.  The higher the weight,
 * the more likely it will be that the channel will be chosen.
 * A weigt of zero means that we should close the channel as it
 * is so bad, that we should not use it.
 *
 * @param exit exit to calculate the weight for
 * @return weight of the channel
 */
static uint32_t
get_channel_weight (struct CadetExit *exit)
{
  uint32_t dropped;
  uint32_t drop_percent;
  uint32_t good_percent;

  GNUNET_assert (exit->num_transmitted >= exit->num_answered);
  dropped = exit->num_transmitted - exit->num_answered;
  if (exit->num_transmitted > 0)
    drop_percent = (uint32_t) ((100LL * dropped) / exit->num_transmitted);
  else
    drop_percent = 50; /* no data */
  if ( (exit->num_transmitted > 20) &&
       (drop_percent > 25) )
    return 0; /* statistically significant, and > 25% loss, die */
  good_percent = 100 - drop_percent;
  GNUNET_assert (0 != good_percent);
  if ( UINT32_MAX / good_percent / good_percent < exit->num_transmitted)
    return UINT32_MAX; /* formula below would overflow */
  return 1 + good_percent * good_percent * exit->num_transmitted;
}


/**
 * Choose a cadet exit for a DNS request.  We try to use a channel
 * that is reliable and currently available.  All existing
 * channels are given a base weight of 1, plus a score relating
 * to the total number of queries answered in relation to the
 * total number of queries we sent to that channel.  That
 * score is doubled if the channel is currently idle.
 *
 * @return NULL if no exit is known, otherwise the
 *         exit that we should use to queue a message with
 */
static struct CadetExit *
choose_exit ()
{
  struct CadetExit *pos;
  uint64_t total_transmitted;
  uint64_t selected_offset;
  uint32_t channel_weight;

  total_transmitted = 0;
  for (pos = exit_head; NULL != pos; pos = pos->next)
  {
    if (NULL == pos->cadet_channel)
      break;
    channel_weight = get_channel_weight (pos);
    total_transmitted += channel_weight;
    /* double weight for idle channels */
    if (NULL == pos->cadet_th)
      total_transmitted += channel_weight;
  }
  if (0 == total_transmitted)
  {
    /* no channels available, or only a very bad one... */
    return exit_head;
  }
  selected_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
					      total_transmitted);
  total_transmitted = 0;
  for (pos = exit_head; NULL != pos; pos = pos->next)
  {
    if (NULL == pos->cadet_channel)
      break;
    channel_weight = get_channel_weight (pos);
    total_transmitted += channel_weight;
    /* double weight for idle channels */
    if (NULL == pos->cadet_th)
      total_transmitted += channel_weight;
    if (total_transmitted > selected_offset)
      return pos;
  }
  GNUNET_break (0);
  return NULL;
}


/**
 * We're done modifying all records in the response.  Submit the reply
 * and free the resources of the rc.
 *
 * @param rc context to process
 */
static void
finish_request (struct ReplyContext *rc)
{
  char *buf;
  size_t buf_len;

  if (GNUNET_SYSERR ==
      GNUNET_DNSPARSER_pack (rc->dns,
			     MAX_DNS_SIZE,
			     &buf,
			     &buf_len))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Failed to pack DNS request.  Dropping.\n"));
    GNUNET_DNS_request_drop (rc->rh);
  }
  else
  {
    GNUNET_STATISTICS_update (stats,
			      gettext_noop ("# DNS requests mapped to VPN"),
			      1, GNUNET_NO);
    GNUNET_DNS_request_answer (rc->rh,
			       buf_len, buf);
    GNUNET_free (buf);
  }
  GNUNET_DNSPARSER_free_packet (rc->dns);
  GNUNET_free (rc);
}


/**
 * Process the next record of the given request context.
 * When done, submit the reply and free the resources of
 * the rc.
 *
 * @param rc context to process
 */
static void
submit_request (struct ReplyContext *rc);


/**
 * Callback invoked from the VPN service once a redirection is
 * available.  Provides the IP address that can now be used to
 * reach the requested destination.  We substitute the active
 * record and then continue with 'submit_request' to look at
 * the other records.
 *
 * @param cls our `struct ReplyContext`
 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
 *                will match 'result_af' from the request
 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
 *                that the VPN allocated for the redirection;
 *                traffic to this IP will now be redirected to the
 *                specified target peer; NULL on error
 */
static void
vpn_allocation_callback (void *cls,
			 int af,
			 const void *address)
{
  struct ReplyContext *rc = cls;

  rc->rr = NULL;
  if (af == AF_UNSPEC)
  {
    GNUNET_DNS_request_drop (rc->rh);
    GNUNET_DNSPARSER_free_packet (rc->dns);
    GNUNET_free (rc);
    return;
  }
  GNUNET_STATISTICS_update (stats,
			    gettext_noop ("# DNS records modified"),
			    1, GNUNET_NO);
  switch (rc->rec->type)
  {
  case GNUNET_DNSPARSER_TYPE_A:
    GNUNET_assert (AF_INET == af);
    memcpy (rc->rec->data.raw.data, address, sizeof (struct in_addr));
    break;
  case GNUNET_DNSPARSER_TYPE_AAAA:
    GNUNET_assert (AF_INET6 == af);
    memcpy (rc->rec->data.raw.data, address, sizeof (struct in6_addr));
    break;
  default:
    GNUNET_assert (0);
    return;
  }
  rc->rec = NULL;
  submit_request (rc);
}


/**
 * Modify the given DNS record by asking VPN to create a channel
 * to the given address.  When done, continue with submitting
 * other records from the request context ('submit_request' is
 * our continuation).
 *
 * @param rc context to process
 * @param rec record to modify
 */
static void
modify_address (struct ReplyContext *rc,
		struct GNUNET_DNSPARSER_Record *rec)
{
  int af;

  switch (rec->type)
  {
  case GNUNET_DNSPARSER_TYPE_A:
    af = AF_INET;
    GNUNET_assert (rec->data.raw.data_len == sizeof (struct in_addr));
    break;
  case GNUNET_DNSPARSER_TYPE_AAAA:
    af = AF_INET6;
    GNUNET_assert (rec->data.raw.data_len == sizeof (struct in6_addr));
    break;
  default:
    GNUNET_assert (0);
    return;
  }
  rc->rec = rec;
  rc->rr = GNUNET_VPN_redirect_to_ip (vpn_handle,
				      af, af,
				      rec->data.raw.data,
				      GNUNET_TIME_relative_to_absolute (TIMEOUT),
				      &vpn_allocation_callback,
				      rc);
}


/**
 * Process the next record of the given request context.
 * When done, submit the reply and free the resources of
 * the rc.
 *
 * @param rc context to process
 */
static void
submit_request (struct ReplyContext *rc)
{
  struct GNUNET_DNSPARSER_Record *ra;
  unsigned int ra_len;
  unsigned int i;

  while (1)
  {
    switch (rc->group)
    {
    case ANSWERS:
      ra = rc->dns->answers;
      ra_len = rc->dns->num_answers;
      break;
    case AUTHORITY_RECORDS:
      ra = rc->dns->authority_records;
      ra_len = rc->dns->num_authority_records;
      break;
    case ADDITIONAL_RECORDS:
      ra = rc->dns->additional_records;
      ra_len = rc->dns->num_additional_records;
      break;
    case END:
      finish_request (rc);
      return;
    default:
      GNUNET_assert (0);
    }
    for (i=rc->offset;i<ra_len;i++)
    {
      switch (ra[i].type)
      {
      case GNUNET_DNSPARSER_TYPE_A:
	if (ipv4_pt)
	{
	  rc->offset = i + 1;
	  modify_address (rc, &ra[i]);
	  return;
	}
	break;
      case GNUNET_DNSPARSER_TYPE_AAAA:
	if (ipv6_pt)
	{
	  rc->offset = i + 1;
	  modify_address (rc, &ra[i]);
	  return;
	}
	break;
      }
    }
    rc->group++;
  }
}


/**
 * Test if any of the given records need protocol-translation work.
 *
 * @param ra array of records
 * @param ra_len number of entries in @a ra
 * @return #GNUNET_YES if any of the given records require protocol-translation
 */
static int
work_test (const struct GNUNET_DNSPARSER_Record *ra,
	   unsigned int ra_len)
{
  unsigned int i;

  for (i=0;i<ra_len;i++)
  {
    switch (ra[i].type)
    {
    case GNUNET_DNSPARSER_TYPE_A:
      if (ipv4_pt)
	return GNUNET_YES;
      break;
    case GNUNET_DNSPARSER_TYPE_AAAA:
      if (ipv6_pt)
	return GNUNET_YES;
      break;
    }
  }
  return GNUNET_NO;
}


/**
 * This function is called AFTER we got an IP address for a
 * DNS request.  Now, the PT daemon has the chance to substitute
 * the IP address with one from the VPN range to channel requests
 * destined for this IP address via VPN and CADET.
 *
 * @param cls closure
 * @param rh request handle to user for reply
 * @param request_length number of bytes in request
 * @param request udp payload of the DNS request
 */
static void
dns_post_request_handler (void *cls,
			  struct GNUNET_DNS_RequestHandle *rh,
			  size_t request_length,
			  const char *request)
{
  struct GNUNET_DNSPARSER_Packet *dns;
  struct ReplyContext *rc;
  int work;

  GNUNET_STATISTICS_update (stats,
			    gettext_noop ("# DNS replies intercepted"),
			    1, GNUNET_NO);
  dns = GNUNET_DNSPARSER_parse (request, request_length);
  if (NULL == dns)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Failed to parse DNS request.  Dropping.\n"));
    GNUNET_DNS_request_drop (rh);
    return;
  }
  work = GNUNET_NO;
  work |= work_test (dns->answers, dns->num_answers);
  work |= work_test (dns->authority_records, dns->num_authority_records);
  work |= work_test (dns->additional_records, dns->num_additional_records);
  if (! work)
  {
    GNUNET_DNS_request_forward (rh);
    GNUNET_DNSPARSER_free_packet (dns);
    return;
  }
  rc = GNUNET_new (struct ReplyContext);
  rc->rh = rh;
  rc->dns = dns;
  rc->offset = 0;
  rc->group = ANSWERS;
  submit_request (rc);
}


/**
 * Transmit a DNS request via CADET and move the request
 * handle to the receive queue.
 *
 * @param cls the `struct CadetExit`
 * @param size number of bytes available in buf
 * @param buf where to copy the message
 * @return number of bytes written to buf
 */
static size_t
transmit_dns_request_to_cadet (void *cls,
			      size_t size,
			      void *buf)
{
  struct CadetExit *exit = cls;
  struct RequestContext *rc;
  size_t mlen;

  exit->cadet_th = NULL;
  if (NULL == (rc = exit->transmit_queue_head))
    return 0;
  mlen = rc->mlen;
  if (mlen > size)
  {
    exit->cadet_th = GNUNET_CADET_notify_transmit_ready (exit->cadet_channel,
						       GNUNET_NO,
						       TIMEOUT,
						       mlen,
						       &transmit_dns_request_to_cadet,
						       exit);
    return 0;
  }
  GNUNET_assert (GNUNET_NO == rc->was_transmitted);
  memcpy (buf, rc->cadet_message, mlen);
  GNUNET_CONTAINER_DLL_remove (exit->transmit_queue_head,
			       exit->transmit_queue_tail,
			       rc);
  rc->was_transmitted = GNUNET_YES;
  GNUNET_CONTAINER_DLL_insert (exit->receive_queue_head,
			       exit->receive_queue_tail,
			       rc);
  rc = exit->transmit_queue_head;
  if (NULL != rc)
    exit->cadet_th = GNUNET_CADET_notify_transmit_ready (exit->cadet_channel,
						       GNUNET_NO,
						       TIMEOUT,
						       rc->mlen,
						       &transmit_dns_request_to_cadet,
						       exit);
  return mlen;
}


/**
 * Task run if the time to answer a DNS request via CADET is over.
 *
 * @param cls the `struct RequestContext` to abort
 * @param tc scheduler context
 */
static void
timeout_request (void *cls,
		 const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct RequestContext *rc = cls;
  struct CadetExit *exit = rc->exit;

  if (rc->was_transmitted)
  {
    exit->num_transmitted++;
    GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
				 exit->receive_queue_tail,
				 rc);
  }
  else
  {
    GNUNET_CONTAINER_DLL_remove (exit->transmit_queue_head,
				 exit->transmit_queue_tail,
				 rc);
  }
  GNUNET_STATISTICS_update (stats,
			    gettext_noop ("# DNS requests dropped (timeout)"),
			    1, GNUNET_NO);
  GNUNET_DNS_request_drop (rc->rh);
  GNUNET_free (rc);
  if ( (0 == get_channel_weight (exit)) &&
       (NULL == exit->receive_queue_head) &&
       (NULL == exit->transmit_queue_head) )
  {
    /* this straw broke the camel's back: this channel now has
       such a low score that it will not be used; close it! */
    GNUNET_assert (NULL == exit->cadet_th);
    GNUNET_CADET_channel_destroy (exit->cadet_channel);
    exit->cadet_channel = NULL;
    GNUNET_CONTAINER_DLL_remove (exit_head,
				 exit_tail,
				 exit);
    GNUNET_CONTAINER_DLL_insert_tail (exit_head,
				      exit_tail,
				      exit);
    /* go back to semi-innocent: mark as not great, but
       avoid a prohibitively negative score (see
       #get_channel_weight, which checks for a certain
       minimum number of transmissions before making
       up an opinion) */
    exit->num_transmitted = 5;
    exit->num_answered = 0;
    dns_exit_available--;
    /* now try to open an alternative exit */
    try_open_exit ();
  }
}


/**
 * This function is called *before* the DNS request has been
 * given to a "local" DNS resolver.  Channeling for DNS requests
 * was enabled, so we now need to send the request via some CADET
 * channel to a DNS EXIT for resolution.
 *
 * @param cls closure
 * @param rh request handle to user for reply
 * @param request_length number of bytes in request
 * @param request udp payload of the DNS request
 */
static void
dns_pre_request_handler (void *cls,
			 struct GNUNET_DNS_RequestHandle *rh,
			 size_t request_length,
			 const char *request)
{
  struct RequestContext *rc;
  size_t mlen;
  struct GNUNET_MessageHeader hdr;
  struct GNUNET_TUN_DnsHeader dns;
  struct CadetExit *exit;

  GNUNET_STATISTICS_update (stats,
			    gettext_noop ("# DNS requests intercepted"),
			    1, GNUNET_NO);
  if (0 == dns_exit_available)
  {
    GNUNET_STATISTICS_update (stats,
			      gettext_noop ("# DNS requests dropped (DNS cadet channel down)"),
			      1, GNUNET_NO);
    GNUNET_DNS_request_drop (rh);
    return;
  }
  if (request_length < sizeof (dns))
  {
    GNUNET_STATISTICS_update (stats,
			      gettext_noop ("# DNS requests dropped (malformed)"),
			      1, GNUNET_NO);
    GNUNET_DNS_request_drop (rh);
    return;
  }
  memcpy (&dns, request, sizeof (dns));
  mlen = sizeof (struct GNUNET_MessageHeader) + request_length;
  exit = choose_exit ();
  GNUNET_assert (NULL != exit);
  GNUNET_assert (NULL != exit->cadet_channel);
  rc = GNUNET_malloc (sizeof (struct RequestContext) + mlen);
  rc->exit = exit;
  rc->rh = rh;
  rc->cadet_message = (const struct GNUNET_MessageHeader*) &rc[1];
  rc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
						   &timeout_request,
						   rc);
  rc->dns_id = dns.id;
  rc->mlen = mlen;
  hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET);
  hdr.size = htons (mlen);
  memcpy (&rc[1], &hdr, sizeof (struct GNUNET_MessageHeader));
  memcpy (&(((char*)&rc[1])[sizeof (struct GNUNET_MessageHeader)]),
	  request,
	  request_length);
  GNUNET_CONTAINER_DLL_insert_tail (exit->transmit_queue_head,
				    exit->transmit_queue_tail,
				    rc);
  if (NULL == exit->cadet_th)
    exit->cadet_th = GNUNET_CADET_notify_transmit_ready (exit->cadet_channel,
						       GNUNET_NO,
						       TIMEOUT,
						       mlen,
						       &transmit_dns_request_to_cadet,
						       exit);
}


/**
 * Process a request via cadet to perform a DNS query.
 *
 * @param cls NULL
 * @param channel connection to the other end
 * @param channel_ctx pointer to our `struct CadetExit`
 * @param message the actual message
 * @return #GNUNET_OK to keep the connection open,
 *         #GNUNET_SYSERR to close it (signal serious error)
 */
static int
receive_dns_response (void *cls,
		      struct GNUNET_CADET_Channel *channel,
		      void **channel_ctx,
		      const struct GNUNET_MessageHeader *message)
{
  struct CadetExit *exit = *channel_ctx;
  struct GNUNET_TUN_DnsHeader dns;
  size_t mlen;
  struct RequestContext *rc;

  mlen = ntohs (message->size);
  mlen -= sizeof (struct GNUNET_MessageHeader);
  if (mlen < sizeof (struct GNUNET_TUN_DnsHeader))
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  memcpy (&dns, &message[1], sizeof (dns));
  for (rc = exit->receive_queue_head; NULL != rc; rc = rc->next)
  {
    GNUNET_assert (GNUNET_YES == rc->was_transmitted);
    if (dns.id == rc->dns_id)
    {
      GNUNET_STATISTICS_update (stats,
				gettext_noop ("# DNS replies received"),
				1, GNUNET_NO);
      GNUNET_DNS_request_answer (rc->rh,
				 mlen,
				 (const void*) &message[1]);
      GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
				   exit->receive_queue_tail,
				   rc);
      GNUNET_SCHEDULER_cancel (rc->timeout_task);
      GNUNET_free (rc);
      exit->num_answered++;
      exit->num_transmitted++;
      return GNUNET_OK;
    }
  }
  GNUNET_STATISTICS_update (stats,
			    gettext_noop ("# DNS replies dropped (too late?)"),
			    1, GNUNET_NO);
  return GNUNET_OK;
}


/**
 * Abort all pending DNS requests with the given cadet exit.
 *
 * @param exit cadet exit to abort requests for
 */
static void
abort_all_requests (struct CadetExit *exit)
{
  struct RequestContext *rc;

  while (NULL != (rc = exit->receive_queue_head))
  {
    GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
				 exit->receive_queue_tail,
				 rc);
    GNUNET_DNS_request_drop (rc->rh);
    GNUNET_SCHEDULER_cancel (rc->timeout_task);
    GNUNET_free (rc);
  }
  while (NULL != (rc = exit->transmit_queue_head))
  {
    GNUNET_CONTAINER_DLL_remove (exit->transmit_queue_head,
				 exit->transmit_queue_tail,
				 rc);
    GNUNET_DNS_request_drop (rc->rh);
    GNUNET_SCHEDULER_cancel (rc->timeout_task);
    GNUNET_free (rc);
  }
}


/**
 * Function scheduled as very last function, cleans up after us
 *
 * @param cls closure, NULL
 * @param tskctx scheduler context, unused
 */
static void
cleanup (void *cls,
         const struct GNUNET_SCHEDULER_TaskContext *tskctx)
{
  struct CadetExit *exit;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Protocol translation daemon is shutting down now\n");
  if (NULL != vpn_handle)
  {
    GNUNET_VPN_disconnect (vpn_handle);
    vpn_handle = NULL;
  }
  while (NULL != (exit = exit_head))
  {
    GNUNET_CONTAINER_DLL_remove (exit_head,
				 exit_tail,
				 exit);
    if (NULL != exit->cadet_th)
    {
      GNUNET_CADET_notify_transmit_ready_cancel (exit->cadet_th);
      exit->cadet_th = NULL;
    }
    if (NULL != exit->cadet_channel)
    {
      GNUNET_CADET_channel_destroy (exit->cadet_channel);
      exit->cadet_channel = NULL;
    }
    abort_all_requests (exit);
    GNUNET_free (exit);
  }
  if (NULL != cadet_handle)
  {
    GNUNET_CADET_disconnect (cadet_handle);
    cadet_handle = NULL;
  }
  if (NULL != dns_post_handle)
  {
    GNUNET_DNS_disconnect (dns_post_handle);
    dns_post_handle = NULL;
  }
  if (NULL != dns_pre_handle)
  {
    GNUNET_DNS_disconnect (dns_pre_handle);
    dns_pre_handle = NULL;
  }
  if (NULL != stats)
  {
    GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
    stats = NULL;
  }
  if (NULL != dht_get)
  {
    GNUNET_DHT_get_stop (dht_get);
    dht_get = NULL;
  }
  if (NULL != dht)
  {
    GNUNET_DHT_disconnect (dht);
    dht = NULL;
  }
}


/**
 * Function called whenever a channel is destroyed.  Should clean up
 * the associated state and attempt to build a new one.
 *
 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
 *
 * @param cls closure (the `struct CadetExit` set from #GNUNET_CADET_connect)
 * @param channel connection to the other end (henceforth invalid)
 * @param channel_ctx place where local state associated
 *                   with the channel is stored
 */
static void
cadet_channel_end_cb (void *cls,
		    const struct GNUNET_CADET_Channel *channel,
		    void *channel_ctx)
{
  struct CadetExit *exit = channel_ctx;
  struct CadetExit *alt;
  struct RequestContext *rc;

  if (NULL != exit->cadet_th)
  {
    GNUNET_CADET_notify_transmit_ready_cancel (exit->cadet_th);
    exit->cadet_th = NULL;
  }
  exit->cadet_channel = NULL;
  dns_exit_available--;
  /* open alternative channels */
  try_open_exit ();
  if (NULL == exit->cadet_channel)
  {
    /* our channel is now closed, move our requests to an alternative
       channel */
    alt = choose_exit ();
    while (NULL != (rc = exit->transmit_queue_head))
    {
      GNUNET_CONTAINER_DLL_remove (exit->transmit_queue_head,
				   exit->transmit_queue_tail,
				   rc);
      rc->exit = alt;
      GNUNET_CONTAINER_DLL_insert (alt->transmit_queue_head,
				   alt->transmit_queue_tail,
				   rc);
    }
    while (NULL != (rc = exit->receive_queue_head))
    {
      GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
				   exit->receive_queue_tail,
				   rc);
      rc->was_transmitted = GNUNET_NO;
      rc->exit = alt;
      GNUNET_CONTAINER_DLL_insert (alt->transmit_queue_head,
				   alt->transmit_queue_tail,
				   rc);
    }
  }
  else
  {
    /* the same peer was chosen, just make sure the queue processing is restarted */
    alt = exit;
  }
  if ( (NULL == alt->cadet_th) &&
       (NULL != (rc = alt->transmit_queue_head)) )
    alt->cadet_th = GNUNET_CADET_notify_transmit_ready (alt->cadet_channel,
						      GNUNET_NO,
						      TIMEOUT,
						      rc->mlen,
						      &transmit_dns_request_to_cadet,
						      alt);
}


/**
 * Function called whenever we find an advertisement for a
 * DNS exit in the DHT.  If we don't have a cadet channel,
 * we should build one; otherwise, we should save the
 * advertisement for later use.
 *
 * @param cls closure
 * @param exp when will this value expire
 * @param key key of the result
 * @param get_path peers on reply path (or NULL if not recorded)
 *                 [0] = datastore's first neighbor, [length - 1] = local peer
 * @param get_path_length number of entries in @a get_path
 * @param put_path peers on the PUT path (or NULL if not recorded)
 *                 [0] = origin, [length - 1] = datastore
 * @param put_path_length number of entries in @a put_path
 * @param type type of the result
 * @param size number of bytes in @a data
 * @param data pointer to the result data
 */
static void
handle_dht_result (void *cls,
		   struct GNUNET_TIME_Absolute exp,
		   const struct GNUNET_HashCode *key,
		   const struct GNUNET_PeerIdentity *get_path,
		   unsigned int get_path_length,
		   const struct GNUNET_PeerIdentity *put_path,
		   unsigned int put_path_length,
		   enum GNUNET_BLOCK_Type type,
		   size_t size, const void *data)
{
  const struct GNUNET_DNS_Advertisement *ad;
  struct CadetExit *exit;

  if (sizeof (struct GNUNET_DNS_Advertisement) != size)
  {
    GNUNET_break (0);
    return;
  }
  ad = data;
  for (exit = exit_head; NULL != exit; exit = exit->next)
    if (0 == memcmp (&ad->peer,
		     &exit->peer,
		     sizeof (struct GNUNET_PeerIdentity)))
      break;
  if (NULL == exit)
  {
    exit = GNUNET_new (struct CadetExit);
    exit->peer = ad->peer;
    /* channel is closed, so insert at the end */
    GNUNET_CONTAINER_DLL_insert_tail (exit_head,
				      exit_tail,
				      exit);
  }
  exit->expiration = GNUNET_TIME_absolute_max (exit->expiration,
					       GNUNET_TIME_absolute_ntoh (ad->expiration_time));
  if (dns_exit_available < MAX_OPEN_TUNNELS)
    try_open_exit ();
}


/**
 * @brief Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg_ configuration
 */
static void
run (void *cls, char *const *args GNUNET_UNUSED,
     const char *cfgfile GNUNET_UNUSED,
     const struct GNUNET_CONFIGURATION_Handle *cfg_)
{
  struct GNUNET_HashCode dns_key;

  cfg = cfg_;
  stats = GNUNET_STATISTICS_create ("pt", cfg);
  ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV4");
  ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV6");
  dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_DNS");
  if (! (ipv4_pt || ipv6_pt || dns_channel))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("No useful service enabled.  Exiting.\n"));
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
  if (ipv4_pt || ipv6_pt)
  {
    dns_post_handle
      = GNUNET_DNS_connect (cfg,
			    GNUNET_DNS_FLAG_POST_RESOLUTION,
			    &dns_post_request_handler, NULL);
    if (NULL == dns_post_handle)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Failed to connect to %s service.  Exiting.\n"),
		  "DNS");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    vpn_handle = GNUNET_VPN_connect (cfg);
    if (NULL == vpn_handle)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Failed to connect to %s service.  Exiting.\n"),
		  "VPN");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
  }
  if (dns_channel)
  {
    static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
      {&receive_dns_response, GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET, 0},
      {NULL, 0, 0}
    };

    dns_pre_handle
      = GNUNET_DNS_connect (cfg,
			    GNUNET_DNS_FLAG_PRE_RESOLUTION,
			    &dns_pre_request_handler, NULL);
    if (NULL == dns_pre_handle)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Failed to connect to %s service.  Exiting.\n"),
		  "DNS");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    cadet_handle = GNUNET_CADET_connect (cfg, NULL, NULL,
				       &cadet_channel_end_cb,
				       cadet_handlers, NULL);
    if (NULL == cadet_handle)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Failed to connect to %s service.  Exiting.\n"),
		  "CADET");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    dht = GNUNET_DHT_connect (cfg, 1);
    if (NULL == dht)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Failed to connect to %s service.  Exiting.\n"),
		  "DHT");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    GNUNET_CRYPTO_hash ("dns", strlen ("dns"), &dns_key);
    dht_get = GNUNET_DHT_get_start (dht,
				    GNUNET_BLOCK_TYPE_DNS,
				    &dns_key,
				    1,
				    GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
				    NULL, 0,
				    &handle_dht_result, NULL);
  }
}


/**
 * The main function
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
    return 2;
  ret = (GNUNET_OK ==
	 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-pt",
			     gettext_noop
			     ("Daemon to run to perform IP protocol translation to GNUnet"),
			     options, &run, NULL)) ? 0 : 1;
  GNUNET_free ((void*) argv);
  return ret;
}


/* end of gnunet-daemon-pt.c */