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 address_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 collect_mappings(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tc) {
135 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
138 struct map_entry* me = GNUNET_CONTAINER_heap_remove_root(heap);
140 /* This is free()ed memory! */
141 me->heap_node = NULL;
143 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
145 GNUNET_CONTAINER_multihashmap_remove(hashmap, &me->hash, me);
151 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
152 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
155 struct ip6_icmp* request = cls;
157 struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
158 GNUNET_assert(response != NULL);
159 memset(response, 0, ntohs(request->shdr.size));
161 response->shdr.size = request->shdr.size;
162 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
164 response->tun.flags = 0;
165 response->tun.type = htons(0x86dd);
167 response->ip6_hdr.hoplmt = 255;
168 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
169 response->ip6_hdr.nxthdr = 0x3a;
170 response->ip6_hdr.version = 6;
171 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
172 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
174 response->icmp_hdr.code = 0;
175 response->icmp_hdr.type = 0x81;
177 /* Magic, more Magic! */
178 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
180 /* Copy the rest of the packet */
181 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
183 write_to_helper(response, ntohs(response->shdr.size));
185 GNUNET_free(request);
189 * cls is the pointer to a GNUNET_MessageHeader that is
190 * followed by the service-descriptor and the packet that should be sent;
193 send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
195 struct GNUNET_MESH_Tunnel **tunnel = cls;
196 GNUNET_MESH_tunnel_set_data(*tunnel, NULL);
197 struct GNUNET_MessageHeader *hdr =
198 (struct GNUNET_MessageHeader *) (tunnel + 1);
199 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
200 GNUNET_assert (size >= ntohs (hdr->size));
201 memcpy (buf, hdr, ntohs (hdr->size));
202 size = ntohs(hdr->size);
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
206 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
208 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
209 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
210 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
212 GNUNET_CONTAINER_DLL_remove(head, tail, element);
214 GNUNET_MESH_tunnel_set_head(*tunnel, head);
215 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
217 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
220 GNUNET_TIME_relative_divide
221 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
222 (const struct GNUNET_PeerIdentity *)
224 send_pkt_to_peer_notify_callback, element->cls);
225 /* save the handle */
226 GNUNET_MESH_tunnel_set_data(*tunnel, th);
233 port_in_ports (uint64_t ports, uint16_t port)
235 uint16_t *ps = (uint16_t *) & ports;
236 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
240 send_pkt_to_peer (void *cls,
241 const struct GNUNET_PeerIdentity *peer,
242 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
244 /* peer == NULL means that all peers in this request are connected */
245 if (peer == NULL) return;
246 struct GNUNET_MESH_Tunnel **tunnel = cls;
247 struct GNUNET_MessageHeader *hdr =
248 (struct GNUNET_MessageHeader *) (tunnel + 1);
250 GNUNET_assert(NULL != tunnel);
251 GNUNET_assert(NULL != *tunnel);
253 if (NULL == GNUNET_MESH_tunnel_get_data(*tunnel))
255 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
258 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
259 (const struct GNUNET_PeerIdentity *)NULL,
261 send_pkt_to_peer_notify_callback,
263 GNUNET_MESH_tunnel_set_data(*tunnel, th);
267 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
268 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
269 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof *element);
272 element->len = ntohs(hdr->size);
274 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
276 GNUNET_MESH_tunnel_set_head(*tunnel, head);
277 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
282 * Create a new Address from an answer-packet
285 new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
287 unsigned long long ipv6prefix;
288 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
289 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
290 GNUNET_assert(ipv6prefix < 127);
291 ipv6prefix = (ipv6prefix + 7)/8;
293 inet_pton (AF_INET6, ipv6addr, buf);
294 GNUNET_free(ipv6addr);
296 int peer_length = 16 - ipv6prefix - 6;
297 if (peer_length <= 0)
300 int service_length = 16 - ipv6prefix - peer_length;
301 if (service_length <= 0)
304 memcpy(buf+ipv6prefix, service_desc, service_length);
305 memcpy(buf+ipv6prefix+service_length, peer, peer_length);
311 * Create a new Address from an answer-packet
314 new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
317 unsigned long long ipv6prefix;
318 GNUNET_assert (GNUNET_OK ==
319 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
322 GNUNET_assert (GNUNET_OK ==
323 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
326 GNUNET_assert (ipv6prefix < 127);
327 ipv6prefix = (ipv6prefix + 7) / 8;
329 inet_pton (AF_INET6, ipv6addr, buf);
330 GNUNET_free (ipv6addr);
332 int local_length = 16 - ipv6prefix;
334 memcpy (buf + ipv6prefix, addr, GNUNET_MAX (addrlen, local_length));
339 * This gets scheduled with cls pointing to an answer_packet and does everything
340 * needed in order to send it to the helper.
342 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
343 * doing nothing for "real" services.
346 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
347 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
350 struct answer_packet* pkt = cls;
351 struct answer_packet_list* list;
353 /* This answer is about a .gnunet-service
355 * It contains an almost complete DNS-Response, we have to fill in the ip
356 * at the offset pkt->addroffset
358 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
360 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
363 memset(&key, 0, sizeof(GNUNET_HashCode));
365 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
366 unsigned char* k = (unsigned char*)&key;
367 new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
369 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
372 for (i = 0; i < 16; i++)
375 uint16_t namelen = strlen((char*)pkt->data+12)+1;
377 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
378 char* name = (char*)(value +1);
380 value->namelen = namelen;
381 memcpy(name, pkt->data+12, namelen);
383 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
385 memset(value->additional_ports, 0, 8192);
387 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
390 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
392 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
393 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
395 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
396 GNUNET_TIME_absolute_get ().abs_value);
397 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
398 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
404 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
406 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
409 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
412 memset(&key, 0, sizeof key);
413 unsigned char* k = (unsigned char*)&key;
414 unsigned char* s = pkt->data+12;
416 /* Whoever designed the reverse IPv6-lookup is batshit insane */
417 for (i = 0; i < 16; i++)
419 unsigned char c1 = s[(4*i)+1];
420 unsigned char c2 = s[(4*i)+3];
424 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
426 k[i] += 16*(c2 - '0');
428 k[i] += 16*(c2 - 87);
431 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
432 uint16_t offset = ntohs(pkt->addroffset);
434 if (map_entry == NULL)
440 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
441 GNUNET_TIME_absolute_get ().abs_value);
444 unsigned short namelen = htons(map_entry->namelen);
445 char* name = (char*)(map_entry + 1);
447 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
449 struct answer_packet* rpkt = &list->pkt;
451 /* The offset points to the first byte belonging to the address */
452 memcpy(rpkt, pkt, offset - 1);
454 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
455 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
457 memcpy(((char*)rpkt)+offset, &namelen, 2);
458 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
461 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
463 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
464 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
466 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE)
468 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
471 memset(&key, 0, sizeof(GNUNET_HashCode));
473 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
474 new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
475 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",
492 unsigned char* k = (unsigned char*)&key;
494 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
497 for (i = 0; i < 16; i++)
500 uint16_t namelen = strlen((char*)pkt->data+12)+1;
502 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
503 char* name = (char*)(value +1);
505 value->namelen = namelen;
506 memcpy(name, pkt->data+12, namelen);
508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
509 value->addrlen = pkt->addrsize;
510 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
511 memset(value->additional_ports, 0, 8192);
513 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
516 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
518 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
520 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
521 GNUNET_TIME_absolute_get ().abs_value);
522 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
523 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
528 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
530 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
541 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
543 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
549 * Sets a bit active in a bitArray.
551 * @param bitArray memory area to set the bit in
552 * @param bitIdx which bit to set
555 setBit (char *bitArray, unsigned int bitIdx)
558 unsigned int targetBit;
560 arraySlot = bitIdx / 8;
561 targetBit = (1L << (bitIdx % 8));
562 bitArray[arraySlot] |= targetBit;
566 * Clears a bit from bitArray.
568 * @param bitArray memory area to set the bit in
569 * @param bitIdx which bit to unset
572 clearBit (char *bitArray, unsigned int bitIdx)
575 unsigned int targetBit;
578 targetBit = (1L << (bitIdx % 8));
579 bitArray[slot] = bitArray[slot] & (~targetBit);
583 * Checks if a bit is active in the bitArray
585 * @param bitArray memory area to set the bit in
586 * @param bitIdx which bit to test
587 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
590 testBit (char *bitArray, unsigned int bitIdx)
593 unsigned int targetBit;
596 targetBit = (1L << (bitIdx % 8));
597 if (bitArray[slot] & targetBit)
604 * @brief Add the port to the list of additional ports in the map_entry
606 * @param me the map_entry
607 * @param port the port in host-byte-order
610 add_additional_port (struct map_entry *me, uint16_t port)
612 setBit(me->additional_ports, port);
616 receive_udp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
617 void **tunnel_ctx __attribute__((unused)),
618 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
619 const struct GNUNET_MessageHeader *message,
620 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
622 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
623 struct remote_addr* s = (struct remote_addr*)desc;
624 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
625 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
627 size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
629 struct ip6_udp* pkt6 = alloca(size);
631 GNUNET_assert(pkt6 != NULL);
633 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
634 new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
636 new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
638 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
640 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
641 pkt6->shdr.size = htons(size);
644 pkt6->tun.type = htons(0x86dd);
646 pkt6->ip6_hdr.version = 6;
647 pkt6->ip6_hdr.tclass_h = 0;
648 pkt6->ip6_hdr.tclass_l = 0;
649 pkt6->ip6_hdr.flowlbl = 0;
650 pkt6->ip6_hdr.paylgth = pkt->len;
651 pkt6->ip6_hdr.nxthdr = 0x11;
652 pkt6->ip6_hdr.hoplmt = 0xff;
656 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
657 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
658 GNUNET_free(ipv6addr);
660 memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
662 GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
663 GNUNET_assert (key != NULL);
665 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
666 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
667 GNUNET_TIME_absolute_get ().abs_value);
671 GNUNET_assert (me != NULL);
672 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
674 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
675 if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) &&
676 !testBit(me->additional_ports, ntohs(pkt6->udp_hdr.spt))) {
677 add_additional_port(me, ntohs(pkt6->udp_hdr.spt));
681 pkt6->udp_hdr.crc = 0;
683 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
684 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
685 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
686 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
687 tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
688 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
690 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
691 pkt6->udp_hdr.crc = calculate_checksum_end(sum);
693 write_to_helper(pkt6, size);
699 receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
700 void **tunnel_ctx __attribute__((unused)),
701 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
702 const struct GNUNET_MessageHeader *message,
703 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
705 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
706 struct remote_addr* s = (struct remote_addr*)desc;
707 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
708 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
710 size_t pktlen = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader) - sizeof(GNUNET_HashCode);
711 size_t size = pktlen + sizeof(struct ip6_tcp) - 1;
713 struct ip6_tcp* pkt6 = alloca(size);
714 memset(pkt6, 0, size);
716 GNUNET_assert(pkt6 != NULL);
718 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK)
719 new_ip6addr(pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
721 new_ip6addr_remote(pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
723 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
724 pkt6->shdr.size = htons(size);
727 pkt6->tun.type = htons(0x86dd);
729 pkt6->ip6_hdr.version = 6;
730 pkt6->ip6_hdr.tclass_h = 0;
731 pkt6->ip6_hdr.tclass_l = 0;
732 pkt6->ip6_hdr.flowlbl = 0;
733 pkt6->ip6_hdr.paylgth = htons(pktlen);
734 pkt6->ip6_hdr.nxthdr = 0x06;
735 pkt6->ip6_hdr.hoplmt = 0xff;
739 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
740 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
741 GNUNET_free(ipv6addr);
743 memcpy(&pkt6->tcp_hdr, pkt, pktlen);
745 GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
746 GNUNET_assert (key != NULL);
748 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
749 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
750 GNUNET_TIME_absolute_get ().abs_value);
754 GNUNET_assert (me != NULL);
755 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
756 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_TCP));
758 pkt6->tcp_hdr.crc = 0;
762 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
764 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
766 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
767 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
768 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
771 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
772 ntohs (pkt6->ip6_hdr.paylgth));
773 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
775 write_to_helper(pkt6, size);
781 * Main function that will be run by the scheduler.
784 * @param args remaining command-line arguments
785 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
786 * @param cfg_ configuration
790 char *const *args __attribute__((unused)),
791 const char *cfgfilep __attribute__((unused)),
792 const struct GNUNET_CONFIGURATION_Handle *cfg_)
794 static const struct GNUNET_MESH_MessageHandler handlers[] = {
795 {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
796 {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
797 {receive_udp_back, GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK, 0},
798 {receive_tcp_back, GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK, 0},
802 static const GNUNET_MESH_ApplicationType types[] = {
803 GNUNET_APPLICATION_TYPE_END
806 mesh_handle = GNUNET_MESH_connect(cfg_,
813 hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
814 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
815 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
817 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
818 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
819 shs_task = GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
820 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
824 * The main function to obtain template from gnunetd.
826 * @param argc number of arguments from the command line
827 * @param argv command line arguments
828 * @return 0 ok, 1 on error
831 main (int argc, char *const *argv) {
832 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
833 GNUNET_GETOPT_OPTION_END
837 GNUNET_PROGRAM_run (argc,
840 gettext_noop ("help text"),
841 options, &run, NULL)) ? ret : 1;
844 /* end of gnunet-daemon-vpn.c */