2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Toelke
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet-vpn-packet.h"
30 #include "gnunet_common.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include <gnunet_mesh_service.h>
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include <block_dns.h>
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-daemon-vpn-dns.h"
40 #include "gnunet-daemon-vpn.h"
41 #include "gnunet-vpn-checksum.h"
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_MESH_Handle *mesh_handle;
45 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
46 static struct GNUNET_CONTAINER_Heap *heap;
48 struct tunnel_notify_queue
50 struct tunnel_notify_queue *next;
51 struct tunnel_notify_queue *prev;
57 * If there are at least this many address-mappings, old ones will be removed
59 static long long unsigned int max_mappings = 200;
67 * This hashmap contains the mapping from peer, service-descriptor,
68 * source-port and destination-port to a socket
70 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
72 GNUNET_SCHEDULER_TaskIdentifier conn_task;
74 GNUNET_SCHEDULER_TaskIdentifier shs_task;
77 * Function scheduled as very last function, cleans up after us
82 __attribute__ ((unused)),
83 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
85 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
88 cleanup_helper (helper_handle);
90 /* close the connection to the service-dns */
91 if (dns_connection != NULL)
93 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
94 dns_connection = NULL;
97 if (mesh_handle != NULL)
99 GNUNET_MESH_disconnect (mesh_handle);
102 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
104 GNUNET_SCHEDULER_cancel (shs_task);
105 shs_task = GNUNET_SCHEDULER_NO_TASK;
107 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
109 GNUNET_SCHEDULER_cancel (conn_task);
110 conn_task = GNUNET_SCHEDULER_NO_TASK;
117 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
120 address6_mapping_exists (unsigned char addr[])
122 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
123 unsigned char *k = (unsigned char *) key;
125 memset (key, 0, sizeof (GNUNET_HashCode));
128 for (i = 0; i < 16; i++)
131 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
141 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
144 address4_mapping_exists (uint32_t addr)
146 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
148 memset (key, 0, sizeof (GNUNET_HashCode));
149 unsigned char *c = (unsigned char *) &addr;
150 unsigned char *k = (unsigned char *) key;
153 for (i = 0; i < 4; i++)
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
158 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
160 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
171 collect_mappings (void *cls
172 __attribute__ ((unused)),
173 const struct GNUNET_SCHEDULER_TaskContext *tc)
175 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
178 struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
180 /* This is free()ed memory! */
181 me->heap_node = NULL;
183 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
185 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me);
191 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
196 struct ip_icmp *request = cls;
198 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
200 GNUNET_assert (response != NULL);
201 memset (response, 0, ntohs (request->shdr.size));
203 response->shdr.size = request->shdr.size;
204 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
206 response->tun.flags = 0;
207 response->tun.type = htons (0x0800);
209 response->ip_hdr.hdr_lngth = 5;
210 response->ip_hdr.version = 4;
211 response->ip_hdr.proto = 0x01;
212 response->ip_hdr.dadr = request->ip_hdr.sadr;
213 response->ip_hdr.sadr = request->ip_hdr.dadr;
214 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
216 response->ip_hdr.chks =
217 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
219 response->icmp_hdr.code = 0;
220 response->icmp_hdr.type = 0x0;
222 /* Magic, more Magic! */
223 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
225 /* Copy the rest of the packet */
226 memcpy (response + 1, request + 1,
227 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
229 write_to_helper (response, ntohs (response->shdr.size));
231 GNUNET_free (request);
235 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
240 struct ip6_icmp *request = cls;
242 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
244 GNUNET_assert (response != NULL);
245 memset (response, 0, ntohs (request->shdr.size));
247 response->shdr.size = request->shdr.size;
248 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
250 response->tun.flags = 0;
251 response->tun.type = htons (0x86dd);
253 response->ip6_hdr.hoplmt = 255;
254 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
255 response->ip6_hdr.nxthdr = 0x3a;
256 response->ip6_hdr.version = 6;
257 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
258 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
260 response->icmp_hdr.code = 0;
261 response->icmp_hdr.type = 0x81;
263 /* Magic, more Magic! */
264 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
266 /* Copy the rest of the packet */
267 memcpy (response + 1, request + 1,
268 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
270 write_to_helper (response, ntohs (response->shdr.size));
272 GNUNET_free (request);
276 * cls is the pointer to a GNUNET_MessageHeader that is
277 * followed by the service-descriptor and the packet that should be sent;
280 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
282 struct GNUNET_MESH_Tunnel **tunnel = cls;
284 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
285 struct GNUNET_MessageHeader *hdr =
286 (struct GNUNET_MessageHeader *) (tunnel + 1);
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
290 GNUNET_assert (size >= ntohs (hdr->size));
291 memcpy (buf, hdr, ntohs (hdr->size));
292 size = ntohs (hdr->size);
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
295 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
297 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
298 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
299 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
301 GNUNET_CONTAINER_DLL_remove (head, tail, element);
303 GNUNET_MESH_tunnel_set_head (*tunnel, head);
304 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
306 struct GNUNET_MESH_TransmitHandle *th =
307 GNUNET_MESH_notify_transmit_ready (*tunnel,
310 GNUNET_TIME_relative_divide
311 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
312 (const struct GNUNET_PeerIdentity *)
314 send_pkt_to_peer_notify_callback,
317 /* save the handle */
318 GNUNET_MESH_tunnel_set_data (*tunnel, th);
319 GNUNET_free (element);
327 port_in_ports (uint64_t ports, uint16_t port)
329 uint16_t *ps = (uint16_t *) & ports;
331 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
335 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
336 const struct GNUNET_TRANSPORT_ATS_Information *atsi
337 __attribute__ ((unused)))
339 /* peer == NULL means that all peers in this request are connected */
342 struct GNUNET_MESH_Tunnel **tunnel = cls;
343 struct GNUNET_MessageHeader *hdr =
344 (struct GNUNET_MessageHeader *) (tunnel + 1);
346 GNUNET_assert (NULL != tunnel);
347 GNUNET_assert (NULL != *tunnel);
349 if (NULL == GNUNET_MESH_tunnel_get_data (*tunnel))
351 struct GNUNET_MESH_TransmitHandle *th =
352 GNUNET_MESH_notify_transmit_ready (*tunnel,
355 GNUNET_TIME_relative_divide
356 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
357 (const struct GNUNET_PeerIdentity *)
360 send_pkt_to_peer_notify_callback,
363 GNUNET_MESH_tunnel_set_data (*tunnel, th);
367 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
368 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
369 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
372 element->len = ntohs (hdr->size);
374 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
376 GNUNET_MESH_tunnel_set_head (*tunnel, head);
377 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
382 * Create a new Address from an answer-packet
385 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
386 const GNUNET_HashCode * service_desc)
389 unsigned long long ipv6prefix;
391 GNUNET_assert (GNUNET_OK ==
392 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
394 GNUNET_assert (GNUNET_OK ==
395 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
398 GNUNET_assert (ipv6prefix < 127);
399 ipv6prefix = (ipv6prefix + 7) / 8;
401 inet_pton (AF_INET6, ipv6addr, buf);
402 GNUNET_free (ipv6addr);
404 int peer_length = 16 - ipv6prefix - 6;
406 if (peer_length <= 0)
409 int service_length = 16 - ipv6prefix - peer_length;
411 if (service_length <= 0)
414 memcpy (buf + ipv6prefix, service_desc, service_length);
415 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
422 * Create a new Address from an answer-packet
425 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
428 unsigned long long ipv6prefix;
430 GNUNET_assert (GNUNET_OK ==
431 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
433 GNUNET_assert (GNUNET_OK ==
434 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
437 GNUNET_assert (ipv6prefix < 127);
438 ipv6prefix = (ipv6prefix + 7) / 8;
440 inet_pton (AF_INET6, ipv6addr, buf);
441 GNUNET_free (ipv6addr);
443 int local_length = 16 - ipv6prefix;
445 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
451 * Create a new Address from an answer-packet
454 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
459 GNUNET_assert (GNUNET_OK ==
460 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
462 GNUNET_assert (GNUNET_OK ==
463 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
467 inet_pton (AF_INET, ipv4addr, buf);
468 int r = inet_pton (AF_INET, ipv4mask, &mask);
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
474 GNUNET_free (ipv4addr);
480 mask = (mask ^ (mask - 1)) >> 1;
481 for (c = 0; mask; c++)
488 c = CHAR_BIT * sizeof (mask);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
495 GNUNET_free (ipv4mask);
502 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
508 * This gets scheduled with cls pointing to an answer_packet and does everything
509 * needed in order to send it to the helper.
511 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
512 * doing nothing for "real" services.
515 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
520 struct answer_packet *pkt = cls;
521 struct answer_packet_list *list;
523 /* This answer is about a .gnunet-service
525 * It contains an almost complete DNS-Response, we have to fill in the ip
526 * at the offset pkt->addroffset
528 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
530 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
534 memset (&key, 0, sizeof (GNUNET_HashCode));
536 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
537 unsigned char *k = (unsigned char *) &key;
539 new_ip6addr (c, &pkt->service_descr.peer,
540 &pkt->service_descr.service_descriptor);
542 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
546 for (i = 0; i < 16; i++)
549 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
551 struct map_entry *value =
552 GNUNET_malloc (sizeof (struct map_entry) + namelen);
553 char *name = (char *) (value + 1);
555 value->namelen = namelen;
556 memcpy (name, pkt->data + 12, namelen);
558 memcpy (&value->desc, &pkt->service_descr,
559 sizeof (struct GNUNET_vpn_service_descriptor));
561 memset (value->additional_ports, 0, 8192);
563 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
565 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
567 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
571 GNUNET_CONTAINER_heap_insert (heap, value,
572 GNUNET_TIME_absolute_get ().abs_value);
573 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
574 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
581 GNUNET_malloc (htons (pkt->hdr.size) +
582 2 * sizeof (struct answer_packet_list *));
584 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
587 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
591 memset (&key, 0, sizeof key);
592 unsigned char *k = (unsigned char *) &key;
593 unsigned char *s = pkt->data + 12;
596 /* Whoever designed the reverse IPv6-lookup is batshit insane */
597 for (i = 0; i < 16; i++)
599 unsigned char c1 = s[(4 * i) + 1];
600 unsigned char c2 = s[(4 * i) + 3];
605 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
607 k[i] += 16 * (c2 - '0');
609 k[i] += 16 * (c2 - 87);
612 struct map_entry *map_entry =
613 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
614 uint16_t offset = ntohs (pkt->addroffset);
616 if (map_entry == NULL)
622 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
623 GNUNET_TIME_absolute_get ().abs_value);
626 unsigned short namelen = htons (map_entry->namelen);
627 char *name = (char *) (map_entry + 1);
630 GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 +
633 struct answer_packet *rpkt = &list->pkt;
635 /* The offset points to the first byte belonging to the address */
636 memcpy (rpkt, pkt, offset - 1);
638 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
639 rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
641 memcpy (((char *) rpkt) + offset, &namelen, 2);
642 memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
645 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
648 GNUNET_malloc (htons (pkt->hdr.size) +
649 2 * sizeof (struct answer_packet_list *));
650 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
652 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
654 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
658 memset (&key, 0, sizeof (GNUNET_HashCode));
660 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
662 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
665 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
666 c[10], c[11], c[12], c[13], c[14], c[15]);
667 unsigned char *k = (unsigned char *) &key;
670 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
674 for (i = 0; i < 16; i++)
677 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
679 struct map_entry *value =
680 GNUNET_malloc (sizeof (struct map_entry) + namelen);
681 char *name = (char *) (value + 1);
683 value->namelen = namelen;
684 memcpy (name, pkt->data + 12, namelen);
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
688 value->addrlen = pkt->addrsize;
689 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
690 memset (value->additional_ports, 0, 8192);
692 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
694 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
696 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
697 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
699 GNUNET_CONTAINER_heap_insert (heap, value,
700 GNUNET_TIME_absolute_get ().abs_value);
701 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
702 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
708 GNUNET_malloc (htons (pkt->hdr.size) +
709 2 * sizeof (struct answer_packet_list *));
711 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
713 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
715 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
719 memset (&key, 0, sizeof (GNUNET_HashCode));
721 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
723 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
726 unsigned char *k = (unsigned char *) &key;
729 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
733 for (i = 0; i < 4; i++)
736 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
738 struct map_entry *value =
739 GNUNET_malloc (sizeof (struct map_entry) + namelen);
740 char *name = (char *) (value + 1);
742 value->namelen = namelen;
743 memcpy (name, pkt->data + 12, namelen);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
747 value->addrlen = pkt->addrsize;
748 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
749 memset (value->additional_ports, 0, 8192);
751 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
753 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
755 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
756 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
758 GNUNET_CONTAINER_heap_insert (heap, value,
759 GNUNET_TIME_absolute_get ().abs_value);
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Mapping is saved in the hashmap with key %08x.\n",
762 *((uint32_t *) (&key)));
763 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
764 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
770 GNUNET_malloc (htons (pkt->hdr.size) +
771 2 * sizeof (struct answer_packet_list *));
773 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
784 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
785 answer_proc_tail, list);
787 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
793 * Sets a bit active in a bitArray.
795 * @param bitArray memory area to set the bit in
796 * @param bitIdx which bit to set
799 setBit (char *bitArray, unsigned int bitIdx)
802 unsigned int targetBit;
804 arraySlot = bitIdx / 8;
805 targetBit = (1L << (bitIdx % 8));
806 bitArray[arraySlot] |= targetBit;
810 * Clears a bit from bitArray.
812 * @param bitArray memory area to set the bit in
813 * @param bitIdx which bit to unset
816 clearBit (char *bitArray, unsigned int bitIdx)
819 unsigned int targetBit;
822 targetBit = (1L << (bitIdx % 8));
823 bitArray[slot] = bitArray[slot] & (~targetBit);
827 * Checks if a bit is active in the bitArray
829 * @param bitArray memory area to set the bit in
830 * @param bitIdx which bit to test
831 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
834 testBit (char *bitArray, unsigned int bitIdx)
837 unsigned int targetBit;
840 targetBit = (1L << (bitIdx % 8));
841 if (bitArray[slot] & targetBit)
848 * @brief Add the port to the list of additional ports in the map_entry
850 * @param me the map_entry
851 * @param port the port in host-byte-order
854 add_additional_port (struct map_entry *me, uint16_t port)
856 setBit (me->additional_ports, port);
860 receive_udp_back (void *cls
861 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
863 __attribute__ ((unused)),
864 const struct GNUNET_PeerIdentity *sender
865 __attribute__ ((unused)),
866 const struct GNUNET_MessageHeader *message,
867 const struct GNUNET_TRANSPORT_ATS_Information *atsi
868 __attribute__ ((unused)))
870 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
871 struct remote_addr *s = (struct remote_addr *) desc;
872 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
873 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
875 if (16 == s->addrlen)
878 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
879 sizeof (struct udp_pkt);
881 struct ip6_udp *pkt6 = alloca (size);
883 GNUNET_assert (pkt6 != NULL);
885 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
886 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
888 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
892 ntohs (message->size), ntohs (pkt->len));
894 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
895 pkt6->shdr.size = htons (size);
898 pkt6->tun.type = htons (0x86dd);
900 pkt6->ip6_hdr.version = 6;
901 pkt6->ip6_hdr.tclass_h = 0;
902 pkt6->ip6_hdr.tclass_l = 0;
903 pkt6->ip6_hdr.flowlbl = 0;
904 pkt6->ip6_hdr.paylgth = pkt->len;
905 pkt6->ip6_hdr.nxthdr = 0x11;
906 pkt6->ip6_hdr.hoplmt = 0xff;
911 GNUNET_assert (GNUNET_OK ==
912 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
915 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
916 GNUNET_free (ipv6addr);
918 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
920 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
922 GNUNET_assert (key != NULL);
924 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
926 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
927 GNUNET_TIME_absolute_get ().abs_value);
931 GNUNET_assert (me != NULL);
932 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
934 GNUNET_assert (me->desc.
935 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
936 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
937 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
939 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
943 pkt6->udp_hdr.crc = 0;
947 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
949 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
950 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
952 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
953 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
954 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
957 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
959 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
961 write_to_helper (pkt6, size);
966 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
968 struct ip_udp *pkt4 = alloca (size);
970 GNUNET_assert (pkt4 != NULL);
972 GNUNET_assert (ntohs (message->type) ==
973 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
976 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
977 pkt4->ip_hdr.sadr = sadr;
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
981 ntohs (message->size), ntohs (pkt->len));
983 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
984 pkt4->shdr.size = htons (size);
987 pkt4->tun.type = htons (0x0800);
989 pkt4->ip_hdr.version = 4;
990 pkt4->ip_hdr.hdr_lngth = 5;
991 pkt4->ip_hdr.diff_serv = 0;
992 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
993 pkt4->ip_hdr.ident = 0;
994 pkt4->ip_hdr.flags = 0;
995 pkt4->ip_hdr.frag_off = 0;
996 pkt4->ip_hdr.ttl = 255;
997 pkt4->ip_hdr.proto = 0x11;
998 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1004 GNUNET_assert (GNUNET_OK ==
1005 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1008 inet_pton (AF_INET, ipv4addr, &dadr);
1009 GNUNET_free (ipv4addr);
1010 pkt4->ip_hdr.dadr = dadr;
1012 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1014 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1016 GNUNET_assert (key != NULL);
1018 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1020 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1021 GNUNET_TIME_absolute_get ().abs_value);
1025 GNUNET_assert (me != NULL);
1027 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1030 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1032 write_to_helper (pkt4, size);
1039 receive_tcp_back (void *cls
1040 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1042 __attribute__ ((unused)),
1043 const struct GNUNET_PeerIdentity *sender
1044 __attribute__ ((unused)),
1045 const struct GNUNET_MessageHeader *message,
1046 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1047 __attribute__ ((unused)))
1049 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1050 struct remote_addr *s = (struct remote_addr *) desc;
1051 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1052 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
1055 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1056 sizeof (GNUNET_HashCode);
1058 if (s->addrlen == 16)
1060 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1062 struct ip6_tcp *pkt6 = alloca (size);
1064 memset (pkt6, 0, size);
1066 GNUNET_assert (pkt6 != NULL);
1068 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1069 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1071 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1073 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1074 pkt6->shdr.size = htons (size);
1076 pkt6->tun.flags = 0;
1077 pkt6->tun.type = htons (0x86dd);
1079 pkt6->ip6_hdr.version = 6;
1080 pkt6->ip6_hdr.tclass_h = 0;
1081 pkt6->ip6_hdr.tclass_l = 0;
1082 pkt6->ip6_hdr.flowlbl = 0;
1083 pkt6->ip6_hdr.paylgth = htons (pktlen);
1084 pkt6->ip6_hdr.nxthdr = 0x06;
1085 pkt6->ip6_hdr.hoplmt = 0xff;
1090 GNUNET_assert (GNUNET_OK ==
1091 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1094 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1095 GNUNET_free (ipv6addr);
1097 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1099 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1101 GNUNET_assert (key != NULL);
1103 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1105 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1106 GNUNET_TIME_absolute_get ().abs_value);
1110 GNUNET_assert (me != NULL);
1111 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1112 GNUNET_assert (me->desc.
1113 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1115 pkt6->tcp_hdr.crc = 0;
1120 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1122 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1123 tmp = htonl (pktlen);
1124 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1125 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1126 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1129 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1130 ntohs (pkt6->ip6_hdr.paylgth));
1131 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1133 write_to_helper (pkt6, size);
1137 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1139 struct ip_tcp *pkt4 = alloca (size);
1141 GNUNET_assert (pkt4 != NULL);
1142 memset (pkt4, 0, size);
1144 GNUNET_assert (ntohs (message->type) ==
1145 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1148 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1149 pkt4->ip_hdr.sadr = sadr;
1151 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1152 pkt4->shdr.size = htons (size);
1154 pkt4->tun.flags = 0;
1155 pkt4->tun.type = htons (0x0800);
1157 pkt4->ip_hdr.version = 4;
1158 pkt4->ip_hdr.hdr_lngth = 5;
1159 pkt4->ip_hdr.diff_serv = 0;
1160 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1161 pkt4->ip_hdr.ident = 0;
1162 pkt4->ip_hdr.flags = 0;
1163 pkt4->ip_hdr.frag_off = 0;
1164 pkt4->ip_hdr.ttl = 255;
1165 pkt4->ip_hdr.proto = 0x06;
1166 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1172 GNUNET_assert (GNUNET_OK ==
1173 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1176 inet_pton (AF_INET, ipv4addr, &dadr);
1177 GNUNET_free (ipv4addr);
1178 pkt4->ip_hdr.dadr = dadr;
1181 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1183 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1185 GNUNET_assert (key != NULL);
1187 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1189 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1190 GNUNET_TIME_absolute_get ().abs_value);
1194 GNUNET_assert (me != NULL);
1195 pkt4->tcp_hdr.crc = 0;
1199 tmp = pkt4->ip_hdr.sadr;
1200 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1201 tmp = pkt4->ip_hdr.dadr;
1202 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1204 tmp = (0x06 << 16) | (0xffff & pktlen);
1208 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1210 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1211 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1214 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1216 write_to_helper (pkt4, size);
1223 * Main function that will be run by the scheduler.
1225 * @param cls closure
1226 * @param args remaining command-line arguments
1227 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1228 * @param cfg_ configuration
1231 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
1232 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1234 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1235 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1236 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1237 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1238 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1242 static const GNUNET_MESH_ApplicationType types[] = {
1243 GNUNET_APPLICATION_TYPE_END
1246 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, types);
1249 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1250 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1251 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1253 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1254 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1256 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1257 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1261 * The main function to obtain template from gnunetd.
1263 * @param argc number of arguments from the command line
1264 * @param argv command line arguments
1265 * @return 0 ok, 1 on error
1268 main (int argc, char *const *argv)
1270 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1271 GNUNET_GETOPT_OPTION_END
1274 return (GNUNET_OK ==
1275 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1276 options, &run, NULL)) ? ret : 1;
1279 /* end of gnunet-daemon-vpn.c */