move to new client API: remove old client API
[oweals/gnunet.git] / src / include / gnunet_gnsrecord_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012, 2013 GNUnet e.V.
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 gnsrecord  GNS Record library
28  * Manipulate GNS record data
29  *
30  * @see [Documentation](https://gnunet.org/gns-plugins)
31  *
32  * @{
33  */
34 #ifndef GNUNET_GNSRECORD_LIB_H
35 #define GNUNET_GNSRECORD_LIB_H
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #if 0                           /* keep Emacsens' auto-indent happy */
41 }
42 #endif
43 #endif
44
45 /**
46  * Maximum size of a value that can be stored in a GNS block.
47  */
48 #define GNUNET_GNSRECORD_MAX_BLOCK_SIZE (63 * 1024)
49
50
51 /**
52  * Record type indicating any record/'*'
53  */
54 #define GNUNET_GNSRECORD_TYPE_ANY 0
55
56 /**
57  * Record type for GNS zone transfer ("PKEY").
58  */
59 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
60
61 /**
62  * Record type for GNS nick names ("NICK").
63  */
64 #define GNUNET_GNSRECORD_TYPE_NICK 65537
65
66 /**
67  * Record type for GNS legacy hostnames ("LEHO").
68  */
69 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
70
71 /**
72  * Record type for VPN resolution
73  */
74 #define GNUNET_GNSRECORD_TYPE_VPN 65539
75
76 /**
77  * Record type for delegation to DNS.
78  */
79 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
80
81 /**
82  * Record type for a boxed record (see TLSA/SRV handling in GNS).
83  */
84 #define GNUNET_GNSRECORD_TYPE_BOX 65541
85
86 /**
87  * Record type for a social place.
88  */
89 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
90
91 /**
92  * Record type for a phone (of CONVERSATION).
93  */
94 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
95
96 /**
97  * Record type for identity attributes (of IDENTITY).
98  */
99 #define GNUNET_GNSRECORD_TYPE_ID_ATTR 65544
100
101 /**
102  * Record type for an identity token (of IDENTITY-TOKEN).
103  */
104 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN 65545
105
106 /**
107  * Record type for the private metadata of an identity token (of IDENTITY-TOKEN).
108  */
109 #define GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA 65546
110
111 /**
112  * Record type for reverse lookups
113  */
114 #define GNUNET_GNSRECORD_TYPE_REVERSE 65548
115
116 /**
117  * Flags that can be set for a record.
118  */
119 enum GNUNET_GNSRECORD_Flags
120 {
121
122   /**
123    * No special options.
124    */
125   GNUNET_GNSRECORD_RF_NONE = 0,
126
127   /**
128    * This is a private record of this peer and it should
129    * thus not be handed out to other peers.
130    */
131   GNUNET_GNSRECORD_RF_PRIVATE = 2,
132
133   /**
134    * This flag is currently unused; former RF_PENDING flag
135    *
136    * GNUNET_GNSRECORD_RF_UNUSED = 4,
137    */
138
139   /**
140    * This expiration time of the record is a relative
141    * time (not an absolute time).
142    */
143   GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
144
145   /**
146    * This record should not be used unless all (other) records with an absolute
147    * expiration time have expired.
148    */
149   GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
150
151   /**
152    * When comparing flags for record equality for removal,
153    * which flags should must match (in addition to the type,
154    * name, expiration value and data of the record)?  All flags
155    * that are not listed here will be ignored for this purpose.
156    * (for example, we don't expect that users will remember to
157    * pass the '--private' option when removing a record from
158    * the namestore, hence we don't require this particular option
159    * to match upon removal).  See also
160    * #GNUNET_GNSRECORD_records_cmp.
161    */
162 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
163 };
164
165
166 /**
167  * A GNS record.
168  */
169 struct GNUNET_GNSRECORD_Data
170 {
171
172   /**
173    * Binary value stored in the DNS record.  Note: "data" must never
174    * be individually 'malloc'ed, but instead always points into some
175    * existing data area.
176    */
177   const void *data;
178
179   /**
180    * Expiration time for the DNS record.  Can be relative
181    * or absolute, depending on @e flags.  Measured in the same
182    * unit as GNUnet time (microseconds).
183    */
184   uint64_t expiration_time;
185
186   /**
187    * Number of bytes in @e data.
188    */
189   size_t data_size;
190
191   /**
192    * Type of the GNS/DNS record.
193    */
194   uint32_t record_type;
195
196   /**
197    * Flags for the record.
198    */
199   enum GNUNET_GNSRECORD_Flags flags;
200 };
201
202
203 GNUNET_NETWORK_STRUCT_BEGIN
204
205 /**
206  * Data stored in a PLACE record.
207  */
208 struct GNUNET_GNSRECORD_PlaceData
209 {
210   /**
211    * Public key of the place.
212    */
213   struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
214
215   /**
216    * Peer identity of the origin.
217    */
218   struct GNUNET_PeerIdentity origin;
219
220   /**
221    * Number of relays that follow.
222    */
223   uint32_t relay_count GNUNET_PACKED;
224
225   /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
226 };
227
228
229 /**
230  * Information we have in an encrypted block with record data (i.e. in the DHT).
231  */
232 struct GNUNET_GNSRECORD_Block
233 {
234
235   /**
236    * Signature of the block.
237    */
238   struct GNUNET_CRYPTO_EcdsaSignature signature;
239
240   /**
241    * Derived key used for signing; hash of this is the query.
242    */
243   struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
244
245   /**
246    * Number of bytes signed; also specifies the number of bytes
247    * of encrypted data that follow.
248    */
249   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
250
251   /**
252    * Expiration time of the block.
253    */
254   struct GNUNET_TIME_AbsoluteNBO expiration_time;
255
256   /* followed by encrypted data */
257 };
258
259
260 /**
261  * Record type used to box up SRV and TLSA records.  For example, a
262  * TLSA record for "_https._tcp.foo.gnu" will be stored under
263  * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
264  * (tcp) and record_type "TLSA".  When a BOX record is received, GNS
265  * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
266  * leaves it untouched.  This is done to ensure that TLSA (and SRV)
267  * records do not require a separate network request, thus making TLSA
268  * records inseparable from the "main" A/AAAA/VPN/etc. records.
269  */
270 struct GNUNET_GNSRECORD_BoxRecord
271 {
272
273   /**
274    * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
275    * Yes, in IP protocols are usually limited to 8 bits. In NBO.
276    */
277   uint16_t protocol GNUNET_PACKED;
278
279   /**
280    * Service of the boxed record (aka port number), in NBO.
281    */
282   uint16_t service GNUNET_PACKED;
283
284   /**
285    * GNS record type of the boxed record. In NBO.
286    */
287   uint32_t record_type GNUNET_PACKED;
288
289   /* followed by the 'original' record */
290
291 };
292
293 /**
294  * Record type used internally to keep track of reverse mappings into a
295  * namespace.
296  * The record contains data related to PKEY delegations from other namespaces to
297  * the namespace the record belongs to.
298  * It is exclusively found under the label ``+''.
299  */
300 struct GNUNET_GNSRECORD_ReverseRecord
301 {
302   /**
303    * The public key of the namespace the is delegating to our namespace
304    */
305   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
306
307   /**
308    * The expiration time of the delegation
309    */
310   struct GNUNET_TIME_Absolute expiration;
311
312   /* followed by the name the delegator uses to refer to our namespace */
313 };
314
315 GNUNET_NETWORK_STRUCT_END
316
317
318 /**
319  * Process a records that were decrypted from a block.
320  *
321  * @param cls closure
322  * @param rd_count number of entries in @a rd array
323  * @param rd array of records with data to store
324  */
325 typedef void (*GNUNET_GNSRECORD_RecordCallback) (void *cls,
326                                                  unsigned int rd_count,
327                                                  const struct GNUNET_GNSRECORD_Data *rd);
328
329
330
331 /* ***************** API related to GNSRECORD plugins ************** */
332
333 /**
334  * Convert the binary value @a data of a record of
335  * type @a type to a human-readable string.
336  *
337  * @param type type of the record
338  * @param data value in binary encoding
339  * @param data_size number of bytes in @a data
340  * @return NULL on error, otherwise human-readable representation of the value
341  */
342 char *
343 GNUNET_GNSRECORD_value_to_string (uint32_t type,
344                                   const void *data,
345                                   size_t data_size);
346
347
348 /**
349  * Convert human-readable version of the value @a s of a record
350  * of type @a type to the respective binary representation.
351  *
352  * @param type type of the record
353  * @param s human-readable string
354  * @param data set to value in binary encoding (will be allocated)
355  * @param data_size set to number of bytes in @a data
356  * @return #GNUNET_OK on success
357  */
358 int
359 GNUNET_GNSRECORD_string_to_value (uint32_t type,
360                                   const char *s,
361                                   void **data,
362                                   size_t *data_size);
363
364
365 /**
366  * Convert a type name (i.e. "AAAA") to the corresponding number.
367  *
368  * @param dns_typename name to convert
369  * @return corresponding number, UINT32_MAX on error
370  */
371 uint32_t
372 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
373
374
375 /**
376  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
377  *
378  * @param type number of a type to convert
379  * @return corresponding typestring, NULL on error
380  */
381 const char *
382 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
383
384
385 /* convenience APIs for serializing / deserializing GNS records */
386
387 /**
388  * Calculate how many bytes we will need to serialize the given
389  * records.
390  *
391  * @param rd_count number of records in the @a rd array
392  * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
393  * @return the required size to serialize
394  */
395 size_t
396 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
397                                    const struct GNUNET_GNSRECORD_Data *rd);
398
399
400 /**
401  * Serialize the given records to the given destination buffer.
402  *
403  * @param rd_count number of records in the @a rd array
404  * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
405  * @param dest_size size of the destination array @a dst
406  * @param dest where to write the result
407  * @return the size of serialized records, -1 if records do not fit
408  */
409 ssize_t
410 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
411                                     const struct GNUNET_GNSRECORD_Data *rd,
412                                     size_t dest_size,
413                                     char *dest);
414
415
416 /**
417  * Deserialize the given records to the given destination.
418  *
419  * @param len size of the serialized record data
420  * @param src the serialized record data
421  * @param rd_count number of records in the @a dest array
422  * @param dest where to put the data
423  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
424  */
425 int
426 GNUNET_GNSRECORD_records_deserialize (size_t len,
427                                       const char *src,
428                                       unsigned int rd_count,
429                                       struct GNUNET_GNSRECORD_Data *dest);
430
431
432 /* ******* general APIs relating to blocks, records and labels ******** */
433
434
435
436 /**
437  * Test if a given record is expired.
438  *
439  * @param rd record to test
440  * @return #GNUNET_YES if the record is expired,
441  *         #GNUNET_NO if not
442  */
443 int
444 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
445
446
447 /**
448  * Convert a UTF-8 string to UTF-8 lowercase
449  * @param src source string
450  * @return converted result
451  */
452 char *
453 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
454
455
456 /**
457  * Convert a zone to a string (for printing debug messages).
458  * This is one of the very few calls in the entire API that is
459  * NOT reentrant!
460  *
461  * @param z public key of a zone
462  * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s.
463  */
464 const char *
465 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
466
467
468 /**
469  * Convert public key to the respective absolute domain name in the
470  * ".zkey" pTLD.
471  * This is one of the very few calls in the entire API that is
472  * NOT reentrant!
473  *
474  * @param pkey a public key with a point on the eliptic curve
475  * @return string "X.zkey" where X is the coordinates of the public
476  *         key in an encoding suitable for DNS labels.
477  */
478 const char *
479 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
480
481
482 /**
483  * Convert an absolute domain name in the ".zkey" pTLD to the
484  * respective public key.
485  *
486  * @param zkey string "X.zkey" where X is the public
487  *         key in an encoding suitable for DNS labels.
488  * @param pkey set to a public key on the eliptic curve
489  * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
490  */
491 int
492 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
493                                struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
494
495
496 /**
497  * Calculate the DHT query for a given @a label in a given @a zone.
498  *
499  * @param zone private key of the zone
500  * @param label label of the record
501  * @param query hash to use for the query
502  */
503 void
504 GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
505                                          const char *label,
506                                          struct GNUNET_HashCode *query);
507
508
509 /**
510  * Calculate the DHT query for a given @a label in a given @a zone.
511  *
512  * @param pub public key of the zone
513  * @param label label of the record
514  * @param query hash to use for the query
515  */
516 void
517 GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
518                                         const char *label,
519                                         struct GNUNET_HashCode *query);
520
521
522 /**
523  * Sign name and records
524  *
525  * @param key the private key
526  * @param expire block expiration
527  * @param label the name for the records
528  * @param rd record data
529  * @param rd_count number of records in @a rd
530  */
531 struct GNUNET_GNSRECORD_Block *
532 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
533                                struct GNUNET_TIME_Absolute expire,
534                                const char *label,
535                                const struct GNUNET_GNSRECORD_Data *rd,
536                                unsigned int rd_count);
537
538
539 /**
540  * Check if a signature is valid.  This API is used by the GNS Block
541  * to validate signatures received from the network.
542  *
543  * @param block block to verify
544  * @return #GNUNET_OK if the signature is valid
545  */
546 int
547 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
548
549
550 /**
551  * Decrypt block.
552  *
553  * @param block block to decrypt
554  * @param zone_key public key of the zone
555  * @param label the name for the records
556  * @param proc function to call with the result
557  * @param proc_cls closure for @a proc
558  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
559  *        not well-formed
560  */
561 int
562 GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
563                                 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
564                                 const char *label,
565                                 GNUNET_GNSRECORD_RecordCallback proc,
566                                 void *proc_cls);
567
568
569 /**
570  * Compares if two records are equal
571  *
572  * @param a a record
573  * @param b another record
574  * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
575  */
576 int
577 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
578                               const struct GNUNET_GNSRECORD_Data *b);
579
580
581 /**
582  * Returns the expiration time of the given block of records. The block
583  * expiration time is the expiration time of the record with smallest
584  * expiration time.
585  *
586  * @param rd_count number of records given in @a rd
587  * @param rd array of records
588  * @return absolute expiration time
589  */
590 struct GNUNET_TIME_Absolute
591 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
592                                              const struct GNUNET_GNSRECORD_Data *rd);
593
594
595 #if 0                           /* keep Emacsens' auto-indent happy */
596 {
597 #endif
598 #ifdef __cplusplus
599 }
600 #endif
601
602 #endif
603
604 /** @} */  /* end of group */