2 This file is part of GNUnet
3 Copyright (C) 2012, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
25 * API that can be used to manipulate GNS record data
27 * @defgroup gnsrecord GNS Record library
28 * Manipulate GNS record data
30 * @see [Documentation](https://gnunet.org/gns-plugins)
34 #ifndef GNUNET_GNSRECORD_LIB_H
35 #define GNUNET_GNSRECORD_LIB_H
40 #if 0 /* keep Emacsens' auto-indent happy */
46 * Maximum size of a value that can be stored in a GNS block.
48 #define GNUNET_GNSRECORD_MAX_BLOCK_SIZE (63 * 1024)
52 * Record type indicating any record/'*'
54 #define GNUNET_GNSRECORD_TYPE_ANY 0
57 * Record type for GNS zone transfer ("PKEY").
59 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
62 * Record type for GNS nick names ("NICK").
64 #define GNUNET_GNSRECORD_TYPE_NICK 65537
67 * Record type for GNS legacy hostnames ("LEHO").
69 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
72 * Record type for VPN resolution
74 #define GNUNET_GNSRECORD_TYPE_VPN 65539
77 * Record type for delegation to DNS.
79 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
82 * Record type for a boxed record (see TLSA/SRV handling in GNS).
84 #define GNUNET_GNSRECORD_TYPE_BOX 65541
87 * Record type for a social place.
89 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
92 * Record type for a phone (of CONVERSATION).
94 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
97 * Record type for identity attributes (of IDENTITY).
99 #define GNUNET_GNSRECORD_TYPE_ID_ATTR 65544
102 * Record type for an identity token (of IDENTITY-TOKEN).
104 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN 65545
107 * Record type for the private metadata of an identity token (of IDENTITY-TOKEN).
109 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA 65546
112 * Record type for credential
114 #define GNUNET_GNSRECORD_TYPE_CREDENTIAL 65547
117 * Record type for reverse lookups
119 #define GNUNET_GNSRECORD_TYPE_REVERSE 65548
124 * Flags that can be set for a record.
126 enum GNUNET_GNSRECORD_Flags
130 * No special options.
132 GNUNET_GNSRECORD_RF_NONE = 0,
135 * This is a private record of this peer and it should
136 * thus not be handed out to other peers.
138 GNUNET_GNSRECORD_RF_PRIVATE = 2,
141 * This flag is currently unused; former RF_PENDING flag
143 * GNUNET_GNSRECORD_RF_UNUSED = 4,
147 * This expiration time of the record is a relative
148 * time (not an absolute time).
150 GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
153 * This record should not be used unless all (other) records with an absolute
154 * expiration time have expired.
156 GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
159 * When comparing flags for record equality for removal,
160 * which flags should must match (in addition to the type,
161 * name, expiration value and data of the record)? All flags
162 * that are not listed here will be ignored for this purpose.
163 * (for example, we don't expect that users will remember to
164 * pass the '--private' option when removing a record from
165 * the namestore, hence we don't require this particular option
166 * to match upon removal). See also
167 * #GNUNET_GNSRECORD_records_cmp.
169 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
176 struct GNUNET_GNSRECORD_Data
180 * Binary value stored in the DNS record. Note: "data" must never
181 * be individually 'malloc'ed, but instead always points into some
182 * existing data area.
187 * Expiration time for the DNS record. Can be relative
188 * or absolute, depending on @e flags. Measured in the same
189 * unit as GNUnet time (microseconds).
191 uint64_t expiration_time;
194 * Number of bytes in @e data.
199 * Type of the GNS/DNS record.
201 uint32_t record_type;
204 * Flags for the record.
206 enum GNUNET_GNSRECORD_Flags flags;
210 GNUNET_NETWORK_STRUCT_BEGIN
213 * Data stored in a PLACE record.
215 struct GNUNET_GNSRECORD_PlaceData
218 * Public key of the place.
220 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
223 * Peer identity of the origin.
225 struct GNUNET_PeerIdentity origin;
228 * Number of relays that follow.
230 uint32_t relay_count GNUNET_PACKED;
232 /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
237 * Information we have in an encrypted block with record data (i.e. in the DHT).
239 struct GNUNET_GNSRECORD_Block
243 * Signature of the block.
245 struct GNUNET_CRYPTO_EcdsaSignature signature;
248 * Derived key used for signing; hash of this is the query.
250 struct GNUNET_CRYPTO_EcdsaPublicKey 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 */
268 * Record type used to box up SRV and TLSA records. For example, a
269 * TLSA record for "_https._tcp.foo.gnu" will be stored under
270 * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
271 * (tcp) and record_type "TLSA". When a BOX record is received, GNS
272 * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
273 * leaves it untouched. This is done to ensure that TLSA (and SRV)
274 * records do not require a separate network request, thus making TLSA
275 * records inseparable from the "main" A/AAAA/VPN/etc. records.
277 struct GNUNET_GNSRECORD_BoxRecord
281 * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
282 * Yes, in IP protocols are usually limited to 8 bits. In NBO.
284 uint16_t protocol GNUNET_PACKED;
287 * Service of the boxed record (aka port number), in NBO.
289 uint16_t service GNUNET_PACKED;
292 * GNS record type of the boxed record. In NBO.
294 uint32_t record_type GNUNET_PACKED;
296 /* followed by the 'original' record */
301 * Record type used internally to keep track of reverse mappings into a
303 * The record contains data related to PKEY delegations from other namespaces to
304 * the namespace the record belongs to.
305 * It is exclusively found under the label ``+''.
307 struct GNUNET_GNSRECORD_ReverseRecord
310 * The public key of the namespace the is delegating to our namespace
312 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
315 * The expiration time of the delegation
317 struct GNUNET_TIME_Absolute expiration;
319 /* followed by the name the delegator uses to refer to our namespace */
322 GNUNET_NETWORK_STRUCT_END
326 * Process a records that were decrypted from a block.
329 * @param rd_count number of entries in @a rd array
330 * @param rd array of records with data to store
332 typedef void (*GNUNET_GNSRECORD_RecordCallback) (void *cls,
333 unsigned int rd_count,
334 const struct GNUNET_GNSRECORD_Data *rd);
338 /* ***************** API related to GNSRECORD plugins ************** */
341 * Convert the binary value @a data of a record of
342 * type @a type to a human-readable string.
344 * @param type type of the record
345 * @param data value in binary encoding
346 * @param data_size number of bytes in @a data
347 * @return NULL on error, otherwise human-readable representation of the value
350 GNUNET_GNSRECORD_value_to_string (uint32_t type,
356 * Convert human-readable version of the value @a s of a record
357 * of type @a type to the respective binary representation.
359 * @param type type of the record
360 * @param s human-readable string
361 * @param data set to value in binary encoding (will be allocated)
362 * @param data_size set to number of bytes in @a data
363 * @return #GNUNET_OK on success
366 GNUNET_GNSRECORD_string_to_value (uint32_t type,
373 * Convert a type name (i.e. "AAAA") to the corresponding number.
375 * @param dns_typename name to convert
376 * @return corresponding number, UINT32_MAX on error
379 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
383 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
385 * @param type number of a type to convert
386 * @return corresponding typestring, NULL on error
389 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
392 /* convenience APIs for serializing / deserializing GNS records */
395 * Calculate how many bytes we will need to serialize the given
398 * @param rd_count number of records in the @a rd array
399 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
400 * @return the required size to serialize
403 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
404 const struct GNUNET_GNSRECORD_Data *rd);
408 * Serialize the given records to the given destination buffer.
410 * @param rd_count number of records in the @a rd array
411 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
412 * @param dest_size size of the destination array @a dst
413 * @param dest where to write the result
414 * @return the size of serialized records, -1 if records do not fit
417 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
418 const struct GNUNET_GNSRECORD_Data *rd,
424 * Deserialize the given records to the given destination.
426 * @param len size of the serialized record data
427 * @param src the serialized record data
428 * @param rd_count number of records in the @a dest array
429 * @param dest where to put the data
430 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
433 GNUNET_GNSRECORD_records_deserialize (size_t len,
435 unsigned int rd_count,
436 struct GNUNET_GNSRECORD_Data *dest);
439 /* ******* general APIs relating to blocks, records and labels ******** */
444 * Test if a given record is expired.
446 * @param rd record to test
447 * @return #GNUNET_YES if the record is expired,
451 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
455 * Convert a UTF-8 string to UTF-8 lowercase
456 * @param src source string
457 * @return converted result
460 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
464 * Convert a zone to a string (for printing debug messages).
465 * This is one of the very few calls in the entire API that is
468 * @param z public key of a zone
469 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s.
472 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
476 * Convert public key to the respective absolute domain name in the
478 * This is one of the very few calls in the entire API that is
481 * @param pkey a public key with a point on the eliptic curve
482 * @return string "X.zkey" where X is the coordinates of the public
483 * key in an encoding suitable for DNS labels.
486 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
490 * Convert an absolute domain name in the ".zkey" pTLD to the
491 * respective public key.
493 * @param zkey string "X.zkey" where X is the public
494 * key in an encoding suitable for DNS labels.
495 * @param pkey set to a public key on the eliptic curve
496 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
499 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
500 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
504 * Calculate the DHT query for a given @a label in a given @a zone.
506 * @param zone private key of the zone
507 * @param label label of the record
508 * @param query hash to use for the query
511 GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
513 struct GNUNET_HashCode *query);
517 * Calculate the DHT query for a given @a label in a given @a zone.
519 * @param pub public key of the zone
520 * @param label label of the record
521 * @param query hash to use for the query
524 GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
526 struct GNUNET_HashCode *query);
530 * Sign name and records
532 * @param key the private key
533 * @param expire block expiration
534 * @param label the name for the records
535 * @param rd record data
536 * @param rd_count number of records in @a rd
538 struct GNUNET_GNSRECORD_Block *
539 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
540 struct GNUNET_TIME_Absolute expire,
542 const struct GNUNET_GNSRECORD_Data *rd,
543 unsigned int rd_count);
547 * Check if a signature is valid. This API is used by the GNS Block
548 * to validate signatures received from the network.
550 * @param block block to verify
551 * @return #GNUNET_OK if the signature is valid
554 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
560 * @param block block to decrypt
561 * @param zone_key public key of the zone
562 * @param label the name for the records
563 * @param proc function to call with the result
564 * @param proc_cls closure for @a proc
565 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
569 GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
570 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
572 GNUNET_GNSRECORD_RecordCallback proc,
577 * Compares if two records are equal
580 * @param b another record
581 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
584 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
585 const struct GNUNET_GNSRECORD_Data *b);
589 * Returns the expiration time of the given block of records. The block
590 * expiration time is the expiration time of the record with smallest
593 * @param rd_count number of records given in @a rd
594 * @param rd array of records
595 * @return absolute expiration time
597 struct GNUNET_TIME_Absolute
598 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
599 const struct GNUNET_GNSRECORD_Data *rd);
602 #if 0 /* keep Emacsens' auto-indent happy */
611 /** @} */ /* end of group */