2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file namestore/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "namestore.h"
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 struct GNUNET_NAMESTORE_QueueEntry
43 struct GNUNET_NAMESTORE_QueueEntry *next;
44 struct GNUNET_NAMESTORE_QueueEntry *prev;
46 struct GNUNET_NAMESTORE_Handle *nsh;
50 GNUNET_NAMESTORE_ContinuationWithStatus cont;
53 GNUNET_NAMESTORE_RecordProcessor proc;
56 char *data; /*stub data pointer*/
63 struct GNUNET_NAMESTORE_ZoneIterator
65 struct GNUNET_NAMESTORE_ZoneIterator *next;
66 struct GNUNET_NAMESTORE_ZoneIterator *prev;
70 struct GNUNET_NAMESTORE_Handle *h;
71 GNUNET_NAMESTORE_RecordProcessor proc;
80 * Message in linked list we should send to the service. The
81 * actual binary message follows this struct.
89 struct PendingMessage *next;
94 struct PendingMessage *prev;
97 * Size of the message.
102 * Is this the 'START' message?
109 * Connection to the NAMESTORE service.
111 struct GNUNET_NAMESTORE_Handle
115 * Configuration to use.
117 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 * Socket (if available).
122 struct GNUNET_CLIENT_Connection *client;
125 * Currently pending transmission request (or NULL).
127 struct GNUNET_CLIENT_TransmitHandle *th;
132 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
135 * Pending messages to send to the service
138 struct PendingMessage * pending_head;
139 struct PendingMessage * pending_tail;
142 * Should we reconnect to service due to some serious error?
148 * Pending namestore queue entries
150 struct GNUNET_NAMESTORE_QueueEntry * op_head;
151 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
156 * Pending namestore zone iterator entries
158 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
159 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
162 struct GNUNET_NAMESTORE_SimpleRecord
167 struct GNUNET_NAMESTORE_SimpleRecord *next;
172 struct GNUNET_NAMESTORE_SimpleRecord *prev;
175 const GNUNET_HashCode *zone;
176 uint32_t record_type;
177 struct GNUNET_TIME_Absolute expiration;
178 enum GNUNET_NAMESTORE_RecordFlags flags;
185 * Disconnect from service and then reconnect.
187 * @param h our handle
190 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
193 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
194 struct LookupNameResponseMessage * msg,
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
198 "LOOKUP_NAME_RESPONSE");
200 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
201 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
203 struct GNUNET_NAMESTORE_RecordData *rd = NULL;
204 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
205 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
206 struct GNUNET_TIME_Absolute expire;
207 unsigned int rd_count = 0;
210 int contains_sig = GNUNET_NO;
212 rd_count = ntohl (msg->rc_count);
213 msg_len = ntohs (msg->header.size);
214 name_len = ntohs (msg->name_len);
215 contains_sig = ntohs (msg->contains_sig);
216 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
218 if (msg_len != sizeof (struct LookupNameResponseMessage) +
219 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
221 rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData) +
222 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature))
228 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
229 name = (char *) &zone_key[1];
230 rd = (struct GNUNET_NAMESTORE_RecordData *) &name[name_len];
232 /* reset values if values not contained */
233 if (contains_sig == GNUNET_NO)
236 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd[rd_count];
242 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
243 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
246 if (qe->proc != NULL)
248 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, rd, signature);
250 /* Operation done, remove */
251 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
257 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
258 struct RecordPutResponseMessage* msg,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
262 "RECORD_PUT_RESPONSE");
264 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
267 if (ntohs (msg->op_result) == GNUNET_OK)
270 if (qe->cont != NULL)
272 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
276 else if (ntohs (msg->op_result) == GNUNET_NO)
279 if (qe->cont != NULL)
281 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
290 /* Operation done, remove */
291 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
298 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
299 struct RecordCreateResponseMessage* msg,
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
303 "RECORD_CREATE_RESPONSE");
305 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
308 if (ntohs (msg->op_result) == GNUNET_OK)
311 if (qe->cont != NULL)
313 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
317 else if (ntohs (msg->op_result) == GNUNET_NO)
320 if (qe->cont != NULL)
322 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
331 /* Operation done, remove */
332 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
339 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
340 const struct GNUNET_MessageHeader *msg,
341 int type, size_t size)
344 /* handle different message type */
346 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
347 if (size < sizeof (struct LookupNameResponseMessage))
352 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
354 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
355 if (size != sizeof (struct RecordPutResponseMessage))
360 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
362 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
363 if (size != sizeof (struct RecordCreateResponseMessage))
368 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
377 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
378 struct ZoneIterationResponseMessage *msg,
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
382 "ZONE_ITERATION_RESPONSE");
387 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
388 const struct GNUNET_MessageHeader *msg,
389 int type, size_t size)
392 /* handle different message type */
394 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
395 if (size < sizeof (struct ZoneIterationResponseMessage))
400 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
409 * Type of a function to call when we receive a message
412 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
413 * @param msg message received, NULL on timeout or fatal error
416 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
418 struct GNUNET_NAMESTORE_Handle *h = cls;
419 struct GenericMessage * gm;
420 struct GNUNET_NAMESTORE_QueueEntry *qe;
421 struct GNUNET_NAMESTORE_ZoneIterator *ze;
424 uint32_t op_id = UINT32_MAX;
432 size = ntohs (msg->size);
433 type = ntohs (msg->type);
435 if (size < sizeof (struct GenericMessage))
438 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
439 GNUNET_TIME_UNIT_FOREVER_REL);
443 gm = (struct GenericMessage *) msg;
444 op_id = ntohl (gm->op_id);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, op_id);
448 /* Find matching operation */
449 if (op_id > h->op_id)
451 /* No matching pending operation found */
453 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
454 GNUNET_TIME_UNIT_FOREVER_REL);
458 /* Is it a record related operation ? */
459 for (qe = h->op_head; qe != NULL; qe = qe->next)
461 if (qe->op_id == op_id)
466 manage_record_operations (qe, msg, type, size);
469 /* Is it a zone iteration operation ? */
470 for (ze = h->z_head; ze != NULL; ze = ze->next)
472 if (ze->op_id == op_id)
477 manage_zone_operations (ze, msg, type, size);
480 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
481 GNUNET_TIME_UNIT_FOREVER_REL);
483 if (GNUNET_YES == h->reconnect)
490 * Transmit messages from the message queue to the service
491 * (if there are any, and if we are not already trying).
493 * @param h handle to use
496 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
500 * We can now transmit a message to NAMESTORE. Do it.
502 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
503 * @param size number of bytes we can transmit
504 * @param buf where to copy the messages
505 * @return number of bytes copied into buf
508 transmit_message_to_namestore (void *cls, size_t size, void *buf)
510 struct GNUNET_NAMESTORE_Handle *h = cls;
511 struct PendingMessage *p;
516 if ((size == 0) || (buf == NULL))
523 while ((NULL != (p = h->pending_head)) && (p->size <= size))
525 memcpy (&cbuf[ret], &p[1], p->size);
528 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
529 if (GNUNET_YES == p->is_init)
530 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
531 GNUNET_TIME_UNIT_FOREVER_REL);
540 * Transmit messages from the message queue to the service
541 * (if there are any, and if we are not already trying).
543 * @param h handle to use
546 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
548 struct PendingMessage *p;
552 if (NULL == (p = h->pending_head))
554 if (NULL == h->client)
555 return; /* currently reconnecting */
557 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
558 GNUNET_TIME_UNIT_FOREVER_REL,
559 GNUNET_NO, &transmit_message_to_namestore,
565 * Reconnect to namestore service.
567 * @param h the handle to the namestore service
570 reconnect (struct GNUNET_NAMESTORE_Handle *h)
572 struct PendingMessage *p;
573 struct StartMessage *init;
575 GNUNET_assert (NULL == h->client);
576 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
577 GNUNET_assert (NULL != h->client);
579 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
581 p = GNUNET_malloc (sizeof (struct PendingMessage) +
582 sizeof (struct StartMessage));
583 p->size = sizeof (struct StartMessage);
584 p->is_init = GNUNET_YES;
585 init = (struct StartMessage *) &p[1];
586 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
587 init->header.size = htons (sizeof (struct StartMessage));
588 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
594 * Re-establish the connection to the service.
596 * @param cls handle to use to re-connect.
597 * @param tc scheduler context
600 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
602 struct GNUNET_NAMESTORE_Handle *h = cls;
604 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
610 * Disconnect from service and then reconnect.
612 * @param h our handle
615 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
617 h->reconnect = GNUNET_NO;
618 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
620 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
626 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
628 uint32_t op_id = h->op_id;
634 * Initialize the connection with the NAMESTORE service.
636 * @param cfg configuration to use
637 * @return handle to the GNS service, or NULL on error
639 struct GNUNET_NAMESTORE_Handle *
640 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
642 struct GNUNET_NAMESTORE_Handle *h;
644 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
646 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
653 * Disconnect from the namestore service (and free associated
656 * @param h handle to the namestore
657 * @param drop set to GNUNET_YES to delete all data in namestore (!)
660 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
662 struct PendingMessage *p;
663 struct GNUNET_NAMESTORE_QueueEntry *q;
664 struct GNUNET_NAMESTORE_ZoneIterator *z;
666 GNUNET_assert (h != NULL);
668 while (NULL != (p = h->pending_head))
670 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
674 while (NULL != (q = h->op_head))
676 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
680 while (NULL != (z = h->z_head))
682 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
686 if (NULL != h->client)
688 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
691 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
693 GNUNET_SCHEDULER_cancel (h->reconnect_task);
694 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
702 * Store an item in the namestore. If the item is already present,
703 * the expiration time is updated to the max of the existing time and
704 * the new time. This API is used when we cache signatures from other
707 * @param h handle to the namestore
708 * @param zone_key public key of the zone
709 * @param name name that is being mapped (at most 255 characters long)
710 * @param expire when does the corresponding block in the DHT expire (until
711 * when should we never do a DHT lookup for the same name again)?
712 * @param rd_count number of entries in 'rd' array
713 * @param rd array of records with data to store
714 * @param signature signature for all the records in the zone under the given name
715 * @param cont continuation to call when done
716 * @param cont_cls closure for cont
717 * @return handle to abort the request
719 struct GNUNET_NAMESTORE_QueueEntry *
720 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
721 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
723 struct GNUNET_TIME_Absolute expire,
724 unsigned int rd_count,
725 const struct GNUNET_NAMESTORE_RecordData *rd,
726 const struct GNUNET_CRYPTO_RsaSignature *signature,
727 GNUNET_NAMESTORE_ContinuationWithStatus cont,
730 struct GNUNET_NAMESTORE_QueueEntry *qe;
731 struct PendingMessage *pe;
733 /* pointer to elements */
734 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
735 struct GNUNET_NAMESTORE_RecordData *rd_tmp;
739 size_t name_len = strlen(name) + 1;
742 GNUNET_assert (NULL != h);
744 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
747 qe->cont_cls = cont_cls;
749 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
752 struct RecordPutMessage * msg;
753 msg_size = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len + rd_count * (sizeof (struct GNUNET_NAMESTORE_RecordData));
755 /* create msg here */
756 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
758 pe->is_init = GNUNET_NO;
759 msg = (struct RecordPutMessage *) &pe[1];
760 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
761 name_tmp = (char *) &zone_key_tmp[1];
762 rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
764 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
765 msg->header.size = htons (msg_size);
766 msg->op_id = htonl (id);
767 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
768 msg->signature = *signature;
769 msg->name_len = htons (name_len);
770 memcpy (name_tmp, name, name_len);
771 msg->expire = GNUNET_TIME_absolute_hton (expire);
772 msg->rd_count = htonl(rd_count);
773 memcpy (rd_tmp, rd, rd_count * (sizeof (struct GNUNET_NAMESTORE_RecordData)));
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
777 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
785 * Check if a signature is valid. This API is used by the GNS Block
786 * to validate signatures received from the network.
788 * @param public_key public key of the zone
789 * @param name name that is being mapped (at most 255 characters long)
790 * @param rd_count number of entries in 'rd' array
791 * @param rd array of records with data to store
792 * @param signature signature for all the records in the zone under the given name
793 * @return GNUNET_OK if the signature is valid
796 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
798 unsigned int rd_count,
799 const struct GNUNET_NAMESTORE_RecordData *rd,
800 const struct GNUNET_CRYPTO_RsaSignature *signature)
802 return GNUNET_SYSERR;
806 * Store an item in the namestore. If the item is already present,
807 * the expiration time is updated to the max of the existing time and
808 * the new time. This API is used by the authority of a zone.
810 * @param h handle to the namestore
811 * @param pkey private key of the zone
812 * @param name name that is being mapped (at most 255 characters long)
813 * @param rd record data to store
814 * @param cont continuation to call when done
815 * @param cont_cls closure for cont
816 * @return handle to abort the request
818 struct GNUNET_NAMESTORE_QueueEntry *
819 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
820 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
822 const struct GNUNET_NAMESTORE_RecordData *rd,
823 GNUNET_NAMESTORE_ContinuationWithStatus cont,
826 struct GNUNET_NAMESTORE_QueueEntry *qe;
827 struct PendingMessage *pe;
828 struct GNUNET_NAMESTORE_RecordData * rd_tmp;
834 GNUNET_assert (NULL != h);
837 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
840 qe->cont_cls = cont_cls;
844 struct RecordCreateMessage * msg;
845 msg_size = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
847 /* create msg here */
848 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
850 pe->is_init = GNUNET_NO;
851 msg = (struct RecordCreateMessage *) &pe[1];
853 name_tmp = (char *) &msg[1];
854 rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
856 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
857 msg->header.size = htons (msg_size);
858 msg->op_id = htonl (id);
859 //msg->signature = *signature;
860 msg->name_len = htons (name_len);
861 memcpy (name_tmp, name, name_len);
862 memcpy (rd_tmp, rd, sizeof (struct GNUNET_NAMESTORE_RecordData));
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
866 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
873 * Explicitly remove some content from the database. The
874 * "cont"inuation will be called with status "GNUNET_OK" if content
875 * was removed, "GNUNET_NO" if no matching entry was found and
876 * "GNUNET_SYSERR" on all other types of errors.
877 * This API is used by the authority of a zone.
879 * @param h handle to the namestore
880 * @param pkey private key of the zone
881 * @param name name that is being mapped (at most 255 characters long)
882 * @param rd record data
883 * @param cont continuation to call when done
884 * @param cont_cls closure for cont
885 * @return handle to abort the request
887 struct GNUNET_NAMESTORE_QueueEntry *
888 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
889 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
891 const struct GNUNET_NAMESTORE_RecordData *rd,
892 GNUNET_NAMESTORE_ContinuationWithStatus cont,
895 struct GNUNET_NAMESTORE_QueueEntry *qe;
896 struct PendingMessage *pe;
897 struct GNUNET_NAMESTORE_RecordData * rd_tmp;
903 GNUNET_assert (NULL != h);
906 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
909 qe->cont_cls = cont_cls;
913 struct RecordRemoveMessage * msg;
914 msg_size = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
916 /* create msg here */
917 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
919 pe->is_init = GNUNET_NO;
920 msg = (struct RecordRemoveMessage *) &pe[1];
922 name_tmp = (char *) &msg[1];
923 rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
925 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
926 msg->header.size = htons (msg_size);
927 msg->op_id = htonl (id);
928 //msg->signature = *signature;
929 msg->name_len = htons (name_len);
930 memcpy (name_tmp, name, name_len);
931 memcpy (rd_tmp, rd, sizeof (struct GNUNET_NAMESTORE_RecordData));
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
935 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
942 * Get a result for a particular key from the namestore. The processor
943 * will only be called once.
945 * @param h handle to the namestore
946 * @param zone zone to look up a record from
947 * @param name name to look up
948 * @param record_type desired record type, 0 for all
949 * @param proc function to call on the matching records, or with
950 * NULL (rd_count == 0) if there are no matching records
951 * @param proc_cls closure for proc
952 * @return a handle that can be used to
955 struct GNUNET_NAMESTORE_QueueEntry *
956 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
957 const GNUNET_HashCode *zone,
959 uint32_t record_type,
960 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
962 struct GNUNET_NAMESTORE_QueueEntry *qe;
963 struct PendingMessage *pe;
968 GNUNET_assert (NULL != h);
969 GNUNET_assert (NULL != zone);
970 GNUNET_assert (NULL != name);
972 name_len = strlen (name) + 1;
973 if ((name_len == 0) || (name_len > 256))
980 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
983 qe->proc_cls = proc_cls;
985 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
988 msg_size = sizeof (struct LookupNameMessage) + name_len;
989 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
991 /* create msg here */
992 struct LookupNameMessage * msg;
994 pe->is_init = GNUNET_NO;
995 msg = (struct LookupNameMessage *) &pe[1];
996 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
997 msg->header.size = htons (msg_size);
998 msg->op_id = htonl (id);
999 msg->record_type = htonl (record_type);
1001 msg->name_len = htonl (name_len);
1002 memcpy (&msg[1], name, name_len);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1006 /* transmit message */
1007 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1016 * Starts a new zone iteration (used to periodically PUT all of our
1017 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1018 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1019 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1020 * immediately, and then again after
1021 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1023 * @param h handle to the namestore
1024 * @param zone zone to access, NULL for all zones
1025 * @param must_have_flags flags that must be set for the record to be returned
1026 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1027 * @param proc function to call on each name from the zone; it
1028 * will be called repeatedly with a value (if available)
1029 * and always once at the end with a name of NULL.
1030 * @param proc_cls closure for proc
1031 * @return an iterator handle to use for iteration
1033 struct GNUNET_NAMESTORE_ZoneIterator *
1034 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1035 const GNUNET_HashCode *zone,
1036 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1037 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1038 GNUNET_NAMESTORE_RecordProcessor proc,
1041 struct GNUNET_NAMESTORE_ZoneIterator *it;
1042 struct PendingMessage *pe;
1043 size_t msg_size = 0;
1046 GNUNET_assert (NULL != h);
1047 GNUNET_assert (NULL != zone);
1050 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1053 it->proc_cls = proc;
1056 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1059 msg_size = sizeof (struct ZoneIterationStartMessage);
1060 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1062 /* create msg here */
1063 struct ZoneIterationStartMessage * msg;
1064 pe->size = msg_size;
1065 pe->is_init = GNUNET_NO;
1066 msg = (struct ZoneIterationStartMessage *) &pe[1];
1067 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1068 msg->header.size = htons (msg_size);
1069 msg->op_id = htonl (id);
1071 msg->must_have_flags = ntohs (must_have_flags);
1072 msg->must_not_have_flags = ntohs (must_not_have_flags);
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1076 /* transmit message */
1077 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1085 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1086 * for the next record.
1088 * @param it the iterator
1091 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1093 struct GNUNET_NAMESTORE_Handle *h;
1094 struct PendingMessage *pe;
1095 size_t msg_size = 0;
1097 GNUNET_assert (NULL != it);
1101 msg_size = sizeof (struct ZoneIterationNextMessage);
1102 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1104 /* create msg here */
1105 struct ZoneIterationNextMessage * msg;
1106 pe->size = msg_size;
1107 pe->is_init = GNUNET_NO;
1108 msg = (struct ZoneIterationNextMessage *) &pe[1];
1109 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1110 msg->header.size = htons (msg_size);
1111 msg->op_id = htonl (it->op_id);
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1115 /* transmit message */
1116 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1122 * Stops iteration and releases the namestore handle for further calls.
1124 * @param it the iterator
1127 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1129 GNUNET_assert (NULL != it);
1130 struct PendingMessage *pe;
1131 size_t msg_size = 0;
1132 struct GNUNET_NAMESTORE_Handle *h = it->h;
1135 msg_size = sizeof (struct ZoneIterationStopMessage);
1136 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1138 /* create msg here */
1139 struct ZoneIterationStopMessage * msg;
1140 pe->size = msg_size;
1141 pe->is_init = GNUNET_NO;
1142 msg = (struct ZoneIterationStopMessage *) &pe[1];
1143 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1144 msg->header.size = htons (msg_size);
1145 msg->op_id = htonl (it->op_id);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1149 /* transmit message */
1150 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1156 * Cancel a namestore operation. The final callback from the
1157 * operation must not have been done yet.
1159 * @param qe operation to cancel
1162 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1164 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1166 GNUNET_assert (qe != NULL);
1168 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1173 /* end of namestore_api.c */