2 This file is part of GNUnet
3 (C) 2012, 2013 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 include/gnunet_namestore_service.h
23 * @brief API that can be used to store naming information on a GNUnet node;
24 * Naming information can either be records for which this peer/user
25 * is authoritative, or blocks which are cached, encrypted naming
26 * data from other peers.
27 * @author Christian Grothoff
29 #ifndef GNUNET_NAMESTORE_SERVICE_H
30 #define GNUNET_NAMESTORE_SERVICE_H
32 #include "gnunet_util_lib.h"
33 #include "gnunet_block_lib.h"
38 #if 0 /* keep Emacsens' auto-indent happy */
44 * Record type indicating any record/'*'
46 #define GNUNET_NAMESTORE_TYPE_ANY 0
49 * Record type for GNS zone transfer ("PKEY").
51 #define GNUNET_NAMESTORE_TYPE_PKEY 65536
54 * Record type for GNS zone transfer ("PSEU").
56 #define GNUNET_NAMESTORE_TYPE_PSEU 65537
59 * Record type for GNS legacy hostnames ("LEHO").
61 #define GNUNET_NAMESTORE_TYPE_LEHO 65538
64 * Record type for VPN resolution
66 #define GNUNET_NAMESTORE_TYPE_VPN 65539
69 * Record type for zone revocation
71 #define GNUNET_NAMESTORE_TYPE_REV 65540
74 * Record type for a social place.
76 #define GNUNET_NAMESTORE_TYPE_PLACE 65541
81 struct GNUNET_NAMESTORE_QueueEntry;
84 * Handle to the namestore service.
86 struct GNUNET_NAMESTORE_Handle;
89 * Handle to the namestore zone iterator.
91 struct GNUNET_NAMESTORE_ZoneIterator;
94 * Maximum size of a value that can be stored in the namestore.
96 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
100 * Connect to the namestore service.
102 * @param cfg configuration to use
103 * @return handle to use to access the service
105 struct GNUNET_NAMESTORE_Handle *
106 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
110 * Disconnect from the namestore service (and free associated
113 * @param h handle to the namestore
116 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
120 * Continuation called to notify client about result of the
124 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
125 * GNUNET_NO if content was already there or not found
126 * GNUNET_YES (or other positive value) on success
127 * @param emsg NULL on success, otherwise an error message
129 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
135 * Flags that can be set for a record.
137 enum GNUNET_NAMESTORE_RecordFlags
141 * No special options.
143 GNUNET_NAMESTORE_RF_NONE = 0,
146 * This peer is the authority for this record; it must thus
147 * not be deleted (other records can be deleted if we run
150 GNUNET_NAMESTORE_RF_AUTHORITY = 1,
153 * This is a private record of this peer and it should
154 * thus not be handed out to other peers.
156 GNUNET_NAMESTORE_RF_PRIVATE = 2,
159 * This record was added by the system
160 * and is pending user confimation
162 GNUNET_NAMESTORE_RF_PENDING = 4,
165 * This expiration time of the record is a relative
166 * time (not an absolute time).
168 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
171 * This record should not be used unless all (other) records with an absolute
172 * expiration time have expired.
174 GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
177 * When comparing flags for record equality for removal,
178 * which flags should must match (in addition to the type,
179 * name, expiration value and data of the record)? All flags
180 * that are not listed here will be ignored for this purpose.
181 * (for example, we don't expect that users will remember to
182 * pass the '--private' option when removing a record from
183 * the namestore, hence we don't require this particular option
184 * to match upon removal). See also
185 * 'GNUNET_NAMESTORE_records_cmp'.
187 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
194 struct GNUNET_NAMESTORE_RecordData
198 * Binary value stored in the DNS record.
199 * FIXME: goofy API: sometimes 'data' is individually
200 * 'malloc'ed, sometimes it points into some existing
201 * data area (so sometimes this should be a 'void *',
202 * sometimes a 'const void *'). This is unclean. We
203 * should go over the code and make sure that 'data'
204 * is NEVER individually alloc'ed here but always points
205 * to some "other" location.
210 * Expiration time for the DNS record. Can be relative
211 * or absolute, depending on 'flags'.
213 uint64_t expiration_time;
216 * Number of bytes in 'data'.
221 * Type of the GNS/DNS record.
223 uint32_t record_type;
226 * Flags for the record.
228 enum GNUNET_NAMESTORE_RecordFlags flags;
233 GNUNET_NETWORK_STRUCT_BEGIN
237 * Information we have in an encrypted block with record data (i.e. in the DHT).
239 struct GNUNET_NAMESTORE_Block
243 * Signature of the block.
245 struct GNUNET_CRYPTO_EccSignature signature;
248 * Derived key used for signing; hash of this is the query.
250 struct GNUNET_CRYPTO_EccPublicKey derived_key;
253 * Number of bytes signed; also specifies the number of bytes
254 * of encrypted data that follow.
256 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
259 * Expiration time of the block.
261 struct GNUNET_TIME_AbsoluteNBO expiration_time;
263 /* followed by encrypted data */
266 GNUNET_NETWORK_STRUCT_END
269 * Store an item in the namestore. If the item is already present,
270 * it is replaced with the new record.
272 * @param h handle to the namestore
273 * @param block block to store
274 * @param cont continuation to call when done
275 * @param cont_cls closure for cont
276 * @return handle to abort the request
278 struct GNUNET_NAMESTORE_QueueEntry *
279 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
280 const struct GNUNET_NAMESTORE_Block *block,
281 GNUNET_NAMESTORE_ContinuationWithStatus cont,
286 * Store an item in the namestore. If the item is already present,
287 * it is replaced with the new record. Use an empty array to
288 * remove all records under the given name.
290 * @param h handle to the namestore
291 * @param pkey private key of the zone
292 * @param label name that is being mapped (at most 255 characters long)
293 * @param rd_count number of records in the 'rd' array
294 * @param rd array of records with data to store
295 * @param cont continuation to call when done
296 * @param cont_cls closure for 'cont'
297 * @return handle to abort the request
299 struct GNUNET_NAMESTORE_QueueEntry *
300 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
301 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
303 unsigned int rd_count,
304 const struct GNUNET_NAMESTORE_RecordData *rd,
305 GNUNET_NAMESTORE_ContinuationWithStatus cont,
310 * Process a record that was stored in the namestore.
313 * @param block block that was stored in the namestore
315 typedef void (*GNUNET_NAMESTORE_BlockProcessor) (void *cls,
316 const struct GNUNET_NAMESTORE_Block *block);
320 * Get a result for a particular key from the namestore. The processor
321 * will only be called once.
323 * @param h handle to the namestore
324 * @param derived_hash hash of zone key combined with name to lookup
325 * @param proc function to call on the matching block, or with
326 * NULL if there is no matching block
327 * @param proc_cls closure for proc
328 * @return a handle that can be used to cancel
330 struct GNUNET_NAMESTORE_QueueEntry *
331 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
332 const struct GNUNET_HashCode *derived_hash,
333 GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls);
337 * Process a record that was stored in the namestore.
340 * @param zone private key of the zone
341 * @param label label of the records
342 * @param rd_count number of entries in 'rd' array
343 * @param rd array of records with data to store
345 typedef void (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
346 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
348 unsigned int rd_count,
349 const struct GNUNET_NAMESTORE_RecordData *rd);
353 * Look for an existing PKEY delegation record for a given public key.
354 * Returns at most one result to the processor.
356 * @param h handle to the namestore
357 * @param zone public key of the zone to look up in, never NULL
358 * @param value_zone public key of the target zone (value), never NULL
359 * @param proc function to call on the matching records, or with
360 * NULL (rd_count == 0) if there are no matching records
361 * @param proc_cls closure for proc
362 * @return a handle that can be used to
365 struct GNUNET_NAMESTORE_QueueEntry *
366 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
367 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
368 const struct GNUNET_CRYPTO_EccPublicKey *value_zone,
369 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
373 * Process a records that were decrypted from a block.
376 * @param rd_count number of entries in @a rd array
377 * @param rd array of records with data to store
379 typedef void (*GNUNET_NAMESTORE_RecordCallback) (void *cls,
380 unsigned int rd_count,
381 const struct GNUNET_NAMESTORE_RecordData *rd);
385 * Perform a lookup and decrypt the resulting block.
387 * @param h namestore to perform lookup in
388 * @param value_zone zone to look up record in
389 * @param label label to look for
390 * @param proc function to call with the result
391 * @param proc_cls closure for @a proc
393 struct GNUNET_NAMESTORE_QueueEntry *
394 GNUNET_NAMESTORE_lookup (struct GNUNET_NAMESTORE_Handle *h,
395 const struct GNUNET_CRYPTO_EccPublicKey *value_zone,
397 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
401 * Cancel a namestore operation. The final callback from the
402 * operation must not have been done yet. Must be called on any
403 * namestore operation that has not yet completed prior to calling
404 * #GNUNET_NAMESTORE_disconnect.
406 * @param qe operation to cancel
409 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
413 * Starts a new zone iteration (used to periodically PUT all of our
414 * records into our DHT). @a proc will be called once immediately, and
415 * then again after #GNUNET_NAMESTORE_zone_iterator_next is invoked.
417 * @param h handle to the namestore
418 * @param zone zone to access
419 * @param proc function to call on each name from the zone; it
420 * will be called repeatedly with a value (if available)
421 * and always once at the end with a name of NULL.
422 * @param proc_cls closure for proc
423 * @return an iterator handle to use for iteration
425 struct GNUNET_NAMESTORE_ZoneIterator *
426 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
427 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
428 GNUNET_NAMESTORE_RecordMonitor proc,
433 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
434 * for the next record.
436 * @param it the iterator
439 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
443 * Stops iteration and releases the namestore handle for further calls. Must
444 * be called on any iteration that has not yet completed prior to calling
445 * #GNUNET_NAMESTORE_disconnect.
447 * @param it the iterator
450 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
454 * Handle for a monitoring activity.
456 struct GNUNET_NAMESTORE_ZoneMonitor;
460 * Function called once the monitor has caught up with the current
461 * state of the database. Will be called AGAIN after each disconnect
462 * (record monitor called with 'NULL' for zone_key) once we're again
467 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
471 * Begin monitoring a zone for changes. Will first call the 'monitor' function
472 * on all existing records in the selected zone(s), then calls 'sync_cb',
473 * and then calls the 'monitor' whenever a record changes. If the namestore
474 * disconnects, the 'monitor' function is called with a disconnect event; if
475 * the connection is re-established, the process begins from the start (all
476 * existing records, sync, then updates).
478 * @param cfg configuration to use to connect to namestore
479 * @param zone zone to monitor
480 * @param monitor function to call on zone changes
481 * @param sync_cb function called when we're in sync with the namestore
482 * @param cls closure for 'monitor' and 'sync_cb'
483 * @return handle to stop monitoring
485 struct GNUNET_NAMESTORE_ZoneMonitor *
486 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
487 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
488 GNUNET_NAMESTORE_RecordMonitor monitor,
489 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
494 * Stop monitoring a zone for changes.
496 * @param zm handle to the monitor activity to stop
499 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
502 /* convenience APIs for serializing / deserializing GNS records */
505 * Calculate how many bytes we will need to serialize the given
508 * @param rd_count number of records in the rd array
509 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
511 * @return the required size to serialize
515 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
516 const struct GNUNET_NAMESTORE_RecordData *rd);
520 * Serialize the given records to the given destination buffer.
522 * @param rd_count number of records in the rd array
523 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
524 * @param dest_size size of the destination array
525 * @param dest where to write the result
526 * @return the size of serialized records, -1 if records do not fit
529 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
530 const struct GNUNET_NAMESTORE_RecordData *rd,
536 * Deserialize the given records to the given destination.
538 * @param len size of the serialized record data
539 * @param src the serialized record data
540 * @param rd_count number of records in the rd array
541 * @param dest where to put the data
543 * @return GNUNET_OK on success, GNUNET_SYSERR on error
546 GNUNET_NAMESTORE_records_deserialize (size_t len,
548 unsigned int rd_count,
549 struct GNUNET_NAMESTORE_RecordData *dest);
553 * Convert the 'value' of a record to a string.
555 * @param type type of the record
556 * @param data value in binary encoding
557 * @param data_size number of bytes in data
558 * @return NULL on error, otherwise human-readable representation of the value
561 GNUNET_NAMESTORE_value_to_string (uint32_t type,
567 * Convert human-readable version of a 'value' of a record to the binary
570 * @param type type of the record
571 * @param s human-readable string
572 * @param data set to value in binary encoding (will be allocated)
573 * @param data_size set to number of bytes in data
574 * @return GNUNET_OK on success
577 GNUNET_NAMESTORE_string_to_value (uint32_t type,
584 * Convert a type name (i.e. "AAAA") to the corresponding number.
586 * @param typename name to convert
587 * @return corresponding number, UINT32_MAX on error
590 GNUNET_NAMESTORE_typename_to_number (const char *typename);
594 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
596 * @param type number of a type to convert
597 * @return corresponding typestring, NULL on error
600 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
604 * Test if a given record is expired.
606 * @param rd record to test
607 * @return #GNUNET_YES if the record is expired,
611 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
615 * Convert a UTF-8 string to UTF-8 lowercase
616 * @param src source string
617 * @return converted result
620 GNUNET_NAMESTORE_normalize_string (const char *src);
624 * Convert a zone to a string (for printing debug messages).
625 * This is one of the very few calls in the entire API that is
628 * @param z public key of a zone
629 * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s.
632 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicKey *z);
636 * Calculate the DHT query for a given @a label in a given @a zone.
638 * @param zone private key of the zone
639 * @param label label of the record
640 * @param query hash to use for the query
643 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
645 struct GNUNET_HashCode *query);
649 * Calculate the DHT query for a given @a label in a given @a zone.
651 * @param pub public key of the zone
652 * @param label label of the record
653 * @param query hash to use for the query
656 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicKey *pub,
658 struct GNUNET_HashCode *query);
662 * Sign name and records
664 * @param key the private key
665 * @param expire block expiration
666 * @param label the name for the records
667 * @param rd record data
668 * @param rd_count number of records
670 struct GNUNET_NAMESTORE_Block *
671 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
672 struct GNUNET_TIME_Absolute expire,
674 const struct GNUNET_NAMESTORE_RecordData *rd,
675 unsigned int rd_count);
679 * Check if a signature is valid. This API is used by the GNS Block
680 * to validate signatures received from the network.
682 * @param block block to verify
683 * @return #GNUNET_OK if the signature is valid
686 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block);
692 * @param block block to decrypt
693 * @param zone_key public key of the zone
694 * @param label the name for the records
695 * @param proc function to call with the result
696 * @param proc_cls closure for @a proc
697 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
701 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
702 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
704 GNUNET_NAMESTORE_RecordCallback proc,
709 * Compares if two records are equal
712 * @param b another record
713 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
716 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
717 const struct GNUNET_NAMESTORE_RecordData *b);
721 * Returns the expiration time of the given block of records. The block
722 * expiration time is the expiration time of the block with smallest
725 * @param rd_count number of records given in 'rd'
726 * @param rd array of records
727 * @return absolute expiration time
729 struct GNUNET_TIME_Absolute
730 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count,
731 const struct GNUNET_NAMESTORE_RecordData *rd);
734 #if 0 /* keep Emacsens' auto-indent happy */
741 /* end of gnunet_namestore_service.h */