doxygen: group/module definitions (part 2)
[oweals/gnunet.git] / src / include / gnunet_gnsrecord_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API that can be used to manipulate GNS record data
26  *
27  * @defgroup gns-record  GNS Record library
28  * Manipulate GNS record data
29  * @{
30  */
31 #ifndef GNUNET_GNSRECORD_LIB_H
32 #define GNUNET_GNSRECORD_LIB_H
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42 /**
43  * Maximum size of a value that can be stored in a GNS block.
44  */
45 #define GNUNET_GNSRECORD_MAX_BLOCK_SIZE (63 * 1024)
46
47
48 /**
49  * Record type indicating any record/'*'
50  */
51 #define GNUNET_GNSRECORD_TYPE_ANY 0
52
53 /**
54  * Record type for GNS zone transfer ("PKEY").
55  */
56 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
57
58 /**
59  * Record type for GNS nick names ("NICK").
60  */
61 #define GNUNET_GNSRECORD_TYPE_NICK 65537
62
63 /**
64  * Record type for GNS legacy hostnames ("LEHO").
65  */
66 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
67
68 /**
69  * Record type for VPN resolution
70  */
71 #define GNUNET_GNSRECORD_TYPE_VPN 65539
72
73 /**
74  * Record type for delegation to DNS.
75  */
76 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
77
78 /**
79  * Record type for a boxed record (see TLSA/SRV handling in GNS).
80  */
81 #define GNUNET_GNSRECORD_TYPE_BOX 65541
82
83 /**
84  * Record type for a social place.
85  */
86 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
87
88 /**
89  * Record type for a phone (of CONVERSATION).
90  */
91 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
92
93 /**
94  * Record type for identity attributes (of IDENTITY).
95  */
96 #define GNUNET_GNSRECORD_TYPE_ID_ATTR 65544
97
98 /**
99  * Record type for an identity token (of IDENTITY-TOKEN).
100  */
101 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN 65545
102
103 /**
104  * Record type for the private metadata of an identity token (of IDENTITY-TOKEN).
105  */
106 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA 65546
107
108
109 /**
110  * Flags that can be set for a record.
111  */
112 enum GNUNET_GNSRECORD_Flags
113 {
114
115   /**
116    * No special options.
117    */
118   GNUNET_GNSRECORD_RF_NONE = 0,
119
120   /**
121    * This is a private record of this peer and it should
122    * thus not be handed out to other peers.
123    */
124   GNUNET_GNSRECORD_RF_PRIVATE = 2,
125
126   /**
127    * This flag is currently unused; former RF_PENDING flag
128    *
129    * GNUNET_GNSRECORD_RF_UNUSED = 4,
130    */
131
132   /**
133    * This expiration time of the record is a relative
134    * time (not an absolute time).
135    */
136   GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
137
138   /**
139    * This record should not be used unless all (other) records with an absolute
140    * expiration time have expired.
141    */
142   GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
143
144   /**
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.
154    */
155 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
156 };
157
158
159 /**
160  * A GNS record.
161  */
162 struct GNUNET_GNSRECORD_Data
163 {
164
165   /**
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.
169    */
170   const void *data;
171
172   /**
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).
176    */
177   uint64_t expiration_time;
178
179   /**
180    * Number of bytes in @e data.
181    */
182   size_t data_size;
183
184   /**
185    * Type of the GNS/DNS record.
186    */
187   uint32_t record_type;
188
189   /**
190    * Flags for the record.
191    */
192   enum GNUNET_GNSRECORD_Flags flags;
193 };
194
195
196 GNUNET_NETWORK_STRUCT_BEGIN
197
198 /**
199  * Data stored in a PLACE record.
200  */
201 struct GNUNET_GNSRECORD_PlaceData
202 {
203   /**
204    * Public key of the place.
205    */
206   struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
207
208   /**
209    * Peer identity of the origin.
210    */
211   struct GNUNET_PeerIdentity origin;
212
213   /**
214    * Number of relays that follow.
215    */
216   uint32_t relay_count GNUNET_PACKED;
217
218   /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
219 };
220
221
222 /**
223  * Information we have in an encrypted block with record data (i.e. in the DHT).
224  */
225 struct GNUNET_GNSRECORD_Block
226 {
227
228   /**
229    * Signature of the block.
230    */
231   struct GNUNET_CRYPTO_EcdsaSignature signature;
232
233   /**
234    * Derived key used for signing; hash of this is the query.
235    */
236   struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
237
238   /**
239    * Number of bytes signed; also specifies the number of bytes
240    * of encrypted data that follow.
241    */
242   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
243
244   /**
245    * Expiration time of the block.
246    */
247   struct GNUNET_TIME_AbsoluteNBO expiration_time;
248
249   /* followed by encrypted data */
250 };
251
252
253 /**
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.
262  */
263 struct GNUNET_GNSRECORD_BoxRecord
264 {
265
266   /**
267    * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
268    * Yes, in IP protocols are usually limited to 8 bits. In NBO.
269    */
270   uint16_t protocol GNUNET_PACKED;
271
272   /**
273    * Service of the boxed record (aka port number), in NBO.
274    */
275   uint16_t service GNUNET_PACKED;
276
277   /**
278    * GNS record type of the boxed record. In NBO.
279    */
280   uint32_t record_type GNUNET_PACKED;
281
282   /* followed by the 'original' record */
283
284 };
285
286
287 GNUNET_NETWORK_STRUCT_END
288
289
290 /**
291  * Process a records that were decrypted from a block.
292  *
293  * @param cls closure
294  * @param rd_count number of entries in @a rd array
295  * @param rd array of records with data to store
296  */
297 typedef void (*GNUNET_GNSRECORD_RecordCallback) (void *cls,
298                                                  unsigned int rd_count,
299                                                  const struct GNUNET_GNSRECORD_Data *rd);
300
301
302
303 /* ***************** API related to GNSRECORD plugins ************** */
304
305 /**
306  * Convert the binary value @a data of a record of
307  * type @a type to a human-readable string.
308  *
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
313  */
314 char *
315 GNUNET_GNSRECORD_value_to_string (uint32_t type,
316                                   const void *data,
317                                   size_t data_size);
318
319
320 /**
321  * Convert human-readable version of the value @a s of a record
322  * of type @a type to the respective binary representation.
323  *
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
329  */
330 int
331 GNUNET_GNSRECORD_string_to_value (uint32_t type,
332                                   const char *s,
333                                   void **data,
334                                   size_t *data_size);
335
336
337 /**
338  * Convert a type name (i.e. "AAAA") to the corresponding number.
339  *
340  * @param dns_typename name to convert
341  * @return corresponding number, UINT32_MAX on error
342  */
343 uint32_t
344 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
345
346
347 /**
348  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
349  *
350  * @param type number of a type to convert
351  * @return corresponding typestring, NULL on error
352  */
353 const char *
354 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
355
356
357 /* convenience APIs for serializing / deserializing GNS records */
358
359 /**
360  * Calculate how many bytes we will need to serialize the given
361  * records.
362  *
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
366  */
367 size_t
368 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
369                                    const struct GNUNET_GNSRECORD_Data *rd);
370
371
372 /**
373  * Serialize the given records to the given destination buffer.
374  *
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
380  */
381 ssize_t
382 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
383                                     const struct GNUNET_GNSRECORD_Data *rd,
384                                     size_t dest_size,
385                                     char *dest);
386
387
388 /**
389  * Deserialize the given records to the given destination.
390  *
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
396  */
397 int
398 GNUNET_GNSRECORD_records_deserialize (size_t len,
399                                       const char *src,
400                                       unsigned int rd_count,
401                                       struct GNUNET_GNSRECORD_Data *dest);
402
403
404 /* ******* general APIs relating to blocks, records and labels ******** */
405
406
407
408 /**
409  * Test if a given record is expired.
410  *
411  * @param rd record to test
412  * @return #GNUNET_YES if the record is expired,
413  *         #GNUNET_NO if not
414  */
415 int
416 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
417
418
419 /**
420  * Convert a UTF-8 string to UTF-8 lowercase
421  * @param src source string
422  * @return converted result
423  */
424 char *
425 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
426
427
428 /**
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
431  * NOT reentrant!
432  *
433  * @param z public key of a zone
434  * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s.
435  */
436 const char *
437 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
438
439
440 /**
441  * Convert public key to the respective absolute domain name in the
442  * ".zkey" pTLD.
443  * This is one of the very few calls in the entire API that is
444  * NOT reentrant!
445  *
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.
449  */
450 const char *
451 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
452
453
454 /**
455  * Convert an absolute domain name in the ".zkey" pTLD to the
456  * respective public key.
457  *
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
462  */
463 int
464 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
465                                struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
466
467
468 /**
469  * Calculate the DHT query for a given @a label in a given @a zone.
470  *
471  * @param zone private key of the zone
472  * @param label label of the record
473  * @param query hash to use for the query
474  */
475 void
476 GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
477                                          const char *label,
478                                          struct GNUNET_HashCode *query);
479
480
481 /**
482  * Calculate the DHT query for a given @a label in a given @a zone.
483  *
484  * @param pub public key of the zone
485  * @param label label of the record
486  * @param query hash to use for the query
487  */
488 void
489 GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
490                                         const char *label,
491                                         struct GNUNET_HashCode *query);
492
493
494 /**
495  * Sign name and records
496  *
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
502  */
503 struct GNUNET_GNSRECORD_Block *
504 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
505                                struct GNUNET_TIME_Absolute expire,
506                                const char *label,
507                                const struct GNUNET_GNSRECORD_Data *rd,
508                                unsigned int rd_count);
509
510
511 /**
512  * Check if a signature is valid.  This API is used by the GNS Block
513  * to validate signatures received from the network.
514  *
515  * @param block block to verify
516  * @return #GNUNET_OK if the signature is valid
517  */
518 int
519 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
520
521
522 /**
523  * Decrypt block.
524  *
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
531  *        not well-formed
532  */
533 int
534 GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
535                                 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
536                                 const char *label,
537                                 GNUNET_GNSRECORD_RecordCallback proc,
538                                 void *proc_cls);
539
540
541 /**
542  * Compares if two records are equal
543  *
544  * @param a a record
545  * @param b another record
546  * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
547  */
548 int
549 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
550                               const struct GNUNET_GNSRECORD_Data *b);
551
552
553 /**
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
556  * expiration time.
557  *
558  * @param rd_count number of records given in @a rd
559  * @param rd array of records
560  * @return absolute expiration time
561  */
562 struct GNUNET_TIME_Absolute
563 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
564                                              const struct GNUNET_GNSRECORD_Data *rd);
565
566
567 #if 0                           /* keep Emacsens' auto-indent happy */
568 {
569 #endif
570 #ifdef __cplusplus
571 }
572 #endif
573
574 #endif
575
576 /** @} */  /* end of group */