2 This file is part of GNUnet.
3 (C) 2010-2013 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
26 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_namestore_service.h"
37 #include "namestore.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
44 * An QueueEntry used to store information for a pending
45 * NAMESTORE record operation
47 struct GNUNET_NAMESTORE_QueueEntry
53 struct GNUNET_NAMESTORE_QueueEntry *next;
58 struct GNUNET_NAMESTORE_QueueEntry *prev;
61 * Main handle to access the namestore.
63 struct GNUNET_NAMESTORE_Handle *nsh;
66 * Continuation to call
68 GNUNET_NAMESTORE_ContinuationWithStatus cont;
76 * Function to call with the records we get back; or NULL.
78 GNUNET_NAMESTORE_RecordMonitor proc;
81 * Closure for @e proc.
86 * Function to call with the blocks we get back; or NULL.
88 GNUNET_NAMESTORE_BlockProcessor block_proc;
91 * Closure for @e block_proc.
96 * The operation id this zone iteration operation has
104 * Handle for a zone iterator operation
106 struct GNUNET_NAMESTORE_ZoneIterator
112 struct GNUNET_NAMESTORE_ZoneIterator *next;
117 struct GNUNET_NAMESTORE_ZoneIterator *prev;
120 * Main handle to access the namestore.
122 struct GNUNET_NAMESTORE_Handle *h;
125 * The continuation to call with the results
127 GNUNET_NAMESTORE_RecordMonitor proc;
130 * Closure for @e proc.
135 * Private key of the zone.
137 struct GNUNET_CRYPTO_EccPrivateKey zone;
140 * The operation id this zone iteration operation has
148 * Message in linked list we should send to the service. The
149 * actual binary message follows this struct.
151 struct PendingMessage
157 struct PendingMessage *next;
162 struct PendingMessage *prev;
165 * Size of the message.
173 * Connection to the NAMESTORE service.
175 struct GNUNET_NAMESTORE_Handle
179 * Configuration to use.
181 const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Socket (if available).
186 struct GNUNET_CLIENT_Connection *client;
189 * Currently pending transmission request (or NULL).
191 struct GNUNET_CLIENT_TransmitHandle *th;
194 * Head of linked list of pending messages to send to the service
196 struct PendingMessage *pending_head;
199 * Tail of linked list of pending messages to send to the service
201 struct PendingMessage *pending_tail;
204 * Head of pending namestore queue entries
206 struct GNUNET_NAMESTORE_QueueEntry *op_head;
209 * Tail of pending namestore queue entries
211 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
214 * Head of pending namestore zone iterator entries
216 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
219 * Tail of pending namestore zone iterator entries
221 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
226 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
229 * Delay introduced before we reconnect.
231 struct GNUNET_TIME_Relative reconnect_delay;
234 * Should we reconnect to service due to some serious error?
239 * Did we start to receive yet?
244 * The last operation id used for a NAMESTORE operation
246 uint32_t last_op_id_used;
252 * Disconnect from service and then reconnect.
254 * @param h our handle
257 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
261 * Handle an incoming message of type
262 * #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE.
264 * @param qe the respective entry in the message queue
265 * @param msg the message we received
266 * @param size the message size
267 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
270 handle_lookup_block_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
271 const struct LookupBlockResponseMessage *msg,
274 struct GNUNET_NAMESTORE_Block *block;
275 char buf[size + sizeof (struct GNUNET_NAMESTORE_Block)
276 - sizeof (struct LookupBlockResponseMessage)];
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
280 "LOOKUP_BLOCK_RESPONSE");
281 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
284 if (NULL != qe->block_proc)
285 qe->block_proc (qe->block_proc_cls, NULL);
289 block = (struct GNUNET_NAMESTORE_Block *) buf;
290 block->signature = msg->signature;
291 block->derived_key = msg->derived_key;
292 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
293 block->purpose.size = htonl (size - sizeof (struct LookupBlockResponseMessage) +
294 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
295 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
296 block->expiration_time = msg->expire;
299 size - sizeof (struct LookupBlockResponseMessage));
301 GNUNET_NAMESTORE_block_verify (block))
304 return GNUNET_SYSERR;
306 if (NULL != qe->block_proc)
307 qe->block_proc (qe->block_proc_cls, block);
315 * Handle an incoming message of type
316 * #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE
318 * @param qe the respective entry in the message queue
319 * @param msg the message we received
320 * @param size the message size
321 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
324 handle_block_cache_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
325 const struct BlockCacheResponseMessage *msg,
330 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "BLOCK_CACHE_RESPONSE");
333 res = ntohl (msg->op_result);
334 /* TODO: add actual error message from namestore to response... */
335 if (NULL != qe->cont)
336 qe->cont (qe->cont_cls,
340 : _("Namestore failed to cache block"));
346 * Handle an incoming message of type
347 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
349 * @param qe the respective entry in the message queue
350 * @param msg the message we received
351 * @param size the message size
352 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
355 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
356 const struct RecordStoreResponseMessage* msg,
362 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "RECORD_STORE_RESPONSE");
365 /* TODO: add actual error message from namestore to response... */
366 res = ntohl (msg->op_result);
367 if (GNUNET_SYSERR == res)
368 emsg = _("Namestore failed to store record\n");
371 if (NULL != qe->cont)
372 qe->cont (qe->cont_cls, res, emsg);
378 * Handle an incoming message of type
379 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
381 * @param qe the respective entry in the message queue
382 * @param msg the message we received
383 * @param size the message size
384 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
387 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
388 const struct RecordResultMessage *msg,
397 unsigned int rd_count;
399 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 rd_len = ntohs (msg->rd_len);
403 rd_count = ntohs (msg->rd_count);
404 msg_len = ntohs (msg->gns_header.header.size);
405 name_len = ntohs (msg->name_len);
406 GNUNET_break (0 == ntohs (msg->reserved));
407 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
408 if (msg_len != exp_msg_len)
411 return GNUNET_SYSERR;
413 name = (const char *) &msg[1];
414 if ( (name_len > 0) &&
415 ('\0' != name[name_len -1]) )
418 return GNUNET_SYSERR;
420 rd_tmp = &name[name_len];
422 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
424 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd))
427 return GNUNET_SYSERR;
431 if (NULL != qe->proc)
432 qe->proc (qe->proc_cls,
436 (rd_count > 0) ? rd : NULL);
443 * Handle an incoming message of type
444 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
446 * @param qe the respective entry in the message queue
447 * @param msg the message we received
448 * @param size the message size
449 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
450 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
453 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
454 const struct ZoneToNameResponseMessage *msg,
460 unsigned int rd_count;
461 const char *name_tmp;
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "ZONE_TO_NAME_RESPONSE");
467 res = ntohs (msg->res);
471 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "An error occured during zone to name operation\n");
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Namestore has no result for zone to name mapping \n");
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 "Namestore has result for zone to name mapping \n");
481 name_len = ntohs (msg->name_len);
482 rd_count = ntohs (msg->rd_count);
483 rd_ser_len = ntohs (msg->rd_len);
484 name_tmp = (const char *) &msg[1];
485 if ( (name_len > 0) &&
486 ('\0' != name_tmp[name_len -1]) )
489 return GNUNET_SYSERR;
491 rd_tmp = &name_tmp[name_len];
493 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
495 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
498 return GNUNET_SYSERR;
500 /* normal end, call continuation with result */
501 if (NULL != qe->proc)
502 qe->proc (qe->proc_cls,
506 /* return is important here: break would call continuation with error! */
511 return GNUNET_SYSERR;
513 /* error case, call continuation with error */
514 if (NULL != qe->proc)
515 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
521 * Handle incoming messages for record operations
523 * @param qe the respective zone iteration handle
524 * @param msg the message we received
525 * @param type the message type in host byte order
526 * @param size the message size
527 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
528 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
531 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
532 const struct GNUNET_MessageHeader *msg,
536 /* handle different message type */
539 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE:
540 if (size < sizeof (struct LookupBlockResponseMessage))
543 return GNUNET_SYSERR;
545 return handle_lookup_block_response (qe, (const struct LookupBlockResponseMessage *) msg, size);
546 case GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE:
547 if (size != sizeof (struct BlockCacheResponseMessage))
550 return GNUNET_SYSERR;
552 return handle_block_cache_response (qe, (const struct BlockCacheResponseMessage *) msg, size);
553 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
554 if (size != sizeof (struct RecordStoreResponseMessage))
557 return GNUNET_SYSERR;
559 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
560 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
561 if (size < sizeof (struct ZoneToNameResponseMessage))
564 return GNUNET_SYSERR;
566 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
567 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
568 if (size < sizeof (struct RecordResultMessage))
571 return GNUNET_SYSERR;
573 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
576 return GNUNET_SYSERR;
582 * Handle a response from NAMESTORE service for a zone iteration request
584 * @param ze the respective iterator for this operation
585 * @param msg the message containing the respoonse
586 * @param size the message size
587 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
588 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
591 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
592 const struct RecordResultMessage *msg,
595 static struct GNUNET_CRYPTO_EccPrivateKey priv_dummy;
601 const char *name_tmp;
602 const char *rd_ser_tmp;
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "ZONE_ITERATION_RESPONSE");
607 msg_len = ntohs (msg->gns_header.header.size);
608 rd_len = ntohs (msg->rd_len);
609 rd_count = ntohs (msg->rd_count);
610 name_len = ntohs (msg->name_len);
611 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
612 if (msg_len != exp_msg_len)
615 return GNUNET_SYSERR;
617 if ( (0 == name_len) &&
618 (0 == (memcmp (&msg->private_key,
620 sizeof (priv_dummy)))) )
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "Zone iteration completed!\n");
624 if (NULL != ze->proc)
625 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
628 name_tmp = (const char *) &msg[1];
629 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
632 return GNUNET_SYSERR;
634 rd_ser_tmp = (const char *) &name_tmp[name_len];
636 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
638 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len,
644 return GNUNET_SYSERR;
646 if (NULL != ze->proc)
647 ze->proc (ze->proc_cls,
657 * Handle incoming messages for zone iterations
659 * @param ze the respective zone iteration handle
660 * @param msg the message we received
661 * @param type the message type in HBO
662 * @param size the message size
663 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
664 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
667 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
668 const struct GNUNET_MessageHeader *msg,
669 int type, size_t size)
671 /* handle different message type */
674 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
675 if (size < sizeof (struct RecordResultMessage))
678 return GNUNET_SYSERR;
680 return handle_zone_iteration_response (ze,
681 (const struct RecordResultMessage *) msg,
685 return GNUNET_SYSERR;
691 * Type of a function to call when we receive a message
694 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
695 * @param msg message received, NULL on timeout or fatal error
698 process_namestore_message (void *cls,
699 const struct GNUNET_MessageHeader *msg)
701 struct GNUNET_NAMESTORE_Handle *h = cls;
702 const struct GNUNET_NAMESTORE_Header *gm;
703 struct GNUNET_NAMESTORE_QueueEntry *qe;
704 struct GNUNET_NAMESTORE_ZoneIterator *ze;
715 size = ntohs (msg->size);
716 type = ntohs (msg->type);
717 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
720 GNUNET_CLIENT_receive (h->client,
721 &process_namestore_message, h,
722 GNUNET_TIME_UNIT_FOREVER_REL);
725 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
726 r_id = ntohl (gm->r_id);
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Received message type %u size %u op %u\n",
732 (unsigned int) r_id);
734 /* Is it a record related operation ? */
735 for (qe = h->op_head; qe != NULL; qe = qe->next)
736 if (qe->op_id == r_id)
740 ret = manage_record_operations (qe, msg, type, size);
741 if (GNUNET_SYSERR == ret)
743 /* protocol error, need to reconnect */
744 h->reconnect = GNUNET_YES;
748 /* client was notified about success or failure, clean up 'qe' */
749 GNUNET_CONTAINER_DLL_remove (h->op_head,
755 /* Is it a zone iteration operation? */
756 for (ze = h->z_head; ze != NULL; ze = ze->next)
757 if (ze->op_id == r_id)
761 ret = manage_zone_operations (ze, msg, type, size);
762 if (GNUNET_NO == ret)
764 /* end of iteration, clean up 'ze' */
765 GNUNET_CONTAINER_DLL_remove (h->z_head,
770 if (GNUNET_SYSERR == ret)
772 /* protocol error, need to reconnect */
773 h->reconnect = GNUNET_YES;
776 if (GNUNET_YES == h->reconnect)
781 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
782 GNUNET_TIME_UNIT_FOREVER_REL);
787 * Transmit messages from the message queue to the service
788 * (if there are any, and if we are not already trying).
790 * @param h handle to use
793 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
797 * We can now transmit a message to NAMESTORE. Do it.
799 * @param cls the `struct GNUNET_NAMESTORE_Handle`
800 * @param size number of bytes we can transmit
801 * @param buf where to copy the messages
802 * @return number of bytes copied into @a buf
805 transmit_message_to_namestore (void *cls,
809 struct GNUNET_NAMESTORE_Handle *h = cls;
810 struct PendingMessage *p;
815 if ((0 == size) || (NULL == buf))
822 while ( (NULL != (p = h->pending_head)) &&
825 memcpy (&cbuf[ret], &p[1], p->size);
828 GNUNET_CONTAINER_DLL_remove (h->pending_head,
831 if (GNUNET_NO == h->is_receiving)
833 h->is_receiving = GNUNET_YES;
834 GNUNET_CLIENT_receive (h->client,
835 &process_namestore_message, h,
836 GNUNET_TIME_UNIT_FOREVER_REL);
846 * Transmit messages from the message queue to the service
847 * (if there are any, and if we are not already trying).
849 * @param h handle to use
852 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
854 struct PendingMessage *p;
857 return; /* transmission request already pending */
858 if (NULL == (p = h->pending_head))
859 return; /* transmission queue empty */
860 if (NULL == h->client)
861 return; /* currently reconnecting */
862 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
863 GNUNET_TIME_UNIT_FOREVER_REL,
864 GNUNET_NO, &transmit_message_to_namestore,
866 GNUNET_break (NULL != h->th);
871 * Reconnect to namestore service.
873 * @param h the handle to the NAMESTORE service
876 reconnect (struct GNUNET_NAMESTORE_Handle *h)
878 GNUNET_assert (NULL == h->client);
879 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
880 GNUNET_assert (NULL != h->client);
886 * Re-establish the connection to the service.
888 * @param cls handle to use to re-connect.
889 * @param tc scheduler context
892 reconnect_task (void *cls,
893 const struct GNUNET_SCHEDULER_TaskContext *tc)
895 struct GNUNET_NAMESTORE_Handle *h = cls;
897 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
903 * Disconnect from service and then reconnect.
905 * @param h our handle
908 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
912 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
915 h->reconnect = GNUNET_NO;
916 GNUNET_CLIENT_disconnect (h->client);
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Reconnecting to namestore\n");
919 h->is_receiving = GNUNET_NO;
921 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
922 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
929 * Get a fresh operation id to distinguish between namestore requests
931 * @param h the namestore handle
932 * @return next operation id to use
935 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
937 return h->last_op_id_used++;
942 * Initialize the connection with the NAMESTORE service.
944 * @param cfg configuration to use
945 * @return handle to the GNS service, or NULL on error
947 struct GNUNET_NAMESTORE_Handle *
948 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
950 struct GNUNET_NAMESTORE_Handle *h;
952 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
954 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
955 h->last_op_id_used = 0;
961 * Disconnect from the namestore service (and free associated
964 * @param h handle to the namestore
967 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
969 struct PendingMessage *p;
970 struct GNUNET_NAMESTORE_QueueEntry *q;
971 struct GNUNET_NAMESTORE_ZoneIterator *z;
973 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
974 GNUNET_assert (NULL != h);
977 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
980 while (NULL != (p = h->pending_head))
982 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
985 GNUNET_break (NULL == h->op_head);
986 while (NULL != (q = h->op_head))
988 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
991 GNUNET_break (NULL == h->z_head);
992 while (NULL != (z = h->z_head))
994 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
997 if (NULL != h->client)
999 GNUNET_CLIENT_disconnect (h->client);
1002 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
1004 GNUNET_SCHEDULER_cancel (h->reconnect_task);
1005 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1012 * Store an item in the namestore. If the item is already present,
1013 * it is replaced with the new record.
1015 * @param h handle to the namestore
1016 * @param block block to store
1017 * @param cont continuation to call when done
1018 * @param cont_cls closure for cont
1019 * @return handle to abort the request
1021 struct GNUNET_NAMESTORE_QueueEntry *
1022 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
1023 const struct GNUNET_NAMESTORE_Block *block,
1024 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1027 struct GNUNET_NAMESTORE_QueueEntry *qe;
1028 struct PendingMessage *pe;
1029 struct BlockCacheMessage *msg;
1034 GNUNET_assert (NULL != h);
1035 blen = ntohl (block->purpose.size)
1036 - sizeof (struct GNUNET_TIME_AbsoluteNBO)
1037 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
1038 rid = get_op_id (h);
1039 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1042 qe->cont_cls = cont_cls;
1044 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1047 msg_size = sizeof (struct BlockCacheMessage) + blen;
1048 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1049 pe->size = msg_size;
1050 msg = (struct BlockCacheMessage *) &pe[1];
1051 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE);
1052 msg->gns_header.header.size = htons (msg_size);
1053 msg->gns_header.r_id = htonl (rid);
1054 msg->expire = block->expiration_time;
1055 msg->signature = block->signature;
1056 msg->derived_key = block->derived_key;
1057 memcpy (&msg[1], &block[1], blen);
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1059 "Sending `%s' message with size %u\n",
1060 "NAMESTORE_BLOCK_CACHE",
1061 (unsigned int) msg_size);
1062 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1069 * Store an item in the namestore. If the item is already present,
1070 * it is replaced with the new record. Use an empty array to
1071 * remove all records under the given name.
1073 * @param h handle to the namestore
1074 * @param pkey private key of the zone
1075 * @param label name that is being mapped (at most 255 characters long)
1076 * @param rd_count number of records in the 'rd' array
1077 * @param rd array of records with data to store
1078 * @param cont continuation to call when done
1079 * @param cont_cls closure for 'cont'
1080 * @return handle to abort the request
1082 struct GNUNET_NAMESTORE_QueueEntry *
1083 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1084 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1086 unsigned int rd_count,
1087 const struct GNUNET_NAMESTORE_RecordData *rd,
1088 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1091 struct GNUNET_NAMESTORE_QueueEntry *qe;
1092 struct PendingMessage *pe;
1099 struct RecordStoreMessage *msg;
1101 GNUNET_assert (NULL != h);
1102 GNUNET_assert (NULL != pkey);
1103 GNUNET_assert (NULL != label);
1104 name_len = strlen (label) + 1;
1105 if (name_len > MAX_NAME_LEN)
1110 rid = get_op_id (h);
1111 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1114 qe->cont_cls = cont_cls;
1116 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1119 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1120 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1121 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1122 pe->size = msg_size;
1123 msg = (struct RecordStoreMessage *) &pe[1];
1124 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1125 msg->gns_header.header.size = htons (msg_size);
1126 msg->gns_header.r_id = htonl (rid);
1127 msg->name_len = htons (name_len);
1128 msg->rd_count = htons (rd_count);
1129 msg->rd_len = htons (rd_ser_len);
1130 msg->reserved = htons (0);
1131 msg->private_key = *pkey;
1133 name_tmp = (char *) &msg[1];
1134 memcpy (name_tmp, label, name_len);
1135 rd_ser = &name_tmp[name_len];
1136 GNUNET_break (rd_ser_len ==
1137 GNUNET_NAMESTORE_records_serialize (rd_count, rd,
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Sending `%s' message for name `%s' with size %u and %u records\n",
1142 "NAMESTORE_RECORD_STORE", label, msg_size,
1144 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1151 * Get a result for a particular key from the namestore. The processor
1152 * will only be called once.
1154 * @param h handle to the namestore
1155 * @param derived_hash hash of zone key combined with name to lookup
1156 * @param proc function to call on the matching block, or with
1157 * NULL if there is no matching block
1158 * @param proc_cls closure for proc
1159 * @return a handle that can be used to cancel
1161 struct GNUNET_NAMESTORE_QueueEntry *
1162 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
1163 const struct GNUNET_HashCode *derived_hash,
1164 GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls)
1166 struct GNUNET_NAMESTORE_QueueEntry *qe;
1167 struct PendingMessage *pe;
1168 struct LookupBlockMessage *msg;
1172 GNUNET_assert (NULL != h);
1173 GNUNET_assert (NULL != derived_hash);
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Looking for block under %s\n",
1176 GNUNET_h2s (derived_hash));
1178 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1180 qe->block_proc = proc;
1181 qe->block_proc_cls = proc_cls;
1183 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1185 msg_size = sizeof (struct LookupBlockMessage);
1186 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1187 pe->size = msg_size;
1188 msg = (struct LookupBlockMessage *) &pe[1];
1189 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK);
1190 msg->gns_header.header.size = htons (msg_size);
1191 msg->gns_header.r_id = htonl (rid);
1192 msg->query = *derived_hash;
1193 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1200 * Look for an existing PKEY delegation record for a given public key.
1201 * Returns at most one result to the processor.
1203 * @param h handle to the namestore
1204 * @param zone public key of the zone to look up in, never NULL
1205 * @param value_zone public key of the target zone (value), never NULL
1206 * @param proc function to call on the matching records, or with
1207 * NULL (rd_count == 0) if there are no matching records
1208 * @param proc_cls closure for @a proc
1209 * @return a handle that can be used to
1212 struct GNUNET_NAMESTORE_QueueEntry *
1213 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1214 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1215 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
1216 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1218 struct GNUNET_NAMESTORE_QueueEntry *qe;
1219 struct PendingMessage *pe;
1220 struct ZoneToNameMessage * msg;
1224 GNUNET_assert (NULL != h);
1225 GNUNET_assert (NULL != zone);
1226 GNUNET_assert (NULL != value_zone);
1228 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1231 qe->proc_cls = proc_cls;
1233 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1235 msg_size = sizeof (struct ZoneToNameMessage);
1236 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1237 pe->size = msg_size;
1238 msg = (struct ZoneToNameMessage *) &pe[1];
1239 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1240 msg->gns_header.header.size = htons (msg_size);
1241 msg->gns_header.r_id = htonl (rid);
1243 msg->value_zone = *value_zone;
1245 /* transmit message */
1246 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1253 * Starts a new zone iteration (used to periodically PUT all of our
1254 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1255 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1256 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1257 * immediately, and then again after
1258 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1260 * @param h handle to the namestore
1261 * @param zone zone to access, NULL for all zones
1262 * @param proc function to call on each name from the zone; it
1263 * will be called repeatedly with a value (if available)
1264 * and always once at the end with a name of NULL.
1265 * @param proc_cls closure for @a proc
1266 * @return an iterator handle to use for iteration
1268 struct GNUNET_NAMESTORE_ZoneIterator *
1269 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1270 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1271 GNUNET_NAMESTORE_RecordMonitor proc,
1274 struct GNUNET_NAMESTORE_ZoneIterator *it;
1275 struct PendingMessage *pe;
1276 struct ZoneIterationStartMessage * msg;
1280 GNUNET_assert (NULL != h);
1282 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1285 it->proc_cls = proc_cls;
1289 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1291 msg_size = sizeof (struct ZoneIterationStartMessage);
1292 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1293 pe->size = msg_size;
1294 msg = (struct ZoneIterationStartMessage *) &pe[1];
1295 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1296 msg->gns_header.header.size = htons (msg_size);
1297 msg->gns_header.r_id = htonl (rid);
1300 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1307 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1308 * for the next record.
1310 * @param it the iterator
1313 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1315 struct GNUNET_NAMESTORE_Handle *h;
1316 struct ZoneIterationNextMessage * msg;
1317 struct PendingMessage *pe;
1320 GNUNET_assert (NULL != it);
1322 msg_size = sizeof (struct ZoneIterationNextMessage);
1323 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1324 pe->size = msg_size;
1325 msg = (struct ZoneIterationNextMessage *) &pe[1];
1326 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1327 msg->gns_header.header.size = htons (msg_size);
1328 msg->gns_header.r_id = htonl (it->op_id);
1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1330 "Sending `%s' message\n",
1331 "ZONE_ITERATION_NEXT");
1332 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1338 * Stops iteration and releases the namestore handle for further calls.
1340 * @param it the iterator
1343 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1345 struct GNUNET_NAMESTORE_Handle *h;
1346 struct PendingMessage *pe;
1348 struct ZoneIterationStopMessage * msg;
1350 GNUNET_assert (NULL != it);
1352 GNUNET_CONTAINER_DLL_remove (h->z_head,
1355 msg_size = sizeof (struct ZoneIterationStopMessage);
1356 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1357 pe->size = msg_size;
1358 msg = (struct ZoneIterationStopMessage *) &pe[1];
1359 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1360 msg->gns_header.header.size = htons (msg_size);
1361 msg->gns_header.r_id = htonl (it->op_id);
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Sending `%s' message\n",
1364 "ZONE_ITERATION_STOP");
1365 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1372 * Cancel a namestore operation. The final callback from the
1373 * operation must not have been done yet.
1375 * @param qe operation to cancel
1378 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1380 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1382 GNUNET_assert (NULL != qe);
1383 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1388 /* end of namestore_api.c */