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
77 struct GNUNET_NAMESTORE_QueueEntry;
80 * Handle to the namestore service.
82 struct GNUNET_NAMESTORE_Handle;
85 * Handle to the namestore zone iterator.
87 struct GNUNET_NAMESTORE_ZoneIterator;
90 * Maximum size of a value that can be stored in the namestore.
92 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
97 * Connect to the namestore service.
99 * @param cfg configuration to use
100 * @return handle to use to access the service
102 struct GNUNET_NAMESTORE_Handle *
103 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
107 * Disconnect from the namestore service (and free associated
110 * @param h handle to the namestore
113 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
117 * Continuation called to notify client about result of the
121 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
122 * GNUNET_NO if content was already there or not found
123 * GNUNET_YES (or other positive value) on success
124 * @param emsg NULL on success, otherwise an error message
126 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
132 * Flags that can be set for a record.
134 enum GNUNET_NAMESTORE_RecordFlags
138 * No special options.
140 GNUNET_NAMESTORE_RF_NONE = 0,
143 * This peer is the authority for this record; it must thus
144 * not be deleted (other records can be deleted if we run
147 GNUNET_NAMESTORE_RF_AUTHORITY = 1,
150 * This is a private record of this peer and it should
151 * thus not be handed out to other peers.
153 GNUNET_NAMESTORE_RF_PRIVATE = 2,
156 * This record was added by the system
157 * and is pending user confimation
159 GNUNET_NAMESTORE_RF_PENDING = 4,
162 * This expiration time of the record is a relative
163 * time (not an absolute time).
165 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
168 * This record should not be used unless all (other) records with an absolute
169 * expiration time have expired.
171 GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
174 * When comparing flags for record equality for removal,
175 * which flags should must match (in addition to the type,
176 * name, expiration value and data of the record)? All flags
177 * that are not listed here will be ignored for this purpose.
178 * (for example, we don't expect that users will remember to
179 * pass the '--private' option when removing a record from
180 * the namestore, hence we don't require this particular option
181 * to match upon removal). See also
182 * 'GNUNET_NAMESTORE_records_cmp'.
184 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
191 struct GNUNET_NAMESTORE_RecordData
195 * Binary value stored in the DNS record.
196 * FIXME: goofy API: sometimes 'data' is individually
197 * 'malloc'ed, sometimes it points into some existing
198 * data area (so sometimes this should be a 'void *',
199 * sometimes a 'const void *'). This is unclean.
204 * Expiration time for the DNS record. Can be relative
205 * or absolute, depending on 'flags'.
207 uint64_t expiration_time;
210 * Number of bytes in 'data'.
215 * Type of the GNS/DNS record.
217 uint32_t record_type;
220 * Flags for the record.
222 enum GNUNET_NAMESTORE_RecordFlags flags;
227 * Store an item in the namestore. If the item is already present,
228 * it is replaced with the new record. Use an empty array to
229 * remove all records under the given name.
231 * @param h handle to the namestore
232 * @param zone_key public key of the zone
233 * @param name name that is being mapped (at most 255 characters long)
234 * @param freshness when does the corresponding block in the DHT expire (until
235 * when should we never do a DHT lookup for the same name again)?
236 * @param rd_count number of entries in 'rd' array
237 * @param rd array of records with data to store
238 * @param signature signature for all the records in the zone under the given name
239 * @param cont continuation to call when done
240 * @param cont_cls closure for cont
241 * @return handle to abort the request
243 struct GNUNET_NAMESTORE_QueueEntry *
244 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
245 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
247 struct GNUNET_TIME_Absolute freshness,
248 unsigned int rd_count,
249 const struct GNUNET_NAMESTORE_RecordData *rd,
250 const struct GNUNET_CRYPTO_EccSignature *signature,
251 GNUNET_NAMESTORE_ContinuationWithStatus cont,
256 * Check if a signature is valid. This API is used by the GNS Block
257 * to validate signatures received from the network.
259 * @param public_key public key of the zone
260 * @param freshness time set for block expiration
261 * @param name name that is being mapped (at most 255 characters long)
262 * @param rd_count number of entries in 'rd' array
263 * @param rd array of records with data to store
264 * @param signature signature for all the records in the zone under the given name
265 * @return GNUNET_OK if the signature is valid
268 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *public_key,
269 const struct GNUNET_TIME_Absolute freshness,
271 unsigned int rd_count,
272 const struct GNUNET_NAMESTORE_RecordData *rd,
273 const struct GNUNET_CRYPTO_EccSignature *signature);
277 * Store an item in the namestore. If the item is already present,
278 * it is replaced with the new record. Use an empty array to
279 * remove all records under the given name.
281 * @param h handle to the namestore
282 * @param pkey private key of the zone
283 * @param name name that is being mapped (at most 255 characters long)
284 * @param rd_count number of records in the 'rd' array
285 * @param rd array of records with data to store
286 * @param cont continuation to call when done
287 * @param cont_cls closure for 'cont'
288 * @return handle to abort the request
290 struct GNUNET_NAMESTORE_QueueEntry *
291 GNUNET_NAMESTORE_record_put_by_authority (struct GNUNET_NAMESTORE_Handle *h,
292 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
294 unsigned int rd_count,
295 const struct GNUNET_NAMESTORE_RecordData *rd,
296 GNUNET_NAMESTORE_ContinuationWithStatus cont,
301 * Process a record that was stored in the namestore.
304 * @param zone_key public key of the zone
305 * @param freshness when does the corresponding block in the DHT expire (until
306 * when should we never do a DHT lookup for the same name again)?;
307 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
308 * or the expiration time of the block in the namestore (even if there are zero
309 * records matching the desired record type)
310 * @param name name that is being mapped (at most 255 characters long)
311 * @param rd_count number of entries in 'rd' array
312 * @param rd array of records with data to store
313 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
314 * because the user queried for a particular record type only)
316 typedef void (*GNUNET_NAMESTORE_RecordProcessor) (void *cls,
317 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
318 struct GNUNET_TIME_Absolute freshness,
320 unsigned int rd_count,
321 const struct GNUNET_NAMESTORE_RecordData *rd,
322 const struct GNUNET_CRYPTO_EccSignature *signature);
326 * Get a result for a particular key from the namestore. The processor
327 * will only be called once. When using this functions, relative expiration
328 * times will be converted to absolute expiration times and a signature
329 * will be created if we are the authority. The record data and signature
330 * passed to 'proc' is thus always suitable for passing on to other peers
331 * (if we are the authority). If the record type is NOT set to 'ANY' and
332 * if we are NOT the authority, then non-matching records may be omitted
333 * from the result and no valid signature can be created; in this case,
334 * 'signature' will be NULL and the result cannot be given to other peers.
336 * @param h handle to the namestore
337 * @param zone zone to look up a record from
338 * @param name name to look up
339 * @param record_type desired record type, 0 for all
340 * @param proc function to call on the matching records, or with
341 * NULL (rd_count == 0) if there are no matching records
342 * @param proc_cls closure for proc
343 * @return a handle that can be used to
346 struct GNUNET_NAMESTORE_QueueEntry *
347 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
348 const struct GNUNET_CRYPTO_ShortHashCode *zone,
350 uint32_t record_type,
351 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
355 * Look for an existing PKEY delegation record for a given public key.
356 * Returns at most one result to the processor.
358 * @param h handle to the namestore
359 * @param zone hash of public key of the zone to look up in, never NULL
360 * @param value_zone hash of the public key of the target zone (value), never NULL
361 * @param proc function to call on the matching records, or with
362 * NULL (rd_count == 0) if there are no matching records
363 * @param proc_cls closure for proc
364 * @return a handle that can be used to
367 struct GNUNET_NAMESTORE_QueueEntry *
368 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
369 const struct GNUNET_CRYPTO_ShortHashCode *zone,
370 const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
371 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
376 * Starts a new zone iteration (used to periodically PUT all of our
377 * records into our DHT). "proc" will be called once immediately, and
378 * then again after "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
380 * By specifying a 'zone' of NULL and setting 'GNUNET_NAMESTORE_RF_AUTHORITY'
381 * in 'must_have_flags', we can iterate over all records for which we are
382 * the authority (the 'authority' flag will NOT be set in the returned
385 * The 'GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION'
386 * bit in 'must_have_flags' has a special meaning:
388 * 0) If the bit is clear, all relative expriation times are converted to
389 * absolute expiration times. This is useful for performing DHT PUT
390 * operations (and zone transfers) of our zone. The generated signatures
391 * will be valid for other peers.
392 * 1) if it is set, it means that relative expiration times should be
393 * preserved when returned (this is useful for the zone editor user
394 * interface). No signatures will be created in this case, as
395 * signatures must not cover records with relative expiration times.
397 * Note that not all queries against this interface are equally performant
398 * as for some combinations no efficient index may exist.
400 * @param h handle to the namestore
401 * @param zone zone to access, NULL for all zones
402 * @param must_have_flags flags that must be set for the record to be returned
403 * @param must_not_have_flags flags that must NOT be set for the record to be returned
404 * @param proc function to call on each name from the zone; it
405 * will be called repeatedly with a value (if available)
406 * and always once at the end with a name of NULL.
407 * @param proc_cls closure for proc
408 * @return an iterator handle to use for iteration
410 struct GNUNET_NAMESTORE_ZoneIterator *
411 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
412 const struct GNUNET_CRYPTO_ShortHashCode *zone,
413 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
414 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
415 GNUNET_NAMESTORE_RecordProcessor 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 whenever the records for a given name changed.
450 * @param was_removed GNUNET_NO if the record was added, GNUNET_YES if it was removed
451 * @param freshness when does the corresponding block in the DHT expire (until
452 * when should we never do a DHT lookup for the same name again)?;
453 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
454 * or the expiration time of the block in the namestore (even if there are zero
455 * records matching the desired record type)
456 * @param name name that is being mapped (at most 255 characters long)
457 * @param rd_count number of entries in 'rd' array
458 * @param rd array of records with data to store
459 * @param signature signature of the record block
461 typedef void (*GNUNET_NAMESTORE_RecordMonitor)(void *cls,
463 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
464 struct GNUNET_TIME_Absolute freshness,
467 const struct GNUNET_NAMESTORE_RecordData *rd,
468 const struct GNUNET_CRYPTO_EccSignature *signature);
472 * Begin monitoring a zone for changes. Will first call the 'monitor' function
473 * on all existing records in the selected zone(s) and then call it whenever
476 * @param cfg configuration to use to connect to namestore
477 * @param zone zone to monitor, NULL for all zones
478 * @param monitor function to call on zone changes
479 * @param monitor_cls closure for 'monitor'
480 * @return handle to stop monitoring
482 struct GNUNET_NAMESTORE_ZoneMonitor *
483 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
484 const struct GNUNET_CRYPTO_ShortHashCode *zone,
485 GNUNET_NAMESTORE_RecordMonitor monitor,
490 * Stop monitoring a zone for changes.
492 * @param zm handle to the monitor activity to stop
495 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
499 * Cancel a namestore operation. The final callback from the
500 * operation must not have been done yet. Must be called on any
501 * namestore operation that has not yet completed prior to calling
502 * 'GNUNET_NAMESTORE_disconnect'.
504 * @param qe operation to cancel
507 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
510 /* convenience APIs for serializing / deserializing GNS records */
513 * Calculate how many bytes we will need to serialize the given
516 * @param rd_count number of records in the rd array
517 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
519 * @return the required size to serialize
523 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
524 const struct GNUNET_NAMESTORE_RecordData *rd);
528 * Serialize the given records to the given destination buffer.
530 * @param rd_count number of records in the rd array
531 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
532 * @param dest_size size of the destination array
533 * @param dest where to write the result
535 * @return the size of serialized records, -1 if records do not fit
538 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
539 const struct GNUNET_NAMESTORE_RecordData *rd,
545 * Deserialize the given records to the given destination.
547 * @param len size of the serialized record data
548 * @param src the serialized record data
549 * @param rd_count number of records in the rd array
550 * @param dest where to put the data
552 * @return GNUNET_OK on success, GNUNET_SYSERR on error
555 GNUNET_NAMESTORE_records_deserialize (size_t len,
557 unsigned int rd_count,
558 struct GNUNET_NAMESTORE_RecordData *dest);
562 * Convert the 'value' of a record to a string.
564 * @param type type of the record
565 * @param data value in binary encoding
566 * @param data_size number of bytes in data
567 * @return NULL on error, otherwise human-readable representation of the value
570 GNUNET_NAMESTORE_value_to_string (uint32_t type,
576 * Convert human-readable version of a 'value' of a record to the binary
579 * @param type type of the record
580 * @param s human-readable string
581 * @param data set to value in binary encoding (will be allocated)
582 * @param data_size set to number of bytes in data
583 * @return GNUNET_OK on success
586 GNUNET_NAMESTORE_string_to_value (uint32_t type,
593 * Convert a type name (i.e. "AAAA") to the corresponding number.
595 * @param typename name to convert
596 * @return corresponding number, UINT32_MAX on error
599 GNUNET_NAMESTORE_typename_to_number (const char *typename);
603 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
605 * @param type number of a type to convert
606 * @return corresponding typestring, NULL on error
609 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
613 * Test if a given record is expired.
615 * @param rd record to test
616 * @return GNUNET_YES if the record is expired,
620 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
623 #if 0 /* keep Emacsens' auto-indent happy */
630 /* end of gnunet_namestore_service.h */