summaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-daemon-exit.c
blob: e7d904120adb3e7d38d3dae17ac84ef6379152a8 (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
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
/*
     This file is part of GNUnet.
     (C) 2010 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 vpn/gnunet-daemon-exit.c
 * @brief
 * @author Philipp Toelke
 */
#include <platform.h>
#include <gnunet_common.h>
#include <gnunet_program_lib.h>
#include <gnunet_protocols.h>
#include <gnunet_applications.h>
#include <gnunet_mesh_service.h>
#include <gnunet_constants.h>
#include <string.h>

#include "gnunet-vpn-packet.h"
#include "gnunet-helper-vpn-api.h"
#include "gnunet-vpn-checksum.h"

GNUNET_SCHEDULER_TaskIdentifier shs_task;

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

/**
 * The handle to the helper
 */
struct GNUNET_VPN_HELPER_Handle *helper_handle;

/**
 * Final status code.
 */
static int ret;

/**
 * The handle to mesh
 */
static struct GNUNET_MESH_Handle *mesh_handle;

/**
 * This hashmaps contains the mapping from peer, service-descriptor,
 * source-port and destination-port to a struct redirect_state
 */
static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;

/**
 * If there are at least this many udp-Connections, old ones will be removed
 */
static long long unsigned int max_udp_connections = 200;

/**
 * If there are at least this many tcp-Connections, old ones will be removed
 */
static long long unsigned int max_tcp_connections = 200;

struct remote_addr
{
  char addrlen;
  unsigned char addr[16];
  char proto;
};

/**
 * This struct is saved into the services-hashmap
 */
struct redirect_service
{
  /**
   * One of 4 or 6
   */
  unsigned int version;
  uint16_t my_port;
  uint16_t remote_port;

  union
  {
    struct
    {
      char ip4address[4];
    } v4;
    struct
    {
      char ip6address[16];
    } v6;
  };
};

struct redirect_info
{
    /**
     * The source-address of this connection. When a packet to this address is
     * received, this tunnel is used to forward it.  ipv4-addresses will be put
     * here left-aligned */
  char addr[16];
    /**
     * The source-port of this connection
     */
  uint16_t pt;
};

/**
 * This struct is saved into {tcp,udp}_connections;
 */
struct redirect_state
{
  struct GNUNET_MESH_Tunnel *tunnel;
  GNUNET_HashCode desc;
  struct redirect_service *serv;
  struct remote_addr remote;

  struct GNUNET_CONTAINER_HeapNode *heap_node;
  struct GNUNET_CONTAINER_MultiHashMap *hashmap;
  GNUNET_HashCode hash;

  enum
  { SERVICE, REMOTE } type;

  /**
   * The source-address and -port of this connection
   */
  struct redirect_info redirect_info;
};

/**
 * This hashmaps saves interesting things about the configured services
 */
static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;

struct tunnel_notify_queue
{
  struct tunnel_notify_queue *next;
  struct tunnel_notify_queue *prev;
  void *cls;
  size_t len;
};

struct tunnel_state
{
  struct tunnel_notify_queue *head;
  struct tunnel_notify_queue *tail;
  struct GNUNET_MESH_TransmitHandle *th;
};

/**
 * Function that frees everything from a hashmap
 */
static int
free_iterate (void *cls GNUNET_UNUSED, const GNUNET_HashCode * hash
              GNUNET_UNUSED, void *value)
{
  GNUNET_free (value);
  return GNUNET_YES;
}

/**
 * Function scheduled as very last function, cleans up after us
 */
static void
cleanup (void *cls
         GNUNET_UNUSED,
         const struct GNUNET_SCHEDULER_TaskContext *tskctx)
{
  GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));

  GNUNET_CONTAINER_multihashmap_iterate (udp_connections, free_iterate, NULL);

  GNUNET_CONTAINER_multihashmap_iterate (tcp_connections, free_iterate, NULL);

  if (mesh_handle != NULL)
  {
    GNUNET_MESH_disconnect (mesh_handle);
    mesh_handle = NULL;
  }
}

static void *
new_tunnel (void *cls
            GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
            const struct GNUNET_PeerIdentity *initiator
            GNUNET_UNUSED, const struct GNUNET_ATS_Information *ats
            GNUNET_UNUSED)
{
  struct tunnel_state *s = GNUNET_malloc (sizeof *s);

  s->head = NULL;
  s->tail = NULL;
  s->th = NULL;
  return s;
}

static void
clean_tunnel (void *cls
              GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
              void *tunnel_ctx)
{
  GNUNET_free (tunnel_ctx);
}

static void
collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
    return;


  struct GNUNET_CONTAINER_Heap *heap = cls;

  struct redirect_state *state = GNUNET_CONTAINER_heap_remove_root (heap);

  /* This is free()ed memory! */
  state->heap_node = NULL;

  /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multihashmap_remove (state->hashmap,
                                                       &state->hash, state));

  GNUNET_free (state);
}

static void
hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i,
                    size_t addrlen)
{

  /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
   *
   * build the hash out of the last bytes of the address and the 2 bytes of
   * the port
   */
  memcpy (hash, &u_i->pt, sizeof (u_i->pt));
  memcpy (((unsigned char *) hash) + 2,
          u_i->addr + (addrlen - (sizeof (unsigned int) - 2)),
          (sizeof (unsigned int) - 2));
  memset (((unsigned char *) hash) + sizeof (unsigned int), 0,
          sizeof (GNUNET_HashCode) - sizeof (unsigned int));
}

/**
 * cls is the pointer to a GNUNET_MessageHeader that is
 * followed by the service-descriptor and the udp-packet that should be sent;
 */
static size_t
send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
{
  struct GNUNET_MESH_Tunnel **tunnel = cls;

  GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
  struct GNUNET_MessageHeader *hdr =
      (struct GNUNET_MessageHeader *) (tunnel + 1);
  GNUNET_assert (size >= ntohs (hdr->size));
  memcpy (buf, hdr, ntohs (hdr->size));
  size = ntohs (hdr->size);

  struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);

  if (NULL != s->head)
  {
    struct tunnel_notify_queue *element = s->head;

    GNUNET_CONTAINER_DLL_remove (s->head, s->tail, element);

    s->th =
        GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
                                           GNUNET_TIME_relative_divide
                                           (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                           (const struct GNUNET_PeerIdentity *)
                                           NULL, element->len,
                                           send_udp_to_peer_notify_callback,
                                           element->cls);

    /* save the handle */
    GNUNET_free (element);
  }

  GNUNET_free (cls);

  return size;
}

/**
 * @brief Handles an UDP-Packet received from the helper.
 *
 * @param udp A pointer to the Packet
 * @param dadr The IP-Destination-address
 * @param addrlen The length of the address
 */
static void
udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
{
  struct redirect_info u_i;
  struct GNUNET_MESH_Tunnel *tunnel;
  uint32_t len;
  struct GNUNET_MessageHeader *msg;

  memset (&u_i, 0, sizeof (struct redirect_info));

  memcpy (&u_i.addr, dadr, addrlen);

  u_i.pt = udp->dpt;

  /* get tunnel and service-descriptor from this */
  GNUNET_HashCode hash;

  hash_redirect_info (&hash, &u_i, addrlen);

  struct redirect_state *state =
      GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);

  /* Mark this connection as freshly used */
  GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value);

  tunnel = state->tunnel;

  if (state->type == SERVICE)
  {
    /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
    if (ntohs (udp->spt) == state->serv->remote_port)
    {
      udp->spt = htons (state->serv->my_port);
    }
    else
    {
      /* otherwise the answer came from a different port (tftp does this)
       * add this new port to the list of all services, so that the packets
       * coming back from the client to this new port will be routed correctly
       */
      struct redirect_service *serv =
          GNUNET_malloc (sizeof (struct redirect_service));
      memcpy (serv, state->serv, sizeof (struct redirect_service));
      serv->my_port = ntohs (udp->spt);
      serv->remote_port = ntohs (udp->spt);
      uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);

      memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
              sizeof (GNUNET_HashCode));
      *desc = ntohs (udp->spt);
      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CONTAINER_multihashmap_put (udp_services,
                                                        (GNUNET_HashCode *)
                                                        desc, serv,
                                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));

      state->serv = serv;
    }
  }

  /* send udp-packet back */
  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
      ntohs (udp->len);
  struct GNUNET_MESH_Tunnel **ctunnel =
      GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
  *ctunnel = tunnel;
  msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
  msg->size = htons (len);
  msg->type =
      htons (state->type ==
             SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK :
             GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);

  if (state->type == SERVICE)
    memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
  else
    memcpy (desc, &state->remote, sizeof (struct remote_addr));
  void *_udp = desc + 1;

  memcpy (_udp, udp, ntohs (udp->len));

  struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel);

  if (NULL == s->th)
  {
    /* No notify is pending */
    s->th =
        GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42,
                                           GNUNET_TIME_relative_divide
                                           (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                           (const struct GNUNET_PeerIdentity *)
                                           NULL, len,
                                           send_udp_to_peer_notify_callback,
                                           ctunnel);
  }
  else
  {
    struct tunnel_notify_queue *element =
        GNUNET_malloc (sizeof (struct tunnel_notify_queue));
    element->cls = ctunnel;
    element->len = len;

    GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
  }
}

/**
 * @brief Handles a TCP-Packet received from the helper.
 *
 * @param tcp A pointer to the Packet
 * @param dadr The IP-Destination-address
 * @param addrlen The length of the address
 * @param pktlen the length of the packet, including its header
 */
static void
tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
                 size_t pktlen)
{
  struct redirect_info u_i;
  struct GNUNET_MESH_Tunnel *tunnel;
  uint32_t len;
  struct GNUNET_MessageHeader *msg;

  memset (&u_i, 0, sizeof (struct redirect_info));

  memcpy (&u_i.addr, dadr, addrlen);
  u_i.pt = tcp->dpt;

  /* get tunnel and service-descriptor from this */
  GNUNET_HashCode hash;

  hash_redirect_info (&hash, &u_i, addrlen);

  struct redirect_state *state =
      GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);

  if (state == NULL)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "No mapping for this connection; hash is %x\n",
                *((uint32_t *) & hash));
    return;
  }

  /* Mark this connection as freshly used */
  GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value);

  tunnel = state->tunnel;

  if (state->type == SERVICE)
  {
    /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
    if (ntohs (tcp->spt) == state->serv->remote_port)
    {
      tcp->spt = htons (state->serv->my_port);
    }
    else
    {
      // This is an illegal packet.
      return;
    }
  }

  /* send tcp-packet back */
  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
  struct GNUNET_MESH_Tunnel **ctunnel =
      GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
  *ctunnel = tunnel;
  msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
  msg->size = htons (len);
  msg->type =
      htons (state->type ==
             SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK :
             GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);

  if (state->type == SERVICE)
    memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
  else
    memcpy (desc, &state->remote, sizeof (struct remote_addr));
  void *_tcp = desc + 1;

  memcpy (_tcp, tcp, pktlen);

  struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel);

  if (NULL == s->th)
  {
    /* No notify is pending */
    s->th =
        GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42,
                                           GNUNET_TIME_relative_divide
                                           (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                           (const struct GNUNET_PeerIdentity *)
                                           NULL, len,
                                           send_udp_to_peer_notify_callback,
                                           ctunnel);
  }
  else
  {
    struct tunnel_notify_queue *element =
        GNUNET_malloc (sizeof (struct tunnel_notify_queue));
    element->cls = ctunnel;
    element->len = len;

    GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
  }
}


/**
 * Receive packets from the helper-process
 */
static void
message_token (void *cls GNUNET_UNUSED, void *client
               GNUNET_UNUSED,
               const struct GNUNET_MessageHeader *message)
{
  GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);

  struct tun_pkt *pkt_tun = (struct tun_pkt *) message;

  /* ethertype is ipv6 */
  if (ntohs (pkt_tun->tun.type) == 0x86dd)
  {
    struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;

    if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr)
      udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
                       (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
    else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr)
      tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
                       (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
                       ntohs (pkt6->ip6_hdr.paylgth));
  }
  else if (ntohs (pkt_tun->tun.type) == 0x0800)
  {
    struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
    uint32_t tmp = pkt4->ip_hdr.dadr;

    if (IPPROTO_UDP == pkt4->ip_hdr.proto)
      udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
                       (unsigned char *) &tmp, 4);
    else if (IPPROTO_TCP == pkt4->ip_hdr.proto)
    {
      size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth);

      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
      pktlen -= 4 * pkt4->ip_hdr.hdr_lngth;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
      tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
                       (unsigned char *) &tmp, 4, pktlen);
    }
  }
  else
  {
    return;
  }
}

/**
 * Reads the configuration servicecfg and populates udp_services
 *
 * @param cls unused
 * @param section name of section in config, equal to hostname
 */
static void
read_service_conf (void *cls GNUNET_UNUSED, const char *section)
{
  if ((strlen (section) < 8) ||
      (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
    return;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n",
              strlen (section), section, section + (strlen (section) - 8));

  char *cpy;
  char *redirect;
  char *hostname;
  char *hostport;
  uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);

  GNUNET_CRYPTO_hash (section, strlen (section) + 1,
                      (GNUNET_HashCode *) (desc + 1));

#define TCP 2
#define UDP 1

  int proto = UDP;

  do
  {
    if (proto == UDP &&
        (GNUNET_OK !=
         GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
                                                &cpy)))
      goto next;
    else if (proto == TCP &&
             (GNUNET_OK !=
              GNUNET_CONFIGURATION_get_value_string (cfg, section,
                                                     "TCP_REDIRECTS", &cpy)))
      goto next;

    for (redirect = strtok (cpy, " "); redirect != NULL;
         redirect = strtok (NULL, " "))
    {
      if (NULL == (hostname = strstr (redirect, ":")))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Warning: option %s is not formatted correctly!\n",
                    redirect);
        continue;
      }
      hostname[0] = '\0';
      hostname++;
      if (NULL == (hostport = strstr (hostname, ":")))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Warning: option %s is not formatted correctly!\n",
                    redirect);
        continue;
      }
      hostport[0] = '\0';
      hostport++;

      int local_port = atoi (redirect);

      if (!((local_port > 0) && (local_port < 65536)))
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Warning: %s is not a correct port.", redirect);

      *desc = local_port;

      struct redirect_service *serv =
          GNUNET_malloc (sizeof (struct redirect_service));
      serv->my_port = local_port;

      if (0 == strcmp ("localhost4", hostname))
      {
        serv->version = 4;

        char *ip4addr;

        GNUNET_assert (GNUNET_OK ==
                       GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
                                                              "IPV4ADDR",
                                                              &ip4addr));
        GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address));
        GNUNET_free (ip4addr);
      }
      else if (0 == strcmp ("localhost6", hostname))
      {
        serv->version = 6;

        char *ip6addr;

        GNUNET_assert (GNUNET_OK ==
                       GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
                                                              "IPV6ADDR",
                                                              &ip6addr));
        GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address));
        GNUNET_free (ip6addr);
      }
      else
      {
        struct addrinfo *res;

        int ret = getaddrinfo (hostname, NULL, NULL, &res);

        if (ret != 0)
        {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n",
                      hostname);
          GNUNET_free (serv);
          continue;
        }
        else
        {
          char buf[256];
          struct addrinfo *c = res;

          if (c)
          {
            if (c->ai_family == AF_INET)
            {
              serv->version = 4;
              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                          "Found %s as address for %s\n",
                          inet_ntop (c->ai_family,
                                     &((struct sockaddr_in *) (c->
                                                               ai_addr))->sin_addr,
                                     (char *) &buf, 256), hostname);
              memcpy (serv->v4.ip4address,
                      &((struct sockaddr_in *) (c->ai_addr))->sin_addr, 4);
            }
            else if (c->ai_family == AF_INET6)
            {
              serv->version = 6;
              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                          "Found %s as address for %s\n",
                          inet_ntop (c->ai_family,
                                     &((struct sockaddr_in6 *) (c->
                                                                ai_addr))->sin6_addr,
                                     (char *) &buf, 256), hostname);
              memcpy (serv->v6.ip6address,
                      &((struct sockaddr_in6 *) (c->ai_addr))->sin6_addr, 16);
            }
          }
          else
          {
            freeaddrinfo (res);
            GNUNET_free (serv);
            continue;
          }
          freeaddrinfo (res);
        }
      }
      serv->remote_port = atoi (hostport);
      if (UDP == proto)
        GNUNET_assert (GNUNET_OK ==
                       GNUNET_CONTAINER_multihashmap_put (udp_services,
                                                          (GNUNET_HashCode *)
                                                          desc, serv,
                                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
      else
        GNUNET_assert (GNUNET_OK ==
                       GNUNET_CONTAINER_multihashmap_put (tcp_services,
                                                          (GNUNET_HashCode *)
                                                          desc, serv,
                                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));

    }
    GNUNET_free (cpy);
next:
    proto = (proto == UDP) ? TCP : UDP;
  }
  while (proto != UDP);
}

/**
 * Start the helper-process
 *
 * If cls != NULL it is assumed that this function is called as a result of a dying
 * helper. cls is then taken as handle to the old helper and is cleaned up.
 */
static void
start_helper_and_schedule (void *cls,
                           const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    return;

  if (cls != NULL)
    cleanup_helper (cls);
  cls = NULL;

  char *ifname;
  char *ipv6addr;
  char *ipv6prefix;
  char *ipv4addr;
  char *ipv4mask;

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "No entry 'IFNAME' in configuration!\n");
    exit (1);
  }

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
                                             &ipv6addr))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "No entry 'IPV6ADDR' in configuration!\n");
    exit (1);
  }

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
                                             &ipv6prefix))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "No entry 'IPV6PREFIX' in configuration!\n");
    exit (1);
  }

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
                                             &ipv4addr))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "No entry 'IPV4ADDR' in configuration!\n");
    exit (1);
  }

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
                                             &ipv4mask))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "No entry 'IPV4MASK' in configuration!\n");
    exit (1);
  }

  /* Start the helper
   * Messages get passed to the function message_token
   * When the helper dies, this function will be called again with the
   * helper_handle as cls.
   */
  helper_handle =
      start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
                    "exit-gnunet", start_helper_and_schedule, message_token,
                    NULL);

  GNUNET_free (ipv6addr);
  GNUNET_free (ipv6prefix);
  GNUNET_free (ipv4addr);
  GNUNET_free (ipv4mask);
  GNUNET_free (ifname);
}

static void
prepare_ipv4_packet (size_t len, uint16_t pktlen, void *payload,
                     uint16_t protocol, void *ipaddress, void *tunnel,
                     struct redirect_state *state, struct ip_pkt *pkt4)
{
  uint32_t tmp, tmp2;

  pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  pkt4->shdr.size = htons (len);
  pkt4->tun.flags = 0;
  pkt4->tun.type = htons (0x0800);

  memcpy (&pkt4->data, payload, pktlen);

  pkt4->ip_hdr.version = 4;
  pkt4->ip_hdr.hdr_lngth = 5;
  pkt4->ip_hdr.diff_serv = 0;
  pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
  pkt4->ip_hdr.ident = 0;
  pkt4->ip_hdr.flags = 0;
  pkt4->ip_hdr.frag_off = 0;
  pkt4->ip_hdr.ttl = 255;
  pkt4->ip_hdr.proto = protocol;
  pkt4->ip_hdr.chks = 0;        /* Will be calculated later */

  memcpy (&tmp, ipaddress, 4);
  pkt4->ip_hdr.dadr = tmp;

  /* Generate a new src-address */
  char *ipv4addr;
  char *ipv4mask;

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
                                                        &ipv4addr));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
                                                        &ipv4mask));
  inet_pton (AF_INET, ipv4addr, &tmp);
  inet_pton (AF_INET, ipv4mask, &tmp2);
  GNUNET_free (ipv4addr);
  GNUNET_free (ipv4mask);

  /* This should be a noop */
  tmp = tmp & tmp2;

  tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);

  pkt4->ip_hdr.sadr = tmp;

  memcpy (&state->redirect_info.addr, &tmp, 4);
  if (IPPROTO_UDP == protocol)
  {
    struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;

    state->redirect_info.pt = pkt4_udp->udp_hdr.spt;

    pkt4_udp->udp_hdr.crc = 0;  /* Optional for IPv4 */
  }
  else if (IPPROTO_TCP == protocol)
  {
    struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;

    state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;

    pkt4_tcp->tcp_hdr.crc = 0;
    uint32_t sum = 0;

    tmp = pkt4->ip_hdr.sadr;
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
    tmp = pkt4->ip_hdr.dadr;
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);

    tmp = (protocol << 16) | (0xffff & pktlen);

    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
                (0xffff & pktlen));

    tmp = htonl (tmp);

    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);

    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
                                   pktlen);
    pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
  }

  pkt4->ip_hdr.chks =
      calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
}

static void
prepare_ipv6_packet (size_t len, uint16_t pktlen, void *payload,
                     uint16_t protocol, void *ipaddress, void *tunnel,
                     struct redirect_state *state, struct ip6_pkt *pkt6)
{
  uint32_t tmp;

  pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  pkt6->shdr.size = htons (len);
  pkt6->tun.flags = 0;

  pkt6->tun.type = htons (0x86dd);

  memcpy (&pkt6->data, payload, pktlen);

  pkt6->ip6_hdr.version = 6;
  pkt6->ip6_hdr.nxthdr = protocol;
  pkt6->ip6_hdr.paylgth = htons (pktlen);
  pkt6->ip6_hdr.hoplmt = 64;

  memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);

  /* Generate a new src-address
   * This takes as much from the address of the tunnel as fits into
   * the host-mask*/
  char *ipv6addr;
  unsigned long long ipv6prefix;

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
                                                        &ipv6addr));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
                                                        "IPV6PREFIX",
                                                        &ipv6prefix));
  GNUNET_assert (ipv6prefix < 127);
  ipv6prefix = (ipv6prefix + 7) / 8;

  inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
  GNUNET_free (ipv6addr);

  if (ipv6prefix < (16 - sizeof (void *)))
    ipv6prefix = 16 - sizeof (void *);

  unsigned int offset = ipv6prefix - (16 - sizeof (void *));

  memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
          ((char *) &tunnel) + offset, 16 - ipv6prefix);

  /* copy the needed information into the state */
  memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);

  if (IPPROTO_UDP == protocol)
  {
    struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;

    state->redirect_info.pt = pkt6_udp->udp_hdr.spt;

    pkt6_udp->udp_hdr.crc = 0;
    uint32_t sum = 0;

    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
                                   16);
    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
                                   16);
    tmp = (htons (pktlen) & 0xffff);
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
    tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);

    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
                                   ntohs (pkt6_udp->udp_hdr.len));
    pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
  }
  else if (IPPROTO_TCP == protocol)
  {
    struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;

    state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;

    pkt6_tcp->tcp_hdr.crc = 0;
    uint32_t sum = 0;

    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
    tmp = htonl (pktlen);
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
    tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);

    sum =
        calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
                                   ntohs (pkt6->ip6_hdr.paylgth));
    pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
  }
}

/**
 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
 */
static int
receive_tcp_service (void *cls
                     GNUNET_UNUSED,
                     struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx
                     GNUNET_UNUSED,
                     const struct GNUNET_PeerIdentity *sender
                     GNUNET_UNUSED,
                     const struct GNUNET_MessageHeader *message,
                     const struct GNUNET_ATS_Information *atsi
                     GNUNET_UNUSED)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
  uint16_t pkt_len =
      ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
      sizeof (GNUNET_HashCode);

  /** Get the configuration from the services-hashmap.
   *
   * Which service is needed only depends on the service-descriptor and the
   * destination-port
   */
  uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);

  memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
  *tcp_desc = ntohs (pkt->dpt);
  struct redirect_service *serv =
      GNUNET_CONTAINER_multihashmap_get (tcp_services,
                                         (GNUNET_HashCode *) tcp_desc);

  if (NULL == serv)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for TCP dpt %d!\n",
                *tcp_desc);
    return GNUNET_YES;
  }

  pkt->dpt = htons (serv->remote_port);

  /*
   * At this point it would be possible to check against some kind of ACL.
   */

  char *buf;
  size_t len;

  /* Prepare the state.
   * This will be saved in the hashmap, so that the receiving procedure knows
   * through which tunnel this connection has to be routed.
   */
  struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));

  state->tunnel = tunnel;
  state->serv = serv;
  state->type = SERVICE;
  state->hashmap = tcp_connections;
  memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));

  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
      sizeof (struct ip6_hdr) + pkt_len;
  buf = alloca (len);

  memset (buf, 0, len);

  switch (serv->version)
  {
  case 4:
    prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v4.ip4address,
                         tunnel, state, (struct ip_pkt *) buf);
    break;
  case 6:
    prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v6.ip6address,
                         tunnel, state, (struct ip6_pkt *) buf);

    break;
  default:
    GNUNET_assert (0);
    break;
  }

  hash_redirect_info (&state->hash, &state->redirect_info,
                      serv->version == 4 ? 4 : 16);

  if (GNUNET_NO ==
      GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
  {
    GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);

    state->heap_node =
        GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
                                      GNUNET_TIME_absolute_get ().abs_value);

    if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
        max_tcp_connections)
      GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
  }
  else
    GNUNET_free (state);

  (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  return GNUNET_YES;
}

static int
receive_tcp_remote (void *cls
                    GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                    void **tunnel_ctx
                    GNUNET_UNUSED,
                    const struct GNUNET_PeerIdentity *sender
                    GNUNET_UNUSED,
                    const struct GNUNET_MessageHeader *message,
                    const struct GNUNET_ATS_Information *atsi
                    GNUNET_UNUSED)
{
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
  struct remote_addr *s = (struct remote_addr *) desc;
  char *buf;
  size_t len;
  uint16_t pkt_len =
      ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
      sizeof (GNUNET_HashCode);

  struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));

  state->tunnel = tunnel;
  state->type = REMOTE;
  state->hashmap = tcp_connections;
  memcpy (&state->remote, s, sizeof (struct remote_addr));

  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
      sizeof (struct ip6_hdr) + pkt_len;
  buf = alloca (len);

  memset (buf, 0, len);

  switch (s->addrlen)
  {
  case 4:
    prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel,
                         state, (struct ip_pkt *) buf);
    break;
  case 16:
    prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel,
                         state, (struct ip6_pkt *) buf);
    break;
  default:
    GNUNET_free (state);
    return GNUNET_SYSERR;
  }

  hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
              *((uint32_t *) & state->hash));

  if (GNUNET_NO ==
      GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
  {
    GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);

    state->heap_node =
        GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
                                      GNUNET_TIME_absolute_get ().abs_value);

    if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
        max_tcp_connections)
      GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
  }
  else
    GNUNET_free (state);

  (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  return GNUNET_YES;

}

static int
receive_udp_remote (void *cls
                    GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                    void **tunnel_ctx
                    GNUNET_UNUSED,
                    const struct GNUNET_PeerIdentity *sender
                    GNUNET_UNUSED,
                    const struct GNUNET_MessageHeader *message,
                    const struct GNUNET_ATS_Information *atsi
                    GNUNET_UNUSED)
{
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
  struct remote_addr *s = (struct remote_addr *) desc;
  char *buf;
  size_t len;

  GNUNET_assert (ntohs (pkt->len) ==
                 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
                 sizeof (GNUNET_HashCode));

  /* Prepare the state.
   * This will be saved in the hashmap, so that the receiving procedure knows
   * through which tunnel this connection has to be routed.
   */
  struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));

  state->tunnel = tunnel;
  state->hashmap = udp_connections;
  state->type = REMOTE;
  memcpy (&state->remote, s, sizeof (struct remote_addr));

  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
      sizeof (struct ip6_hdr) + ntohs (pkt->len);
  buf = alloca (len);

  memset (buf, 0, len);

  switch (s->addrlen)
  {
  case 4:
    prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr,
                         tunnel, state, (struct ip_pkt *) buf);
    break;
  case 16:
    prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr,
                         tunnel, state, (struct ip6_pkt *) buf);
    break;
  default:
    GNUNET_assert (0);
    break;
  }

  hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);

  if (GNUNET_NO ==
      GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
  {
    GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);

    state->heap_node =
        GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
                                      GNUNET_TIME_absolute_get ().abs_value);

    if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
        max_udp_connections)
      GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
  }
  else
    GNUNET_free (state);

  (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  return GNUNET_YES;
}

/**
 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
 */
static int
receive_udp_service (void *cls
                     GNUNET_UNUSED,
                     struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
                     const struct GNUNET_PeerIdentity *sender
                     GNUNET_UNUSED,
                     const struct GNUNET_MessageHeader *message,
                     const struct GNUNET_ATS_Information *atsi
                     GNUNET_UNUSED)
{
  GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);

  GNUNET_assert (ntohs (pkt->len) ==
                 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
                 sizeof (GNUNET_HashCode));

  /* Get the configuration from the hashmap */
  uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);

  memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
  *udp_desc = ntohs (pkt->dpt);
  struct redirect_service *serv =
      GNUNET_CONTAINER_multihashmap_get (udp_services,
                                         (GNUNET_HashCode *) udp_desc);

  if (NULL == serv)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "No service found for UDP dpt %d!\n",
                *udp_desc);
    return GNUNET_YES;
  }

  pkt->dpt = htons (serv->remote_port);

  /*
   * At this point it would be possible to check against some kind of ACL.
   */

  char *buf;
  size_t len;

  /* Prepare the state.
   * This will be saved in the hashmap, so that the receiving procedure knows
   * through which tunnel this connection has to be routed.
   */
  struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));

  state->tunnel = tunnel;
  state->serv = serv;
  state->type = SERVICE;
  state->hashmap = udp_connections;
  memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));

  len =
      sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
      sizeof (struct ip6_hdr) + ntohs (pkt->len);
  buf = alloca (len);

  memset (buf, 0, len);

  switch (serv->version)
  {
  case 4:
    prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
                         &serv->v4.ip4address, tunnel, state,
                         (struct ip_pkt *) buf);
    break;
  case 6:
    prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP,
                         &serv->v6.ip6address, tunnel, state,
                         (struct ip6_pkt *) buf);

    break;
  default:
    GNUNET_assert (0);
    break;
  }

  hash_redirect_info (&state->hash, &state->redirect_info,
                      serv->version == 4 ? 4 : 16);

  if (GNUNET_NO ==
      GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
  {
    GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);

    state->heap_node =
        GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
                                      GNUNET_TIME_absolute_get ().abs_value);

    if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
        max_udp_connections)
      GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
  }
  else
    GNUNET_free (state);

  (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  return GNUNET_YES;
}

static void
connect_to_mesh ()
{
  int udp, tcp;
  int handler_idx, app_idx;

  udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
  tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");

  static struct GNUNET_MESH_MessageHandler handlers[] = {
    {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
    {receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP, 0},
    {NULL, 0, 0},
    {NULL, 0, 0},
    {NULL, 0, 0}
  };

  static GNUNET_MESH_ApplicationType apptypes[] = {
    GNUNET_APPLICATION_TYPE_END,
    GNUNET_APPLICATION_TYPE_END,
    GNUNET_APPLICATION_TYPE_END
  };

  app_idx = 0;
  handler_idx = 2;

  if (GNUNET_YES == udp)
  {
    handlers[handler_idx].callback = receive_udp_remote;
    handlers[handler_idx].expected_size = 0;
    handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
    apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
    handler_idx++;
    app_idx++;
  }

  if (GNUNET_YES == tcp)
  {
    handlers[handler_idx].callback = receive_tcp_remote;
    handlers[handler_idx].expected_size = 0;
    handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
    apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
    handler_idx++;
    app_idx++;
  }

  mesh_handle =
      GNUNET_MESH_connect (cfg, 42, NULL, new_tunnel, clean_tunnel, handlers,
                           apptypes);
}


/**
 * @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_)
{
  cfg = cfg_;

  connect_to_mesh ();

  udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
  udp_connections_heap =
      GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
  tcp_connections_heap =
      GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
  tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
                                             &max_udp_connections))
    max_udp_connections = 1024;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
                                             &max_tcp_connections))
    max_tcp_connections = 256;
  GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
  GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
}


/**
 * 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
  };

  return (GNUNET_OK ==
          GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
                              gettext_noop
                              ("Daemon to run to provide an IP exit node for the VPN"),
                              options, &run, NULL)) ? ret : 1;
}


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