2 This file is part of GNUnet.
3 (C) 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 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 gns/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_namestore_service.h"
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 struct GNUNET_NAMESTORE_QueueEntry
43 char *data; /*stub data pointer*/
47 * Connection to the NAMESTORE service.
49 struct GNUNET_NAMESTORE_Handle
53 * Configuration to use.
55 const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * Socket (if available).
60 struct GNUNET_CLIENT_Connection *client;
63 * Currently pending transmission request (or NULL).
65 struct GNUNET_CLIENT_TransmitHandle *th;
67 /* dll to use for records */
68 struct GNUNET_NAMESTORE_SimpleRecord * records_head;
69 struct GNUNET_NAMESTORE_SimpleRecord * records_tail;
73 struct GNUNET_NAMESTORE_SimpleRecord
78 struct GNUNET_NAMESTORE_SimpleRecord *next;
83 struct GNUNET_NAMESTORE_SimpleRecord *prev;
86 const GNUNET_HashCode *zone;
88 struct GNUNET_TIME_Absolute expiration;
89 enum GNUNET_NAMESTORE_RecordFlags flags;
95 * Initialize the connection with the NAMESTORE service.
97 * @param cfg configuration to use
98 * @return handle to the GNS service, or NULL on error
100 struct GNUNET_NAMESTORE_Handle *
101 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
103 struct GNUNET_NAMESTORE_Handle *handle;
105 handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
112 * Shutdown connection with the NAMESTORE service.
114 * @param handle handle of the NAMESTORE connection to stop
117 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
123 * Sign a record. This function is used by the authority of the zone
126 * @param h handle to the namestore
127 * @param zone_privkey private key of the zone
128 * @param record_hash hash of the record to be signed
129 * @param cont continuation to call when done
130 * @param cont_cls closure for cont
131 * @return handle to abort the request
133 struct GNUNET_NAMESTORE_QueueEntry *
134 GNUNET_NAMESTORE_stree_extend (struct GNUNET_NAMESTORE_Handle *h,
135 const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
136 const GNUNET_HashCode *record_hash,
137 GNUNET_NAMESTORE_ContinuationWithSignature cont,
140 struct GNUNET_NAMESTORE_QueueEntry *qe;
141 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
146 * Rebalance the signature tree of our zone. This function should
147 * be called "rarely" to rebalance the tree.
149 * @param h handle to the namestore
150 * @param zone_privkey private key for the zone to rebalance
151 * @param cont continuation to call when done
152 * @param cont_cls closure for cont
153 * @return handle to abort the request
155 struct GNUNET_NAMESTORE_QueueEntry *
156 GNUNET_NAMESTORE_stree_rebalance (struct GNUNET_NAMESTORE_Handle *h,
157 const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
158 GNUNET_NAMESTORE_ContinuationWithStatus cont,
161 struct GNUNET_NAMESTORE_QueueEntry *qe;
162 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
167 * Provide the root of a signature tree. This function is
168 * used by non-authorities as the first operation when
169 * adding a foreign zone.
171 * @param h handle to the namestore
172 * @param zone_key public key of the zone
173 * @param signature signature of the top-level entry of the zone
174 * @param revision revision number of the zone
175 * @param top_hash top-level hash of the zone
176 * @param cont continuation to call when done
177 * @param cont_cls closure for cont
178 * @return handle to abort the request
180 struct GNUNET_NAMESTORE_QueueEntry *
181 GNUNET_NAMESTORE_stree_start (struct GNUNET_NAMESTORE_Handle *h,
182 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
183 const struct GNUNET_CRYPTO_RsaSignature *signature,
185 const GNUNET_HashCode *top_hash,
186 GNUNET_NAMESTORE_ContinuationWithSignature cont,
189 struct GNUNET_NAMESTORE_QueueEntry *qe;
190 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
195 * Store part of a signature B-tree in the namestore. This function
196 * is used by non-authorities to cache parts of a zone's signature tree.
197 * Note that the tree must be build top-down. This function must check
198 * that the nodes being added are valid, and if not refuse the operation.
200 * @param h handle to the namestore
201 * @param zone_key public key of the zone
202 * @param loc location in the B-tree
203 * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root
204 * @param top_sig signature at the top, NULL if 'loc.depth > 0'
205 * @param num_entries number of entries at this node in the B-tree
206 * @param entries the 'num_entries' entries to store (hashes over the
208 * @param cont continuation to call when done
209 * @param cont_cls closure for cont
210 * @return handle to abort the request
212 struct GNUNET_NAMESTORE_QueueEntry *
213 GNUNET_NAMESTORE_stree_put (struct GNUNET_NAMESTORE_Handle *h,
214 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
215 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
216 const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
217 const struct GNUNET_CRYPTO_RsaSignature *sig,
218 unsigned int num_entries,
219 const GNUNET_HashCode *entries,
220 GNUNET_NAMESTORE_ContinuationWithStatus cont,
223 struct GNUNET_NAMESTORE_QueueEntry *qe;
224 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
229 * Store an item in the namestore. If the item is already present,
230 * the expiration time is updated to the max of the existing time and
231 * the new time. The operation must fail if there is no matching
232 * entry in the signature tree.
234 * @param h handle to the namestore
235 * @param zone hash of the public key of the zone
236 * @param name name that is being mapped (at most 255 characters long)
237 * @param record_type type of the record (A, AAAA, PKEY, etc.)
238 * @param expiration expiration time for the content
239 * @param flags flags for the content
240 * @param sig_loc where is the information about the signature for this record stored?
241 * @param data_size number of bytes in data
242 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and
243 * GNS specification for GNS extensions)
244 * @param cont continuation to call when done
245 * @param cont_cls closure for cont
246 * @return handle to abort the request
248 struct GNUNET_NAMESTORE_QueueEntry *
249 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
250 const GNUNET_HashCode *zone,
252 uint32_t record_type,
253 struct GNUNET_TIME_Absolute expiration,
254 enum GNUNET_NAMESTORE_RecordFlags flags,
255 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
258 GNUNET_NAMESTORE_ContinuationWithStatus cont,
261 struct GNUNET_NAMESTORE_QueueEntry *qe;
262 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
264 struct GNUNET_NAMESTORE_SimpleRecord *sr;
265 sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
267 sr->record_type = record_type;
268 sr->expiration = expiration;
270 sr->data_size = data_size;
272 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
277 * Explicitly remove some content from the database. The
278 * "cont"inuation will be called with status "GNUNET_OK" if content
279 * was removed, "GNUNET_NO" if no matching entry was found and
280 * "GNUNET_SYSERR" on all other types of errors.
282 * @param h handle to the namestore
283 * @param zone hash of the public key of the zone
284 * @param name name that is being mapped (at most 255 characters long)
285 * @param record_type type of the record (A, AAAA, PKEY, etc.)
286 * @param size number of bytes in data
287 * @param data content stored
288 * @param cont continuation to call when done
289 * @param cont_cls closure for cont
290 * @return handle to abort the request
292 struct GNUNET_NAMESTORE_QueueEntry *
293 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
294 const GNUNET_HashCode *zone,
296 uint32_t record_type,
299 GNUNET_NAMESTORE_ContinuationWithStatus cont,
302 struct GNUNET_NAMESTORE_QueueEntry *qe;
303 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
305 struct GNUNET_NAMESTORE_SimpleRecord *iter;
306 for (iter=h->records_head; iter != NULL; iter=iter->next)
308 if (strcmp ( iter->name, name ) &&
309 iter->record_type == record_type &&
310 GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
314 GNUNET_CONTAINER_DLL_remove(h->records_head,
322 * Get a result for a particular key from the namestore. The processor
323 * will only be called once.
325 * @param h handle to the namestore
326 * @param zone zone to look up a record from
327 * @param name name to look up
328 * @param record_type desired record type
329 * @param proc function to call on each matching value;
330 * will be called once with a NULL value at the end
331 * @param proc_cls closure for proc
332 * @return a handle that can be used to
335 struct GNUNET_NAMESTORE_QueueEntry *
336 GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h,
337 const GNUNET_HashCode *zone,
339 uint32_t record_type,
340 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
342 struct GNUNET_NAMESTORE_QueueEntry *qe;
343 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
345 struct GNUNET_NAMESTORE_SimpleRecord *iter;
346 for (iter=h->records_head; iter != NULL; iter=iter->next)
348 proc(proc_cls, iter->zone, iter->name, iter->record_type,
352 iter->data_size /*size*/,
353 iter->data /* data */);
355 proc(proc_cls, zone, name, record_type,
356 GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
363 * Get the hash of a record (what will be signed in the Stree for
366 * @param zone hash of the public key of the zone
367 * @param name name that is being mapped (at most 255 characters long)
368 * @param record_type type of the record (A, AAAA, PKEY, etc.)
369 * @param expiration expiration time for the content
370 * @param flags flags for the content
371 * @param data_size number of bytes in data
372 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and.
373 * GNS specification for GNS extensions)
374 * @param record_hash hash of the record (set)
377 GNUNET_NAMESTORE_record_hash (struct GNUNET_NAMESTORE_Handle *h,
378 const GNUNET_HashCode *zone,
380 uint32_t record_type,
381 struct GNUNET_TIME_Absolute expiration,
382 enum GNUNET_NAMESTORE_RecordFlags flags,
385 GNUNET_HashCode *record_hash)
387 char* teststring = "namestore-stub";
388 GNUNET_CRYPTO_hash(teststring, strlen(teststring), record_hash);
392 * Obtain part of a signature B-tree. The processor
393 * will only be called once.
395 * @param h handle to the namestore
396 * @param zone zone to look up a record from
397 * @param sig_loc location to look up
398 * @param proc function to call on each matching value;
399 * will be called once with a NULL value at the end
400 * @param proc_cls closure for proc
401 * @return a handle that can be used to
404 struct GNUNET_NAMESTORE_QueueEntry *
405 GNUNET_NAMESTORE_lookup_stree (struct GNUNET_NAMESTORE_Handle *h,
406 const GNUNET_HashCode *zone,
407 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
408 GNUNET_NAMESTORE_StreeProcessor proc, void *proc_cls)
410 struct GNUNET_NAMESTORE_QueueEntry *qe;
411 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
417 * Get all records of a zone.
419 * @param h handle to the namestore
420 * @param zone zone to access
421 * @param proc function to call on a random value; it
422 * will be called repeatedly with a value (if available)
423 * and always once at the end with a zone and name of NULL.
424 * @param proc_cls closure for proc
425 * @return a handle that can be used to
428 struct GNUNET_NAMESTORE_QueueEntry *
429 GNUNET_NAMESTORE_zone_transfer (struct GNUNET_NAMESTORE_Handle *h,
430 const GNUNET_HashCode *zone,
431 GNUNET_NAMESTORE_RecordProcessor proc,
434 struct GNUNET_NAMESTORE_QueueEntry *qe;
435 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
443 * Cancel a namestore operation. The final callback from the
444 * operation must not have been done yet.
446 * @param qe operation to cancel
449 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
455 /* end of namestore_api.c */