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_gnsrecord_lib.h
23 * @brief API that can be used to manipulate GNS record data
24 * @author Christian Grothoff
26 #ifndef GNUNET_GNSRECORD_LIB_H
27 #define GNUNET_GNSRECORD_LIB_H
32 #if 0 /* keep Emacsens' auto-indent happy */
38 * Maximum size of a value that can be stored in a GNS block.
40 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
44 * Record type indicating any record/'*'
46 #define GNUNET_GNSRECORD_TYPE_ANY 0
49 * Record type for GNS zone transfer ("PKEY").
51 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
54 * Record type for GNS zone transfer ("PSEU").
56 #define GNUNET_GNSRECORD_TYPE_PSEU 65537
59 * Record type for GNS legacy hostnames ("LEHO").
61 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
64 * Record type for VPN resolution
66 #define GNUNET_GNSRECORD_TYPE_VPN 65539
69 * Record type for delegation to DNS.
71 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
74 * Record type for a social place.
76 #define GNUNET_GNSRECORD_TYPE_PLACE 65541
79 * Record type for a phone (of CONVERSATION).
81 #define GNUNET_GNSRECORD_TYPE_PHONE 65542
85 * Flags that can be set for a record.
87 enum GNUNET_NAMESTORE_RecordFlags
93 GNUNET_NAMESTORE_RF_NONE = 0,
96 * This is a private record of this peer and it should
97 * thus not be handed out to other peers.
99 GNUNET_NAMESTORE_RF_PRIVATE = 2,
102 * This record was added automatically by the system
103 * and is pending user confimation.
105 GNUNET_NAMESTORE_RF_PENDING = 4,
108 * This expiration time of the record is a relative
109 * time (not an absolute time).
111 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
114 * This record should not be used unless all (other) records with an absolute
115 * expiration time have expired.
117 GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
120 * When comparing flags for record equality for removal,
121 * which flags should must match (in addition to the type,
122 * name, expiration value and data of the record)? All flags
123 * that are not listed here will be ignored for this purpose.
124 * (for example, we don't expect that users will remember to
125 * pass the '--private' option when removing a record from
126 * the namestore, hence we don't require this particular option
127 * to match upon removal). See also
128 * #GNUNET_NAMESTORE_records_cmp.
130 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
137 struct GNUNET_NAMESTORE_RecordData
141 * Binary value stored in the DNS record. Note: "data" must never
142 * be individually 'malloc'ed, but instead always points into some
143 * existing data area.
148 * Expiration time for the DNS record. Can be relative
149 * or absolute, depending on 'flags'. Measured in the same
150 * unit as GNUnet time (microseconds).
152 uint64_t expiration_time;
155 * Number of bytes in 'data'.
160 * Type of the GNS/DNS record.
162 uint32_t record_type;
165 * Flags for the record.
167 enum GNUNET_NAMESTORE_RecordFlags flags;
172 GNUNET_NETWORK_STRUCT_BEGIN
176 * Information we have in an encrypted block with record data (i.e. in the DHT).
178 struct GNUNET_NAMESTORE_Block
182 * Signature of the block.
184 struct GNUNET_CRYPTO_EcdsaSignature signature;
187 * Derived key used for signing; hash of this is the query.
189 struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
192 * Number of bytes signed; also specifies the number of bytes
193 * of encrypted data that follow.
195 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
198 * Expiration time of the block.
200 struct GNUNET_TIME_AbsoluteNBO expiration_time;
202 /* followed by encrypted data */
205 GNUNET_NETWORK_STRUCT_END
209 * Process a records that were decrypted from a block.
212 * @param rd_count number of entries in @a rd array
213 * @param rd array of records with data to store
215 typedef void (*GNUNET_NAMESTORE_RecordCallback) (void *cls,
216 unsigned int rd_count,
217 const struct GNUNET_NAMESTORE_RecordData *rd);
221 /* ***************** API related to GNSRECORD plugins ************** */
224 * Convert the binary value @a data of a record of
225 * type @a type to a human-readable string.
227 * @param type type of the record
228 * @param data value in binary encoding
229 * @param data_size number of bytes in @a data
230 * @return NULL on error, otherwise human-readable representation of the value
233 GNUNET_GNSRECORD_value_to_string (uint32_t type,
239 * Convert human-readable version of the value @a s of a record
240 * of type @a type to the respective binary representation.
242 * @param type type of the record
243 * @param s human-readable string
244 * @param data set to value in binary encoding (will be allocated)
245 * @param data_size set to number of bytes in @a data
246 * @return #GNUNET_OK on success
249 GNUNET_GNSRECORD_string_to_value (uint32_t type,
256 * Convert a type name (i.e. "AAAA") to the corresponding number.
258 * @param dns_typename name to convert
259 * @return corresponding number, UINT32_MAX on error
262 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
266 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
268 * @param type number of a type to convert
269 * @return corresponding typestring, NULL on error
272 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
275 /* convenience APIs for serializing / deserializing GNS records */
278 * Calculate how many bytes we will need to serialize the given
281 * @param rd_count number of records in the @a rd array
282 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
283 * @return the required size to serialize
286 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
287 const struct GNUNET_NAMESTORE_RecordData *rd);
291 * Serialize the given records to the given destination buffer.
293 * @param rd_count number of records in the @a rd array
294 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
295 * @param dest_size size of the destination array @a dst
296 * @param dest where to write the result
297 * @return the size of serialized records, -1 if records do not fit
300 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
301 const struct GNUNET_NAMESTORE_RecordData *rd,
307 * Deserialize the given records to the given destination.
309 * @param len size of the serialized record data
310 * @param src the serialized record data
311 * @param rd_count number of records in the @a dest array
312 * @param dest where to put the data
313 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
316 GNUNET_NAMESTORE_records_deserialize (size_t len,
318 unsigned int rd_count,
319 struct GNUNET_NAMESTORE_RecordData *dest);
322 /* ******* general APIs relating to blocks, records and labels ******** */
327 * Test if a given record is expired.
329 * @param rd record to test
330 * @return #GNUNET_YES if the record is expired,
334 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
338 * Convert a UTF-8 string to UTF-8 lowercase
339 * @param src source string
340 * @return converted result
343 GNUNET_NAMESTORE_normalize_string (const char *src);
347 * Convert a zone to a string (for printing debug messages).
348 * This is one of the very few calls in the entire API that is
351 * @param z public key of a zone
352 * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s.
355 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
359 * Convert public key to the respective absolute domain name in the
361 * This is one of the very few calls in the entire API that is
364 * @param pkey a public key with a point on the eliptic curve
365 * @return string "X.zkey" where X is the coordinates of the public
366 * key in an encoding suitable for DNS labels.
369 GNUNET_NAMESTORE_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
373 * Convert an absolute domain name in the ".zkey" pTLD to the
374 * respective public key.
376 * @param zkey string "X.zkey" where X is the public
377 * key in an encoding suitable for DNS labels.
378 * @param pkey set to a public key on the eliptic curve
379 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
382 GNUNET_NAMESTORE_zkey_to_pkey (const char *zkey,
383 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
387 * Calculate the DHT query for a given @a label in a given @a zone.
389 * @param zone private key of the zone
390 * @param label label of the record
391 * @param query hash to use for the query
394 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
396 struct GNUNET_HashCode *query);
400 * Calculate the DHT query for a given @a label in a given @a zone.
402 * @param pub public key of the zone
403 * @param label label of the record
404 * @param query hash to use for the query
407 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
409 struct GNUNET_HashCode *query);
413 * Sign name and records
415 * @param key the private key
416 * @param expire block expiration
417 * @param label the name for the records
418 * @param rd record data
419 * @param rd_count number of records in @a rd
421 struct GNUNET_NAMESTORE_Block *
422 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
423 struct GNUNET_TIME_Absolute expire,
425 const struct GNUNET_NAMESTORE_RecordData *rd,
426 unsigned int rd_count);
430 * Check if a signature is valid. This API is used by the GNS Block
431 * to validate signatures received from the network.
433 * @param block block to verify
434 * @return #GNUNET_OK if the signature is valid
437 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block);
443 * @param block block to decrypt
444 * @param zone_key public key of the zone
445 * @param label the name for the records
446 * @param proc function to call with the result
447 * @param proc_cls closure for @a proc
448 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
452 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
453 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
455 GNUNET_NAMESTORE_RecordCallback proc,
460 * Compares if two records are equal
463 * @param b another record
464 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
467 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
468 const struct GNUNET_NAMESTORE_RecordData *b);
472 * Returns the expiration time of the given block of records. The block
473 * expiration time is the expiration time of the record with smallest
476 * @param rd_count number of records given in @a rd
477 * @param rd array of records
478 * @return absolute expiration time
480 struct GNUNET_TIME_Absolute
481 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count,
482 const struct GNUNET_NAMESTORE_RecordData *rd);
486 #if 0 /* keep Emacsens' auto-indent happy */