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 * Label we are currently trying out (during #perform_pseu_lookup).
82 * The zone for which we are trying to find the PSEU record.
84 struct GNUNET_CRYPTO_EcdsaPublicKey target_zone;
87 * Handle for DHT lookups. Should be NULL if no lookups are in progress
89 struct GNUNET_DHT_GetHandle *get_handle;
92 * Handle to namestore request
94 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
97 * Handle to namecache request
99 struct GNUNET_NAMECACHE_QueueEntry *namecache_task;
102 * Task to abort DHT lookup operation.
104 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
110 * Head of PSEU/shorten operations list.
112 static struct GetPseuAuthorityHandle *gph_head;
115 * Tail of PSEU/shorten operations list.
117 static struct GetPseuAuthorityHandle *gph_tail;
120 * Our handle to the namestore service
122 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
125 * Our handle to the namecache service
127 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
130 * Resolver handle to the dht
132 static struct GNUNET_DHT_Handle *dht_handle;
135 * Cleanup a 'struct GetPseuAuthorityHandle', terminating all
136 * pending activities.
138 * @param gph handle to terminate
141 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
143 if (NULL != gph->get_handle)
145 GNUNET_DHT_get_stop (gph->get_handle);
146 gph->get_handle = NULL;
148 if (NULL != gph->namestore_task)
150 GNUNET_NAMESTORE_cancel (gph->namestore_task);
151 gph->namestore_task = NULL;
153 if (NULL != gph->namecache_task)
155 GNUNET_NAMECACHE_cancel (gph->namecache_task);
156 gph->namecache_task = NULL;
158 if (GNUNET_SCHEDULER_NO_TASK != gph->timeout_task)
160 GNUNET_SCHEDULER_cancel (gph->timeout_task);
161 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
163 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
164 GNUNET_free_non_null (gph->current_label);
170 * Continuation for pkey record creation (shorten)
172 * @param cls a GetPseuAuthorityHandle
173 * @param success unused
177 create_pkey_cont (void* cls,
181 struct GetPseuAuthorityHandle* gph = cls;
183 gph->namestore_task = NULL;
184 free_get_pseu_authority_handle (gph);
189 * Namestore calls this function if we have record for this name.
190 * (or with rd_count=0 to indicate no matches).
192 * @param cls the pending query
193 * @param rd_count the number of records with 'name'
194 * @param rd the record data
197 process_pseu_lookup_ns (void *cls,
198 unsigned int rd_count,
199 const struct GNUNET_GNSRECORD_Data *rd);
203 * We obtained a result for our query to the shorten zone from
204 * the namestore. Try to decrypt.
206 * @param cls the handle to our shorten operation
207 * @param block resulting encrypted block
210 process_pseu_block_ns (void *cls,
211 const struct GNUNET_GNSRECORD_Block *block)
213 struct GetPseuAuthorityHandle *gph = cls;
214 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
216 gph->namecache_task = NULL;
219 process_pseu_lookup_ns (gph, 0, NULL);
222 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
225 GNUNET_GNSRECORD_block_decrypt (block,
228 &process_pseu_lookup_ns,
232 free_get_pseu_authority_handle (gph);
239 * Lookup in the namestore for the shorten zone the given label.
241 * @param gph the handle to our shorten operation
242 * @param label the label to lookup
245 perform_pseu_lookup (struct GetPseuAuthorityHandle *gph,
248 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
249 struct GNUNET_HashCode query;
251 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
253 GNUNET_free_non_null (gph->current_label);
254 gph->current_label = GNUNET_strdup (label);
255 GNUNET_GNSRECORD_query_from_public_key (&pub,
258 gph->namecache_task = GNUNET_NAMECACHE_lookup_block (namecache_handle,
260 &process_pseu_block_ns,
266 * Namestore calls this function if we have record for this name.
267 * (or with rd_count=0 to indicate no matches).
269 * @param cls the pending query
270 * @param rd_count the number of records with 'name'
271 * @param rd the record data
274 process_pseu_lookup_ns (void *cls,
275 unsigned int rd_count,
276 const struct GNUNET_GNSRECORD_Data *rd)
278 struct GetPseuAuthorityHandle *gph = cls;
279 struct GNUNET_GNSRECORD_Data new_pkey;
281 gph->namestore_task = NULL;
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Name `%s' already taken, cannot shorten.\n",
287 /* if this was not yet the original label, try one more
288 time, this time not using PSEU but the original label */
289 if (0 == strcmp (gph->current_label,
292 free_get_pseu_authority_handle (gph);
296 perform_pseu_lookup (gph, gph->label);
300 /* name is available */
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Shortening `%s' to `%s'\n",
303 GNUNET_GNSRECORD_z2s (&gph->target_zone),
305 new_pkey.expiration_time = UINT64_MAX;
306 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
307 new_pkey.data = &gph->target_zone;
308 new_pkey.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
309 new_pkey.flags = GNUNET_GNSRECORD_RF_NONE
310 | GNUNET_GNSRECORD_RF_PRIVATE;
312 = GNUNET_NAMESTORE_records_store (namestore_handle,
313 &gph->shorten_zone_key,
316 &create_pkey_cont, gph);
321 * Process result of a DHT lookup for a PSEU record.
323 * @param gph the handle to our shorten operation
324 * @param pseu the pseu result or NULL
327 process_pseu_result (struct GetPseuAuthorityHandle* gph,
332 /* no PSEU found, try original label */
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "No PSEU found, trying original label `%s' instead.\n",
336 perform_pseu_lookup (gph, gph->label);
339 /* check if 'pseu' is taken */
340 perform_pseu_lookup (gph, pseu);
345 * Handle timeout for DHT request during shortening.
347 * @param cls the request handle as closure
348 * @param tc the task context
351 handle_auth_discovery_timeout (void *cls,
352 const struct GNUNET_SCHEDULER_TaskContext *tc)
354 struct GetPseuAuthorityHandle *gph = cls;
356 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "DHT lookup for PSEU query in zone `%s' timed out.\n",
359 GNUNET_GNSRECORD_z2s (&gph->target_zone));
360 GNUNET_DHT_get_stop (gph->get_handle);
361 gph->get_handle = NULL;
362 process_pseu_result (gph, NULL);
367 * Handle decrypted records from DHT result.
369 * @param cls closure with our 'struct GetPseuAuthorityHandle'
370 * @param rd_count number of entries in 'rd' array
371 * @param rd array of records with data to store
374 process_auth_records (void *cls,
375 unsigned int rd_count,
376 const struct GNUNET_GNSRECORD_Data *rd)
378 struct GetPseuAuthorityHandle *gph = cls;
381 for (i=0; i < rd_count; i++)
383 if (GNUNET_GNSRECORD_TYPE_PSEU == rd[i].record_type)
385 char pseu[rd[i].data_size + 1];
391 pseu[rd[i].data_size] = '\0';
392 process_pseu_result (gph,
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "No PSEU record found in DHT reply with %u records.\n",
400 process_pseu_result (gph, NULL);
405 * Function called when we find a PSEU entry in the DHT
407 * @param cls the request handle
408 * @param exp lifetime
409 * @param key the key the record was stored under
410 * @param get_path get path
411 * @param get_path_length @a get_path length
412 * @param put_path put path
413 * @param put_path_length @a put_path length
414 * @param type the block type
415 * @param size number of bytes in @a data
416 * @param data the record data
419 process_auth_discovery_dht_result (void* cls,
420 struct GNUNET_TIME_Absolute exp,
421 const struct GNUNET_HashCode *key,
422 const struct GNUNET_PeerIdentity *get_path,
423 unsigned int get_path_length,
424 const struct GNUNET_PeerIdentity *put_path,
425 unsigned int put_path_length,
426 enum GNUNET_BLOCK_Type type,
430 struct GetPseuAuthorityHandle *gph = cls;
431 const struct GNUNET_GNSRECORD_Block *block;
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Got DHT result for PSEU request\n");
435 GNUNET_DHT_get_stop (gph->get_handle);
436 gph->get_handle = NULL;
437 GNUNET_SCHEDULER_cancel (gph->timeout_task);
438 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
442 /* is this allowed!? */
444 process_pseu_result (gph, NULL);
447 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
449 /* how did this pass DHT block validation!? */
451 process_pseu_result (gph, NULL);
456 ntohl (block->purpose.size) +
457 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
458 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
460 /* how did this pass DHT block validation!? */
462 process_pseu_result (gph, NULL);
466 GNUNET_GNSRECORD_block_decrypt (block,
469 &process_auth_records,
472 /* other peer encrypted invalid block, complain */
474 process_pseu_result (gph, NULL);
481 * Callback called by namestore for a zone to name result. We're
482 * trying to see if a short name for a given zone already exists.
484 * @param cls the closure
485 * @param zone_key the zone we queried
486 * @param name the name found or NULL
487 * @param rd_len number of records for the name
488 * @param rd the record data (PKEY) for the name
491 process_zone_to_name_discover (void *cls,
492 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
495 const struct GNUNET_GNSRECORD_Data *rd)
497 struct GetPseuAuthorityHandle* gph = cls;
498 struct GNUNET_HashCode lookup_key;
500 gph->namestore_task = NULL;
503 /* we found a match in our own zone */
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Shortening aborted, name `%s' already reserved for the zone\n",
507 free_get_pseu_authority_handle (gph);
510 /* record does not yet exist, go into DHT to find PSEU record */
511 GNUNET_GNSRECORD_query_from_public_key (&gph->target_zone,
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Shortening searches in DHT for PSEU record under `%s' in zone `%s'\n",
516 GNUNET_h2s (&lookup_key),
517 GNUNET_GNSRECORD_z2s (&gph->target_zone));
519 gph->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
520 &handle_auth_discovery_timeout,
522 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
523 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
525 DHT_GNS_REPLICATION_LEVEL,
526 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
528 &process_auth_discovery_dht_result,
534 * Start shortening algorithm, try to allocate a nice short
535 * canonical name for @a pub in @a shorten_zone, using
536 * @a original_label as one possible suggestion.
538 * @param original_label original label for the zone
539 * @param pub public key of the zone to shorten
540 * @param shorten_zone private key of the target zone for the new record
543 GNS_shorten_start (const char *original_label,
544 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
545 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone)
547 struct GetPseuAuthorityHandle *gph;
549 if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Starting shortening process for `%s' with old label `%s'\n",
556 GNUNET_GNSRECORD_z2s (pub),
558 gph = GNUNET_new (struct GetPseuAuthorityHandle);
559 gph->shorten_zone_key = *shorten_zone;
560 gph->target_zone = *pub;
561 strcpy (gph->label, original_label);
562 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
563 /* first, check if we *already* have a record for this zone */
564 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
567 &process_zone_to_name_discover,
573 * Initialize the shortening subsystem
575 * @param nh the namestore handle
576 * @param nc the namecache handle
577 * @param dht the dht handle
580 GNS_shorten_init (struct GNUNET_NAMESTORE_Handle *nh,
581 struct GNUNET_NAMECACHE_Handle *nc,
582 struct GNUNET_DHT_Handle *dht)
584 namestore_handle = nh;
585 namecache_handle = nc;
591 * Shutdown shortening.
596 /* abort active shorten operations */
597 while (NULL != gph_head)
598 free_get_pseu_authority_handle (gph_head);
600 namestore_handle = NULL;
601 namecache_handle = NULL;
604 /* end of gnunet-service-gns_shorten.c */