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;
86 * Function to call with the blocks we get back; or NULL.
88 GNUNET_NAMESTORE_BlockProcessor block_proc;
91 * Closure for '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 '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 * Should we reconnect to service due to some serious error?
234 * Did we start to receive yet?
239 * The last operation id used for a NAMESTORE operation
241 uint32_t last_op_id_used;
247 * Disconnect from service and then reconnect.
249 * @param h our handle
252 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
256 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE.
258 * @param qe the respective entry in the message queue
259 * @param msg the message we received
260 * @param size the message size
261 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
264 handle_lookup_block_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
265 const struct LookupBlockResponseMessage *msg,
268 struct GNUNET_NAMESTORE_Block *block;
269 char buf[size + sizeof (struct GNUNET_NAMESTORE_Block) - sizeof (struct LookupBlockResponseMessage)];
271 LOG (GNUNET_ERROR_TYPE_DEBUG,
273 "LOOKUP_BLOCK_RESPONSE");
274 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
277 if (NULL != qe->block_proc)
278 qe->block_proc (qe->block_proc_cls, NULL);
282 block = (struct GNUNET_NAMESTORE_Block *) buf;
283 block->signature = msg->signature;
284 block->derived_key = msg->derived_key;
285 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
286 block->purpose.size = htonl (size - sizeof (struct LookupBlockResponseMessage) +
287 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
288 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
289 block->expiration_time = msg->expire;
292 size - sizeof (struct LookupBlockResponseMessage));
294 GNUNET_NAMESTORE_block_verify (block))
297 return GNUNET_SYSERR;
299 if (NULL != qe->block_proc)
300 qe->block_proc (qe->block_proc_cls, block);
308 * Handle an incoming message of type 'GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE'
310 * @param qe the respective entry in the message queue
311 * @param msg the message we received
312 * @param size the message size
313 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
316 handle_block_cache_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
317 const struct BlockCacheResponseMessage *msg,
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s'\n",
323 "BLOCK_CACHE_RESPONSE");
324 res = ntohl (msg->op_result);
325 /* TODO: add actual error message from namestore to response... */
326 if (NULL != qe->cont)
327 qe->cont (qe->cont_cls, res, (GNUNET_OK == res) ? NULL : _("Namestore failed to cache block"));
333 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
335 * @param qe the respective entry in the message queue
336 * @param msg the message we received
337 * @param size the message size
338 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
341 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
342 const struct RecordStoreResponseMessage* msg,
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
350 "RECORD_STORE_RESPONSE");
351 /* TODO: add actual error message from namestore to response... */
352 res = ntohl (msg->op_result);
353 if (GNUNET_SYSERR == res)
354 emsg = _("Namestore failed to store record\n");
357 if (NULL != qe->cont)
358 qe->cont (qe->cont_cls, res, emsg);
364 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
366 * @param qe the respective entry in the message queue
367 * @param msg the message we received
368 * @param size the message size
369 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
372 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
373 const struct RecordResultMessage *msg,
382 unsigned int rd_count;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 rd_len = ntohs (msg->rd_len);
388 rd_count = ntohs (msg->rd_count);
389 msg_len = ntohs (msg->gns_header.header.size);
390 name_len = ntohs (msg->name_len);
391 GNUNET_break (0 == ntohs (msg->reserved));
392 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
393 if (msg_len != exp_msg_len)
396 return GNUNET_SYSERR;
398 name = (const char *) &msg[1];
399 if ( (name_len > 0) &&
400 ('\0' != name[name_len -1]) )
403 return GNUNET_SYSERR;
405 rd_tmp = &name[name_len];
407 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
409 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd))
412 return GNUNET_SYSERR;
416 if (NULL != qe->proc)
417 qe->proc (qe->proc_cls,
421 (rd_count > 0) ? rd : NULL);
428 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
430 * @param qe the respective entry in the message queue
431 * @param msg the message we received
432 * @param size the message size
433 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
434 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
437 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
438 const struct ZoneToNameResponseMessage *msg,
444 unsigned int rd_count;
445 const char *name_tmp;
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "ZONE_TO_NAME_RESPONSE");
451 res = ntohs (msg->res);
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "An error occured during zone to name operation\n");
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Namestore has no result for zone to name mapping \n");
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 "Namestore has result for zone to name mapping \n");
465 name_len = ntohs (msg->name_len);
466 rd_count = ntohs (msg->rd_count);
467 rd_ser_len = ntohs (msg->rd_len);
468 name_tmp = (const char *) &msg[1];
469 if ( (name_len > 0) &&
470 ('\0' != name_tmp[name_len -1]) )
473 return GNUNET_SYSERR;
475 rd_tmp = &name_tmp[name_len];
477 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
479 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
482 return GNUNET_SYSERR;
484 /* normal end, call continuation with result */
485 if (NULL != qe->proc)
486 qe->proc (qe->proc_cls,
490 /* return is important here: break would call continuation with error! */
495 return GNUNET_SYSERR;
497 /* error case, call continuation with error */
498 if (NULL != qe->proc)
499 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
505 * Handle incoming messages for record operations
507 * @param qe the respective zone iteration handle
508 * @param msg the message we received
509 * @param type the message type in host byte order
510 * @param size the message size
511 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
512 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
515 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
516 const struct GNUNET_MessageHeader *msg,
520 /* handle different message type */
523 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE:
524 if (size < sizeof (struct LookupBlockResponseMessage))
527 return GNUNET_SYSERR;
529 return handle_lookup_block_response (qe, (const struct LookupBlockResponseMessage *) msg, size);
530 case GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE:
531 if (size != sizeof (struct BlockCacheResponseMessage))
534 return GNUNET_SYSERR;
536 return handle_block_cache_response (qe, (const struct BlockCacheResponseMessage *) msg, size);
537 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
538 if (size != sizeof (struct RecordStoreResponseMessage))
541 return GNUNET_SYSERR;
543 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
544 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
545 if (size < sizeof (struct ZoneToNameResponseMessage))
548 return GNUNET_SYSERR;
550 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
551 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
552 if (size < sizeof (struct RecordResultMessage))
555 return GNUNET_SYSERR;
557 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
560 return GNUNET_SYSERR;
566 * Handle a response from NAMESTORE service for a zone iteration request
568 * @param ze the respective iterator for this operation
569 * @param msg the message containing the respoonse
570 * @param size the message size
571 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
572 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
575 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
576 const struct RecordResultMessage *msg,
579 static struct GNUNET_CRYPTO_EccPrivateKey priv_dummy;
585 const char *name_tmp;
586 const char *rd_ser_tmp;
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "ZONE_ITERATION_RESPONSE");
591 msg_len = ntohs (msg->gns_header.header.size);
592 rd_len = ntohs (msg->rd_len);
593 rd_count = ntohs (msg->rd_count);
594 name_len = ntohs (msg->name_len);
595 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
596 if (msg_len != exp_msg_len)
599 return GNUNET_SYSERR;
601 if ( (0 == name_len) &&
602 (0 == (memcmp (&msg->private_key, &priv_dummy, sizeof (priv_dummy)))) )
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
605 "Zone iteration is completed!\n");
606 if (NULL != ze->proc)
607 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
610 name_tmp = (const char *) &msg[1];
611 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
614 return GNUNET_SYSERR;
616 rd_ser_tmp = (const char *) &name_tmp[name_len];
618 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
620 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
623 return GNUNET_SYSERR;
625 if (NULL != ze->proc)
626 ze->proc (ze->proc_cls, &msg->private_key, name_tmp, rd_count, rd);
633 * Handle incoming messages for zone iterations
635 * @param ze the respective zone iteration handle
636 * @param msg the message we received
637 * @param type the message type in HBO
638 * @param size the message size
639 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
640 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
643 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
644 const struct GNUNET_MessageHeader *msg,
645 int type, size_t size)
647 /* handle different message type */
650 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
651 if (size < sizeof (struct RecordResultMessage))
654 return GNUNET_SYSERR;
656 return handle_zone_iteration_response (ze, (const struct RecordResultMessage *) msg, size);
659 return GNUNET_SYSERR;
665 * Type of a function to call when we receive a message
668 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
669 * @param msg message received, NULL on timeout or fatal error
672 process_namestore_message (void *cls,
673 const struct GNUNET_MessageHeader *msg)
675 struct GNUNET_NAMESTORE_Handle *h = cls;
676 const struct GNUNET_NAMESTORE_Header *gm;
677 struct GNUNET_NAMESTORE_QueueEntry *qe;
678 struct GNUNET_NAMESTORE_ZoneIterator *ze;
689 size = ntohs (msg->size);
690 type = ntohs (msg->type);
691 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
694 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
695 GNUNET_TIME_UNIT_FOREVER_REL);
698 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
699 r_id = ntohl (gm->r_id);
701 LOG (GNUNET_ERROR_TYPE_DEBUG,
702 "Received message type %u size %u op %u\n",
705 (unsigned int) r_id);
707 /* Is it a record related operation ? */
708 for (qe = h->op_head; qe != NULL; qe = qe->next)
709 if (qe->op_id == r_id)
713 ret = manage_record_operations (qe, msg, type, size);
714 if (GNUNET_SYSERR == ret)
716 /* protocol error, need to reconnect */
717 h->reconnect = GNUNET_YES;
721 /* client was notified about success or failure, clean up 'qe' */
722 GNUNET_CONTAINER_DLL_remove (h->op_head,
728 /* Is it a zone iteration operation? */
729 for (ze = h->z_head; ze != NULL; ze = ze->next)
730 if (ze->op_id == r_id)
734 ret = manage_zone_operations (ze, msg, type, size);
735 if (GNUNET_NO == ret)
737 /* end of iteration, clean up 'ze' */
738 GNUNET_CONTAINER_DLL_remove (h->z_head,
743 if (GNUNET_SYSERR == ret)
745 /* protocol error, need to reconnect */
746 h->reconnect = GNUNET_YES;
749 if (GNUNET_YES == h->reconnect)
754 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
755 GNUNET_TIME_UNIT_FOREVER_REL);
760 * Transmit messages from the message queue to the service
761 * (if there are any, and if we are not already trying).
763 * @param h handle to use
766 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
770 * We can now transmit a message to NAMESTORE. Do it.
772 * @param cls the `struct GNUNET_NAMESTORE_Handle`
773 * @param size number of bytes we can transmit
774 * @param buf where to copy the messages
775 * @return number of bytes copied into buf
778 transmit_message_to_namestore (void *cls, size_t size, void *buf)
780 struct GNUNET_NAMESTORE_Handle *h = cls;
781 struct PendingMessage *p;
786 if ((0 == size) || (NULL == buf))
793 while ((NULL != (p = h->pending_head)) && (p->size <= size))
795 memcpy (&cbuf[ret], &p[1], p->size);
798 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
799 if (GNUNET_NO == h->is_receiving)
801 h->is_receiving = GNUNET_YES;
802 GNUNET_CLIENT_receive (h->client,
803 &process_namestore_message, h,
804 GNUNET_TIME_UNIT_FOREVER_REL);
814 * Transmit messages from the message queue to the service
815 * (if there are any, and if we are not already trying).
817 * @param h handle to use
820 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
822 struct PendingMessage *p;
825 return; /* transmission request already pending */
826 if (NULL == (p = h->pending_head))
827 return; /* transmission queue empty */
828 if (NULL == h->client)
829 return; /* currently reconnecting */
830 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
831 GNUNET_TIME_UNIT_FOREVER_REL,
832 GNUNET_NO, &transmit_message_to_namestore,
838 * Reconnect to namestore service.
840 * @param h the handle to the NAMESTORE service
843 reconnect (struct GNUNET_NAMESTORE_Handle *h)
845 GNUNET_assert (NULL == h->client);
846 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
847 GNUNET_assert (NULL != h->client);
853 * Re-establish the connection to the service.
855 * @param cls handle to use to re-connect.
856 * @param tc scheduler context
859 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
861 struct GNUNET_NAMESTORE_Handle *h = cls;
863 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
869 * Disconnect from service and then reconnect.
871 * @param h our handle
874 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
876 h->reconnect = GNUNET_NO;
877 GNUNET_CLIENT_disconnect (h->client);
878 h->is_receiving = GNUNET_NO;
880 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
887 * Get a fresh operation id to distinguish between namestore requests
889 * @param h the namestore handle
890 * @return next operation id to use
893 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
895 return h->last_op_id_used++;
900 * Initialize the connection with the NAMESTORE service.
902 * @param cfg configuration to use
903 * @return handle to the GNS service, or NULL on error
905 struct GNUNET_NAMESTORE_Handle *
906 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
908 struct GNUNET_NAMESTORE_Handle *h;
910 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
912 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
913 h->last_op_id_used = 0;
919 * Disconnect from the namestore service (and free associated
922 * @param h handle to the namestore
925 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
927 struct PendingMessage *p;
928 struct GNUNET_NAMESTORE_QueueEntry *q;
929 struct GNUNET_NAMESTORE_ZoneIterator *z;
931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
932 GNUNET_assert (NULL != h);
933 while (NULL != (p = h->pending_head))
935 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
938 GNUNET_break (NULL == h->op_head);
939 while (NULL != (q = h->op_head))
941 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
944 GNUNET_break (NULL == h->z_head);
945 while (NULL != (z = h->z_head))
947 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
950 if (NULL != h->client)
952 GNUNET_CLIENT_disconnect (h->client);
955 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
957 GNUNET_SCHEDULER_cancel (h->reconnect_task);
958 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
965 * Store an item in the namestore. If the item is already present,
966 * it is replaced with the new record.
968 * @param h handle to the namestore
969 * @param block block to store
970 * @param cont continuation to call when done
971 * @param cont_cls closure for cont
972 * @return handle to abort the request
974 struct GNUNET_NAMESTORE_QueueEntry *
975 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
976 const struct GNUNET_NAMESTORE_Block *block,
977 GNUNET_NAMESTORE_ContinuationWithStatus cont,
980 struct GNUNET_NAMESTORE_QueueEntry *qe;
981 struct PendingMessage *pe;
982 struct BlockCacheMessage *msg;
987 GNUNET_assert (NULL != h);
988 blen = ntohl (block->purpose.size) - sizeof (struct GNUNET_TIME_AbsoluteNBO);
990 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
993 qe->cont_cls = cont_cls;
995 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
998 msg_size = sizeof (struct BlockCacheMessage) + blen;
999 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1000 pe->size = msg_size;
1001 msg = (struct BlockCacheMessage *) &pe[1];
1002 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE);
1003 msg->gns_header.header.size = htons (msg_size);
1004 msg->gns_header.r_id = htonl (rid);
1005 msg->expire = block->expiration_time;
1006 msg->signature = block->signature;
1007 msg->derived_key = block->derived_key;
1008 memcpy (&msg[1], &block[1], blen);
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "Sending `%s' message with size %u\n",
1011 "NAMESTORE_BLOCK_CACHE",
1012 (unsigned int) msg_size);
1013 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1020 * Store an item in the namestore. If the item is already present,
1021 * it is replaced with the new record. Use an empty array to
1022 * remove all records under the given name.
1024 * @param h handle to the namestore
1025 * @param pkey private key of the zone
1026 * @param label name that is being mapped (at most 255 characters long)
1027 * @param rd_count number of records in the 'rd' array
1028 * @param rd array of records with data to store
1029 * @param cont continuation to call when done
1030 * @param cont_cls closure for 'cont'
1031 * @return handle to abort the request
1033 struct GNUNET_NAMESTORE_QueueEntry *
1034 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1035 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1037 unsigned int rd_count,
1038 const struct GNUNET_NAMESTORE_RecordData *rd,
1039 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1042 struct GNUNET_NAMESTORE_QueueEntry *qe;
1043 struct PendingMessage *pe;
1050 struct RecordStoreMessage *msg;
1052 GNUNET_assert (NULL != h);
1053 GNUNET_assert (NULL != pkey);
1054 GNUNET_assert (NULL != label);
1055 name_len = strlen (label) + 1;
1056 if (name_len > MAX_NAME_LEN)
1061 rid = get_op_id (h);
1062 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1065 qe->cont_cls = cont_cls;
1067 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1070 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1071 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1072 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1073 pe->size = msg_size;
1074 msg = (struct RecordStoreMessage *) &pe[1];
1075 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1076 msg->gns_header.header.size = htons (msg_size);
1077 msg->gns_header.r_id = htonl (rid);
1078 msg->name_len = htons (name_len);
1079 msg->rd_count = htons (rd_count);
1080 msg->rd_len = htons (rd_ser_len);
1081 msg->reserved = htons (0);
1082 msg->private_key = *pkey;
1084 name_tmp = (char *) &msg[1];
1085 memcpy (name_tmp, label, name_len);
1086 rd_ser = &name_tmp[name_len];
1087 GNUNET_break (rd_ser_len == GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser));
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Sending `%s' message for name `%s' with size %u and %u records\n",
1091 "NAMESTORE_RECORD_STORE", label, msg_size,
1093 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1100 * Get a result for a particular key from the namestore. The processor
1101 * will only be called once.
1103 * @param h handle to the namestore
1104 * @param derived_hash hash of zone key combined with name to lookup
1105 * @param proc function to call on the matching block, or with
1106 * NULL if there is no matching block
1107 * @param proc_cls closure for proc
1108 * @return a handle that can be used to cancel
1110 struct GNUNET_NAMESTORE_QueueEntry *
1111 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
1112 const struct GNUNET_HashCode *derived_hash,
1113 GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls)
1115 struct GNUNET_NAMESTORE_QueueEntry *qe;
1116 struct PendingMessage *pe;
1117 struct LookupBlockMessage *msg;
1121 GNUNET_assert (NULL != h);
1122 GNUNET_assert (NULL != derived_hash);
1123 LOG (GNUNET_ERROR_TYPE_DEBUG,
1124 "Looking for block under %s\n",
1125 GNUNET_h2s (derived_hash));
1127 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1129 qe->block_proc = proc;
1130 qe->block_proc_cls = proc_cls;
1132 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1134 msg_size = sizeof (struct LookupBlockMessage);
1135 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1136 pe->size = msg_size;
1137 msg = (struct LookupBlockMessage *) &pe[1];
1138 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK);
1139 msg->gns_header.header.size = htons (msg_size);
1140 msg->gns_header.r_id = htonl (rid);
1141 msg->query = *derived_hash;
1142 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1149 * Look for an existing PKEY delegation record for a given public key.
1150 * Returns at most one result to the processor.
1152 * @param h handle to the namestore
1153 * @param zone public key of the zone to look up in, never NULL
1154 * @param value_zone public key of the target zone (value), never NULL
1155 * @param proc function to call on the matching records, or with
1156 * NULL (rd_count == 0) if there are no matching records
1157 * @param proc_cls closure for proc
1158 * @return a handle that can be used to
1161 struct GNUNET_NAMESTORE_QueueEntry *
1162 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1163 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1164 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
1165 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1167 struct GNUNET_NAMESTORE_QueueEntry *qe;
1168 struct PendingMessage *pe;
1169 struct ZoneToNameMessage * msg;
1173 GNUNET_assert (NULL != h);
1174 GNUNET_assert (NULL != zone);
1175 GNUNET_assert (NULL != value_zone);
1177 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1180 qe->proc_cls = proc_cls;
1182 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1184 msg_size = sizeof (struct ZoneToNameMessage);
1185 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1186 pe->size = msg_size;
1187 msg = (struct ZoneToNameMessage *) &pe[1];
1188 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1189 msg->gns_header.header.size = htons (msg_size);
1190 msg->gns_header.r_id = htonl (rid);
1192 msg->value_zone = *value_zone;
1194 /* transmit message */
1195 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1202 * Starts a new zone iteration (used to periodically PUT all of our
1203 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1204 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1205 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1206 * immediately, and then again after
1207 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1209 * @param h handle to the namestore
1210 * @param zone zone to access, NULL for all zones
1211 * @param proc function to call on each name from the zone; it
1212 * will be called repeatedly with a value (if available)
1213 * and always once at the end with a name of NULL.
1214 * @param proc_cls closure for @a proc
1215 * @return an iterator handle to use for iteration
1217 struct GNUNET_NAMESTORE_ZoneIterator *
1218 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1219 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1220 GNUNET_NAMESTORE_RecordMonitor proc,
1223 struct GNUNET_NAMESTORE_ZoneIterator *it;
1224 struct PendingMessage *pe;
1225 struct ZoneIterationStartMessage * msg;
1229 GNUNET_assert (NULL != h);
1231 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1234 it->proc_cls = proc_cls;
1238 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1240 msg_size = sizeof (struct ZoneIterationStartMessage);
1241 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1242 pe->size = msg_size;
1243 msg = (struct ZoneIterationStartMessage *) &pe[1];
1244 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1245 msg->gns_header.header.size = htons (msg_size);
1246 msg->gns_header.r_id = htonl (rid);
1249 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1256 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1257 * for the next record.
1259 * @param it the iterator
1262 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1264 struct GNUNET_NAMESTORE_Handle *h;
1265 struct ZoneIterationNextMessage * msg;
1266 struct PendingMessage *pe;
1269 GNUNET_assert (NULL != it);
1271 msg_size = sizeof (struct ZoneIterationNextMessage);
1272 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1273 pe->size = msg_size;
1274 msg = (struct ZoneIterationNextMessage *) &pe[1];
1275 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1276 msg->gns_header.header.size = htons (msg_size);
1277 msg->gns_header.r_id = htonl (it->op_id);
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1279 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1285 * Stops iteration and releases the namestore handle for further calls.
1287 * @param it the iterator
1290 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1292 struct GNUNET_NAMESTORE_Handle *h;
1293 struct PendingMessage *pe;
1295 struct ZoneIterationStopMessage * msg;
1297 GNUNET_assert (NULL != it);
1299 GNUNET_CONTAINER_DLL_remove (h->z_head,
1302 msg_size = sizeof (struct ZoneIterationStopMessage);
1303 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1304 pe->size = msg_size;
1305 msg = (struct ZoneIterationStopMessage *) &pe[1];
1306 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1307 msg->gns_header.header.size = htons (msg_size);
1308 msg->gns_header.r_id = htonl (it->op_id);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Sending `%s' message\n",
1311 "ZONE_ITERATION_STOP");
1312 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1319 * Cancel a namestore operation. The final callback from the
1320 * operation must not have been done yet.
1322 * @param qe operation to cancel
1325 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1327 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1329 GNUNET_assert (NULL != qe);
1330 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1335 /* end of namestore_api.c */