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 * @author Christian Grothoff
26 * Other functions we might want:
27 * - enumerate all known zones
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 zone revocation
72 #define GNUNET_NAMESTORE_TYPE_REV 65540
75 * Record type for a social place.
77 #define GNUNET_NAMESTORE_TYPE_PLACE 65541
82 struct GNUNET_NAMESTORE_QueueEntry;
85 * Handle to the namestore service.
87 struct GNUNET_NAMESTORE_Handle;
90 * Handle to the namestore zone iterator.
92 struct GNUNET_NAMESTORE_ZoneIterator;
95 * Maximum size of a value that can be stored in the namestore.
97 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
102 * Connect to the namestore service.
104 * @param cfg configuration to use
105 * @return handle to use to access the service
107 struct GNUNET_NAMESTORE_Handle *
108 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
112 * Disconnect from the namestore service (and free associated
115 * @param h handle to the namestore
118 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
122 * Continuation called to notify client about result of the
126 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
127 * GNUNET_NO if content was already there or not found
128 * GNUNET_YES (or other positive value) on success
129 * @param emsg NULL on success, otherwise an error message
131 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
137 * Flags that can be set for a record.
139 enum GNUNET_NAMESTORE_RecordFlags
143 * No special options.
145 GNUNET_NAMESTORE_RF_NONE = 0,
148 * This peer is the authority for this record; it must thus
149 * not be deleted (other records can be deleted if we run
152 GNUNET_NAMESTORE_RF_AUTHORITY = 1,
155 * This is a private record of this peer and it should
156 * thus not be handed out to other peers.
158 GNUNET_NAMESTORE_RF_PRIVATE = 2,
161 * This record was added by the system
162 * and is pending user confimation
164 GNUNET_NAMESTORE_RF_PENDING = 4,
167 * This expiration time of the record is a relative
168 * time (not an absolute time).
170 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
173 * This record should not be used unless all (other) records with an absolute
174 * expiration time have expired.
176 GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
179 * When comparing flags for record equality for removal,
180 * which flags should must match (in addition to the type,
181 * name, expiration value and data of the record)? All flags
182 * that are not listed here will be ignored for this purpose.
183 * (for example, we don't expect that users will remember to
184 * pass the '--private' option when removing a record from
185 * the namestore, hence we don't require this particular option
186 * to match upon removal). See also
187 * 'GNUNET_NAMESTORE_records_cmp'.
189 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
196 struct GNUNET_NAMESTORE_RecordData
200 * Binary value stored in the DNS record.
201 * FIXME: goofy API: sometimes 'data' is individually
202 * 'malloc'ed, sometimes it points into some existing
203 * data area (so sometimes this should be a 'void *',
204 * sometimes a 'const void *'). This is unclean.
209 * Expiration time for the DNS record. Can be relative
210 * or absolute, depending on 'flags'.
212 uint64_t expiration_time;
215 * Number of bytes in 'data'.
220 * Type of the GNS/DNS record.
222 uint32_t record_type;
225 * Flags for the record.
227 enum GNUNET_NAMESTORE_RecordFlags flags;
232 * Store an item in the namestore. If the item is already present,
233 * it is replaced with the new record. Use an empty array to
234 * remove all records under the given name.
236 * @param h handle to the namestore
237 * @param zone_key public key of the zone
238 * @param name name that is being mapped (at most 255 characters long)
239 * @param freshness when does the corresponding block in the DHT expire (until
240 * when should we never do a DHT lookup for the same name again)?
241 * @param rd_count number of entries in 'rd' array
242 * @param rd array of records with data to store
243 * @param signature signature for all the records in the zone under the given name
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 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
252 struct GNUNET_TIME_Absolute freshness,
253 unsigned int rd_count,
254 const struct GNUNET_NAMESTORE_RecordData *rd,
255 const struct GNUNET_CRYPTO_EccSignature *signature,
256 GNUNET_NAMESTORE_ContinuationWithStatus cont,
261 * Check if a signature is valid. This API is used by the GNS Block
262 * to validate signatures received from the network.
264 * @param public_key public key of the zone
265 * @param freshness time set for block expiration
266 * @param name name that is being mapped (at most 255 characters long)
267 * @param rd_count number of entries in 'rd' array
268 * @param rd array of records with data to store
269 * @param signature signature for all the records in the zone under the given name
270 * @return GNUNET_OK if the signature is valid
273 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *public_key,
274 const struct GNUNET_TIME_Absolute freshness,
276 unsigned int rd_count,
277 const struct GNUNET_NAMESTORE_RecordData *rd,
278 const struct GNUNET_CRYPTO_EccSignature *signature);
282 * Store an item in the namestore. If the item is already present,
283 * it is replaced with the new record. Use an empty array to
284 * remove all records under the given name.
286 * @param h handle to the namestore
287 * @param pkey private key of the zone
288 * @param name name that is being mapped (at most 255 characters long)
289 * @param rd_count number of records in the 'rd' array
290 * @param rd array of records with data to store
291 * @param cont continuation to call when done
292 * @param cont_cls closure for 'cont'
293 * @return handle to abort the request
295 struct GNUNET_NAMESTORE_QueueEntry *
296 GNUNET_NAMESTORE_record_put_by_authority (struct GNUNET_NAMESTORE_Handle *h,
297 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
299 unsigned int rd_count,
300 const struct GNUNET_NAMESTORE_RecordData *rd,
301 GNUNET_NAMESTORE_ContinuationWithStatus cont,
306 * Process a record that was stored in the namestore.
309 * @param zone_key public key of the zone
310 * @param freshness when does the corresponding block in the DHT expire (until
311 * when should we never do a DHT lookup for the same name again)?;
312 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
313 * or the expiration time of the block in the namestore (even if there are zero
314 * records matching the desired record type)
315 * @param name name that is being mapped (at most 255 characters long)
316 * @param rd_count number of entries in 'rd' array
317 * @param rd array of records with data to store
318 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
319 * because the user queried for a particular record type only)
321 typedef void (*GNUNET_NAMESTORE_RecordProcessor) (void *cls,
322 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
323 struct GNUNET_TIME_Absolute freshness,
325 unsigned int rd_count,
326 const struct GNUNET_NAMESTORE_RecordData *rd,
327 const struct GNUNET_CRYPTO_EccSignature *signature);
331 * Get a result for a particular key from the namestore. The processor
332 * will only be called once. When using this functions, relative expiration
333 * times will be converted to absolute expiration times and a signature
334 * will be created if we are the authority. The record data and signature
335 * passed to 'proc' is thus always suitable for passing on to other peers
336 * (if we are the authority). If the record type is NOT set to 'ANY' and
337 * if we are NOT the authority, then non-matching records may be omitted
338 * from the result and no valid signature can be created; in this case,
339 * 'signature' will be NULL and the result cannot be given to other peers.
341 * @param h handle to the namestore
342 * @param zone zone to look up a record from
343 * @param name name to look up
344 * @param record_type desired record type, 0 for all
345 * @param proc function to call on the matching records, or with
346 * NULL (rd_count == 0) if there are no matching records
347 * @param proc_cls closure for proc
348 * @return a handle that can be used to
351 struct GNUNET_NAMESTORE_QueueEntry *
352 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
353 const struct GNUNET_CRYPTO_ShortHashCode *zone,
355 uint32_t record_type,
356 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
360 * Look for an existing PKEY delegation record for a given public key.
361 * Returns at most one result to the processor.
363 * @param h handle to the namestore
364 * @param zone hash of public key of the zone to look up in, never NULL
365 * @param value_zone hash of the public key of the target zone (value), never NULL
366 * @param proc function to call on the matching records, or with
367 * NULL (rd_count == 0) if there are no matching records
368 * @param proc_cls closure for proc
369 * @return a handle that can be used to
372 struct GNUNET_NAMESTORE_QueueEntry *
373 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
374 const struct GNUNET_CRYPTO_ShortHashCode *zone,
375 const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
376 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
380 * Cancel a namestore operation. The final callback from the
381 * operation must not have been done yet. Must be called on any
382 * namestore operation that has not yet completed prior to calling
383 * 'GNUNET_NAMESTORE_disconnect'.
385 * @param qe operation to cancel
388 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
392 * Starts a new zone iteration (used to periodically PUT all of our
393 * records into our DHT). "proc" will be called once immediately, and
394 * then again after "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
396 * By specifying a 'zone' of NULL and setting 'GNUNET_NAMESTORE_RF_AUTHORITY'
397 * in 'must_have_flags', we can iterate over all records for which we are
398 * the authority (the 'authority' flag will NOT be set in the returned
401 * The 'GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION'
402 * bit in 'must_have_flags' has a special meaning:
404 * 0) If the bit is clear, all relative expriation times are converted to
405 * absolute expiration times. This is useful for performing DHT PUT
406 * operations (and zone transfers) of our zone. The generated signatures
407 * will be valid for other peers.
408 * 1) if it is set, it means that relative expiration times should be
409 * preserved when returned (this is useful for the zone editor user
410 * interface). No signatures will be created in this case, as
411 * signatures must not cover records with relative expiration times.
413 * Note that not all queries against this interface are equally performant
414 * as for some combinations no efficient index may exist.
416 * @param h handle to the namestore
417 * @param zone zone to access, NULL for all zones
418 * @param must_have_flags flags that must be set for the record to be returned
419 * @param must_not_have_flags flags that must NOT be set for the record to be returned
420 * @param proc function to call on each name from the zone; it
421 * will be called repeatedly with a value (if available)
422 * and always once at the end with a name of NULL.
423 * @param proc_cls closure for proc
424 * @return an iterator handle to use for iteration
426 struct GNUNET_NAMESTORE_ZoneIterator *
427 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
428 const struct GNUNET_CRYPTO_ShortHashCode *zone,
429 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
430 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
431 GNUNET_NAMESTORE_RecordProcessor proc,
436 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
437 * for the next record.
439 * @param it the iterator
442 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
446 * Stops iteration and releases the namestore handle for further calls. Must
447 * be called on any iteration that has not yet completed prior to calling
448 * 'GNUNET_NAMESTORE_disconnect'.
450 * @param it the iterator
453 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
457 * Handle for a monitoring activity.
459 struct GNUNET_NAMESTORE_ZoneMonitor;
463 * Function called whenever the records for a given name changed.
466 * @param zone_key NULL if the communication with the namestore broke down
467 * (and thus all entries should be 'cleared' until the communication
468 * can be re-established, at which point the monitor will
469 * re-add all records that are (still) in the namestore after
470 * the reconnect); if this value is NULL, all other arguments
471 * will also be 0/NULL.
472 * @param freshness when does the corresponding block in the DHT expire (until
473 * when should we never do a DHT lookup for the same name again)?;
474 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
475 * or the expiration time of the block in the namestore (even if there are zero
476 * records matching the desired record type)
477 * @param name name that is being mapped (at most 255 characters long)
478 * @param rd_count number of entries in 'rd' array
479 * @param rd array of records with data to store
480 * @param signature signature of the record block
482 typedef void (*GNUNET_NAMESTORE_RecordMonitor)(void *cls,
483 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
484 struct GNUNET_TIME_Absolute freshness,
487 const struct GNUNET_NAMESTORE_RecordData *rd,
488 const struct GNUNET_CRYPTO_EccSignature *signature);
492 * Function called once the monitor has caught up with the current
493 * state of the database. Will be called AGAIN after each disconnect
494 * (record monitor called with 'NULL' for zone_key) once we're again
499 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
503 * Begin monitoring a zone for changes. Will first call the 'monitor' function
504 * on all existing records in the selected zone(s), then calls 'sync_cb',
505 * and then calls the 'monitor' whenever a record changes. If the namestore
506 * disconnects, the 'monitor' function is called with a disconnect event; if
507 * the connection is re-established, the process begins from the start (all
508 * existing records, sync, then updates).
510 * Note that the 'signature' in the 'monitor' callback will always be
511 * NULL (as signatures are usually not valuable to monitoring clients,
512 * the service does not bother to generate a valid signature for
515 * @param cfg configuration to use to connect to namestore
516 * @param zone zone to monitor, NULL for all zones
517 * @param monitor function to call on zone changes
518 * @param sync_cb function called when we're in sync with the namestore
519 * @param cls closure for 'monitor' and 'sync_cb'
520 * @return handle to stop monitoring
522 struct GNUNET_NAMESTORE_ZoneMonitor *
523 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
524 const struct GNUNET_CRYPTO_ShortHashCode *zone,
525 GNUNET_NAMESTORE_RecordMonitor monitor,
526 GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
531 * Stop monitoring a zone for changes.
533 * @param zm handle to the monitor activity to stop
536 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
539 /* convenience APIs for serializing / deserializing GNS records */
542 * Calculate how many bytes we will need to serialize the given
545 * @param rd_count number of records in the rd array
546 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
548 * @return the required size to serialize
552 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
553 const struct GNUNET_NAMESTORE_RecordData *rd);
557 * Serialize the given records to the given destination buffer.
559 * @param rd_count number of records in the rd array
560 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
561 * @param dest_size size of the destination array
562 * @param dest where to write the result
564 * @return the size of serialized records, -1 if records do not fit
567 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
568 const struct GNUNET_NAMESTORE_RecordData *rd,
574 * Deserialize the given records to the given destination.
576 * @param len size of the serialized record data
577 * @param src the serialized record data
578 * @param rd_count number of records in the rd array
579 * @param dest where to put the data
581 * @return GNUNET_OK on success, GNUNET_SYSERR on error
584 GNUNET_NAMESTORE_records_deserialize (size_t len,
586 unsigned int rd_count,
587 struct GNUNET_NAMESTORE_RecordData *dest);
591 * Convert the 'value' of a record to a string.
593 * @param type type of the record
594 * @param data value in binary encoding
595 * @param data_size number of bytes in data
596 * @return NULL on error, otherwise human-readable representation of the value
599 GNUNET_NAMESTORE_value_to_string (uint32_t type,
605 * Convert human-readable version of a 'value' of a record to the binary
608 * @param type type of the record
609 * @param s human-readable string
610 * @param data set to value in binary encoding (will be allocated)
611 * @param data_size set to number of bytes in data
612 * @return GNUNET_OK on success
615 GNUNET_NAMESTORE_string_to_value (uint32_t type,
622 * Convert a type name (i.e. "AAAA") to the corresponding number.
624 * @param typename name to convert
625 * @return corresponding number, UINT32_MAX on error
628 GNUNET_NAMESTORE_typename_to_number (const char *typename);
632 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
634 * @param type number of a type to convert
635 * @return corresponding typestring, NULL on error
638 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
642 * Test if a given record is expired.
644 * @param rd record to test
645 * @return GNUNET_YES if the record is expired,
649 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
653 * Convert a UTF-8 string to UTF-8 lowercase
654 * @param src source string
655 * @return converted result
658 GNUNET_NAMESTORE_normalize_string (const char *src);
662 * Convert a short hash to a string (for printing debug messages).
663 * This is one of the very few calls in the entire API that is
666 * @param hc the short hash code
667 * @return string form; will be overwritten by next call to GNUNET_h2s.
670 GNUNET_NAMESTORE_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc);
674 * Sign name and records
676 * @param key the private key
677 * @param expire block expiration
678 * @param name the name
679 * @param rd record data
680 * @param rd_count number of records
682 * @return the signature
684 struct GNUNET_CRYPTO_EccSignature *
685 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_EccPrivateKey *key,
686 struct GNUNET_TIME_Absolute expire,
688 const struct GNUNET_NAMESTORE_RecordData *rd,
689 unsigned int rd_count);
693 * Compares if two records are equal
698 * @return GNUNET_YES or GNUNET_NO
701 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
702 const struct GNUNET_NAMESTORE_RecordData *b);
707 #if 0 /* keep Emacsens' auto-indent happy */
714 /* end of gnunet_namestore_service.h */