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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file namestore/namestore_api.c
18 * @brief API to access the NAMESTORE service
19 * @author Martin Schanzenbach
20 * @author Matthias Wachs
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_gns_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "namestore.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
39 * We grant the namestore up to 1 minute of latency, if it is slower than
40 * that, store queries will fail.
42 #define NAMESTORE_DELAY_TOLERANCE GNUNET_TIME_UNIT_MINUTES
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 * Task scheduled to warn us if the namestore is way too slow.
105 struct GNUNET_SCHEDULER_Task *timeout_task;
108 * The operation id this zone iteration operation has
116 * Handle for a zone iterator operation
118 struct GNUNET_NAMESTORE_ZoneIterator
124 struct GNUNET_NAMESTORE_ZoneIterator *next;
129 struct GNUNET_NAMESTORE_ZoneIterator *prev;
132 * Main handle to access the namestore.
134 struct GNUNET_NAMESTORE_Handle *h;
137 * Function to call on completion.
139 GNUNET_SCHEDULER_TaskCallback finish_cb;
142 * Closure for @e error_cb.
147 * The continuation to call with the results
149 GNUNET_NAMESTORE_RecordMonitor proc;
152 * Closure for @e proc.
157 * Function to call on errors.
159 GNUNET_SCHEDULER_TaskCallback error_cb;
162 * Closure for @e error_cb.
167 * Envelope of the message to send to the service, if not yet
170 struct GNUNET_MQ_Envelope *env;
173 * Private key of the zone.
175 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
178 * The operation id this zone iteration operation has
186 * Connection to the NAMESTORE service.
188 struct GNUNET_NAMESTORE_Handle
192 * Configuration to use.
194 const struct GNUNET_CONFIGURATION_Handle *cfg;
197 * Connection to the service (if available).
199 struct GNUNET_MQ_Handle *mq;
202 * Head of pending namestore queue entries
204 struct GNUNET_NAMESTORE_QueueEntry *op_head;
207 * Tail of pending namestore queue entries
209 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
212 * Head of pending namestore zone iterator entries
214 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
217 * Tail of pending namestore zone iterator entries
219 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
224 struct GNUNET_SCHEDULER_Task *reconnect_task;
227 * Delay introduced before we reconnect.
229 struct GNUNET_TIME_Relative reconnect_delay;
232 * Should we reconnect to service due to some serious error?
237 * The last operation id used for a NAMESTORE operation
239 uint32_t last_op_id_used;
245 * Disconnect from service and then reconnect.
247 * @param h our handle
250 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
254 * Find the queue entry that matches the @a rid
256 * @param h namestore handle
257 * @param rid id to look up
258 * @return NULL if @a rid was not found
260 static struct GNUNET_NAMESTORE_QueueEntry *
261 find_qe (struct GNUNET_NAMESTORE_Handle *h,
264 struct GNUNET_NAMESTORE_QueueEntry *qe;
266 for (qe = h->op_head; qe != NULL; qe = qe->next)
267 if (qe->op_id == rid)
274 * Find the zone iteration entry that matches the @a rid
276 * @param h namestore handle
277 * @param rid id to look up
278 * @return NULL if @a rid was not found
280 static struct GNUNET_NAMESTORE_ZoneIterator *
281 find_zi (struct GNUNET_NAMESTORE_Handle *h,
284 struct GNUNET_NAMESTORE_ZoneIterator *ze;
286 for (ze = h->z_head; ze != NULL; ze = ze->next)
287 if (ze->op_id == rid)
296 * @param qe entry to free
299 free_qe (struct GNUNET_NAMESTORE_QueueEntry *qe)
301 struct GNUNET_NAMESTORE_Handle *h = qe->h;
303 GNUNET_CONTAINER_DLL_remove (h->op_head,
307 GNUNET_MQ_discard (qe->env);
308 if (NULL != qe->timeout_task)
309 GNUNET_SCHEDULER_cancel (qe->timeout_task);
317 * @param ze entry to free
320 free_ze (struct GNUNET_NAMESTORE_ZoneIterator *ze)
322 struct GNUNET_NAMESTORE_Handle *h = ze->h;
324 GNUNET_CONTAINER_DLL_remove (h->z_head,
328 GNUNET_MQ_discard (ze->env);
334 * Check that @a rd_buf of lenght @a rd_len contains
335 * @a rd_count records.
337 * @param rd_len length of @a rd_buf
338 * @param rd_buf buffer with serialized records
339 * @param rd_count number of records expected
340 * @return #GNUNET_OK if @a rd_buf is well-formed
343 check_rd (size_t rd_len,
345 unsigned int rd_count)
347 struct GNUNET_GNSRECORD_Data rd[rd_count];
350 GNUNET_GNSRECORD_records_deserialize (rd_len,
356 return GNUNET_SYSERR;
363 * Handle an incoming message of type
364 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
367 * @param msg the message we received
370 handle_record_store_response (void *cls,
371 const struct RecordStoreResponseMessage *msg)
373 struct GNUNET_NAMESTORE_Handle *h = cls;
374 struct GNUNET_NAMESTORE_QueueEntry *qe;
379 ntohl (msg->gns_header.r_id));
380 res = ntohl (msg->op_result);
381 LOG (GNUNET_ERROR_TYPE_DEBUG,
382 "Received RECORD_STORE_RESPONSE with result %d\n",
384 /* TODO: add actual error message from namestore to response... */
385 if (GNUNET_SYSERR == res)
386 emsg = _("Namestore failed to store record\n");
389 if (NULL != qe->cont)
390 qe->cont (qe->cont_cls,
398 * Check validity of an incoming message of type
399 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
402 * @param msg the message we received
403 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
406 check_lookup_result (void *cls,
407 const struct LabelLookupResponseMessage *msg)
416 rd_len = ntohs (msg->rd_len);
417 msg_len = ntohs (msg->gns_header.header.size);
418 name_len = ntohs (msg->name_len);
419 exp_msg_len = sizeof (*msg) + name_len + rd_len;
420 if (msg_len != exp_msg_len)
423 return GNUNET_SYSERR;
425 name = (const char *) &msg[1];
426 if ( (name_len > 0) &&
427 ('\0' != name[name_len -1]) )
430 return GNUNET_SYSERR;
432 if (GNUNET_NO == ntohs (msg->found))
434 if (0 != ntohs (msg->rd_count))
437 return GNUNET_SYSERR;
441 return check_rd (rd_len,
443 ntohs (msg->rd_count));
448 * Handle an incoming message of type
449 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
452 * @param msg the message we received
455 handle_lookup_result (void *cls,
456 const struct LabelLookupResponseMessage *msg)
458 struct GNUNET_NAMESTORE_Handle *h = cls;
459 struct GNUNET_NAMESTORE_QueueEntry *qe;
464 unsigned int rd_count;
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 "Received RECORD_LOOKUP_RESULT\n");
469 ntohl (msg->gns_header.r_id));
472 rd_len = ntohs (msg->rd_len);
473 rd_count = ntohs (msg->rd_count);
474 name_len = ntohs (msg->name_len);
475 name = (const char *) &msg[1];
476 if (GNUNET_NO == ntohs (msg->found))
478 /* label was not in namestore */
479 if (NULL != qe->proc)
480 qe->proc (qe->proc_cls,
489 rd_tmp = &name[name_len];
491 struct GNUNET_GNSRECORD_Data rd[rd_count];
493 GNUNET_assert (GNUNET_OK ==
494 GNUNET_GNSRECORD_records_deserialize (rd_len,
500 if (NULL != qe->proc)
501 qe->proc (qe->proc_cls,
505 (rd_count > 0) ? rd : NULL);
512 * Handle an incoming message of type
513 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
516 * @param msg the message we received
517 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
520 check_record_result (void *cls,
521 const struct RecordResultMessage *msg)
529 rd_len = ntohs (msg->rd_len);
530 msg_len = ntohs (msg->gns_header.header.size);
531 name_len = ntohs (msg->name_len);
532 if (0 != ntohs (msg->reserved))
535 return GNUNET_SYSERR;
537 if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
540 return GNUNET_SYSERR;
542 name = (const char *) &msg[1];
543 if ( (name_len > 0) &&
544 ('\0' != name[name_len -1]) )
547 return GNUNET_SYSERR;
549 return check_rd (rd_len,
551 ntohs (msg->rd_count));
556 * Handle an incoming message of type
557 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
560 * @param msg the message we received
563 handle_record_result (void *cls,
564 const struct RecordResultMessage *msg)
566 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
567 struct GNUNET_NAMESTORE_Handle *h = cls;
568 struct GNUNET_NAMESTORE_QueueEntry *qe;
569 struct GNUNET_NAMESTORE_ZoneIterator *ze;
574 unsigned int rd_count;
576 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Received RECORD_RESULT\n");
578 rd_len = ntohs (msg->rd_len);
579 rd_count = ntohs (msg->rd_count);
580 name_len = ntohs (msg->name_len);
582 ntohl (msg->gns_header.r_id));
584 ntohl (msg->gns_header.r_id));
587 return; /* rid not found */
591 GNUNET_break (0); /* rid ambigous */
595 if ( (0 == name_len) &&
596 (0 == (memcmp (&msg->private_key,
598 sizeof (priv_dummy)))) )
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Zone iteration completed!\n");
608 if (NULL != ze->finish_cb)
609 ze->finish_cb (ze->finish_cb_cls);
614 name = (const char *) &msg[1];
615 rd_tmp = &name[name_len];
617 struct GNUNET_GNSRECORD_Data rd[rd_count];
619 GNUNET_assert (GNUNET_OK ==
620 GNUNET_GNSRECORD_records_deserialize(rd_len,
628 if (NULL != qe->proc)
629 qe->proc (qe->proc_cls,
633 (rd_count > 0) ? rd : NULL);
639 if (NULL != ze->proc)
640 ze->proc (ze->proc_cls,
653 * Handle an incoming message of type
654 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
656 * @param qe the respective entry in the message queue
657 * @param msg the message we received
658 * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
661 check_zone_to_name_response (void *cls,
662 const struct ZoneToNameResponseMessage *msg)
666 const char *name_tmp;
669 if (GNUNET_OK != ntohs (msg->res))
671 name_len = ntohs (msg->name_len);
672 rd_ser_len = ntohs (msg->rd_len);
673 if (ntohs (msg->gns_header.header.size) !=
674 sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
677 return GNUNET_SYSERR;
679 name_tmp = (const char *) &msg[1];
680 if ( (name_len > 0) &&
681 ('\0' != name_tmp[name_len -1]) )
684 return GNUNET_SYSERR;
686 return check_rd (rd_ser_len,
688 ntohs (msg->rd_count));
693 * Handle an incoming message of type
694 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
697 * @param msg the message we received
700 handle_zone_to_name_response (void *cls,
701 const struct ZoneToNameResponseMessage *msg)
703 struct GNUNET_NAMESTORE_Handle *h = cls;
704 struct GNUNET_NAMESTORE_QueueEntry *qe;
708 unsigned int rd_count;
709 const char *name_tmp;
712 LOG (GNUNET_ERROR_TYPE_DEBUG,
713 "Received ZONE_TO_NAME_RESPONSE\n");
715 ntohl (msg->gns_header.r_id));
716 res = ntohs (msg->res);
720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "An error occured during zone to name operation\n");
724 LOG (GNUNET_ERROR_TYPE_DEBUG,
725 "Namestore has no result for zone to name mapping \n");
726 if (NULL != qe->proc)
727 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "Namestore has result for zone to name mapping \n");
733 name_len = ntohs (msg->name_len);
734 rd_count = ntohs (msg->rd_count);
735 rd_ser_len = ntohs (msg->rd_len);
736 name_tmp = (const char *) &msg[1];
737 rd_tmp = &name_tmp[name_len];
739 struct GNUNET_GNSRECORD_Data rd[rd_count];
741 GNUNET_assert (GNUNET_OK ==
742 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
746 /* normal end, call continuation with result */
747 if (NULL != qe->proc)
748 qe->proc (qe->proc_cls,
753 /* return is important here: break would call continuation with error! */
762 /* error case, call continuation with error */
763 if (NULL != qe->error_cb)
764 qe->error_cb (qe->error_cb_cls);
771 * Generic error handler, called with the appropriate error code and
772 * the same closure specified at the creation of the message queue.
773 * Not every message queue implementation supports an error handler.
775 * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
776 * @param error error code
779 mq_error_handler (void *cls,
780 enum GNUNET_MQ_Error error)
782 struct GNUNET_NAMESTORE_Handle *h = cls;
790 * Reconnect to namestore service.
792 * @param h the handle to the NAMESTORE service
795 reconnect (struct GNUNET_NAMESTORE_Handle *h)
797 struct GNUNET_MQ_MessageHandler handlers[] = {
798 GNUNET_MQ_hd_fixed_size (record_store_response,
799 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
800 struct RecordStoreResponseMessage,
802 GNUNET_MQ_hd_var_size (zone_to_name_response,
803 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
804 struct ZoneToNameResponseMessage,
806 GNUNET_MQ_hd_var_size (record_result,
807 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
808 struct RecordResultMessage,
810 GNUNET_MQ_hd_var_size (lookup_result,
811 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
812 struct LabelLookupResponseMessage,
814 GNUNET_MQ_handler_end ()
816 struct GNUNET_NAMESTORE_ZoneIterator *it;
817 struct GNUNET_NAMESTORE_QueueEntry *qe;
819 GNUNET_assert (NULL == h->mq);
820 h->mq = GNUNET_CLIENT_connect (h->cfg,
827 /* re-transmit pending requests that waited for a reconnect... */
828 for (it = h->z_head; NULL != it; it = it->next)
830 GNUNET_MQ_send (h->mq,
834 for (qe = h->op_head; NULL != qe; qe = qe->next)
836 GNUNET_MQ_send (h->mq,
844 * Re-establish the connection to the service.
846 * @param cls handle to use to re-connect.
849 reconnect_task (void *cls)
851 struct GNUNET_NAMESTORE_Handle *h = cls;
853 h->reconnect_task = NULL;
859 * Disconnect from service and then reconnect.
861 * @param h our handle
864 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
866 struct GNUNET_NAMESTORE_ZoneIterator *ze;
867 struct GNUNET_NAMESTORE_QueueEntry *qe;
869 GNUNET_MQ_destroy (h->mq);
871 while (NULL != (ze = h->z_head))
873 if (NULL != ze->error_cb)
874 ze->error_cb (ze->error_cb_cls);
877 while (NULL != (qe = h->op_head))
879 if (NULL != qe->error_cb)
880 qe->error_cb (qe->error_cb_cls);
881 if (NULL != qe->cont)
882 qe->cont (qe->cont_cls,
884 "failure in communication with namestore service");
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Reconnecting to namestore\n");
890 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
891 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
898 * Get a fresh operation id to distinguish between namestore requests
900 * @param h the namestore handle
901 * @return next operation id to use
904 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
906 return h->last_op_id_used++;
911 * Initialize the connection with the NAMESTORE service.
913 * @param cfg configuration to use
914 * @return handle to the GNS service, or NULL on error
916 struct GNUNET_NAMESTORE_Handle *
917 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
919 struct GNUNET_NAMESTORE_Handle *h;
921 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
934 * Disconnect from the namestore service (and free associated
937 * @param h handle to the namestore
940 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
942 struct GNUNET_NAMESTORE_QueueEntry *q;
943 struct GNUNET_NAMESTORE_ZoneIterator *z;
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
947 GNUNET_break (NULL == h->op_head);
948 while (NULL != (q = h->op_head))
950 GNUNET_CONTAINER_DLL_remove (h->op_head,
955 GNUNET_break (NULL == h->z_head);
956 while (NULL != (z = h->z_head))
958 GNUNET_CONTAINER_DLL_remove (h->z_head,
965 GNUNET_MQ_destroy (h->mq);
968 if (NULL != h->reconnect_task)
970 GNUNET_SCHEDULER_cancel (h->reconnect_task);
971 h->reconnect_task = NULL;
978 * Task launched to warn the user that the namestore is
979 * excessively slow and that a query was thus dropped.
981 * @param cls a `struct GNUNET_NAMESTORE_QueueEntry *`
984 warn_delay (void *cls)
986 struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
988 qe->timeout_task = NULL;
989 LOG (GNUNET_ERROR_TYPE_WARNING,
990 "Did not receive response from namestore after %s!\n",
991 GNUNET_STRINGS_relative_time_to_string (NAMESTORE_DELAY_TOLERANCE,
993 if (NULL != qe->cont)
995 qe->cont (qe->cont_cls,
1000 GNUNET_NAMESTORE_cancel (qe);
1005 * Store an item in the namestore. If the item is already present,
1006 * it is replaced with the new record. Use an empty array to
1007 * remove all records under the given name.
1009 * @param h handle to the namestore
1010 * @param pkey private key of the zone
1011 * @param label name that is being mapped (at most 255 characters long)
1012 * @param rd_count number of records in the @a rd array
1013 * @param rd array of records with data to store
1014 * @param cont continuation to call when done
1015 * @param cont_cls closure for @a cont
1016 * @return handle to abort the request
1018 struct GNUNET_NAMESTORE_QueueEntry *
1019 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1020 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1022 unsigned int rd_count,
1023 const struct GNUNET_GNSRECORD_Data *rd,
1024 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1027 struct GNUNET_NAMESTORE_QueueEntry *qe;
1028 struct GNUNET_MQ_Envelope *env;
1034 struct RecordStoreMessage *msg;
1037 name_len = strlen (label) + 1;
1038 if (name_len > MAX_NAME_LEN)
1043 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1050 if (rd_ser_len > UINT16_MAX)
1055 rid = get_op_id (h);
1056 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1059 qe->cont_cls = cont_cls;
1061 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1066 env = GNUNET_MQ_msg_extra (msg,
1067 name_len + rd_ser_len,
1068 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1069 msg->gns_header.r_id = htonl (rid);
1070 msg->name_len = htons (name_len);
1071 msg->rd_count = htons (rd_count);
1072 msg->rd_len = htons (rd_ser_len);
1073 msg->reserved = htons (0);
1074 msg->private_key = *pkey;
1076 name_tmp = (char *) &msg[1];
1077 GNUNET_memcpy (name_tmp,
1080 rd_ser = &name_tmp[name_len];
1081 sret = GNUNET_GNSRECORD_records_serialize (rd_count,
1086 (sret != rd_ser_len) )
1092 GNUNET_assert (rd_ser_len == (size_t) sret);
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1097 qe->timeout_task = GNUNET_SCHEDULER_add_delayed (NAMESTORE_DELAY_TOLERANCE,
1103 LOG (GNUNET_ERROR_TYPE_WARNING,
1104 "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
1108 GNUNET_MQ_send (h->mq,
1116 * Set the desired nick name for a zone
1118 * @param h handle to the namestore
1119 * @param pkey private key of the zone
1120 * @param nick the nick name to set
1121 * @param cont continuation to call when done
1122 * @param cont_cls closure for @a cont
1123 * @return handle to abort the request
1125 struct GNUNET_NAMESTORE_QueueEntry *
1126 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1127 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1129 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1132 struct GNUNET_GNSRECORD_Data rd;
1136 memset (&rd, 0, sizeof (rd));
1138 rd.data_size = strlen (nick) +1;
1139 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1140 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1141 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1142 return GNUNET_NAMESTORE_records_store (h,
1144 GNUNET_GNS_EMPTY_LABEL_AT,
1153 * Lookup an item in the namestore.
1155 * @param h handle to the namestore
1156 * @param pkey private key of the zone
1157 * @param label name that is being mapped (at most 255 characters long)
1158 * @param error_cb function to call on error (i.e. disconnect)
1159 * @param error_cb_cls closure for @a error_cb
1160 * @param rm function to call with the result (with 0 records if we don't have that label)
1161 * @param rm_cls closure for @a rm
1162 * @return handle to abort the request
1164 struct GNUNET_NAMESTORE_QueueEntry *
1165 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1166 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1168 GNUNET_SCHEDULER_TaskCallback error_cb,
1170 GNUNET_NAMESTORE_RecordMonitor rm,
1173 struct GNUNET_NAMESTORE_QueueEntry *qe;
1174 struct GNUNET_MQ_Envelope *env;
1175 struct LabelLookupMessage *msg;
1178 if (1 == (label_len = strlen (label) + 1))
1184 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1186 qe->error_cb = error_cb;
1187 qe->error_cb_cls = error_cb_cls;
1189 qe->proc_cls = rm_cls;
1190 qe->op_id = get_op_id(h);
1191 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1195 env = GNUNET_MQ_msg_extra (msg,
1197 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1198 msg->gns_header.r_id = htonl (qe->op_id);
1200 msg->label_len = htonl (label_len);
1201 GNUNET_memcpy (&msg[1],
1207 GNUNET_MQ_send (h->mq,
1214 * Look for an existing PKEY delegation record for a given public key.
1215 * Returns at most one result to the processor.
1217 * @param h handle to the namestore
1218 * @param zone public key of the zone to look up in, never NULL
1219 * @param value_zone public key of the target zone (value), never NULL
1220 * @param error_cb function to call on error (i.e. disconnect)
1221 * @param error_cb_cls closure for @a error_cb
1222 * @param proc function to call on the matching records, or with
1223 * NULL (rd_count == 0) if there are no matching records
1224 * @param proc_cls closure for @a proc
1225 * @return a handle that can be used to
1228 struct GNUNET_NAMESTORE_QueueEntry *
1229 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1230 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1231 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1232 GNUNET_SCHEDULER_TaskCallback error_cb,
1234 GNUNET_NAMESTORE_RecordMonitor proc,
1237 struct GNUNET_NAMESTORE_QueueEntry *qe;
1238 struct GNUNET_MQ_Envelope *env;
1239 struct ZoneToNameMessage *msg;
1243 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1245 qe->error_cb = error_cb;
1246 qe->error_cb_cls = error_cb_cls;
1248 qe->proc_cls = proc_cls;
1250 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1254 env = GNUNET_MQ_msg (msg,
1255 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1256 msg->gns_header.r_id = htonl (rid);
1258 msg->value_zone = *value_zone;
1262 GNUNET_MQ_send (h->mq,
1269 * Starts a new zone iteration (used to periodically PUT all of our
1270 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1271 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1272 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1273 * immediately, and then again after
1274 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1276 * @param h handle to the namestore
1277 * @param zone zone to access, NULL for all zones
1278 * @param error_cb function to call on error (i.e. disconnect)
1279 * @param error_cb_cls closure for @a error_cb
1280 * @param proc function to call on each name from the zone; it
1281 * will be called repeatedly with a value (if available)
1282 * @param proc_cls closure for @a proc
1283 * @param finish_cb function to call on completion
1284 * @param finish_cb_cls closure for @a finish_cb
1285 * @return an iterator handle to use for iteration
1287 struct GNUNET_NAMESTORE_ZoneIterator *
1288 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1289 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1290 GNUNET_SCHEDULER_TaskCallback error_cb,
1292 GNUNET_NAMESTORE_RecordMonitor proc,
1294 GNUNET_SCHEDULER_TaskCallback finish_cb,
1295 void *finish_cb_cls)
1297 struct GNUNET_NAMESTORE_ZoneIterator *it;
1298 struct GNUNET_MQ_Envelope *env;
1299 struct ZoneIterationStartMessage *msg;
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "Sending ZONE_ITERATION_START message\n");
1304 rid = get_op_id (h);
1305 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1307 it->error_cb = error_cb;
1308 it->error_cb_cls = error_cb_cls;
1309 it->finish_cb = finish_cb;
1310 it->finish_cb_cls = finish_cb_cls;
1312 it->proc_cls = proc_cls;
1316 GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
1319 env = GNUNET_MQ_msg (msg,
1320 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1321 msg->gns_header.r_id = htonl (rid);
1327 GNUNET_MQ_send (h->mq,
1334 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1335 * for the next record.
1337 * @param it the iterator
1338 * @param limit number of records to return to the iterator in one shot
1339 * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
1342 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
1345 struct GNUNET_NAMESTORE_Handle *h = it->h;
1346 struct ZoneIterationNextMessage *msg;
1347 struct GNUNET_MQ_Envelope *env;
1349 LOG (GNUNET_ERROR_TYPE_DEBUG,
1350 "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
1351 (unsigned long long) limit);
1352 env = GNUNET_MQ_msg (msg,
1353 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1354 msg->gns_header.r_id = htonl (it->op_id);
1355 msg->limit = GNUNET_htonll (limit);
1356 GNUNET_MQ_send (h->mq,
1362 * Stops iteration and releases the namestore handle for further calls.
1364 * @param it the iterator
1367 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1369 struct GNUNET_NAMESTORE_Handle *h = it->h;
1370 struct GNUNET_MQ_Envelope *env;
1371 struct ZoneIterationStopMessage *msg;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Sending ZONE_ITERATION_STOP message\n");
1377 env = GNUNET_MQ_msg (msg,
1378 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1379 msg->gns_header.r_id = htonl (it->op_id);
1380 GNUNET_MQ_send (h->mq,
1388 * Cancel a namestore operation. The final callback from the
1389 * operation must not have been done yet.
1391 * @param qe operation to cancel
1394 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1400 /* end of namestore_api.c */