2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Toelke
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet-vpn-packet.h"
30 #include "gnunet_common.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include <gnunet_mesh_service.h>
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include <block_dns.h>
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-daemon-vpn-dns.h"
40 #include "gnunet-daemon-vpn.h"
41 #include "gnunet-vpn-checksum.h"
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_MESH_Handle *mesh_handle;
45 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
46 static struct GNUNET_CONTAINER_Heap *heap;
48 struct tunnel_notify_queue
50 struct tunnel_notify_queue *next;
51 struct tunnel_notify_queue *prev;
57 * If there are at least this many address-mappings, old ones will be removed
59 static long long unsigned int max_mappings = 200;
67 * This hashmap contains the mapping from peer, service-descriptor,
68 * source-port and destination-port to a socket
70 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
72 GNUNET_SCHEDULER_TaskIdentifier conn_task;
74 GNUNET_SCHEDULER_TaskIdentifier shs_task;
77 * Function scheduled as very last function, cleans up after us
82 __attribute__ ((unused)),
83 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
85 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
88 cleanup_helper (helper_handle);
90 /* close the connection to the service-dns */
91 if (dns_connection != NULL)
93 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
94 dns_connection = NULL;
97 if (mesh_handle != NULL)
99 GNUNET_MESH_disconnect (mesh_handle);
102 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
104 GNUNET_SCHEDULER_cancel (shs_task);
105 shs_task = GNUNET_SCHEDULER_NO_TASK;
107 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
109 GNUNET_SCHEDULER_cancel (conn_task);
110 conn_task = GNUNET_SCHEDULER_NO_TASK;
117 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
120 address6_mapping_exists (unsigned char addr[])
122 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
123 unsigned char *k = (unsigned char *) key;
125 memset (key, 0, sizeof (GNUNET_HashCode));
128 for (i = 0; i < 16; i++)
131 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
141 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
144 address4_mapping_exists (uint32_t addr)
146 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
148 memset (key, 0, sizeof (GNUNET_HashCode));
149 unsigned char *c = (unsigned char *) &addr;
150 unsigned char *k = (unsigned char *) key;
153 for (i = 0; i < 4; i++)
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
158 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
160 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
171 collect_mappings (void *cls
172 __attribute__ ((unused)),
173 const struct GNUNET_SCHEDULER_TaskContext *tc)
175 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
178 struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
180 /* This is free()ed memory! */
181 me->heap_node = NULL;
183 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
185 GNUNET_assert (GNUNET_YES ==
186 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
192 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
197 struct ip_icmp *request = cls;
199 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
201 GNUNET_assert (response != NULL);
202 memset (response, 0, ntohs (request->shdr.size));
204 response->shdr.size = request->shdr.size;
205 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
207 response->tun.flags = 0;
208 response->tun.type = htons (0x0800);
210 response->ip_hdr.hdr_lngth = 5;
211 response->ip_hdr.version = 4;
212 response->ip_hdr.proto = 0x01;
213 response->ip_hdr.dadr = request->ip_hdr.sadr;
214 response->ip_hdr.sadr = request->ip_hdr.dadr;
215 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
217 response->ip_hdr.chks =
218 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
220 response->icmp_hdr.code = 0;
221 response->icmp_hdr.type = 0x0;
223 /* Magic, more Magic! */
224 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
226 /* Copy the rest of the packet */
227 memcpy (response + 1, request + 1,
228 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
230 write_to_helper (response, ntohs (response->shdr.size));
232 GNUNET_free (request);
236 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
241 struct ip6_icmp *request = cls;
243 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
245 GNUNET_assert (response != NULL);
246 memset (response, 0, ntohs (request->shdr.size));
248 response->shdr.size = request->shdr.size;
249 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
251 response->tun.flags = 0;
252 response->tun.type = htons (0x86dd);
254 response->ip6_hdr.hoplmt = 255;
255 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
256 response->ip6_hdr.nxthdr = 0x3a;
257 response->ip6_hdr.version = 6;
258 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
259 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
261 response->icmp_hdr.code = 0;
262 response->icmp_hdr.type = 0x81;
264 /* Magic, more Magic! */
265 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
267 /* Copy the rest of the packet */
268 memcpy (response + 1, request + 1,
269 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
271 write_to_helper (response, ntohs (response->shdr.size));
273 GNUNET_free (request);
277 * cls is the pointer to a GNUNET_MessageHeader that is
278 * followed by the service-descriptor and the packet that should be sent;
281 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
283 struct GNUNET_MESH_Tunnel **tunnel = cls;
285 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
286 struct GNUNET_MessageHeader *hdr =
287 (struct GNUNET_MessageHeader *) (tunnel + 1);
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
291 GNUNET_assert (size >= ntohs (hdr->size));
292 memcpy (buf, hdr, ntohs (hdr->size));
293 size = ntohs (hdr->size);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
296 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
298 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
299 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
300 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
302 GNUNET_CONTAINER_DLL_remove (head, tail, element);
304 GNUNET_MESH_tunnel_set_head (*tunnel, head);
305 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
307 struct GNUNET_MESH_TransmitHandle *th =
308 GNUNET_MESH_notify_transmit_ready (*tunnel,
311 GNUNET_TIME_relative_divide
312 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
313 (const struct GNUNET_PeerIdentity *)
315 send_pkt_to_peer_notify_callback,
318 /* save the handle */
319 GNUNET_MESH_tunnel_set_data (*tunnel, th);
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 || ps[3] == port;
336 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
337 const struct GNUNET_TRANSPORT_ATS_Information *atsi
338 __attribute__ ((unused)))
340 /* peer == NULL means that all peers in this request are connected */
343 struct GNUNET_MESH_Tunnel **tunnel = cls;
344 struct GNUNET_MessageHeader *hdr =
345 (struct GNUNET_MessageHeader *) (tunnel + 1);
347 GNUNET_assert (NULL != tunnel);
348 GNUNET_assert (NULL != *tunnel);
350 if (NULL == GNUNET_MESH_tunnel_get_data (*tunnel))
352 struct GNUNET_MESH_TransmitHandle *th =
353 GNUNET_MESH_notify_transmit_ready (*tunnel,
356 GNUNET_TIME_relative_divide
357 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
358 (const struct GNUNET_PeerIdentity *)
361 send_pkt_to_peer_notify_callback,
364 GNUNET_MESH_tunnel_set_data (*tunnel, th);
368 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
369 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
370 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
373 element->len = ntohs (hdr->size);
375 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
377 GNUNET_MESH_tunnel_set_head (*tunnel, head);
378 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
383 * Create a new Address from an answer-packet
386 new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
387 const GNUNET_HashCode * service_desc)
390 unsigned long long ipv6prefix;
392 GNUNET_assert (GNUNET_OK ==
393 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
395 GNUNET_assert (GNUNET_OK ==
396 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
399 GNUNET_assert (ipv6prefix < 127);
400 ipv6prefix = (ipv6prefix + 7) / 8;
402 inet_pton (AF_INET6, ipv6addr, buf);
403 GNUNET_free (ipv6addr);
405 int peer_length = 16 - ipv6prefix - 6;
407 if (peer_length <= 0)
410 int service_length = 16 - ipv6prefix - peer_length;
412 if (service_length <= 0)
415 memcpy (buf + ipv6prefix, service_desc, service_length);
416 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
423 * Create a new Address from an answer-packet
426 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
429 unsigned long long ipv6prefix;
431 GNUNET_assert (GNUNET_OK ==
432 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
434 GNUNET_assert (GNUNET_OK ==
435 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
438 GNUNET_assert (ipv6prefix < 127);
439 ipv6prefix = (ipv6prefix + 7) / 8;
441 inet_pton (AF_INET6, ipv6addr, buf);
442 GNUNET_free (ipv6addr);
444 int local_length = 16 - ipv6prefix;
446 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
452 * Create a new Address from an answer-packet
455 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
460 GNUNET_assert (GNUNET_OK ==
461 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
463 GNUNET_assert (GNUNET_OK ==
464 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
468 inet_pton (AF_INET, ipv4addr, buf);
469 int r = inet_pton (AF_INET, ipv4mask, &mask);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
475 GNUNET_free (ipv4addr);
481 mask = (mask ^ (mask - 1)) >> 1;
482 for (c = 0; mask; c++)
489 c = CHAR_BIT * sizeof (mask);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
496 GNUNET_free (ipv4mask);
503 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
509 * This gets scheduled with cls pointing to an answer_packet and does everything
510 * needed in order to send it to the helper.
512 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
513 * doing nothing for "real" services.
516 process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
521 struct answer_packet *pkt = cls;
522 struct answer_packet_list *list;
524 /* This answer is about a .gnunet-service
526 * It contains an almost complete DNS-Response, we have to fill in the ip
527 * at the offset pkt->addroffset
529 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
531 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
535 memset (&key, 0, sizeof (GNUNET_HashCode));
537 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
538 unsigned char *k = (unsigned char *) &key;
540 new_ip6addr (c, &pkt->service_descr.peer,
541 &pkt->service_descr.service_descriptor);
543 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
547 for (i = 0; i < 16; i++)
550 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
552 struct map_entry *value =
553 GNUNET_malloc (sizeof (struct map_entry) + namelen);
554 char *name = (char *) (value + 1);
556 value->namelen = namelen;
557 memcpy (name, pkt->data + 12, namelen);
559 memcpy (&value->desc, &pkt->service_descr,
560 sizeof (struct GNUNET_vpn_service_descriptor));
562 memset (value->additional_ports, 0, 8192);
564 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
566 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
568 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
572 GNUNET_CONTAINER_heap_insert (heap, value,
573 GNUNET_TIME_absolute_get ().abs_value);
574 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
575 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
582 GNUNET_malloc (htons (pkt->hdr.size) +
583 2 * sizeof (struct answer_packet_list *));
585 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
588 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
592 memset (&key, 0, sizeof key);
593 unsigned char *k = (unsigned char *) &key;
594 unsigned char *s = pkt->data + 12;
597 /* Whoever designed the reverse IPv6-lookup is batshit insane */
598 for (i = 0; i < 16; i++)
600 unsigned char c1 = s[(4 * i) + 1];
601 unsigned char c2 = s[(4 * i) + 3];
606 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
608 k[i] += 16 * (c2 - '0');
610 k[i] += 16 * (c2 - 87);
613 struct map_entry *map_entry =
614 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
615 uint16_t offset = ntohs (pkt->addroffset);
617 if (map_entry == NULL)
623 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
624 GNUNET_TIME_absolute_get ().abs_value);
627 unsigned short namelen = htons (map_entry->namelen);
628 char *name = (char *) (map_entry + 1);
631 GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + 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 2 * sizeof (struct answer_packet_list *));
651 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
653 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
655 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
659 memset (&key, 0, sizeof (GNUNET_HashCode));
661 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
663 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
666 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
667 c[10], c[11], c[12], c[13], c[14], c[15]);
668 unsigned char *k = (unsigned char *) &key;
671 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
675 for (i = 0; i < 16; i++)
678 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
680 struct map_entry *value =
681 GNUNET_malloc (sizeof (struct map_entry) + namelen);
682 char *name = (char *) (value + 1);
684 value->namelen = namelen;
685 memcpy (name, pkt->data + 12, namelen);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
689 value->addrlen = pkt->addrsize;
690 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
691 memset (value->additional_ports, 0, 8192);
693 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
695 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
697 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
698 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
700 GNUNET_CONTAINER_heap_insert (heap, value,
701 GNUNET_TIME_absolute_get ().abs_value);
702 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
703 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
709 GNUNET_malloc (htons (pkt->hdr.size) +
710 2 * sizeof (struct answer_packet_list *));
712 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
714 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
716 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
720 memset (&key, 0, sizeof (GNUNET_HashCode));
722 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
724 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
727 unsigned char *k = (unsigned char *) &key;
730 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
734 for (i = 0; i < 4; i++)
737 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
739 struct map_entry *value =
740 GNUNET_malloc (sizeof (struct map_entry) + namelen);
741 char *name = (char *) (value + 1);
743 value->namelen = namelen;
744 memcpy (name, pkt->data + 12, namelen);
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
748 value->addrlen = pkt->addrsize;
749 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
750 memset (value->additional_ports, 0, 8192);
752 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
754 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
756 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
757 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
759 GNUNET_CONTAINER_heap_insert (heap, value,
760 GNUNET_TIME_absolute_get ().abs_value);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Mapping is saved in the hashmap with key %08x.\n",
763 *((uint32_t *) (&key)));
764 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
765 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
771 GNUNET_malloc (htons (pkt->hdr.size) +
772 2 * sizeof (struct answer_packet_list *));
774 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
785 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
786 answer_proc_tail, list);
788 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
794 * Sets a bit active in a bitArray.
796 * @param bitArray memory area to set the bit in
797 * @param bitIdx which bit to set
800 setBit (char *bitArray, unsigned int bitIdx)
803 unsigned int targetBit;
805 arraySlot = bitIdx / 8;
806 targetBit = (1L << (bitIdx % 8));
807 bitArray[arraySlot] |= targetBit;
811 * Clears a bit from bitArray.
813 * @param bitArray memory area to set the bit in
814 * @param bitIdx which bit to unset
817 clearBit (char *bitArray, unsigned int bitIdx)
820 unsigned int targetBit;
823 targetBit = (1L << (bitIdx % 8));
824 bitArray[slot] = bitArray[slot] & (~targetBit);
828 * Checks if a bit is active in the bitArray
830 * @param bitArray memory area to set the bit in
831 * @param bitIdx which bit to test
832 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
835 testBit (char *bitArray, unsigned int bitIdx)
838 unsigned int targetBit;
841 targetBit = (1L << (bitIdx % 8));
842 if (bitArray[slot] & targetBit)
849 * @brief Add the port to the list of additional ports in the map_entry
851 * @param me the map_entry
852 * @param port the port in host-byte-order
855 add_additional_port (struct map_entry *me, uint16_t port)
857 setBit (me->additional_ports, port);
861 receive_udp_back (void *cls
862 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
864 __attribute__ ((unused)),
865 const struct GNUNET_PeerIdentity *sender
866 __attribute__ ((unused)),
867 const struct GNUNET_MessageHeader *message,
868 const struct GNUNET_TRANSPORT_ATS_Information *atsi
869 __attribute__ ((unused)))
871 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
872 struct remote_addr *s = (struct remote_addr *) desc;
873 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
874 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
876 if (16 == s->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 = 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 = dadr;
1013 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1015 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
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
1041 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1043 __attribute__ ((unused)),
1044 const struct GNUNET_PeerIdentity *sender
1045 __attribute__ ((unused)),
1046 const struct GNUNET_MessageHeader *message,
1047 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1048 __attribute__ ((unused)))
1050 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1051 struct remote_addr *s = (struct remote_addr *) desc;
1052 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1053 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
1056 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1057 sizeof (GNUNET_HashCode);
1059 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1061 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1064 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1066 struct ip6_tcp *pkt6 = alloca (size);
1068 memset (pkt6, 0, size);
1070 GNUNET_assert (pkt6 != NULL);
1072 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1073 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1075 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1077 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1078 pkt6->shdr.size = htons (size);
1080 pkt6->tun.flags = 0;
1081 pkt6->tun.type = htons (0x86dd);
1083 pkt6->ip6_hdr.version = 6;
1084 pkt6->ip6_hdr.tclass_h = 0;
1085 pkt6->ip6_hdr.tclass_l = 0;
1086 pkt6->ip6_hdr.flowlbl = 0;
1087 pkt6->ip6_hdr.paylgth = htons (pktlen);
1088 pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
1089 pkt6->ip6_hdr.hoplmt = 0xff;
1094 GNUNET_assert (GNUNET_OK ==
1095 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1098 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1099 GNUNET_free (ipv6addr);
1101 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1103 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1105 GNUNET_assert (key != NULL);
1107 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1109 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1110 GNUNET_TIME_absolute_get ().abs_value);
1114 GNUNET_assert (me != NULL);
1115 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1116 GNUNET_assert (me->desc.
1117 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1119 pkt6->tcp_hdr.crc = 0;
1124 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1126 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1127 tmp = htonl (pktlen);
1128 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1129 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1130 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1133 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1134 ntohs (pkt6->ip6_hdr.paylgth));
1135 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1137 write_to_helper (pkt6, size);
1141 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1143 struct ip_tcp *pkt4 = alloca (size);
1145 GNUNET_assert (pkt4 != NULL);
1146 memset (pkt4, 0, size);
1148 GNUNET_assert (ntohs (message->type) ==
1149 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1152 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1153 pkt4->ip_hdr.sadr = sadr;
1155 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1156 pkt4->shdr.size = htons (size);
1158 pkt4->tun.flags = 0;
1159 pkt4->tun.type = htons (0x0800);
1161 pkt4->ip_hdr.version = 4;
1162 pkt4->ip_hdr.hdr_lngth = 5;
1163 pkt4->ip_hdr.diff_serv = 0;
1164 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1165 pkt4->ip_hdr.ident = 0;
1166 pkt4->ip_hdr.flags = 0;
1167 pkt4->ip_hdr.frag_off = 0;
1168 pkt4->ip_hdr.ttl = 255;
1169 pkt4->ip_hdr.proto = IPPROTO_TCP;
1170 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1176 GNUNET_assert (GNUNET_OK ==
1177 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1180 inet_pton (AF_INET, ipv4addr, &dadr);
1181 GNUNET_free (ipv4addr);
1182 pkt4->ip_hdr.dadr = dadr;
1185 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1187 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1189 GNUNET_assert (key != NULL);
1191 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1193 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1194 GNUNET_TIME_absolute_get ().abs_value);
1198 GNUNET_assert (me != NULL);
1199 pkt4->tcp_hdr.crc = 0;
1203 tmp = pkt4->ip_hdr.sadr;
1204 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1205 tmp = pkt4->ip_hdr.dadr;
1206 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1208 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1212 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1214 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1215 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1218 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1220 write_to_helper (pkt4, size);
1227 * Main function that will be run by the scheduler.
1229 * @param cls closure
1230 * @param args remaining command-line arguments
1231 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1232 * @param cfg_ configuration
1235 run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
1236 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1238 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1239 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1240 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1241 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1242 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1246 static const GNUNET_MESH_ApplicationType types[] = {
1247 GNUNET_APPLICATION_TYPE_END
1250 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, types);
1253 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1254 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1255 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1257 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1258 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1260 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1265 * The main function to obtain template from gnunetd.
1267 * @param argc number of arguments from the command line
1268 * @param argv command line arguments
1269 * @return 0 ok, 1 on error
1272 main (int argc, char *const *argv)
1274 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1275 GNUNET_GETOPT_OPTION_END
1278 return (GNUNET_OK ==
1279 GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
1280 options, &run, NULL)) ? ret : 1;
1283 /* end of gnunet-daemon-vpn.c */