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
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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Namecache did not return information for label `%s' \n",
227 process_pseu_lookup_ns (gph, 0, NULL);
230 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
233 GNUNET_GNSRECORD_block_decrypt (block,
236 &process_pseu_lookup_ns,
240 free_get_pseu_authority_handle (gph);
247 * Lookup in the namecache for the shorten zone the given label.
249 * @param gph the handle to our shorten operation
250 * @param label the label to lookup
253 perform_nick_lookup (struct GetPseuAuthorityHandle *gph,
256 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
257 struct GNUNET_HashCode query;
259 GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
261 GNUNET_free_non_null (gph->current_label);
262 gph->current_label = GNUNET_strdup (label);
263 GNUNET_GNSRECORD_query_from_public_key (&pub,
266 gph->namecache_task = GNUNET_NAMECACHE_lookup_block (namecache_handle,
268 &process_pseu_block_ns,
274 * Namestore calls this function if we have record for this name.
275 * (or with rd_count=0 to indicate no matches).
277 * @param cls the pending query
278 * @param rd_count the number of records with 'name'
279 * @param rd the record data
282 process_pseu_lookup_ns (void *cls,
283 unsigned int rd_count,
284 const struct GNUNET_GNSRECORD_Data *rd)
286 struct GetPseuAuthorityHandle *gph = cls;
287 struct GNUNET_GNSRECORD_Data new_pkey;
289 gph->namestore_task = NULL;
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Name `%s' already taken, cannot shorten.\n",
295 /* if this was not yet the original label, try one more
296 time, this time not using PSEU but the original label */
297 if (0 == strcmp (gph->current_label,
300 free_get_pseu_authority_handle (gph);
304 perform_nick_lookup (gph, gph->label);
308 /* name is available */
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Shortening `%s' to `%s'\n",
311 GNUNET_GNSRECORD_z2s (&gph->target_zone),
313 new_pkey.expiration_time = UINT64_MAX;
314 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
315 new_pkey.data = &gph->target_zone;
316 new_pkey.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
317 new_pkey.flags = GNUNET_GNSRECORD_RF_NONE
318 | GNUNET_GNSRECORD_RF_PRIVATE;
320 = GNUNET_NAMESTORE_records_store (namestore_handle,
321 &gph->shorten_zone_key,
324 &create_pkey_cont, gph);
329 * Callback called by namestore for a zone to name result. We're
330 * trying to see if a short name for a given zone already exists.
332 * @param cls the closure
333 * @param zone_key the zone we queried
334 * @param name the name found or NULL
335 * @param rd_len number of records for the name
336 * @param rd the record data (PKEY) for the name
339 process_zone_to_name_discover (void *cls,
340 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
343 const struct GNUNET_GNSRECORD_Data *rd)
345 struct GetPseuAuthorityHandle* gph = cls;
347 struct GNUNET_HashCode lookup_key;
350 gph->namestore_task = NULL;
353 /* we found a match in our own zone */
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Shortening aborted, name `%s' already reserved for the zone\n",
357 free_get_pseu_authority_handle (gph);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Shortening continuing, no name not reserved in shorten zone\n");
365 /* record does not yet exist, check if suggested label is available */
366 perform_nick_lookup (gph, gph->suggested_label);
371 * Start shortening algorithm, try to allocate a nice short
372 * canonical name for @a pub in @a shorten_zone, using
373 * @a original_label as one possible suggestion.
375 * @param original_label original label for the zone
376 * @param suggested_label suggested label for the zone
377 * @param pub public key of the zone to shorten
378 * @param shorten_zone private key of the target zone for the new record
381 GNS_shorten_start (const char *original_label,
382 const char *suggested_label,
383 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
384 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone)
386 struct GetPseuAuthorityHandle *gph;
387 struct GNUNET_CRYPTO_EcdsaPublicKey shorten_pub;
389 if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
394 GNUNET_CRYPTO_ecdsa_key_get_public (shorten_zone, &shorten_pub);
395 if (0 == memcmp (&shorten_pub, pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
397 /* Do not shorten the shorten zone */
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Starting shortening process for `%s' with old label `%s' and suggested nickname `%s'\n",
403 GNUNET_GNSRECORD_z2s (pub),
404 original_label, suggested_label);
405 gph = GNUNET_new (struct GetPseuAuthorityHandle);
406 gph->shorten_zone_key = *shorten_zone;
407 gph->target_zone = *pub;
408 gph->suggested_label = GNUNET_strdup (suggested_label);
409 strcpy (gph->label, original_label);
410 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
411 /* first, check if we *already* have a record for this zone */
412 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
415 &process_zone_to_name_discover,
421 * Initialize the shortening subsystem
423 * @param nh the namestore handle
424 * @param nc the namecache handle
425 * @param dht the dht handle
428 GNS_shorten_init (struct GNUNET_NAMESTORE_Handle *nh,
429 struct GNUNET_NAMECACHE_Handle *nc,
430 struct GNUNET_DHT_Handle *dht)
432 namestore_handle = nh;
433 namecache_handle = nc;
439 * Shutdown shortening.
444 /* abort active shorten operations */
445 while (NULL != gph_head)
446 free_get_pseu_authority_handle (gph_head);
448 namestore_handle = NULL;
449 namecache_handle = NULL;
452 /* end of gnunet-service-gns_shorten.c */