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;
204 struct GNUNET_NAMESTORE_RecordData *rd = NULL;
205 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
206 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
207 struct GNUNET_TIME_Absolute expire;
212 int contains_sig = GNUNET_NO;
215 rd_len = ntohl (msg->rd_len);
216 msg_len = ntohs (msg->header.size);
217 name_len = ntohs (msg->name_len);
218 contains_sig = ntohs (msg->contains_sig);
219 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
221 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
222 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
225 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
227 if (msg_len != exp_msg_len)
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
230 msg_len, exp_msg_len);
235 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
236 name = (char *) &zone_key[1];
237 rd_tmp = &name[name_len];
238 rd_count = GNUNET_NAMESTORE_records_deserialize(&rd, rd_tmp, rd_len);
240 /* reset values if values not contained */
241 if (contains_sig == GNUNET_NO)
244 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
250 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
251 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
254 if (qe->proc != NULL)
256 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, rd, signature);
259 GNUNET_NAMESTORE_records_free(rd_count, rd);
261 /* Operation done, remove */
262 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
268 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
269 struct RecordPutResponseMessage* msg,
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
273 "RECORD_PUT_RESPONSE");
275 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
278 if (ntohs (msg->op_result) == GNUNET_OK)
281 if (qe->cont != NULL)
283 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
287 else if (ntohs (msg->op_result) == GNUNET_NO)
290 if (qe->cont != NULL)
292 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
301 /* Operation done, remove */
302 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
309 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
310 struct RecordCreateResponseMessage* msg,
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
314 "RECORD_CREATE_RESPONSE");
316 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
319 if (ntohs (msg->op_result) == GNUNET_OK)
322 if (qe->cont != NULL)
324 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
328 else if (ntohs (msg->op_result) == GNUNET_NO)
331 if (qe->cont != NULL)
333 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
342 /* Operation done, remove */
343 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
350 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
351 const struct GNUNET_MessageHeader *msg,
352 int type, size_t size)
355 /* handle different message type */
357 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
358 if (size < sizeof (struct LookupNameResponseMessage))
363 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
365 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
366 if (size != sizeof (struct RecordPutResponseMessage))
371 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
373 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
374 if (size != sizeof (struct RecordCreateResponseMessage))
379 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
388 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
389 struct ZoneIterationResponseMessage *msg,
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
393 "ZONE_ITERATION_RESPONSE");
396 if (ze->proc != NULL)
399 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
405 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
406 const struct GNUNET_MessageHeader *msg,
407 int type, size_t size)
410 /* handle different message type */
412 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
413 if (size < sizeof (struct ZoneIterationResponseMessage))
418 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
427 * Type of a function to call when we receive a message
430 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
431 * @param msg message received, NULL on timeout or fatal error
434 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
436 struct GNUNET_NAMESTORE_Handle *h = cls;
437 struct GenericMessage * gm;
438 struct GNUNET_NAMESTORE_QueueEntry *qe;
439 struct GNUNET_NAMESTORE_ZoneIterator *ze;
442 uint32_t op_id = UINT32_MAX;
450 size = ntohs (msg->size);
451 type = ntohs (msg->type);
453 if (size < sizeof (struct GenericMessage))
456 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
457 GNUNET_TIME_UNIT_FOREVER_REL);
461 gm = (struct GenericMessage *) msg;
462 op_id = ntohl (gm->op_id);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, op_id);
466 /* Find matching operation */
467 if (op_id > h->op_id)
469 /* No matching pending operation found */
471 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
472 GNUNET_TIME_UNIT_FOREVER_REL);
476 /* Is it a record related operation ? */
477 for (qe = h->op_head; qe != NULL; qe = qe->next)
479 if (qe->op_id == op_id)
484 manage_record_operations (qe, msg, type, size);
487 /* Is it a zone iteration operation ? */
488 for (ze = h->z_head; ze != NULL; ze = ze->next)
490 if (ze->op_id == op_id)
495 manage_zone_operations (ze, msg, type, size);
498 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
499 GNUNET_TIME_UNIT_FOREVER_REL);
501 if (GNUNET_YES == h->reconnect)
508 * Transmit messages from the message queue to the service
509 * (if there are any, and if we are not already trying).
511 * @param h handle to use
514 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
518 * We can now transmit a message to NAMESTORE. Do it.
520 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
521 * @param size number of bytes we can transmit
522 * @param buf where to copy the messages
523 * @return number of bytes copied into buf
526 transmit_message_to_namestore (void *cls, size_t size, void *buf)
528 struct GNUNET_NAMESTORE_Handle *h = cls;
529 struct PendingMessage *p;
534 if ((size == 0) || (buf == NULL))
541 while ((NULL != (p = h->pending_head)) && (p->size <= size))
543 memcpy (&cbuf[ret], &p[1], p->size);
546 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
547 if (GNUNET_YES == p->is_init)
548 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
549 GNUNET_TIME_UNIT_FOREVER_REL);
558 * Transmit messages from the message queue to the service
559 * (if there are any, and if we are not already trying).
561 * @param h handle to use
564 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
566 struct PendingMessage *p;
570 if (NULL == (p = h->pending_head))
572 if (NULL == h->client)
573 return; /* currently reconnecting */
575 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
576 GNUNET_TIME_UNIT_FOREVER_REL,
577 GNUNET_NO, &transmit_message_to_namestore,
583 * Reconnect to namestore service.
585 * @param h the handle to the namestore service
588 reconnect (struct GNUNET_NAMESTORE_Handle *h)
590 struct PendingMessage *p;
591 struct StartMessage *init;
593 GNUNET_assert (NULL == h->client);
594 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
595 GNUNET_assert (NULL != h->client);
597 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
599 p = GNUNET_malloc (sizeof (struct PendingMessage) +
600 sizeof (struct StartMessage));
601 p->size = sizeof (struct StartMessage);
602 p->is_init = GNUNET_YES;
603 init = (struct StartMessage *) &p[1];
604 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
605 init->header.size = htons (sizeof (struct StartMessage));
606 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
612 * Re-establish the connection to the service.
614 * @param cls handle to use to re-connect.
615 * @param tc scheduler context
618 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct GNUNET_NAMESTORE_Handle *h = cls;
622 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
628 * Disconnect from service and then reconnect.
630 * @param h our handle
633 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
635 h->reconnect = GNUNET_NO;
636 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
638 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
644 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
646 uint32_t op_id = h->op_id;
652 * Initialize the connection with the NAMESTORE service.
654 * @param cfg configuration to use
655 * @return handle to the GNS service, or NULL on error
657 struct GNUNET_NAMESTORE_Handle *
658 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
660 struct GNUNET_NAMESTORE_Handle *h;
662 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
664 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
671 * Disconnect from the namestore service (and free associated
674 * @param h handle to the namestore
675 * @param drop set to GNUNET_YES to delete all data in namestore (!)
678 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
680 struct PendingMessage *p;
681 struct GNUNET_NAMESTORE_QueueEntry *q;
682 struct GNUNET_NAMESTORE_ZoneIterator *z;
684 GNUNET_assert (h != NULL);
686 while (NULL != (p = h->pending_head))
688 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
692 while (NULL != (q = h->op_head))
694 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
698 while (NULL != (z = h->z_head))
700 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
704 if (NULL != h->client)
706 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
709 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
711 GNUNET_SCHEDULER_cancel (h->reconnect_task);
712 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
720 * Store an item in the namestore. If the item is already present,
721 * the expiration time is updated to the max of the existing time and
722 * the new time. This API is used when we cache signatures from other
725 * @param h handle to the namestore
726 * @param zone_key public key of the zone
727 * @param name name that is being mapped (at most 255 characters long)
728 * @param expire when does the corresponding block in the DHT expire (until
729 * when should we never do a DHT lookup for the same name again)?
730 * @param rd_count number of entries in 'rd' array
731 * @param rd array of records with data to store
732 * @param signature signature for all the records in the zone under the given name
733 * @param cont continuation to call when done
734 * @param cont_cls closure for cont
735 * @return handle to abort the request
737 struct GNUNET_NAMESTORE_QueueEntry *
738 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
739 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
741 struct GNUNET_TIME_Absolute expire,
742 unsigned int rd_count,
743 const struct GNUNET_NAMESTORE_RecordData *rd,
744 const struct GNUNET_CRYPTO_RsaSignature *signature,
745 GNUNET_NAMESTORE_ContinuationWithStatus cont,
748 struct GNUNET_NAMESTORE_QueueEntry *qe;
749 struct PendingMessage *pe;
751 /* pointer to elements */
752 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
758 size_t name_len = strlen(name) + 1;
759 size_t rd_ser_len = 0;
762 GNUNET_assert (NULL != h);
764 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
767 qe->cont_cls = cont_cls;
769 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
772 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, rd_count, rd);
774 struct RecordPutMessage * msg;
775 msg_size = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len + rd_ser_len;
777 /* create msg here */
778 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
780 pe->is_init = GNUNET_NO;
781 msg = (struct RecordPutMessage *) &pe[1];
782 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
783 name_tmp = (char *) &zone_key_tmp[1];
784 rd_tmp = &name_tmp[name_len];
786 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
787 msg->header.size = htons (msg_size);
788 msg->op_id = htonl (id);
789 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
790 msg->signature = *signature;
791 msg->name_len = htons (name_len);
792 memcpy (name_tmp, name, name_len);
793 msg->expire = GNUNET_TIME_absolute_hton (expire);
794 msg->rd_len = htons (rd_ser_len);
796 memcpy (rd_tmp, rd_ser, rd_ser_len);
797 GNUNET_free (rd_ser);
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
801 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
809 * Check if a signature is valid. This API is used by the GNS Block
810 * to validate signatures received from the network.
812 * @param public_key public key of the zone
813 * @param name name that is being mapped (at most 255 characters long)
814 * @param rd_count number of entries in 'rd' array
815 * @param rd array of records with data to store
816 * @param signature signature for all the records in the zone under the given name
817 * @return GNUNET_OK if the signature is valid
820 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
822 unsigned int rd_count,
823 const struct GNUNET_NAMESTORE_RecordData *rd,
824 const struct GNUNET_CRYPTO_RsaSignature *signature)
826 return GNUNET_SYSERR;
830 * Store an item in the namestore. If the item is already present,
831 * the expiration time is updated to the max of the existing time and
832 * the new time. This API is used by the authority of a zone.
834 * @param h handle to the namestore
835 * @param pkey private key of the zone
836 * @param name name that is being mapped (at most 255 characters long)
837 * @param rd record data to store
838 * @param cont continuation to call when done
839 * @param cont_cls closure for cont
840 * @return handle to abort the request
842 struct GNUNET_NAMESTORE_QueueEntry *
843 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
844 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
846 const struct GNUNET_NAMESTORE_RecordData *rd,
847 GNUNET_NAMESTORE_ContinuationWithStatus cont,
850 struct GNUNET_NAMESTORE_QueueEntry *qe;
851 struct PendingMessage *pe;
855 size_t rd_ser_len = 0;
860 GNUNET_assert (NULL != h);
863 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
866 qe->cont_cls = cont_cls;
870 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, 1, rd);
871 struct RecordCreateMessage * msg;
872 msg_size = sizeof (struct RecordCreateMessage) + name_len + rd_ser_len;
874 /* create msg here */
875 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
877 pe->is_init = GNUNET_NO;
878 msg = (struct RecordCreateMessage *) &pe[1];
880 name_tmp = (char *) &msg[1];
881 rd_tmp = &name_tmp[name_len];
883 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
884 msg->header.size = htons (msg_size);
885 msg->op_id = htonl (id);
886 //msg->signature = *signature;
887 msg->name_len = htons (name_len);
888 memcpy (name_tmp, name, name_len);
889 memcpy (rd_tmp, rd_ser, rd_ser_len);
890 GNUNET_free (rd_ser);
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
894 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
901 * Explicitly remove some content from the database. The
902 * "cont"inuation will be called with status "GNUNET_OK" if content
903 * was removed, "GNUNET_NO" if no matching entry was found and
904 * "GNUNET_SYSERR" on all other types of errors.
905 * This API is used by the authority of a zone.
907 * @param h handle to the namestore
908 * @param pkey private key of the zone
909 * @param name name that is being mapped (at most 255 characters long)
910 * @param rd record data
911 * @param cont continuation to call when done
912 * @param cont_cls closure for cont
913 * @return handle to abort the request
915 struct GNUNET_NAMESTORE_QueueEntry *
916 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
917 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
919 const struct GNUNET_NAMESTORE_RecordData *rd,
920 GNUNET_NAMESTORE_ContinuationWithStatus cont,
923 struct GNUNET_NAMESTORE_QueueEntry *qe;
924 struct PendingMessage *pe;
928 size_t rd_ser_len = 0;
933 GNUNET_assert (NULL != h);
936 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
939 qe->cont_cls = cont_cls;
943 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, 1, rd);
944 struct RecordRemoveMessage * msg;
945 msg_size = sizeof (struct RecordRemoveMessage) + name_len + rd_ser_len;
947 /* create msg here */
948 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
950 pe->is_init = GNUNET_NO;
951 msg = (struct RecordRemoveMessage *) &pe[1];
953 name_tmp = (char *) &msg[1];
954 rd_tmp = &name_tmp[name_len];
956 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
957 msg->header.size = htons (msg_size);
958 msg->op_id = htonl (id);
959 //msg->signature = *signature;
960 msg->name_len = htons (name_len);
961 memcpy (name_tmp, name, name_len);
962 memcpy (rd_tmp, rd_ser, rd_ser_len);
963 GNUNET_free (rd_ser);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
967 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
974 * Get a result for a particular key from the namestore. The processor
975 * will only be called once.
977 * @param h handle to the namestore
978 * @param zone zone to look up a record from
979 * @param name name to look up
980 * @param record_type desired record type, 0 for all
981 * @param proc function to call on the matching records, or with
982 * NULL (rd_count == 0) if there are no matching records
983 * @param proc_cls closure for proc
984 * @return a handle that can be used to
987 struct GNUNET_NAMESTORE_QueueEntry *
988 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
989 const GNUNET_HashCode *zone,
991 uint32_t record_type,
992 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
994 struct GNUNET_NAMESTORE_QueueEntry *qe;
995 struct PendingMessage *pe;
1000 GNUNET_assert (NULL != h);
1001 GNUNET_assert (NULL != zone);
1002 GNUNET_assert (NULL != name);
1004 name_len = strlen (name) + 1;
1005 if ((name_len == 0) || (name_len > 256))
1012 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1015 qe->proc_cls = proc_cls;
1017 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1020 msg_size = sizeof (struct LookupNameMessage) + name_len;
1021 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1023 /* create msg here */
1024 struct LookupNameMessage * msg;
1025 pe->size = msg_size;
1026 pe->is_init = GNUNET_NO;
1027 msg = (struct LookupNameMessage *) &pe[1];
1028 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1029 msg->header.size = htons (msg_size);
1030 msg->op_id = htonl (id);
1031 msg->record_type = htonl (record_type);
1033 msg->name_len = htonl (name_len);
1034 memcpy (&msg[1], name, name_len);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1038 /* transmit message */
1039 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1048 * Starts a new zone iteration (used to periodically PUT all of our
1049 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1050 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1051 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1052 * immediately, and then again after
1053 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1055 * @param h handle to the namestore
1056 * @param zone zone to access, NULL for all zones
1057 * @param must_have_flags flags that must be set for the record to be returned
1058 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1059 * @param proc function to call on each name from the zone; it
1060 * will be called repeatedly with a value (if available)
1061 * and always once at the end with a name of NULL.
1062 * @param proc_cls closure for proc
1063 * @return an iterator handle to use for iteration
1065 struct GNUNET_NAMESTORE_ZoneIterator *
1066 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1067 const GNUNET_HashCode *zone,
1068 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1069 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1070 GNUNET_NAMESTORE_RecordProcessor proc,
1073 struct GNUNET_NAMESTORE_ZoneIterator *it;
1074 struct PendingMessage *pe;
1075 size_t msg_size = 0;
1078 GNUNET_assert (NULL != h);
1079 GNUNET_assert (NULL != zone);
1082 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1085 it->proc_cls = proc;
1088 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1091 msg_size = sizeof (struct ZoneIterationStartMessage);
1092 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1094 /* create msg here */
1095 struct ZoneIterationStartMessage * msg;
1096 pe->size = msg_size;
1097 pe->is_init = GNUNET_NO;
1098 msg = (struct ZoneIterationStartMessage *) &pe[1];
1099 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1100 msg->header.size = htons (msg_size);
1101 msg->op_id = htonl (id);
1103 msg->must_have_flags = ntohs (must_have_flags);
1104 msg->must_not_have_flags = ntohs (must_not_have_flags);
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1108 /* transmit message */
1109 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1117 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1118 * for the next record.
1120 * @param it the iterator
1123 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1125 struct GNUNET_NAMESTORE_Handle *h;
1126 struct PendingMessage *pe;
1127 size_t msg_size = 0;
1129 GNUNET_assert (NULL != it);
1133 msg_size = sizeof (struct ZoneIterationNextMessage);
1134 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1136 /* create msg here */
1137 struct ZoneIterationNextMessage * msg;
1138 pe->size = msg_size;
1139 pe->is_init = GNUNET_NO;
1140 msg = (struct ZoneIterationNextMessage *) &pe[1];
1141 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1142 msg->header.size = htons (msg_size);
1143 msg->op_id = htonl (it->op_id);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1147 /* transmit message */
1148 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1154 * Stops iteration and releases the namestore handle for further calls.
1156 * @param it the iterator
1159 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1161 GNUNET_assert (NULL != it);
1162 struct PendingMessage *pe;
1163 size_t msg_size = 0;
1164 struct GNUNET_NAMESTORE_Handle *h = it->h;
1167 msg_size = sizeof (struct ZoneIterationStopMessage);
1168 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1170 /* create msg here */
1171 struct ZoneIterationStopMessage * msg;
1172 pe->size = msg_size;
1173 pe->is_init = GNUNET_NO;
1174 msg = (struct ZoneIterationStopMessage *) &pe[1];
1175 msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1176 msg->header.size = htons (msg_size);
1177 msg->op_id = htonl (it->op_id);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1181 /* transmit message */
1182 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1188 * Cancel a namestore operation. The final callback from the
1189 * operation must not have been done yet.
1191 * @param qe operation to cancel
1194 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1196 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1198 GNUNET_assert (qe != NULL);
1200 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1205 /* end of namestore_api.c */