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)
408 rd_len = ntohs (msg->rd_len);
409 msg_len = ntohs (msg->gns_header.header.size);
410 name_len = ntohs (msg->name_len);
411 exp_msg_len = sizeof (*msg) + name_len + rd_len;
412 if (msg_len != exp_msg_len)
415 return GNUNET_SYSERR;
417 name = (const char *) &msg[1];
418 if ( (name_len > 0) &&
419 ('\0' != name[name_len -1]) )
422 return GNUNET_SYSERR;
424 if (GNUNET_NO == ntohs (msg->found))
426 if (0 != ntohs (msg->rd_count))
429 return GNUNET_SYSERR;
433 return check_rd (rd_len,
435 ntohs (msg->rd_count));
440 * Handle an incoming message of type
441 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
444 * @param msg the message we received
447 handle_lookup_result (void *cls,
448 const struct LabelLookupResponseMessage *msg)
450 struct GNUNET_NAMESTORE_Handle *h = cls;
451 struct GNUNET_NAMESTORE_QueueEntry *qe;
456 unsigned int rd_count;
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "Received RECORD_LOOKUP_RESULT\n");
461 ntohl (msg->gns_header.r_id));
464 rd_len = ntohs (msg->rd_len);
465 rd_count = ntohs (msg->rd_count);
466 name_len = ntohs (msg->name_len);
467 name = (const char *) &msg[1];
468 if (GNUNET_NO == ntohs (msg->found))
470 /* label was not in namestore */
471 if (NULL != qe->proc)
472 qe->proc (qe->proc_cls,
481 rd_tmp = &name[name_len];
483 struct GNUNET_GNSRECORD_Data rd[rd_count];
485 GNUNET_assert (GNUNET_OK ==
486 GNUNET_GNSRECORD_records_deserialize (rd_len,
492 if (NULL != qe->proc)
493 qe->proc (qe->proc_cls,
497 (rd_count > 0) ? rd : NULL);
504 * Handle an incoming message of type
505 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
508 * @param msg the message we received
509 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
512 check_record_result (void *cls,
513 const struct RecordResultMessage *msg)
520 rd_len = ntohs (msg->rd_len);
521 msg_len = ntohs (msg->gns_header.header.size);
522 name_len = ntohs (msg->name_len);
523 if (0 != ntohs (msg->reserved))
526 return GNUNET_SYSERR;
528 if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
531 return GNUNET_SYSERR;
533 name = (const char *) &msg[1];
534 if ( (name_len > 0) &&
535 ('\0' != name[name_len -1]) )
538 return GNUNET_SYSERR;
540 return check_rd (rd_len,
542 ntohs (msg->rd_count));
547 * Handle an incoming message of type
548 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
551 * @param msg the message we received
554 handle_record_result (void *cls,
555 const struct RecordResultMessage *msg)
557 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
558 struct GNUNET_NAMESTORE_Handle *h = cls;
559 struct GNUNET_NAMESTORE_QueueEntry *qe;
560 struct GNUNET_NAMESTORE_ZoneIterator *ze;
565 unsigned int rd_count;
567 LOG (GNUNET_ERROR_TYPE_DEBUG,
568 "Received RECORD_RESULT\n");
569 rd_len = ntohs (msg->rd_len);
570 rd_count = ntohs (msg->rd_count);
571 name_len = ntohs (msg->name_len);
573 ntohl (msg->gns_header.r_id));
575 ntohl (msg->gns_header.r_id));
578 return; /* rid not found */
582 GNUNET_break (0); /* rid ambigous */
586 if ( (0 == name_len) &&
587 (0 == (memcmp (&msg->private_key,
589 sizeof (priv_dummy)))) )
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "Zone iteration completed!\n");
599 if (NULL != ze->finish_cb)
600 ze->finish_cb (ze->finish_cb_cls);
605 name = (const char *) &msg[1];
606 rd_tmp = &name[name_len];
608 struct GNUNET_GNSRECORD_Data rd[rd_count];
610 GNUNET_assert (GNUNET_OK ==
611 GNUNET_GNSRECORD_records_deserialize(rd_len,
619 if (NULL != qe->proc)
620 qe->proc (qe->proc_cls,
624 (rd_count > 0) ? rd : NULL);
630 if (NULL != ze->proc)
631 ze->proc (ze->proc_cls,
644 * Handle an incoming message of type
645 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
647 * @param qe the respective entry in the message queue
648 * @param msg the message we received
649 * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
652 check_zone_to_name_response (void *cls,
653 const struct ZoneToNameResponseMessage *msg)
657 const char *name_tmp;
659 if (GNUNET_OK != ntohs (msg->res))
661 name_len = ntohs (msg->name_len);
662 rd_ser_len = ntohs (msg->rd_len);
663 if (ntohs (msg->gns_header.header.size) !=
664 sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
667 return GNUNET_SYSERR;
669 name_tmp = (const char *) &msg[1];
670 if ( (name_len > 0) &&
671 ('\0' != name_tmp[name_len -1]) )
674 return GNUNET_SYSERR;
676 return check_rd (rd_ser_len,
678 ntohs (msg->rd_count));
683 * Handle an incoming message of type
684 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
687 * @param msg the message we received
690 handle_zone_to_name_response (void *cls,
691 const struct ZoneToNameResponseMessage *msg)
693 struct GNUNET_NAMESTORE_Handle *h = cls;
694 struct GNUNET_NAMESTORE_QueueEntry *qe;
698 unsigned int rd_count;
699 const char *name_tmp;
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Received ZONE_TO_NAME_RESPONSE\n");
705 ntohl (msg->gns_header.r_id));
706 res = ntohs (msg->res);
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "An error occured during zone to name operation\n");
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Namestore has no result for zone to name mapping \n");
716 if (NULL != qe->proc)
717 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Namestore has result for zone to name mapping \n");
723 name_len = ntohs (msg->name_len);
724 rd_count = ntohs (msg->rd_count);
725 rd_ser_len = ntohs (msg->rd_len);
726 name_tmp = (const char *) &msg[1];
727 rd_tmp = &name_tmp[name_len];
729 struct GNUNET_GNSRECORD_Data rd[rd_count];
731 GNUNET_assert (GNUNET_OK ==
732 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
736 /* normal end, call continuation with result */
737 if (NULL != qe->proc)
738 qe->proc (qe->proc_cls,
743 /* return is important here: break would call continuation with error! */
752 /* error case, call continuation with error */
753 if (NULL != qe->error_cb)
754 qe->error_cb (qe->error_cb_cls);
761 * Generic error handler, called with the appropriate error code and
762 * the same closure specified at the creation of the message queue.
763 * Not every message queue implementation supports an error handler.
765 * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
766 * @param error error code
769 mq_error_handler (void *cls,
770 enum GNUNET_MQ_Error error)
772 struct GNUNET_NAMESTORE_Handle *h = cls;
779 * Reconnect to namestore service.
781 * @param h the handle to the NAMESTORE service
784 reconnect (struct GNUNET_NAMESTORE_Handle *h)
786 struct GNUNET_MQ_MessageHandler handlers[] = {
787 GNUNET_MQ_hd_fixed_size (record_store_response,
788 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
789 struct RecordStoreResponseMessage,
791 GNUNET_MQ_hd_var_size (zone_to_name_response,
792 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
793 struct ZoneToNameResponseMessage,
795 GNUNET_MQ_hd_var_size (record_result,
796 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
797 struct RecordResultMessage,
799 GNUNET_MQ_hd_var_size (lookup_result,
800 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
801 struct LabelLookupResponseMessage,
803 GNUNET_MQ_handler_end ()
805 struct GNUNET_NAMESTORE_ZoneIterator *it;
806 struct GNUNET_NAMESTORE_QueueEntry *qe;
808 GNUNET_assert (NULL == h->mq);
809 h->mq = GNUNET_CLIENT_connect (h->cfg,
816 /* re-transmit pending requests that waited for a reconnect... */
817 for (it = h->z_head; NULL != it; it = it->next)
819 GNUNET_MQ_send (h->mq,
823 for (qe = h->op_head; NULL != qe; qe = qe->next)
825 GNUNET_MQ_send (h->mq,
833 * Re-establish the connection to the service.
835 * @param cls handle to use to re-connect.
838 reconnect_task (void *cls)
840 struct GNUNET_NAMESTORE_Handle *h = cls;
842 h->reconnect_task = NULL;
848 * Disconnect from service and then reconnect.
850 * @param h our handle
853 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
855 struct GNUNET_NAMESTORE_ZoneIterator *ze;
856 struct GNUNET_NAMESTORE_QueueEntry *qe;
858 GNUNET_MQ_destroy (h->mq);
860 while (NULL != (ze = h->z_head))
862 if (NULL != ze->error_cb)
863 ze->error_cb (ze->error_cb_cls);
866 while (NULL != (qe = h->op_head))
868 if (NULL != qe->error_cb)
869 qe->error_cb (qe->error_cb_cls);
870 if (NULL != qe->cont)
871 qe->cont (qe->cont_cls,
873 "failure in communication with namestore service");
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Reconnecting to namestore\n");
879 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
880 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
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);
923 * Disconnect from the namestore service (and free associated
926 * @param h handle to the namestore
929 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
931 struct GNUNET_NAMESTORE_QueueEntry *q;
932 struct GNUNET_NAMESTORE_ZoneIterator *z;
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
936 GNUNET_break (NULL == h->op_head);
937 while (NULL != (q = h->op_head))
939 GNUNET_CONTAINER_DLL_remove (h->op_head,
944 GNUNET_break (NULL == h->z_head);
945 while (NULL != (z = h->z_head))
947 GNUNET_CONTAINER_DLL_remove (h->z_head,
954 GNUNET_MQ_destroy (h->mq);
957 if (NULL != h->reconnect_task)
959 GNUNET_SCHEDULER_cancel (h->reconnect_task);
960 h->reconnect_task = NULL;
967 * Store an item in the namestore. If the item is already present,
968 * it is replaced with the new record. Use an empty array to
969 * remove all records under the given name.
971 * @param h handle to the namestore
972 * @param pkey private key of the zone
973 * @param label name that is being mapped (at most 255 characters long)
974 * @param rd_count number of records in the @a rd array
975 * @param rd array of records with data to store
976 * @param cont continuation to call when done
977 * @param cont_cls closure for @a cont
978 * @return handle to abort the request
980 struct GNUNET_NAMESTORE_QueueEntry *
981 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
982 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
984 unsigned int rd_count,
985 const struct GNUNET_GNSRECORD_Data *rd,
986 GNUNET_NAMESTORE_ContinuationWithStatus cont,
989 struct GNUNET_NAMESTORE_QueueEntry *qe;
990 struct GNUNET_MQ_Envelope *env;
996 struct RecordStoreMessage *msg;
998 name_len = strlen (label) + 1;
999 if (name_len > MAX_NAME_LEN)
1004 rid = get_op_id (h);
1005 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1008 qe->cont_cls = cont_cls;
1010 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1015 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1017 env = GNUNET_MQ_msg_extra (msg,
1018 name_len + rd_ser_len,
1019 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1020 msg->gns_header.r_id = htonl (rid);
1021 msg->name_len = htons (name_len);
1022 msg->rd_count = htons (rd_count);
1023 msg->rd_len = htons (rd_ser_len);
1024 msg->reserved = htons (0);
1025 msg->private_key = *pkey;
1027 name_tmp = (char *) &msg[1];
1028 GNUNET_memcpy (name_tmp,
1031 rd_ser = &name_tmp[name_len];
1032 GNUNET_assert (rd_ser_len ==
1033 GNUNET_GNSRECORD_records_serialize (rd_count,
1037 LOG (GNUNET_ERROR_TYPE_DEBUG,
1038 "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1045 GNUNET_MQ_send (h->mq,
1052 * Set the desired nick name for a zone
1054 * @param h handle to the namestore
1055 * @param pkey private key of the zone
1056 * @param nick the nick name to set
1057 * @param cont continuation to call when done
1058 * @param cont_cls closure for @a cont
1059 * @return handle to abort the request
1061 struct GNUNET_NAMESTORE_QueueEntry *
1062 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1063 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1065 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1068 struct GNUNET_GNSRECORD_Data rd;
1072 memset (&rd, 0, sizeof (rd));
1074 rd.data_size = strlen (nick) +1;
1075 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1076 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1077 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1078 return GNUNET_NAMESTORE_records_store (h,
1080 GNUNET_GNS_MASTERZONE_STR,
1089 * Lookup an item in the namestore.
1091 * @param h handle to the namestore
1092 * @param pkey private key of the zone
1093 * @param label name that is being mapped (at most 255 characters long)
1094 * @param error_cb function to call on error (i.e. disconnect)
1095 * @param error_cb_cls closure for @a error_cb
1096 * @param rm function to call with the result (with 0 records if we don't have that label)
1097 * @param rm_cls closure for @a rm
1098 * @return handle to abort the request
1100 struct GNUNET_NAMESTORE_QueueEntry *
1101 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1102 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1104 GNUNET_SCHEDULER_TaskCallback error_cb,
1106 GNUNET_NAMESTORE_RecordMonitor rm,
1109 struct GNUNET_NAMESTORE_QueueEntry *qe;
1110 struct GNUNET_MQ_Envelope *env;
1111 struct LabelLookupMessage *msg;
1114 if (1 == (label_len = strlen (label) + 1))
1120 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1122 qe->error_cb = error_cb;
1123 qe->error_cb_cls = error_cb_cls;
1125 qe->proc_cls = rm_cls;
1126 qe->op_id = get_op_id(h);
1127 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1131 env = GNUNET_MQ_msg_extra (msg,
1133 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1134 msg->gns_header.r_id = htonl (qe->op_id);
1136 msg->label_len = htonl (label_len);
1137 GNUNET_memcpy (&msg[1],
1143 GNUNET_MQ_send (h->mq,
1150 * Look for an existing PKEY delegation record for a given public key.
1151 * Returns at most one result to the processor.
1153 * @param h handle to the namestore
1154 * @param zone public key of the zone to look up in, never NULL
1155 * @param value_zone public key of the target zone (value), never NULL
1156 * @param error_cb function to call on error (i.e. disconnect)
1157 * @param error_cb_cls closure for @a error_cb
1158 * @param proc function to call on the matching records, or with
1159 * NULL (rd_count == 0) if there are no matching records
1160 * @param proc_cls closure for @a proc
1161 * @return a handle that can be used to
1164 struct GNUNET_NAMESTORE_QueueEntry *
1165 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1166 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1167 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1168 GNUNET_SCHEDULER_TaskCallback error_cb,
1170 GNUNET_NAMESTORE_RecordMonitor proc,
1173 struct GNUNET_NAMESTORE_QueueEntry *qe;
1174 struct GNUNET_MQ_Envelope *env;
1175 struct ZoneToNameMessage *msg;
1179 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1181 qe->error_cb = error_cb;
1182 qe->error_cb_cls = error_cb_cls;
1184 qe->proc_cls = proc_cls;
1186 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1190 env = GNUNET_MQ_msg (msg,
1191 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1192 msg->gns_header.r_id = htonl (rid);
1194 msg->value_zone = *value_zone;
1198 GNUNET_MQ_send (h->mq,
1205 * Starts a new zone iteration (used to periodically PUT all of our
1206 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1207 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1208 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1209 * immediately, and then again after
1210 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1212 * @param h handle to the namestore
1213 * @param zone zone to access, NULL for all zones
1214 * @param error_cb function to call on error (i.e. disconnect)
1215 * @param error_cb_cls closure for @a error_cb
1216 * @param proc function to call on each name from the zone; it
1217 * will be called repeatedly with a value (if available)
1218 * @param proc_cls closure for @a proc
1219 * @param finish_cb function to call on completion
1220 * @param finish_cb_cls closure for @a finish_cb
1221 * @return an iterator handle to use for iteration
1223 struct GNUNET_NAMESTORE_ZoneIterator *
1224 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1225 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1226 GNUNET_SCHEDULER_TaskCallback error_cb,
1228 GNUNET_NAMESTORE_RecordMonitor proc,
1230 GNUNET_SCHEDULER_TaskCallback finish_cb,
1231 void *finish_cb_cls)
1233 struct GNUNET_NAMESTORE_ZoneIterator *it;
1234 struct GNUNET_MQ_Envelope *env;
1235 struct ZoneIterationStartMessage *msg;
1238 LOG (GNUNET_ERROR_TYPE_DEBUG,
1239 "Sending ZONE_ITERATION_START message\n");
1240 rid = get_op_id (h);
1241 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1243 it->error_cb = error_cb;
1244 it->error_cb_cls = error_cb_cls;
1245 it->finish_cb = finish_cb;
1246 it->finish_cb_cls = finish_cb_cls;
1248 it->proc_cls = proc_cls;
1252 GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
1255 env = GNUNET_MQ_msg (msg,
1256 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1257 msg->gns_header.r_id = htonl (rid);
1263 GNUNET_MQ_send (h->mq,
1270 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1271 * for the next record.
1273 * @param it the iterator
1276 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1278 struct GNUNET_NAMESTORE_Handle *h = it->h;
1279 struct ZoneIterationNextMessage *msg;
1280 struct GNUNET_MQ_Envelope *env;
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "Sending ZONE_ITERATION_NEXT message\n");
1284 env = GNUNET_MQ_msg (msg,
1285 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1286 msg->gns_header.r_id = htonl (it->op_id);
1287 GNUNET_MQ_send (h->mq,
1293 * Stops iteration and releases the namestore handle for further calls.
1295 * @param it the iterator
1298 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1300 struct GNUNET_NAMESTORE_Handle *h = it->h;
1301 struct GNUNET_MQ_Envelope *env;
1302 struct ZoneIterationStopMessage *msg;
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Sending ZONE_ITERATION_STOP message\n");
1306 env = GNUNET_MQ_msg (msg,
1307 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1308 msg->gns_header.r_id = htonl (it->op_id);
1309 GNUNET_MQ_send (h->mq,
1316 * Cancel a namestore operation. The final callback from the
1317 * operation must not have been done yet.
1319 * @param qe operation to cancel
1322 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1328 /* end of namestore_api.c */