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 namestore/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 h our handle
188 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
191 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
192 struct LookupNameResponseMessage * msg,
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
196 "LOOKUP_NAME_RESPONSE");
198 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
199 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
201 struct GNUNET_NAMESTORE_RecordData *rd = NULL;
202 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
203 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
204 struct GNUNET_TIME_Absolute expire;
205 unsigned int rd_count = 0;
208 int contains_sig = GNUNET_NO;
210 rd_count = ntohl (msg->rc_count);
211 msg_len = ntohs (msg->header.size);
212 name_len = ntohs (msg->name_len);
213 contains_sig = ntohs (msg->contains_sig);
214 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
216 if (msg_len != sizeof (struct LookupNameResponseMessage) +
217 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
219 rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData) +
220 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature))
226 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
227 name = (char *) &zone_key[1];
228 rd = (struct GNUNET_NAMESTORE_RecordData *) &name[name_len];
230 /* reset values if values not contained */
231 if (contains_sig == GNUNET_NO)
234 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd[rd_count];
240 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
241 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
244 if (qe->proc != NULL)
246 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, rd, signature);
250 /* Operation done, remove */
251 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
258 * Type of a function to call when we receive a message
261 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
262 * @param msg message received, NULL on timeout or fatal error
265 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
267 struct GNUNET_NAMESTORE_Handle *h = cls;
268 struct GenericMessage * gm;
269 struct GNUNET_NAMESTORE_QueueEntry *qe;
272 uint32_t op_id = UINT32_MAX;
280 size = ntohs (msg->size);
281 type = ntohs (msg->type);
283 if (size < sizeof (struct GenericMessage))
286 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
287 GNUNET_TIME_UNIT_FOREVER_REL);
291 gm = (struct GenericMessage *) msg;
292 op_id = ntohl (gm->op_id);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, op_id);
296 /* Find matching operation */
297 if (op_id > h->op_id)
299 /* No matching pending operation found */
301 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
302 GNUNET_TIME_UNIT_FOREVER_REL);
305 for (qe = h->op_head; qe != NULL; qe = qe->next)
307 if (qe->op_id == op_id)
312 /* No matching pending operation found */
314 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
315 GNUNET_TIME_UNIT_FOREVER_REL);
319 /* handle different message type */
321 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
322 if (size < sizeof (struct LookupNameResponseMessage))
327 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
334 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
335 GNUNET_TIME_UNIT_FOREVER_REL);
337 if (GNUNET_YES == h->reconnect)
343 * Transmit messages from the message queue to the service
344 * (if there are any, and if we are not already trying).
346 * @param h handle to use
349 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
353 * We can now transmit a message to NAMESTORE. Do it.
355 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
356 * @param size number of bytes we can transmit
357 * @param buf where to copy the messages
358 * @return number of bytes copied into buf
361 transmit_message_to_namestore (void *cls, size_t size, void *buf)
363 struct GNUNET_NAMESTORE_Handle *h = cls;
364 struct PendingMessage *p;
369 if ((size == 0) || (buf == NULL))
376 while ((NULL != (p = h->pending_head)) && (p->size <= size))
378 memcpy (&cbuf[ret], &p[1], p->size);
381 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
382 if (GNUNET_YES == p->is_init)
383 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
384 GNUNET_TIME_UNIT_FOREVER_REL);
393 * Transmit messages from the message queue to the service
394 * (if there are any, and if we are not already trying).
396 * @param h handle to use
399 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
401 struct PendingMessage *p;
405 if (NULL == (p = h->pending_head))
407 if (NULL == h->client)
408 return; /* currently reconnecting */
410 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
411 GNUNET_TIME_UNIT_FOREVER_REL,
412 GNUNET_NO, &transmit_message_to_namestore,
418 * Reconnect to namestore service.
420 * @param h the handle to the namestore service
423 reconnect (struct GNUNET_NAMESTORE_Handle *h)
425 struct PendingMessage *p;
426 struct StartMessage *init;
428 GNUNET_assert (NULL == h->client);
429 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
430 GNUNET_assert (NULL != h->client);
432 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
434 p = GNUNET_malloc (sizeof (struct PendingMessage) +
435 sizeof (struct StartMessage));
436 p->size = sizeof (struct StartMessage);
437 p->is_init = GNUNET_YES;
438 init = (struct StartMessage *) &p[1];
439 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
440 init->header.size = htons (sizeof (struct StartMessage));
441 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
447 * Re-establish the connection to the service.
449 * @param cls handle to use to re-connect.
450 * @param tc scheduler context
453 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
455 struct GNUNET_NAMESTORE_Handle *h = cls;
457 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
463 * Disconnect from service and then reconnect.
465 * @param h our handle
468 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
470 h->reconnect = GNUNET_NO;
471 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
473 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
479 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
481 uint32_t op_id = h->op_id;
487 * Initialize the connection with the NAMESTORE service.
489 * @param cfg configuration to use
490 * @return handle to the GNS service, or NULL on error
492 struct GNUNET_NAMESTORE_Handle *
493 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
495 struct GNUNET_NAMESTORE_Handle *h;
497 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
499 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
506 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
508 struct PendingMessage *p;
509 struct GNUNET_NAMESTORE_QueueEntry *q;
510 struct GNUNET_NAMESTORE_ZoneIterator *z;
512 GNUNET_assert (h != NULL);
514 while (NULL != (p = h->pending_head))
516 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
520 while (NULL != (q = h->op_head))
522 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
526 while (NULL != (z = h->z_head))
528 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
532 if (NULL != h->client)
534 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
537 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
539 GNUNET_SCHEDULER_cancel (h->reconnect_task);
540 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
548 * Store an item in the namestore. If the item is already present,
549 * the expiration time is updated to the max of the existing time and
550 * the new time. This API is used when we cache signatures from other
553 * @param h handle to the namestore
554 * @param zone_key public key of the zone
555 * @param name name that is being mapped (at most 255 characters long)
556 * @param expire when does the corresponding block in the DHT expire (until
557 * when should we never do a DHT lookup for the same name again)?
558 * @param rd_count number of entries in 'rd' array
559 * @param rd array of records with data to store
560 * @param signature signature for all the records in the zone under the given name
561 * @param cont continuation to call when done
562 * @param cont_cls closure for cont
563 * @return handle to abort the request
565 struct GNUNET_NAMESTORE_QueueEntry *
566 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
567 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
569 struct GNUNET_TIME_Absolute expire,
570 unsigned int rd_count,
571 const struct GNUNET_NAMESTORE_RecordData *rd,
572 const struct GNUNET_CRYPTO_RsaSignature *signature,
573 GNUNET_NAMESTORE_ContinuationWithStatus cont,
576 struct GNUNET_NAMESTORE_QueueEntry *qe;
577 struct PendingMessage *pe;
581 GNUNET_assert (NULL != h);
583 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
586 qe->cont_cls = cont_cls;
588 GNUNET_CONTAINER_DLL_insert(h->op_head, h->op_tail, qe);
591 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
593 /* create msg here */
595 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
599 struct GNUNET_NAMESTORE_SimpleRecord *sr;
600 sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
602 sr->record_type = record_type;
603 sr->expiration = expiration;
605 sr->data_size = data_size;
607 GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
614 * Check if a signature is valid. This API is used by the GNS Block
615 * to validate signatures received from the network.
617 * @param public_key public key of the zone
618 * @param name name that is being mapped (at most 255 characters long)
619 * @param rd_count number of entries in 'rd' array
620 * @param rd array of records with data to store
621 * @param signature signature for all the records in the zone under the given name
622 * @return GNUNET_OK if the signature is valid
625 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
627 unsigned int rd_count,
628 const struct GNUNET_NAMESTORE_RecordData *rd,
629 const struct GNUNET_CRYPTO_RsaSignature *signature)
631 return GNUNET_SYSERR;
635 * Store an item in the namestore. If the item is already present,
636 * the expiration time is updated to the max of the existing time and
637 * the new time. This API is used by the authority of a zone.
639 * @param h handle to the namestore
640 * @param pkey private key of the zone
641 * @param name name that is being mapped (at most 255 characters long)
642 * @param rd record data to store
643 * @param cont continuation to call when done
644 * @param cont_cls closure for cont
645 * @return handle to abort the request
647 struct GNUNET_NAMESTORE_QueueEntry *
648 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
649 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
651 const struct GNUNET_NAMESTORE_RecordData *rd,
652 GNUNET_NAMESTORE_ContinuationWithStatus cont,
655 struct GNUNET_NAMESTORE_QueueEntry *qe;
656 struct PendingMessage *pe;
659 GNUNET_assert (NULL != h);
661 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
664 qe->cont_cls = cont_cls;
668 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
670 /* create msg here */
672 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
679 * Explicitly remove some content from the database. The
680 * "cont"inuation will be called with status "GNUNET_OK" if content
681 * was removed, "GNUNET_NO" if no matching entry was found and
682 * "GNUNET_SYSERR" on all other types of errors.
683 * This API is used by the authority of a zone.
685 * @param h handle to the namestore
686 * @param pkey private key of the zone
687 * @param name name that is being mapped (at most 255 characters long)
688 * @param rd record data
689 * @param cont continuation to call when done
690 * @param cont_cls closure for cont
691 * @return handle to abort the request
693 struct GNUNET_NAMESTORE_QueueEntry *
694 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
695 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
697 const struct GNUNET_NAMESTORE_RecordData *rd,
698 GNUNET_NAMESTORE_ContinuationWithStatus cont,
701 struct GNUNET_NAMESTORE_QueueEntry *qe;
702 struct PendingMessage *pe;
705 GNUNET_assert (NULL != h);
707 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
710 qe->cont_cls = cont_cls;
714 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
716 /* create msg here */
718 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
722 struct GNUNET_NAMESTORE_SimpleRecord *iter;
723 for (iter=h->records_head; iter != NULL; iter=iter->next)
725 if (strcmp ( iter->name, name ) &&
726 iter->record_type == record_type &&
727 GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
731 GNUNET_CONTAINER_DLL_remove(h->records_head,
740 * Get a result for a particular key from the namestore. The processor
741 * will only be called once.
743 * @param h handle to the namestore
744 * @param zone zone to look up a record from
745 * @param name name to look up
746 * @param record_type desired record type, 0 for all
747 * @param proc function to call on the matching records, or with
748 * NULL (rd_count == 0) if there are no matching records
749 * @param proc_cls closure for proc
750 * @return a handle that can be used to
753 struct GNUNET_NAMESTORE_QueueEntry *
754 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
755 const GNUNET_HashCode *zone,
757 uint32_t record_type,
758 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
760 struct GNUNET_NAMESTORE_QueueEntry *qe;
761 struct PendingMessage *pe;
766 GNUNET_assert (NULL != h);
767 GNUNET_assert (NULL != zone);
768 GNUNET_assert (NULL != name);
770 name_len = strlen (name) + 1;
771 if ((name_len == 0) || (name_len > 256))
778 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
781 qe->proc_cls = proc_cls;
783 GNUNET_CONTAINER_DLL_insert(h->op_head, h->op_tail, qe);
786 msg_size = sizeof (struct LookupNameMessage) + name_len;
787 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
789 /* create msg here */
790 struct LookupNameMessage * msg;
792 pe->is_init = GNUNET_NO;
793 msg = (struct LookupNameMessage *) &pe[1];
794 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
795 msg->header.size = htons (msg_size);
796 msg->op_id = htonl (id);
797 msg->record_type = htonl (record_type);
799 msg->name_len = htonl (name_len);
800 memcpy (&msg[1], name, name_len);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
804 /* transmit message */
805 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe);
814 * Starts a new zone iteration (used to periodically PUT all of our
815 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
816 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
817 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
818 * immediately, and then again after
819 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
821 * @param h handle to the namestore
822 * @param zone zone to access, NULL for all zones
823 * @param must_have_flags flags that must be set for the record to be returned
824 * @param must_not_have_flags flags that must NOT be set for the record to be returned
825 * @param proc function to call on each name from the zone; it
826 * will be called repeatedly with a value (if available)
827 * and always once at the end with a name of NULL.
828 * @param proc_cls closure for proc
829 * @return an iterator handle to use for iteration
831 struct GNUNET_NAMESTORE_ZoneIterator *
832 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
833 const GNUNET_HashCode *zone,
834 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
835 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
836 GNUNET_NAMESTORE_RecordProcessor proc,
839 struct GNUNET_NAMESTORE_ZoneIterator *it;
841 GNUNET_assert (h != NULL);
843 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
847 GNUNET_CONTAINER_DLL_insert(h->z_head, h->z_tail, it);
854 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
855 * for the next record.
857 * @param it the iterator
860 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
867 * Stops iteration and releases the namestore handle for further calls.
869 * @param it the iterator
872 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
874 struct GNUNET_NAMESTORE_Handle * h;
875 GNUNET_assert (it != NULL);
878 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, it);
885 * Cancel a namestore operation. The final callback from the
886 * operation must not have been done yet.
888 * @param qe operation to cancel
891 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
893 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
895 GNUNET_assert (qe != NULL);
897 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
902 /* end of namestore_api.c */