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_GNSRECORD_MAX_BLOCK_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 nick names ("NICK").
56 #define GNUNET_GNSRECORD_TYPE_NICK 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 boxed record (see TLSA/SRV handling in GNS).
76 #define GNUNET_GNSRECORD_TYPE_BOX 65541
79 * Record type for a social place.
81 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
84 * Record type for a phone (of CONVERSATION).
86 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
90 * Flags that can be set for a record.
92 enum GNUNET_GNSRECORD_Flags
98 GNUNET_GNSRECORD_RF_NONE = 0,
101 * This is a private record of this peer and it should
102 * thus not be handed out to other peers.
104 GNUNET_GNSRECORD_RF_PRIVATE = 2,
107 * This record was added automatically by the system
108 * and is pending user confimation.
110 GNUNET_GNSRECORD_RF_PENDING = 4,
113 * This expiration time of the record is a relative
114 * time (not an absolute time).
116 GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
119 * This record should not be used unless all (other) records with an absolute
120 * expiration time have expired.
122 GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
125 * When comparing flags for record equality for removal,
126 * which flags should must match (in addition to the type,
127 * name, expiration value and data of the record)? All flags
128 * that are not listed here will be ignored for this purpose.
129 * (for example, we don't expect that users will remember to
130 * pass the '--private' option when removing a record from
131 * the namestore, hence we don't require this particular option
132 * to match upon removal). See also
133 * #GNUNET_GNSRECORD_records_cmp.
135 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
142 struct GNUNET_GNSRECORD_Data
146 * Binary value stored in the DNS record. Note: "data" must never
147 * be individually 'malloc'ed, but instead always points into some
148 * existing data area.
153 * Expiration time for the DNS record. Can be relative
154 * or absolute, depending on @e flags. Measured in the same
155 * unit as GNUnet time (microseconds).
157 uint64_t expiration_time;
160 * Number of bytes in @e data.
165 * Type of the GNS/DNS record.
167 uint32_t record_type;
170 * Flags for the record.
172 enum GNUNET_GNSRECORD_Flags flags;
177 GNUNET_NETWORK_STRUCT_BEGIN
181 * Information we have in an encrypted block with record data (i.e. in the DHT).
183 struct GNUNET_GNSRECORD_Block
187 * Signature of the block.
189 struct GNUNET_CRYPTO_EcdsaSignature signature;
192 * Derived key used for signing; hash of this is the query.
194 struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
197 * Number of bytes signed; also specifies the number of bytes
198 * of encrypted data that follow.
200 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
203 * Expiration time of the block.
205 struct GNUNET_TIME_AbsoluteNBO expiration_time;
207 /* followed by encrypted data */
212 * Record type used to box up SRV and TLSA records. For example, a
213 * TLSA record for "_https._tcp.foo.gnu" will be stored under
214 * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
215 * (tcp) and record_type "TLSA". When a BOX record is received, GNS
216 * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
217 * leaves it untouched. This is done to ensure that TLSA (and SRV)
218 * records do not require a separate network request, thus making TLSA
219 * records inseparable from the "main" A/AAAA/VPN/etc. records.
221 struct GNUNET_GNSRECORD_BoxRecord
225 * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
226 * Yes, in IP protocols are usually limited to 8 bits. In NBO.
228 uint16_t protocol GNUNET_PACKED;
231 * Service of the boxed record (aka port number), in NBO.
233 uint16_t service GNUNET_PACKED;
236 * GNS record type of the boxed record. In NBO.
238 uint32_t record_type GNUNET_PACKED;
240 /* followed by the 'original' record */
245 GNUNET_NETWORK_STRUCT_END
249 * Process a records that were decrypted from a block.
252 * @param rd_count number of entries in @a rd array
253 * @param rd array of records with data to store
255 typedef void (*GNUNET_GNSRECORD_RecordCallback) (void *cls,
256 unsigned int rd_count,
257 const struct GNUNET_GNSRECORD_Data *rd);
261 /* ***************** API related to GNSRECORD plugins ************** */
264 * Convert the binary value @a data of a record of
265 * type @a type to a human-readable string.
267 * @param type type of the record
268 * @param data value in binary encoding
269 * @param data_size number of bytes in @a data
270 * @return NULL on error, otherwise human-readable representation of the value
273 GNUNET_GNSRECORD_value_to_string (uint32_t type,
279 * Convert human-readable version of the value @a s of a record
280 * of type @a type to the respective binary representation.
282 * @param type type of the record
283 * @param s human-readable string
284 * @param data set to value in binary encoding (will be allocated)
285 * @param data_size set to number of bytes in @a data
286 * @return #GNUNET_OK on success
289 GNUNET_GNSRECORD_string_to_value (uint32_t type,
296 * Convert a type name (i.e. "AAAA") to the corresponding number.
298 * @param dns_typename name to convert
299 * @return corresponding number, UINT32_MAX on error
302 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
306 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
308 * @param type number of a type to convert
309 * @return corresponding typestring, NULL on error
312 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
315 /* convenience APIs for serializing / deserializing GNS records */
318 * Calculate how many bytes we will need to serialize the given
321 * @param rd_count number of records in the @a rd array
322 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
323 * @return the required size to serialize
326 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
327 const struct GNUNET_GNSRECORD_Data *rd);
331 * Serialize the given records to the given destination buffer.
333 * @param rd_count number of records in the @a rd array
334 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
335 * @param dest_size size of the destination array @a dst
336 * @param dest where to write the result
337 * @return the size of serialized records, -1 if records do not fit
340 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
341 const struct GNUNET_GNSRECORD_Data *rd,
347 * Deserialize the given records to the given destination.
349 * @param len size of the serialized record data
350 * @param src the serialized record data
351 * @param rd_count number of records in the @a dest array
352 * @param dest where to put the data
353 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
356 GNUNET_GNSRECORD_records_deserialize (size_t len,
358 unsigned int rd_count,
359 struct GNUNET_GNSRECORD_Data *dest);
362 /* ******* general APIs relating to blocks, records and labels ******** */
367 * Test if a given record is expired.
369 * @param rd record to test
370 * @return #GNUNET_YES if the record is expired,
374 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
378 * Convert a UTF-8 string to UTF-8 lowercase
379 * @param src source string
380 * @return converted result
383 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
387 * Convert a zone to a string (for printing debug messages).
388 * This is one of the very few calls in the entire API that is
391 * @param z public key of a zone
392 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s.
395 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
399 * Convert public key to the respective absolute domain name in the
401 * This is one of the very few calls in the entire API that is
404 * @param pkey a public key with a point on the eliptic curve
405 * @return string "X.zkey" where X is the coordinates of the public
406 * key in an encoding suitable for DNS labels.
409 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
413 * Convert an absolute domain name in the ".zkey" pTLD to the
414 * respective public key.
416 * @param zkey string "X.zkey" where X is the public
417 * key in an encoding suitable for DNS labels.
418 * @param pkey set to a public key on the eliptic curve
419 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
422 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
423 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
427 * Calculate the DHT query for a given @a label in a given @a zone.
429 * @param zone private key of the zone
430 * @param label label of the record
431 * @param query hash to use for the query
434 GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
436 struct GNUNET_HashCode *query);
440 * Calculate the DHT query for a given @a label in a given @a zone.
442 * @param pub public key of the zone
443 * @param label label of the record
444 * @param query hash to use for the query
447 GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
449 struct GNUNET_HashCode *query);
453 * Sign name and records
455 * @param key the private key
456 * @param expire block expiration
457 * @param label the name for the records
458 * @param rd record data
459 * @param rd_count number of records in @a rd
461 struct GNUNET_GNSRECORD_Block *
462 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
463 struct GNUNET_TIME_Absolute expire,
465 const struct GNUNET_GNSRECORD_Data *rd,
466 unsigned int rd_count);
470 * Check if a signature is valid. This API is used by the GNS Block
471 * to validate signatures received from the network.
473 * @param block block to verify
474 * @return #GNUNET_OK if the signature is valid
477 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
483 * @param block block to decrypt
484 * @param zone_key public key of the zone
485 * @param label the name for the records
486 * @param proc function to call with the result
487 * @param proc_cls closure for @a proc
488 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
492 GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
493 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
495 GNUNET_GNSRECORD_RecordCallback proc,
500 * Compares if two records are equal
503 * @param b another record
504 * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
507 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
508 const struct GNUNET_GNSRECORD_Data *b);
512 * Returns the expiration time of the given block of records. The block
513 * expiration time is the expiration time of the record with smallest
516 * @param rd_count number of records given in @a rd
517 * @param rd array of records
518 * @return absolute expiration time
520 struct GNUNET_TIME_Absolute
521 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
522 const struct GNUNET_GNSRECORD_Data *rd);
526 #if 0 /* keep Emacsens' auto-indent happy */