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_assert (GNUNET_YES ==
186 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
192 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
197 struct ip_icmp *request = cls;
199 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
201 GNUNET_assert (response != NULL);
202 memset (response, 0, ntohs (request->shdr.size));
204 response->shdr.size = request->shdr.size;
205 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
207 response->tun.flags = 0;
208 response->tun.type = htons (0x0800);
210 response->ip_hdr.hdr_lngth = 5;
211 response->ip_hdr.version = 4;
212 response->ip_hdr.proto = 0x01;
213 response->ip_hdr.dadr = request->ip_hdr.sadr;
214 response->ip_hdr.sadr = request->ip_hdr.dadr;
215 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
217 response->ip_hdr.chks =
218 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
220 response->icmp_hdr.code = 0;
221 response->icmp_hdr.type = 0x0;
223 /* Magic, more Magic! */
224 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
226 /* Copy the rest of the packet */
227 memcpy (response + 1, request + 1,
228 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
230 write_to_helper (response, ntohs (response->shdr.size));
232 GNUNET_free (request);
236 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
241 struct ip6_icmp *request = cls;
243 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
245 GNUNET_assert (response != NULL);
246 memset (response, 0, ntohs (request->shdr.size));
248 response->shdr.size = request->shdr.size;
249 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
251 response->tun.flags = 0;
252 response->tun.type = htons (0x86dd);
254 response->ip6_hdr.hoplmt = 255;
255 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
256 response->ip6_hdr.nxthdr = 0x3a;
257 response->ip6_hdr.version = 6;
258 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
259 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
261 response->icmp_hdr.code = 0;
262 response->icmp_hdr.type = 0x81;
264 /* Magic, more Magic! */
265 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
267 /* Copy the rest of the packet */
268 memcpy (response + 1, request + 1,
269 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
271 write_to_helper (response, ntohs (response->shdr.size));
273 GNUNET_free (request);
277 * cls is the pointer to a GNUNET_MessageHeader that is
278 * followed by the service-descriptor and the packet that should be sent;
281 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
283 struct GNUNET_MESH_Tunnel **tunnel = cls;
285 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel);
290 struct GNUNET_MessageHeader *hdr =
291 (struct GNUNET_MessageHeader *) (tunnel + 1);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
295 GNUNET_assert (size >= ntohs (hdr->size));
296 memcpy (buf, hdr, ntohs (hdr->size));
297 size = ntohs (hdr->size);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
303 if (NULL != ts->head)
305 struct tunnel_notify_queue *element = ts->head;
307 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
310 GNUNET_MESH_notify_transmit_ready (*tunnel,
313 GNUNET_TIME_relative_divide
314 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
315 (const struct GNUNET_PeerIdentity *)
317 send_pkt_to_peer_notify_callback,
320 /* save the handle */
321 GNUNET_free (element);
329 port_in_ports (uint64_t ports, uint16_t port)
331 uint16_t *ps = (uint16_t *) & ports;
333 return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
337 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
338 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
340 /* peer == NULL means that all peers in this request are connected */
343 struct GNUNET_MESH_Tunnel **tunnel = cls;
344 struct GNUNET_MessageHeader *hdr =
345 (struct GNUNET_MessageHeader *) (tunnel + 1);
347 GNUNET_assert (NULL != tunnel);
348 GNUNET_assert (NULL != *tunnel);
350 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel);
354 GNUNET_MESH_notify_transmit_ready (*tunnel,
357 GNUNET_TIME_relative_divide
358 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
359 (const struct GNUNET_PeerIdentity *)
362 send_pkt_to_peer_notify_callback,
367 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
370 element->len = ntohs (hdr->size);
372 GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element);
377 * Create a new Address from an answer-packet
380 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
381 const GNUNET_HashCode * service_desc)
384 unsigned long long ipv6prefix;
386 GNUNET_assert (GNUNET_OK ==
387 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
389 GNUNET_assert (GNUNET_OK ==
390 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
393 GNUNET_assert (ipv6prefix < 127);
394 ipv6prefix = (ipv6prefix + 7) / 8;
396 inet_pton (AF_INET6, ipv6addr, buf);
397 GNUNET_free (ipv6addr);
399 int peer_length = 16 - ipv6prefix - 6;
401 if (peer_length <= 0)
404 int service_length = 16 - ipv6prefix - peer_length;
406 if (service_length <= 0)
409 memcpy (buf + ipv6prefix, service_desc, service_length);
410 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
417 * Create a new Address from an answer-packet
420 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
423 unsigned long long ipv6prefix;
425 GNUNET_assert (GNUNET_OK ==
426 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
428 GNUNET_assert (GNUNET_OK ==
429 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
432 GNUNET_assert (ipv6prefix < 127);
433 ipv6prefix = (ipv6prefix + 7) / 8;
435 inet_pton (AF_INET6, ipv6addr, buf);
436 GNUNET_free (ipv6addr);
438 int local_length = 16 - ipv6prefix;
440 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
446 * Create a new Address from an answer-packet
449 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
454 GNUNET_assert (GNUNET_OK ==
455 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
457 GNUNET_assert (GNUNET_OK ==
458 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
462 inet_pton (AF_INET, ipv4addr, buf);
463 int r = inet_pton (AF_INET, ipv4mask, &mask);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
469 GNUNET_free (ipv4addr);
475 mask = (mask ^ (mask - 1)) >> 1;
476 for (c = 0; mask; c++)
483 c = CHAR_BIT * sizeof (mask);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
490 GNUNET_free (ipv4mask);
497 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
503 * This gets scheduled with cls pointing to an answer_packet and does everything
504 * needed in order to send it to the helper.
506 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
507 * doing nothing for "real" services.
510 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
512 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
515 struct answer_packet *pkt = cls;
516 struct answer_packet_list *list;
518 /* This answer is about a .gnunet-service
520 * It contains an almost complete DNS-Response, we have to fill in the ip
521 * at the offset pkt->addroffset
523 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
525 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
529 memset (&key, 0, sizeof (GNUNET_HashCode));
531 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
532 unsigned char *k = (unsigned char *) &key;
534 new_ip6addr (c, &pkt->service_descr.peer,
535 &pkt->service_descr.service_descriptor);
537 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
541 for (i = 0; i < 16; i++)
544 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
546 struct map_entry *value =
547 GNUNET_malloc (sizeof (struct map_entry) + namelen);
548 char *name = (char *) (value + 1);
550 value->namelen = namelen;
551 memcpy (name, pkt->data + 12, namelen);
553 memcpy (&value->desc, &pkt->service_descr,
554 sizeof (struct GNUNET_vpn_service_descriptor));
556 memset (value->additional_ports, 0, 8192);
558 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
560 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
562 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
566 GNUNET_CONTAINER_heap_insert (heap, value,
567 GNUNET_TIME_absolute_get ().abs_value);
568 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
569 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
576 GNUNET_malloc (htons (pkt->hdr.size) +
577 2 * sizeof (struct answer_packet_list *));
579 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
582 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
586 memset (&key, 0, sizeof key);
587 unsigned char *k = (unsigned char *) &key;
588 unsigned char *s = pkt->data + 12;
591 /* Whoever designed the reverse IPv6-lookup is batshit insane */
592 for (i = 0; i < 16; i++)
594 unsigned char c1 = s[(4 * i) + 1];
595 unsigned char c2 = s[(4 * i) + 3];
600 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
602 k[i] += 16 * (c2 - '0');
604 k[i] += 16 * (c2 - 87);
607 struct map_entry *map_entry =
608 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
609 uint16_t offset = ntohs (pkt->addroffset);
611 if (map_entry == NULL)
617 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
618 GNUNET_TIME_absolute_get ().abs_value);
621 unsigned short namelen = htons (map_entry->namelen);
622 char *name = (char *) (map_entry + 1);
625 GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 +
628 struct answer_packet *rpkt = &list->pkt;
630 /* The offset points to the first byte belonging to the address */
631 memcpy (rpkt, pkt, offset - 1);
633 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
634 rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
636 memcpy (((char *) rpkt) + offset, &namelen, 2);
637 memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
640 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
643 GNUNET_malloc (htons (pkt->hdr.size) +
644 2 * sizeof (struct answer_packet_list *));
645 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
647 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
649 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
653 memset (&key, 0, sizeof (GNUNET_HashCode));
655 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
657 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
660 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
661 c[10], c[11], c[12], c[13], c[14], c[15]);
662 unsigned char *k = (unsigned char *) &key;
665 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
669 for (i = 0; i < 16; i++)
672 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
674 struct map_entry *value =
675 GNUNET_malloc (sizeof (struct map_entry) + namelen);
676 char *name = (char *) (value + 1);
678 value->namelen = namelen;
679 memcpy (name, pkt->data + 12, namelen);
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
683 value->addrlen = pkt->addrsize;
684 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
685 memset (value->additional_ports, 0, 8192);
687 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
689 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
691 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
694 GNUNET_CONTAINER_heap_insert (heap, value,
695 GNUNET_TIME_absolute_get ().abs_value);
696 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
697 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
703 GNUNET_malloc (htons (pkt->hdr.size) +
704 2 * sizeof (struct answer_packet_list *));
706 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
708 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
710 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
714 memset (&key, 0, sizeof (GNUNET_HashCode));
716 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
718 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
721 unsigned char *k = (unsigned char *) &key;
724 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
728 for (i = 0; i < 4; i++)
731 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
733 struct map_entry *value =
734 GNUNET_malloc (sizeof (struct map_entry) + namelen);
735 char *name = (char *) (value + 1);
737 value->namelen = namelen;
738 memcpy (name, pkt->data + 12, namelen);
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
742 value->addrlen = pkt->addrsize;
743 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
744 memset (value->additional_ports, 0, 8192);
746 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
748 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
750 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
753 GNUNET_CONTAINER_heap_insert (heap, value,
754 GNUNET_TIME_absolute_get ().abs_value);
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Mapping is saved in the hashmap with key %08x.\n",
757 *((uint32_t *) (&key)));
758 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
759 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
765 GNUNET_malloc (htons (pkt->hdr.size) +
766 2 * sizeof (struct answer_packet_list *));
768 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
779 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
780 answer_proc_tail, list);
782 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
788 * Sets a bit active in a bitArray.
790 * @param bitArray memory area to set the bit in
791 * @param bitIdx which bit to set
794 setBit (char *bitArray, unsigned int bitIdx)
797 unsigned int targetBit;
799 arraySlot = bitIdx / 8;
800 targetBit = (1L << (bitIdx % 8));
801 bitArray[arraySlot] |= targetBit;
805 * Clears a bit from bitArray.
807 * @param bitArray memory area to set the bit in
808 * @param bitIdx which bit to unset
811 clearBit (char *bitArray, unsigned int bitIdx)
814 unsigned int targetBit;
817 targetBit = (1L << (bitIdx % 8));
818 bitArray[slot] = bitArray[slot] & (~targetBit);
822 * Checks if a bit is active in the bitArray
824 * @param bitArray memory area to set the bit in
825 * @param bitIdx which bit to test
826 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
829 testBit (char *bitArray, unsigned int bitIdx)
832 unsigned int targetBit;
835 targetBit = (1L << (bitIdx % 8));
836 if (bitArray[slot] & targetBit)
843 * @brief Add the port to the list of additional ports in the map_entry
845 * @param me the map_entry
846 * @param port the port in host-byte-order
849 add_additional_port (struct map_entry *me, uint16_t port)
851 setBit (me->additional_ports, port);
855 receive_udp_back (void *cls __attribute__ ((unused)),
856 struct GNUNET_MESH_Tunnel *tunnel,
858 const struct GNUNET_PeerIdentity *sender,
859 const struct GNUNET_MessageHeader *message,
860 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
862 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
863 struct remote_addr *s = (struct remote_addr *) desc;
864 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
865 const struct GNUNET_PeerIdentity *other = sender;
866 struct tunnel_state *ts = *tunnel_ctx;
868 if (16 == ts->addrlen)
871 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
872 sizeof (struct udp_pkt);
874 struct ip6_udp *pkt6 = alloca (size);
876 GNUNET_assert (pkt6 != NULL);
878 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
879 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
881 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
885 ntohs (message->size), ntohs (pkt->len));
887 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
888 pkt6->shdr.size = htons (size);
891 pkt6->tun.type = htons (0x86dd);
893 pkt6->ip6_hdr.version = 6;
894 pkt6->ip6_hdr.tclass_h = 0;
895 pkt6->ip6_hdr.tclass_l = 0;
896 pkt6->ip6_hdr.flowlbl = 0;
897 pkt6->ip6_hdr.paylgth = pkt->len;
898 pkt6->ip6_hdr.nxthdr = IPPROTO_UDP;
899 pkt6->ip6_hdr.hoplmt = 0xff;
904 GNUNET_assert (GNUNET_OK ==
905 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
908 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
909 GNUNET_free (ipv6addr);
911 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
913 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
915 GNUNET_assert (key != NULL);
917 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
919 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
920 GNUNET_TIME_absolute_get ().abs_value);
924 GNUNET_assert (me != NULL);
925 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
927 GNUNET_assert (me->desc.
928 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
929 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
930 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
932 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
936 pkt6->udp_hdr.crc = 0;
940 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
942 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
943 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
945 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
946 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
947 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
950 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
952 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
954 write_to_helper (pkt6, size);
959 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
961 struct ip_udp *pkt4 = alloca (size);
963 GNUNET_assert (pkt4 != NULL);
965 GNUNET_assert (ntohs (message->type) ==
966 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
969 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
970 pkt4->ip_hdr.sadr = sadr;
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
974 ntohs (message->size), ntohs (pkt->len));
976 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
977 pkt4->shdr.size = htons (size);
980 pkt4->tun.type = htons (0x0800);
982 pkt4->ip_hdr.version = 4;
983 pkt4->ip_hdr.hdr_lngth = 5;
984 pkt4->ip_hdr.diff_serv = 0;
985 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
986 pkt4->ip_hdr.ident = 0;
987 pkt4->ip_hdr.flags = 0;
988 pkt4->ip_hdr.frag_off = 0;
989 pkt4->ip_hdr.ttl = 255;
990 pkt4->ip_hdr.proto = IPPROTO_UDP;
991 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
997 GNUNET_assert (GNUNET_OK ==
998 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1001 inet_pton (AF_INET, ipv4addr, &dadr);
1002 GNUNET_free (ipv4addr);
1003 pkt4->ip_hdr.dadr = dadr;
1005 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1007 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1009 GNUNET_assert (key != NULL);
1011 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1013 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1014 GNUNET_TIME_absolute_get ().abs_value);
1018 GNUNET_assert (me != NULL);
1020 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1023 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1025 write_to_helper (pkt4, size);
1032 receive_tcp_back (void *cls __attribute__ ((unused)),
1033 struct GNUNET_MESH_Tunnel *tunnel,
1035 const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)),
1036 const struct GNUNET_MessageHeader *message,
1037 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
1039 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1040 struct remote_addr *s = (struct remote_addr *) desc;
1041 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1042 const struct GNUNET_PeerIdentity *other = sender;
1043 struct tunnel_state *ts = *tunnel_ctx;
1046 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1047 sizeof (GNUNET_HashCode);
1049 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1051 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1054 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1056 struct ip6_tcp *pkt6 = alloca (size);
1058 memset (pkt6, 0, size);
1060 GNUNET_assert (pkt6 != NULL);
1062 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1063 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1065 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1067 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1068 pkt6->shdr.size = htons (size);
1070 pkt6->tun.flags = 0;
1071 pkt6->tun.type = htons (0x86dd);
1073 pkt6->ip6_hdr.version = 6;
1074 pkt6->ip6_hdr.tclass_h = 0;
1075 pkt6->ip6_hdr.tclass_l = 0;
1076 pkt6->ip6_hdr.flowlbl = 0;
1077 pkt6->ip6_hdr.paylgth = htons (pktlen);
1078 pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
1079 pkt6->ip6_hdr.hoplmt = 0xff;
1084 GNUNET_assert (GNUNET_OK ==
1085 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1088 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1089 GNUNET_free (ipv6addr);
1091 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1093 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1095 GNUNET_assert (key != NULL);
1097 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1099 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1100 GNUNET_TIME_absolute_get ().abs_value);
1104 GNUNET_assert (me != NULL);
1105 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1106 GNUNET_assert (me->desc.
1107 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1109 pkt6->tcp_hdr.crc = 0;
1114 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1116 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1117 tmp = htonl (pktlen);
1118 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1119 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1120 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1123 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1124 ntohs (pkt6->ip6_hdr.paylgth));
1125 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1127 write_to_helper (pkt6, size);
1131 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1133 struct ip_tcp *pkt4 = alloca (size);
1135 GNUNET_assert (pkt4 != NULL);
1136 memset (pkt4, 0, size);
1138 GNUNET_assert (ntohs (message->type) ==
1139 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1142 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1143 pkt4->ip_hdr.sadr = sadr;
1145 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1146 pkt4->shdr.size = htons (size);
1148 pkt4->tun.flags = 0;
1149 pkt4->tun.type = htons (0x0800);
1151 pkt4->ip_hdr.version = 4;
1152 pkt4->ip_hdr.hdr_lngth = 5;
1153 pkt4->ip_hdr.diff_serv = 0;
1154 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1155 pkt4->ip_hdr.ident = 0;
1156 pkt4->ip_hdr.flags = 0;
1157 pkt4->ip_hdr.frag_off = 0;
1158 pkt4->ip_hdr.ttl = 255;
1159 pkt4->ip_hdr.proto = IPPROTO_TCP;
1160 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1166 GNUNET_assert (GNUNET_OK ==
1167 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1170 inet_pton (AF_INET, ipv4addr, &dadr);
1171 GNUNET_free (ipv4addr);
1172 pkt4->ip_hdr.dadr = dadr;
1175 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1177 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1179 GNUNET_assert (key != NULL);
1181 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1183 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1184 GNUNET_TIME_absolute_get ().abs_value);
1188 GNUNET_assert (me != NULL);
1189 pkt4->tcp_hdr.crc = 0;
1193 tmp = pkt4->ip_hdr.sadr;
1194 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1195 tmp = pkt4->ip_hdr.dadr;
1196 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1198 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1202 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1204 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1205 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1208 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1210 write_to_helper (pkt4, size);
1216 static void* new_tunnel(void* cls,
1217 struct GNUNET_MESH_Tunnel *tunnel,
1218 const struct GNUNET_PeerIdentity *initiator,
1219 const struct GNUNET_ATS_Information *atsi)
1221 /* Why should anyone open an inbound tunnel to vpn? */
1226 static void cleaner(void *cls,
1227 const struct GNUNET_MESH_Tunnel *tunnel,
1230 /* Why should anyone open an inbound tunnel to vpn? */
1235 * Main function that will be run by the scheduler.
1237 * @param cls closure
1238 * @param args remaining command-line arguments
1239 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1240 * @param cfg_ configuration
1243 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
1244 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1246 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1247 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1248 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1249 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1250 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1254 static const GNUNET_MESH_ApplicationType types[] = {
1255 GNUNET_APPLICATION_TYPE_END
1258 mesh_handle = GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers, types);
1261 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1262 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1263 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1265 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1266 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1268 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1269 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1273 * The main function to obtain template from gnunetd.
1275 * @param argc number of arguments from the command line
1276 * @param argv command line arguments
1277 * @return 0 ok, 1 on error
1280 main (int argc, char *const *argv)
1282 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1283 GNUNET_GETOPT_OPTION_END
1286 return (GNUNET_OK ==
1287 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1288 options, &run, NULL)) ? ret : 1;
1291 /* end of gnunet-daemon-vpn.c */