3 This file is part of GNUnet
4 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
23 * @file include/gnunet_namestore_service.h
24 * @brief API that can be used to store naming information on a GNUnet node;
25 * Naming information can either be records for which this peer/user
26 * is authoritative, or blocks which are cached, encrypted naming
27 * data from other peers.
28 * @author Christian Grothoff
30 #ifndef GNUNET_NAMESTORE_SERVICE_H
31 #define GNUNET_NAMESTORE_SERVICE_H
33 #include "gnunet_util_lib.h"
34 #include "gnunet_block_lib.h"
39 #if 0 /* keep Emacsens' auto-indent happy */
45 * Record type indicating any record/'*'
47 #define GNUNET_NAMESTORE_TYPE_ANY 0
50 * Record type for GNS zone transfer ("PKEY").
52 #define GNUNET_NAMESTORE_TYPE_PKEY 65536
55 * Record type for GNS zone transfer ("PSEU").
57 #define GNUNET_NAMESTORE_TYPE_PSEU 65537
60 * Record type for GNS legacy hostnames ("LEHO").
62 #define GNUNET_NAMESTORE_TYPE_LEHO 65538
65 * Record type for VPN resolution
67 #define GNUNET_NAMESTORE_TYPE_VPN 65539
70 * Record type for delegation to DNS.
72 #define GNUNET_NAMESTORE_TYPE_GNS2DNS 65540
75 * Record type for a social place.
77 #define GNUNET_NAMESTORE_TYPE_PLACE 65541
80 * Record type for a phone (of CONVERSATION).
82 #define GNUNET_NAMESTORE_TYPE_PHONE 65542
88 struct GNUNET_NAMESTORE_QueueEntry;
91 * Handle to the namestore service.
93 struct GNUNET_NAMESTORE_Handle;
96 * Handle to the namestore zone iterator.
98 struct GNUNET_NAMESTORE_ZoneIterator;
101 * Maximum size of a value that can be stored in the namestore.
103 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
107 * Connect to the namestore service.
109 * @param cfg configuration to use
110 * @return handle to use to access the service
112 struct GNUNET_NAMESTORE_Handle *
113 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
117 * Disconnect from the namestore service (and free associated
118 * resources). Must not be called from within operation callbacks of
121 * @param h handle to the namestore
124 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
128 * Continuation called to notify client about result of the
132 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
133 * #GNUNET_NO if content was already there or not found
134 * #GNUNET_YES (or other positive value) on success
135 * @param emsg NULL on success, otherwise an error message
137 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
143 * Flags that can be set for a record.
145 enum GNUNET_NAMESTORE_RecordFlags
149 * No special options.
151 GNUNET_NAMESTORE_RF_NONE = 0,
154 * This is a private record of this peer and it should
155 * thus not be handed out to other peers.
157 GNUNET_NAMESTORE_RF_PRIVATE = 2,
160 * This record was added automatically by the system
161 * and is pending user confimation.
163 GNUNET_NAMESTORE_RF_PENDING = 4,
166 * This expiration time of the record is a relative
167 * time (not an absolute time).
169 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
172 * This record should not be used unless all (other) records with an absolute
173 * expiration time have expired.
175 GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
178 * When comparing flags for record equality for removal,
179 * which flags should must match (in addition to the type,
180 * name, expiration value and data of the record)? All flags
181 * that are not listed here will be ignored for this purpose.
182 * (for example, we don't expect that users will remember to
183 * pass the '--private' option when removing a record from
184 * the namestore, hence we don't require this particular option
185 * to match upon removal). See also
186 * #GNUNET_NAMESTORE_records_cmp.
188 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
195 struct GNUNET_NAMESTORE_RecordData
199 * Binary value stored in the DNS record. Note: "data" must never
200 * be individually 'malloc'ed, but instead always points into some
201 * existing data area.
206 * Expiration time for the DNS record. Can be relative
207 * or absolute, depending on 'flags'. Measured in the same
208 * unit as GNUnet time (microseconds).
210 uint64_t expiration_time;
213 * Number of bytes in 'data'.
218 * Type of the GNS/DNS record.
220 uint32_t record_type;
223 * Flags for the record.
225 enum GNUNET_NAMESTORE_RecordFlags flags;
230 GNUNET_NETWORK_STRUCT_BEGIN
234 * Information we have in an encrypted block with record data (i.e. in the DHT).
236 struct GNUNET_NAMESTORE_Block
240 * Signature of the block.
242 struct GNUNET_CRYPTO_EccSignature signature;
245 * Derived key used for signing; hash of this is the query.
247 struct GNUNET_CRYPTO_EccPublicSignKey derived_key;
250 * Number of bytes signed; also specifies the number of bytes
251 * of encrypted data that follow.
253 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
256 * Expiration time of the block.
258 struct GNUNET_TIME_AbsoluteNBO expiration_time;
260 /* followed by encrypted data */
265 GNUNET_NETWORK_STRUCT_END
268 * Store an item in the namestore. If the item is already present,
269 * it is replaced with the new record.
271 * @param h handle to the namestore
272 * @param block block to store
273 * @param cont continuation to call when done
274 * @param cont_cls closure for @a cont
275 * @return handle to abort the request
277 struct GNUNET_NAMESTORE_QueueEntry *
278 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
279 const struct GNUNET_NAMESTORE_Block *block,
280 GNUNET_NAMESTORE_ContinuationWithStatus cont,
285 * Store an item in the namestore. If the item is already present,
286 * it is replaced with the new record. Use an empty array to
287 * remove all records under the given name.
289 * @param h handle to the namestore
290 * @param pkey private key of the zone
291 * @param label name that is being mapped (at most 255 characters long)
292 * @param rd_count number of records in the 'rd' array
293 * @param rd array of records with data to store
294 * @param cont continuation to call when done
295 * @param cont_cls closure for @a cont
296 * @return handle to abort the request
298 struct GNUNET_NAMESTORE_QueueEntry *
299 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
300 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
302 unsigned int rd_count,
303 const struct GNUNET_NAMESTORE_RecordData *rd,
304 GNUNET_NAMESTORE_ContinuationWithStatus cont,
309 * Process a record that was stored in the namestore.
312 * @param block block that was stored in the namestore
314 typedef void (*GNUNET_NAMESTORE_BlockProcessor) (void *cls,
315 const struct GNUNET_NAMESTORE_Block *block);
319 * Get a result for a particular key from the namestore. The processor
320 * will only be called once.
322 * @param h handle to the namestore
323 * @param derived_hash hash of zone key combined with name to lookup
324 * then at the end once with NULL
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 @a 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; NULL on disconnect
341 * @param label label of the records; NULL on disconnect
342 * @param rd_count number of entries in @a rd array, 0 if label was deleted
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 @a 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_EccPublicSignKey *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 * Cancel a namestore operation. The final callback from the
386 * operation must not have been done yet. Must be called on any
387 * namestore operation that has not yet completed prior to calling
388 * #GNUNET_NAMESTORE_disconnect.
390 * @param qe operation to cancel
393 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
397 * Starts a new zone iteration (used to periodically PUT all of our
398 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
399 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
400 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
401 * immediately, and then again after
402 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
404 * @param h handle to the namestore
405 * @param zone zone to access, NULL for all zones
406 * @param proc function to call on each name from the zone; it
407 * will be called repeatedly with a value (if available)
408 * and always once at the end with a name of NULL.
409 * @param proc_cls closure for @a proc
410 * @return an iterator handle to use for iteration
412 struct GNUNET_NAMESTORE_ZoneIterator *
413 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
414 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
415 GNUNET_NAMESTORE_RecordMonitor proc,
420 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
421 * for the next record.
423 * @param it the iterator
426 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
430 * Stops iteration and releases the namestore handle for further calls. Must
431 * be called on any iteration that has not yet completed prior to calling
432 * #GNUNET_NAMESTORE_disconnect.
434 * @param it the iterator
437 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
441 * Handle for a monitoring activity.
443 struct GNUNET_NAMESTORE_ZoneMonitor;
447 * Function called once the monitor has caught up with the current
448 * state of the database. Will be called AGAIN after each disconnect
449 * (record monitor called with 'NULL' for zone_key) once we're again
454 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
458 * Begin monitoring a zone for changes. Will first call the @a monitor function
459 * on all existing records in the selected zone(s), then calls @a sync_cb,
460 * and then calls the @a monitor whenever a record changes. If the namestore
461 * disconnects, the @a monitor function is called with a disconnect event; if
462 * the connection is re-established, the process begins from the start (all
463 * existing records, sync, then updates).
465 * @param cfg configuration to use to connect to namestore
466 * @param zone zone to monitor
467 * @param monitor function to call on zone changes
468 * @param sync_cb function called when we're in sync with the namestore
469 * @param cls closure for @a monitor and @a sync_cb
470 * @return handle to stop monitoring
472 struct GNUNET_NAMESTORE_ZoneMonitor *
473 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
474 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
475 GNUNET_NAMESTORE_RecordMonitor monitor,
476 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
481 * Stop monitoring a zone for changes.
483 * @param zm handle to the monitor activity to stop
486 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
489 /* convenience APIs for serializing / deserializing GNS records */
492 * Calculate how many bytes we will need to serialize the given
495 * @param rd_count number of records in the @a rd array
496 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
497 * @return the required size to serialize
500 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
501 const struct GNUNET_NAMESTORE_RecordData *rd);
505 * Serialize the given records to the given destination buffer.
507 * @param rd_count number of records in the @a rd array
508 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
509 * @param dest_size size of the destination array @a dst
510 * @param dest where to write the result
511 * @return the size of serialized records, -1 if records do not fit
514 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
515 const struct GNUNET_NAMESTORE_RecordData *rd,
521 * Deserialize the given records to the given destination.
523 * @param len size of the serialized record data
524 * @param src the serialized record data
525 * @param rd_count number of records in the @a dest array
526 * @param dest where to put the data
527 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
530 GNUNET_NAMESTORE_records_deserialize (size_t len,
532 unsigned int rd_count,
533 struct GNUNET_NAMESTORE_RecordData *dest);
537 * Convert the binary value @a data of a record of
538 * type @a type to a human-readable string.
540 * @param type type of the record
541 * @param data value in binary encoding
542 * @param data_size number of bytes in @a data
543 * @return NULL on error, otherwise human-readable representation of the value
546 GNUNET_NAMESTORE_value_to_string (uint32_t type,
552 * Convert human-readable version of the value @a s of a record
553 * of type @a type to the respective binary representation.
555 * @param type type of the record
556 * @param s human-readable string
557 * @param data set to value in binary encoding (will be allocated)
558 * @param data_size set to number of bytes in @a data
559 * @return #GNUNET_OK on success
562 GNUNET_NAMESTORE_string_to_value (uint32_t type,
569 * Convert a type name (i.e. "AAAA") to the corresponding number.
571 * @param dns_typename name to convert
572 * @return corresponding number, UINT32_MAX on error
575 GNUNET_NAMESTORE_typename_to_number (const char *dns_typename);
579 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
581 * @param type number of a type to convert
582 * @return corresponding typestring, NULL on error
585 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
589 * Test if a given record is expired.
591 * @param rd record to test
592 * @return #GNUNET_YES if the record is expired,
596 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
600 * Convert a UTF-8 string to UTF-8 lowercase
601 * @param src source string
602 * @return converted result
605 GNUNET_NAMESTORE_normalize_string (const char *src);
609 * Convert a zone to a string (for printing debug messages).
610 * This is one of the very few calls in the entire API that is
613 * @param z public key of a zone
614 * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s.
617 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicSignKey *z);
621 * Convert public key to the respective absolute domain name in the
623 * This is one of the very few calls in the entire API that is
626 * @param pkey a public key with a point on the eliptic curve
627 * @return string "X.zkey" where X is the coordinates of the public
628 * key in an encoding suitable for DNS labels.
631 GNUNET_NAMESTORE_pkey_to_zkey (const struct GNUNET_CRYPTO_EccPublicSignKey *pkey);
635 * Convert an absolute domain name in the ".zkey" pTLD to the
636 * respective public key.
638 * @param zkey string "X.zkey" where X is the public
639 * key in an encoding suitable for DNS labels.
640 * @param pkey set to a public key on the eliptic curve
641 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
644 GNUNET_NAMESTORE_zkey_to_pkey (const char *zkey,
645 struct GNUNET_CRYPTO_EccPublicSignKey *pkey);
649 * Calculate the DHT query for a given @a label in a given @a zone.
651 * @param zone private 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_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
658 struct GNUNET_HashCode *query);
662 * Calculate the DHT query for a given @a label in a given @a zone.
664 * @param pub public key of the zone
665 * @param label label of the record
666 * @param query hash to use for the query
669 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
671 struct GNUNET_HashCode *query);
675 * Sign name and records
677 * @param key the private key
678 * @param expire block expiration
679 * @param label the name for the records
680 * @param rd record data
681 * @param rd_count number of records in @a rd
683 struct GNUNET_NAMESTORE_Block *
684 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
685 struct GNUNET_TIME_Absolute expire,
687 const struct GNUNET_NAMESTORE_RecordData *rd,
688 unsigned int rd_count);
692 * Check if a signature is valid. This API is used by the GNS Block
693 * to validate signatures received from the network.
695 * @param block block to verify
696 * @return #GNUNET_OK if the signature is valid
699 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block);
705 * @param block block to decrypt
706 * @param zone_key public key of the zone
707 * @param label the name for the records
708 * @param proc function to call with the result
709 * @param proc_cls closure for @a proc
710 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
714 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
715 const struct GNUNET_CRYPTO_EccPublicSignKey *zone_key,
717 GNUNET_NAMESTORE_RecordCallback proc,
722 * Compares if two records are equal
725 * @param b another record
726 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
729 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
730 const struct GNUNET_NAMESTORE_RecordData *b);
734 * Returns the expiration time of the given block of records. The block
735 * expiration time is the expiration time of the record with smallest
738 * @param rd_count number of records given in @a rd
739 * @param rd array of records
740 * @return absolute expiration time
742 struct GNUNET_TIME_Absolute
743 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count,
744 const struct GNUNET_NAMESTORE_RecordData *rd);
747 #if 0 /* keep Emacsens' auto-indent happy */
754 /* end of gnunet_namestore_service.h */