-fix bad sizeof
[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    * Suggested label based on NICK record
78    */
79   char * suggested_label;
80
81   /**
82    * Label we are currently trying out
83    */
84   char *current_label;
85
86   /**
87    * The zone for which we are trying to find the PSEU record.
88    */
89   struct GNUNET_CRYPTO_EcdsaPublicKey target_zone;
90
91   /**
92    * Handle for DHT lookups. Should be NULL if no lookups are in progress
93    */
94   struct GNUNET_DHT_GetHandle *get_handle;
95
96   /**
97    * Handle to namestore request
98    */
99   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
100
101   /**
102    * Handle to namecache request
103    */
104   struct GNUNET_NAMECACHE_QueueEntry *namecache_task;
105
106   /**
107    * Task to abort DHT lookup operation.
108    */
109   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
110
111 };
112
113
114 /**
115  * Head of PSEU/shorten operations list.
116  */
117 static struct GetPseuAuthorityHandle *gph_head;
118
119 /**
120  * Tail of PSEU/shorten operations list.
121  */
122 static struct GetPseuAuthorityHandle *gph_tail;
123
124 /**
125  * Our handle to the namestore service
126  */
127 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
128
129 /**
130  * Our handle to the namecache service
131  */
132 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
133
134 /**
135  * Resolver handle to the dht
136  */
137 static struct GNUNET_DHT_Handle *dht_handle;
138
139 /**
140  * Cleanup a 'struct GetPseuAuthorityHandle', terminating all
141  * pending activities.
142  *
143  * @param gph handle to terminate
144  */
145 static void
146 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
147 {
148   if (NULL != gph->get_handle)
149   {
150     GNUNET_DHT_get_stop (gph->get_handle);
151     gph->get_handle = NULL;
152   }
153   if (NULL != gph->namestore_task)
154   {
155     GNUNET_NAMESTORE_cancel (gph->namestore_task);
156     gph->namestore_task = NULL;
157   }
158   if (NULL != gph->namecache_task)
159   {
160     GNUNET_NAMECACHE_cancel (gph->namecache_task);
161     gph->namecache_task = NULL;
162   }
163   if (GNUNET_SCHEDULER_NO_TASK != gph->timeout_task)
164   {
165     GNUNET_SCHEDULER_cancel (gph->timeout_task);
166     gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
167   }
168   GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
169   GNUNET_free_non_null (gph->current_label);
170   GNUNET_free (gph);
171 }
172
173
174 /**
175  * Continuation for pkey record creation (shorten)
176  *
177  * @param cls a GetPseuAuthorityHandle
178  * @param success unused
179  * @param emsg unused
180  */
181 static void
182 create_pkey_cont (void* cls,
183                   int32_t success,
184                   const char *emsg)
185 {
186   struct GetPseuAuthorityHandle* gph = cls;
187
188   gph->namestore_task = NULL;
189   free_get_pseu_authority_handle (gph);
190 }
191
192
193 /**
194  * Namestore calls this function if we have record for this name.
195  * (or with rd_count=0 to indicate no matches).
196  *
197  * @param cls the pending query
198  * @param rd_count the number of records with 'name'
199  * @param rd the record data
200  */
201 static void
202 process_pseu_lookup_ns (void *cls,
203                         unsigned int rd_count,
204                         const struct GNUNET_GNSRECORD_Data *rd);
205
206
207 /**
208  * We obtained a result for our query to the shorten zone from
209  * the namestore.  Try to decrypt.
210  *
211  * @param cls the handle to our shorten operation
212  * @param block resulting encrypted block
213  */
214 static void
215 process_pseu_block_ns (void *cls,
216                        const struct GNUNET_GNSRECORD_Block *block)
217 {
218   struct GetPseuAuthorityHandle *gph = cls;
219   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
220
221   gph->namecache_task = NULL;
222   if (NULL == block)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                 "Namecache did not return information for label `%s' \n",
226                 gph->current_label);
227     process_pseu_lookup_ns (gph, 0, NULL);
228     return;
229   }
230   GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
231                                     &pub);
232   if (GNUNET_OK !=
233       GNUNET_GNSRECORD_block_decrypt (block,
234                                       &pub,
235                                       gph->current_label,
236                                       &process_pseu_lookup_ns,
237                                       gph))
238   {
239     GNUNET_break (0);
240     free_get_pseu_authority_handle (gph);
241     return;
242   }
243 }
244
245
246 /**
247  * Lookup in the namecache for the shorten zone the given label.
248  *
249  * @param gph the handle to our shorten operation
250  * @param label the label to lookup
251  */
252 static void
253 perform_nick_lookup (struct GetPseuAuthorityHandle *gph,
254                      const char *label)
255 {
256   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
257   struct GNUNET_HashCode query;
258
259   GNUNET_CRYPTO_ecdsa_key_get_public (&gph->shorten_zone_key,
260                                     &pub);
261   GNUNET_free_non_null (gph->current_label);
262   gph->current_label = GNUNET_strdup (label);
263   GNUNET_GNSRECORD_query_from_public_key (&pub,
264                                           label,
265                                           &query);
266   gph->namecache_task = GNUNET_NAMECACHE_lookup_block (namecache_handle,
267                                                        &query,
268                                                        &process_pseu_block_ns,
269                                                        gph);
270 }
271
272
273 /**
274  * Namestore calls this function if we have record for this name.
275  * (or with rd_count=0 to indicate no matches).
276  *
277  * @param cls the pending query
278  * @param rd_count the number of records with 'name'
279  * @param rd the record data
280  */
281 static void
282 process_pseu_lookup_ns (void *cls,
283                         unsigned int rd_count,
284                         const struct GNUNET_GNSRECORD_Data *rd)
285 {
286   struct GetPseuAuthorityHandle *gph = cls;
287   struct GNUNET_GNSRECORD_Data new_pkey;
288
289   gph->namestore_task = NULL;
290   if (rd_count > 0)
291   {
292     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293                 "Name `%s' already taken, cannot shorten.\n",
294                 gph->current_label);
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,
298                      gph->label))
299     {
300       free_get_pseu_authority_handle (gph);
301     }
302     else
303     {
304       perform_nick_lookup (gph, gph->label);
305     }
306     return;
307   }
308   /* name is available */
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Shortening `%s' to `%s'\n",
311               GNUNET_GNSRECORD_z2s (&gph->target_zone),
312               gph->current_label);
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;
319   gph->namestore_task
320     = GNUNET_NAMESTORE_records_store (namestore_handle,
321                                       &gph->shorten_zone_key,
322                                       gph->current_label,
323                                       1, &new_pkey,
324                                       &create_pkey_cont, gph);
325 }
326
327
328 /**
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.
331  *
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
337  */
338 static void
339 process_zone_to_name_discover (void *cls,
340                                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
341                                const char *name,
342                                unsigned int rd_len,
343                                const struct GNUNET_GNSRECORD_Data *rd)
344 {
345   struct GetPseuAuthorityHandle* gph = cls;
346 #if 0
347   struct GNUNET_HashCode lookup_key;
348 #endif
349
350   gph->namestore_task = NULL;
351   if (0 != rd_len)
352   {
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",
356                 name);
357     free_get_pseu_authority_handle (gph);
358     return;
359   }
360   else
361   {
362     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363                 "Shortening continuing, no name not reserved in shorten zone\n");
364   }
365   /* record does not yet exist, check if suggested label is available */
366   perform_nick_lookup (gph, gph->suggested_label);
367 }
368
369
370 /**
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.
374  *
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
379  */
380 void
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)
385 {
386   struct GetPseuAuthorityHandle *gph;
387   struct GNUNET_CRYPTO_EcdsaPublicKey shorten_pub;
388
389   if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
390   {
391     GNUNET_break (0);
392     return;
393   }
394   GNUNET_CRYPTO_ecdsa_key_get_public (shorten_zone, &shorten_pub);
395   if (0 == memcmp (&shorten_pub, pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
396   {
397     /* Do not shorten the shorten zone */
398     return;
399   }
400
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,
413                                                        shorten_zone,
414                                                        pub,
415                                                        &process_zone_to_name_discover,
416                                                        gph);
417 }
418
419
420 /**
421  * Initialize the shortening subsystem
422  *
423  * @param nh the namestore handle
424  * @param nc the namecache handle
425  * @param dht the dht handle
426  */
427 void
428 GNS_shorten_init (struct GNUNET_NAMESTORE_Handle *nh,
429                   struct GNUNET_NAMECACHE_Handle *nc,
430                   struct GNUNET_DHT_Handle *dht)
431 {
432   namestore_handle = nh;
433   namecache_handle = nc;
434   dht_handle = dht;
435 }
436
437
438 /**
439  * Shutdown shortening.
440  */
441 void
442 GNS_shorten_done ()
443 {
444   /* abort active shorten operations */
445   while (NULL != gph_head)
446     free_get_pseu_authority_handle (gph_head);
447   dht_handle = NULL;
448   namestore_handle = NULL;
449   namecache_handle = NULL;
450 }
451
452 /* end of gnunet-service-gns_shorten.c */