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
81 cleanup (void *cls GNUNET_UNUSED,
82 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
84 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
87 cleanup_helper (helper_handle);
89 /* close the connection to the service-dns */
90 if (dns_connection != NULL)
92 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
93 dns_connection = NULL;
96 if (mesh_handle != NULL)
98 GNUNET_MESH_disconnect (mesh_handle);
101 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
103 GNUNET_SCHEDULER_cancel (shs_task);
104 shs_task = GNUNET_SCHEDULER_NO_TASK;
106 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
108 GNUNET_SCHEDULER_cancel (conn_task);
109 conn_task = GNUNET_SCHEDULER_NO_TASK;
116 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
119 address6_mapping_exists (unsigned char addr[])
121 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
122 unsigned char *k = (unsigned char *) key;
124 memset (key, 0, sizeof (GNUNET_HashCode));
127 for (i = 0; i < 16; i++)
130 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
140 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
143 address4_mapping_exists (uint32_t addr)
145 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
147 memset (key, 0, sizeof (GNUNET_HashCode));
148 unsigned char *c = (unsigned char *) &addr;
149 unsigned char *k = (unsigned char *) key;
152 for (i = 0; i < 4; i++)
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
157 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
159 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
170 collect_mappings (void *cls GNUNET_UNUSED,
171 const struct GNUNET_SCHEDULER_TaskContext *tc)
173 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
176 struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
178 /* This is free()ed memory! */
179 me->heap_node = NULL;
181 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
183 GNUNET_assert (GNUNET_YES ==
184 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
190 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
195 struct ip_icmp *request = cls;
197 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
199 GNUNET_assert (response != NULL);
200 memset (response, 0, ntohs (request->shdr.size));
202 response->shdr.size = request->shdr.size;
203 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
205 response->tun.flags = 0;
206 response->tun.type = htons (0x0800);
208 response->ip_hdr.hdr_lngth = 5;
209 response->ip_hdr.version = 4;
210 response->ip_hdr.proto = 0x01;
211 response->ip_hdr.dadr = request->ip_hdr.sadr;
212 response->ip_hdr.sadr = request->ip_hdr.dadr;
213 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
215 response->ip_hdr.chks =
216 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
218 response->icmp_hdr.code = 0;
219 response->icmp_hdr.type = 0x0;
221 /* Magic, more Magic! */
222 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
224 /* Copy the rest of the packet */
225 memcpy (response + 1, request + 1,
226 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
228 write_to_helper (response, ntohs (response->shdr.size));
230 GNUNET_free (request);
234 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
239 struct ip6_icmp *request = cls;
241 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
243 GNUNET_assert (response != NULL);
244 memset (response, 0, ntohs (request->shdr.size));
246 response->shdr.size = request->shdr.size;
247 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
249 response->tun.flags = 0;
250 response->tun.type = htons (0x86dd);
252 response->ip6_hdr.hoplmt = 255;
253 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
254 response->ip6_hdr.nxthdr = 0x3a;
255 response->ip6_hdr.version = 6;
256 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
257 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
259 response->icmp_hdr.code = 0;
260 response->icmp_hdr.type = 0x81;
262 /* Magic, more Magic! */
263 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
265 /* Copy the rest of the packet */
266 memcpy (response + 1, request + 1,
267 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
269 write_to_helper (response, ntohs (response->shdr.size));
271 GNUNET_free (request);
275 * cls is the pointer to a GNUNET_MessageHeader that is
276 * followed by the service-descriptor and the packet that should be sent;
279 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
281 struct GNUNET_MESH_Tunnel **tunnel = cls;
283 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
289 struct GNUNET_MessageHeader *hdr =
290 (struct GNUNET_MessageHeader *) (tunnel + 1);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
294 GNUNET_assert (size >= ntohs (hdr->size));
295 memcpy (buf, hdr, ntohs (hdr->size));
296 size = ntohs (hdr->size);
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
302 if (NULL != ts->head)
304 struct tunnel_notify_queue *element = ts->head;
306 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
309 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
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_free (element);
326 port_in_ports (uint64_t ports, uint16_t port)
328 uint16_t *ps = (uint16_t *) & ports;
330 return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port ||
335 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
336 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
338 /* peer == NULL means that all peers in this request are connected */
341 struct GNUNET_MESH_Tunnel **tunnel = cls;
342 struct GNUNET_MessageHeader *hdr =
343 (struct GNUNET_MessageHeader *) (tunnel + 1);
345 GNUNET_assert (NULL != tunnel);
346 GNUNET_assert (NULL != *tunnel);
348 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
353 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
354 GNUNET_TIME_relative_divide
355 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
356 (const struct GNUNET_PeerIdentity *)
357 NULL, ntohs (hdr->size),
358 send_pkt_to_peer_notify_callback,
363 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
366 element->len = ntohs (hdr->size);
368 GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element);
373 * Create a new Address from an answer-packet
376 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
377 const GNUNET_HashCode * service_desc)
380 unsigned long long ipv6prefix;
382 GNUNET_assert (GNUNET_OK ==
383 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
385 GNUNET_assert (GNUNET_OK ==
386 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
389 GNUNET_assert (ipv6prefix < 127);
390 ipv6prefix = (ipv6prefix + 7) / 8;
392 inet_pton (AF_INET6, ipv6addr, buf);
393 GNUNET_free (ipv6addr);
395 int peer_length = 16 - ipv6prefix - 6;
397 if (peer_length <= 0)
400 int service_length = 16 - ipv6prefix - peer_length;
402 if (service_length <= 0)
405 memcpy (buf + ipv6prefix, service_desc, service_length);
406 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
413 * Create a new Address from an answer-packet
416 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
419 unsigned long long ipv6prefix;
421 GNUNET_assert (GNUNET_OK ==
422 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
424 GNUNET_assert (GNUNET_OK ==
425 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
428 GNUNET_assert (ipv6prefix < 127);
429 ipv6prefix = (ipv6prefix + 7) / 8;
431 inet_pton (AF_INET6, ipv6addr, buf);
432 GNUNET_free (ipv6addr);
434 int local_length = 16 - ipv6prefix;
436 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
442 * Create a new Address from an answer-packet
445 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
450 GNUNET_assert (GNUNET_OK ==
451 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
453 GNUNET_assert (GNUNET_OK ==
454 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
458 inet_pton (AF_INET, ipv4addr, buf);
459 int r = inet_pton (AF_INET, ipv4mask, &mask);
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
465 GNUNET_free (ipv4addr);
471 mask = (mask ^ (mask - 1)) >> 1;
472 for (c = 0; mask; c++)
479 c = CHAR_BIT * sizeof (mask);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
486 GNUNET_free (ipv4mask);
493 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
499 * This gets scheduled with cls pointing to an answer_packet and does everything
500 * needed in order to send it to the helper.
502 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
503 * doing nothing for "real" services.
506 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
508 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
511 struct answer_packet *pkt = cls;
512 struct answer_packet_list *list;
514 /* This answer is about a .gnunet-service
516 * It contains an almost complete DNS-Response, we have to fill in the ip
517 * at the offset pkt->addroffset
519 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
521 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
525 memset (&key, 0, sizeof (GNUNET_HashCode));
527 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
528 unsigned char *k = (unsigned char *) &key;
530 new_ip6addr (c, &pkt->service_descr.peer,
531 &pkt->service_descr.service_descriptor);
533 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
537 for (i = 0; i < 16; i++)
540 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
542 struct map_entry *value =
543 GNUNET_malloc (sizeof (struct map_entry) + namelen);
544 char *name = (char *) (value + 1);
546 value->namelen = namelen;
547 memcpy (name, pkt->data + 12, namelen);
549 memcpy (&value->desc, &pkt->service_descr,
550 sizeof (struct GNUNET_vpn_service_descriptor));
552 memset (value->additional_ports, 0, 8192);
554 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
556 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
558 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
562 GNUNET_CONTAINER_heap_insert (heap, value,
563 GNUNET_TIME_absolute_get ().abs_value);
564 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
565 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
572 GNUNET_malloc (htons (pkt->hdr.size) +
573 sizeof (struct answer_packet_list) -
574 sizeof (struct answer_packet));
576 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
579 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
583 memset (&key, 0, sizeof key);
584 unsigned char *k = (unsigned char *) &key;
585 unsigned char *s = pkt->data + 12;
588 /* Whoever designed the reverse IPv6-lookup is batshit insane */
589 for (i = 0; i < 16; i++)
591 unsigned char c1 = s[(4 * i) + 1];
592 unsigned char c2 = s[(4 * i) + 3];
597 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
599 k[i] += 16 * (c2 - '0');
601 k[i] += 16 * (c2 - 87);
604 struct map_entry *map_entry =
605 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
606 uint16_t offset = ntohs (pkt->addroffset);
608 if (map_entry == NULL)
614 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
615 GNUNET_TIME_absolute_get ().abs_value);
618 unsigned short namelen = htons (map_entry->namelen);
619 char *name = (char *) (map_entry + 1);
622 GNUNET_malloc (sizeof (struct answer_packet_list) -
623 sizeof (struct answer_packet) + offset + 2 +
626 struct answer_packet *rpkt = &list->pkt;
628 /* The offset points to the first byte belonging to the address */
629 memcpy (rpkt, pkt, offset - 1);
631 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
632 rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
634 memcpy (((char *) rpkt) + offset, &namelen, 2);
635 memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
638 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
641 GNUNET_malloc (htons (pkt->hdr.size) +
642 sizeof (struct answer_packet_list) -
643 sizeof (struct answer_packet));
644 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
646 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
648 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
652 memset (&key, 0, sizeof (GNUNET_HashCode));
654 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
656 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
659 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
660 c[10], c[11], c[12], c[13], c[14], c[15]);
661 unsigned char *k = (unsigned char *) &key;
664 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
668 for (i = 0; i < 16; i++)
671 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
673 struct map_entry *value =
674 GNUNET_malloc (sizeof (struct map_entry) + namelen);
675 char *name = (char *) (value + 1);
677 value->namelen = namelen;
678 memcpy (name, pkt->data + 12, namelen);
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
682 value->addrlen = pkt->addrsize;
683 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
684 memset (value->additional_ports, 0, 8192);
686 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
688 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
690 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
693 GNUNET_CONTAINER_heap_insert (heap, value,
694 GNUNET_TIME_absolute_get ().abs_value);
695 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
696 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
702 GNUNET_malloc (htons (pkt->hdr.size) +
703 sizeof (struct answer_packet_list) -
704 sizeof (struct answer_packet));
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 sizeof (struct answer_packet_list) -
767 sizeof (struct answer_packet));
769 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
780 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
781 answer_proc_tail, list);
783 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
789 * Sets a bit active in a bitArray.
791 * @param bitArray memory area to set the bit in
792 * @param bitIdx which bit to set
795 setBit (char *bitArray, unsigned int bitIdx)
798 unsigned int targetBit;
800 arraySlot = bitIdx / 8;
801 targetBit = (1L << (bitIdx % 8));
802 bitArray[arraySlot] |= targetBit;
806 * Clears a bit from bitArray.
808 * @param bitArray memory area to set the bit in
809 * @param bitIdx which bit to unset
812 clearBit (char *bitArray, unsigned int bitIdx)
815 unsigned int targetBit;
818 targetBit = (1L << (bitIdx % 8));
819 bitArray[slot] = bitArray[slot] & (~targetBit);
823 * Checks if a bit is active in the bitArray
825 * @param bitArray memory area to set the bit in
826 * @param bitIdx which bit to test
827 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
830 testBit (char *bitArray, unsigned int bitIdx)
833 unsigned int targetBit;
836 targetBit = (1L << (bitIdx % 8));
837 if (bitArray[slot] & targetBit)
844 * @brief Add the port to the list of additional ports in the map_entry
846 * @param me the map_entry
847 * @param port the port in host-byte-order
850 add_additional_port (struct map_entry *me, uint16_t port)
852 setBit (me->additional_ports, port);
856 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
857 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
858 const struct GNUNET_MessageHeader *message,
859 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
861 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
862 struct remote_addr *s = (struct remote_addr *) desc;
863 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
864 const struct GNUNET_PeerIdentity *other = sender;
865 struct tunnel_state *ts = *tunnel_ctx;
867 if (16 == ts->addrlen)
870 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
871 sizeof (struct udp_pkt);
873 struct ip6_udp *pkt6 = alloca (size);
875 GNUNET_assert (pkt6 != NULL);
877 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
878 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
880 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
884 ntohs (message->size), ntohs (pkt->len));
886 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
887 pkt6->shdr.size = htons (size);
890 pkt6->tun.type = htons (0x86dd);
892 pkt6->ip6_hdr.version = 6;
893 pkt6->ip6_hdr.tclass_h = 0;
894 pkt6->ip6_hdr.tclass_l = 0;
895 pkt6->ip6_hdr.flowlbl = 0;
896 pkt6->ip6_hdr.paylgth = pkt->len;
897 pkt6->ip6_hdr.nxthdr = IPPROTO_UDP;
898 pkt6->ip6_hdr.hoplmt = 0xff;
903 GNUNET_assert (GNUNET_OK ==
904 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
907 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
908 GNUNET_free (ipv6addr);
910 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
912 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
914 GNUNET_assert (key != NULL);
916 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
918 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
919 GNUNET_TIME_absolute_get ().abs_value);
923 GNUNET_assert (me != NULL);
924 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
926 GNUNET_assert (me->desc.
927 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
928 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
929 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
931 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
935 pkt6->udp_hdr.crc = 0;
939 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
941 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
942 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
944 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
945 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
946 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
949 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
951 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
953 write_to_helper (pkt6, size);
958 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
960 struct ip_udp *pkt4 = alloca (size);
962 GNUNET_assert (pkt4 != NULL);
964 GNUNET_assert (ntohs (message->type) ==
965 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
968 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
969 pkt4->ip_hdr.sadr = sadr;
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
973 ntohs (message->size), ntohs (pkt->len));
975 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
976 pkt4->shdr.size = htons (size);
979 pkt4->tun.type = htons (0x0800);
981 pkt4->ip_hdr.version = 4;
982 pkt4->ip_hdr.hdr_lngth = 5;
983 pkt4->ip_hdr.diff_serv = 0;
984 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
985 pkt4->ip_hdr.ident = 0;
986 pkt4->ip_hdr.flags = 0;
987 pkt4->ip_hdr.frag_off = 0;
988 pkt4->ip_hdr.ttl = 255;
989 pkt4->ip_hdr.proto = IPPROTO_UDP;
990 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
996 GNUNET_assert (GNUNET_OK ==
997 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1000 inet_pton (AF_INET, ipv4addr, &dadr);
1001 GNUNET_free (ipv4addr);
1002 pkt4->ip_hdr.dadr = dadr;
1004 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1006 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1008 GNUNET_assert (key != NULL);
1010 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1012 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1013 GNUNET_TIME_absolute_get ().abs_value);
1017 GNUNET_assert (me != NULL);
1019 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1022 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1024 write_to_helper (pkt4, size);
1031 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1033 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1034 const struct GNUNET_MessageHeader *message,
1035 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1037 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1038 struct remote_addr *s = (struct remote_addr *) desc;
1039 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1040 const struct GNUNET_PeerIdentity *other = sender;
1041 struct tunnel_state *ts = *tunnel_ctx;
1044 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1045 sizeof (GNUNET_HashCode);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1050 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1053 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1055 struct ip6_tcp *pkt6 = alloca (size);
1057 memset (pkt6, 0, size);
1059 GNUNET_assert (pkt6 != NULL);
1061 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1062 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1064 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1066 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1067 pkt6->shdr.size = htons (size);
1069 pkt6->tun.flags = 0;
1070 pkt6->tun.type = htons (0x86dd);
1072 pkt6->ip6_hdr.version = 6;
1073 pkt6->ip6_hdr.tclass_h = 0;
1074 pkt6->ip6_hdr.tclass_l = 0;
1075 pkt6->ip6_hdr.flowlbl = 0;
1076 pkt6->ip6_hdr.paylgth = htons (pktlen);
1077 pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
1078 pkt6->ip6_hdr.hoplmt = 0xff;
1083 GNUNET_assert (GNUNET_OK ==
1084 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1087 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1088 GNUNET_free (ipv6addr);
1090 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1092 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1094 GNUNET_assert (key != NULL);
1096 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1098 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1099 GNUNET_TIME_absolute_get ().abs_value);
1103 GNUNET_assert (me != NULL);
1104 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1105 GNUNET_assert (me->desc.
1106 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1108 pkt6->tcp_hdr.crc = 0;
1113 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1115 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1116 tmp = htonl (pktlen);
1117 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1118 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1119 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1122 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1123 ntohs (pkt6->ip6_hdr.paylgth));
1124 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1126 write_to_helper (pkt6, size);
1130 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1132 struct ip_tcp *pkt4 = alloca (size);
1134 GNUNET_assert (pkt4 != NULL);
1135 memset (pkt4, 0, size);
1137 GNUNET_assert (ntohs (message->type) ==
1138 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1141 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1142 pkt4->ip_hdr.sadr = sadr;
1144 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1145 pkt4->shdr.size = htons (size);
1147 pkt4->tun.flags = 0;
1148 pkt4->tun.type = htons (0x0800);
1150 pkt4->ip_hdr.version = 4;
1151 pkt4->ip_hdr.hdr_lngth = 5;
1152 pkt4->ip_hdr.diff_serv = 0;
1153 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1154 pkt4->ip_hdr.ident = 0;
1155 pkt4->ip_hdr.flags = 0;
1156 pkt4->ip_hdr.frag_off = 0;
1157 pkt4->ip_hdr.ttl = 255;
1158 pkt4->ip_hdr.proto = IPPROTO_TCP;
1159 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1165 GNUNET_assert (GNUNET_OK ==
1166 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1169 inet_pton (AF_INET, ipv4addr, &dadr);
1170 GNUNET_free (ipv4addr);
1171 pkt4->ip_hdr.dadr = dadr;
1174 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1176 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1178 GNUNET_assert (key != NULL);
1180 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1182 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1183 GNUNET_TIME_absolute_get ().abs_value);
1187 GNUNET_assert (me != NULL);
1188 pkt4->tcp_hdr.crc = 0;
1192 tmp = pkt4->ip_hdr.sadr;
1193 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1194 tmp = pkt4->ip_hdr.dadr;
1195 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1197 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1201 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1203 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1204 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1207 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1209 write_to_helper (pkt4, size);
1216 new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1217 const struct GNUNET_PeerIdentity *initiator,
1218 const struct GNUNET_ATS_Information *atsi)
1220 /* Why should anyone open an inbound tunnel to vpn? */
1226 cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
1228 /* Why should anyone open an inbound tunnel to vpn? */
1233 * Main function that will be run by the scheduler.
1235 * @param cls closure
1236 * @param args remaining command-line arguments
1237 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1238 * @param cfg_ configuration
1241 run (void *cls, char *const *args GNUNET_UNUSED,
1242 const char *cfgfile GNUNET_UNUSED,
1243 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1245 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1246 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1247 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1248 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1249 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1253 static const GNUNET_MESH_ApplicationType types[] = {
1254 GNUNET_APPLICATION_TYPE_END
1258 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
1262 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1263 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1264 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1266 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1267 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1269 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1270 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1274 * The main function to obtain template from gnunetd.
1276 * @param argc number of arguments from the command line
1277 * @param argv command line arguments
1278 * @return 0 ok, 1 on error
1281 main (int argc, char *const *argv)
1283 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1284 GNUNET_GETOPT_OPTION_END
1287 return (GNUNET_OK ==
1288 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1289 options, &run, NULL)) ? ret : 1;
1292 /* end of gnunet-daemon-vpn.c */