X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ffs%2Ffs_namespace.c;h=5d52265b74c9c05916bb28053d9f33d9e5de0abf;hb=a7fbcf1c827ea25046c9f9e5c4e2a567eba72318;hp=46121aa9c2a0db121e83bfc148914680570cfa04;hpb=046e90172489ca6091346685bf444a037b1187ee;p=oweals%2Fgnunet.git diff --git a/src/fs/fs_namespace.c b/src/fs/fs_namespace.c index 46121aa9c..5d52265b7 100644 --- a/src/fs/fs_namespace.c +++ b/src/fs/fs_namespace.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet - (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors) + Copyright (C) 2003-2013 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,13 +14,14 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** * @file fs/fs_namespace.c - * @brief create and destroy namespaces + * @brief publishing to namespaces, and tracking updateable entries + * for our namespaces * @author Christian Grothoff */ #include "platform.h" @@ -29,109 +30,167 @@ #include "gnunet_util_lib.h" #include "gnunet_fs_service.h" #include "fs_api.h" +#include "fs_publish_ublock.h" /** - * Maximum legal size for an sblock. + * Information about an (updateable) node in the + * namespace. */ -#define MAX_SBLOCK_SIZE (60 * 1024) +struct NamespaceUpdateNode +{ + /** + * Identifier for this node. + */ + char *id; + + /** + * Identifier of children of this node. + */ + char *update; + + /** + * Metadata for this entry. + */ + struct GNUNET_CONTAINER_MetaData *md; + + /** + * URI of this entry in the namespace. + */ + struct GNUNET_FS_Uri *uri; + + /** + * Namespace update generation ID. Used to ensure + * freshness of the tree_id. + */ + unsigned int nug; + + /** + * TREE this entry belongs to (if nug is current). + */ + unsigned int tree_id; + +}; /** - * Context for creating a namespace asynchronously. + * Handle to update information for a namespace. */ -struct GNUNET_FS_NamespaceCreationContext +struct GNUNET_FS_UpdateInformationGraph { + /** - * Context for asynchronous key creation. + * Handle to the FS service context. */ - struct GNUNET_CRYPTO_RsaKeyGenerationContext *keycreator; + struct GNUNET_FS_Handle *h; /** - * Name of the file to store key in / read key from. + * Array with information about nodes in the namespace. */ - char *filename; + struct NamespaceUpdateNode **update_nodes; /** - * Name of the namespace. + * Private key for the namespace. */ - char *name; + struct GNUNET_CRYPTO_EcdsaPrivateKey ns; /** - * Global fs handle + * Hash map mapping identifiers of update nodes + * to the update nodes (initialized on-demand). */ - struct GNUNET_FS_Handle *h; + struct GNUNET_CONTAINER_MultiHashMap *update_map; /** - * Function to call when generation ends (successfully or not) + * Size of the update nodes array. */ - GNUNET_FS_NamespaceCreationCallback cont; + unsigned int update_node_count; /** - * Client value to pass to continuation function. + * Reference counter. */ - void *cont_cls; + unsigned int rc; + + /** + * Generator for unique nug numbers. + */ + unsigned int nug_gen; }; /** * Return the name of the directory in which we store - * our local namespaces (or rather, their public keys). + * the update information graph for the given local namespace. * - * @param h global fs handle + * @param h file-sharing handle + * @param ns namespace handle * @return NULL on error, otherwise the name of the directory */ static char * -get_namespace_directory (struct GNUNET_FS_Handle *h) +get_update_information_directory (struct GNUNET_FS_Handle *h, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns) { char *dn; + char *ret; + struct GNUNET_CRYPTO_EcdsaPublicKey pub; + struct GNUNET_HashCode hc; + struct GNUNET_CRYPTO_HashAsciiEncoded enc; if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "IDENTITY_DIR", + GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "UPDATE_DIR", &dn)) { GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "fs", "IDENTITY_DIR"); + "fs", "UPDATE_DIR"); return NULL; } - return dn; + GNUNET_CRYPTO_ecdsa_key_get_public (ns, &pub); + GNUNET_CRYPTO_hash (&pub, sizeof (pub), &hc); + GNUNET_CRYPTO_hash_to_enc (&hc, + &enc); + GNUNET_asprintf (&ret, "%s%s%s", + dn, + DIR_SEPARATOR_STR, + (const char *) enc.encoding); + GNUNET_free (dn); + return ret; } /** - * Return the name of the directory in which we store - * the update information graph for the given local namespace. + * Release memory occupied by UIG datastructure. * - * @param ns namespace handle - * @return NULL on error, otherwise the name of the directory + * @param uig data structure to free */ -static char * -get_update_information_directory (struct GNUNET_FS_Namespace *ns) +static void +free_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig) { - char *dn; - char *ret; + unsigned int i; + struct NamespaceUpdateNode *nsn; - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg, "FS", "UPDATE_DIR", - &dn)) + for (i = 0; i < uig->update_node_count; i++) { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "fs", "UPDATE_DIR"); - return NULL; + nsn = uig->update_nodes[i]; + GNUNET_CONTAINER_meta_data_destroy (nsn->md); + GNUNET_FS_uri_destroy (nsn->uri); + GNUNET_free (nsn->id); + GNUNET_free (nsn->update); + GNUNET_free (nsn); } - GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name); - GNUNET_free (dn); - return ret; + GNUNET_array_grow (uig->update_nodes, uig->update_node_count, + 0); + if (NULL != uig->update_map) + GNUNET_CONTAINER_multihashmap_destroy (uig->update_map); + GNUNET_free (uig); } /** - * Write the namespace update node graph to a file. + * Write a namespace's update node graph to a file. * - * @param ns namespace to dump + * @param uig update information graph to dump */ static void -write_update_information_graph (struct GNUNET_FS_Namespace *ns) +write_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig) { char *fn; struct GNUNET_BIO_WriteHandle *wh; @@ -139,7 +198,8 @@ write_update_information_graph (struct GNUNET_FS_Namespace *ns) struct NamespaceUpdateNode *n; char *uris; - fn = get_update_information_directory (ns); + fn = get_update_information_directory (uig->h, + &uig->ns); wh = GNUNET_BIO_write_open (fn); if (NULL == wh) { @@ -148,11 +208,11 @@ write_update_information_graph (struct GNUNET_FS_Namespace *ns) GNUNET_free (fn); return; } - if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, ns->update_node_count)) + if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, uig->update_node_count)) goto END; - for (i = 0; i < ns->update_node_count; i++) + for (i = 0; i < uig->update_node_count; i++) { - n = ns->update_nodes[i]; + n = uig->update_nodes[i]; uris = GNUNET_FS_uri_to_string (n->uri); if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) || @@ -175,11 +235,15 @@ END: /** * Read the namespace update node graph from a file. * + * @param h FS handle to use * @param ns namespace to read + * @return update graph, never NULL */ -static void -read_update_information_graph (struct GNUNET_FS_Namespace *ns) +static struct GNUNET_FS_UpdateInformationGraph * +read_update_information_graph (struct GNUNET_FS_Handle *h, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns) { + struct GNUNET_FS_UpdateInformationGraph *uig; char *fn; struct GNUNET_BIO_ReadHandle *rh; unsigned int i; @@ -188,17 +252,20 @@ read_update_information_graph (struct GNUNET_FS_Namespace *ns) uint32_t count; char *emsg; - fn = get_update_information_directory (ns); + uig = GNUNET_new (struct GNUNET_FS_UpdateInformationGraph); + uig->h = h; + uig->ns = *ns; + fn = get_update_information_directory (h, ns); if (GNUNET_YES != GNUNET_DISK_file_test (fn)) { GNUNET_free (fn); - return; + return uig; } rh = GNUNET_BIO_read_open (fn); if (NULL == rh) { GNUNET_free (fn); - return; + return uig; } if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count)) { @@ -211,17 +278,13 @@ read_update_information_graph (struct GNUNET_FS_Namespace *ns) goto END; } if (0 == count) - { - GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL)); - GNUNET_free (fn); - return; - } - ns->update_nodes = - GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *)); + goto END; + uig->update_nodes = + GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *)); for (i = 0; i < count; i++) { - n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode)); + n = GNUNET_new (struct NamespaceUpdateNode); if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024)) || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) || (GNUNET_OK != @@ -248,317 +311,18 @@ read_update_information_graph (struct GNUNET_FS_Namespace *ns) GNUNET_free (n); break; } - ns->update_nodes[i] = n; + uig->update_nodes[i] = n; } - ns->update_node_count = i; -END: + uig->update_node_count = i; + END: if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read `%s': %s\n"), + fn, emsg); GNUNET_free (emsg); } GNUNET_free (fn); -} - - -/** - * Create a namespace with the given name; if one already - * exists, return a handle to the existing namespace. - * - * @param h handle to the file sharing subsystem - * @param name name to use for the namespace - * @return handle to the namespace, NULL on error (i.e. invalid filename) - */ -struct GNUNET_FS_Namespace * -GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name) -{ - char *dn; - char *fn; - struct GNUNET_FS_Namespace *ret; - - dn = get_namespace_directory (h); - if (NULL == dn) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Can't determine where namespace directory is\n")); - return NULL; - } - GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name); - GNUNET_free (dn); - ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace)); - ret->h = h; - ret->rc = 1; - ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn); - if (NULL == ret->key) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to create or read private key for namespace `%s'\n"), - name); - GNUNET_free (ret); - GNUNET_free (fn); - return NULL; - } - ret->name = GNUNET_strdup (name); - ret->filename = fn; - return ret; -} - - -/** - * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_start'. - * - * @param cls closure - * @param pk NULL on error, otherwise the private key (which must be free'd by the callee) - * @param emsg NULL on success, otherwise an error message - */ -static void -ns_key_created (void *cls, struct GNUNET_CRYPTO_RsaPrivateKey *pk, - const char *emsg) -{ - struct GNUNET_FS_NamespaceCreationContext *ncc = cls; - - ncc->keycreator = NULL; - - if (pk) - { - struct GNUNET_FS_Namespace *ret; - ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace)); - ret->rc = 1; - ret->key = pk; - ret->h = ncc->h; - ret->name = ncc->name; - ret->filename = ncc->filename; - ncc->cont (ncc->cont_cls, ret, NULL); - } - else - { - GNUNET_free (ncc->filename); - GNUNET_free (ncc->name); - ncc->cont (ncc->cont_cls, NULL, emsg); - } - GNUNET_free (ncc); -} - - -/** - * Create a namespace with the given name. - * If one already exists, the continuation will be called with a handle to - * the existing namespace. - * Otherwise creates a new namespace. - * - * @param h handle to the file sharing subsystem - * @param name name to use for the namespace - * @return namespace creation context, NULL on error (i.e. invalid filename) - */ -struct GNUNET_FS_NamespaceCreationContext * -GNUNET_FS_namespace_create_start (struct GNUNET_FS_Handle *h, const char *name, - GNUNET_FS_NamespaceCreationCallback cont, void *cont_cls) -{ - char *dn; - char *fn; - struct GNUNET_FS_NamespaceCreationContext *ret; - - dn = get_namespace_directory (h); - if (NULL == dn) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Can't determine where namespace directory is\n")); - return NULL; - } - GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name); - GNUNET_free (dn); - - ret = GNUNET_malloc (sizeof (struct GNUNET_FS_NamespaceCreationContext)); - ret->filename = fn; - ret->h = h; - ret->name = GNUNET_strdup (name); - ret->cont = cont; - ret->cont_cls = cont_cls; - - ret->keycreator = GNUNET_CRYPTO_rsa_key_create_start (fn, - ns_key_created, ret); - - if (NULL == ret->keycreator) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to start creating or reading private key for namespace `%s'\n"), - name); - GNUNET_free (fn); - GNUNET_free (ret->name); - GNUNET_free (ret); - return NULL; - } - return ret; -} - - -/** - * Abort namespace creation. - * - * @param ncc namespace creation context to abort - */ -void -GNUNET_FS_namespace_create_stop (struct GNUNET_FS_NamespaceCreationContext *ncc) -{ - if (NULL != ncc->keycreator) - { - GNUNET_CRYPTO_rsa_key_create_stop (ncc->keycreator); - ncc->keycreator = NULL; - } - if (NULL != ncc->filename) - { - if (0 != UNLINK (ncc->filename)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", ncc->filename); - GNUNET_free (ncc->filename); - } - GNUNET_free_non_null (ncc->name); - GNUNET_free (ncc); -} - - -/** - * Duplicate a namespace handle. - * - * @param ns namespace handle - * @return duplicated handle to the namespace - */ -struct GNUNET_FS_Namespace * -GNUNET_FS_namespace_dup (struct GNUNET_FS_Namespace *ns) -{ - ns->rc++; - return ns; -} - - -/** - * Delete a namespace handle. Can be used for a clean shutdown (free - * memory) or also to freeze the namespace to prevent further - * insertions by anyone. - * - * @param ns handle to the namespace that should be deleted / freed - * @param freeze prevents future insertions; creating a namespace - * with the same name again will create a fresh namespace instead - * - * @return GNUNET_OK on success, GNUNET_SYSERR on error - */ -int -GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *ns, int freeze) -{ - unsigned int i; - struct NamespaceUpdateNode *nsn; - - ns->rc--; - if (freeze) - { - if (0 != UNLINK (ns->filename)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", - ns->filename); - } - if (0 != ns->rc) - return GNUNET_OK; - GNUNET_CRYPTO_rsa_key_free (ns->key); - GNUNET_free (ns->filename); - GNUNET_free (ns->name); - for (i = 0; i < ns->update_node_count; i++) - { - nsn = ns->update_nodes[i]; - GNUNET_CONTAINER_meta_data_destroy (nsn->md); - GNUNET_FS_uri_destroy (nsn->uri); - GNUNET_free (nsn->id); - GNUNET_free (nsn->update); - GNUNET_free (nsn); - } - GNUNET_array_grow (ns->update_nodes, ns->update_node_count, - 0); - if (ns->update_map != NULL) - GNUNET_CONTAINER_multihashmap_destroy (ns->update_map); - GNUNET_free (ns); - return GNUNET_OK; -} - - -/** - * Context for the 'process_namespace' callback. - * Specifies a function to call on each namespace. - */ -struct ProcessNamespaceContext -{ - /** - * Function to call. - */ - GNUNET_FS_NamespaceInfoProcessor cb; - - /** - * Closure for 'cb'. - */ - void *cb_cls; -}; - - -/** - * Function called with a filename of a namespace. Reads the key and - * calls the callback. - * - * @param cls closure (struct ProcessNamespaceContext) - * @param filename complete filename (absolute path) - * @return GNUNET_OK to continue to iterate, - * GNUNET_SYSERR to abort iteration with error! - */ -static int -process_namespace (void *cls, const char *filename) -{ - struct ProcessNamespaceContext *pnc = cls; - struct GNUNET_CRYPTO_RsaPrivateKey *key; - struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk; - struct GNUNET_HashCode id; - const char *name; - const char *t; - - key = GNUNET_CRYPTO_rsa_key_create_from_file (filename); - if (NULL == key) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ - ("Failed to read namespace private key file `%s', deleting it!\n"), - filename); - if (0 != UNLINK (filename)) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); - return GNUNET_OK; - } - GNUNET_CRYPTO_rsa_key_get_public (key, &pk); - GNUNET_CRYPTO_rsa_key_free (key); - GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id); - name = filename; - while (NULL != (t = strstr (name, DIR_SEPARATOR_STR))) - name = t + 1; - pnc->cb (pnc->cb_cls, name, &id); - return GNUNET_OK; -} - - -/** - * Build a list of all available local (!) namespaces The returned - * names are only the nicknames since we only iterate over the local - * namespaces. - * - * @param h handle to the file sharing subsystem - * @param cb function to call on each known namespace - * @param cb_cls closure for cb - */ -void -GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h, - GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls) -{ - char *dn; - struct ProcessNamespaceContext ctx; - - dn = get_namespace_directory (h); - if (NULL == dn) - return; - ctx.cb = cb; - ctx.cb_cls = cb_cls; - GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx); - GNUNET_free (dn); + return uig; } @@ -582,13 +346,18 @@ struct GNUNET_FS_PublishSksContext /** * Namespace we're publishing to. */ - struct GNUNET_FS_Namespace *ns; + struct GNUNET_CRYPTO_EcdsaPrivateKey ns; /** * Handle to the datastore. */ struct GNUNET_DATASTORE_Handle *dsh; + /** + * Handle to FS. + */ + struct GNUNET_FS_Handle *h; + /** * Function to call once we're done. */ @@ -600,31 +369,28 @@ struct GNUNET_FS_PublishSksContext void *cont_cls; /** - * Handle for our datastore request. + * Handle for our UBlock operation request. */ - struct GNUNET_DATASTORE_QueueEntry *dqe; + struct GNUNET_FS_PublishUblockContext *uc; }; /** - * Function called by the datastore API with - * the result from the PUT (SBlock) request. + * Function called by the UBlock construction with + * the result from the PUT (UBlock) request. * * @param cls closure of type "struct GNUNET_FS_PublishSksContext*" - * @param success GNUNET_OK on success - * @param min_expiration minimum expiration time required for content to be stored * @param msg error message (or NULL) */ static void -sb_put_cont (void *cls, int success, - struct GNUNET_TIME_Absolute min_expiration, - const char *msg) +sks_publish_cont (void *cls, + const char *msg) { struct GNUNET_FS_PublishSksContext *psc = cls; - struct GNUNET_HashCode hc; + struct GNUNET_FS_UpdateInformationGraph *uig; - psc->dqe = NULL; - if (GNUNET_OK != success) + psc->uc = NULL; + if (NULL != msg) { if (NULL != psc->cont) psc->cont (psc->cont_cls, NULL, msg); @@ -636,19 +402,14 @@ sb_put_cont (void *cls, int success, /* FIXME: this can be done much more * efficiently by simply appending to the * file and overwriting the 4-byte header */ - if (psc->ns->update_nodes == NULL) - read_update_information_graph (psc->ns); - GNUNET_array_append (psc->ns->update_nodes, - psc->ns->update_node_count, psc->nsn); - if (psc->ns->update_map != NULL) - { - GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc); - GNUNET_CONTAINER_multihashmap_put (psc->ns->update_map, &hc, - psc->nsn, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); - } + uig = read_update_information_graph (psc->h, + &psc->ns); + GNUNET_array_append (uig->update_nodes, + uig->update_node_count, + psc->nsn); psc->nsn = NULL; - write_update_information_graph (psc->ns); + write_update_information_graph (uig); + free_update_information_graph (uig); } if (NULL != psc->cont) psc->cont (psc->cont_cls, psc->uri, NULL); @@ -673,7 +434,7 @@ sb_put_cont (void *cls, int success, */ struct GNUNET_FS_PublishSksContext * GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h, - struct GNUNET_FS_Namespace *ns, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns, const char *identifier, const char *update, const struct GNUNET_CONTAINER_MetaData *meta, const struct GNUNET_FS_Uri *uri, @@ -682,131 +443,49 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h, GNUNET_FS_PublishContinuation cont, void *cont_cls) { struct GNUNET_FS_PublishSksContext *psc; - struct GNUNET_CRYPTO_AesSessionKey sk; - struct GNUNET_CRYPTO_AesInitializationVector iv; struct GNUNET_FS_Uri *sks_uri; - char *uris; - size_t size; - size_t slen; - size_t nidlen; - size_t idlen; - ssize_t mdsize; - struct SBlock *sb; - struct SBlock *sb_enc; - char *dest; - struct GNUNET_CONTAINER_MetaData *mmeta; - struct GNUNET_HashCode key; /* hash of thisId = key */ - struct GNUNET_HashCode id; /* hash of hc = identifier */ - struct GNUNET_HashCode query; /* id ^ nsid = DB query */ - - idlen = strlen (identifier); - if (NULL != update) - nidlen = strlen (update) + 1; - else - nidlen = 1; - uris = GNUNET_FS_uri_to_string (uri); - slen = strlen (uris) + 1; - if ( (slen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock)) || - (nidlen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock) - slen) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Identifiers or URI too long to create SBlock")); - GNUNET_free (uris); - return NULL; - } - if (NULL == meta) - mmeta = GNUNET_CONTAINER_meta_data_create (); - else - mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta); - mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta); - size = sizeof (struct SBlock) + slen + nidlen + mdsize; - if ( (size > MAX_SBLOCK_SIZE) || - (size < sizeof (struct SBlock) + slen + nidlen) ) - { - size = MAX_SBLOCK_SIZE; - mdsize = MAX_SBLOCK_SIZE - (sizeof (struct SBlock) + slen + nidlen); - } - sb = GNUNET_malloc (sizeof (struct SBlock) + size); - dest = (char *) &sb[1]; - if (NULL != update) - memcpy (dest, update, nidlen); - else - memset (dest, 0, 1); - dest += nidlen; - memcpy (dest, uris, slen); - GNUNET_free (uris); - dest += slen; - mdsize = - GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize, - GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); - GNUNET_CONTAINER_meta_data_destroy (mmeta); - if (-1 == mdsize) - { - GNUNET_break (0); - GNUNET_free (sb); - if (NULL != cont) - cont (cont_cls, NULL, _("Internal error.")); - return NULL; - } - size = sizeof (struct SBlock) + mdsize + slen + nidlen; - sb_enc = GNUNET_malloc (size); - GNUNET_CRYPTO_hash (identifier, idlen, &key); - GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &id); - sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri)); + + sks_uri = GNUNET_new (struct GNUNET_FS_Uri); sks_uri->type = GNUNET_FS_URI_SKS; - GNUNET_CRYPTO_rsa_key_get_public (ns->key, &sb_enc->subspace); - GNUNET_CRYPTO_hash (&sb_enc->subspace, - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), - &sks_uri->data.sks.ns); sks_uri->data.sks.identifier = GNUNET_strdup (identifier); - GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.ns, - &sb_enc->identifier); - GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv); - GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv, - &sb_enc[1]); - sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK); - sb_enc->purpose.size = - htonl (slen + mdsize + nidlen + sizeof (struct SBlock) - - sizeof (struct GNUNET_CRYPTO_RsaSignature)); - GNUNET_assert (GNUNET_OK == - GNUNET_CRYPTO_rsa_sign (ns->key, &sb_enc->purpose, - &sb_enc->signature)); - psc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishSksContext)); + GNUNET_CRYPTO_ecdsa_key_get_public (ns, + &sks_uri->data.sks.ns); + + psc = GNUNET_new (struct GNUNET_FS_PublishSksContext); + psc->h = h; psc->uri = sks_uri; psc->cont = cont; - psc->ns = GNUNET_FS_namespace_dup (ns); psc->cont_cls = cont_cls; - if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) - { - GNUNET_free (sb_enc); - GNUNET_free (sb); - sb_put_cont (psc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL); - return NULL; - } - psc->dsh = GNUNET_DATASTORE_connect (h->cfg); - if (NULL == psc->dsh) + psc->ns = *ns; + if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) { - GNUNET_free (sb_enc); - GNUNET_free (sb); - sb_put_cont (psc, GNUNET_NO, GNUNET_TIME_UNIT_ZERO_ABS, _("Failed to connect to datastore.")); - return NULL; + psc->dsh = GNUNET_DATASTORE_connect (h->cfg); + if (NULL == psc->dsh) + { + sks_publish_cont (psc, + _("Failed to connect to datastore.")); + return NULL; + } } - GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.ns, &id, &query); if (NULL != update) { - psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode)); + psc->nsn = GNUNET_new (struct NamespaceUpdateNode); psc->nsn->id = GNUNET_strdup (identifier); psc->nsn->update = GNUNET_strdup (update); psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta); psc->nsn->uri = GNUNET_FS_uri_dup (uri); } - psc->dqe = GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc, - GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority, - bo->anonymity_level, bo->replication_level, - bo->expiration_time, -2, 1, - GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc); - GNUNET_free (sb); - GNUNET_free (sb_enc); + psc->uc = GNUNET_FS_publish_ublock_ (h, + psc->dsh, + identifier, + update, + ns, + meta, + uri, + bo, + options, + &sks_publish_cont, + psc); return psc; } @@ -819,17 +498,16 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h, void GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc) { - if (NULL != psc->dqe) + if (NULL != psc->uc) { - GNUNET_DATASTORE_cancel (psc->dqe); - psc->dqe = NULL; + GNUNET_FS_publish_ublock_cancel_ (psc->uc); + psc->uc = NULL; } if (NULL != psc->dsh) { GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO); psc->dsh = NULL; } - GNUNET_FS_namespace_delete (psc->ns, GNUNET_NO); GNUNET_FS_uri_destroy (psc->uri); if (NULL != psc->nsn) { @@ -871,12 +549,18 @@ struct ProcessUpdateClosure * GNUNET_NO if not. */ static int -process_update_node (void *cls, const struct GNUNET_HashCode * key, void *value) +process_update_node (void *cls, + const struct GNUNET_HashCode *key, + void *value) { struct ProcessUpdateClosure *pc = cls; struct NamespaceUpdateNode *nsn = value; - pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update); + pc->ip (pc->ip_cls, + nsn->id, + nsn->uri, + nsn->md, + nsn->update); return GNUNET_YES; } @@ -887,9 +571,9 @@ process_update_node (void *cls, const struct GNUNET_HashCode * key, void *value) struct FindTreeClosure { /** - * Namespace we are operating on. + * UIG we are operating on. */ - struct GNUNET_FS_Namespace *ns; + struct GNUNET_FS_UpdateInformationGraph *uig; /** * Array with 'head's of TREEs. @@ -934,7 +618,9 @@ struct FindTreeClosure * GNUNET_NO if not. */ static int -find_trees (void *cls, const struct GNUNET_HashCode * key, void *value) +find_trees (void *cls, + const struct GNUNET_HashCode *key, + void *value) { struct FindTreeClosure *fc = cls; struct NamespaceUpdateNode *nsn = value; @@ -961,7 +647,7 @@ find_trees (void *cls, const struct GNUNET_HashCode * key, void *value) nsn->tree_id = UINT_MAX; /* mark as undef */ /* trace */ GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc); - GNUNET_CONTAINER_multihashmap_get_multiple (fc->ns->update_map, &hc, + GNUNET_CONTAINER_multihashmap_get_multiple (fc->uig->update_map, &hc, &find_trees, fc); } return GNUNET_YES; @@ -985,13 +671,15 @@ find_trees (void *cls, const struct GNUNET_HashCode * key, void *value) * I know, odd definition of a tree, but the GUI will display an actual * tree (GtkTreeView), so that's what counts for the term here. * + * @param h fs handle to use * @param ns namespace to inspect for updateable content * @param next_id ID to look for; use NULL to look for tree roots * @param ip function to call on each updateable identifier * @param ip_cls closure for ip */ void -GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, +GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Handle *h, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns, const char *next_id, GNUNET_FS_IdentifierProcessor ip, void *ip_cls) @@ -1002,50 +690,48 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, struct NamespaceUpdateNode *nsn; struct ProcessUpdateClosure pc; struct FindTreeClosure fc; + struct GNUNET_FS_UpdateInformationGraph *uig; - if (NULL == ns->update_nodes) - read_update_information_graph (ns); - if (NULL == ns->update_nodes) + uig = read_update_information_graph (h, ns); + if (NULL == uig->update_nodes) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No updateable nodes found for ID `%s'\n", next_id); + free_update_information_graph (uig); return; /* no nodes */ } - if (NULL == ns->update_map) + uig->update_map = + GNUNET_CONTAINER_multihashmap_create (2 + + 3 * uig->update_node_count / + 4, + GNUNET_NO); + for (i = 0; i < uig->update_node_count; i++) { - /* need to construct */ - ns->update_map = - GNUNET_CONTAINER_multihashmap_create (2 + - 3 * ns->update_node_count / - 4, - GNUNET_NO); - for (i = 0; i < ns->update_node_count; i++) - { - nsn = ns->update_nodes[i]; - GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc); - GNUNET_CONTAINER_multihashmap_put (ns->update_map, &hc, nsn, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); - } + nsn = uig->update_nodes[i]; + GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc); + GNUNET_CONTAINER_multihashmap_put (uig->update_map, &hc, nsn, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); } if (NULL != next_id) { GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc); pc.ip = ip; pc.ip_cls = ip_cls; - GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc, + GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc, &process_update_node, &pc); + free_update_information_graph (uig); return; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calculating TREEs to find roots of update trees\n"); /* Find heads of TREEs in update graph */ - nug = ++ns->nug_gen; + nug = ++uig->nug_gen; fc.tree_array = NULL; fc.tree_array_size = 0; - for (i = 0; i < ns->update_node_count; i++) + for (i = 0; i < uig->update_node_count; i++) { - nsn = ns->update_nodes[i]; + nsn = uig->update_nodes[i]; if (nsn->nug == nug) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id, @@ -1057,15 +743,15 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, nsn->tree_id = UINT_MAX; fc.id = UINT_MAX; fc.nug = nug; - fc.ns = ns; - GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc, + fc.uig = uig; + GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc, &find_trees, &fc); if (UINT_MAX == fc.id) { /* start new TREE */ for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++) { - if (fc.tree_array[fc.id] == NULL) + if (NULL == fc.tree_array[fc.id]) { fc.tree_array[fc.id] = nsn; nsn->tree_id = fc.id; @@ -1084,8 +770,8 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc); fc.id = nsn->tree_id; fc.nug = nug; - fc.ns = ns; - GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc, + fc.uig = uig; + GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc, &find_trees, &fc); } else @@ -1094,7 +780,8 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, fc.tree_array[fc.id] = nsn; nsn->tree_id = fc.id; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id, + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "TREE of node `%s' is %u\n", nsn->id, fc.id); } for (i = 0; i < fc.tree_array_size; i++) @@ -1109,6 +796,7 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns, } GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n"); + free_update_information_graph (uig); }