2 This file is part of GNUnet.
3 (C) 2011-2013 Christian Grothoff (and other contributing authors)
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 gns/gnunet-service-gns_shorten.c
23 * @brief GNUnet GNS shortening logic
24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_gns_service.h"
35 #include "gnunet-service-gns_shorten.h"
36 #include "gnunet_vpn_service.h"
40 * Default DHT timeout for lookups.
42 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45 * DHT replication level
47 #define DHT_GNS_REPLICATION_LEVEL 5
51 * Handle for a PSEU lookup used to shorten names.
53 struct GetPseuAuthorityHandle
58 struct GetPseuAuthorityHandle *next;
63 struct GetPseuAuthorityHandle *prev;
66 * Private key of the (shorten) zone to store the resulting
69 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_zone_key;
72 * Original label (used if no PSEU record is found).
74 char label[GNUNET_DNSPARSER_MAX_LABEL_LENGTH + 1];
77 * Suggested label based on NICK record
79 char * suggested_label;
82 * Label we are currently trying out (during #perform_pseu_lookup).
87 * The zone for which we are trying to find the PSEU record.
89 struct GNUNET_CRYPTO_EcdsaPublicKey target_zone;
92 * Handle for DHT lookups. Should be NULL if no lookups are in progress
94 struct GNUNET_DHT_GetHandle *get_handle;
97 * Handle to namestore request
99 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
102 * Handle to namecache request
104 struct GNUNET_NAMECACHE_QueueEntry *namecache_task;
107 * Task to abort DHT lookup operation.
109 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
115 * Head of PSEU/shorten operations list.
117 static struct GetPseuAuthorityHandle *gph_head;
120 * Tail of PSEU/shorten operations list.
122 static struct GetPseuAuthorityHandle *gph_tail;
125 * Our handle to the namestore service
127 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
130 * Our handle to the namecache service
132 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
135 * Resolver handle to the dht
137 static struct GNUNET_DHT_Handle *dht_handle;
140 * Cleanup a 'struct GetPseuAuthorityHandle', terminating all
141 * pending activities.
143 * @param gph handle to terminate
146 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
148 if (NULL != gph->get_handle)
150 GNUNET_DHT_get_stop (gph->get_handle);
151 gph->get_handle = NULL;
153 if (NULL != gph->namestore_task)
155 GNUNET_NAMESTORE_cancel (gph->namestore_task);
156 gph->namestore_task = NULL;
158 if (NULL != gph->namecache_task)
160 GNUNET_NAMECACHE_cancel (gph->namecache_task);
161 gph->namecache_task = NULL;
163 if (GNUNET_SCHEDULER_NO_TASK != gph->timeout_task)
165 GNUNET_SCHEDULER_cancel (gph->timeout_task);
166 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
168 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
169 GNUNET_free_non_null (gph->current_label);
175 * Continuation for pkey record creation (shorten)
177 * @param cls a GetPseuAuthorityHandle
178 * @param success unused
182 create_pkey_cont (void* cls,
186 struct GetPseuAuthorityHandle* gph = cls;
188 gph->namestore_task = NULL;
189 free_get_pseu_authority_handle (gph);
194 * Namestore calls this function if we have record for this name.
195 * (or with rd_count=0 to indicate no matches).
197 * @param cls the pending query
198 * @param rd_count the number of records with 'name'
199 * @param rd the record data
202 process_pseu_lookup_ns (void *cls,
203 unsigned int rd_count,
204 const struct GNUNET_GNSRECORD_Data *rd);
208 * We obtained a result for our query to the shorten zone from
209 * the namestore. Try to decrypt.
211 * @param cls the handle to our shorten operation
212 * @param block resulting encrypted block
215 process_pseu_block_ns (void *cls,
216 const struct GNUNET_GNSRECORD_Block *block)
218 struct GetPseuAuthorityHandle *gph = cls;
219 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
221 gph->namecache_task = NULL;
224 process_pseu_lookup_ns (gph, 0, NULL);
227 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
230 GNUNET_GNSRECORD_block_decrypt (block,
233 &process_pseu_lookup_ns,
237 free_get_pseu_authority_handle (gph);
244 * Lookup in the namecache for the shorten zone the given label.
246 * @param gph the handle to our shorten operation
247 * @param label the label to lookup
250 perform_pseu_lookup (struct GetPseuAuthorityHandle *gph,
253 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
254 struct GNUNET_HashCode query;
256 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
258 GNUNET_free_non_null (gph->current_label);
259 gph->current_label = GNUNET_strdup (label);
260 GNUNET_GNSRECORD_query_from_public_key (&pub,
263 gph->namecache_task = GNUNET_NAMECACHE_lookup_block (namecache_handle,
265 &process_pseu_block_ns,
271 * Namestore calls this function if we have record for this name.
272 * (or with rd_count=0 to indicate no matches).
274 * @param cls the pending query
275 * @param rd_count the number of records with 'name'
276 * @param rd the record data
279 process_pseu_lookup_ns (void *cls,
280 unsigned int rd_count,
281 const struct GNUNET_GNSRECORD_Data *rd)
283 struct GetPseuAuthorityHandle *gph = cls;
284 struct GNUNET_GNSRECORD_Data new_pkey;
286 gph->namestore_task = NULL;
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Name `%s' already taken, cannot shorten.\n",
292 /* if this was not yet the original label, try one more
293 time, this time not using PSEU but the original label */
294 if (0 == strcmp (gph->current_label,
297 free_get_pseu_authority_handle (gph);
301 perform_pseu_lookup (gph, gph->label);
305 /* name is available */
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Shortening `%s' to `%s'\n",
308 GNUNET_GNSRECORD_z2s (&gph->target_zone),
310 new_pkey.expiration_time = UINT64_MAX;
311 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
312 new_pkey.data = &gph->target_zone;
313 new_pkey.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
314 new_pkey.flags = GNUNET_GNSRECORD_RF_NONE
315 | GNUNET_GNSRECORD_RF_PRIVATE;
317 = GNUNET_NAMESTORE_records_store (namestore_handle,
318 &gph->shorten_zone_key,
321 &create_pkey_cont, gph);
326 * Process result of a DHT lookup for a PSEU record.
328 * @param gph the handle to our shorten operation
329 * @param pseu the pseu result or NULL
332 process_pseu_result (struct GetPseuAuthorityHandle* gph,
337 /* no PSEU found, try original label */
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "No PSEU found, trying original label `%s' instead.\n",
341 perform_pseu_lookup (gph, gph->label);
344 /* check if 'pseu' is taken */
345 perform_pseu_lookup (gph, pseu);
350 * Handle timeout for DHT request during shortening.
352 * @param cls the request handle as closure
353 * @param tc the task context
356 handle_auth_discovery_timeout (void *cls,
357 const struct GNUNET_SCHEDULER_TaskContext *tc)
359 struct GetPseuAuthorityHandle *gph = cls;
361 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "DHT lookup for PSEU query in zone `%s' timed out.\n",
364 GNUNET_GNSRECORD_z2s (&gph->target_zone));
365 GNUNET_DHT_get_stop (gph->get_handle);
366 gph->get_handle = NULL;
367 process_pseu_result (gph, NULL);
372 * Handle decrypted records from DHT result.
374 * @param cls closure with our 'struct GetPseuAuthorityHandle'
375 * @param rd_count number of entries in 'rd' array
376 * @param rd array of records with data to store
379 process_auth_records (void *cls,
380 unsigned int rd_count,
381 const struct GNUNET_GNSRECORD_Data *rd)
383 struct GetPseuAuthorityHandle *gph = cls;
386 for (i=0; i < rd_count; i++)
388 if (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type)
390 char pseu[rd[i].data_size + 1];
396 pseu[rd[i].data_size] = '\0';
397 process_pseu_result (gph,
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "No PSEU record found in DHT reply with %u records.\n",
405 process_pseu_result (gph, NULL);
410 * Function called when we find a PSEU entry in the DHT
412 * @param cls the request handle
413 * @param exp lifetime
414 * @param key the key the record was stored under
415 * @param get_path get path
416 * @param get_path_length @a get_path length
417 * @param put_path put path
418 * @param put_path_length @a put_path length
419 * @param type the block type
420 * @param size number of bytes in @a data
421 * @param data the record data
424 process_auth_discovery_dht_result (void* cls,
425 struct GNUNET_TIME_Absolute exp,
426 const struct GNUNET_HashCode *key,
427 const struct GNUNET_PeerIdentity *get_path,
428 unsigned int get_path_length,
429 const struct GNUNET_PeerIdentity *put_path,
430 unsigned int put_path_length,
431 enum GNUNET_BLOCK_Type type,
435 struct GetPseuAuthorityHandle *gph = cls;
436 const struct GNUNET_GNSRECORD_Block *block;
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Got DHT result for PSEU request\n");
440 GNUNET_DHT_get_stop (gph->get_handle);
441 gph->get_handle = NULL;
442 GNUNET_SCHEDULER_cancel (gph->timeout_task);
443 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
447 /* is this allowed!? */
449 process_pseu_result (gph, NULL);
452 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
454 /* how did this pass DHT block validation!? */
456 process_pseu_result (gph, NULL);
461 ntohl (block->purpose.size) +
462 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
463 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
465 /* how did this pass DHT block validation!? */
467 process_pseu_result (gph, NULL);
471 GNUNET_GNSRECORD_block_decrypt (block,
474 &process_auth_records,
477 /* other peer encrypted invalid block, complain */
479 process_pseu_result (gph, NULL);
484 static void suggested_lookup_cb (void *cls,
485 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
487 unsigned int rd_count,
488 const struct GNUNET_GNSRECORD_Data *rd)
490 struct GetPseuAuthorityHandle* gph = cls;
491 gph->namestore_task = NULL;
492 if ((0 == strcmp (label, gph->suggested_label)) && (0 == rd_count) && (NULL == rd))
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Shortening to suggested name `%s' possible\n",
497 gph->suggested_label);
498 process_pseu_result (gph, gph->suggested_label);
505 * Callback called by namestore for a zone to name result. We're
506 * trying to see if a short name for a given zone already exists.
508 * @param cls the closure
509 * @param zone_key the zone we queried
510 * @param name the name found or NULL
511 * @param rd_len number of records for the name
512 * @param rd the record data (PKEY) for the name
515 process_zone_to_name_discover (void *cls,
516 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
519 const struct GNUNET_GNSRECORD_Data *rd)
521 struct GetPseuAuthorityHandle* gph = cls;
523 struct GNUNET_HashCode lookup_key;
526 gph->namestore_task = NULL;
529 /* we found a match in our own zone */
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Shortening aborted, name `%s' already reserved for the zone\n",
533 free_get_pseu_authority_handle (gph);
536 /* record does not yet exist, check if suggested label is available */
538 if (NULL != gph->suggested_label)
539 gph->namestore_task = GNUNET_NAMESTORE_records_lookup (namestore_handle, zone_key,
540 gph->suggested_label, &suggested_lookup_cb, gph);
543 GNUNET_GNSRECORD_query_from_public_key (&gph->target_zone,
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Shortening searches in DHT for PSEU record under `%s' in zone `%s'\n",
548 GNUNET_h2s (&lookup_key),
549 GNUNET_GNSRECORD_z2s (&gph->target_zone));
551 gph->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
552 &handle_auth_discovery_timeout,
554 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
555 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
557 DHT_GNS_REPLICATION_LEVEL,
558 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
560 &process_auth_discovery_dht_result,
567 * Start shortening algorithm, try to allocate a nice short
568 * canonical name for @a pub in @a shorten_zone, using
569 * @a original_label as one possible suggestion.
571 * @param original_label original label for the zone
572 * @param suggested_label suggested label for the zone
573 * @param pub public key of the zone to shorten
574 * @param shorten_zone private key of the target zone for the new record
577 GNS_shorten_start (const char *original_label,
578 const char *suggested_label,
579 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
580 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone)
582 struct GetPseuAuthorityHandle *gph;
584 if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Starting shortening process for `%s' with old label `%s'\n",
591 GNUNET_GNSRECORD_z2s (pub),
593 gph = GNUNET_new (struct GetPseuAuthorityHandle);
594 gph->shorten_zone_key = *shorten_zone;
595 gph->target_zone = *pub;
596 gph->suggested_label = GNUNET_strdup (suggested_label);
597 strcpy (gph->label, original_label);
598 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
599 /* first, check if we *already* have a record for this zone */
600 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
603 &process_zone_to_name_discover,
609 * Initialize the shortening subsystem
611 * @param nh the namestore handle
612 * @param nc the namecache handle
613 * @param dht the dht handle
616 GNS_shorten_init (struct GNUNET_NAMESTORE_Handle *nh,
617 struct GNUNET_NAMECACHE_Handle *nc,
618 struct GNUNET_DHT_Handle *dht)
620 namestore_handle = nh;
621 namecache_handle = nc;
627 * Shutdown shortening.
632 /* abort active shorten operations */
633 while (NULL != gph_head)
634 free_get_pseu_authority_handle (gph_head);
636 namestore_handle = NULL;
637 namecache_handle = NULL;
640 /* end of gnunet-service-gns_shorten.c */