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