2 This file is part of GNUnet.
3 Copyright (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., 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 *nsh;
67 * Continuation to call
69 GNUNET_NAMESTORE_ContinuationWithStatus cont;
77 * Function to call with the records we get back; or NULL.
79 GNUNET_NAMESTORE_RecordMonitor proc;
82 * Closure for @e proc.
87 * The operation id this zone iteration operation has
95 * Handle for a zone iterator operation
97 struct GNUNET_NAMESTORE_ZoneIterator
103 struct GNUNET_NAMESTORE_ZoneIterator *next;
108 struct GNUNET_NAMESTORE_ZoneIterator *prev;
111 * Main handle to access the namestore.
113 struct GNUNET_NAMESTORE_Handle *h;
116 * The continuation to call with the results
118 GNUNET_NAMESTORE_RecordMonitor proc;
121 * Closure for @e proc.
126 * Private key of the zone.
128 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
131 * The operation id this zone iteration operation has
139 * Message in linked list we should send to the service. The
140 * actual binary message follows this struct.
142 struct PendingMessage
148 struct PendingMessage *next;
153 struct PendingMessage *prev;
156 * Size of the message.
164 * Connection to the NAMESTORE service.
166 struct GNUNET_NAMESTORE_Handle
170 * Configuration to use.
172 const struct GNUNET_CONFIGURATION_Handle *cfg;
175 * Socket (if available).
177 struct GNUNET_CLIENT_Connection *client;
180 * Currently pending transmission request (or NULL).
182 struct GNUNET_CLIENT_TransmitHandle *th;
185 * Head of linked list of pending messages to send to the service
187 struct PendingMessage *pending_head;
190 * Tail of linked list of pending messages to send to the service
192 struct PendingMessage *pending_tail;
195 * Head of pending namestore queue entries
197 struct GNUNET_NAMESTORE_QueueEntry *op_head;
200 * Tail of pending namestore queue entries
202 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
205 * Head of pending namestore zone iterator entries
207 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
210 * Tail of pending namestore zone iterator entries
212 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
217 struct GNUNET_SCHEDULER_Task * reconnect_task;
220 * Delay introduced before we reconnect.
222 struct GNUNET_TIME_Relative reconnect_delay;
225 * Should we reconnect to service due to some serious error?
230 * Did we start to receive yet?
235 * The last operation id used for a NAMESTORE operation
237 uint32_t last_op_id_used;
243 * Disconnect from service and then reconnect.
245 * @param h our handle
248 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
252 * Handle an incoming message of type
253 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
255 * @param qe the respective entry in the message queue
256 * @param msg the message we received
257 * @param size the message size
258 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
261 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
262 const struct RecordStoreResponseMessage* msg,
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "Received `%s' with result %i\n",
270 "RECORD_STORE_RESPONSE",
271 ntohl (msg->op_result));
272 /* TODO: add actual error message from namestore to response... */
273 res = ntohl (msg->op_result);
274 if (GNUNET_SYSERR == res)
275 emsg = _("Namestore failed to store record\n");
278 if (NULL != qe->cont)
279 qe->cont (qe->cont_cls, res, emsg);
285 * Handle an incoming message of type
286 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
288 * @param qe the respective entry in the message queue
289 * @param msg the message we received
290 * @param size the message size
291 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
294 handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
295 const struct LabelLookupResponseMessage *msg,
304 unsigned int rd_count;
307 LOG (GNUNET_ERROR_TYPE_DEBUG,
309 "RECORD_LOOKUP_RESULT");
311 rd_len = ntohs (msg->rd_len);
312 rd_count = ntohs (msg->rd_count);
313 msg_len = ntohs (msg->gns_header.header.size);
314 name_len = ntohs (msg->name_len);
315 found = ntohs (msg->found);
316 exp_msg_len = sizeof (struct LabelLookupResponseMessage) + name_len + rd_len;
317 if (msg_len != exp_msg_len)
320 return GNUNET_SYSERR;
322 name = (const char *) &msg[1];
323 if ( (name_len > 0) &&
324 ('\0' != name[name_len -1]) )
327 return GNUNET_SYSERR;
329 if (GNUNET_NO == found)
331 /* label was not in namestore */
332 if (NULL != qe->proc)
333 qe->proc (qe->proc_cls,
340 rd_tmp = &name[name_len];
342 struct GNUNET_GNSRECORD_Data rd[rd_count];
344 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
347 return GNUNET_SYSERR;
351 if (NULL != qe->proc)
352 qe->proc (qe->proc_cls,
356 (rd_count > 0) ? rd : NULL);
363 * Handle an incoming message of type
364 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
366 * @param qe the respective entry in the message queue
367 * @param msg the message we received
368 * @param size the message size
369 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
372 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
373 const struct RecordResultMessage *msg,
382 unsigned int rd_count;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 rd_len = ntohs (msg->rd_len);
388 rd_count = ntohs (msg->rd_count);
389 msg_len = ntohs (msg->gns_header.header.size);
390 name_len = ntohs (msg->name_len);
391 GNUNET_break (0 == ntohs (msg->reserved));
392 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
393 if (msg_len != exp_msg_len)
396 return GNUNET_SYSERR;
398 name = (const char *) &msg[1];
399 if ( (name_len > 0) &&
400 ('\0' != name[name_len -1]) )
403 return GNUNET_SYSERR;
405 rd_tmp = &name[name_len];
407 struct GNUNET_GNSRECORD_Data rd[rd_count];
409 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
412 return GNUNET_SYSERR;
416 if (NULL != qe->proc)
417 qe->proc (qe->proc_cls,
421 (rd_count > 0) ? rd : NULL);
428 * Handle an incoming message of type
429 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
431 * @param qe the respective entry in the message queue
432 * @param msg the message we received
433 * @param size the message size
434 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
435 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
438 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
439 const struct ZoneToNameResponseMessage *msg,
445 unsigned int rd_count;
446 const char *name_tmp;
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
451 "ZONE_TO_NAME_RESPONSE");
452 res = ntohs (msg->res);
456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 "An error occured during zone to name operation\n");
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "Namestore has no result for zone to name mapping \n");
462 if (NULL != qe->proc)
463 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
466 LOG (GNUNET_ERROR_TYPE_DEBUG,
467 "Namestore has result for zone to name mapping \n");
468 name_len = ntohs (msg->name_len);
469 rd_count = ntohs (msg->rd_count);
470 rd_ser_len = ntohs (msg->rd_len);
471 name_tmp = (const char *) &msg[1];
472 if ( (name_len > 0) &&
473 ('\0' != name_tmp[name_len -1]) )
476 return GNUNET_SYSERR;
478 rd_tmp = &name_tmp[name_len];
480 struct GNUNET_GNSRECORD_Data rd[rd_count];
482 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
485 return GNUNET_SYSERR;
487 /* normal end, call continuation with result */
488 if (NULL != qe->proc)
489 qe->proc (qe->proc_cls,
493 /* return is important here: break would call continuation with error! */
498 return GNUNET_SYSERR;
500 /* error case, call continuation with error */
501 if (NULL != qe->proc)
502 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
508 * Handle incoming messages for record operations
510 * @param qe the respective zone iteration handle
511 * @param msg the message we received
512 * @param type the message type in host byte order
513 * @param size the message size
514 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
515 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
518 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
519 const struct GNUNET_MessageHeader *msg,
523 /* handle different message type */
526 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
527 if (size != sizeof (struct RecordStoreResponseMessage))
530 return GNUNET_SYSERR;
532 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
533 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
534 if (size < sizeof (struct ZoneToNameResponseMessage))
537 return GNUNET_SYSERR;
539 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
540 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
541 if (size < sizeof (struct RecordResultMessage))
544 return GNUNET_SYSERR;
546 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
547 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE:
548 if (size < sizeof (struct LabelLookupResponseMessage))
551 return GNUNET_SYSERR;
553 return handle_lookup_result (qe, (const struct LabelLookupResponseMessage *) msg, size);
556 return GNUNET_SYSERR;
562 * Handle a response from NAMESTORE service for a zone iteration request
564 * @param ze the respective iterator for this operation
565 * @param msg the message containing the respoonse
566 * @param size the message size
567 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
568 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
571 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
572 const struct RecordResultMessage *msg,
575 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
581 const char *name_tmp;
582 const char *rd_ser_tmp;
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "ZONE_ITERATION_RESPONSE");
587 msg_len = ntohs (msg->gns_header.header.size);
588 rd_len = ntohs (msg->rd_len);
589 rd_count = ntohs (msg->rd_count);
590 name_len = ntohs (msg->name_len);
591 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
592 if (msg_len != exp_msg_len)
595 return GNUNET_SYSERR;
597 if ( (0 == name_len) &&
598 (0 == (memcmp (&msg->private_key,
600 sizeof (priv_dummy)))) )
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Zone iteration completed!\n");
604 if (NULL != ze->proc)
605 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
608 name_tmp = (const char *) &msg[1];
609 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
612 return GNUNET_SYSERR;
614 rd_ser_tmp = (const char *) &name_tmp[name_len];
616 struct GNUNET_GNSRECORD_Data rd[rd_count];
618 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (rd_len,
624 return GNUNET_SYSERR;
626 if (NULL != ze->proc)
627 ze->proc (ze->proc_cls,
637 * Handle incoming messages for zone iterations
639 * @param ze the respective zone iteration handle
640 * @param msg the message we received
641 * @param type the message type in HBO
642 * @param size the message size
643 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
644 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
647 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
648 const struct GNUNET_MessageHeader *msg,
649 int type, size_t size)
651 /* handle different message type */
654 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
655 if (size < sizeof (struct RecordResultMessage))
658 return GNUNET_SYSERR;
660 return handle_zone_iteration_response (ze,
661 (const struct RecordResultMessage *) msg,
665 return GNUNET_SYSERR;
671 * Type of a function to call when we receive a message
674 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
675 * @param msg message received, NULL on timeout or fatal error
678 process_namestore_message (void *cls,
679 const struct GNUNET_MessageHeader *msg)
681 struct GNUNET_NAMESTORE_Handle *h = cls;
682 const struct GNUNET_NAMESTORE_Header *gm;
683 struct GNUNET_NAMESTORE_QueueEntry *qe;
684 struct GNUNET_NAMESTORE_ZoneIterator *ze;
695 size = ntohs (msg->size);
696 type = ntohs (msg->type);
697 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
700 GNUNET_CLIENT_receive (h->client,
701 &process_namestore_message, h,
702 GNUNET_TIME_UNIT_FOREVER_REL);
705 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
706 r_id = ntohl (gm->r_id);
708 LOG (GNUNET_ERROR_TYPE_DEBUG,
709 "Received message type %u size %u op %u\n",
712 (unsigned int) r_id);
714 /* Is it a record related operation ? */
715 for (qe = h->op_head; qe != NULL; qe = qe->next)
716 if (qe->op_id == r_id)
720 ret = manage_record_operations (qe, msg, type, size);
721 if (GNUNET_SYSERR == ret)
723 /* protocol error, need to reconnect */
724 h->reconnect = GNUNET_YES;
728 /* client was notified about success or failure, clean up 'qe' */
729 GNUNET_CONTAINER_DLL_remove (h->op_head,
735 /* Is it a zone iteration operation? */
736 for (ze = h->z_head; ze != NULL; ze = ze->next)
737 if (ze->op_id == r_id)
741 ret = manage_zone_operations (ze, msg, type, size);
742 if (GNUNET_NO == ret)
744 /* end of iteration, clean up 'ze' */
745 GNUNET_CONTAINER_DLL_remove (h->z_head,
750 if (GNUNET_SYSERR == ret)
752 /* protocol error, need to reconnect */
753 h->reconnect = GNUNET_YES;
756 if (GNUNET_YES == h->reconnect)
761 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
762 GNUNET_TIME_UNIT_FOREVER_REL);
767 * Transmit messages from the message queue to the service
768 * (if there are any, and if we are not already trying).
770 * @param h handle to use
773 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
777 * We can now transmit a message to NAMESTORE. Do it.
779 * @param cls the `struct GNUNET_NAMESTORE_Handle`
780 * @param size number of bytes we can transmit
781 * @param buf where to copy the messages
782 * @return number of bytes copied into @a buf
785 transmit_message_to_namestore (void *cls,
789 struct GNUNET_NAMESTORE_Handle *h = cls;
790 struct PendingMessage *p;
795 if ((0 == size) || (NULL == buf))
802 while ( (NULL != (p = h->pending_head)) &&
805 memcpy (&cbuf[ret], &p[1], p->size);
808 GNUNET_CONTAINER_DLL_remove (h->pending_head,
811 if (GNUNET_NO == h->is_receiving)
813 h->is_receiving = GNUNET_YES;
814 GNUNET_CLIENT_receive (h->client,
815 &process_namestore_message, h,
816 GNUNET_TIME_UNIT_FOREVER_REL);
826 * Transmit messages from the message queue to the service
827 * (if there are any, and if we are not already trying).
829 * @param h handle to use
832 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
834 struct PendingMessage *p;
837 return; /* transmission request already pending */
838 if (NULL == (p = h->pending_head))
839 return; /* transmission queue empty */
840 if (NULL == h->client)
841 return; /* currently reconnecting */
842 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
843 GNUNET_TIME_UNIT_FOREVER_REL,
844 GNUNET_NO, &transmit_message_to_namestore,
846 GNUNET_break (NULL != h->th);
851 * Reconnect to namestore service.
853 * @param h the handle to the NAMESTORE service
856 reconnect (struct GNUNET_NAMESTORE_Handle *h)
858 GNUNET_assert (NULL == h->client);
859 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
860 GNUNET_assert (NULL != h->client);
866 * Re-establish the connection to the service.
868 * @param cls handle to use to re-connect.
869 * @param tc scheduler context
872 reconnect_task (void *cls,
873 const struct GNUNET_SCHEDULER_TaskContext *tc)
875 struct GNUNET_NAMESTORE_Handle *h = cls;
877 h->reconnect_task = NULL;
883 * Disconnect from service and then reconnect.
885 * @param h our handle
888 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
892 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
895 h->reconnect = GNUNET_NO;
896 GNUNET_CLIENT_disconnect (h->client);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Reconnecting to namestore\n");
899 h->is_receiving = GNUNET_NO;
901 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
902 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
909 * Get a fresh operation id to distinguish between namestore requests
911 * @param h the namestore handle
912 * @return next operation id to use
915 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
917 return h->last_op_id_used++;
922 * Initialize the connection with the NAMESTORE service.
924 * @param cfg configuration to use
925 * @return handle to the GNS service, or NULL on error
927 struct GNUNET_NAMESTORE_Handle *
928 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
930 struct GNUNET_NAMESTORE_Handle *h;
932 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
934 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
935 h->last_op_id_used = 0;
941 * Disconnect from the namestore service (and free associated
944 * @param h handle to the namestore
947 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
949 struct PendingMessage *p;
950 struct GNUNET_NAMESTORE_QueueEntry *q;
951 struct GNUNET_NAMESTORE_ZoneIterator *z;
953 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
954 GNUNET_assert (NULL != h);
957 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
960 while (NULL != (p = h->pending_head))
962 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
965 GNUNET_break (NULL == h->op_head);
966 while (NULL != (q = h->op_head))
968 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
971 GNUNET_break (NULL == h->z_head);
972 while (NULL != (z = h->z_head))
974 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
977 if (NULL != h->client)
979 GNUNET_CLIENT_disconnect (h->client);
982 if (NULL != h->reconnect_task)
984 GNUNET_SCHEDULER_cancel (h->reconnect_task);
985 h->reconnect_task = NULL;
992 * Store an item in the namestore. If the item is already present,
993 * it is replaced with the new record. Use an empty array to
994 * remove all records under the given name.
996 * @param h handle to the namestore
997 * @param pkey private key of the zone
998 * @param label name that is being mapped (at most 255 characters long)
999 * @param rd_count number of records in the 'rd' array
1000 * @param rd array of records with data to store
1001 * @param cont continuation to call when done
1002 * @param cont_cls closure for @a cont
1003 * @return handle to abort the request
1005 struct GNUNET_NAMESTORE_QueueEntry *
1006 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1007 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1009 unsigned int rd_count,
1010 const struct GNUNET_GNSRECORD_Data *rd,
1011 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1014 struct GNUNET_NAMESTORE_QueueEntry *qe;
1015 struct PendingMessage *pe;
1022 struct RecordStoreMessage *msg;
1024 GNUNET_assert (NULL != h);
1025 GNUNET_assert (NULL != pkey);
1026 GNUNET_assert (NULL != label);
1027 name_len = strlen (label) + 1;
1028 if (name_len > MAX_NAME_LEN)
1033 rid = get_op_id (h);
1034 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1037 qe->cont_cls = cont_cls;
1039 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1042 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1043 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1044 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1045 pe->size = msg_size;
1046 msg = (struct RecordStoreMessage *) &pe[1];
1047 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1048 msg->gns_header.header.size = htons (msg_size);
1049 msg->gns_header.r_id = htonl (rid);
1050 msg->name_len = htons (name_len);
1051 msg->rd_count = htons (rd_count);
1052 msg->rd_len = htons (rd_ser_len);
1053 msg->reserved = htons (0);
1054 msg->private_key = *pkey;
1056 name_tmp = (char *) &msg[1];
1057 memcpy (name_tmp, label, name_len);
1058 rd_ser = &name_tmp[name_len];
1059 GNUNET_break (rd_ser_len ==
1060 GNUNET_GNSRECORD_records_serialize (rd_count, rd,
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "Sending `%s' message for name `%s' with size %u and %u records\n",
1065 "NAMESTORE_RECORD_STORE", label, msg_size,
1067 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1073 * Set the desired nick name for a zone
1075 * @param h handle to the namestore
1076 * @param pkey private key of the zone
1077 * @param nick the nick name to set
1078 * @param cont continuation to call when done
1079 * @param cont_cls closure for 'cont'
1080 * @return handle to abort the request
1082 struct GNUNET_NAMESTORE_QueueEntry *
1083 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1084 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1086 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1089 struct GNUNET_GNSRECORD_Data rd;
1091 memset (&rd, 0, sizeof (rd));
1093 rd.data_size = strlen (nick) +1;
1094 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1095 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1096 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1097 return GNUNET_NAMESTORE_records_store(h, pkey, GNUNET_GNS_MASTERZONE_STR, 1, &rd, cont, cont_cls);
1102 * Lookup an item in the namestore.
1104 * @param h handle to the namestore
1105 * @param pkey private key of the zone
1106 * @param label name that is being mapped (at most 255 characters long)
1107 * @param rm function to call with the result (with 0 records if we don't have that label)
1108 * @param rm_cls closure for @a rm
1109 * @return handle to abort the request
1111 struct GNUNET_NAMESTORE_QueueEntry *
1112 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1113 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1115 GNUNET_NAMESTORE_RecordMonitor rm,
1118 struct GNUNET_NAMESTORE_QueueEntry *qe;
1119 struct PendingMessage *pe;
1120 struct LabelLookupMessage * msg;
1124 GNUNET_assert (NULL != h);
1125 GNUNET_assert (NULL != pkey);
1126 GNUNET_assert (NULL != label);
1128 if (1 == (label_len = strlen (label) + 1))
1131 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1134 qe->proc_cls = rm_cls;
1135 qe->op_id = get_op_id(h);
1136 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1138 msg_size = sizeof (struct LabelLookupMessage) + label_len;
1139 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1140 pe->size = msg_size;
1141 msg = (struct LabelLookupMessage *) &pe[1];
1142 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1143 msg->gns_header.header.size = htons (msg_size);
1144 msg->gns_header.r_id = htonl (qe->op_id);
1146 msg->label_len = htonl(label_len);
1147 memcpy (&msg[1], label, label_len);
1149 /* transmit message */
1150 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1157 * Look for an existing PKEY delegation record for a given public key.
1158 * Returns at most one result to the processor.
1160 * @param h handle to the namestore
1161 * @param zone public key of the zone to look up in, never NULL
1162 * @param value_zone public key of the target zone (value), never NULL
1163 * @param proc function to call on the matching records, or with
1164 * NULL (rd_count == 0) if there are no matching records
1165 * @param proc_cls closure for @a proc
1166 * @return a handle that can be used to
1169 struct GNUNET_NAMESTORE_QueueEntry *
1170 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1171 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1172 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1173 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1175 struct GNUNET_NAMESTORE_QueueEntry *qe;
1176 struct PendingMessage *pe;
1177 struct ZoneToNameMessage * msg;
1181 GNUNET_assert (NULL != h);
1182 GNUNET_assert (NULL != zone);
1183 GNUNET_assert (NULL != value_zone);
1185 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1188 qe->proc_cls = proc_cls;
1190 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1192 msg_size = sizeof (struct ZoneToNameMessage);
1193 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1194 pe->size = msg_size;
1195 msg = (struct ZoneToNameMessage *) &pe[1];
1196 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1197 msg->gns_header.header.size = htons (msg_size);
1198 msg->gns_header.r_id = htonl (rid);
1200 msg->value_zone = *value_zone;
1202 /* transmit message */
1203 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1210 * Starts a new zone iteration (used to periodically PUT all of our
1211 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1212 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1213 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1214 * immediately, and then again after
1215 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1217 * @param h handle to the namestore
1218 * @param zone zone to access, NULL for all zones
1219 * @param proc function to call on each name from the zone; it
1220 * will be called repeatedly with a value (if available)
1221 * and always once at the end with a name of NULL.
1222 * @param proc_cls closure for @a proc
1223 * @return an iterator handle to use for iteration
1225 struct GNUNET_NAMESTORE_ZoneIterator *
1226 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1227 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1228 GNUNET_NAMESTORE_RecordMonitor proc,
1231 struct GNUNET_NAMESTORE_ZoneIterator *it;
1232 struct PendingMessage *pe;
1233 struct ZoneIterationStartMessage * msg;
1237 GNUNET_assert (NULL != h);
1239 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1242 it->proc_cls = proc_cls;
1246 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1248 msg_size = sizeof (struct ZoneIterationStartMessage);
1249 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1250 pe->size = msg_size;
1251 msg = (struct ZoneIterationStartMessage *) &pe[1];
1252 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1253 msg->gns_header.header.size = htons (msg_size);
1254 msg->gns_header.r_id = htonl (rid);
1257 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1264 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1265 * for the next record.
1267 * @param it the iterator
1270 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1272 struct GNUNET_NAMESTORE_Handle *h;
1273 struct ZoneIterationNextMessage * msg;
1274 struct PendingMessage *pe;
1277 GNUNET_assert (NULL != it);
1279 msg_size = sizeof (struct ZoneIterationNextMessage);
1280 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1281 pe->size = msg_size;
1282 msg = (struct ZoneIterationNextMessage *) &pe[1];
1283 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1284 msg->gns_header.header.size = htons (msg_size);
1285 msg->gns_header.r_id = htonl (it->op_id);
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "Sending `%s' message\n",
1288 "ZONE_ITERATION_NEXT");
1289 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1295 * Stops iteration and releases the namestore handle for further calls.
1297 * @param it the iterator
1300 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1302 struct GNUNET_NAMESTORE_Handle *h;
1303 struct PendingMessage *pe;
1305 struct ZoneIterationStopMessage * msg;
1307 GNUNET_assert (NULL != it);
1309 GNUNET_CONTAINER_DLL_remove (h->z_head,
1312 msg_size = sizeof (struct ZoneIterationStopMessage);
1313 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1314 pe->size = msg_size;
1315 msg = (struct ZoneIterationStopMessage *) &pe[1];
1316 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1317 msg->gns_header.header.size = htons (msg_size);
1318 msg->gns_header.r_id = htonl (it->op_id);
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Sending `%s' message\n",
1321 "ZONE_ITERATION_STOP");
1322 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1329 * Cancel a namestore operation. The final callback from the
1330 * operation must not have been done yet.
1332 * @param qe operation to cancel
1335 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1337 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1339 GNUNET_assert (NULL != qe);
1340 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1345 /* end of namestore_api.c */