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_dns_service.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 GNUNET_DNS_Handle *dns_handle;
50 struct answer_packet_list *answer_proc_head;
52 struct answer_packet_list *answer_proc_tail;
55 struct tunnel_notify_queue
57 struct tunnel_notify_queue *next;
58 struct tunnel_notify_queue *prev;
64 * If there are at least this many address-mappings, old ones will be removed
66 static long long unsigned int max_mappings = 200;
74 * This hashmap contains the mapping from peer, service-descriptor,
75 * source-port and destination-port to a socket
77 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
79 GNUNET_SCHEDULER_TaskIdentifier conn_task;
81 GNUNET_SCHEDULER_TaskIdentifier shs_task;
84 * Function scheduled as very last function, cleans up after us
88 cleanup (void *cls GNUNET_UNUSED,
89 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
91 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
94 cleanup_helper (helper_handle);
96 /* close the connection to the service-dns */
97 GNUNET_DNS_disconnect (dns_handle);
98 if (mesh_handle != NULL)
100 GNUNET_MESH_disconnect (mesh_handle);
103 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
105 GNUNET_SCHEDULER_cancel (shs_task);
106 shs_task = GNUNET_SCHEDULER_NO_TASK;
108 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
110 GNUNET_SCHEDULER_cancel (conn_task);
111 conn_task = GNUNET_SCHEDULER_NO_TASK;
118 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
121 address6_mapping_exists (unsigned char addr[])
123 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
124 unsigned char *k = (unsigned char *) key;
126 memset (key, 0, sizeof (GNUNET_HashCode));
129 for (i = 0; i < 16; i++)
132 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
142 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
145 address4_mapping_exists (uint32_t addr)
147 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
149 memset (key, 0, sizeof (GNUNET_HashCode));
150 unsigned char *c = (unsigned char *) &addr;
151 unsigned char *k = (unsigned char *) key;
154 for (i = 0; i < 4; i++)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
159 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
161 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
172 collect_mappings (void *cls GNUNET_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);
291 struct GNUNET_MessageHeader *hdr =
292 (struct GNUNET_MessageHeader *) (tunnel + 1);
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
296 GNUNET_assert (size >= ntohs (hdr->size));
297 memcpy (buf, hdr, ntohs (hdr->size));
298 size = ntohs (hdr->size);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
304 if (NULL != ts->head)
306 struct tunnel_notify_queue *element = ts->head;
308 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
311 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
312 GNUNET_TIME_relative_divide
313 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
314 (const struct GNUNET_PeerIdentity *)
316 send_pkt_to_peer_notify_callback,
319 /* save the handle */
320 GNUNET_free (element);
328 port_in_ports (uint64_t ports, uint16_t port)
330 uint16_t *ps = (uint16_t *) & ports;
332 return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port ||
337 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
338 const struct GNUNET_ATS_Information *atsi GNUNET_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);
355 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
356 GNUNET_TIME_relative_divide
357 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
358 (const struct GNUNET_PeerIdentity *)
359 NULL, ntohs (hdr->size),
360 send_pkt_to_peer_notify_callback,
365 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
368 element->len = ntohs (hdr->size);
370 GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element);
375 * Create a new Address from an answer-packet
378 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
379 const GNUNET_HashCode * service_desc)
382 unsigned long long ipv6prefix;
384 GNUNET_assert (GNUNET_OK ==
385 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
387 GNUNET_assert (GNUNET_OK ==
388 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
391 GNUNET_assert (ipv6prefix < 127);
392 ipv6prefix = (ipv6prefix + 7) / 8;
394 inet_pton (AF_INET6, ipv6addr, buf);
395 GNUNET_free (ipv6addr);
397 int peer_length = 16 - ipv6prefix - 6;
399 if (peer_length <= 0)
402 int service_length = 16 - ipv6prefix - peer_length;
404 if (service_length <= 0)
407 memcpy (buf + ipv6prefix, service_desc, service_length);
408 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
415 * Create a new Address from an answer-packet
418 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
421 unsigned long long ipv6prefix;
423 GNUNET_assert (GNUNET_OK ==
424 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
426 GNUNET_assert (GNUNET_OK ==
427 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
430 GNUNET_assert (ipv6prefix < 127);
431 ipv6prefix = (ipv6prefix + 7) / 8;
433 inet_pton (AF_INET6, ipv6addr, buf);
434 GNUNET_free (ipv6addr);
436 int local_length = 16 - ipv6prefix;
438 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
444 * Create a new Address from an answer-packet
447 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
452 GNUNET_assert (GNUNET_OK ==
453 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
455 GNUNET_assert (GNUNET_OK ==
456 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
460 inet_pton (AF_INET, ipv4addr, buf);
461 int r = inet_pton (AF_INET, ipv4mask, &mask);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
467 GNUNET_free (ipv4addr);
473 mask = (mask ^ (mask - 1)) >> 1;
474 for (c = 0; mask; c++)
481 c = CHAR_BIT * sizeof (mask);
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
488 GNUNET_free (ipv4mask);
495 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
501 * This gets scheduled with cls pointing to an answer_packet and does everything
502 * needed in order to send it to the helper.
504 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
505 * doing nothing for "real" services.
508 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
510 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
513 struct answer_packet *pkt = cls;
514 struct answer_packet_list *list;
516 /* This answer is about a .gnunet-service
518 * It contains an almost complete DNS-Response, we have to fill in the ip
519 * at the offset pkt->addroffset
521 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
523 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
527 memset (&key, 0, sizeof (GNUNET_HashCode));
529 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
530 unsigned char *k = (unsigned char *) &key;
532 new_ip6addr (c, &pkt->service_descr.peer,
533 &pkt->service_descr.service_descriptor);
535 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
539 for (i = 0; i < 16; i++)
542 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
544 struct map_entry *value =
545 GNUNET_malloc (sizeof (struct map_entry) + namelen);
546 char *name = (char *) (value + 1);
548 value->namelen = namelen;
549 memcpy (name, pkt->data + 12, namelen);
551 memcpy (&value->desc, &pkt->service_descr,
552 sizeof (struct GNUNET_vpn_service_descriptor));
554 memset (value->additional_ports, 0, 8192);
556 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
558 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
560 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
561 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
564 GNUNET_CONTAINER_heap_insert (heap, value,
565 GNUNET_TIME_absolute_get ().abs_value);
566 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
567 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
574 GNUNET_malloc (htons (pkt->hdr.size) +
575 sizeof (struct answer_packet_list) -
576 sizeof (struct answer_packet));
578 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
581 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
585 memset (&key, 0, sizeof key);
586 unsigned char *k = (unsigned char *) &key;
587 unsigned char *s = pkt->data + 12;
590 /* Whoever designed the reverse IPv6-lookup is batshit insane */
591 for (i = 0; i < 16; i++)
593 unsigned char c1 = s[(4 * i) + 1];
594 unsigned char c2 = s[(4 * i) + 3];
599 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
601 k[i] += 16 * (c2 - '0');
603 k[i] += 16 * (c2 - 87);
606 struct map_entry *map_entry =
607 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
608 uint16_t offset = ntohs (pkt->addroffset);
610 if (map_entry == NULL)
616 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
617 GNUNET_TIME_absolute_get ().abs_value);
620 unsigned short namelen = htons (map_entry->namelen);
621 char *name = (char *) (map_entry + 1);
624 GNUNET_malloc (sizeof (struct answer_packet_list) -
625 sizeof (struct answer_packet) + 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 sizeof (struct answer_packet_list) -
645 sizeof (struct answer_packet));
646 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
648 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
650 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
654 memset (&key, 0, sizeof (GNUNET_HashCode));
656 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
658 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
661 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
662 c[10], c[11], c[12], c[13], c[14], c[15]);
663 unsigned char *k = (unsigned char *) &key;
666 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
670 for (i = 0; i < 16; i++)
673 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
675 struct map_entry *value =
676 GNUNET_malloc (sizeof (struct map_entry) + namelen);
677 char *name = (char *) (value + 1);
679 value->namelen = namelen;
680 memcpy (name, pkt->data + 12, namelen);
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
684 value->addrlen = pkt->addrsize;
685 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
686 memset (value->additional_ports, 0, 8192);
688 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
690 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
692 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
693 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
695 GNUNET_CONTAINER_heap_insert (heap, value,
696 GNUNET_TIME_absolute_get ().abs_value);
697 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
698 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
704 GNUNET_malloc (htons (pkt->hdr.size) +
705 sizeof (struct answer_packet_list) -
706 sizeof (struct answer_packet));
708 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
710 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
712 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
716 memset (&key, 0, sizeof (GNUNET_HashCode));
718 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
720 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
723 unsigned char *k = (unsigned char *) &key;
726 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
730 for (i = 0; i < 4; i++)
733 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
735 struct map_entry *value =
736 GNUNET_malloc (sizeof (struct map_entry) + namelen);
737 char *name = (char *) (value + 1);
739 value->namelen = namelen;
740 memcpy (name, pkt->data + 12, namelen);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
744 value->addrlen = pkt->addrsize;
745 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
746 memset (value->additional_ports, 0, 8192);
748 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
750 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
752 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
753 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
755 GNUNET_CONTAINER_heap_insert (heap, value,
756 GNUNET_TIME_absolute_get ().abs_value);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Mapping is saved in the hashmap with key %08x.\n",
759 *((uint32_t *) (&key)));
760 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
761 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
767 GNUNET_malloc (htons (pkt->hdr.size) +
768 sizeof (struct answer_packet_list) -
769 sizeof (struct answer_packet));
771 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
782 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
783 answer_proc_tail, list);
785 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
791 * Sets a bit active in a bitArray.
793 * @param bitArray memory area to set the bit in
794 * @param bitIdx which bit to set
797 setBit (char *bitArray, unsigned int bitIdx)
800 unsigned int targetBit;
802 arraySlot = bitIdx / 8;
803 targetBit = (1L << (bitIdx % 8));
804 bitArray[arraySlot] |= targetBit;
808 * Clears a bit from bitArray.
810 * @param bitArray memory area to set the bit in
811 * @param bitIdx which bit to unset
814 clearBit (char *bitArray, unsigned int bitIdx)
817 unsigned int targetBit;
820 targetBit = (1L << (bitIdx % 8));
821 bitArray[slot] = bitArray[slot] & (~targetBit);
825 * Checks if a bit is active in the bitArray
827 * @param bitArray memory area to set the bit in
828 * @param bitIdx which bit to test
829 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
832 testBit (char *bitArray, unsigned int bitIdx)
835 unsigned int targetBit;
838 targetBit = (1L << (bitIdx % 8));
839 if (bitArray[slot] & targetBit)
846 * @brief Add the port to the list of additional ports in the map_entry
848 * @param me the map_entry
849 * @param port the port in host-byte-order
852 add_additional_port (struct map_entry *me, uint16_t port)
854 setBit (me->additional_ports, port);
858 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
859 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
860 const struct GNUNET_MessageHeader *message,
861 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
863 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
864 struct remote_addr *s = (struct remote_addr *) desc;
865 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
866 const struct GNUNET_PeerIdentity *other = sender;
867 struct tunnel_state *ts = *tunnel_ctx;
869 if (16 == ts->addrlen)
872 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
873 sizeof (struct udp_pkt);
875 struct ip6_udp *pkt6 = alloca (size);
877 GNUNET_assert (pkt6 != NULL);
879 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
880 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
882 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
886 ntohs (message->size), ntohs (pkt->len));
888 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
889 pkt6->shdr.size = htons (size);
892 pkt6->tun.type = htons (0x86dd);
894 pkt6->ip6_hdr.version = 6;
895 pkt6->ip6_hdr.tclass_h = 0;
896 pkt6->ip6_hdr.tclass_l = 0;
897 pkt6->ip6_hdr.flowlbl = 0;
898 pkt6->ip6_hdr.paylgth = pkt->len;
899 pkt6->ip6_hdr.nxthdr = IPPROTO_UDP;
900 pkt6->ip6_hdr.hoplmt = 0xff;
905 GNUNET_assert (GNUNET_OK ==
906 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
909 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
910 GNUNET_free (ipv6addr);
912 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
914 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
916 GNUNET_assert (key != NULL);
918 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
920 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
921 GNUNET_TIME_absolute_get ().abs_value);
925 GNUNET_assert (me != NULL);
926 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
928 GNUNET_assert (me->desc.
929 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
930 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
931 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
933 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
937 pkt6->udp_hdr.crc = 0;
941 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
943 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
944 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
946 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
947 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
948 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
951 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
953 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
955 write_to_helper (pkt6, size);
960 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
962 struct ip_udp *pkt4 = alloca (size);
964 GNUNET_assert (pkt4 != NULL);
966 GNUNET_assert (ntohs (message->type) ==
967 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
970 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
971 pkt4->ip_hdr.sadr = sadr;
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
975 ntohs (message->size), ntohs (pkt->len));
977 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
978 pkt4->shdr.size = htons (size);
981 pkt4->tun.type = htons (0x0800);
983 pkt4->ip_hdr.version = 4;
984 pkt4->ip_hdr.hdr_lngth = 5;
985 pkt4->ip_hdr.diff_serv = 0;
986 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
987 pkt4->ip_hdr.ident = 0;
988 pkt4->ip_hdr.flags = 0;
989 pkt4->ip_hdr.frag_off = 0;
990 pkt4->ip_hdr.ttl = 255;
991 pkt4->ip_hdr.proto = IPPROTO_UDP;
992 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
998 GNUNET_assert (GNUNET_OK ==
999 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1002 inet_pton (AF_INET, ipv4addr, &dadr);
1003 GNUNET_free (ipv4addr);
1004 pkt4->ip_hdr.dadr = dadr;
1006 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1008 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1010 GNUNET_assert (key != NULL);
1012 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1014 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1015 GNUNET_TIME_absolute_get ().abs_value);
1019 GNUNET_assert (me != NULL);
1021 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1024 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1026 write_to_helper (pkt4, size);
1033 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1035 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1036 const struct GNUNET_MessageHeader *message,
1037 const struct GNUNET_ATS_Information *atsi GNUNET_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,
1050 "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1052 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1055 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1057 struct ip6_tcp *pkt6 = alloca (size);
1059 memset (pkt6, 0, size);
1061 GNUNET_assert (pkt6 != NULL);
1063 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1064 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1066 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1068 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1069 pkt6->shdr.size = htons (size);
1071 pkt6->tun.flags = 0;
1072 pkt6->tun.type = htons (0x86dd);
1074 pkt6->ip6_hdr.version = 6;
1075 pkt6->ip6_hdr.tclass_h = 0;
1076 pkt6->ip6_hdr.tclass_l = 0;
1077 pkt6->ip6_hdr.flowlbl = 0;
1078 pkt6->ip6_hdr.paylgth = htons (pktlen);
1079 pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
1080 pkt6->ip6_hdr.hoplmt = 0xff;
1085 GNUNET_assert (GNUNET_OK ==
1086 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1089 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1090 GNUNET_free (ipv6addr);
1092 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1094 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1096 GNUNET_assert (key != NULL);
1098 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1100 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1101 GNUNET_TIME_absolute_get ().abs_value);
1105 GNUNET_assert (me != NULL);
1106 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1107 GNUNET_assert (me->desc.
1108 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1110 pkt6->tcp_hdr.crc = 0;
1115 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1117 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1118 tmp = htonl (pktlen);
1119 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1120 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1121 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1124 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1125 ntohs (pkt6->ip6_hdr.paylgth));
1126 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1128 write_to_helper (pkt6, size);
1132 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1134 struct ip_tcp *pkt4 = alloca (size);
1136 GNUNET_assert (pkt4 != NULL);
1137 memset (pkt4, 0, size);
1139 GNUNET_assert (ntohs (message->type) ==
1140 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1143 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1144 pkt4->ip_hdr.sadr = sadr;
1146 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1147 pkt4->shdr.size = htons (size);
1149 pkt4->tun.flags = 0;
1150 pkt4->tun.type = htons (0x0800);
1152 pkt4->ip_hdr.version = 4;
1153 pkt4->ip_hdr.hdr_lngth = 5;
1154 pkt4->ip_hdr.diff_serv = 0;
1155 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1156 pkt4->ip_hdr.ident = 0;
1157 pkt4->ip_hdr.flags = 0;
1158 pkt4->ip_hdr.frag_off = 0;
1159 pkt4->ip_hdr.ttl = 255;
1160 pkt4->ip_hdr.proto = IPPROTO_TCP;
1161 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1167 GNUNET_assert (GNUNET_OK ==
1168 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1171 inet_pton (AF_INET, ipv4addr, &dadr);
1172 GNUNET_free (ipv4addr);
1173 pkt4->ip_hdr.dadr = dadr;
1176 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1178 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1180 GNUNET_assert (key != NULL);
1182 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1184 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1185 GNUNET_TIME_absolute_get ().abs_value);
1189 GNUNET_assert (me != NULL);
1190 pkt4->tcp_hdr.crc = 0;
1194 tmp = pkt4->ip_hdr.sadr;
1195 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1196 tmp = pkt4->ip_hdr.dadr;
1197 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1199 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1203 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1205 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1206 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1209 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1211 write_to_helper (pkt4, size);
1218 new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1219 const struct GNUNET_PeerIdentity *initiator,
1220 const struct GNUNET_ATS_Information *atsi)
1222 /* Why should anyone open an inbound tunnel to vpn? */
1228 cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
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 GNUNET_UNUSED,
1244 const char *cfgfile GNUNET_UNUSED,
1245 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1247 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1248 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1249 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1250 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1251 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1255 static const GNUNET_MESH_ApplicationType types[] = {
1256 GNUNET_APPLICATION_TYPE_END
1260 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
1263 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1264 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1265 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1267 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1268 dns_handle = GNUNET_DNS_connect (cfg,
1272 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1277 * The main function to obtain template from gnunetd.
1279 * @param argc number of arguments from the command line
1280 * @param argv command line arguments
1281 * @return 0 ok, 1 on error
1284 main (int argc, char *const *argv)
1286 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1287 GNUNET_GETOPT_OPTION_END
1290 return (GNUNET_OK ==
1291 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1292 options, &run, NULL)) ? ret : 1;
1295 /* end of gnunet-daemon-vpn.c */