990deab1e902cd9e65c3e51ad113d8b65912efd6
[oweals/gnunet.git] / src / namestore / namestore.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 namestore/namestore.h
23  * @brief common internal definitions for namestore service
24  * @author Matthias Wachs
25  */
26 #ifndef NAMESTORE_H
27 #define NAMESTORE_H
28
29 /**
30  * Maximum length of any name, including 0-termination.
31  */
32 #define MAX_NAME_LEN 256
33
34
35
36 /*
37  * Collect message types here, move to protocols later
38  */
39 #define GNUNET_MESSAGE_TYPE_NAMESTORE_START 430
40 #define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME 431
41 #define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE 432
42 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT 433
43 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE 434
44 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE 435
45 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE 436
46 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE 437
47 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE 438
48 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME 439
49 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE 440
50
51 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START 445
52 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE 446
53 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT 447
54 #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP 448
55
56
57 /**
58  * Convert a short hash to a string (for printing debug messages).
59  * This is one of the very few calls in the entire API that is
60  * NOT reentrant!
61  *
62  * @param hc the short hash code
63  * @return string form; will be overwritten by next call to GNUNET_h2s.
64  */
65 const char *
66 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc);
67
68
69 /**
70  * Sign name and records
71  *
72  * @param key the private key
73  * @param expire block expiration
74  * @param name the name
75  * @param rd record data
76  * @param rd_count number of records
77  *
78  * @return the signature
79  */
80 struct GNUNET_CRYPTO_RsaSignature *
81 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
82     struct GNUNET_TIME_Absolute expire,
83     const char *name,
84     const struct GNUNET_NAMESTORE_RecordData *rd,
85     unsigned int rd_count);
86
87
88 /**
89  * Compares if two records are equal
90  *
91  * @param a Record a
92  * @param b Record b
93  *
94  * @return GNUNET_YES or GNUNET_NO
95  */
96 int
97 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
98                               const struct GNUNET_NAMESTORE_RecordData *b);
99
100
101 GNUNET_NETWORK_STRUCT_BEGIN
102 /**
103  * A GNS record serialized for network transmission.
104  *
105  * Layout is [struct GNUNET_NAMESTORE_NetworkRecord][char[data_size] data]
106  */
107 struct GNUNET_NAMESTORE_NetworkRecord
108 {
109   /**
110    * Expiration time for the DNS record.
111    */
112   struct GNUNET_TIME_AbsoluteNBO expiration;
113
114   /**
115    * Number of bytes in 'data'.
116    */
117   uint32_t data_size;
118
119   /**
120    * Type of the GNS/DNS record.
121    */
122   uint32_t record_type;
123
124   /**
125    * Flags for the record.
126    */
127   uint32_t flags;
128 };
129
130
131
132 /**
133  * Connect to namestore service.  FIXME: UNNECESSARY.
134  */
135 struct StartMessage
136 {
137
138   /**
139    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_START
140    */
141   struct GNUNET_MessageHeader header;
142
143 };
144
145
146 /**
147  * Generic namestore message with op id
148  */
149 struct GNUNET_NAMESTORE_Header
150 {
151   /**
152    * header.type will be GNUNET_MESSAGE_TYPE_NAMESTORE_*
153    * header.size will be message size
154    */
155   struct GNUNET_MessageHeader header;
156
157   /**
158    * Request ID in NBO
159    */
160   uint32_t r_id;
161 };
162
163
164 /**
165  * Lookup a name in the namestore
166  */
167 struct LookupNameMessage
168 {
169   struct GNUNET_NAMESTORE_Header gns_header;
170
171   /**
172    * The zone 
173    */
174   struct GNUNET_CRYPTO_ShortHashCode zone;
175
176   /**
177    * Requested record type 
178    */
179   uint32_t record_type;
180
181   /**
182    * Length of the name
183    */
184   uint32_t name_len;
185
186   /* 0-terminated name here */
187 };
188
189
190 /**
191  * Lookup response
192  */
193 struct LookupNameResponseMessage
194 {
195   /**
196    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE
197    */
198   struct GNUNET_NAMESTORE_Header gns_header;
199
200   /**
201    * Expiration time
202    */
203   struct GNUNET_TIME_AbsoluteNBO expire;
204
205
206   /**
207    * Name length
208    */
209   uint16_t name_len;
210
211   /**
212    * Bytes of serialized record data
213    */
214   uint16_t rd_len;
215
216   /**
217    * Number of records contained
218    */
219   uint16_t rd_count;
220
221   /**
222    * Is the signature valid
223    * GNUNET_YES or GNUNET_NO
224    */
225   int16_t contains_sig;
226
227   /**
228    * All zeros if 'contains_sig' is GNUNET_NO.
229    */
230   struct GNUNET_CRYPTO_RsaSignature signature;
231
232   /**
233    * The public key for the name
234    */
235   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
236
237   /* 0-terminated name and serialized record data */
238   /* rd_len bytes serialized record data */
239 };
240
241
242 /**
243  * Put a record to the namestore
244  */
245 struct RecordPutMessage
246 {
247   /**
248    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_RECORD_PUT
249    */
250   struct GNUNET_NAMESTORE_Header gns_header;
251
252   /**
253    * Expiration time
254    */
255   struct GNUNET_TIME_AbsoluteNBO expire;
256
257   /**
258    * Name length
259    */
260   uint16_t name_len;
261
262   /**
263    * Length of serialized record data
264    */
265   uint16_t rd_len;
266
267   /**
268    * Number of records contained 
269    */
270   uint16_t rd_count;
271
272   /**
273    * always zero (for alignment)
274    */
275   uint16_t reserved;
276
277   /**
278    * The signature
279    */
280   struct GNUNET_CRYPTO_RsaSignature signature;
281
282   /**
283    * The public key
284    */
285   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
286
287   /* name (0-terminated) followed by "rd_count" serialized records */
288
289 };
290
291
292 /**
293  * Put a record to the namestore response
294  */
295 struct RecordPutResponseMessage
296 {
297   /**
298    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE
299    */
300   struct GNUNET_NAMESTORE_Header gns_header;
301
302   /**
303    * result:
304    * GNUNET_SYSERR on failure
305    * GNUNET_OK on success
306    */
307   int32_t op_result;
308 };
309
310
311 /**
312  * Create a record and put it to the namestore
313  * Memory layout:
314  */
315 struct RecordCreateMessage
316 {
317   /**
318    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE
319    */
320   struct GNUNET_NAMESTORE_Header gns_header;
321
322   struct GNUNET_TIME_AbsoluteNBO expire;
323
324   /**
325    * Name length
326    */
327   uint16_t name_len;
328
329   /**
330    * Length of serialized record data
331    */
332   uint16_t rd_len;
333
334   /**
335    * Record count 
336    */
337   uint16_t rd_count;
338
339   /**
340    * private key length 
341    */
342   uint16_t pkey_len;
343
344   /* followed by:
345    * GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded private key with length pkey_len
346    * name with length name_len
347    * serialized record data with length rd_len
348    * */
349 };
350
351
352 /**
353  * Create a record to the namestore response
354  */
355 struct RecordCreateResponseMessage
356 {
357   /**
358    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE
359    */
360   struct GNUNET_NAMESTORE_Header gns_header;
361
362   /**
363    *  name length: GNUNET_NO already exists, GNUNET_YES on success, GNUNET_SYSERR error
364    */
365   int32_t op_result;
366 };
367
368
369 /**
370  * Remove a record from the namestore
371  * Memory layout:
372  */
373 struct RecordRemoveMessage
374 {
375   /**
376    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE
377    */
378   struct GNUNET_NAMESTORE_Header gns_header;
379
380   /**
381    * Name length 
382    */
383   uint16_t name_len;
384
385   /**
386    * Length of serialized rd data 
387    */
388   uint16_t rd_len;
389
390   /**
391    * Number of records contained 
392    */
393   uint16_t rd_count;
394
395   /**
396    * Length of private key
397    */
398   uint16_t pkey_len;
399
400   /* followed by:
401    * GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded private key with length pkey_len
402    * name with length name_len
403    * serialized record data with length rd_len
404    * */
405 };
406
407
408 /**
409  * Removal of the record succeeded.
410  */
411 #define RECORD_REMOVE_RESULT_SUCCESS 0
412
413 /**
414  * There are NO records for the given name.
415  */
416 #define RECORD_REMOVE_RESULT_NO_RECORDS 1
417
418 /**
419  * The specific record that was to be removed was
420  * not found.
421  */
422 #define RECORD_REMOVE_RESULT_RECORD_NOT_FOUND 2
423
424 /**
425  * Internal error, failed to sign the remaining records.
426  * (Note: not used?)
427  */
428 #define RECORD_REMOVE_RESULT_FAILED_TO_SIGN 3
429
430 /**
431  * Internal error, failed to store the updated record set
432  */
433 #define RECORD_REMOVE_RESULT_FAILED_TO_PUT_UPDATE 4
434
435 /**
436  * Internal error, failed to remove records from database
437  */
438 #define RECORD_REMOVE_RESULT_FAILED_TO_REMOVE 5
439
440
441 /**
442  * Remove a record from the namestore response
443  */
444 struct RecordRemoveResponseMessage
445 {
446   /**
447    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE
448    */
449   struct GNUNET_NAMESTORE_Header gns_header;
450
451   /**
452    * Result code (see RECORD_REMOVE_RESULT_*).  In network byte order.
453    */
454   int32_t op_result;
455 };
456
457
458 /**
459  * Lookup a name for a zone hash
460  */
461 struct ZoneToNameMessage
462 {
463   /**
464    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME
465    */
466   struct GNUNET_NAMESTORE_Header gns_header;
467
468   /**
469    * The hash of public key of the zone to look up in 
470    */
471   struct GNUNET_CRYPTO_ShortHashCode zone;
472
473   /**
474    * The  hash of the public key of the target zone  
475    */
476   struct GNUNET_CRYPTO_ShortHashCode value_zone;
477 };
478
479 /**
480  * Respone for zone to name lookup
481  */
482 struct ZoneToNameResponseMessage
483 {
484   /**
485    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE
486    */
487   struct GNUNET_NAMESTORE_Header gns_header;
488
489   /**
490    * Record block expiration
491    */
492   struct GNUNET_TIME_AbsoluteNBO expire;
493
494   /**
495    * Length of the name
496    */
497   uint16_t name_len;
498
499   /**
500    * Length of serialized record data
501    */
502   uint16_t rd_len;
503
504   /**
505    * Number of records contained
506    */
507   uint16_t rd_count;
508
509   /* result in NBO: GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error */
510   int16_t res;
511
512   /**
513    * Signature
514    */
515   struct GNUNET_CRYPTO_RsaSignature signature;
516
517   /**
518    * Publik key
519    */
520   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
521
522 };
523
524
525
526 /**
527  * Start a zone iteration for the given zone
528  */
529 struct ZoneIterationStartMessage
530 {
531   /**
532    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START
533    */
534   struct GNUNET_NAMESTORE_Header gns_header;
535
536   /**
537    * Zone hash
538    */
539   struct GNUNET_CRYPTO_ShortHashCode zone;
540
541   /**
542    * Which flags must be included
543    */
544   uint16_t must_have_flags;
545
546   /**
547    * Which flags must not be included
548    */
549   uint16_t must_not_have_flags;
550 };
551
552
553 /**
554  * Ask for next result of zone iteration for the given operation
555  */
556 struct ZoneIterationNextMessage
557 {
558   /**
559    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT
560    */
561   struct GNUNET_NAMESTORE_Header gns_header;
562 };
563
564
565 /**
566  * Stop zone iteration for the given operation
567  */
568 struct ZoneIterationStopMessage
569 {
570   /**
571    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP
572    */
573   struct GNUNET_NAMESTORE_Header gns_header;
574 };
575
576 /**
577  * Next result of zone iteration for the given operation
578  * // FIXME: use 'struct LookupResponseMessage' instead? (identical except
579  * for having 'contains_sig' instead of 'reserved', but fully compatible otherwise).
580  */
581 struct ZoneIterationResponseMessage
582 {
583   /**
584    * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE
585    */
586   struct GNUNET_NAMESTORE_Header gns_header;
587
588   struct GNUNET_TIME_AbsoluteNBO expire;
589
590   uint16_t name_len;
591
592   /* Record data length */
593   uint16_t rd_len;
594
595   /**
596    * Number of records contained 
597    */
598   uint16_t rd_count;
599
600   /**
601    * always zero (for alignment)
602    */
603   uint16_t reserved;
604
605   /**
606    * All zeros if 'contains_sig' is GNUNET_NO.
607    */
608   struct GNUNET_CRYPTO_RsaSignature signature;
609
610   /**
611    * The public key
612    */
613   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
614
615  
616  
617 };
618 GNUNET_NETWORK_STRUCT_END
619
620
621 /* end of namestore.h */
622 #endif