2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 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 3, 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_advertise.c
23 * @brief advertise namespaces (creating NBlocks)
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"
35 * Maximum legal size for an nblock.
37 #define MAX_NBLOCK_SIZE (60 * 1024)
41 * Context for advertising a namespace.
43 struct GNUNET_FS_AdvertisementContext
46 * Function to call with the result.
48 GNUNET_FS_PublishContinuation cont;
58 struct GNUNET_DATASTORE_Handle *dsh;
63 struct GNUNET_FS_Uri *ksk_uri;
71 * NBlock to sign and store.
78 struct GNUNET_FS_Namespace *ns;
81 * Current datastore queue entry for advertising.
83 struct GNUNET_DATASTORE_QueueEntry *dqe;
88 struct GNUNET_FS_BlockOptions bo;
91 * Number of bytes of plaintext.
96 * Current keyword offset.
102 // FIXME: I see no good reason why this should need to be done
103 // in a new task (anymore). Integrate with 'cancel' function below?
105 * Disconnect from the datastore.
107 * @param cls datastore handle
108 * @param tc scheduler context
111 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct GNUNET_DATASTORE_Handle *dsh = cls;
115 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
120 * Continuation called to notify client about result of the
123 * @param cls closure (our struct GNUNET_FS_AdvertismentContext)
124 * @param success GNUNET_SYSERR on failure
125 * @param min_expiration minimum expiration time required for content to be stored
126 * @param msg NULL on success, otherwise an error message
129 advertisement_cont (void *cls, int success,
130 struct GNUNET_TIME_Absolute min_expiration,
133 struct GNUNET_FS_AdvertisementContext *ac = cls;
136 GNUNET_HashCode query;
137 struct GNUNET_CRYPTO_AesSessionKey skey;
138 struct GNUNET_CRYPTO_AesInitializationVector iv;
139 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
142 if (GNUNET_SYSERR == success)
145 (void) GNUNET_SCHEDULER_add_now (&do_disconnect, ac->dsh);
150 msg = _("Unknown error");
152 if (ac->cont != NULL)
154 ac->cont (ac->cont_cls, NULL, msg);
157 GNUNET_FS_namespace_advertise_cancel (ac);
160 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
163 (void) GNUNET_SCHEDULER_add_now (&do_disconnect, ac->dsh);
165 if (ac->cont != NULL)
167 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
170 GNUNET_FS_namespace_advertise_cancel (ac);
173 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
174 /* first character of keyword indicates if it is
175 * mandatory or not -- ignore for hashing */
176 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
177 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
178 GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
179 GNUNET_break (GNUNET_OK ==
180 GNUNET_CRYPTO_rsa_sign (ac->ns->key, &ac->nb->ns_purpose,
181 &ac->nb->ns_signature));
182 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
183 GNUNET_assert (pk != NULL);
184 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
185 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
186 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
188 GNUNET_break (GNUNET_OK ==
189 GNUNET_CRYPTO_rsa_sign (pk, &ac->nb->ksk_purpose,
190 &ac->nb->ksk_signature));
191 GNUNET_CRYPTO_rsa_key_free (pk);
192 ac->dqe = GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
193 &query, ac->pt_size + sizeof (struct NBlock), ac->nb,
194 GNUNET_BLOCK_TYPE_FS_NBLOCK, ac->bo.content_priority,
195 ac->bo.anonymity_level, ac->bo.replication_level,
196 ac->bo.expiration_time, -2, 1,
197 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &advertisement_cont,
203 * Publish an advertismement for a namespace.
205 * @param h handle to the file sharing subsystem
206 * @param ksk_uri keywords to use for advertisment
207 * @param namespace handle for the namespace that should be advertised
208 * @param meta meta-data for the namespace advertisement
209 * @param bo block options
210 * @param rootEntry name of the root of the namespace
211 * @param cont continuation
212 * @param cont_cls closure for cont
213 * @return NULL on error ('cont' is still called)
215 struct GNUNET_FS_AdvertisementContext *
216 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
217 struct GNUNET_FS_Uri *ksk_uri,
218 struct GNUNET_FS_Namespace *namespace,
219 const struct GNUNET_CONTAINER_MetaData *meta,
220 const struct GNUNET_FS_BlockOptions *bo,
221 const char *rootEntry,
222 GNUNET_FS_PublishContinuation cont,
230 struct GNUNET_DATASTORE_Handle *dsh;
231 struct GNUNET_FS_AdvertisementContext *ctx;
234 /* create advertisements */
235 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
238 cont (cont_cls, NULL, _("Failed to serialize meta data"));
241 reslen = strlen (rootEntry) + 1;
242 size = mdsize + sizeof (struct NBlock) + reslen;
243 if (size > MAX_NBLOCK_SIZE)
245 size = MAX_NBLOCK_SIZE;
246 mdsize = size - sizeof (struct NBlock) - reslen;
249 pt = GNUNET_malloc (mdsize + reslen);
250 memcpy (pt, rootEntry, reslen);
253 GNUNET_CONTAINER_meta_data_serialize (meta, &mdst, mdsize,
254 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
259 cont (cont_cls, NULL, _("Failed to serialize meta data"));
262 size = mdsize + sizeof (struct NBlock) + reslen;
263 nb = GNUNET_malloc (size);
264 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
265 nb->ns_purpose.size =
266 htonl (mdsize + reslen +
267 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
268 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
269 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
270 nb->ksk_purpose.size =
271 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
272 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
273 dsh = GNUNET_DATASTORE_connect (h->cfg);
278 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
281 ctx = GNUNET_malloc (sizeof (struct GNUNET_FS_AdvertisementContext));
283 ctx->cont_cls = cont_cls;
285 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
288 ctx->pt_size = mdsize + reslen;
292 advertisement_cont (ctx, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
298 * Abort the namespace advertisement operation.
300 * @param ac context of the operation to abort.
303 GNUNET_FS_namespace_advertise_cancel (struct GNUNET_FS_AdvertisementContext *ac)
307 GNUNET_DATASTORE_cancel (ac->dqe);
312 GNUNET_DATASTORE_disconnect (ac->dsh, GNUNET_NO);
315 GNUNET_FS_uri_destroy (ac->ksk_uri);
316 GNUNET_free (ac->pt);
317 GNUNET_free (ac->nb);
318 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
323 /* end of fs_namespace_advertise.c */