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 and expiration %s\n",
1060 "NAMESTORE_BLOCK_CACHE",
1061 (unsigned int) msg_size,
1062 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (msg->expire)));
1063 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1070 * Store an item in the namestore. If the item is already present,
1071 * it is replaced with the new record. Use an empty array to
1072 * remove all records under the given name.
1074 * @param h handle to the namestore
1075 * @param pkey private key of the zone
1076 * @param label name that is being mapped (at most 255 characters long)
1077 * @param rd_count number of records in the 'rd' array
1078 * @param rd array of records with data to store
1079 * @param cont continuation to call when done
1080 * @param cont_cls closure for 'cont'
1081 * @return handle to abort the request
1083 struct GNUNET_NAMESTORE_QueueEntry *
1084 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1085 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1087 unsigned int rd_count,
1088 const struct GNUNET_NAMESTORE_RecordData *rd,
1089 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1092 struct GNUNET_NAMESTORE_QueueEntry *qe;
1093 struct PendingMessage *pe;
1100 struct RecordStoreMessage *msg;
1102 GNUNET_assert (NULL != h);
1103 GNUNET_assert (NULL != pkey);
1104 GNUNET_assert (NULL != label);
1105 name_len = strlen (label) + 1;
1106 if (name_len > MAX_NAME_LEN)
1111 rid = get_op_id (h);
1112 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1115 qe->cont_cls = cont_cls;
1117 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1120 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1121 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1122 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1123 pe->size = msg_size;
1124 msg = (struct RecordStoreMessage *) &pe[1];
1125 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1126 msg->gns_header.header.size = htons (msg_size);
1127 msg->gns_header.r_id = htonl (rid);
1128 msg->name_len = htons (name_len);
1129 msg->rd_count = htons (rd_count);
1130 msg->rd_len = htons (rd_ser_len);
1131 msg->reserved = htons (0);
1132 msg->private_key = *pkey;
1134 name_tmp = (char *) &msg[1];
1135 memcpy (name_tmp, label, name_len);
1136 rd_ser = &name_tmp[name_len];
1137 GNUNET_break (rd_ser_len ==
1138 GNUNET_NAMESTORE_records_serialize (rd_count, rd,
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Sending `%s' message for name `%s' with size %u and %u records\n",
1143 "NAMESTORE_RECORD_STORE", label, msg_size,
1145 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1152 * Get a result for a particular key from the namestore. The processor
1153 * will only be called once.
1155 * @param h handle to the namestore
1156 * @param derived_hash hash of zone key combined with name to lookup
1157 * @param proc function to call on the matching block, or with
1158 * NULL if there is no matching block
1159 * @param proc_cls closure for proc
1160 * @return a handle that can be used to cancel
1162 struct GNUNET_NAMESTORE_QueueEntry *
1163 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
1164 const struct GNUNET_HashCode *derived_hash,
1165 GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls)
1167 struct GNUNET_NAMESTORE_QueueEntry *qe;
1168 struct PendingMessage *pe;
1169 struct LookupBlockMessage *msg;
1173 GNUNET_assert (NULL != h);
1174 GNUNET_assert (NULL != derived_hash);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "Looking for block under %s\n",
1177 GNUNET_h2s (derived_hash));
1179 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1181 qe->block_proc = proc;
1182 qe->block_proc_cls = proc_cls;
1184 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1186 msg_size = sizeof (struct LookupBlockMessage);
1187 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1188 pe->size = msg_size;
1189 msg = (struct LookupBlockMessage *) &pe[1];
1190 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK);
1191 msg->gns_header.header.size = htons (msg_size);
1192 msg->gns_header.r_id = htonl (rid);
1193 msg->query = *derived_hash;
1194 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1201 * Look for an existing PKEY delegation record for a given public key.
1202 * Returns at most one result to the processor.
1204 * @param h handle to the namestore
1205 * @param zone public key of the zone to look up in, never NULL
1206 * @param value_zone public key of the target zone (value), never NULL
1207 * @param proc function to call on the matching records, or with
1208 * NULL (rd_count == 0) if there are no matching records
1209 * @param proc_cls closure for @a proc
1210 * @return a handle that can be used to
1213 struct GNUNET_NAMESTORE_QueueEntry *
1214 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1215 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1216 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
1217 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1219 struct GNUNET_NAMESTORE_QueueEntry *qe;
1220 struct PendingMessage *pe;
1221 struct ZoneToNameMessage * msg;
1225 GNUNET_assert (NULL != h);
1226 GNUNET_assert (NULL != zone);
1227 GNUNET_assert (NULL != value_zone);
1229 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1232 qe->proc_cls = proc_cls;
1234 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1236 msg_size = sizeof (struct ZoneToNameMessage);
1237 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1238 pe->size = msg_size;
1239 msg = (struct ZoneToNameMessage *) &pe[1];
1240 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1241 msg->gns_header.header.size = htons (msg_size);
1242 msg->gns_header.r_id = htonl (rid);
1244 msg->value_zone = *value_zone;
1246 /* transmit message */
1247 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1254 * Starts a new zone iteration (used to periodically PUT all of our
1255 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1256 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1257 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1258 * immediately, and then again after
1259 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1261 * @param h handle to the namestore
1262 * @param zone zone to access, NULL for all zones
1263 * @param proc function to call on each name from the zone; it
1264 * will be called repeatedly with a value (if available)
1265 * and always once at the end with a name of NULL.
1266 * @param proc_cls closure for @a proc
1267 * @return an iterator handle to use for iteration
1269 struct GNUNET_NAMESTORE_ZoneIterator *
1270 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1271 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1272 GNUNET_NAMESTORE_RecordMonitor proc,
1275 struct GNUNET_NAMESTORE_ZoneIterator *it;
1276 struct PendingMessage *pe;
1277 struct ZoneIterationStartMessage * msg;
1281 GNUNET_assert (NULL != h);
1283 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1286 it->proc_cls = proc_cls;
1290 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1292 msg_size = sizeof (struct ZoneIterationStartMessage);
1293 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1294 pe->size = msg_size;
1295 msg = (struct ZoneIterationStartMessage *) &pe[1];
1296 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1297 msg->gns_header.header.size = htons (msg_size);
1298 msg->gns_header.r_id = htonl (rid);
1301 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1308 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1309 * for the next record.
1311 * @param it the iterator
1314 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1316 struct GNUNET_NAMESTORE_Handle *h;
1317 struct ZoneIterationNextMessage * msg;
1318 struct PendingMessage *pe;
1321 GNUNET_assert (NULL != it);
1323 msg_size = sizeof (struct ZoneIterationNextMessage);
1324 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1325 pe->size = msg_size;
1326 msg = (struct ZoneIterationNextMessage *) &pe[1];
1327 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1328 msg->gns_header.header.size = htons (msg_size);
1329 msg->gns_header.r_id = htonl (it->op_id);
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 "Sending `%s' message\n",
1332 "ZONE_ITERATION_NEXT");
1333 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1339 * Stops iteration and releases the namestore handle for further calls.
1341 * @param it the iterator
1344 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1346 struct GNUNET_NAMESTORE_Handle *h;
1347 struct PendingMessage *pe;
1349 struct ZoneIterationStopMessage * msg;
1351 GNUNET_assert (NULL != it);
1353 GNUNET_CONTAINER_DLL_remove (h->z_head,
1356 msg_size = sizeof (struct ZoneIterationStopMessage);
1357 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1358 pe->size = msg_size;
1359 msg = (struct ZoneIterationStopMessage *) &pe[1];
1360 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1361 msg->gns_header.header.size = htons (msg_size);
1362 msg->gns_header.r_id = htonl (it->op_id);
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Sending `%s' message\n",
1365 "ZONE_ITERATION_STOP");
1366 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1373 * Cancel a namestore operation. The final callback from the
1374 * operation must not have been done yet.
1376 * @param qe operation to cancel
1379 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1381 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1383 GNUNET_assert (NULL != qe);
1384 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1389 /* end of namestore_api.c */