Asynchronous namespace creation. With a test.
[oweals/gnunet.git] / src / fs / fs_namespace.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 fs/fs_namespace.c
23  * @brief create and destroy namespaces
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
31 #include "fs_api.h"
32
33
34 /**
35  * Maximum legal size for an sblock.
36  */
37 #define MAX_SBLOCK_SIZE (60 * 1024)
38
39
40 /**
41  * Context for creating a namespace asynchronously.
42  */
43 struct GNUNET_FS_NamespaceCreationContext
44 {
45   /**
46    * Context for asynchronous key creation.
47    */
48   struct GNUNET_CRYPTO_RsaKeyGenerationContext *keycreator;
49
50   /**
51    * Name of the file to store key in / read key from.
52    */
53   char *filename;
54
55   /**
56    * Name of the namespace.
57    */
58   char *name;
59
60   /**
61    * Global fs handle
62    */
63   struct GNUNET_FS_Handle *h;
64
65   /**
66    * Function to call when generation ends (successfully or not)
67    */
68   GNUNET_FS_NamespaceCreationCallback cont;
69
70   /**
71    * Client value to pass to continuation function.
72    */
73   void *cont_cls;
74 };
75
76
77 /**
78  * Return the name of the directory in which we store
79  * our local namespaces (or rather, their public keys).
80  *
81  * @param h global fs handle
82  * @return NULL on error, otherwise the name of the directory
83  */
84 static char *
85 get_namespace_directory (struct GNUNET_FS_Handle *h)
86 {
87   char *dn;
88
89   if (GNUNET_OK !=
90       GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "IDENTITY_DIR",
91                                                &dn))
92   {
93     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
94                                "fs", "IDENTITY_DIR");
95     return NULL;
96   }
97   return dn;
98 }
99
100
101 /**
102  * Return the name of the directory in which we store
103  * the update information graph for the given local namespace.
104  *
105  * @param ns namespace handle
106  * @return NULL on error, otherwise the name of the directory
107  */
108 static char *
109 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
110 {
111   char *dn;
112   char *ret;
113
114   if (GNUNET_OK !=
115       GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg, "FS", "UPDATE_DIR",
116                                                &dn))
117   {
118     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
119                                "fs", "UPDATE_DIR");
120     return NULL;
121   }
122   GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name);
123   GNUNET_free (dn);
124   return ret;
125 }
126
127
128 /**
129  * Write the namespace update node graph to a file.
130  *
131  * @param ns namespace to dump
132  */
133 static void
134 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
135 {
136   char *fn;
137   struct GNUNET_BIO_WriteHandle *wh;
138   unsigned int i;
139   struct NamespaceUpdateNode *n;
140   char *uris;
141
142   fn = get_update_information_directory (ns);
143   wh = GNUNET_BIO_write_open (fn);
144   if (NULL == wh)
145   {
146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147                 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
148     GNUNET_free (fn);
149     return;
150   }
151   if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, ns->update_node_count))
152     goto END;
153   for (i = 0; i < ns->update_node_count; i++)
154   {
155     n = ns->update_nodes[i];
156     uris = GNUNET_FS_uri_to_string (n->uri);
157     if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
158         (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
159         (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
160         (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
161     {
162       GNUNET_free (uris);
163       break;
164     }
165     GNUNET_free (uris);
166   }
167 END:
168   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
170                 STRERROR (errno));
171   GNUNET_free (fn);
172 }
173
174
175 /**
176  * Read the namespace update node graph from a file.
177  *
178  * @param ns namespace to read
179  */
180 static void
181 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
182 {
183   char *fn;
184   struct GNUNET_BIO_ReadHandle *rh;
185   unsigned int i;
186   struct NamespaceUpdateNode *n;
187   char *uris;
188   uint32_t count;
189   char *emsg;
190
191   fn = get_update_information_directory (ns);
192   if (GNUNET_YES != GNUNET_DISK_file_test (fn))
193   {
194     GNUNET_free (fn);
195     return;
196   }
197   rh = GNUNET_BIO_read_open (fn);
198   if (NULL == rh)
199   {
200     GNUNET_free (fn);
201     return;
202   }
203   if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
204   {
205     GNUNET_break (0);
206     goto END;
207   }
208   if (count > 1024 * 1024)
209   {
210     GNUNET_break (0);
211     goto END;
212   }
213   if (0 == count)
214   {
215     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
216     GNUNET_free (fn);
217     return;
218   }
219   ns->update_nodes =
220       GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
221
222   for (i = 0; i < count; i++)
223   {
224     n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
225     if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
226         || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
227         (GNUNET_OK !=
228          GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
229         (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
230     {
231       GNUNET_break (0);
232       GNUNET_free_non_null (n->id);
233       GNUNET_free_non_null (n->update);
234       if (n->md != NULL)
235         GNUNET_CONTAINER_meta_data_destroy (n->md);
236       GNUNET_free (n);
237       break;
238     }
239     n->uri = GNUNET_FS_uri_parse (uris, &emsg);
240     GNUNET_free (uris);
241     if (n->uri == NULL)
242     {
243       GNUNET_break (0);
244       GNUNET_free (emsg);
245       GNUNET_free (n->id);
246       GNUNET_free_non_null (n->update);
247       GNUNET_CONTAINER_meta_data_destroy (n->md);
248       GNUNET_free (n);
249       break;
250     }
251     ns->update_nodes[i] = n;
252   }
253   ns->update_node_count = i;
254 END:
255   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
256   {
257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg);
258     GNUNET_free (emsg);
259   }
260   GNUNET_free (fn);
261 }
262
263
264 /**
265  * Create a namespace with the given name; if one already
266  * exists, return a handle to the existing namespace.
267  *
268  * @param h handle to the file sharing subsystem
269  * @param name name to use for the namespace
270  * @return handle to the namespace, NULL on error (i.e. invalid filename)
271  */
272 struct GNUNET_FS_Namespace *
273 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
274 {
275   char *dn;
276   char *fn;
277   struct GNUNET_FS_Namespace *ret;
278
279   dn = get_namespace_directory (h);
280   if (NULL == dn)
281   {
282     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283                 _("Can't determine where namespace directory is\n"));
284     return NULL;
285   }
286   GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
287   GNUNET_free (dn);
288   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
289   ret->h = h;
290   ret->rc = 1;
291   ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
292   if (NULL == ret->key)
293   {
294     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295                 _("Failed to create or read private key for namespace `%s'\n"),
296                 name);
297     GNUNET_free (ret);
298     GNUNET_free (fn);
299     return NULL;
300   }
301   ret->name = GNUNET_strdup (name);
302   ret->filename = fn;
303   return ret;
304 }
305
306
307 /**
308  * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_start'.
309  *
310  * @param cls closure
311  * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
312  * @param emsg NULL on success, otherwise an error message
313  */
314 static void
315 ns_key_created (void *cls, struct GNUNET_CRYPTO_RsaPrivateKey *pk,
316     const char *emsg)
317 {
318   struct GNUNET_FS_NamespaceCreationContext *ncc = cls;
319
320   ncc->keycreator = NULL;
321
322   if (pk)
323   {
324     struct GNUNET_FS_Namespace *ret;
325     ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
326     ret->rc = 1;
327     ret->key = pk;
328     ret->h = ncc->h;
329     ret->name = ncc->name;
330     ret->filename = ncc->filename;
331     ncc->cont (ncc->cont_cls, ret, NULL);
332   }
333   else
334   {
335     GNUNET_free (ncc->filename);
336     GNUNET_free (ncc->name);
337     ncc->cont (ncc->cont_cls, NULL, emsg);
338   }
339   GNUNET_free (ncc);
340 }
341
342
343 /**
344  * Create a namespace with the given name.
345  * If one already exists, the continuation will be called with a handle to
346  * the existing namespace.
347  * Otherwise creates a new namespace.
348  *
349  * @param h handle to the file sharing subsystem
350  * @param name name to use for the namespace
351  * @return namespace creation context, NULL on error (i.e. invalid filename)
352  */
353 struct GNUNET_FS_NamespaceCreationContext *
354 GNUNET_FS_namespace_create_start (struct GNUNET_FS_Handle *h, const char *name, 
355     GNUNET_FS_NamespaceCreationCallback cont, void *cont_cls)
356 {
357   char *dn;
358   char *fn;
359   struct GNUNET_FS_NamespaceCreationContext *ret;
360
361   dn = get_namespace_directory (h);
362   if (NULL == dn)
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
365                 _("Can't determine where namespace directory is\n"));
366     return NULL;
367   }
368   GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
369   GNUNET_free (dn);
370
371   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_NamespaceCreationContext));
372   ret->filename = fn;
373   ret->h = h;
374   ret->name = GNUNET_strdup (name);
375   ret->cont = cont;
376   ret->cont_cls = cont_cls;
377
378   ret->keycreator = GNUNET_CRYPTO_rsa_key_create_start (fn,
379       ns_key_created, ret);
380
381   if (NULL == ret->keycreator)
382   {
383     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384         _("Failed to start creating or reading private key for namespace `%s'\n"),
385         name);
386     GNUNET_free (fn);
387     GNUNET_free (ret->name);
388     GNUNET_free (ret);
389     return NULL;
390   }
391   return ret;
392 }
393
394
395 /**
396  * Abort namespace creation.
397  *
398  * @param ncc namespace creation context to abort
399  */
400 void
401 GNUNET_FS_namespace_create_stop (struct GNUNET_FS_NamespaceCreationContext *ncc)
402 {
403   if (NULL != ncc->keycreator)
404   {
405     GNUNET_CRYPTO_rsa_key_create_stop (ncc->keycreator);
406     ncc->keycreator = NULL;
407   }
408   if (NULL != ncc->filename)
409   {
410     if (0 != UNLINK (ncc->filename))
411       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", ncc->filename);
412     GNUNET_free (ncc->filename);
413   }
414   GNUNET_free_non_null (ncc->name);
415   GNUNET_free (ncc);
416 }
417
418
419 /**
420  * Duplicate a namespace handle.
421  *
422  * @param ns namespace handle
423  * @return duplicated handle to the namespace
424  */
425 struct GNUNET_FS_Namespace *
426 GNUNET_FS_namespace_dup (struct GNUNET_FS_Namespace *ns)
427 {
428   ns->rc++;
429   return ns;
430 }
431
432
433 /**
434  * Delete a namespace handle.  Can be used for a clean shutdown (free
435  * memory) or also to freeze the namespace to prevent further
436  * insertions by anyone.
437  *
438  * @param ns handle to the namespace that should be deleted / freed
439  * @param freeze prevents future insertions; creating a namespace
440  *        with the same name again will create a fresh namespace instead
441  *
442  * @return GNUNET_OK on success, GNUNET_SYSERR on error
443  */
444 int
445 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *ns, int freeze)
446 {
447   unsigned int i;
448   struct NamespaceUpdateNode *nsn;
449
450   ns->rc--;
451   if (freeze)
452   {
453     if (0 != UNLINK (ns->filename))
454       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
455                                 ns->filename);
456   }
457   if (0 != ns->rc)
458     return GNUNET_OK;
459   GNUNET_CRYPTO_rsa_key_free (ns->key);
460   GNUNET_free (ns->filename);
461   GNUNET_free (ns->name);
462   for (i = 0; i < ns->update_node_count; i++)
463   {
464     nsn = ns->update_nodes[i];
465     GNUNET_CONTAINER_meta_data_destroy (nsn->md);
466     GNUNET_FS_uri_destroy (nsn->uri);
467     GNUNET_free (nsn->id);
468     GNUNET_free (nsn->update);
469     GNUNET_free (nsn);
470   }
471   GNUNET_array_grow (ns->update_nodes, ns->update_node_count,
472                      0);
473   if (ns->update_map != NULL)
474     GNUNET_CONTAINER_multihashmap_destroy (ns->update_map);
475   GNUNET_free (ns);
476   return GNUNET_OK;
477 }
478
479
480 /**
481  * Context for the 'process_namespace' callback.
482  * Specifies a function to call on each namespace.
483  */
484 struct ProcessNamespaceContext
485 {
486   /**
487    * Function to call.
488    */
489   GNUNET_FS_NamespaceInfoProcessor cb;
490
491   /**
492    * Closure for 'cb'.
493    */
494   void *cb_cls;
495 };
496
497
498 /**
499  * Function called with a filename of a namespace. Reads the key and
500  * calls the callback.
501  *
502  * @param cls closure (struct ProcessNamespaceContext)
503  * @param filename complete filename (absolute path)
504  * @return GNUNET_OK to continue to iterate,
505  *  GNUNET_SYSERR to abort iteration with error!
506  */
507 static int
508 process_namespace (void *cls, const char *filename)
509 {
510   struct ProcessNamespaceContext *pnc = cls;
511   struct GNUNET_CRYPTO_RsaPrivateKey *key;
512   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
513   struct GNUNET_HashCode id;
514   const char *name;
515   const char *t;
516
517   key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
518   if (NULL == key)
519   {
520     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521                 _
522                 ("Failed to read namespace private key file `%s', deleting it!\n"),
523                 filename);
524     if (0 != UNLINK (filename))
525       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
526     return GNUNET_OK;
527   }
528   GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
529   GNUNET_CRYPTO_rsa_key_free (key);
530   GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id);
531   name = filename;
532   while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
533     name = t + 1;
534   pnc->cb (pnc->cb_cls, name, &id);
535   return GNUNET_OK;
536 }
537
538
539 /**
540  * Build a list of all available local (!) namespaces The returned
541  * names are only the nicknames since we only iterate over the local
542  * namespaces.
543  *
544  * @param h handle to the file sharing subsystem
545  * @param cb function to call on each known namespace
546  * @param cb_cls closure for cb
547  */
548 void
549 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
550                           GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
551 {
552   char *dn;
553   struct ProcessNamespaceContext ctx;
554
555   dn = get_namespace_directory (h);
556   if (NULL == dn)
557     return;
558   ctx.cb = cb;
559   ctx.cb_cls = cb_cls;
560   GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
561   GNUNET_free (dn);
562 }
563
564
565 /**
566  * Context for the SKS publication.
567  */
568 struct GNUNET_FS_PublishSksContext
569 {
570
571   /**
572    * URI of the new entry in the namespace.
573    */
574   struct GNUNET_FS_Uri *uri;
575
576   /**
577    * Namespace update node to add to namespace on success (or to be
578    * deleted if publishing failed).
579    */
580   struct NamespaceUpdateNode *nsn;
581
582   /**
583    * Namespace we're publishing to.
584    */
585   struct GNUNET_FS_Namespace *ns;
586
587   /**
588    * Handle to the datastore.
589    */
590   struct GNUNET_DATASTORE_Handle *dsh;
591
592   /**
593    * Function to call once we're done.
594    */
595   GNUNET_FS_PublishContinuation cont;
596
597   /**
598    * Closure for cont.
599    */
600   void *cont_cls;
601
602   /**
603    * Handle for our datastore request.
604    */
605   struct GNUNET_DATASTORE_QueueEntry *dqe;
606 };
607
608
609 /**
610  * Function called by the datastore API with
611  * the result from the PUT (SBlock) request.
612  *
613  * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
614  * @param success GNUNET_OK on success
615  * @param min_expiration minimum expiration time required for content to be stored
616  * @param msg error message (or NULL)
617  */
618 static void
619 sb_put_cont (void *cls, int success, 
620              struct GNUNET_TIME_Absolute min_expiration,
621              const char *msg)
622 {
623   struct GNUNET_FS_PublishSksContext *psc = cls;
624   struct GNUNET_HashCode hc;
625
626   psc->dqe = NULL;
627   if (GNUNET_OK != success)
628   {
629     if (NULL != psc->cont)
630       psc->cont (psc->cont_cls, NULL, msg);
631     GNUNET_FS_publish_sks_cancel (psc);
632     return;
633   }
634   if (NULL != psc->nsn)
635   {
636     /* FIXME: this can be done much more
637      * efficiently by simply appending to the
638      * file and overwriting the 4-byte header */
639     if (psc->ns->update_nodes == NULL)
640       read_update_information_graph (psc->ns);
641     GNUNET_array_append (psc->ns->update_nodes,
642                          psc->ns->update_node_count, psc->nsn);
643     if (psc->ns->update_map != NULL)
644     {
645       GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
646       GNUNET_CONTAINER_multihashmap_put (psc->ns->update_map, &hc,
647                                          psc->nsn,
648                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
649     }
650     psc->nsn = NULL;
651     write_update_information_graph (psc->ns);
652   }
653   if (NULL != psc->cont)
654     psc->cont (psc->cont_cls, psc->uri, NULL);
655   GNUNET_FS_publish_sks_cancel (psc);
656 }
657
658
659 /**
660  * Publish an SBlock on GNUnet.
661  *
662  * @param h handle to the file sharing subsystem
663  * @param ns namespace to publish in
664  * @param identifier identifier to use
665  * @param update update identifier to use
666  * @param meta metadata to use
667  * @param uri URI to refer to in the SBlock
668  * @param bo block options
669  * @param options publication options
670  * @param cont continuation
671  * @param cont_cls closure for cont
672  * @return NULL on error ('cont' will still be called)
673  */
674 struct GNUNET_FS_PublishSksContext *
675 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
676                        struct GNUNET_FS_Namespace *ns,
677                        const char *identifier, const char *update,
678                        const struct GNUNET_CONTAINER_MetaData *meta,
679                        const struct GNUNET_FS_Uri *uri,
680                        const struct GNUNET_FS_BlockOptions *bo,
681                        enum GNUNET_FS_PublishOptions options,
682                        GNUNET_FS_PublishContinuation cont, void *cont_cls)
683 {
684   struct GNUNET_FS_PublishSksContext *psc;
685   struct GNUNET_CRYPTO_AesSessionKey sk;
686   struct GNUNET_CRYPTO_AesInitializationVector iv;
687   struct GNUNET_FS_Uri *sks_uri;
688   char *uris;
689   size_t size;
690   size_t slen;
691   size_t nidlen;
692   size_t idlen;
693   ssize_t mdsize;
694   struct SBlock *sb;
695   struct SBlock *sb_enc;
696   char *dest;
697   struct GNUNET_CONTAINER_MetaData *mmeta;
698   struct GNUNET_HashCode key;          /* hash of thisId = key */
699   struct GNUNET_HashCode id;           /* hash of hc = identifier */
700   struct GNUNET_HashCode query;        /* id ^ nsid = DB query */
701
702   idlen = strlen (identifier);
703   if (NULL != update)
704     nidlen = strlen (update) + 1;
705   else
706     nidlen = 1;
707   uris = GNUNET_FS_uri_to_string (uri);
708   slen = strlen (uris) + 1;
709   if ( (slen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock)) ||
710        (nidlen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock) - slen) )
711   {
712     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713                 _("Identifiers or URI too long to create SBlock"));
714     GNUNET_free (uris);
715     return NULL;
716   }
717   if (NULL == meta)
718     mmeta = GNUNET_CONTAINER_meta_data_create ();
719   else
720     mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
721   mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
722   size = sizeof (struct SBlock) + slen + nidlen + mdsize;
723   if ( (size > MAX_SBLOCK_SIZE) ||
724        (size < sizeof (struct SBlock) + slen + nidlen) )
725   {
726     size = MAX_SBLOCK_SIZE;
727     mdsize = MAX_SBLOCK_SIZE - (sizeof (struct SBlock) + slen + nidlen);
728   }
729   sb = GNUNET_malloc (sizeof (struct SBlock) + size);
730   dest = (char *) &sb[1];
731   if (NULL != update)
732     memcpy (dest, update, nidlen);
733   else
734     memset (dest, 0, 1);
735   dest += nidlen;
736   memcpy (dest, uris, slen);
737   GNUNET_free (uris);
738   dest += slen;
739   mdsize =
740       GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
741                                             GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
742   GNUNET_CONTAINER_meta_data_destroy (mmeta);
743   if (-1 == mdsize)
744   {
745     GNUNET_break (0);
746     GNUNET_free (sb);
747     if (NULL != cont)
748       cont (cont_cls, NULL, _("Internal error."));
749     return NULL;
750   }
751   size = sizeof (struct SBlock) + mdsize + slen + nidlen;
752   sb_enc = GNUNET_malloc (size);
753   GNUNET_CRYPTO_hash (identifier, idlen, &key);
754   GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &id);
755   sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
756   sks_uri->type = GNUNET_FS_URI_SKS;
757   GNUNET_CRYPTO_rsa_key_get_public (ns->key, &sb_enc->subspace);
758   GNUNET_CRYPTO_hash (&sb_enc->subspace,
759                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
760                       &sks_uri->data.sks.ns);
761   sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
762   GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.ns,
763                           &sb_enc->identifier);
764   GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
765   GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
766                              &sb_enc[1]);
767   sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
768   sb_enc->purpose.size =
769       htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
770              sizeof (struct GNUNET_CRYPTO_RsaSignature));
771   GNUNET_assert (GNUNET_OK ==
772                  GNUNET_CRYPTO_rsa_sign (ns->key, &sb_enc->purpose,
773                                          &sb_enc->signature));
774   psc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishSksContext));
775   psc->uri = sks_uri;
776   psc->cont = cont;
777   psc->ns = GNUNET_FS_namespace_dup (ns);
778   psc->cont_cls = cont_cls;
779   if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
780   {
781     GNUNET_free (sb_enc);
782     GNUNET_free (sb);
783     sb_put_cont (psc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
784     return NULL;
785   }
786   psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
787   if (NULL == psc->dsh)
788   {
789     GNUNET_free (sb_enc);
790     GNUNET_free (sb);
791     sb_put_cont (psc, GNUNET_NO, GNUNET_TIME_UNIT_ZERO_ABS, _("Failed to connect to datastore."));
792     return NULL;
793   }
794   GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.ns, &id, &query);
795   if (NULL != update)
796   {
797     psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
798     psc->nsn->id = GNUNET_strdup (identifier);
799     psc->nsn->update = GNUNET_strdup (update);
800     psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
801     psc->nsn->uri = GNUNET_FS_uri_dup (uri);
802   }
803   psc->dqe = GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
804                                    GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
805                                    bo->anonymity_level, bo->replication_level,
806                                    bo->expiration_time, -2, 1,
807                                    GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
808   GNUNET_free (sb);
809   GNUNET_free (sb_enc);
810   return psc;
811 }
812
813
814 /**
815  * Abort the SKS publishing operation.
816  *
817  * @param psc context of the operation to abort.
818  */
819 void
820 GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
821 {
822   if (NULL != psc->dqe)
823   {
824     GNUNET_DATASTORE_cancel (psc->dqe);
825     psc->dqe = NULL;
826   }
827   if (NULL != psc->dsh)
828   {
829     GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
830     psc->dsh = NULL;
831   }
832   GNUNET_FS_namespace_delete (psc->ns, GNUNET_NO);
833   GNUNET_FS_uri_destroy (psc->uri);
834   if (NULL != psc->nsn)
835   {
836     GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
837     GNUNET_FS_uri_destroy (psc->nsn->uri);
838     GNUNET_free (psc->nsn->id);
839     GNUNET_free (psc->nsn->update);
840     GNUNET_free (psc->nsn);
841   }
842   GNUNET_free (psc);
843 }
844
845
846 /**
847  * Closure for 'process_update_node'.
848  */
849 struct ProcessUpdateClosure
850 {
851   /**
852    * Function to call for each node.
853    */
854   GNUNET_FS_IdentifierProcessor ip;
855
856   /**
857    * Closure for 'ip'.
858    */
859   void *ip_cls;
860 };
861
862
863 /**
864  * Call the iterator in the closure for each node.
865  *
866  * @param cls closure (of type 'struct ProcessUpdateClosure *')
867  * @param key current key code
868  * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
869  * @return GNUNET_YES if we should continue to
870  *         iterate,
871  *         GNUNET_NO if not.
872  */
873 static int
874 process_update_node (void *cls, const struct GNUNET_HashCode * key, void *value)
875 {
876   struct ProcessUpdateClosure *pc = cls;
877   struct NamespaceUpdateNode *nsn = value;
878
879   pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
880   return GNUNET_YES;
881 }
882
883
884 /**
885  * Closure for 'find_trees'.
886  */
887 struct FindTreeClosure
888 {
889   /**
890    * Namespace we are operating on.
891    */
892   struct GNUNET_FS_Namespace *ns;
893
894   /**
895    * Array with 'head's of TREEs.
896    */
897   struct NamespaceUpdateNode **tree_array;
898
899   /**
900    * Size of 'tree_array'
901    */
902   unsigned int tree_array_size;
903
904   /**
905    * Current generational ID used.
906    */
907   unsigned int nug;
908
909   /**
910    * Identifier for the current TREE, or UINT_MAX for none yet.
911    */
912   unsigned int id;
913 };
914
915
916 /**
917  * Find all nodes reachable from the current node (including the
918  * current node itself).  If they are in no tree, add them to the
919  * current one.   If they are the head of another tree, merge the
920  * trees.  If they are in the middle of another tree, let them be.
921  * We can tell that a node is already in an tree by checking if
922  * its 'nug' field is set to the current 'nug' value.  It is the
923  * head of an tree if it is in the 'tree_array' under its respective
924  * 'tree_id'.
925  *
926  * In short, we're trying to find the smallest number of tree to
927  * cover a directed graph.
928  *
929  * @param cls closure (of type 'struct FindTreeClosure')
930  * @param key current key code
931  * @param value value in the hash map
932  * @return GNUNET_YES if we should continue to
933  *         iterate,
934  *         GNUNET_NO if not.
935  */
936 static int
937 find_trees (void *cls, const struct GNUNET_HashCode * key, void *value)
938 {
939   struct FindTreeClosure *fc = cls;
940   struct NamespaceUpdateNode *nsn = value;
941   struct GNUNET_HashCode hc;
942
943   if (nsn->nug == fc->nug)
944   {
945     if (UINT_MAX == nsn->tree_id)
946       return GNUNET_YES;        /* circular */
947     GNUNET_assert (nsn->tree_id < fc->tree_array_size);
948     if (fc->tree_array[nsn->tree_id] != nsn)
949       return GNUNET_YES;        /* part of "another" (directed) TREE,
950                                  * and not root of it, end trace */
951     if (nsn->tree_id == fc->id)
952       return GNUNET_YES;        /* that's our own root (can this be?) */
953     /* merge existing TREE, we have a root for both */
954     fc->tree_array[nsn->tree_id] = NULL;
955     if (UINT_MAX == fc->id)
956       fc->id = nsn->tree_id;    /* take over ID */
957   }
958   else
959   {
960     nsn->nug = fc->nug;
961     nsn->tree_id = UINT_MAX;    /* mark as undef */
962     /* trace */
963     GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
964     GNUNET_CONTAINER_multihashmap_get_multiple (fc->ns->update_map, &hc,
965                                                 &find_trees, fc);
966   }
967   return GNUNET_YES;
968 }
969
970
971 /**
972  * List all of the identifiers in the namespace for which we could
973  * produce an update.  Namespace updates form a graph where each node
974  * has a name.  Each node can have any number of URI/meta-data entries
975  * which can each be linked to other nodes.  Cycles are possible.
976  *
977  * Calling this function with "next_id" NULL will cause the library to
978  * call "ip" with a root for each strongly connected component of the
979  * graph (a root being a node from which all other nodes in the Tree
980  * are reachable).
981  *
982  * Calling this function with "next_id" being the name of a node will
983  * cause the library to call "ip" with all children of the node.  Note
984  * that cycles within the final tree are possible (including self-loops).
985  * I know, odd definition of a tree, but the GUI will display an actual
986  * tree (GtkTreeView), so that's what counts for the term here.
987  *
988  * @param ns namespace to inspect for updateable content
989  * @param next_id ID to look for; use NULL to look for tree roots
990  * @param ip function to call on each updateable identifier
991  * @param ip_cls closure for ip
992  */
993 void
994 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns,
995                                      const char *next_id,
996                                      GNUNET_FS_IdentifierProcessor ip,
997                                      void *ip_cls)
998 {
999   unsigned int i;
1000   unsigned int nug;
1001   struct GNUNET_HashCode hc;
1002   struct NamespaceUpdateNode *nsn;
1003   struct ProcessUpdateClosure pc;
1004   struct FindTreeClosure fc;
1005
1006   if (NULL == ns->update_nodes)
1007     read_update_information_graph (ns);
1008   if (NULL == ns->update_nodes)
1009   {
1010     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011                 "No updateable nodes found for ID `%s'\n", next_id);
1012     return;                     /* no nodes */
1013   }
1014   if (NULL == ns->update_map)
1015   {
1016     /* need to construct */
1017     ns->update_map =
1018         GNUNET_CONTAINER_multihashmap_create (2 +
1019                                               3 * ns->update_node_count /
1020                                               4,
1021                                               GNUNET_NO);
1022     for (i = 0; i < ns->update_node_count; i++)
1023     {
1024       nsn = ns->update_nodes[i];
1025       GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1026       GNUNET_CONTAINER_multihashmap_put (ns->update_map, &hc, nsn,
1027                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1028     }
1029   }
1030   if (NULL != next_id)
1031   {
1032     GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1033     pc.ip = ip;
1034     pc.ip_cls = ip_cls;
1035     GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1036                                                 &process_update_node, &pc);
1037     return;
1038   }
1039   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040               "Calculating TREEs to find roots of update trees\n");
1041   /* Find heads of TREEs in update graph */
1042   nug = ++ns->nug_gen;
1043   fc.tree_array = NULL;
1044   fc.tree_array_size = 0;
1045
1046   for (i = 0; i < ns->update_node_count; i++)
1047   {
1048     nsn = ns->update_nodes[i];
1049     if (nsn->nug == nug)
1050     {
1051       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1052                   nsn->nug);
1053       continue;                 /* already placed in TREE */
1054     }
1055     GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1056     nsn->nug = nug;
1057     nsn->tree_id = UINT_MAX;
1058     fc.id = UINT_MAX;
1059     fc.nug = nug;
1060     fc.ns = ns;
1061     GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1062                                                 &find_trees, &fc);
1063     if (UINT_MAX == fc.id)
1064     {
1065       /* start new TREE */
1066       for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1067       {
1068         if (fc.tree_array[fc.id] == NULL)
1069         {
1070           fc.tree_array[fc.id] = nsn;
1071           nsn->tree_id = fc.id;
1072           break;
1073         }
1074       }
1075       if (fc.id == fc.tree_array_size)
1076       {
1077         GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1078         nsn->tree_id = fc.id;
1079       }
1080       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081                   "Starting new TREE %u with node `%s'\n", nsn->tree_id,
1082                   nsn->id);
1083       /* put all nodes with same identifier into this TREE */
1084       GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1085       fc.id = nsn->tree_id;
1086       fc.nug = nug;
1087       fc.ns = ns;
1088       GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1089                                                   &find_trees, &fc);
1090     }
1091     else
1092     {
1093       /* make head of TREE "id" */
1094       fc.tree_array[fc.id] = nsn;
1095       nsn->tree_id = fc.id;
1096     }
1097     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1098                 fc.id);
1099   }
1100   for (i = 0; i < fc.tree_array_size; i++)
1101   {
1102     nsn = fc.tree_array[i];
1103     if (NULL != nsn)
1104     {
1105       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
1106                   nsn->id);
1107       ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1108     }
1109   }
1110   GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1111   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1112 }
1113
1114
1115 /* end of fs_namespace.c */