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);
301 port_in_ports (uint64_t ports, uint16_t port)
303 uint16_t *ps = (uint16_t *) & ports;
304 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
308 send_pkt_to_peer (void *cls,
309 const struct GNUNET_PeerIdentity *peer,
310 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
312 /* peer == NULL means that all peers in this request are connected */
313 if (peer == NULL) return;
314 struct GNUNET_MESH_Tunnel **tunnel = cls;
315 struct GNUNET_MessageHeader *hdr =
316 (struct GNUNET_MessageHeader *) (tunnel + 1);
318 GNUNET_assert(NULL != tunnel);
319 GNUNET_assert(NULL != *tunnel);
321 if (NULL == GNUNET_MESH_tunnel_get_data(*tunnel))
323 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
326 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
327 (const struct GNUNET_PeerIdentity *)NULL,
329 send_pkt_to_peer_notify_callback,
331 GNUNET_MESH_tunnel_set_data(*tunnel, th);
335 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
336 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
337 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof *element);
340 element->len = ntohs(hdr->size);
342 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
344 GNUNET_MESH_tunnel_set_head(*tunnel, head);
345 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
350 * Create a new Address from an answer-packet
353 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
355 unsigned long long ipv6prefix;
356 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
357 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
358 GNUNET_assert(ipv6prefix < 127);
359 ipv6prefix = (ipv6prefix + 7)/8;
361 inet_pton (AF_INET6, ipv6addr, buf);
362 GNUNET_free(ipv6addr);
364 int peer_length = 16 - ipv6prefix - 6;
365 if (peer_length <= 0)
368 int service_length = 16 - ipv6prefix - peer_length;
369 if (service_length <= 0)
372 memcpy(buf+ipv6prefix, service_desc, service_length);
373 memcpy(buf+ipv6prefix+service_length, peer, peer_length);
379 * Create a new Address from an answer-packet
382 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
385 unsigned long long ipv6prefix;
386 GNUNET_assert (GNUNET_OK ==
387 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
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 local_length = 16 - ipv6prefix;
402 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
407 * Create a new Address from an answer-packet
410 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
414 GNUNET_assert (GNUNET_OK ==
415 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
418 GNUNET_assert (GNUNET_OK ==
419 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
423 inet_pton (AF_INET, ipv4addr, buf);
424 int r = inet_pton (AF_INET, ipv4mask, &mask);
426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r, mask);
428 GNUNET_free (ipv4addr);
434 mask = (mask ^ (mask - 1)) >> 1;
435 for (c = 0; mask; c++)
442 c = CHAR_BIT * sizeof(mask);
446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n", ipv4mask, c);
448 GNUNET_free (ipv4mask);
455 memcpy (buf + c, addr, GNUNET_MIN (addrlen, c));
460 * This gets scheduled with cls pointing to an answer_packet and does everything
461 * needed in order to send it to the helper.
463 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
464 * doing nothing for "real" services.
467 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
468 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
471 struct answer_packet* pkt = cls;
472 struct answer_packet_list* list;
474 /* This answer is about a .gnunet-service
476 * It contains an almost complete DNS-Response, we have to fill in the ip
477 * at the offset pkt->addroffset
479 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
481 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
484 memset(&key, 0, sizeof(GNUNET_HashCode));
486 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
487 unsigned char* k = (unsigned char*)&key;
488 new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
490 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
493 for (i = 0; i < 16; i++)
496 uint16_t namelen = strlen((char*)pkt->data+12)+1;
498 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
499 char* name = (char*)(value +1);
501 value->namelen = namelen;
502 memcpy(name, pkt->data+12, namelen);
504 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
506 memset(value->additional_ports, 0, 8192);
508 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
511 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
513 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
516 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
517 GNUNET_TIME_absolute_get ().abs_value);
518 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
519 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
525 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
527 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
530 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
533 memset(&key, 0, sizeof key);
534 unsigned char* k = (unsigned char*)&key;
535 unsigned char* s = pkt->data+12;
537 /* Whoever designed the reverse IPv6-lookup is batshit insane */
538 for (i = 0; i < 16; i++)
540 unsigned char c1 = s[(4*i)+1];
541 unsigned char c2 = s[(4*i)+3];
545 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
547 k[i] += 16*(c2 - '0');
549 k[i] += 16*(c2 - 87);
552 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
553 uint16_t offset = ntohs(pkt->addroffset);
555 if (map_entry == NULL)
561 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
562 GNUNET_TIME_absolute_get ().abs_value);
565 unsigned short namelen = htons(map_entry->namelen);
566 char* name = (char*)(map_entry + 1);
568 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
570 struct answer_packet* rpkt = &list->pkt;
572 /* The offset points to the first byte belonging to the address */
573 memcpy(rpkt, pkt, offset - 1);
575 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
576 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
578 memcpy(((char*)rpkt)+offset, &namelen, 2);
579 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
582 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
584 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
585 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
587 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
589 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
592 memset(&key, 0, sizeof(GNUNET_HashCode));
594 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
595 new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
596 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",
613 unsigned char* k = (unsigned char*)&key;
615 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
618 for (i = 0; i < 16; i++)
621 uint16_t namelen = strlen((char*)pkt->data+12)+1;
623 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
624 char* name = (char*)(value +1);
626 value->namelen = namelen;
627 memcpy(name, pkt->data+12, namelen);
629 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
630 value->addrlen = pkt->addrsize;
631 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
632 memset(value->additional_ports, 0, 8192);
634 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
637 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
639 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
641 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
642 GNUNET_TIME_absolute_get ().abs_value);
643 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
644 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
649 list = GNUNET_malloc(htons(pkt->hdr.size) + 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_A)
655 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
658 memset(&key, 0, sizeof(GNUNET_HashCode));
660 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
661 new_ip4addr_remote(c, pkt->addr, pkt->addrsize);
662 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
667 unsigned char* k = (unsigned char*)&key;
669 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
672 for (i = 0; i < 4; i++)
675 uint16_t namelen = strlen((char*)pkt->data+12)+1;
677 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
678 char* name = (char*)(value +1);
680 value->namelen = namelen;
681 memcpy(name, pkt->data+12, namelen);
683 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
684 value->addrlen = pkt->addrsize;
685 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
686 memset(value->additional_ports, 0, 8192);
688 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
691 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
693 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
695 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
696 GNUNET_TIME_absolute_get ().abs_value);
697 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping is saved in the hashmap with key %08x.\n",
698 *((uint32_t*)(&key)));
699 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
700 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
705 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
707 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
718 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
720 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
726 * Sets a bit active in a bitArray.
728 * @param bitArray memory area to set the bit in
729 * @param bitIdx which bit to set
732 setBit (char *bitArray, unsigned int bitIdx)
735 unsigned int targetBit;
737 arraySlot = bitIdx / 8;
738 targetBit = (1L << (bitIdx % 8));
739 bitArray[arraySlot] |= targetBit;
743 * Clears a bit from bitArray.
745 * @param bitArray memory area to set the bit in
746 * @param bitIdx which bit to unset
749 clearBit (char *bitArray, unsigned int bitIdx)
752 unsigned int targetBit;
755 targetBit = (1L << (bitIdx % 8));
756 bitArray[slot] = bitArray[slot] & (~targetBit);
760 * Checks if a bit is active in the bitArray
762 * @param bitArray memory area to set the bit in
763 * @param bitIdx which bit to test
764 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
767 testBit (char *bitArray, unsigned int bitIdx)
770 unsigned int targetBit;
773 targetBit = (1L << (bitIdx % 8));
774 if (bitArray[slot] & targetBit)
781 * @brief Add the port to the list of additional ports in the map_entry
783 * @param me the map_entry
784 * @param port the port in host-byte-order
787 add_additional_port (struct map_entry *me, uint16_t port)
789 setBit(me->additional_ports, port);
793 receive_udp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
794 void **tunnel_ctx __attribute__((unused)),
795 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
796 const struct GNUNET_MessageHeader *message,
797 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
799 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
800 struct remote_addr* s = (struct remote_addr*)desc;
801 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
802 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
804 size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
806 struct ip6_udp* pkt6 = alloca(size);
808 GNUNET_assert(pkt6 != NULL);
810 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
811 new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
813 new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
817 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
818 pkt6->shdr.size = htons(size);
821 pkt6->tun.type = htons(0x86dd);
823 pkt6->ip6_hdr.version = 6;
824 pkt6->ip6_hdr.tclass_h = 0;
825 pkt6->ip6_hdr.tclass_l = 0;
826 pkt6->ip6_hdr.flowlbl = 0;
827 pkt6->ip6_hdr.paylgth = pkt->len;
828 pkt6->ip6_hdr.nxthdr = 0x11;
829 pkt6->ip6_hdr.hoplmt = 0xff;
833 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
834 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
835 GNUNET_free(ipv6addr);
837 memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
839 GNUNET_HashCode* key = address6_mapping_exists(pkt6->ip6_hdr.sadr);
840 GNUNET_assert (key != NULL);
842 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
843 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
844 GNUNET_TIME_absolute_get ().abs_value);
848 GNUNET_assert (me != NULL);
849 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
851 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
852 if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) &&
853 !testBit(me->additional_ports, ntohs(pkt6->udp_hdr.spt))) {
854 add_additional_port(me, ntohs(pkt6->udp_hdr.spt));
858 pkt6->udp_hdr.crc = 0;
860 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
861 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
862 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
863 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
864 tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
865 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
867 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
868 pkt6->udp_hdr.crc = calculate_checksum_end(sum);
870 write_to_helper(pkt6, size);
876 receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
877 void **tunnel_ctx __attribute__((unused)),
878 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
879 const struct GNUNET_MessageHeader *message,
880 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
882 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
883 struct remote_addr* s = (struct remote_addr*)desc;
884 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
885 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
887 size_t pktlen = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader) - sizeof(GNUNET_HashCode);
888 size_t size = pktlen + sizeof(struct ip6_tcp) - 1;
890 struct ip6_tcp* pkt6 = alloca(size);
891 memset(pkt6, 0, size);
893 GNUNET_assert(pkt6 != NULL);
895 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK)
896 new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
898 new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
900 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
901 pkt6->shdr.size = htons(size);
904 pkt6->tun.type = htons(0x86dd);
906 pkt6->ip6_hdr.version = 6;
907 pkt6->ip6_hdr.tclass_h = 0;
908 pkt6->ip6_hdr.tclass_l = 0;
909 pkt6->ip6_hdr.flowlbl = 0;
910 pkt6->ip6_hdr.paylgth = htons(pktlen);
911 pkt6->ip6_hdr.nxthdr = 0x06;
912 pkt6->ip6_hdr.hoplmt = 0xff;
916 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
917 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
918 GNUNET_free(ipv6addr);
920 memcpy(&pkt6->tcp_hdr, pkt, pktlen);
922 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);
926 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
927 GNUNET_TIME_absolute_get ().abs_value);
931 GNUNET_assert (me != NULL);
932 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
933 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_TCP));
935 pkt6->tcp_hdr.crc = 0;
939 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
941 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
943 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
944 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
945 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
948 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
949 ntohs (pkt6->ip6_hdr.paylgth));
950 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
952 write_to_helper(pkt6, size);
958 * Main function that will be run by the scheduler.
961 * @param args remaining command-line arguments
962 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
963 * @param cfg_ configuration
967 char *const *args __attribute__((unused)),
968 const char *cfgfilep __attribute__((unused)),
969 const struct GNUNET_CONFIGURATION_Handle *cfg_)
971 static const struct GNUNET_MESH_MessageHandler handlers[] = {
972 {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
973 {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
974 {receive_udp_back, GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK, 0},
975 {receive_tcp_back, GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK, 0},
979 static const GNUNET_MESH_ApplicationType types[] = {
980 GNUNET_APPLICATION_TYPE_END
983 mesh_handle = GNUNET_MESH_connect(cfg_,
990 hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
991 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
992 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
994 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
995 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
996 shs_task = GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
997 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1001 * The main function to obtain template from gnunetd.
1003 * @param argc number of arguments from the command line
1004 * @param argv command line arguments
1005 * @return 0 ok, 1 on error
1008 main (int argc, char *const *argv) {
1009 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1010 GNUNET_GETOPT_OPTION_END
1013 return (GNUNET_OK ==
1014 GNUNET_PROGRAM_run (argc,
1017 gettext_noop ("help text"),
1018 options, &run, NULL)) ? ret : 1;
1021 /* end of gnunet-daemon-vpn.c */