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 * The operation id this zone iteration operation has
94 * Handle for a zone iterator operation
96 struct GNUNET_NAMESTORE_ZoneIterator
102 struct GNUNET_NAMESTORE_ZoneIterator *next;
107 struct GNUNET_NAMESTORE_ZoneIterator *prev;
110 * Main handle to access the namestore.
112 struct GNUNET_NAMESTORE_Handle *h;
115 * The continuation to call with the results
117 GNUNET_NAMESTORE_RecordMonitor proc;
120 * Closure for @e proc.
125 * Private key of the zone.
127 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
130 * The operation id this zone iteration operation has
138 * Message in linked list we should send to the service. The
139 * actual binary message follows this struct.
141 struct PendingMessage
147 struct PendingMessage *next;
152 struct PendingMessage *prev;
155 * Size of the message.
163 * Connection to the NAMESTORE service.
165 struct GNUNET_NAMESTORE_Handle
169 * Configuration to use.
171 const struct GNUNET_CONFIGURATION_Handle *cfg;
174 * Socket (if available).
176 struct GNUNET_CLIENT_Connection *client;
179 * Currently pending transmission request (or NULL).
181 struct GNUNET_CLIENT_TransmitHandle *th;
184 * Head of linked list of pending messages to send to the service
186 struct PendingMessage *pending_head;
189 * Tail of linked list of pending messages to send to the service
191 struct PendingMessage *pending_tail;
194 * Head of pending namestore queue entries
196 struct GNUNET_NAMESTORE_QueueEntry *op_head;
199 * Tail of pending namestore queue entries
201 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
204 * Head of pending namestore zone iterator entries
206 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
209 * Tail of pending namestore zone iterator entries
211 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
216 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
219 * Delay introduced before we reconnect.
221 struct GNUNET_TIME_Relative reconnect_delay;
224 * Should we reconnect to service due to some serious error?
229 * Did we start to receive yet?
234 * The last operation id used for a NAMESTORE operation
236 uint32_t last_op_id_used;
242 * Disconnect from service and then reconnect.
244 * @param h our handle
247 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
251 * Handle an incoming message of type
252 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
254 * @param qe the respective entry in the message queue
255 * @param msg the message we received
256 * @param size the message size
257 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
260 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
261 const struct RecordStoreResponseMessage* msg,
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "RECORD_STORE_RESPONSE");
270 /* TODO: add actual error message from namestore to response... */
271 res = ntohl (msg->op_result);
272 if (GNUNET_SYSERR == res)
273 emsg = _("Namestore failed to store record\n");
276 if (NULL != qe->cont)
277 qe->cont (qe->cont_cls, res, emsg);
283 * Handle an incoming message of type
284 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
286 * @param qe the respective entry in the message queue
287 * @param msg the message we received
288 * @param size the message size
289 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
292 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
293 const struct RecordResultMessage *msg,
302 unsigned int rd_count;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 rd_len = ntohs (msg->rd_len);
308 rd_count = ntohs (msg->rd_count);
309 msg_len = ntohs (msg->gns_header.header.size);
310 name_len = ntohs (msg->name_len);
311 GNUNET_break (0 == ntohs (msg->reserved));
312 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
313 if (msg_len != exp_msg_len)
316 return GNUNET_SYSERR;
318 name = (const char *) &msg[1];
319 if ( (name_len > 0) &&
320 ('\0' != name[name_len -1]) )
323 return GNUNET_SYSERR;
325 rd_tmp = &name[name_len];
327 struct GNUNET_GNSRECORD_Data rd[rd_count];
329 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
332 return GNUNET_SYSERR;
336 if (NULL != qe->proc)
337 qe->proc (qe->proc_cls,
341 (rd_count > 0) ? rd : NULL);
348 * Handle an incoming message of type
349 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
351 * @param qe the respective entry in the message queue
352 * @param msg the message we received
353 * @param size the message size
354 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
355 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
358 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
359 const struct ZoneToNameResponseMessage *msg,
365 unsigned int rd_count;
366 const char *name_tmp;
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "ZONE_TO_NAME_RESPONSE");
372 res = ntohs (msg->res);
376 LOG (GNUNET_ERROR_TYPE_DEBUG,
377 "An error occured during zone to name operation\n");
380 LOG (GNUNET_ERROR_TYPE_DEBUG,
381 "Namestore has no result for zone to name mapping \n");
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Namestore has result for zone to name mapping \n");
386 name_len = ntohs (msg->name_len);
387 rd_count = ntohs (msg->rd_count);
388 rd_ser_len = ntohs (msg->rd_len);
389 name_tmp = (const char *) &msg[1];
390 if ( (name_len > 0) &&
391 ('\0' != name_tmp[name_len -1]) )
394 return GNUNET_SYSERR;
396 rd_tmp = &name_tmp[name_len];
398 struct GNUNET_GNSRECORD_Data rd[rd_count];
400 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
403 return GNUNET_SYSERR;
405 /* normal end, call continuation with result */
406 if (NULL != qe->proc)
407 qe->proc (qe->proc_cls,
411 /* return is important here: break would call continuation with error! */
416 return GNUNET_SYSERR;
418 /* error case, call continuation with error */
419 if (NULL != qe->proc)
420 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
426 * Handle incoming messages for record operations
428 * @param qe the respective zone iteration handle
429 * @param msg the message we received
430 * @param type the message type in host byte order
431 * @param size the message size
432 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
433 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
436 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
437 const struct GNUNET_MessageHeader *msg,
441 /* handle different message type */
444 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
445 if (size != sizeof (struct RecordStoreResponseMessage))
448 return GNUNET_SYSERR;
450 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
451 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
452 if (size < sizeof (struct ZoneToNameResponseMessage))
455 return GNUNET_SYSERR;
457 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
458 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
459 if (size < sizeof (struct RecordResultMessage))
462 return GNUNET_SYSERR;
464 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
467 return GNUNET_SYSERR;
473 * Handle a response from NAMESTORE service for a zone iteration request
475 * @param ze the respective iterator for this operation
476 * @param msg the message containing the respoonse
477 * @param size the message size
478 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
479 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
482 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
483 const struct RecordResultMessage *msg,
486 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
492 const char *name_tmp;
493 const char *rd_ser_tmp;
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
497 "ZONE_ITERATION_RESPONSE");
498 msg_len = ntohs (msg->gns_header.header.size);
499 rd_len = ntohs (msg->rd_len);
500 rd_count = ntohs (msg->rd_count);
501 name_len = ntohs (msg->name_len);
502 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
503 if (msg_len != exp_msg_len)
506 return GNUNET_SYSERR;
508 if ( (0 == name_len) &&
509 (0 == (memcmp (&msg->private_key,
511 sizeof (priv_dummy)))) )
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "Zone iteration completed!\n");
515 if (NULL != ze->proc)
516 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
519 name_tmp = (const char *) &msg[1];
520 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
523 return GNUNET_SYSERR;
525 rd_ser_tmp = (const char *) &name_tmp[name_len];
527 struct GNUNET_GNSRECORD_Data rd[rd_count];
529 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (rd_len,
535 return GNUNET_SYSERR;
537 if (NULL != ze->proc)
538 ze->proc (ze->proc_cls,
548 * Handle incoming messages for zone iterations
550 * @param ze the respective zone iteration handle
551 * @param msg the message we received
552 * @param type the message type in HBO
553 * @param size the message size
554 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
555 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
558 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
559 const struct GNUNET_MessageHeader *msg,
560 int type, size_t size)
562 /* handle different message type */
565 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
566 if (size < sizeof (struct RecordResultMessage))
569 return GNUNET_SYSERR;
571 return handle_zone_iteration_response (ze,
572 (const struct RecordResultMessage *) msg,
576 return GNUNET_SYSERR;
582 * Type of a function to call when we receive a message
585 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
586 * @param msg message received, NULL on timeout or fatal error
589 process_namestore_message (void *cls,
590 const struct GNUNET_MessageHeader *msg)
592 struct GNUNET_NAMESTORE_Handle *h = cls;
593 const struct GNUNET_NAMESTORE_Header *gm;
594 struct GNUNET_NAMESTORE_QueueEntry *qe;
595 struct GNUNET_NAMESTORE_ZoneIterator *ze;
606 size = ntohs (msg->size);
607 type = ntohs (msg->type);
608 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
611 GNUNET_CLIENT_receive (h->client,
612 &process_namestore_message, h,
613 GNUNET_TIME_UNIT_FOREVER_REL);
616 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
617 r_id = ntohl (gm->r_id);
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "Received message type %u size %u op %u\n",
623 (unsigned int) r_id);
625 /* Is it a record related operation ? */
626 for (qe = h->op_head; qe != NULL; qe = qe->next)
627 if (qe->op_id == r_id)
631 ret = manage_record_operations (qe, msg, type, size);
632 if (GNUNET_SYSERR == ret)
634 /* protocol error, need to reconnect */
635 h->reconnect = GNUNET_YES;
639 /* client was notified about success or failure, clean up 'qe' */
640 GNUNET_CONTAINER_DLL_remove (h->op_head,
646 /* Is it a zone iteration operation? */
647 for (ze = h->z_head; ze != NULL; ze = ze->next)
648 if (ze->op_id == r_id)
652 ret = manage_zone_operations (ze, msg, type, size);
653 if (GNUNET_NO == ret)
655 /* end of iteration, clean up 'ze' */
656 GNUNET_CONTAINER_DLL_remove (h->z_head,
661 if (GNUNET_SYSERR == ret)
663 /* protocol error, need to reconnect */
664 h->reconnect = GNUNET_YES;
667 if (GNUNET_YES == h->reconnect)
672 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
673 GNUNET_TIME_UNIT_FOREVER_REL);
678 * Transmit messages from the message queue to the service
679 * (if there are any, and if we are not already trying).
681 * @param h handle to use
684 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
688 * We can now transmit a message to NAMESTORE. Do it.
690 * @param cls the `struct GNUNET_NAMESTORE_Handle`
691 * @param size number of bytes we can transmit
692 * @param buf where to copy the messages
693 * @return number of bytes copied into @a buf
696 transmit_message_to_namestore (void *cls,
700 struct GNUNET_NAMESTORE_Handle *h = cls;
701 struct PendingMessage *p;
706 if ((0 == size) || (NULL == buf))
713 while ( (NULL != (p = h->pending_head)) &&
716 memcpy (&cbuf[ret], &p[1], p->size);
719 GNUNET_CONTAINER_DLL_remove (h->pending_head,
722 if (GNUNET_NO == h->is_receiving)
724 h->is_receiving = GNUNET_YES;
725 GNUNET_CLIENT_receive (h->client,
726 &process_namestore_message, h,
727 GNUNET_TIME_UNIT_FOREVER_REL);
737 * Transmit messages from the message queue to the service
738 * (if there are any, and if we are not already trying).
740 * @param h handle to use
743 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
745 struct PendingMessage *p;
748 return; /* transmission request already pending */
749 if (NULL == (p = h->pending_head))
750 return; /* transmission queue empty */
751 if (NULL == h->client)
752 return; /* currently reconnecting */
753 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
754 GNUNET_TIME_UNIT_FOREVER_REL,
755 GNUNET_NO, &transmit_message_to_namestore,
757 GNUNET_break (NULL != h->th);
762 * Reconnect to namestore service.
764 * @param h the handle to the NAMESTORE service
767 reconnect (struct GNUNET_NAMESTORE_Handle *h)
769 GNUNET_assert (NULL == h->client);
770 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
771 GNUNET_assert (NULL != h->client);
777 * Re-establish the connection to the service.
779 * @param cls handle to use to re-connect.
780 * @param tc scheduler context
783 reconnect_task (void *cls,
784 const struct GNUNET_SCHEDULER_TaskContext *tc)
786 struct GNUNET_NAMESTORE_Handle *h = cls;
788 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
794 * Disconnect from service and then reconnect.
796 * @param h our handle
799 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
803 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
806 h->reconnect = GNUNET_NO;
807 GNUNET_CLIENT_disconnect (h->client);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Reconnecting to namestore\n");
810 h->is_receiving = GNUNET_NO;
812 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
813 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
820 * Get a fresh operation id to distinguish between namestore requests
822 * @param h the namestore handle
823 * @return next operation id to use
826 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
828 return h->last_op_id_used++;
833 * Initialize the connection with the NAMESTORE service.
835 * @param cfg configuration to use
836 * @return handle to the GNS service, or NULL on error
838 struct GNUNET_NAMESTORE_Handle *
839 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
841 struct GNUNET_NAMESTORE_Handle *h;
843 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
845 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
846 h->last_op_id_used = 0;
852 * Disconnect from the namestore service (and free associated
855 * @param h handle to the namestore
858 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
860 struct PendingMessage *p;
861 struct GNUNET_NAMESTORE_QueueEntry *q;
862 struct GNUNET_NAMESTORE_ZoneIterator *z;
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
865 GNUNET_assert (NULL != h);
868 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
871 while (NULL != (p = h->pending_head))
873 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
876 GNUNET_break (NULL == h->op_head);
877 while (NULL != (q = h->op_head))
879 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
882 GNUNET_break (NULL == h->z_head);
883 while (NULL != (z = h->z_head))
885 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
888 if (NULL != h->client)
890 GNUNET_CLIENT_disconnect (h->client);
893 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
895 GNUNET_SCHEDULER_cancel (h->reconnect_task);
896 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
903 * Store an item in the namestore. If the item is already present,
904 * it is replaced with the new record. Use an empty array to
905 * remove all records under the given name.
907 * @param h handle to the namestore
908 * @param pkey private key of the zone
909 * @param label name that is being mapped (at most 255 characters long)
910 * @param rd_count number of records in the 'rd' array
911 * @param rd array of records with data to store
912 * @param cont continuation to call when done
913 * @param cont_cls closure for 'cont'
914 * @return handle to abort the request
916 struct GNUNET_NAMESTORE_QueueEntry *
917 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
918 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
920 unsigned int rd_count,
921 const struct GNUNET_GNSRECORD_Data *rd,
922 GNUNET_NAMESTORE_ContinuationWithStatus cont,
925 struct GNUNET_NAMESTORE_QueueEntry *qe;
926 struct PendingMessage *pe;
933 struct RecordStoreMessage *msg;
935 GNUNET_assert (NULL != h);
936 GNUNET_assert (NULL != pkey);
937 GNUNET_assert (NULL != label);
938 name_len = strlen (label) + 1;
939 if (name_len > MAX_NAME_LEN)
945 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
948 qe->cont_cls = cont_cls;
950 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
953 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
954 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
955 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
957 msg = (struct RecordStoreMessage *) &pe[1];
958 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
959 msg->gns_header.header.size = htons (msg_size);
960 msg->gns_header.r_id = htonl (rid);
961 msg->name_len = htons (name_len);
962 msg->rd_count = htons (rd_count);
963 msg->rd_len = htons (rd_ser_len);
964 msg->reserved = htons (0);
965 msg->private_key = *pkey;
967 name_tmp = (char *) &msg[1];
968 memcpy (name_tmp, label, name_len);
969 rd_ser = &name_tmp[name_len];
970 GNUNET_break (rd_ser_len ==
971 GNUNET_GNSRECORD_records_serialize (rd_count, rd,
974 LOG (GNUNET_ERROR_TYPE_DEBUG,
975 "Sending `%s' message for name `%s' with size %u and %u records\n",
976 "NAMESTORE_RECORD_STORE", label, msg_size,
978 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
985 * Look for an existing PKEY delegation record for a given public key.
986 * Returns at most one result to the processor.
988 * @param h handle to the namestore
989 * @param zone public key of the zone to look up in, never NULL
990 * @param value_zone public key of the target zone (value), never NULL
991 * @param proc function to call on the matching records, or with
992 * NULL (rd_count == 0) if there are no matching records
993 * @param proc_cls closure for @a proc
994 * @return a handle that can be used to
997 struct GNUNET_NAMESTORE_QueueEntry *
998 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
999 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1000 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1001 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1003 struct GNUNET_NAMESTORE_QueueEntry *qe;
1004 struct PendingMessage *pe;
1005 struct ZoneToNameMessage * msg;
1009 GNUNET_assert (NULL != h);
1010 GNUNET_assert (NULL != zone);
1011 GNUNET_assert (NULL != value_zone);
1013 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1016 qe->proc_cls = proc_cls;
1018 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1020 msg_size = sizeof (struct ZoneToNameMessage);
1021 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1022 pe->size = msg_size;
1023 msg = (struct ZoneToNameMessage *) &pe[1];
1024 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1025 msg->gns_header.header.size = htons (msg_size);
1026 msg->gns_header.r_id = htonl (rid);
1028 msg->value_zone = *value_zone;
1030 /* transmit message */
1031 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1038 * Starts a new zone iteration (used to periodically PUT all of our
1039 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1040 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1041 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1042 * immediately, and then again after
1043 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1045 * @param h handle to the namestore
1046 * @param zone zone to access, NULL for all zones
1047 * @param proc function to call on each name from the zone; it
1048 * will be called repeatedly with a value (if available)
1049 * and always once at the end with a name of NULL.
1050 * @param proc_cls closure for @a proc
1051 * @return an iterator handle to use for iteration
1053 struct GNUNET_NAMESTORE_ZoneIterator *
1054 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1055 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1056 GNUNET_NAMESTORE_RecordMonitor proc,
1059 struct GNUNET_NAMESTORE_ZoneIterator *it;
1060 struct PendingMessage *pe;
1061 struct ZoneIterationStartMessage * msg;
1065 GNUNET_assert (NULL != h);
1067 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1070 it->proc_cls = proc_cls;
1074 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1076 msg_size = sizeof (struct ZoneIterationStartMessage);
1077 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1078 pe->size = msg_size;
1079 msg = (struct ZoneIterationStartMessage *) &pe[1];
1080 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1081 msg->gns_header.header.size = htons (msg_size);
1082 msg->gns_header.r_id = htonl (rid);
1085 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1092 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1093 * for the next record.
1095 * @param it the iterator
1098 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1100 struct GNUNET_NAMESTORE_Handle *h;
1101 struct ZoneIterationNextMessage * msg;
1102 struct PendingMessage *pe;
1105 GNUNET_assert (NULL != it);
1107 msg_size = sizeof (struct ZoneIterationNextMessage);
1108 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1109 pe->size = msg_size;
1110 msg = (struct ZoneIterationNextMessage *) &pe[1];
1111 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1112 msg->gns_header.header.size = htons (msg_size);
1113 msg->gns_header.r_id = htonl (it->op_id);
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "Sending `%s' message\n",
1116 "ZONE_ITERATION_NEXT");
1117 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1123 * Stops iteration and releases the namestore handle for further calls.
1125 * @param it the iterator
1128 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1130 struct GNUNET_NAMESTORE_Handle *h;
1131 struct PendingMessage *pe;
1133 struct ZoneIterationStopMessage * msg;
1135 GNUNET_assert (NULL != it);
1137 GNUNET_CONTAINER_DLL_remove (h->z_head,
1140 msg_size = sizeof (struct ZoneIterationStopMessage);
1141 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1142 pe->size = msg_size;
1143 msg = (struct ZoneIterationStopMessage *) &pe[1];
1144 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1145 msg->gns_header.header.size = htons (msg_size);
1146 msg->gns_header.r_id = htonl (it->op_id);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "Sending `%s' message\n",
1149 "ZONE_ITERATION_STOP");
1150 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1157 * Cancel a namestore operation. The final callback from the
1158 * operation must not have been done yet.
1160 * @param qe operation to cancel
1163 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1165 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1167 GNUNET_assert (NULL != qe);
1168 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1173 /* end of namestore_api.c */