- basics for the api
[oweals/gnunet.git] / src / namestore / namestore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 gns/namestore_api.c
23  * @brief API to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "namestore.h"
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
37
38 /**
39  * A QueueEntry.
40  */
41 struct GNUNET_NAMESTORE_QueueEntry
42 {
43   char *data; /*stub data pointer*/
44 };
45
46
47 /**
48  * Message in linked list we should send to the service.  The
49  * actual binary message follows this struct.
50  */
51 struct PendingMessage
52 {
53
54   /**
55    * Kept in a DLL.
56    */
57   struct PendingMessage *next;
58
59   /**
60    * Kept in a DLL.
61    */
62   struct PendingMessage *prev;
63
64   /**
65    * Size of the message.
66    */
67   size_t size;
68
69   /**
70    * Is this the 'START' message?
71    */
72   int is_init;
73 };
74
75
76 /**
77  * Connection to the NAMESTORE service.
78  */
79 struct GNUNET_NAMESTORE_Handle
80 {
81
82   /**
83    * Configuration to use.
84    */
85   const struct GNUNET_CONFIGURATION_Handle *cfg;
86
87   /**
88    * Socket (if available).
89    */
90   struct GNUNET_CLIENT_Connection *client;
91
92   /**
93    * Currently pending transmission request (or NULL).
94    */
95   struct GNUNET_CLIENT_TransmitHandle *th;
96
97   /**
98    * Reconnect task
99    */
100   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
101
102   struct PendingMessage * pending_head;
103   struct PendingMessage * pending_tail;
104
105   /**
106    * Should we reconnect to service due to some serious error?
107    */
108   int reconnect;
109 };
110
111 struct GNUNET_NAMESTORE_SimpleRecord
112 {
113   /**
114    * DLL
115    */
116   struct GNUNET_NAMESTORE_SimpleRecord *next;
117
118   /**
119    * DLL
120    */
121   struct GNUNET_NAMESTORE_SimpleRecord *prev;
122   
123   const char *name;
124   const GNUNET_HashCode *zone;
125   uint32_t record_type;
126   struct GNUNET_TIME_Absolute expiration;
127   enum GNUNET_NAMESTORE_RecordFlags flags;
128   size_t data_size;
129   const void *data;
130 };
131
132 /**
133  * Disconnect from service and then reconnect.
134  *
135  * @param nsh our handle
136  */
137 static void
138 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh);
139
140 /**
141  * Transmit messages from the message queue to the service
142  * (if there are any, and if we are not already trying).
143  *
144  * @param nsh handle to use
145  */
146 static void
147 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh);
148
149 /**
150  * We can now transmit a message to NAMESTORE. Do it.
151  *
152  * @param cls the 'struct GNUNET_NAMESTORE_Handle'
153  * @param size number of bytes we can transmit
154  * @param buf where to copy the messages
155  * @return number of bytes copied into buf
156  */
157 static size_t
158 transmit_message_to_namestore (void *cls, size_t size, void *buf)
159 {
160   struct GNUNET_NAMESTORE_Handle *nsh = cls;
161   struct PendingMessage *p;
162   size_t ret;
163   char *cbuf;
164
165   nsh->th = NULL;
166   if ((size == 0) || (buf == NULL))
167   {
168     force_reconnect (nsh);
169     return 0;
170   }
171   ret = 0;
172   cbuf = buf;
173   while ((NULL != (p = nsh->pending_head)) && (p->size <= size))
174   {
175     memcpy (&cbuf[ret], &p[1], p->size);
176     ret += p->size;
177     size -= p->size;
178     GNUNET_CONTAINER_DLL_remove (nsh->pending_head, nsh->pending_tail, p);
179     if (GNUNET_YES == p->is_init)
180       GNUNET_CLIENT_receive (nsh->client,/* &process_namestore_message*/ NULL, nsh,
181                              GNUNET_TIME_UNIT_FOREVER_REL);
182     GNUNET_free (p);
183   }
184   do_transmit (nsh);
185   return ret;
186 }
187
188
189 /**
190  * Transmit messages from the message queue to the service
191  * (if there are any, and if we are not already trying).
192  *
193  * @param nsh handle to use
194  */
195 static void
196 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh)
197 {
198   struct PendingMessage *p;
199
200   if (NULL != nsh->th)
201     return;
202   if (NULL == (p = nsh->pending_head))
203     return;
204   if (NULL == nsh->client)
205     return;                     /* currently reconnecting */
206   nsh->th = GNUNET_CLIENT_notify_transmit_ready (nsh->client, p->size,
207                                            GNUNET_TIME_UNIT_FOREVER_REL,
208                                            GNUNET_NO, &transmit_message_to_namestore,
209                                            nsh);
210 }
211
212
213 /**
214  * Try again to connect to namestore service.
215  *
216  * @param cls the handle to the namestore service
217  * @param tc scheduler context
218  */
219 static void
220 reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
221 {
222   struct PendingMessage *p;
223   struct StartMessage *init;
224
225   GNUNET_assert (NULL == nsh->client);
226   nsh->client = GNUNET_CLIENT_connect ("namestore", nsh->cfg);
227   GNUNET_assert (NULL != nsh->client);
228
229   if ((NULL == (p = nsh->pending_head)) || (GNUNET_YES != p->is_init))
230   {
231     p = GNUNET_malloc (sizeof (struct PendingMessage) +
232                        sizeof (struct StartMessage));
233     p->size = sizeof (struct StartMessage);
234     p->is_init = GNUNET_YES;
235     init = (struct StartMessage *) &p[1];
236     init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
237     init->header.size = htons (sizeof (struct StartMessage));
238     GNUNET_CONTAINER_DLL_insert (nsh->pending_head, nsh->pending_tail, p);
239   }
240   do_transmit (nsh);
241 }
242
243 /**
244  * Re-establish the connection to the service.
245  *
246  * @param cls handle to use to re-connect.
247  * @param tc scheduler context
248  */
249 static void
250 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 {
252   struct GNUNET_NAMESTORE_Handle *nsh = cls;
253
254   nsh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
255   reconnect (nsh);
256 }
257
258
259 /**
260  * Disconnect from service and then reconnect.
261  *
262  * @param nsh our handle
263  */
264 static void
265 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
266 {
267   nsh->reconnect = GNUNET_NO;
268   GNUNET_CLIENT_disconnect (nsh->client, GNUNET_NO);
269   nsh->client = NULL;
270   nsh->reconnect_task =
271       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
272                                     nsh);
273 }
274
275
276
277 /**
278  * Initialize the connection with the NAMESTORE service.
279  *
280  * @param cfg configuration to use
281  * @return handle to the GNS service, or NULL on error
282  */
283 struct GNUNET_NAMESTORE_Handle *
284 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
285 {
286   struct GNUNET_NAMESTORE_Handle *nsh;
287
288   nsh = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
289   nsh->cfg = cfg;
290   nsh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, nsh);
291   return nsh;
292 }
293
294
295 /**
296  * Shutdown connection with the NAMESTORE service.
297  *
298  * @param handle handle of the NAMESTORE connection to stop
299  */
300 void
301 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
302 {
303   GNUNET_free(handle);
304 }
305
306 /**
307  * Sign a record.  This function is used by the authority of the zone
308  * to add a record.
309  *
310  * @param h handle to the namestore
311  * @param zone_privkey private key of the zone
312  * @param record_hash hash of the record to be signed
313  * @param cont continuation to call when done
314  * @param cont_cls closure for cont
315  * @return handle to abort the request
316  */
317 struct GNUNET_NAMESTORE_QueueEntry *
318 GNUNET_NAMESTORE_stree_extend (struct GNUNET_NAMESTORE_Handle *h,
319              const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
320              const GNUNET_HashCode *record_hash,
321              GNUNET_NAMESTORE_ContinuationWithSignature cont,
322              void *cont_cls)
323 {
324   struct GNUNET_NAMESTORE_QueueEntry *qe;
325   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
326   return qe;
327 }
328
329 /**
330  * Rebalance the signature tree of our zone.  This function should
331  * be called "rarely" to rebalance the tree.
332  *
333  * @param h handle to the namestore
334  * @param zone_privkey private key for the zone to rebalance
335  * @param cont continuation to call when done
336  * @param cont_cls closure for cont
337  * @return handle to abort the request
338  */
339 struct GNUNET_NAMESTORE_QueueEntry *
340 GNUNET_NAMESTORE_stree_rebalance (struct GNUNET_NAMESTORE_Handle *h,
341           const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
342           GNUNET_NAMESTORE_ContinuationWithStatus cont,
343           void *cont_cls)
344 {
345   struct GNUNET_NAMESTORE_QueueEntry *qe;
346   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
347   return qe;
348 }
349
350 /**
351  * Provide the root of a signature tree.  This function is 
352  * used by non-authorities as the first operation when 
353  * adding a foreign zone.
354  *
355  * @param h handle to the namestore
356  * @param zone_key public key of the zone
357  * @param signature signature of the top-level entry of the zone
358  * @param revision revision number of the zone
359  * @param top_hash top-level hash of the zone
360  * @param cont continuation to call when done
361  * @param cont_cls closure for cont
362  * @return handle to abort the request
363  */
364 struct GNUNET_NAMESTORE_QueueEntry *
365 GNUNET_NAMESTORE_stree_start (struct GNUNET_NAMESTORE_Handle *h,
366                               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
367                               const struct GNUNET_CRYPTO_RsaSignature *signature,
368                               uint32_t revision,
369                               const GNUNET_HashCode *top_hash,
370                               GNUNET_NAMESTORE_ContinuationWithSignature cont,
371                               void *cont_cls)
372 {
373   struct GNUNET_NAMESTORE_QueueEntry *qe;
374   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
375   return qe;
376 }
377
378 /**
379  * Store part of a signature B-tree in the namestore.  This function
380  * is used by non-authorities to cache parts of a zone's signature tree.
381  * Note that the tree must be build top-down.  This function must check
382  * that the nodes being added are valid, and if not refuse the operation.
383  *
384  * @param h handle to the namestore
385  * @param zone_key public key of the zone
386  * @param loc location in the B-tree
387  * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root
388  * @param top_sig signature at the top, NULL if 'loc.depth > 0'
389  * @param num_entries number of entries at this node in the B-tree
390  * @param entries the 'num_entries' entries to store (hashes over the
391  *                records)
392  * @param cont continuation to call when done
393  * @param cont_cls closure for cont
394  * @return handle to abort the request
395  */
396 struct GNUNET_NAMESTORE_QueueEntry *
397 GNUNET_NAMESTORE_stree_put (struct GNUNET_NAMESTORE_Handle *h,
398                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
399                             const struct GNUNET_NAMESTORE_SignatureLocation *loc,
400                             const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
401                             const struct GNUNET_CRYPTO_RsaSignature *sig,
402                             unsigned int num_entries,
403                             const GNUNET_HashCode *entries,
404                             GNUNET_NAMESTORE_ContinuationWithStatus cont,
405                             void *cont_cls)
406 {
407   struct GNUNET_NAMESTORE_QueueEntry *qe;
408   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
409   return qe;
410 }
411
412 /**
413  * Store an item in the namestore.  If the item is already present,
414  * the expiration time is updated to the max of the existing time and
415  * the new time.  The operation must fail if there is no matching
416  * entry in the signature tree.
417  *
418  * @param h handle to the namestore
419  * @param zone hash of the public key of the zone
420  * @param name name that is being mapped (at most 255 characters long)
421  * @param record_type type of the record (A, AAAA, PKEY, etc.)
422  * @param expiration expiration time for the content
423  * @param flags flags for the content
424  * @param sig_loc where is the information about the signature for this record stored?
425  * @param data_size number of bytes in data
426  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
427  *             GNS specification for GNS extensions)
428  * @param cont continuation to call when done
429  * @param cont_cls closure for cont
430  * @return handle to abort the request
431  */
432 struct GNUNET_NAMESTORE_QueueEntry *
433 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
434                              const GNUNET_HashCode *zone,
435                              const char *name,
436                              uint32_t record_type,
437                              struct GNUNET_TIME_Absolute expiration,
438                              enum GNUNET_NAMESTORE_RecordFlags flags,
439                              const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
440                              size_t data_size,
441                              const void *data,
442                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
443                              void *cont_cls)
444 {
445   struct GNUNET_NAMESTORE_QueueEntry *qe;
446   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
447 #if 0
448   struct GNUNET_NAMESTORE_SimpleRecord *sr;
449   sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
450   sr->name = name;
451   sr->record_type = record_type;
452   sr->expiration = expiration;
453   sr->flags = flags;
454   sr->data_size = data_size;
455   sr->data = data;
456   GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
457 #endif
458   return qe;
459 }
460
461 /**
462  * Explicitly remove some content from the database.  The
463  * "cont"inuation will be called with status "GNUNET_OK" if content
464  * was removed, "GNUNET_NO" if no matching entry was found and
465  * "GNUNET_SYSERR" on all other types of errors.
466  *
467  * @param h handle to the namestore
468  * @param zone hash of the public key of the zone
469  * @param name name that is being mapped (at most 255 characters long)
470  * @param record_type type of the record (A, AAAA, PKEY, etc.)
471  * @param size number of bytes in data
472  * @param data content stored
473  * @param cont continuation to call when done
474  * @param cont_cls closure for cont
475  * @return handle to abort the request
476  */
477 struct GNUNET_NAMESTORE_QueueEntry *
478 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
479                                 const GNUNET_HashCode *zone,
480                                 const char *name,
481                                 uint32_t record_type,
482                                 size_t size,
483                                 const void *data,
484                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
485                                 void *cont_cls)
486 {
487   struct GNUNET_NAMESTORE_QueueEntry *qe;
488   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
489 #if 0
490   struct GNUNET_NAMESTORE_SimpleRecord *iter;
491   for (iter=h->records_head; iter != NULL; iter=iter->next)
492   {
493     if (strcmp ( iter->name, name ) &&
494         iter->record_type == record_type &&
495         GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
496       break;
497   }
498   if (iter)
499     GNUNET_CONTAINER_DLL_remove(h->records_head,
500                                 h->records_tail,
501                                 iter);
502 #endif
503   return qe;
504 }
505
506 /**
507  * Get a result for a particular key from the namestore.  The processor
508  * will only be called once.
509  *
510  * @param h handle to the namestore
511  * @param zone zone to look up a record from
512  * @param name name to look up
513  * @param record_type desired record type
514  * @param proc function to call on each matching value;
515  *        will be called once with a NULL value at the end
516  * @param proc_cls closure for proc
517  * @return a handle that can be used to
518  *         cancel
519  */
520 struct GNUNET_NAMESTORE_QueueEntry *
521 GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h, 
522                               const GNUNET_HashCode *zone,
523                               const char *name,
524                               uint32_t record_type,
525                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
526 {
527   struct GNUNET_NAMESTORE_QueueEntry *qe;
528   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
529 #if 0
530   struct GNUNET_NAMESTORE_SimpleRecord *iter;
531   for (iter=h->records_head; iter != NULL; iter=iter->next)
532   {
533     proc(proc_cls, iter->zone, iter->name, iter->record_type,
534        iter->expiration,
535        iter->flags,
536        NULL /*sig loc*/,
537        iter->data_size /*size*/,
538        iter->data /* data */);
539   }
540   proc(proc_cls, zone, name, record_type,
541        GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
542 #endif
543   return qe;
544 }
545
546
547 /**
548  * Get the hash of a record (what will be signed in the Stree for
549  * the record).
550  *
551  * @param zone hash of the public key of the zone
552  * @param name name that is being mapped (at most 255 characters long)
553  * @param record_type type of the record (A, AAAA, PKEY, etc.)
554  * @param expiration expiration time for the content
555  * @param flags flags for the content
556  * @param data_size number of bytes in data
557  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and.
558  *             GNS specification for GNS extensions)
559  * @param record_hash hash of the record (set)
560  */
561 void
562 GNUNET_NAMESTORE_record_hash (struct GNUNET_NAMESTORE_Handle *h,
563                               const GNUNET_HashCode *zone,
564                               const char *name,
565                               uint32_t record_type,
566                               struct GNUNET_TIME_Absolute expiration,
567                               enum GNUNET_NAMESTORE_RecordFlags flags,
568                               size_t data_size,
569                               const void *data,
570                               GNUNET_HashCode *record_hash)
571 {
572   char* teststring = "namestore-stub";
573   GNUNET_CRYPTO_hash(teststring, strlen(teststring), record_hash);
574 }
575
576 /**
577  * Obtain part of a signature B-tree.  The processor
578  * will only be called once.
579  *
580  * @param h handle to the namestore
581  * @param zone zone to look up a record from
582  * @param sig_loc location to look up
583  * @param proc function to call on each matching value;
584  *        will be called once with a NULL value at the end
585  * @param proc_cls closure for proc
586  * @return a handle that can be used to
587  *         cancel
588  */
589 struct GNUNET_NAMESTORE_QueueEntry *
590 GNUNET_NAMESTORE_lookup_stree (struct GNUNET_NAMESTORE_Handle *h,
591                       const GNUNET_HashCode *zone,
592                       const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
593                       GNUNET_NAMESTORE_StreeProcessor proc, void *proc_cls)
594 {
595   struct GNUNET_NAMESTORE_QueueEntry *qe;
596   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
597   return qe;
598 }
599
600
601 /**
602  * Get all records of a zone.
603  *
604  * @param h handle to the namestore
605  * @param zone zone to access
606  * @param proc function to call on a random value; it
607  *        will be called repeatedly with a value (if available)
608  *        and always once at the end with a zone and name of NULL.
609  * @param proc_cls closure for proc
610  * @return a handle that can be used to
611  *         cancel
612  */
613 struct GNUNET_NAMESTORE_QueueEntry *
614 GNUNET_NAMESTORE_zone_transfer (struct GNUNET_NAMESTORE_Handle *h,
615                                 const GNUNET_HashCode *zone,
616                                 GNUNET_NAMESTORE_RecordProcessor proc,
617                                 void *proc_cls)
618 {
619   struct GNUNET_NAMESTORE_QueueEntry *qe;
620   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
621   return qe;
622 }
623
624
625
626
627 /**
628  * Cancel a namestore operation.  The final callback from the
629  * operation must not have been done yet.
630  *
631  * @param qe operation to cancel
632  */
633 void
634 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
635 {
636   if (qe)
637     GNUNET_free(qe);
638 }
639
640 /* end of namestore_api.c */