2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 2, 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 fs/fs_namespace.c
23 * @brief create and destroy namespaces
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
34 * Return the name of the directory in which we store
35 * our local namespaces (or rather, their public keys).
37 * @param h global fs handle
38 * @return NULL on error, otherwise the name of the directory
41 get_namespace_directory (struct GNUNET_FS_Handle *h)
46 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 _("Configuration fails to specify `%s' in section `%s'\n"),
62 * Context for advertising a namespace.
64 struct AdvertisementContext
67 * Function to call with the result.
69 GNUNET_FS_PublishContinuation cont;
79 struct GNUNET_DATASTORE_Handle *dsh;
82 * URI that was created.
84 struct GNUNET_FS_Uri *uri;
89 * Continuation called to notify client about result of the
92 * @param cls closure (our struct AdvertismentContext)
93 * @param success GNUNET_SYSERR on failure
94 * @param msg NULL on success, otherwise an error message
97 advertisement_cont (void *cls,
101 struct AdvertisementContext *ac = cls;
103 if (GNUNET_OK != success)
104 ac->cont (ac->cont_cls, NULL, msg);
106 ac->cont (ac->cont_cls, ac->uri, NULL);
107 GNUNET_DATASTORE_disconnect (ac->dsh, GNUNET_NO);
108 GNUNET_FS_uri_destroy (ac->uri);
114 * Publish an advertismement for a namespace.
116 * @param h handle to the file sharing subsystem
117 * @param namespace handle for the namespace that should be advertised
118 * @param meta meta-data for the namespace advertisement
119 * @param anonymity for the namespace advertismement
120 * @param priority for the namespace advertisement
121 * @param expiration for the namespace advertisement
122 * @param rootEntry name of the root of the namespace
123 * @param cont continuation
124 * @param cont_cls closure for cont
127 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
128 struct GNUNET_FS_Namespace *namespace,
129 const struct GNUNET_CONTAINER_MetaData *meta,
132 struct GNUNET_TIME_Absolute expiration,
133 const char *rootEntry,
134 GNUNET_FS_PublishContinuation cont,
143 struct GNUNET_DATASTORE_Handle *dsh;
144 struct AdvertisementContext *ctx;
146 /* create advertisements */
147 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
150 cont (cont_cls, NULL, _("Failed to serialize meta data"));
153 reslen = strlen (rootEntry) + 1;
154 size = mdsize + sizeof (struct NBlock) + reslen;
155 if (size > MAX_NBLOCK_SIZE)
157 size = MAX_NBLOCK_SIZE;
158 mdsize = size - sizeof (struct NBlock) - reslen;
160 nb = GNUNET_malloc (size);
161 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
162 rtgt = (char *) &nb[1];
163 memcpy (rtgt, rootEntry, reslen);
164 mdst = &rtgt[reslen];
165 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
168 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
173 cont (cont_cls, NULL, _("Failed to serialize meta data"));
176 size = mdsize + sizeof (struct NBlock) + reslen;
177 nb->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
178 nb->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
179 GNUNET_break (GNUNET_OK ==
180 GNUNET_CRYPTO_rsa_sign (namespace->key,
183 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
187 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
190 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
192 ctx->cont_cls = cont_cls;
194 ctx->uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
195 ctx->uri->type = sks;
196 ctx->uri->data.sks.identifier = GNUNET_strdup ("");
197 GNUNET_CRYPTO_hash (&nb->subspace,
198 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
199 &ctx->uri->data.sks.namespace);
200 GNUNET_DATASTORE_put (dsh,
202 &ctx->uri->data.sks.namespace,
205 GNUNET_DATASTORE_BLOCKTYPE_NBLOCK,
209 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
216 * Create a namespace with the given name; if one already
217 * exists, return a handle to the existing namespace.
219 * @param h handle to the file sharing subsystem
220 * @param name name to use for the namespace
221 * @return handle to the namespace, NULL on error
223 struct GNUNET_FS_Namespace *
224 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
229 struct GNUNET_FS_Namespace *ret;
231 dn = get_namespace_directory (h);
232 GNUNET_asprintf (&fn,
237 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
239 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
240 if (ret->key == NULL)
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 _("Failed to create or read private key for namespace `%s'\n"),
255 * Delete a namespace handle. Can be used for a clean shutdown (free
256 * memory) or also to freeze the namespace to prevent further
257 * insertions by anyone.
259 * @param namespace handle to the namespace that should be deleted / freed
260 * @param freeze prevents future insertions; creating a namespace
261 * with the same name again will create a fresh namespace instead
263 * @return GNUNET_OK on success, GNUNET_SYSERR on error
266 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
272 if (0 != UNLINK (namespace->filename))
273 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
275 namespace->filename);
277 if (0 == namespace->rc)
279 GNUNET_CRYPTO_rsa_key_free (namespace->key);
280 GNUNET_free (namespace->filename);
281 GNUNET_free (namespace);
288 * Context for the 'process_namespace' callback.
289 * Specifies a function to call on each namespace.
291 struct ProcessNamespaceContext
296 GNUNET_FS_NamespaceInfoProcessor cb;
306 * Function called with a filename of a namespace. Reads the key and
307 * calls the callback.
309 * @param cls closure (struct ProcessNamespaceContext)
310 * @param filename complete filename (absolute path)
311 * @return GNUNET_OK to continue to iterate,
312 * GNUNET_SYSERR to abort iteration with error!
315 process_namespace (void *cls,
316 const char *filename)
318 struct ProcessNamespaceContext *pnc = cls;
319 struct GNUNET_CRYPTO_RsaPrivateKey *key;
320 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
325 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 _("Failed to read namespace private key file `%s', deleting it!\n"),
331 if (0 != UNLINK (filename))
332 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
337 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
338 GNUNET_CRYPTO_rsa_key_free (key);
339 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
341 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
343 pnc->cb (pnc->cb_cls,
351 * Build a list of all available local (!) namespaces The returned
352 * names are only the nicknames since we only iterate over the local
355 * @param h handle to the file sharing subsystem
356 * @param cb function to call on each known namespace
357 * @param cb_cls closure for cb
360 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
361 GNUNET_FS_NamespaceInfoProcessor cb,
365 struct ProcessNamespaceContext ctx;
367 dn = get_namespace_directory (h);
372 GNUNET_DISK_directory_scan (dn,
378 /* end of fs_namespace.c */