2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Toelke
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet-vpn-packet.h"
30 #include "gnunet_common.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include <gnunet_mesh_service.h>
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include <block_dns.h>
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-daemon-vpn-dns.h"
40 #include "gnunet-daemon-vpn.h"
41 #include "gnunet-vpn-checksum.h"
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_MESH_Handle *mesh_handle;
45 struct GNUNET_CONTAINER_MultiHashMap* hashmap;
46 static struct GNUNET_CONTAINER_Heap *heap;
48 struct tunnel_notify_queue
50 struct tunnel_notify_queue* next;
51 struct tunnel_notify_queue* prev;
57 * If there are at least this many address-mappings, old ones will be removed
59 static long long unsigned int max_mappings = 200;
67 * This hashmap contains the mapping from peer, service-descriptor,
68 * source-port and destination-port to a socket
70 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
72 GNUNET_SCHEDULER_TaskIdentifier conn_task;
74 GNUNET_SCHEDULER_TaskIdentifier shs_task;
77 * Function scheduled as very last function, cleans up after us
81 cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
82 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
85 cleanup_helper(helper_handle);
87 /* close the connection to the service-dns */
88 if (dns_connection != NULL)
90 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
91 dns_connection = NULL;
94 if (mesh_handle != NULL)
96 GNUNET_MESH_disconnect(mesh_handle);
99 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
101 GNUNET_SCHEDULER_cancel (shs_task);
102 shs_task = GNUNET_SCHEDULER_NO_TASK;
104 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
106 GNUNET_SCHEDULER_cancel (conn_task);
107 conn_task = GNUNET_SCHEDULER_NO_TASK;
113 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
116 address6_mapping_exists(unsigned char addr[]) {
117 GNUNET_HashCode* key = GNUNET_malloc(sizeof(GNUNET_HashCode));
118 unsigned char* k = (unsigned char*)key;
119 memset(key, 0, sizeof(GNUNET_HashCode));
121 for (i = 0; i < 16; i++)
124 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(hashmap, key))
134 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
137 address4_mapping_exists (uint32_t addr)
139 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
140 memset (key, 0, sizeof (GNUNET_HashCode));
141 unsigned char *c = (unsigned char *) &addr;
142 unsigned char *k = (unsigned char *) key;
144 for (i = 0; i < 4; i++)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
149 *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
151 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
162 collect_mappings(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tc) {
163 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
166 struct map_entry* me = GNUNET_CONTAINER_heap_remove_root(heap);
168 /* This is free()ed memory! */
169 me->heap_node = NULL;
171 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
173 GNUNET_CONTAINER_multihashmap_remove(hashmap, &me->hash, me);
179 send_icmp4_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
180 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
183 struct ip_icmp* request = cls;
185 struct ip_icmp* response = alloca(ntohs(request->shdr.size));
186 GNUNET_assert(response != NULL);
187 memset(response, 0, ntohs(request->shdr.size));
189 response->shdr.size = request->shdr.size;
190 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
192 response->tun.flags = 0;
193 response->tun.type = htons(0x0800);
195 response->ip_hdr.hdr_lngth = 5;
196 response->ip_hdr.version = 4;
197 response->ip_hdr.proto = 0x01;
198 response->ip_hdr.dadr = request->ip_hdr.sadr;
199 response->ip_hdr.sadr = request->ip_hdr.dadr;
200 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
202 response->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&response->ip_hdr, 20);
204 response->icmp_hdr.code = 0;
205 response->icmp_hdr.type = 0x0;
207 /* Magic, more Magic! */
208 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
210 /* Copy the rest of the packet */
211 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip_icmp));
213 write_to_helper(response, ntohs(response->shdr.size));
215 GNUNET_free(request);
219 send_icmp6_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
220 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
223 struct ip6_icmp* request = cls;
225 struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
226 GNUNET_assert(response != NULL);
227 memset(response, 0, ntohs(request->shdr.size));
229 response->shdr.size = request->shdr.size;
230 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
232 response->tun.flags = 0;
233 response->tun.type = htons(0x86dd);
235 response->ip6_hdr.hoplmt = 255;
236 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
237 response->ip6_hdr.nxthdr = 0x3a;
238 response->ip6_hdr.version = 6;
239 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
240 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
242 response->icmp_hdr.code = 0;
243 response->icmp_hdr.type = 0x81;
245 /* Magic, more Magic! */
246 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
248 /* Copy the rest of the packet */
249 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
251 write_to_helper(response, ntohs(response->shdr.size));
253 GNUNET_free(request);
257 * cls is the pointer to a GNUNET_MessageHeader that is
258 * followed by the service-descriptor and the packet that should be sent;
261 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
263 struct GNUNET_MESH_Tunnel **tunnel = cls;
264 GNUNET_MESH_tunnel_set_data(*tunnel, NULL);
265 struct GNUNET_MessageHeader *hdr =
266 (struct GNUNET_MessageHeader *) (tunnel + 1);
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
268 GNUNET_assert (size >= ntohs (hdr->size));
269 memcpy (buf, hdr, ntohs (hdr->size));
270 size = ntohs(hdr->size);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
273 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
275 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
276 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
277 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
279 GNUNET_CONTAINER_DLL_remove(head, tail, element);
281 GNUNET_MESH_tunnel_set_head(*tunnel, head);
282 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
284 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
287 GNUNET_TIME_relative_divide
288 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
289 (const struct GNUNET_PeerIdentity *)
291 send_pkt_to_peer_notify_callback, element->cls);
292 /* save the handle */
293 GNUNET_MESH_tunnel_set_data(*tunnel, th);
294 GNUNET_free(element);
302 port_in_ports (uint64_t ports, uint16_t port)
304 uint16_t *ps = (uint16_t *) & ports;
305 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
309 send_pkt_to_peer (void *cls,
310 const struct GNUNET_PeerIdentity *peer,
311 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
313 /* peer == NULL means that all peers in this request are connected */
314 if (peer == NULL) return;
315 struct GNUNET_MESH_Tunnel **tunnel = cls;
316 struct GNUNET_MessageHeader *hdr =
317 (struct GNUNET_MessageHeader *) (tunnel + 1);
319 GNUNET_assert(NULL != tunnel);
320 GNUNET_assert(NULL != *tunnel);
322 if (NULL == GNUNET_MESH_tunnel_get_data(*tunnel))
324 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
327 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
328 (const struct GNUNET_PeerIdentity *)NULL,
330 send_pkt_to_peer_notify_callback,
332 GNUNET_MESH_tunnel_set_data(*tunnel, th);
336 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
337 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
338 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof *element);
341 element->len = ntohs(hdr->size);
343 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
345 GNUNET_MESH_tunnel_set_head(*tunnel, head);
346 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
351 * Create a new Address from an answer-packet
354 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
356 unsigned long long ipv6prefix;
357 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
358 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
359 GNUNET_assert(ipv6prefix < 127);
360 ipv6prefix = (ipv6prefix + 7)/8;
362 inet_pton (AF_INET6, ipv6addr, buf);
363 GNUNET_free(ipv6addr);
365 int peer_length = 16 - ipv6prefix - 6;
366 if (peer_length <= 0)
369 int service_length = 16 - ipv6prefix - peer_length;
370 if (service_length <= 0)
373 memcpy(buf+ipv6prefix, service_desc, service_length);
374 memcpy(buf+ipv6prefix+service_length, peer, peer_length);
380 * Create a new Address from an answer-packet
383 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
386 unsigned long long ipv6prefix;
387 GNUNET_assert (GNUNET_OK ==
388 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
391 GNUNET_assert (GNUNET_OK ==
392 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
395 GNUNET_assert (ipv6prefix < 127);
396 ipv6prefix = (ipv6prefix + 7) / 8;
398 inet_pton (AF_INET6, ipv6addr, buf);
399 GNUNET_free (ipv6addr);
401 int local_length = 16 - ipv6prefix;
403 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
408 * Create a new Address from an answer-packet
411 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
415 GNUNET_assert (GNUNET_OK ==
416 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
419 GNUNET_assert (GNUNET_OK ==
420 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
424 inet_pton (AF_INET, ipv4addr, buf);
425 int r = inet_pton (AF_INET, ipv4mask, &mask);
427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r, mask);
429 GNUNET_free (ipv4addr);
435 mask = (mask ^ (mask - 1)) >> 1;
436 for (c = 0; mask; c++)
443 c = CHAR_BIT * sizeof(mask);
447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n", ipv4mask, c);
449 GNUNET_free (ipv4mask);
456 memcpy (buf + c, addr, GNUNET_MIN (addrlen, c));
461 * This gets scheduled with cls pointing to an answer_packet and does everything
462 * needed in order to send it to the helper.
464 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
465 * doing nothing for "real" services.
468 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
469 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
472 struct answer_packet* pkt = cls;
473 struct answer_packet_list* list;
475 /* This answer is about a .gnunet-service
477 * It contains an almost complete DNS-Response, we have to fill in the ip
478 * at the offset pkt->addroffset
480 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
482 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
485 memset(&key, 0, sizeof(GNUNET_HashCode));
487 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
488 unsigned char* k = (unsigned char*)&key;
489 new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
491 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
494 for (i = 0; i < 16; i++)
497 uint16_t namelen = strlen((char*)pkt->data+12)+1;
499 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
500 char* name = (char*)(value +1);
502 value->namelen = namelen;
503 memcpy(name, pkt->data+12, namelen);
505 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
507 memset(value->additional_ports, 0, 8192);
509 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
512 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
514 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
517 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
518 GNUNET_TIME_absolute_get ().abs_value);
519 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
520 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
526 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
528 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
531 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
534 memset(&key, 0, sizeof key);
535 unsigned char* k = (unsigned char*)&key;
536 unsigned char* s = pkt->data+12;
538 /* Whoever designed the reverse IPv6-lookup is batshit insane */
539 for (i = 0; i < 16; i++)
541 unsigned char c1 = s[(4*i)+1];
542 unsigned char c2 = s[(4*i)+3];
546 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
548 k[i] += 16*(c2 - '0');
550 k[i] += 16*(c2 - 87);
553 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
554 uint16_t offset = ntohs(pkt->addroffset);
556 if (map_entry == NULL)
562 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
563 GNUNET_TIME_absolute_get ().abs_value);
566 unsigned short namelen = htons(map_entry->namelen);
567 char* name = (char*)(map_entry + 1);
569 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
571 struct answer_packet* rpkt = &list->pkt;
573 /* The offset points to the first byte belonging to the address */
574 memcpy(rpkt, pkt, offset - 1);
576 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
577 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
579 memcpy(((char*)rpkt)+offset, &namelen, 2);
580 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
583 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
585 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
586 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
588 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
590 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
593 memset(&key, 0, sizeof(GNUNET_HashCode));
595 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
596 new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
597 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
614 unsigned char* k = (unsigned char*)&key;
616 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
619 for (i = 0; i < 16; i++)
622 uint16_t namelen = strlen((char*)pkt->data+12)+1;
624 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
625 char* name = (char*)(value +1);
627 value->namelen = namelen;
628 memcpy(name, pkt->data+12, namelen);
630 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
631 value->addrlen = pkt->addrsize;
632 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
633 memset(value->additional_ports, 0, 8192);
635 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
638 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
640 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
641 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
642 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
643 GNUNET_TIME_absolute_get ().abs_value);
644 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
645 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
650 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
652 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
654 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
656 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
659 memset(&key, 0, sizeof(GNUNET_HashCode));
661 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
662 new_ip4addr_remote(c, pkt->addr, pkt->addrsize);
663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
668 unsigned char* k = (unsigned char*)&key;
670 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
673 for (i = 0; i < 4; i++)
676 uint16_t namelen = strlen((char*)pkt->data+12)+1;
678 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
679 char* name = (char*)(value +1);
681 value->namelen = namelen;
682 memcpy(name, pkt->data+12, namelen);
684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
685 value->addrlen = pkt->addrsize;
686 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
687 memset(value->additional_ports, 0, 8192);
689 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
692 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
694 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
696 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
697 GNUNET_TIME_absolute_get ().abs_value);
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping is saved in the hashmap with key %08x.\n",
699 *((uint32_t*)(&key)));
700 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
701 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
706 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
708 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
719 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
721 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
727 * Sets a bit active in a bitArray.
729 * @param bitArray memory area to set the bit in
730 * @param bitIdx which bit to set
733 setBit (char *bitArray, unsigned int bitIdx)
736 unsigned int targetBit;
738 arraySlot = bitIdx / 8;
739 targetBit = (1L << (bitIdx % 8));
740 bitArray[arraySlot] |= targetBit;
744 * Clears a bit from bitArray.
746 * @param bitArray memory area to set the bit in
747 * @param bitIdx which bit to unset
750 clearBit (char *bitArray, unsigned int bitIdx)
753 unsigned int targetBit;
756 targetBit = (1L << (bitIdx % 8));
757 bitArray[slot] = bitArray[slot] & (~targetBit);
761 * Checks if a bit is active in the bitArray
763 * @param bitArray memory area to set the bit in
764 * @param bitIdx which bit to test
765 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
768 testBit (char *bitArray, unsigned int bitIdx)
771 unsigned int targetBit;
774 targetBit = (1L << (bitIdx % 8));
775 if (bitArray[slot] & targetBit)
782 * @brief Add the port to the list of additional ports in the map_entry
784 * @param me the map_entry
785 * @param port the port in host-byte-order
788 add_additional_port (struct map_entry *me, uint16_t port)
790 setBit(me->additional_ports, port);
794 receive_udp_back (void *cls
795 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
796 void **tunnel_ctx __attribute__ ((unused)),
797 const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)),
798 const struct GNUNET_MessageHeader *message,
799 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__ ((unused)))
801 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
802 struct remote_addr *s = (struct remote_addr *) desc;
803 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
804 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
806 if (16 == s->addrlen)
809 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
811 struct ip6_udp *pkt6 = alloca (size);
813 GNUNET_assert (pkt6 != NULL);
815 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
816 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
818 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
822 ntohs (message->size), ntohs (pkt->len));
824 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
825 pkt6->shdr.size = htons (size);
828 pkt6->tun.type = htons (0x86dd);
830 pkt6->ip6_hdr.version = 6;
831 pkt6->ip6_hdr.tclass_h = 0;
832 pkt6->ip6_hdr.tclass_l = 0;
833 pkt6->ip6_hdr.flowlbl = 0;
834 pkt6->ip6_hdr.paylgth = pkt->len;
835 pkt6->ip6_hdr.nxthdr = 0x11;
836 pkt6->ip6_hdr.hoplmt = 0xff;
840 GNUNET_assert (GNUNET_OK ==
841 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
844 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
845 GNUNET_free (ipv6addr);
847 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
849 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
850 GNUNET_assert (key != NULL);
852 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
853 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
854 GNUNET_TIME_absolute_get ().
859 GNUNET_assert (me != NULL);
860 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
862 GNUNET_assert (me->desc.
863 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
864 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt)
865 && !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
867 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
871 pkt6->udp_hdr.crc = 0;
874 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
877 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
879 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
880 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
881 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
882 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
885 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
887 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
889 write_to_helper (pkt6, size);
894 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
896 struct ip_udp *pkt4 = alloca (size);
898 GNUNET_assert (pkt4 != NULL);
900 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
902 new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen);
903 pkt4->ip_hdr.sadr = sadr;
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
907 ntohs (message->size), ntohs (pkt->len));
909 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
910 pkt4->shdr.size = htons (size);
913 pkt4->tun.type = htons (0x0800);
915 pkt4->ip_hdr.version = 4;
916 pkt4->ip_hdr.hdr_lngth = 5;
917 pkt4->ip_hdr.diff_serv = 0;
918 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs(pkt->len));
919 pkt4->ip_hdr.ident = 0;
920 pkt4->ip_hdr.flags = 0;
921 pkt4->ip_hdr.frag_off = 0;
922 pkt4->ip_hdr.ttl = 255;
923 pkt4->ip_hdr.proto = 0x11;
924 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
929 GNUNET_assert (GNUNET_OK ==
930 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
933 inet_pton (AF_INET, ipv4addr, &dadr);
934 GNUNET_free (ipv4addr);
935 pkt4->ip_hdr.dadr = dadr;
937 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
939 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
940 GNUNET_assert (key != NULL);
942 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
943 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
944 GNUNET_TIME_absolute_get ().
949 GNUNET_assert (me != NULL);
951 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
954 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
956 write_to_helper (pkt4, size);
963 receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
964 void **tunnel_ctx __attribute__((unused)),
965 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
966 const struct GNUNET_MessageHeader *message,
967 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
969 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
970 struct remote_addr *s = (struct remote_addr *) desc;
971 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
972 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
975 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
976 sizeof (GNUNET_HashCode);
978 if (s->addrlen == 16)
980 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
982 struct ip6_tcp *pkt6 = alloca (size);
983 memset (pkt6, 0, size);
985 GNUNET_assert (pkt6 != NULL);
987 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK)
988 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
990 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
992 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
993 pkt6->shdr.size = htons (size);
996 pkt6->tun.type = htons (0x86dd);
998 pkt6->ip6_hdr.version = 6;
999 pkt6->ip6_hdr.tclass_h = 0;
1000 pkt6->ip6_hdr.tclass_l = 0;
1001 pkt6->ip6_hdr.flowlbl = 0;
1002 pkt6->ip6_hdr.paylgth = htons (pktlen);
1003 pkt6->ip6_hdr.nxthdr = 0x06;
1004 pkt6->ip6_hdr.hoplmt = 0xff;
1008 GNUNET_assert (GNUNET_OK ==
1009 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1012 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1013 GNUNET_free (ipv6addr);
1015 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
1017 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
1018 GNUNET_assert (key != NULL);
1020 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 ().
1027 GNUNET_assert (me != NULL);
1028 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
1029 GNUNET_assert (me->desc.
1030 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1032 pkt6->tcp_hdr.crc = 0;
1036 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
1039 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
1041 tmp = htonl (pktlen);
1042 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1043 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1044 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1047 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1048 ntohs (pkt6->ip6_hdr.paylgth));
1049 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1051 write_to_helper (pkt6, size);
1055 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1057 struct ip_tcp *pkt4 = alloca (size);
1058 GNUNET_assert (pkt4 != NULL);
1059 memset (pkt4, 0, size);
1061 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
1063 new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen);
1064 pkt4->ip_hdr.sadr = sadr;
1066 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1067 pkt4->shdr.size = htons (size);
1069 pkt4->tun.flags = 0;
1070 pkt4->tun.type = htons (0x0800);
1072 pkt4->ip_hdr.version = 4;
1073 pkt4->ip_hdr.hdr_lngth = 5;
1074 pkt4->ip_hdr.diff_serv = 0;
1075 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1076 pkt4->ip_hdr.ident = 0;
1077 pkt4->ip_hdr.flags = 0;
1078 pkt4->ip_hdr.frag_off = 0;
1079 pkt4->ip_hdr.ttl = 255;
1080 pkt4->ip_hdr.proto = 0x06;
1081 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1086 GNUNET_assert (GNUNET_OK ==
1087 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1090 inet_pton (AF_INET, ipv4addr, &dadr);
1091 GNUNET_free (ipv4addr);
1092 pkt4->ip_hdr.dadr = dadr;
1095 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1097 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1098 GNUNET_assert (key != NULL);
1100 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1101 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1102 GNUNET_TIME_absolute_get ().
1107 GNUNET_assert (me != NULL);
1108 pkt4->tcp_hdr.crc = 0;
1111 tmp = pkt4->ip_hdr.sadr;
1113 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1114 tmp = pkt4->ip_hdr.dadr;
1116 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1118 tmp = (0x06 << 16) | (0xffff & pktlen);
1122 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1125 calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1126 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1129 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1131 write_to_helper (pkt4, size);
1138 * Main function that will be run by the scheduler.
1140 * @param cls closure
1141 * @param args remaining command-line arguments
1142 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1143 * @param cfg_ configuration
1147 char *const *args __attribute__((unused)),
1148 const char *cfgfilep __attribute__((unused)),
1149 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1151 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1152 {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
1153 {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
1154 {receive_udp_back, GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK, 0},
1155 {receive_tcp_back, GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK, 0},
1159 static const GNUNET_MESH_ApplicationType types[] = {
1160 GNUNET_APPLICATION_TYPE_END
1163 mesh_handle = GNUNET_MESH_connect(cfg_,
1170 hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
1171 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1172 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1174 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
1175 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1176 shs_task = GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1177 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1181 * The main function to obtain template from gnunetd.
1183 * @param argc number of arguments from the command line
1184 * @param argv command line arguments
1185 * @return 0 ok, 1 on error
1188 main (int argc, char *const *argv) {
1189 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1190 GNUNET_GETOPT_OPTION_END
1193 return (GNUNET_OK ==
1194 GNUNET_PROGRAM_run (argc,
1197 gettext_noop ("help text"),
1198 options, &run, NULL)) ? ret : 1;
1201 /* end of gnunet-daemon-vpn.c */