renaming PSEU to NICK
[oweals/gnunet.git] / src / gns / gnunet-service-gns_shorten.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011-2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file gns/gnunet-service-gns_shorten.c
23  * @brief GNUnet GNS shortening logic
24  * @author Martin Schanzenbach
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
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"
34 #include "gns.h"
35 #include "gnunet-service-gns_shorten.h"
36 #include "gnunet_vpn_service.h"
37
38
39 /**
40  * Default DHT timeout for lookups.
41  */
42 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
43
44 /**
45  * DHT replication level
46  */
47 #define DHT_GNS_REPLICATION_LEVEL 5
48
49
50 /**
51  * Handle for a PSEU lookup used to shorten names.
52  */
53 struct GetPseuAuthorityHandle
54 {
55   /**
56    * DLL
57    */
58   struct GetPseuAuthorityHandle *next;
59
60   /**
61    * DLL
62    */
63   struct GetPseuAuthorityHandle *prev;
64
65   /**
66    * Private key of the (shorten) zone to store the resulting
67    * pseudonym in.
68    */
69   struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_zone_key;
70
71   /**
72    * Original label (used if no PSEU record is found).
73    */
74   char label[GNUNET_DNSPARSER_MAX_LABEL_LENGTH + 1];
75
76   /**
77    * Label we are currently trying out (during #perform_pseu_lookup).
78    */
79   char *current_label;
80
81   /**
82    * The zone for which we are trying to find the PSEU record.
83    */
84   struct GNUNET_CRYPTO_EcdsaPublicKey target_zone;
85
86   /**
87    * Handle for DHT lookups. Should be NULL if no lookups are in progress
88    */
89   struct GNUNET_DHT_GetHandle *get_handle;
90
91   /**
92    * Handle to namestore request
93    */
94   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
95
96   /**
97    * Handle to namecache request
98    */
99   struct GNUNET_NAMECACHE_QueueEntry *namecache_task;
100
101   /**
102    * Task to abort DHT lookup operation.
103    */
104   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105
106 };
107
108
109 /**
110  * Head of PSEU/shorten operations list.
111  */
112 static struct GetPseuAuthorityHandle *gph_head;
113
114 /**
115  * Tail of PSEU/shorten operations list.
116  */
117 static struct GetPseuAuthorityHandle *gph_tail;
118
119 /**
120  * Our handle to the namestore service
121  */
122 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
123
124 /**
125  * Our handle to the namecache service
126  */
127 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
128
129 /**
130  * Resolver handle to the dht
131  */
132 static struct GNUNET_DHT_Handle *dht_handle;
133
134 /**
135  * Cleanup a 'struct GetPseuAuthorityHandle', terminating all
136  * pending activities.
137  *
138  * @param gph handle to terminate
139  */
140 static void
141 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
142 {
143   if (NULL != gph->get_handle)
144   {
145     GNUNET_DHT_get_stop (gph->get_handle);
146     gph->get_handle = NULL;
147   }
148   if (NULL != gph->namestore_task)
149   {
150     GNUNET_NAMESTORE_cancel (gph->namestore_task);
151     gph->namestore_task = NULL;
152   }
153   if (NULL != gph->namecache_task)
154   {
155     GNUNET_NAMECACHE_cancel (gph->namecache_task);
156     gph->namecache_task = NULL;
157   }
158   if (GNUNET_SCHEDULER_NO_TASK != gph->timeout_task)
159   {
160     GNUNET_SCHEDULER_cancel (gph->timeout_task);
161     gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
162   }
163   GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
164   GNUNET_free_non_null (gph->current_label);
165   GNUNET_free (gph);
166 }
167
168
169 /**
170  * Continuation for pkey record creation (shorten)
171  *
172  * @param cls a GetPseuAuthorityHandle
173  * @param success unused
174  * @param emsg unused
175  */
176 static void
177 create_pkey_cont (void* cls,
178                   int32_t success,
179                   const char *emsg)
180 {
181   struct GetPseuAuthorityHandle* gph = cls;
182
183   gph->namestore_task = NULL;
184   free_get_pseu_authority_handle (gph);
185 }
186
187
188 /**
189  * Namestore calls this function if we have record for this name.
190  * (or with rd_count=0 to indicate no matches).
191  *
192  * @param cls the pending query
193  * @param rd_count the number of records with 'name'
194  * @param rd the record data
195  */
196 static void
197 process_pseu_lookup_ns (void *cls,
198                         unsigned int rd_count,
199                         const struct GNUNET_GNSRECORD_Data *rd);
200
201
202 /**
203  * We obtained a result for our query to the shorten zone from
204  * the namestore.  Try to decrypt.
205  *
206  * @param cls the handle to our shorten operation
207  * @param block resulting encrypted block
208  */
209 static void
210 process_pseu_block_ns (void *cls,
211                        const struct GNUNET_GNSRECORD_Block *block)
212 {
213   struct GetPseuAuthorityHandle *gph = cls;
214   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
215
216   gph->namecache_task = NULL;
217   if (NULL == block)
218   {
219     process_pseu_lookup_ns (gph, 0, NULL);
220     return;
221   }
222   GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
223                                     &pub);
224   if (GNUNET_OK !=
225       GNUNET_GNSRECORD_block_decrypt (block,
226                                       &pub,
227                                       gph->current_label,
228                                       &process_pseu_lookup_ns,
229                                       gph))
230   {
231     GNUNET_break (0);
232     free_get_pseu_authority_handle (gph);
233     return;
234   }
235 }
236
237
238 /**
239  * Lookup in the namestore for the shorten zone the given label.
240  *
241  * @param gph the handle to our shorten operation
242  * @param label the label to lookup
243  */
244 static void
245 perform_pseu_lookup (struct GetPseuAuthorityHandle *gph,
246                      const char *label)
247 {
248   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
249   struct GNUNET_HashCode query;
250
251   GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
252                                     &pub);
253   GNUNET_free_non_null (gph->current_label);
254   gph->current_label = GNUNET_strdup (label);
255   GNUNET_GNSRECORD_query_from_public_key (&pub,
256                                           label,
257                                           &query);
258   gph->namecache_task = GNUNET_NAMECACHE_lookup_block (namecache_handle,
259                                                        &query,
260                                                        &process_pseu_block_ns,
261                                                        gph);
262 }
263
264
265 /**
266  * Namestore calls this function if we have record for this name.
267  * (or with rd_count=0 to indicate no matches).
268  *
269  * @param cls the pending query
270  * @param rd_count the number of records with 'name'
271  * @param rd the record data
272  */
273 static void
274 process_pseu_lookup_ns (void *cls,
275                         unsigned int rd_count,
276                         const struct GNUNET_GNSRECORD_Data *rd)
277 {
278   struct GetPseuAuthorityHandle *gph = cls;
279   struct GNUNET_GNSRECORD_Data new_pkey;
280
281   gph->namestore_task = NULL;
282   if (rd_count > 0)
283   {
284     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285                 "Name `%s' already taken, cannot shorten.\n",
286                 gph->current_label);
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,
290                      gph->label))
291     {
292       free_get_pseu_authority_handle (gph);
293     }
294     else
295     {
296       perform_pseu_lookup (gph, gph->label);
297     }
298     return;
299   }
300   /* name is available */
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302               "Shortening `%s' to `%s'\n",
303               GNUNET_GNSRECORD_z2s (&gph->target_zone),
304               gph->current_label);
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;
311   gph->namestore_task
312     = GNUNET_NAMESTORE_records_store (namestore_handle,
313                                       &gph->shorten_zone_key,
314                                       gph->current_label,
315                                       1, &new_pkey,
316                                       &create_pkey_cont, gph);
317 }
318
319
320 /**
321  * Process result of a DHT lookup for a PSEU record.
322  *
323  * @param gph the handle to our shorten operation
324  * @param pseu the pseu result or NULL
325  */
326 static void
327 process_pseu_result (struct GetPseuAuthorityHandle* gph,
328                      const char *pseu)
329 {
330   if (NULL == pseu)
331   {
332     /* no PSEU found, try original label */
333     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334                 "No PSEU found, trying original label `%s' instead.\n",
335                 gph->label);
336     perform_pseu_lookup (gph, gph->label);
337     return;
338   }
339   /* check if 'pseu' is taken */
340   perform_pseu_lookup (gph, pseu);
341 }
342
343
344 /**
345  * Handle timeout for DHT request during shortening.
346  *
347  * @param cls the request handle as closure
348  * @param tc the task context
349  */
350 static void
351 handle_auth_discovery_timeout (void *cls,
352                                const struct GNUNET_SCHEDULER_TaskContext *tc)
353 {
354   struct GetPseuAuthorityHandle *gph = cls;
355
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);
363 }
364
365
366 /**
367  * Handle decrypted records from DHT result.
368  *
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
372  */
373 static void
374 process_auth_records (void *cls,
375                       unsigned int rd_count,
376                       const struct GNUNET_GNSRECORD_Data *rd)
377 {
378   struct GetPseuAuthorityHandle *gph = cls;
379   unsigned int i;
380
381   for (i=0; i < rd_count; i++)
382   {
383     if (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type)
384     {
385       char pseu[rd[i].data_size + 1];
386
387       /* found pseu */
388       memcpy (pseu,
389               rd[i].data,
390               rd[i].data_size);
391       pseu[rd[i].data_size] = '\0';
392       process_pseu_result (gph,
393                            pseu);
394       return;
395     }
396   }
397   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398               "No PSEU record found in DHT reply with %u records.\n",
399               rd_count);
400   process_pseu_result (gph, NULL);
401 }
402
403
404 /**
405  * Function called when we find a PSEU entry in the DHT
406  *
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
417  */
418 static void
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,
427                                    size_t size,
428                                    const void *data)
429 {
430   struct GetPseuAuthorityHandle *gph = cls;
431   const struct GNUNET_GNSRECORD_Block *block;
432
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;
439
440   if (NULL == data)
441   {
442     /* is this allowed!? */
443     GNUNET_break (0);
444     process_pseu_result (gph, NULL);
445     return;
446   }
447   if (size < sizeof (struct GNUNET_GNSRECORD_Block))
448   {
449     /* how did this pass DHT block validation!? */
450     GNUNET_break (0);
451     process_pseu_result (gph, NULL);
452     return;
453   }
454   block = data;
455   if (size !=
456       ntohl (block->purpose.size) +
457       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
458       sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
459   {
460     /* how did this pass DHT block validation!? */
461     GNUNET_break (0);
462     process_pseu_result (gph, NULL);
463     return;
464   }
465   if (GNUNET_OK !=
466       GNUNET_GNSRECORD_block_decrypt (block,
467                                       &gph->target_zone,
468                                       GNUNET_GNS_TLD_PLUS,
469                                       &process_auth_records,
470                                       gph))
471   {
472     /* other peer encrypted invalid block, complain */
473     GNUNET_break_op (0);
474     process_pseu_result (gph, NULL);
475     return;
476   }
477 }
478
479
480 /**
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.
483  *
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
489  */
490 static void
491 process_zone_to_name_discover (void *cls,
492                                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
493                                const char *name,
494                                unsigned int rd_len,
495                                const struct GNUNET_GNSRECORD_Data *rd)
496 {
497   struct GetPseuAuthorityHandle* gph = cls;
498   struct GNUNET_HashCode lookup_key;
499
500   gph->namestore_task = NULL;
501   if (0 != rd_len)
502   {
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",
506                 name);
507     free_get_pseu_authority_handle (gph);
508     return;
509   }
510   /* record does not yet exist, go into DHT to find PSEU record */
511   GNUNET_GNSRECORD_query_from_public_key (&gph->target_zone,
512                                           GNUNET_GNS_TLD_PLUS,
513                                           &lookup_key);
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));
518
519   gph->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
520                                                     &handle_auth_discovery_timeout,
521                                                     gph);
522   gph->get_handle = GNUNET_DHT_get_start (dht_handle,
523                                           GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
524                                           &lookup_key,
525                                           DHT_GNS_REPLICATION_LEVEL,
526                                           GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
527                                           NULL, 0,
528                                           &process_auth_discovery_dht_result,
529                                           gph);
530 }
531
532
533 /**
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.
537  *
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
541  */
542 void
543 GNS_shorten_start (const char *original_label,
544                    const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
545                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone)
546 {
547   struct GetPseuAuthorityHandle *gph;
548
549   if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
550   {
551     GNUNET_break (0);
552     return;
553   }
554   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555               "Starting shortening process for `%s' with old label `%s'\n",
556               GNUNET_GNSRECORD_z2s (pub),
557               original_label);
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,
565                                                        shorten_zone,
566                                                        pub,
567                                                        &process_zone_to_name_discover,
568                                                        gph);
569 }
570
571
572 /**
573  * Initialize the shortening subsystem
574  *
575  * @param nh the namestore handle
576  * @param nc the namecache handle
577  * @param dht the dht handle
578  */
579 void
580 GNS_shorten_init (struct GNUNET_NAMESTORE_Handle *nh,
581                   struct GNUNET_NAMECACHE_Handle *nc,
582                   struct GNUNET_DHT_Handle *dht)
583 {
584   namestore_handle = nh;
585   namecache_handle = nc;
586   dht_handle = dht;
587 }
588
589
590 /**
591  * Shutdown shortening.
592  */
593 void
594 GNS_shorten_done ()
595 {
596   /* abort active shorten operations */
597   while (NULL != gph_head)
598     free_get_pseu_authority_handle (gph_head);
599   dht_handle = NULL;
600   namestore_handle = NULL;
601   namecache_handle = NULL;
602 }
603
604 /* end of gnunet-service-gns_shorten.c */