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 (struct in6_addr *v6addr)
123 unsigned char *addr = (unsigned char*) v6addr;
124 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
125 unsigned char *k = (unsigned char *) key;
127 memset (key, 0, sizeof (GNUNET_HashCode));
130 for (i = 0; i < 16; i++)
133 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
143 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
146 address4_mapping_exists (uint32_t addr)
148 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
150 memset (key, 0, sizeof (GNUNET_HashCode));
151 unsigned char *c = (unsigned char *) &addr;
152 unsigned char *k = (unsigned char *) key;
155 for (i = 0; i < 4; i++)
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
160 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
162 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
173 collect_mappings (void *cls GNUNET_UNUSED,
174 const struct GNUNET_SCHEDULER_TaskContext *tc)
176 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
179 struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
181 /* This is free()ed memory! */
182 me->heap_node = NULL;
184 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
186 GNUNET_assert (GNUNET_YES ==
187 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
193 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
198 struct ip_icmp *request = cls;
200 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
202 GNUNET_assert (response != NULL);
203 memset (response, 0, ntohs (request->shdr.size));
205 response->shdr.size = request->shdr.size;
206 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
208 response->tun.flags = 0;
209 response->tun.type = htons (0x0800);
211 response->ip_hdr.hdr_lngth = 5;
212 response->ip_hdr.version = 4;
213 response->ip_hdr.proto = 0x01;
214 response->ip_hdr.dadr = request->ip_hdr.sadr;
215 response->ip_hdr.sadr = request->ip_hdr.dadr;
216 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
218 response->ip_hdr.chks =
219 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
221 response->icmp_hdr.code = 0;
222 response->icmp_hdr.type = 0x0;
224 /* Magic, more Magic! */
225 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
227 /* Copy the rest of the packet */
228 memcpy (response + 1, request + 1,
229 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
231 write_to_helper (response, ntohs (response->shdr.size));
233 GNUNET_free (request);
237 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
242 struct ip6_icmp *request = cls;
244 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
246 GNUNET_assert (response != NULL);
247 memset (response, 0, ntohs (request->shdr.size));
249 response->shdr.size = request->shdr.size;
250 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
252 response->tun.flags = 0;
253 response->tun.type = htons (0x86dd);
255 response->ip6_hdr.hoplmt = 255;
256 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
257 response->ip6_hdr.nxthdr = 0x3a;
258 response->ip6_hdr.version = 6;
259 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
260 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
262 response->icmp_hdr.code = 0;
263 response->icmp_hdr.type = 0x81;
265 /* Magic, more Magic! */
266 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
268 /* Copy the rest of the packet */
269 memcpy (response + 1, request + 1,
270 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
272 write_to_helper (response, ntohs (response->shdr.size));
274 GNUNET_free (request);
278 * cls is the pointer to a GNUNET_MessageHeader that is
279 * followed by the service-descriptor and the packet that should be sent;
282 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
284 struct GNUNET_MESH_Tunnel **tunnel = cls;
286 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
292 struct GNUNET_MessageHeader *hdr =
293 (struct GNUNET_MessageHeader *) (tunnel + 1);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
297 GNUNET_assert (size >= ntohs (hdr->size));
298 memcpy (buf, hdr, ntohs (hdr->size));
299 size = ntohs (hdr->size);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
305 if (NULL != ts->head)
307 struct tunnel_notify_queue *element = ts->head;
309 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
312 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
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 ||
338 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
339 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
341 /* peer == NULL means that all peers in this request are connected */
344 struct GNUNET_MESH_Tunnel **tunnel = cls;
345 struct GNUNET_MessageHeader *hdr =
346 (struct GNUNET_MessageHeader *) (tunnel + 1);
348 GNUNET_assert (NULL != tunnel);
349 GNUNET_assert (NULL != *tunnel);
351 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
356 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
357 GNUNET_TIME_relative_divide
358 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
359 (const struct GNUNET_PeerIdentity *)
360 NULL, ntohs (hdr->size),
361 send_pkt_to_peer_notify_callback,
366 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
369 element->len = ntohs (hdr->size);
371 GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element);
376 * Create a new Address from an answer-packet
379 new_ip6addr (struct in6_addr *v6addr,
380 const GNUNET_HashCode * peer,
381 const GNUNET_HashCode * service_desc)
383 unsigned char *buf = (unsigned char*) v6addr;
385 unsigned long long ipv6prefix;
387 GNUNET_assert (GNUNET_OK ==
388 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
390 GNUNET_assert (GNUNET_OK ==
391 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
394 GNUNET_assert (ipv6prefix < 127);
395 ipv6prefix = (ipv6prefix + 7) / 8;
397 inet_pton (AF_INET6, ipv6addr, buf);
398 GNUNET_free (ipv6addr);
400 int peer_length = 16 - ipv6prefix - 6;
402 if (peer_length <= 0)
405 int service_length = 16 - ipv6prefix - peer_length;
407 if (service_length <= 0)
410 memcpy (buf + ipv6prefix, service_desc, service_length);
411 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
418 * Create a new Address from an answer-packet
421 new_ip6addr_remote (struct in6_addr *v6addr,
422 unsigned char *addr, char addrlen)
424 unsigned char *buf = (unsigned char*) v6addr;
426 unsigned long long ipv6prefix;
428 GNUNET_assert (GNUNET_OK ==
429 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
431 GNUNET_assert (GNUNET_OK ==
432 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
435 GNUNET_assert (ipv6prefix < 127);
436 ipv6prefix = (ipv6prefix + 7) / 8;
438 inet_pton (AF_INET6, ipv6addr, buf);
439 GNUNET_free (ipv6addr);
441 int local_length = 16 - ipv6prefix;
443 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
449 * Create a new Address from an answer-packet
452 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
457 GNUNET_assert (GNUNET_OK ==
458 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
460 GNUNET_assert (GNUNET_OK ==
461 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
465 inet_pton (AF_INET, ipv4addr, buf);
466 int r = inet_pton (AF_INET, ipv4mask, &mask);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
472 GNUNET_free (ipv4addr);
478 mask = (mask ^ (mask - 1)) >> 1;
479 for (c = 0; mask; c++)
486 c = CHAR_BIT * sizeof (mask);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
493 GNUNET_free (ipv4mask);
500 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
506 * This gets scheduled with cls pointing to an answer_packet and does everything
507 * needed in order to send it to the helper.
509 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
510 * doing nothing for "real" services.
513 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
515 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
518 struct answer_packet *pkt = cls;
519 struct answer_packet_list *list;
521 /* This answer is about a .gnunet-service
523 * It contains an almost complete DNS-Response, we have to fill in the ip
524 * at the offset pkt->addroffset
526 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
528 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
532 memset (&key, 0, sizeof (GNUNET_HashCode));
534 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
535 unsigned char *k = (unsigned char *) &key;
537 new_ip6addr ((struct in6_addr*) c,
538 &pkt->service_descr.peer,
539 &pkt->service_descr.service_descriptor);
541 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
545 for (i = 0; i < 16; i++)
548 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
550 struct map_entry *value =
551 GNUNET_malloc (sizeof (struct map_entry) + namelen);
552 char *name = (char *) (value + 1);
554 value->namelen = namelen;
555 memcpy (name, pkt->data + 12, namelen);
557 memcpy (&value->desc, &pkt->service_descr,
558 sizeof (struct GNUNET_vpn_service_descriptor));
560 memset (value->additional_ports, 0, 8192);
562 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
564 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
566 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
567 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
570 GNUNET_CONTAINER_heap_insert (heap, value,
571 GNUNET_TIME_absolute_get ().abs_value);
572 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
573 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
580 GNUNET_malloc (htons (pkt->hdr.size) +
581 sizeof (struct answer_packet_list) -
582 sizeof (struct answer_packet));
584 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
587 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
591 memset (&key, 0, sizeof key);
592 unsigned char *k = (unsigned char *) &key;
593 unsigned char *s = pkt->data + 12;
596 /* Whoever designed the reverse IPv6-lookup is batshit insane */
597 for (i = 0; i < 16; i++)
599 unsigned char c1 = s[(4 * i) + 1];
600 unsigned char c2 = s[(4 * i) + 3];
605 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
607 k[i] += 16 * (c2 - '0');
609 k[i] += 16 * (c2 - 87);
612 struct map_entry *map_entry =
613 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
614 uint16_t offset = ntohs (pkt->addroffset);
616 if (map_entry == NULL)
622 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
623 GNUNET_TIME_absolute_get ().abs_value);
626 unsigned short namelen = htons (map_entry->namelen);
627 char *name = (char *) (map_entry + 1);
630 GNUNET_malloc (sizeof (struct answer_packet_list) -
631 sizeof (struct answer_packet) + offset + 2 +
634 struct answer_packet *rpkt = &list->pkt;
636 /* The offset points to the first byte belonging to the address */
637 memcpy (rpkt, pkt, offset - 1);
639 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
640 rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
642 memcpy (((char *) rpkt) + offset, &namelen, 2);
643 memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
646 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
649 GNUNET_malloc (htons (pkt->hdr.size) +
650 sizeof (struct answer_packet_list) -
651 sizeof (struct answer_packet));
652 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
654 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
656 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
660 memset (&key, 0, sizeof (GNUNET_HashCode));
662 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
664 new_ip6addr_remote ((struct in6_addr*) c,
665 pkt->addr, pkt->addrsize);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
668 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
669 c[10], c[11], c[12], c[13], c[14], c[15]);
670 unsigned char *k = (unsigned char *) &key;
673 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
677 for (i = 0; i < 16; i++)
680 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
682 struct map_entry *value =
683 GNUNET_malloc (sizeof (struct map_entry) + namelen);
684 char *name = (char *) (value + 1);
686 value->namelen = namelen;
687 memcpy (name, pkt->data + 12, namelen);
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
691 value->addrlen = pkt->addrsize;
692 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
693 memset (value->additional_ports, 0, 8192);
695 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
697 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
699 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
702 GNUNET_CONTAINER_heap_insert (heap, value,
703 GNUNET_TIME_absolute_get ().abs_value);
704 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
705 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
711 GNUNET_malloc (htons (pkt->hdr.size) +
712 sizeof (struct answer_packet_list) -
713 sizeof (struct answer_packet));
715 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
717 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
719 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
723 memset (&key, 0, sizeof (GNUNET_HashCode));
725 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
727 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
730 unsigned char *k = (unsigned char *) &key;
733 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
737 for (i = 0; i < 4; i++)
740 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
742 struct map_entry *value =
743 GNUNET_malloc (sizeof (struct map_entry) + namelen);
744 char *name = (char *) (value + 1);
746 value->namelen = namelen;
747 memcpy (name, pkt->data + 12, namelen);
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
751 value->addrlen = pkt->addrsize;
752 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
753 memset (value->additional_ports, 0, 8192);
755 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
757 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
759 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
762 GNUNET_CONTAINER_heap_insert (heap, value,
763 GNUNET_TIME_absolute_get ().abs_value);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Mapping is saved in the hashmap with key %08x.\n",
766 *((uint32_t *) (&key)));
767 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
768 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
774 GNUNET_malloc (htons (pkt->hdr.size) +
775 sizeof (struct answer_packet_list) -
776 sizeof (struct answer_packet));
778 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
789 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
790 answer_proc_tail, list);
792 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
798 * Sets a bit active in a bitArray.
800 * @param bitArray memory area to set the bit in
801 * @param bitIdx which bit to set
804 setBit (char *bitArray, unsigned int bitIdx)
807 unsigned int targetBit;
809 arraySlot = bitIdx / 8;
810 targetBit = (1L << (bitIdx % 8));
811 bitArray[arraySlot] |= targetBit;
815 * Clears a bit from bitArray.
817 * @param bitArray memory area to set the bit in
818 * @param bitIdx which bit to unset
821 clearBit (char *bitArray, unsigned int bitIdx)
824 unsigned int targetBit;
827 targetBit = (1L << (bitIdx % 8));
828 bitArray[slot] = bitArray[slot] & (~targetBit);
832 * Checks if a bit is active in the bitArray
834 * @param bitArray memory area to set the bit in
835 * @param bitIdx which bit to test
836 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
839 testBit (char *bitArray, unsigned int bitIdx)
842 unsigned int targetBit;
845 targetBit = (1L << (bitIdx % 8));
846 if (bitArray[slot] & targetBit)
853 * @brief Add the port to the list of additional ports in the map_entry
855 * @param me the map_entry
856 * @param port the port in host-byte-order
859 add_additional_port (struct map_entry *me, uint16_t port)
861 setBit (me->additional_ports, port);
865 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
866 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
867 const struct GNUNET_MessageHeader *message,
868 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
870 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
871 struct remote_addr *s = (struct remote_addr *) desc;
872 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
873 const struct GNUNET_PeerIdentity *other = sender;
874 struct tunnel_state *ts = *tunnel_ctx;
876 if (16 == ts->addrlen)
879 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
880 sizeof (struct udp_pkt);
882 struct ip6_udp *pkt6 = alloca (size);
884 GNUNET_assert (pkt6 != NULL);
886 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
887 new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
889 new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
893 ntohs (message->size), ntohs (pkt->len));
895 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
896 pkt6->shdr.size = htons (size);
899 pkt6->tun.type = htons (0x86dd);
901 pkt6->ip6_hdr.version = 6;
902 pkt6->ip6_hdr.tclass_h = 0;
903 pkt6->ip6_hdr.tclass_l = 0;
904 pkt6->ip6_hdr.flowlbl = 0;
905 pkt6->ip6_hdr.paylgth = pkt->len;
906 pkt6->ip6_hdr.nxthdr = IPPROTO_UDP;
907 pkt6->ip6_hdr.hoplmt = 0xff;
912 GNUNET_assert (GNUNET_OK ==
913 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
916 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr);
917 GNUNET_free (ipv6addr);
919 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
921 GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr);
923 GNUNET_assert (key != NULL);
925 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
927 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
928 GNUNET_TIME_absolute_get ().abs_value);
932 GNUNET_assert (me != NULL);
933 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
935 GNUNET_assert (me->desc.
936 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
937 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
938 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
940 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
944 pkt6->udp_hdr.crc = 0;
948 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
950 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
951 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
953 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
954 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
955 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
958 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
960 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
962 write_to_helper (pkt6, size);
967 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
969 struct ip_udp *pkt4 = alloca (size);
971 GNUNET_assert (pkt4 != NULL);
973 GNUNET_assert (ntohs (message->type) ==
974 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
977 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
978 pkt4->ip_hdr.sadr.s_addr = sadr;
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
982 ntohs (message->size), ntohs (pkt->len));
984 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
985 pkt4->shdr.size = htons (size);
988 pkt4->tun.type = htons (0x0800);
990 pkt4->ip_hdr.version = 4;
991 pkt4->ip_hdr.hdr_lngth = 5;
992 pkt4->ip_hdr.diff_serv = 0;
993 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
994 pkt4->ip_hdr.ident = 0;
995 pkt4->ip_hdr.flags = 0;
996 pkt4->ip_hdr.frag_off = 0;
997 pkt4->ip_hdr.ttl = 255;
998 pkt4->ip_hdr.proto = IPPROTO_UDP;
999 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1005 GNUNET_assert (GNUNET_OK ==
1006 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1009 inet_pton (AF_INET, ipv4addr, &dadr);
1010 GNUNET_free (ipv4addr);
1011 pkt4->ip_hdr.dadr.s_addr = dadr;
1013 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1015 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr);
1017 GNUNET_assert (key != NULL);
1019 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1021 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1022 GNUNET_TIME_absolute_get ().abs_value);
1026 GNUNET_assert (me != NULL);
1028 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1031 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1033 write_to_helper (pkt4, size);
1040 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1042 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1043 const struct GNUNET_MessageHeader *message,
1044 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1046 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1047 struct remote_addr *s = (struct remote_addr *) desc;
1048 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1049 const struct GNUNET_PeerIdentity *other = sender;
1050 struct tunnel_state *ts = *tunnel_ctx;
1053 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1054 sizeof (GNUNET_HashCode);
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1059 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1062 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1064 struct ip6_tcp *pkt6 = alloca (size);
1066 memset (pkt6, 0, size);
1068 GNUNET_assert (pkt6 != NULL);
1070 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1071 new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1073 new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1075 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1076 pkt6->shdr.size = htons (size);
1078 pkt6->tun.flags = 0;
1079 pkt6->tun.type = htons (0x86dd);
1081 pkt6->ip6_hdr.version = 6;
1082 pkt6->ip6_hdr.tclass_h = 0;
1083 pkt6->ip6_hdr.tclass_l = 0;
1084 pkt6->ip6_hdr.flowlbl = 0;
1085 pkt6->ip6_hdr.paylgth = htons (pktlen);
1086 pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
1087 pkt6->ip6_hdr.hoplmt = 0xff;
1092 GNUNET_assert (GNUNET_OK ==
1093 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1096 inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr);
1097 GNUNET_free (ipv6addr);
1099 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1101 GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr);
1103 GNUNET_assert (key != NULL);
1105 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1107 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1108 GNUNET_TIME_absolute_get ().abs_value);
1112 GNUNET_assert (me != NULL);
1113 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1114 GNUNET_assert (me->desc.
1115 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1117 pkt6->tcp_hdr.crc = 0;
1122 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1124 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1125 tmp = htonl (pktlen);
1126 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1127 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1128 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1131 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1132 ntohs (pkt6->ip6_hdr.paylgth));
1133 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1135 write_to_helper (pkt6, size);
1139 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1141 struct ip_tcp *pkt4 = alloca (size);
1143 GNUNET_assert (pkt4 != NULL);
1144 memset (pkt4, 0, size);
1146 GNUNET_assert (ntohs (message->type) ==
1147 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1150 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1151 pkt4->ip_hdr.sadr.s_addr = sadr;
1153 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1154 pkt4->shdr.size = htons (size);
1156 pkt4->tun.flags = 0;
1157 pkt4->tun.type = htons (0x0800);
1159 pkt4->ip_hdr.version = 4;
1160 pkt4->ip_hdr.hdr_lngth = 5;
1161 pkt4->ip_hdr.diff_serv = 0;
1162 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1163 pkt4->ip_hdr.ident = 0;
1164 pkt4->ip_hdr.flags = 0;
1165 pkt4->ip_hdr.frag_off = 0;
1166 pkt4->ip_hdr.ttl = 255;
1167 pkt4->ip_hdr.proto = IPPROTO_TCP;
1168 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1174 GNUNET_assert (GNUNET_OK ==
1175 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1178 inet_pton (AF_INET, ipv4addr, &dadr);
1179 GNUNET_free (ipv4addr);
1180 pkt4->ip_hdr.dadr.s_addr = dadr;
1183 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1185 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr);
1187 GNUNET_assert (key != NULL);
1189 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1191 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1192 GNUNET_TIME_absolute_get ().abs_value);
1196 GNUNET_assert (me != NULL);
1197 pkt4->tcp_hdr.crc = 0;
1201 sum = calculate_checksum_update (sum, (uint16_t *) &pkt4->ip_hdr.sadr, 4);
1202 sum = calculate_checksum_update (sum, (uint16_t *) &pkt4->ip_hdr.dadr, 4);
1204 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1208 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1210 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1211 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1214 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1216 write_to_helper (pkt4, size);
1223 new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1224 const struct GNUNET_PeerIdentity *initiator,
1225 const struct GNUNET_ATS_Information *atsi)
1227 /* Why should anyone open an inbound tunnel to vpn? */
1233 cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
1235 /* Why should anyone open an inbound tunnel to vpn? */
1240 * Main function that will be run by the scheduler.
1242 * @param cls closure
1243 * @param args remaining command-line arguments
1244 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1245 * @param cfg_ configuration
1248 run (void *cls, char *const *args GNUNET_UNUSED,
1249 const char *cfgfile GNUNET_UNUSED,
1250 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1252 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1253 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1254 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1255 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1256 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1260 static const GNUNET_MESH_ApplicationType types[] = {
1261 GNUNET_APPLICATION_TYPE_END
1265 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
1268 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1269 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1270 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1272 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1273 dns_handle = GNUNET_DNS_connect (cfg,
1277 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1282 * The main function to obtain template from gnunetd.
1284 * @param argc number of arguments from the command line
1285 * @param argv command line arguments
1286 * @return 0 ok, 1 on error
1289 main (int argc, char *const *argv)
1291 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1292 GNUNET_GETOPT_OPTION_END
1295 return (GNUNET_OK ==
1296 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1297 options, &run, NULL)) ? ret : 1;
1300 /* end of gnunet-daemon-vpn.c */