2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file gns/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
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
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 struct GNUNET_NAMESTORE_QueueEntry
43 struct GNUNET_NAMESTORE_QueueEntry *next;
44 struct GNUNET_NAMESTORE_QueueEntry *prev;
46 struct GNUNET_NAMESTORE_Handle *nsh;
50 GNUNET_NAMESTORE_ContinuationWithStatus cont;
53 GNUNET_NAMESTORE_RecordProcessor proc;
56 char *data; /*stub data pointer*/
63 struct GNUNET_NAMESTORE_ZoneIterator
65 struct GNUNET_NAMESTORE_ZoneIterator *next;
66 struct GNUNET_NAMESTORE_ZoneIterator *prev;
68 struct GNUNET_NAMESTORE_Handle *h;
69 GNUNET_NAMESTORE_RecordProcessor proc;
71 const GNUNET_HashCode * zone;
78 * Message in linked list we should send to the service. The
79 * actual binary message follows this struct.
87 struct PendingMessage *next;
92 struct PendingMessage *prev;
95 * Size of the message.
100 * Is this the 'START' message?
107 * Connection to the NAMESTORE service.
109 struct GNUNET_NAMESTORE_Handle
113 * Configuration to use.
115 const struct GNUNET_CONFIGURATION_Handle *cfg;
118 * Socket (if available).
120 struct GNUNET_CLIENT_Connection *client;
123 * Currently pending transmission request (or NULL).
125 struct GNUNET_CLIENT_TransmitHandle *th;
130 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
133 * Pending messages to send to the service
136 struct PendingMessage * pending_head;
137 struct PendingMessage * pending_tail;
140 * Should we reconnect to service due to some serious error?
146 * Pending namestore queue entries
148 struct GNUNET_NAMESTORE_QueueEntry * op_head;
149 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
154 * Pending namestore zone iterator entries
156 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
157 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
160 struct GNUNET_NAMESTORE_SimpleRecord
165 struct GNUNET_NAMESTORE_SimpleRecord *next;
170 struct GNUNET_NAMESTORE_SimpleRecord *prev;
173 const GNUNET_HashCode *zone;
174 uint32_t record_type;
175 struct GNUNET_TIME_Absolute expiration;
176 enum GNUNET_NAMESTORE_RecordFlags flags;
183 * Disconnect from service and then reconnect.
185 * @param nsh our handle
188 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh);
192 * Type of a function to call when we receive a message
195 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
196 * @param msg message received, NULL on timeout or fatal error
199 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
201 struct GNUNET_NAMESTORE_Handle *nsh = cls;
202 struct GNUNET_NAMESTORE_QueueEntry *qe;
205 uint64_t op_id = UINT64_MAX;
209 force_reconnect (nsh);
213 size = ntohs (msg->size);
214 type = ntohs (msg->type);
216 /* find matching operation */
217 if (op_id > nsh->op_id)
220 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
221 GNUNET_TIME_UNIT_FOREVER_REL);
224 for (qe = nsh->op_head; qe != NULL; qe = qe->next)
226 if (qe->op_id == op_id)
232 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
233 GNUNET_TIME_UNIT_FOREVER_REL);
238 case GNUNET_MESSAGE_TYPE_TEST:
239 /* handle message here */
244 size++; // FIXME: just working around compiler warning here...
245 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
246 GNUNET_TIME_UNIT_FOREVER_REL);
248 if (GNUNET_YES == nsh->reconnect)
249 force_reconnect (nsh);
254 * Transmit messages from the message queue to the service
255 * (if there are any, and if we are not already trying).
257 * @param nsh handle to use
260 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh);
264 * We can now transmit a message to NAMESTORE. Do it.
266 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
267 * @param size number of bytes we can transmit
268 * @param buf where to copy the messages
269 * @return number of bytes copied into buf
272 transmit_message_to_namestore (void *cls, size_t size, void *buf)
274 struct GNUNET_NAMESTORE_Handle *nsh = cls;
275 struct PendingMessage *p;
280 if ((size == 0) || (buf == NULL))
282 force_reconnect (nsh);
287 while ((NULL != (p = nsh->pending_head)) && (p->size <= size))
289 memcpy (&cbuf[ret], &p[1], p->size);
292 GNUNET_CONTAINER_DLL_remove (nsh->pending_head, nsh->pending_tail, p);
293 if (GNUNET_YES == p->is_init)
294 GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh,
295 GNUNET_TIME_UNIT_FOREVER_REL);
304 * Transmit messages from the message queue to the service
305 * (if there are any, and if we are not already trying).
307 * @param nsh handle to use
310 do_transmit (struct GNUNET_NAMESTORE_Handle *nsh)
312 struct PendingMessage *p;
316 if (NULL == (p = nsh->pending_head))
318 if (NULL == nsh->client)
319 return; /* currently reconnecting */
321 nsh->th = GNUNET_CLIENT_notify_transmit_ready (nsh->client, p->size,
322 GNUNET_TIME_UNIT_FOREVER_REL,
323 GNUNET_NO, &transmit_message_to_namestore,
329 * Try again to connect to namestore service.
331 * @param cls the handle to the namestore service
332 * @param tc scheduler context
335 reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
337 struct PendingMessage *p;
338 struct StartMessage *init;
340 GNUNET_assert (NULL == nsh->client);
341 nsh->client = GNUNET_CLIENT_connect ("namestore", nsh->cfg);
342 GNUNET_assert (NULL != nsh->client);
344 if ((NULL == (p = nsh->pending_head)) || (GNUNET_YES != p->is_init))
346 p = GNUNET_malloc (sizeof (struct PendingMessage) +
347 sizeof (struct StartMessage));
348 p->size = sizeof (struct StartMessage);
349 p->is_init = GNUNET_YES;
350 init = (struct StartMessage *) &p[1];
351 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
352 init->header.size = htons (sizeof (struct StartMessage));
353 GNUNET_CONTAINER_DLL_insert (nsh->pending_head, nsh->pending_tail, p);
359 * Re-establish the connection to the service.
361 * @param cls handle to use to re-connect.
362 * @param tc scheduler context
365 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 struct GNUNET_NAMESTORE_Handle *nsh = cls;
369 nsh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
375 * Disconnect from service and then reconnect.
377 * @param nsh our handle
380 force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh)
382 nsh->reconnect = GNUNET_NO;
383 GNUNET_CLIENT_disconnect (nsh->client, GNUNET_NO);
385 nsh->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
391 enqeue_namestore_operation (struct GNUNET_NAMESTORE_Handle *nsh, struct GNUNET_NAMESTORE_QueueEntry *qe)
393 qe->op_id = nsh->op_id;
395 GNUNET_CONTAINER_DLL_insert(nsh->op_head, nsh->op_tail, qe);
399 * Initialize the connection with the NAMESTORE service.
401 * @param cfg configuration to use
402 * @return handle to the GNS service, or NULL on error
404 struct GNUNET_NAMESTORE_Handle *
405 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
407 struct GNUNET_NAMESTORE_Handle *nsh;
409 nsh = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
411 nsh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, nsh);
417 * Shutdown connection with the NAMESTORE service.
419 * @param handle handle of the NAMESTORE connection to stop
422 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *nsh, int drop)
424 struct PendingMessage *p;
425 struct GNUNET_NAMESTORE_QueueEntry *q;
426 struct GNUNET_NAMESTORE_ZoneIterator *z;
428 GNUNET_assert (nsh != NULL);
430 while (NULL != (p = nsh->pending_head))
432 GNUNET_CONTAINER_DLL_remove (nsh->pending_head, nsh->pending_tail, p);
436 while (NULL != (q = nsh->op_head))
438 GNUNET_CONTAINER_DLL_remove (nsh->op_head, nsh->op_tail, q);
442 while (NULL != (z = nsh->z_head))
444 GNUNET_CONTAINER_DLL_remove (nsh->z_head, nsh->z_tail, z);
448 if (NULL != nsh->client)
450 GNUNET_CLIENT_disconnect (nsh->client, GNUNET_NO);
453 if (GNUNET_SCHEDULER_NO_TASK != nsh->reconnect_task)
455 GNUNET_SCHEDULER_cancel (nsh->reconnect_task);
456 nsh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
464 * Store an item in the namestore. If the item is already present,
465 * the expiration time is updated to the max of the existing time and
466 * the new time. This API is used when we cache signatures from other
469 * @param h handle to the namestore
470 * @param zone hash of the public key of the zone
471 * @param name name that is being mapped (at most 255 characters long)
472 * @param expire when does the corresponding block in the DHT expire (until
473 * when should we never do a DHT lookup for the same name again)?
474 * @param rd_count number of entries in 'rd' array
475 * @param rd array of records with data to store
476 * @param signature signature for all the records in the zone under the given name
477 * @param cont continuation to call when done
478 * @param cont_cls closure for cont
479 * @return handle to abort the request
481 struct GNUNET_NAMESTORE_QueueEntry *
482 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
483 const GNUNET_HashCode *zone,
485 struct GNUNET_TIME_Absolute expire,
486 unsigned int rd_count,
487 const struct GNUNET_NAMESTORE_RecordData *rd,
488 const struct GNUNET_CRYPTO_RsaSignature *signature,
489 GNUNET_NAMESTORE_ContinuationWithStatus cont,
492 struct GNUNET_NAMESTORE_QueueEntry *qe;
493 struct PendingMessage *pe;
496 GNUNET_assert (NULL != h);
498 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
501 qe->cont_cls = cont_cls;
502 enqeue_namestore_operation(h, qe);
505 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
507 /* create msg here */
509 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
513 struct GNUNET_NAMESTORE_SimpleRecord *sr;
514 sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
516 sr->record_type = record_type;
517 sr->expiration = expiration;
519 sr->data_size = data_size;
521 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
528 * Check if a signature is valid. This API is used by the GNS Block
529 * to validate signatures received from the network.
531 * @param public_key public key of the zone
532 * @param name name that is being mapped (at most 255 characters long)
533 * @param rd_count number of entries in 'rd' array
534 * @param rd array of records with data to store
535 * @param signature signature for all the records in the zone under the given name
536 * @return GNUNET_OK if the signature is valid
539 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
541 unsigned int rd_count,
542 const struct GNUNET_NAMESTORE_RecordData *rd,
543 const struct GNUNET_CRYPTO_RsaSignature *signature)
545 return GNUNET_SYSERR;
549 * Store an item in the namestore. If the item is already present,
550 * the expiration time is updated to the max of the existing time and
551 * the new time. This API is used by the authority of a zone.
553 * @param h handle to the namestore
554 * @param pkey private key of the zone
555 * @param name name that is being mapped (at most 255 characters long)
556 * @param rd record data to store
557 * @param cont continuation to call when done
558 * @param cont_cls closure for cont
559 * @return handle to abort the request
561 struct GNUNET_NAMESTORE_QueueEntry *
562 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
563 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
565 const struct GNUNET_NAMESTORE_RecordData *rd,
566 GNUNET_NAMESTORE_ContinuationWithStatus cont,
569 struct GNUNET_NAMESTORE_QueueEntry *qe;
570 struct PendingMessage *pe;
573 GNUNET_assert (NULL != h);
575 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
578 qe->cont_cls = cont_cls;
579 enqeue_namestore_operation(h, qe);
582 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
584 /* create msg here */
586 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
593 * Explicitly remove some content from the database. The
594 * "cont"inuation will be called with status "GNUNET_OK" if content
595 * was removed, "GNUNET_NO" if no matching entry was found and
596 * "GNUNET_SYSERR" on all other types of errors.
597 * This API is used by the authority of a zone.
599 * @param h handle to the namestore
600 * @param pkey private key of the zone
601 * @param name name that is being mapped (at most 255 characters long)
602 * @param rd record data
603 * @param cont continuation to call when done
604 * @param cont_cls closure for cont
605 * @return handle to abort the request
607 struct GNUNET_NAMESTORE_QueueEntry *
608 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
609 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
611 const struct GNUNET_NAMESTORE_RecordData *rd,
612 GNUNET_NAMESTORE_ContinuationWithStatus cont,
615 struct GNUNET_NAMESTORE_QueueEntry *qe;
616 struct PendingMessage *pe;
619 GNUNET_assert (NULL != h);
621 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
624 qe->cont_cls = cont_cls;
625 enqeue_namestore_operation(h, qe);
628 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
630 /* create msg here */
632 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
636 struct GNUNET_NAMESTORE_SimpleRecord *iter;
637 for (iter=h->records_head; iter != NULL; iter=iter->next)
639 if (strcmp ( iter->name, name ) &&
640 iter->record_type == record_type &&
641 GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
645 GNUNET_CONTAINER_DLL_remove(h->records_head,
654 * Get a result for a particular key from the namestore. The processor
655 * will only be called once.
657 * @param h handle to the namestore
658 * @param zone zone to look up a record from
659 * @param name name to look up
660 * @param record_type desired record type, 0 for all
661 * @param proc function to call on the matching records, or with
662 * NULL (rd_count == 0) if there are no matching records
663 * @param proc_cls closure for proc
664 * @return a handle that can be used to
667 struct GNUNET_NAMESTORE_QueueEntry *
668 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
669 const GNUNET_HashCode *zone,
671 uint32_t record_type,
672 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
674 struct GNUNET_NAMESTORE_QueueEntry *qe;
675 struct PendingMessage *pe;
678 GNUNET_assert (NULL != h);
680 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
683 qe->proc_cls = proc_cls;
684 enqeue_namestore_operation(h, qe);
687 msg_size = sizeof (struct LookupNameMessage);
688 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
690 /* create msg here */
692 struct LookupNameMessage * msg;
694 pe->is_init = GNUNET_NO;
695 msg = (struct LookupNameMessage *) &pe[1];
696 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
697 msg->header.size = htons (msg_size);
699 /* create msg done */
701 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
705 struct GNUNET_NAMESTORE_SimpleRecord *iter;
706 for (iter=h->records_head; iter != NULL; iter=iter->next)
708 proc(proc_cls, iter->zone, iter->name, iter->record_type,
712 iter->data_size /*size*/,
713 iter->data /* data */);
715 proc(proc_cls, zone, name, record_type,
716 GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
727 * Starts a new zone iteration (used to periodically PUT all of our
728 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
729 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
730 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
731 * immediately, and then again after
732 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
734 * @param h handle to the namestore
735 * @param zone zone to access, NULL for all zones
736 * @param must_have_flags flags that must be set for the record to be returned
737 * @param must_not_have_flags flags that must NOT be set for the record to be returned
738 * @param proc function to call on each name from the zone; it
739 * will be called repeatedly with a value (if available)
740 * and always once at the end with a name of NULL.
741 * @param proc_cls closure for proc
742 * @return an iterator handle to use for iteration
744 struct GNUNET_NAMESTORE_ZoneIterator *
745 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
746 const GNUNET_HashCode *zone,
747 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
748 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
749 GNUNET_NAMESTORE_RecordProcessor proc,
752 struct GNUNET_NAMESTORE_ZoneIterator *it;
754 GNUNET_assert (h != NULL);
756 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
760 GNUNET_CONTAINER_DLL_insert(h->z_head, h->z_tail, it);
767 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
768 * for the next record.
770 * @param it the iterator
773 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
780 * Stops iteration and releases the namestore handle for further calls.
782 * @param it the iterator
785 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
787 struct GNUNET_NAMESTORE_Handle * nsh;
788 GNUNET_assert (it != NULL);
791 GNUNET_CONTAINER_DLL_remove (nsh->z_head, nsh->z_tail, it);
798 * Cancel a namestore operation. The final callback from the
799 * operation must not have been done yet.
801 * @param qe operation to cancel
804 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
806 struct GNUNET_NAMESTORE_Handle *nsh = qe->nsh;
808 GNUNET_assert (qe != NULL);
810 GNUNET_CONTAINER_DLL_remove(nsh->op_head, nsh->op_tail, qe);
815 /* end of namestore_api.c */