2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_gns_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "namestore.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
45 * An QueueEntry used to store information for a pending
46 * NAMESTORE record operation
48 struct GNUNET_NAMESTORE_QueueEntry
54 struct GNUNET_NAMESTORE_QueueEntry *next;
59 struct GNUNET_NAMESTORE_QueueEntry *prev;
62 * Main handle to access the namestore.
64 struct GNUNET_NAMESTORE_Handle *h;
67 * Continuation to call
69 GNUNET_NAMESTORE_ContinuationWithStatus cont;
72 * Closure for @e cont.
77 * Function to call with the records we get back; or NULL.
79 GNUNET_NAMESTORE_RecordMonitor proc;
82 * Closure for @e proc.
87 * Function to call on errors.
89 GNUNET_SCHEDULER_TaskCallback error_cb;
92 * Closure for @e error_cb.
97 * Envelope of the message to send to the service, if not yet
100 struct GNUNET_MQ_Envelope *env;
103 * The operation id this zone iteration operation has
111 * Handle for a zone iterator operation
113 struct GNUNET_NAMESTORE_ZoneIterator
119 struct GNUNET_NAMESTORE_ZoneIterator *next;
124 struct GNUNET_NAMESTORE_ZoneIterator *prev;
127 * Main handle to access the namestore.
129 struct GNUNET_NAMESTORE_Handle *h;
132 * Function to call on completion.
134 GNUNET_SCHEDULER_TaskCallback finish_cb;
137 * Closure for @e error_cb.
142 * The continuation to call with the results
144 GNUNET_NAMESTORE_RecordMonitor proc;
147 * Closure for @e proc.
152 * Function to call on errors.
154 GNUNET_SCHEDULER_TaskCallback error_cb;
157 * Closure for @e error_cb.
162 * Envelope of the message to send to the service, if not yet
165 struct GNUNET_MQ_Envelope *env;
168 * Private key of the zone.
170 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
173 * The operation id this zone iteration operation has
181 * Connection to the NAMESTORE service.
183 struct GNUNET_NAMESTORE_Handle
187 * Configuration to use.
189 const struct GNUNET_CONFIGURATION_Handle *cfg;
192 * Connection to the service (if available).
194 struct GNUNET_MQ_Handle *mq;
197 * Head of pending namestore queue entries
199 struct GNUNET_NAMESTORE_QueueEntry *op_head;
202 * Tail of pending namestore queue entries
204 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
207 * Head of pending namestore zone iterator entries
209 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
212 * Tail of pending namestore zone iterator entries
214 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
219 struct GNUNET_SCHEDULER_Task *reconnect_task;
222 * Delay introduced before we reconnect.
224 struct GNUNET_TIME_Relative reconnect_delay;
227 * Should we reconnect to service due to some serious error?
232 * The last operation id used for a NAMESTORE operation
234 uint32_t last_op_id_used;
240 * Disconnect from service and then reconnect.
242 * @param h our handle
245 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
249 * Find the queue entry that matches the @a rid
251 * @param h namestore handle
252 * @param rid id to look up
253 * @return NULL if @a rid was not found
255 static struct GNUNET_NAMESTORE_QueueEntry *
256 find_qe (struct GNUNET_NAMESTORE_Handle *h,
259 struct GNUNET_NAMESTORE_QueueEntry *qe;
261 for (qe = h->op_head; qe != NULL; qe = qe->next)
262 if (qe->op_id == rid)
269 * Find the zone iteration entry that matches the @a rid
271 * @param h namestore handle
272 * @param rid id to look up
273 * @return NULL if @a rid was not found
275 static struct GNUNET_NAMESTORE_ZoneIterator *
276 find_zi (struct GNUNET_NAMESTORE_Handle *h,
279 struct GNUNET_NAMESTORE_ZoneIterator *ze;
281 for (ze = h->z_head; ze != NULL; ze = ze->next)
282 if (ze->op_id == rid)
291 * @param qe entry to free
294 free_qe (struct GNUNET_NAMESTORE_QueueEntry *qe)
296 struct GNUNET_NAMESTORE_Handle *h = qe->h;
298 GNUNET_CONTAINER_DLL_remove (h->op_head,
302 GNUNET_MQ_discard (qe->env);
310 * @param ze entry to free
313 free_ze (struct GNUNET_NAMESTORE_ZoneIterator *ze)
315 struct GNUNET_NAMESTORE_Handle *h = ze->h;
317 GNUNET_CONTAINER_DLL_remove (h->z_head,
321 GNUNET_MQ_discard (ze->env);
327 * Check that @a rd_buf of lenght @a rd_len contains
328 * @a rd_count records.
330 * @param rd_len length of @a rd_buf
331 * @param rd_buf buffer with serialized records
332 * @param rd_count number of records expected
333 * @return #GNUNET_OK if @a rd_buf is well-formed
336 check_rd (size_t rd_len,
338 unsigned int rd_count)
340 struct GNUNET_GNSRECORD_Data rd[rd_count];
343 GNUNET_GNSRECORD_records_deserialize (rd_len,
349 return GNUNET_SYSERR;
356 * Handle an incoming message of type
357 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
360 * @param msg the message we received
363 handle_record_store_response (void *cls,
364 const struct RecordStoreResponseMessage *msg)
366 struct GNUNET_NAMESTORE_Handle *h = cls;
367 struct GNUNET_NAMESTORE_QueueEntry *qe;
372 ntohl (msg->gns_header.r_id));
373 res = ntohl (msg->op_result);
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Received RECORD_STORE_RESPONSE with result %d\n",
377 /* TODO: add actual error message from namestore to response... */
378 if (GNUNET_SYSERR == res)
379 emsg = _("Namestore failed to store record\n");
382 if (NULL != qe->cont)
383 qe->cont (qe->cont_cls,
391 * Check validity of an incoming message of type
392 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
395 * @param msg the message we received
396 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
399 check_lookup_result (void *cls,
400 const struct LabelLookupResponseMessage *msg)
409 rd_len = ntohs (msg->rd_len);
410 msg_len = ntohs (msg->gns_header.header.size);
411 name_len = ntohs (msg->name_len);
412 exp_msg_len = sizeof (*msg) + name_len + rd_len;
413 if (msg_len != exp_msg_len)
416 return GNUNET_SYSERR;
418 name = (const char *) &msg[1];
419 if ( (name_len > 0) &&
420 ('\0' != name[name_len -1]) )
423 return GNUNET_SYSERR;
425 if (GNUNET_NO == ntohs (msg->found))
427 if (0 != ntohs (msg->rd_count))
430 return GNUNET_SYSERR;
434 return check_rd (rd_len,
436 ntohs (msg->rd_count));
441 * Handle an incoming message of type
442 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
445 * @param msg the message we received
448 handle_lookup_result (void *cls,
449 const struct LabelLookupResponseMessage *msg)
451 struct GNUNET_NAMESTORE_Handle *h = cls;
452 struct GNUNET_NAMESTORE_QueueEntry *qe;
457 unsigned int rd_count;
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Received RECORD_LOOKUP_RESULT\n");
462 ntohl (msg->gns_header.r_id));
465 rd_len = ntohs (msg->rd_len);
466 rd_count = ntohs (msg->rd_count);
467 name_len = ntohs (msg->name_len);
468 name = (const char *) &msg[1];
469 if (GNUNET_NO == ntohs (msg->found))
471 /* label was not in namestore */
472 if (NULL != qe->proc)
473 qe->proc (qe->proc_cls,
482 rd_tmp = &name[name_len];
484 struct GNUNET_GNSRECORD_Data rd[rd_count];
486 GNUNET_assert (GNUNET_OK ==
487 GNUNET_GNSRECORD_records_deserialize (rd_len,
493 if (NULL != qe->proc)
494 qe->proc (qe->proc_cls,
498 (rd_count > 0) ? rd : NULL);
505 * Handle an incoming message of type
506 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
509 * @param msg the message we received
510 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
513 check_record_result (void *cls,
514 const struct RecordResultMessage *msg)
522 rd_len = ntohs (msg->rd_len);
523 msg_len = ntohs (msg->gns_header.header.size);
524 name_len = ntohs (msg->name_len);
525 if (0 != ntohs (msg->reserved))
528 return GNUNET_SYSERR;
530 if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
533 return GNUNET_SYSERR;
535 name = (const char *) &msg[1];
536 if ( (name_len > 0) &&
537 ('\0' != name[name_len -1]) )
540 return GNUNET_SYSERR;
542 return check_rd (rd_len,
544 ntohs (msg->rd_count));
549 * Handle an incoming message of type
550 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
553 * @param msg the message we received
556 handle_record_result (void *cls,
557 const struct RecordResultMessage *msg)
559 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
560 struct GNUNET_NAMESTORE_Handle *h = cls;
561 struct GNUNET_NAMESTORE_QueueEntry *qe;
562 struct GNUNET_NAMESTORE_ZoneIterator *ze;
567 unsigned int rd_count;
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 "Received RECORD_RESULT\n");
571 rd_len = ntohs (msg->rd_len);
572 rd_count = ntohs (msg->rd_count);
573 name_len = ntohs (msg->name_len);
575 ntohl (msg->gns_header.r_id));
577 ntohl (msg->gns_header.r_id));
580 return; /* rid not found */
584 GNUNET_break (0); /* rid ambigous */
588 if ( (0 == name_len) &&
589 (0 == (memcmp (&msg->private_key,
591 sizeof (priv_dummy)))) )
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Zone iteration completed!\n");
601 if (NULL != ze->finish_cb)
602 ze->finish_cb (ze->finish_cb_cls);
607 name = (const char *) &msg[1];
608 rd_tmp = &name[name_len];
610 struct GNUNET_GNSRECORD_Data rd[rd_count];
612 GNUNET_assert (GNUNET_OK ==
613 GNUNET_GNSRECORD_records_deserialize(rd_len,
621 if (NULL != qe->proc)
622 qe->proc (qe->proc_cls,
626 (rd_count > 0) ? rd : NULL);
632 if (NULL != ze->proc)
633 ze->proc (ze->proc_cls,
646 * Handle an incoming message of type
647 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
649 * @param qe the respective entry in the message queue
650 * @param msg the message we received
651 * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
654 check_zone_to_name_response (void *cls,
655 const struct ZoneToNameResponseMessage *msg)
659 const char *name_tmp;
662 if (GNUNET_OK != ntohs (msg->res))
664 name_len = ntohs (msg->name_len);
665 rd_ser_len = ntohs (msg->rd_len);
666 if (ntohs (msg->gns_header.header.size) !=
667 sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
670 return GNUNET_SYSERR;
672 name_tmp = (const char *) &msg[1];
673 if ( (name_len > 0) &&
674 ('\0' != name_tmp[name_len -1]) )
677 return GNUNET_SYSERR;
679 return check_rd (rd_ser_len,
681 ntohs (msg->rd_count));
686 * Handle an incoming message of type
687 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
690 * @param msg the message we received
693 handle_zone_to_name_response (void *cls,
694 const struct ZoneToNameResponseMessage *msg)
696 struct GNUNET_NAMESTORE_Handle *h = cls;
697 struct GNUNET_NAMESTORE_QueueEntry *qe;
701 unsigned int rd_count;
702 const char *name_tmp;
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Received ZONE_TO_NAME_RESPONSE\n");
708 ntohl (msg->gns_header.r_id));
709 res = ntohs (msg->res);
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "An error occured during zone to name operation\n");
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Namestore has no result for zone to name mapping \n");
719 if (NULL != qe->proc)
720 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
724 LOG (GNUNET_ERROR_TYPE_DEBUG,
725 "Namestore has result for zone to name mapping \n");
726 name_len = ntohs (msg->name_len);
727 rd_count = ntohs (msg->rd_count);
728 rd_ser_len = ntohs (msg->rd_len);
729 name_tmp = (const char *) &msg[1];
730 rd_tmp = &name_tmp[name_len];
732 struct GNUNET_GNSRECORD_Data rd[rd_count];
734 GNUNET_assert (GNUNET_OK ==
735 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
739 /* normal end, call continuation with result */
740 if (NULL != qe->proc)
741 qe->proc (qe->proc_cls,
746 /* return is important here: break would call continuation with error! */
755 /* error case, call continuation with error */
756 if (NULL != qe->error_cb)
757 qe->error_cb (qe->error_cb_cls);
764 * Generic error handler, called with the appropriate error code and
765 * the same closure specified at the creation of the message queue.
766 * Not every message queue implementation supports an error handler.
768 * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
769 * @param error error code
772 mq_error_handler (void *cls,
773 enum GNUNET_MQ_Error error)
775 struct GNUNET_NAMESTORE_Handle *h = cls;
783 * Reconnect to namestore service.
785 * @param h the handle to the NAMESTORE service
788 reconnect (struct GNUNET_NAMESTORE_Handle *h)
790 struct GNUNET_MQ_MessageHandler handlers[] = {
791 GNUNET_MQ_hd_fixed_size (record_store_response,
792 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
793 struct RecordStoreResponseMessage,
795 GNUNET_MQ_hd_var_size (zone_to_name_response,
796 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
797 struct ZoneToNameResponseMessage,
799 GNUNET_MQ_hd_var_size (record_result,
800 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
801 struct RecordResultMessage,
803 GNUNET_MQ_hd_var_size (lookup_result,
804 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
805 struct LabelLookupResponseMessage,
807 GNUNET_MQ_handler_end ()
809 struct GNUNET_NAMESTORE_ZoneIterator *it;
810 struct GNUNET_NAMESTORE_QueueEntry *qe;
812 GNUNET_assert (NULL == h->mq);
813 h->mq = GNUNET_CLIENT_connect (h->cfg,
820 /* re-transmit pending requests that waited for a reconnect... */
821 for (it = h->z_head; NULL != it; it = it->next)
823 GNUNET_MQ_send (h->mq,
827 for (qe = h->op_head; NULL != qe; qe = qe->next)
829 GNUNET_MQ_send (h->mq,
837 * Re-establish the connection to the service.
839 * @param cls handle to use to re-connect.
842 reconnect_task (void *cls)
844 struct GNUNET_NAMESTORE_Handle *h = cls;
846 h->reconnect_task = NULL;
852 * Disconnect from service and then reconnect.
854 * @param h our handle
857 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
859 struct GNUNET_NAMESTORE_ZoneIterator *ze;
860 struct GNUNET_NAMESTORE_QueueEntry *qe;
862 GNUNET_MQ_destroy (h->mq);
864 while (NULL != (ze = h->z_head))
866 if (NULL != ze->error_cb)
867 ze->error_cb (ze->error_cb_cls);
870 while (NULL != (qe = h->op_head))
872 if (NULL != qe->error_cb)
873 qe->error_cb (qe->error_cb_cls);
874 if (NULL != qe->cont)
875 qe->cont (qe->cont_cls,
877 "failure in communication with namestore service");
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Reconnecting to namestore\n");
883 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
884 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
891 * Get a fresh operation id to distinguish between namestore requests
893 * @param h the namestore handle
894 * @return next operation id to use
897 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
899 return h->last_op_id_used++;
904 * Initialize the connection with the NAMESTORE service.
906 * @param cfg configuration to use
907 * @return handle to the GNS service, or NULL on error
909 struct GNUNET_NAMESTORE_Handle *
910 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
912 struct GNUNET_NAMESTORE_Handle *h;
914 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
927 * Disconnect from the namestore service (and free associated
930 * @param h handle to the namestore
933 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
935 struct GNUNET_NAMESTORE_QueueEntry *q;
936 struct GNUNET_NAMESTORE_ZoneIterator *z;
938 LOG (GNUNET_ERROR_TYPE_DEBUG,
940 GNUNET_break (NULL == h->op_head);
941 while (NULL != (q = h->op_head))
943 GNUNET_CONTAINER_DLL_remove (h->op_head,
948 GNUNET_break (NULL == h->z_head);
949 while (NULL != (z = h->z_head))
951 GNUNET_CONTAINER_DLL_remove (h->z_head,
958 GNUNET_MQ_destroy (h->mq);
961 if (NULL != h->reconnect_task)
963 GNUNET_SCHEDULER_cancel (h->reconnect_task);
964 h->reconnect_task = NULL;
971 * Store an item in the namestore. If the item is already present,
972 * it is replaced with the new record. Use an empty array to
973 * remove all records under the given name.
975 * @param h handle to the namestore
976 * @param pkey private key of the zone
977 * @param label name that is being mapped (at most 255 characters long)
978 * @param rd_count number of records in the @a rd array
979 * @param rd array of records with data to store
980 * @param cont continuation to call when done
981 * @param cont_cls closure for @a cont
982 * @return handle to abort the request
984 struct GNUNET_NAMESTORE_QueueEntry *
985 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
986 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
988 unsigned int rd_count,
989 const struct GNUNET_GNSRECORD_Data *rd,
990 GNUNET_NAMESTORE_ContinuationWithStatus cont,
993 struct GNUNET_NAMESTORE_QueueEntry *qe;
994 struct GNUNET_MQ_Envelope *env;
1000 struct RecordStoreMessage *msg;
1003 name_len = strlen (label) + 1;
1004 if (name_len > MAX_NAME_LEN)
1009 rid = get_op_id (h);
1010 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1013 qe->cont_cls = cont_cls;
1015 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1020 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1022 env = GNUNET_MQ_msg_extra (msg,
1023 name_len + rd_ser_len,
1024 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1025 msg->gns_header.r_id = htonl (rid);
1026 msg->name_len = htons (name_len);
1027 msg->rd_count = htons (rd_count);
1028 msg->rd_len = htons (rd_ser_len);
1029 msg->reserved = htons (0);
1030 msg->private_key = *pkey;
1032 name_tmp = (char *) &msg[1];
1033 GNUNET_memcpy (name_tmp,
1036 rd_ser = &name_tmp[name_len];
1037 sret = GNUNET_GNSRECORD_records_serialize (rd_count,
1046 GNUNET_assert (rd_ser_len == (size_t) sret);
1047 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1055 GNUNET_MQ_send (h->mq,
1062 * Set the desired nick name for a zone
1064 * @param h handle to the namestore
1065 * @param pkey private key of the zone
1066 * @param nick the nick name to set
1067 * @param cont continuation to call when done
1068 * @param cont_cls closure for @a cont
1069 * @return handle to abort the request
1071 struct GNUNET_NAMESTORE_QueueEntry *
1072 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1073 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1075 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1078 struct GNUNET_GNSRECORD_Data rd;
1082 memset (&rd, 0, sizeof (rd));
1084 rd.data_size = strlen (nick) +1;
1085 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1086 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1087 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1088 return GNUNET_NAMESTORE_records_store (h,
1090 GNUNET_GNS_EMPTY_LABEL_AT,
1099 * Lookup an item in the namestore.
1101 * @param h handle to the namestore
1102 * @param pkey private key of the zone
1103 * @param label name that is being mapped (at most 255 characters long)
1104 * @param error_cb function to call on error (i.e. disconnect)
1105 * @param error_cb_cls closure for @a error_cb
1106 * @param rm function to call with the result (with 0 records if we don't have that label)
1107 * @param rm_cls closure for @a rm
1108 * @return handle to abort the request
1110 struct GNUNET_NAMESTORE_QueueEntry *
1111 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1112 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1114 GNUNET_SCHEDULER_TaskCallback error_cb,
1116 GNUNET_NAMESTORE_RecordMonitor rm,
1119 struct GNUNET_NAMESTORE_QueueEntry *qe;
1120 struct GNUNET_MQ_Envelope *env;
1121 struct LabelLookupMessage *msg;
1124 if (1 == (label_len = strlen (label) + 1))
1130 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1132 qe->error_cb = error_cb;
1133 qe->error_cb_cls = error_cb_cls;
1135 qe->proc_cls = rm_cls;
1136 qe->op_id = get_op_id(h);
1137 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1141 env = GNUNET_MQ_msg_extra (msg,
1143 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1144 msg->gns_header.r_id = htonl (qe->op_id);
1146 msg->label_len = htonl (label_len);
1147 GNUNET_memcpy (&msg[1],
1153 GNUNET_MQ_send (h->mq,
1160 * Look for an existing PKEY delegation record for a given public key.
1161 * Returns at most one result to the processor.
1163 * @param h handle to the namestore
1164 * @param zone public key of the zone to look up in, never NULL
1165 * @param value_zone public key of the target zone (value), never NULL
1166 * @param error_cb function to call on error (i.e. disconnect)
1167 * @param error_cb_cls closure for @a error_cb
1168 * @param proc function to call on the matching records, or with
1169 * NULL (rd_count == 0) if there are no matching records
1170 * @param proc_cls closure for @a proc
1171 * @return a handle that can be used to
1174 struct GNUNET_NAMESTORE_QueueEntry *
1175 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1176 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1177 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1178 GNUNET_SCHEDULER_TaskCallback error_cb,
1180 GNUNET_NAMESTORE_RecordMonitor proc,
1183 struct GNUNET_NAMESTORE_QueueEntry *qe;
1184 struct GNUNET_MQ_Envelope *env;
1185 struct ZoneToNameMessage *msg;
1189 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1191 qe->error_cb = error_cb;
1192 qe->error_cb_cls = error_cb_cls;
1194 qe->proc_cls = proc_cls;
1196 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1200 env = GNUNET_MQ_msg (msg,
1201 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1202 msg->gns_header.r_id = htonl (rid);
1204 msg->value_zone = *value_zone;
1208 GNUNET_MQ_send (h->mq,
1215 * Starts a new zone iteration (used to periodically PUT all of our
1216 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1217 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1218 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1219 * immediately, and then again after
1220 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1222 * @param h handle to the namestore
1223 * @param zone zone to access, NULL for all zones
1224 * @param error_cb function to call on error (i.e. disconnect)
1225 * @param error_cb_cls closure for @a error_cb
1226 * @param proc function to call on each name from the zone; it
1227 * will be called repeatedly with a value (if available)
1228 * @param proc_cls closure for @a proc
1229 * @param finish_cb function to call on completion
1230 * @param finish_cb_cls closure for @a finish_cb
1231 * @return an iterator handle to use for iteration
1233 struct GNUNET_NAMESTORE_ZoneIterator *
1234 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1235 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1236 GNUNET_SCHEDULER_TaskCallback error_cb,
1238 GNUNET_NAMESTORE_RecordMonitor proc,
1240 GNUNET_SCHEDULER_TaskCallback finish_cb,
1241 void *finish_cb_cls)
1243 struct GNUNET_NAMESTORE_ZoneIterator *it;
1244 struct GNUNET_MQ_Envelope *env;
1245 struct ZoneIterationStartMessage *msg;
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Sending ZONE_ITERATION_START message\n");
1250 rid = get_op_id (h);
1251 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1253 it->error_cb = error_cb;
1254 it->error_cb_cls = error_cb_cls;
1255 it->finish_cb = finish_cb;
1256 it->finish_cb_cls = finish_cb_cls;
1258 it->proc_cls = proc_cls;
1262 GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
1265 env = GNUNET_MQ_msg (msg,
1266 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1267 msg->gns_header.r_id = htonl (rid);
1273 GNUNET_MQ_send (h->mq,
1280 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1281 * for the next record.
1283 * @param it the iterator
1284 * @param limit number of records to return to the iterator in one shot
1285 * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
1288 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
1291 struct GNUNET_NAMESTORE_Handle *h = it->h;
1292 struct ZoneIterationNextMessage *msg;
1293 struct GNUNET_MQ_Envelope *env;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
1297 (unsigned long long) limit);
1298 env = GNUNET_MQ_msg (msg,
1299 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1300 msg->gns_header.r_id = htonl (it->op_id);
1301 msg->limit = GNUNET_htonll (limit);
1302 GNUNET_MQ_send (h->mq,
1308 * Stops iteration and releases the namestore handle for further calls.
1310 * @param it the iterator
1313 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1315 struct GNUNET_NAMESTORE_Handle *h = it->h;
1316 struct GNUNET_MQ_Envelope *env;
1317 struct ZoneIterationStopMessage *msg;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Sending ZONE_ITERATION_STOP message\n");
1323 env = GNUNET_MQ_msg (msg,
1324 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1325 msg->gns_header.r_id = htonl (it->op_id);
1326 GNUNET_MQ_send (h->mq,
1334 * Cancel a namestore operation. The final callback from the
1335 * operation must not have been done yet.
1337 * @param qe operation to cancel
1340 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1346 /* end of namestore_api.c */