-/**
- * Context for the KSK publication.
- */
-struct PublishKskContext
-{
-
- /**
- * Keywords to use.
- */
- struct GNUNET_FS_Uri *ksk_uri;
-
- /**
- * Global FS context.
- */
- struct GNUNET_FS_Handle *h;
-
- /**
- * The master block that we are sending
- * (in plaintext), has "mdsize+slen" more
- * bytes than the struct would suggest.
- */
- struct KBlock *kb;
-
- /**
- * Buffer of the same size as "kb" for
- * the encrypted version.
- */
- struct KBlock *cpy;
-
- /**
- * Handle to the datastore, NULL if we are just
- * simulating.
- */
- struct GNUNET_DATASTORE_Handle *dsh;
-
- /**
- * Function to call once we're done.
- */
- GNUNET_FS_PublishContinuation cont;
-
- /**
- * Closure for cont.
- */
- void *cont_cls;
-
- /**
- * When should the KBlocks expire?
- */
- struct GNUNET_TIME_Absolute expirationTime;
-
- /**
- * Size of the serialized metadata.
- */
- ssize_t mdsize;
-
- /**
- * Size of the (CHK) URI as a string.
- */
- size_t slen;
-
- /**
- * Keyword that we are currently processing.
- */
- unsigned int i;
-
- /**
- * Anonymity level for the KBlocks.
- */
- uint32_t anonymity;
-
- /**
- * Priority for the KBlocks.
- */
- uint32_t priority;
-};
-
-
-/**
- * Continuation of "GNUNET_FS_publish_ksk" that performs
- * the actual publishing operation (iterating over all
- * of the keywords).
- *
- * @param cls closure of type "struct PublishKskContext*"
- * @param tc unused
- */
-static void
-publish_ksk_cont (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-/**
- * Function called by the datastore API with
- * the result from the PUT request.
- *
- * @param cls closure of type "struct PublishKskContext*"
- * @param success GNUNET_OK on success
- * @param msg error message (or NULL)
- */
-static void
-kb_put_cont (void *cls,
- int success,
- const char *msg)
-{
- struct PublishKskContext *pkc = cls;
-
- if (GNUNET_OK != success)
- {
- GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
- GNUNET_free (pkc->cpy);
- GNUNET_free (pkc->kb);
- pkc->cont (pkc->cont_cls,
- NULL,
- msg);
- GNUNET_FS_uri_destroy (pkc->ksk_uri);
- GNUNET_free (pkc);
- return;
- }
- GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
- &publish_ksk_cont,
- pkc,
- GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-}
-
-
-/**
- * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
- * publishing operation (iterating over all of the keywords).
- *
- * @param cls closure of type "struct PublishKskContext*"
- * @param tc unused
- */
-static void
-publish_ksk_cont (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct PublishKskContext *pkc = cls;
- const char *keyword;
- GNUNET_HashCode key;
- GNUNET_HashCode query;
- struct GNUNET_CRYPTO_AesSessionKey skey;
- struct GNUNET_CRYPTO_AesInitializationVector iv;
- struct GNUNET_CRYPTO_RsaPrivateKey *pk;
-
-
- if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
- (NULL == pkc->dsh) )
- {
- if (NULL != pkc->dsh)
- GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
- GNUNET_free (pkc->cpy);
- GNUNET_free (pkc->kb);
- pkc->cont (pkc->cont_cls,
- pkc->ksk_uri,
- NULL);
- GNUNET_FS_uri_destroy (pkc->ksk_uri);
- GNUNET_free (pkc);
- return;
- }
- keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
- /* first character of keyword indicates if it is
- mandatory or not -- ignore for hashing */
- GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
- GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
- GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
- pkc->slen + pkc->mdsize,
- &skey,
- &iv,
- &pkc->cpy[1]);
- pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
- GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
- GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
- sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
- &query);
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CRYPTO_rsa_sign (pk,
- &pkc->cpy->purpose,
- &pkc->cpy->signature));
- GNUNET_CRYPTO_rsa_key_free (pk);
- GNUNET_DATASTORE_put (pkc->dsh,
- 0,
- &query,
- pkc->mdsize +
- sizeof (struct KBlock) +
- pkc->slen,
- pkc->cpy,
- GNUNET_BLOCK_TYPE_KBLOCK,
- pkc->priority,
- pkc->anonymity,
- pkc->expirationTime,
- GNUNET_CONSTANTS_SERVICE_TIMEOUT,
- &kb_put_cont,
- pkc);
-}
-
-
-/**
- * Publish a CHK under various keywords on GNUnet.
- *
- * @param h handle to the file sharing subsystem
- * @param ksk_uri keywords to use
- * @param meta metadata to use
- * @param uri URI to refer to in the KBlock
- * @param expirationTime when the KBlock expires
- * @param anonymity anonymity level for the KBlock
- * @param priority priority for the KBlock
- * @param options publication options
- * @param cont continuation
- * @param cont_cls closure for cont
- */
-void
-GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
- const struct GNUNET_FS_Uri *ksk_uri,
- const struct GNUNET_CONTAINER_MetaData *meta,
- const struct GNUNET_FS_Uri *uri,
- struct GNUNET_TIME_Absolute expirationTime,
- uint32_t anonymity,
- uint32_t priority,
- enum GNUNET_FS_PublishOptions options,
- GNUNET_FS_PublishContinuation cont,
- void *cont_cls)
-{
- struct PublishKskContext *pkc;
- char *uris;
- size_t size;
- char *kbe;
- char *sptr;
-
- pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
- pkc->h = h;
- pkc->expirationTime = expirationTime;
- pkc->anonymity = anonymity;
- pkc->priority = priority;
- pkc->cont = cont;
- pkc->cont_cls = cont_cls;
- if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
- {
- pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
- h->sched);
- if (pkc->dsh == NULL)
- {
- cont (cont_cls, NULL, _("Could not connect to datastore."));
- GNUNET_free (pkc);
- return;
- }
- }
- if (meta == NULL)
- pkc->mdsize = 0;
- else
- pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
- GNUNET_assert (pkc->mdsize >= 0);
- uris = GNUNET_FS_uri_to_string (uri);
- pkc->slen = strlen (uris) + 1;
- size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
- if (size > MAX_KBLOCK_SIZE)
- {
- size = MAX_KBLOCK_SIZE;
- pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
- }
- pkc->kb = GNUNET_malloc (size);
- kbe = (char *) &pkc->kb[1];
- memcpy (kbe, uris, pkc->slen);
- GNUNET_free (uris);
- sptr = &kbe[pkc->slen];
- if (meta != NULL)
- pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
- &sptr,
- pkc->mdsize,
- GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
- if (pkc->mdsize == -1)
- {
- GNUNET_break (0);
- GNUNET_free (pkc->kb);
- if (pkc->dsh != NULL)
- GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
- cont (cont_cls, NULL, _("Internal error."));
- GNUNET_free (pkc);
- return;
- }
- size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
-
- pkc->cpy = GNUNET_malloc (size);
- pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
- sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
- pkc->mdsize +
- pkc->slen);
- pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
- pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
- GNUNET_SCHEDULER_add_continuation (h->sched,
- &publish_ksk_cont,
- pkc,
- GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-}
-
-
-/**
- * Context for the SKS publication.
- */
-struct PublishSksContext
-{
-
- /**
- * Global FS context.
- */
- struct GNUNET_FS_Uri *uri;
-
- /**
- * Handle to the datastore.
- */
- struct GNUNET_DATASTORE_Handle *dsh;
-
- /**
- * Function to call once we're done.
- */
- GNUNET_FS_PublishContinuation cont;
-
- /**
- * Closure for cont.
- */
- void *cont_cls;
-
-};
-
-
-/**
- * Function called by the datastore API with
- * the result from the PUT (SBlock) request.
- *
- * @param cls closure of type "struct PublishSksContext*"
- * @param success GNUNET_OK on success
- * @param msg error message (or NULL)
- */
-static void
-sb_put_cont (void *cls,
- int success,
- const char *msg)
-{
- struct PublishSksContext *psc = cls;
-
- if (NULL != psc->dsh)
- GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
- if (GNUNET_OK != success)
- psc->cont (psc->cont_cls,
- NULL,
- msg);
- else
- psc->cont (psc->cont_cls,
- psc->uri,
- NULL);
- GNUNET_FS_uri_destroy (psc->uri);
- GNUNET_free (psc);
-}
-
-
-/**
- * Publish an SBlock on GNUnet.
- *
- * @param h handle to the file sharing subsystem
- * @param namespace namespace to publish in
- * @param identifier identifier to use
- * @param update update identifier to use
- * @param meta metadata to use
- * @param uri URI to refer to in the SBlock
- * @param expirationTime when the SBlock expires
- * @param anonymity anonymity level for the SBlock
- * @param priority priority for the SBlock
- * @param options publication options
- * @param cont continuation
- * @param cont_cls closure for cont
- */
-void
-GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
- struct GNUNET_FS_Namespace *namespace,
- const char *identifier,
- const char *update,
- const struct GNUNET_CONTAINER_MetaData *meta,
- const struct GNUNET_FS_Uri *uri,
- struct GNUNET_TIME_Absolute expirationTime,
- uint32_t anonymity,
- uint32_t priority,
- enum GNUNET_FS_PublishOptions options,
- GNUNET_FS_PublishContinuation cont,
- void *cont_cls)
-{
- struct 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;
- GNUNET_HashCode key; /* hash of thisId = key */
- GNUNET_HashCode id; /* hash of hc = identifier */
- GNUNET_HashCode query; /* id ^ nsid = DB query */
-
- if (NULL == meta)
- mmeta = GNUNET_CONTAINER_meta_data_create ();
- else
- mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
- uris = GNUNET_FS_uri_to_string (uri);
- slen = strlen (uris) + 1;
- idlen = strlen (identifier);
- if (update == NULL)
- update = "";
- nidlen = strlen (update) + 1;
- mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
- size = sizeof (struct SBlock) + slen + nidlen + mdsize;
- if (size > MAX_SBLOCK_SIZE)
- {
- size = MAX_SBLOCK_SIZE;
- mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
- }
- sb = GNUNET_malloc (sizeof (struct SBlock) + size);
- dest = (char *) &sb[1];
- memcpy (dest, update, nidlen);
- dest += nidlen;
- memcpy (dest, uris, slen);
- dest += slen;
- mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
- &dest,
- mdsize,
- GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
- GNUNET_CONTAINER_meta_data_destroy (mmeta);
- if (mdsize == -1)
- {
- GNUNET_break (0);
- GNUNET_free (uris);
- GNUNET_free (sb);
- cont (cont_cls,
- NULL,
- _("Internal error."));
- return;
- }
- size = sizeof (struct SBlock) + mdsize + slen + nidlen;
- sb_enc = GNUNET_malloc (size);
- GNUNET_CRYPTO_hash (identifier, idlen, &key);
- GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
- sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
- sks_uri->type = sks;
- GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
- GNUNET_CRYPTO_hash (&sb_enc->subspace,
- sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
- &sks_uri->data.sks.namespace);
- sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
- GNUNET_CRYPTO_hash_xor (&id,
- &sks_uri->data.sks.namespace,
- &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 (namespace->key,
- &sb_enc->purpose,
- &sb_enc->signature));
- psc = GNUNET_malloc (sizeof(struct PublishSksContext));
- psc->uri = sks_uri;
- psc->cont = cont;
- 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,
- NULL);
- return;
- }
- psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
- if (NULL == psc->dsh)
- {
- GNUNET_free (sb_enc);
- GNUNET_free (sb);
- sb_put_cont (psc,
- GNUNET_NO,
- _("Failed to connect to datastore."));
- return;
- }
- GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
- &id,
- &query);
- GNUNET_DATASTORE_put (psc->dsh,
- 0,
- &sb_enc->identifier,
- size,
- sb_enc,
- GNUNET_BLOCK_TYPE_SBLOCK,
- priority,
- anonymity,
- expirationTime,
- GNUNET_CONSTANTS_SERVICE_TIMEOUT,
- &sb_put_cont,
- psc);
-
- GNUNET_free (sb);
- GNUNET_free (sb_enc);
-}