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