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-vpn-pretty-print.h"
31 #include "gnunet_common.h"
32 #include <gnunet_os_lib.h>
33 #include "gnunet_protocols.h"
34 #include <gnunet_mesh_service.h>
35 #include "gnunet_client_lib.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_constants.h"
38 #include <block_dns.h>
39 #include "gnunet-daemon-vpn-helper.h"
40 #include "gnunet-daemon-vpn-dns.h"
42 #include "gnunet-daemon-vpn.h"
50 * This hashmap contains the mapping from peer, service-descriptor,
51 * source-port and destination-port to a socket
53 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
56 * Function scheduled as very last function, cleans up after us
60 cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
61 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
64 if (helper_proc != NULL)
66 if (0 != GNUNET_OS_process_kill (helper_proc, SIGTERM))
67 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
68 GNUNET_OS_process_wait (helper_proc);
69 GNUNET_OS_process_close (helper_proc);
73 /* close the connection to the service-dns */
74 if (dns_connection != NULL)
76 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
77 dns_connection = NULL;
80 if (mesh_handle != NULL)
82 GNUNET_MESH_disconnect(mesh_handle);
88 static uint32_t calculate_checksum_update(uint32_t sum, uint16_t *hdr, short len) {
89 for(; len >= 2; len -= 2)
92 sum += *((unsigned char*)hdr);
96 static uint16_t calculate_checksum_end(uint32_t sum) {
98 sum = (sum >> 16) + (sum & 0xFFFF);
104 * Calculate the checksum of an IPv4-Header
107 calculate_ip_checksum(uint16_t* hdr, short len) {
108 uint32_t sum = calculate_checksum_update(0, hdr, len);
109 return calculate_checksum_end(sum);
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 send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
135 struct ip6_icmp* request = cls;
137 struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
138 GNUNET_assert(response != NULL);
139 memset(response, 0, ntohs(request->shdr.size));
141 response->shdr.size = request->shdr.size;
142 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
144 response->tun.flags = 0;
145 response->tun.type = htons(0x86dd);
147 response->ip6_hdr.hoplmt = 255;
148 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
149 response->ip6_hdr.nxthdr = 0x3a;
150 response->ip6_hdr.version = 6;
151 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
152 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
154 response->icmp_hdr.code = 0;
155 response->icmp_hdr.type = 0x81;
157 /* Magic, more Magic! */
158 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
160 /* Copy the rest of the packet */
161 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
163 write_to_helper(response, ntohs(response->shdr.size));
165 GNUNET_free(request);
169 * cls is the pointer to a GNUNET_MessageHeader that is
170 * followed by the service-descriptor and the udp-packet that should be sent;
173 send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
175 struct GNUNET_MESH_Tunnel **tunnel = cls;
176 struct GNUNET_MessageHeader *hdr =
177 (struct GNUNET_MessageHeader *) (tunnel + 1);
178 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
179 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
180 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
181 sizeof (GNUNET_HashCode) + ntohs (udp->len));
182 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_udp_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
184 GNUNET_assert (size >= ntohs (hdr->size));
185 memcpy (buf, hdr, ntohs (hdr->size));
186 size = ntohs(hdr->size);
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
193 port_in_ports (uint64_t ports, uint16_t port)
195 uint16_t *ps = (uint16_t *) & ports;
196 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
200 send_udp_to_peer (void *cls,
201 const struct GNUNET_PeerIdentity *peer,
202 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
204 if (peer == NULL) return;
205 struct GNUNET_MESH_Tunnel **tunnel = cls;
206 struct GNUNET_MessageHeader *hdr =
207 (struct GNUNET_MessageHeader *) (tunnel + 1);
208 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
209 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
210 GNUNET_MESH_notify_transmit_ready (*tunnel,
213 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
215 (struct GNUNET_MessageHeader) +
216 sizeof (GNUNET_HashCode) +
218 send_udp_to_peer_notify_callback,
223 * Create a new Address from an answer-packet
226 new_ip6addr(char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
227 memcpy(buf+14, (int[]){htons(0x3412)}, 2);
228 memcpy(buf+8, service_desc, 6);
229 memcpy(buf, peer, 8);
234 * This gets scheduled with cls pointing to an answer_packet and does everything
235 * needed in order to send it to the helper.
237 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
238 * doing nothing for "real" services.
241 process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
242 struct answer_packet* pkt = cls;
243 struct answer_packet_list* list;
245 /* This answer is about a .gnunet-service
247 * It contains an almost complete DNS-Response, we have to fill in the ip
248 * at the offset pkt->addroffset
251 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
253 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
256 memset(&key, 0, sizeof(GNUNET_HashCode));
257 new_ip6addr((char*)&key, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
259 uint16_t namelen = strlen((char*)pkt->data+12)+1;
261 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
262 char* name = (char*)(value +1);
264 value->namelen = namelen;
265 memcpy(name, pkt->data+12, namelen);
267 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
269 value->additional_ports = 0;
271 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(hashmap,
274 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
276 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not store to hashmap\n");
280 * Copy the newly generated backward ip-address to the packet
282 char* c = ((char*)pkt)+ntohs(pkt->addroffset);
283 char* k = (char*)&key;
285 for (i = 0; i < 16; i++)
288 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
290 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
293 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
296 memset(&key, 0, sizeof key);
297 unsigned char* k = (unsigned char*)&key;
298 unsigned char* s = pkt->data+12;
300 /* Whoever designed the reverse IPv6-lookup is batshit insane */
301 for (i = 0; i < 16; i++)
303 unsigned char c1 = s[(4*i)+1];
304 unsigned char c2 = s[(4*i)+3];
308 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
310 k[i] += 16*(c2 - '0');
312 k[i] += 16*(c2 - 87);
315 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
316 unsigned short offset = ntohs(pkt->addroffset);
318 if (map_entry == NULL)
324 unsigned short namelen = htons(map_entry->namelen);
325 char* name = (char*)(map_entry + 1);
327 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
329 struct answer_packet* rpkt = &list->pkt;
331 /* The offset points to the first byte belonging to the address */
332 memcpy(rpkt, pkt, offset - 1);
334 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
335 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
337 memcpy(((char*)rpkt)+offset, &namelen, 2);
338 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
341 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
343 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
344 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
355 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
357 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
363 add_additional_port (struct map_entry *me, uint16_t port)
365 uint16_t *ps = (uint16_t *) & me->additional_ports;
367 for (i = 0; i < 4; i++)
378 receive_udp_back (void *cls, struct GNUNET_MESH_Tunnel* tunnel,
380 const struct GNUNET_MessageHeader *message,
381 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
383 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
384 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
386 const struct GNUNET_PeerIdentity* other = GNUNET_MESH_get_peer(tunnel);
388 new_ip6addr(addr, &other->hashPubKey, desc);
390 size_t size = sizeof(struct ip6_udp) + ntohs(pkt->len) - 1 - sizeof(struct udp_pkt);
392 struct ip6_udp* pkt6 = alloca(size);
394 GNUNET_assert(pkt6 != NULL);
396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, ntohs(message->size), ntohs(pkt->len));
398 pkt6->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
399 pkt6->shdr.size = htons(size);
402 pkt6->tun.type = htons(0x86dd);
404 pkt6->ip6_hdr.version = 6;
405 pkt6->ip6_hdr.tclass_h = 0;
406 pkt6->ip6_hdr.tclass_l = 0;
407 pkt6->ip6_hdr.flowlbl = 0;
408 pkt6->ip6_hdr.paylgth = pkt->len;
409 pkt6->ip6_hdr.nxthdr = 0x11;
410 pkt6->ip6_hdr.hoplmt = 0xff;
413 for (i = 0; i < 16; i++)
414 pkt6->ip6_hdr.sadr[15-i] = addr[i];
416 memcpy(pkt6->ip6_hdr.dadr, (unsigned char[]){0x12, 0x34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 16);
418 memcpy(&pkt6->udp_hdr, pkt, ntohs(pkt->len));
420 GNUNET_HashCode* key = address_mapping_exists(pkt6->ip6_hdr.sadr);
421 GNUNET_assert (key != NULL);
423 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get(hashmap, key);
427 GNUNET_assert (me != NULL);
428 GNUNET_assert (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP));
429 if (!port_in_ports(me->desc.ports, pkt6->udp_hdr.spt) ||
430 !port_in_ports(me->additional_ports, pkt6->udp_hdr.spt)) {
431 add_additional_port(me, pkt6->udp_hdr.spt);
434 pkt6->udp_hdr.crc = 0;
436 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.sadr, 16);
437 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->ip6_hdr.dadr, 16);
438 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
439 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
440 tmp = htons(((pkt6->ip6_hdr.nxthdr & 0x00ff)));
441 sum = calculate_checksum_update(sum, (uint16_t*)&tmp, 4);
443 sum = calculate_checksum_update(sum, (uint16_t*)&pkt6->udp_hdr, ntohs(pkt->len));
444 pkt6->udp_hdr.crc = calculate_checksum_end(sum);
446 write_to_helper(pkt6, size);
451 void init_mesh (void* cls, struct GNUNET_MESH_Handle* server, const struct GNUNET_PeerIdentity* my_identity, const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey) {
452 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to MESH, I am %x\n", *((unsigned long*)my_identity));
455 void connect_mesh (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi) {
456 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %x\n", *((unsigned long*)peer));
460 * Main function that will be run by the scheduler.
463 * @param args remaining command-line arguments
464 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
465 * @param cfg_ configuration
471 const struct GNUNET_CONFIGURATION_Handle *cfg_)
473 const static struct GNUNET_MESH_MessageHandler handlers[] = {
474 {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
477 mesh_handle = GNUNET_MESH_connect(cfg_,
481 mst = GNUNET_SERVER_mst_create(&message_token, NULL);
484 hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
485 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
486 GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
487 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
488 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
492 * The main function to obtain template from gnunetd.
494 * @param argc number of arguments from the command line
495 * @param argv command line arguments
496 * @return 0 ok, 1 on error
499 main (int argc, char *const *argv) {
500 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
501 GNUNET_GETOPT_OPTION_END
505 GNUNET_PROGRAM_run (argc,
508 gettext_noop ("help text"),
509 options, &run, NULL)) ? ret : 1;
512 /* end of gnunet-daemon-vpn.c */