2 This file is part of GNUnet
3 Copyright (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., 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
31 #ifndef GNUNET_GNSRECORD_LIB_H
32 #define GNUNET_GNSRECORD_LIB_H
37 #if 0 /* keep Emacsens' auto-indent happy */
43 * Maximum size of a value that can be stored in a GNS block.
45 #define GNUNET_GNSRECORD_MAX_BLOCK_SIZE (63 * 1024)
49 * Record type indicating any record/'*'
51 #define GNUNET_GNSRECORD_TYPE_ANY 0
54 * Record type for GNS zone transfer ("PKEY").
56 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
59 * Record type for GNS nick names ("NICK").
61 #define GNUNET_GNSRECORD_TYPE_NICK 65537
64 * Record type for GNS legacy hostnames ("LEHO").
66 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
69 * Record type for VPN resolution
71 #define GNUNET_GNSRECORD_TYPE_VPN 65539
74 * Record type for delegation to DNS.
76 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
79 * Record type for a boxed record (see TLSA/SRV handling in GNS).
81 #define GNUNET_GNSRECORD_TYPE_BOX 65541
84 * Record type for a social place.
86 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
89 * Record type for a phone (of CONVERSATION).
91 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
94 * Record type for identity attributes (of IDENTITY).
96 #define GNUNET_GNSRECORD_TYPE_ID_ATTR 65544
99 * Record type for an identity token (of IDENTITY-TOKEN).
101 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN 65545
104 * Record type for the private metadata of an identity token (of IDENTITY-TOKEN).
106 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA 65546
110 * Flags that can be set for a record.
112 enum GNUNET_GNSRECORD_Flags
116 * No special options.
118 GNUNET_GNSRECORD_RF_NONE = 0,
121 * This is a private record of this peer and it should
122 * thus not be handed out to other peers.
124 GNUNET_GNSRECORD_RF_PRIVATE = 2,
127 * This flag is currently unused; former RF_PENDING flag
129 * GNUNET_GNSRECORD_RF_UNUSED = 4,
133 * This expiration time of the record is a relative
134 * time (not an absolute time).
136 GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
139 * This record should not be used unless all (other) records with an absolute
140 * expiration time have expired.
142 GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
145 * When comparing flags for record equality for removal,
146 * which flags should must match (in addition to the type,
147 * name, expiration value and data of the record)? All flags
148 * that are not listed here will be ignored for this purpose.
149 * (for example, we don't expect that users will remember to
150 * pass the '--private' option when removing a record from
151 * the namestore, hence we don't require this particular option
152 * to match upon removal). See also
153 * #GNUNET_GNSRECORD_records_cmp.
155 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
162 struct GNUNET_GNSRECORD_Data
166 * Binary value stored in the DNS record. Note: "data" must never
167 * be individually 'malloc'ed, but instead always points into some
168 * existing data area.
173 * Expiration time for the DNS record. Can be relative
174 * or absolute, depending on @e flags. Measured in the same
175 * unit as GNUnet time (microseconds).
177 uint64_t expiration_time;
180 * Number of bytes in @e data.
185 * Type of the GNS/DNS record.
187 uint32_t record_type;
190 * Flags for the record.
192 enum GNUNET_GNSRECORD_Flags flags;
196 GNUNET_NETWORK_STRUCT_BEGIN
199 * Data stored in a PLACE record.
201 struct GNUNET_GNSRECORD_PlaceData
204 * Public key of the place.
206 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
209 * Peer identity of the origin.
211 struct GNUNET_PeerIdentity origin;
214 * Number of relays that follow.
216 uint32_t relay_count GNUNET_PACKED;
218 /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
223 * Information we have in an encrypted block with record data (i.e. in the DHT).
225 struct GNUNET_GNSRECORD_Block
229 * Signature of the block.
231 struct GNUNET_CRYPTO_EcdsaSignature signature;
234 * Derived key used for signing; hash of this is the query.
236 struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
239 * Number of bytes signed; also specifies the number of bytes
240 * of encrypted data that follow.
242 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
245 * Expiration time of the block.
247 struct GNUNET_TIME_AbsoluteNBO expiration_time;
249 /* followed by encrypted data */
254 * Record type used to box up SRV and TLSA records. For example, a
255 * TLSA record for "_https._tcp.foo.gnu" will be stored under
256 * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
257 * (tcp) and record_type "TLSA". When a BOX record is received, GNS
258 * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
259 * leaves it untouched. This is done to ensure that TLSA (and SRV)
260 * records do not require a separate network request, thus making TLSA
261 * records inseparable from the "main" A/AAAA/VPN/etc. records.
263 struct GNUNET_GNSRECORD_BoxRecord
267 * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
268 * Yes, in IP protocols are usually limited to 8 bits. In NBO.
270 uint16_t protocol GNUNET_PACKED;
273 * Service of the boxed record (aka port number), in NBO.
275 uint16_t service GNUNET_PACKED;
278 * GNS record type of the boxed record. In NBO.
280 uint32_t record_type GNUNET_PACKED;
282 /* followed by the 'original' record */
287 GNUNET_NETWORK_STRUCT_END
291 * Process a records that were decrypted from a block.
294 * @param rd_count number of entries in @a rd array
295 * @param rd array of records with data to store
297 typedef void (*GNUNET_GNSRECORD_RecordCallback) (void *cls,
298 unsigned int rd_count,
299 const struct GNUNET_GNSRECORD_Data *rd);
303 /* ***************** API related to GNSRECORD plugins ************** */
306 * Convert the binary value @a data of a record of
307 * type @a type to a human-readable string.
309 * @param type type of the record
310 * @param data value in binary encoding
311 * @param data_size number of bytes in @a data
312 * @return NULL on error, otherwise human-readable representation of the value
315 GNUNET_GNSRECORD_value_to_string (uint32_t type,
321 * Convert human-readable version of the value @a s of a record
322 * of type @a type to the respective binary representation.
324 * @param type type of the record
325 * @param s human-readable string
326 * @param data set to value in binary encoding (will be allocated)
327 * @param data_size set to number of bytes in @a data
328 * @return #GNUNET_OK on success
331 GNUNET_GNSRECORD_string_to_value (uint32_t type,
338 * Convert a type name (i.e. "AAAA") to the corresponding number.
340 * @param dns_typename name to convert
341 * @return corresponding number, UINT32_MAX on error
344 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
348 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
350 * @param type number of a type to convert
351 * @return corresponding typestring, NULL on error
354 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
357 /* convenience APIs for serializing / deserializing GNS records */
360 * Calculate how many bytes we will need to serialize the given
363 * @param rd_count number of records in the @a rd array
364 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
365 * @return the required size to serialize
368 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
369 const struct GNUNET_GNSRECORD_Data *rd);
373 * Serialize the given records to the given destination buffer.
375 * @param rd_count number of records in the @a rd array
376 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
377 * @param dest_size size of the destination array @a dst
378 * @param dest where to write the result
379 * @return the size of serialized records, -1 if records do not fit
382 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
383 const struct GNUNET_GNSRECORD_Data *rd,
389 * Deserialize the given records to the given destination.
391 * @param len size of the serialized record data
392 * @param src the serialized record data
393 * @param rd_count number of records in the @a dest array
394 * @param dest where to put the data
395 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
398 GNUNET_GNSRECORD_records_deserialize (size_t len,
400 unsigned int rd_count,
401 struct GNUNET_GNSRECORD_Data *dest);
404 /* ******* general APIs relating to blocks, records and labels ******** */
409 * Test if a given record is expired.
411 * @param rd record to test
412 * @return #GNUNET_YES if the record is expired,
416 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
420 * Convert a UTF-8 string to UTF-8 lowercase
421 * @param src source string
422 * @return converted result
425 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
429 * Convert a zone to a string (for printing debug messages).
430 * This is one of the very few calls in the entire API that is
433 * @param z public key of a zone
434 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s.
437 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
441 * Convert public key to the respective absolute domain name in the
443 * This is one of the very few calls in the entire API that is
446 * @param pkey a public key with a point on the eliptic curve
447 * @return string "X.zkey" where X is the coordinates of the public
448 * key in an encoding suitable for DNS labels.
451 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
455 * Convert an absolute domain name in the ".zkey" pTLD to the
456 * respective public key.
458 * @param zkey string "X.zkey" where X is the public
459 * key in an encoding suitable for DNS labels.
460 * @param pkey set to a public key on the eliptic curve
461 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
464 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
465 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
469 * Calculate the DHT query for a given @a label in a given @a zone.
471 * @param zone private key of the zone
472 * @param label label of the record
473 * @param query hash to use for the query
476 GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
478 struct GNUNET_HashCode *query);
482 * Calculate the DHT query for a given @a label in a given @a zone.
484 * @param pub public key of the zone
485 * @param label label of the record
486 * @param query hash to use for the query
489 GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
491 struct GNUNET_HashCode *query);
495 * Sign name and records
497 * @param key the private key
498 * @param expire block expiration
499 * @param label the name for the records
500 * @param rd record data
501 * @param rd_count number of records in @a rd
503 struct GNUNET_GNSRECORD_Block *
504 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
505 struct GNUNET_TIME_Absolute expire,
507 const struct GNUNET_GNSRECORD_Data *rd,
508 unsigned int rd_count);
512 * Check if a signature is valid. This API is used by the GNS Block
513 * to validate signatures received from the network.
515 * @param block block to verify
516 * @return #GNUNET_OK if the signature is valid
519 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
525 * @param block block to decrypt
526 * @param zone_key public key of the zone
527 * @param label the name for the records
528 * @param proc function to call with the result
529 * @param proc_cls closure for @a proc
530 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
534 GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
535 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
537 GNUNET_GNSRECORD_RecordCallback proc,
542 * Compares if two records are equal
545 * @param b another record
546 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
549 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
550 const struct GNUNET_GNSRECORD_Data *b);
554 * Returns the expiration time of the given block of records. The block
555 * expiration time is the expiration time of the record with smallest
558 * @param rd_count number of records given in @a rd
559 * @param rd array of records
560 * @return absolute expiration time
562 struct GNUNET_TIME_Absolute
563 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
564 const struct GNUNET_GNSRECORD_Data *rd);
567 #if 0 /* keep Emacsens' auto-indent happy */
576 /** @} */ /* end of group */