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