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_crypto_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
38 #include "gnunet_common.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_disk_lib.h"
42 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
44 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
49 struct GNUNET_NAMESTORE_QueueEntry
51 struct GNUNET_NAMESTORE_QueueEntry *next;
52 struct GNUNET_NAMESTORE_QueueEntry *prev;
54 struct GNUNET_NAMESTORE_Handle *nsh;
58 GNUNET_NAMESTORE_ContinuationWithStatus cont;
61 GNUNET_NAMESTORE_RecordProcessor proc;
64 char *data; /*stub data pointer*/
71 struct GNUNET_NAMESTORE_ZoneIterator
73 struct GNUNET_NAMESTORE_ZoneIterator *next;
74 struct GNUNET_NAMESTORE_ZoneIterator *prev;
78 struct GNUNET_NAMESTORE_Handle *h;
79 GNUNET_NAMESTORE_RecordProcessor proc;
88 * Message in linked list we should send to the service. The
89 * actual binary message follows this struct.
97 struct PendingMessage *next;
102 struct PendingMessage *prev;
105 * Size of the message.
110 * Is this the 'START' message?
117 * Connection to the NAMESTORE service.
119 struct GNUNET_NAMESTORE_Handle
123 * Configuration to use.
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
128 * Socket (if available).
130 struct GNUNET_CLIENT_Connection *client;
133 * Currently pending transmission request (or NULL).
135 struct GNUNET_CLIENT_TransmitHandle *th;
140 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
143 * Pending messages to send to the service
146 struct PendingMessage * pending_head;
147 struct PendingMessage * pending_tail;
150 * Should we reconnect to service due to some serious error?
156 * Pending namestore queue entries
158 struct GNUNET_NAMESTORE_QueueEntry * op_head;
159 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
164 * Pending namestore zone iterator entries
166 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
167 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
170 struct GNUNET_NAMESTORE_SimpleRecord
175 struct GNUNET_NAMESTORE_SimpleRecord *next;
180 struct GNUNET_NAMESTORE_SimpleRecord *prev;
183 const GNUNET_HashCode *zone;
184 uint32_t record_type;
185 struct GNUNET_TIME_Absolute expiration;
186 enum GNUNET_NAMESTORE_RecordFlags flags;
193 * Disconnect from service and then reconnect.
195 * @param h our handle
198 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
201 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
202 struct LookupNameResponseMessage * msg,
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
206 "LOOKUP_NAME_RESPONSE");
208 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
209 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
212 struct GNUNET_NAMESTORE_RecordData *rd = NULL;
213 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
214 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
215 struct GNUNET_TIME_Absolute expire;
220 int contains_sig = GNUNET_NO;
223 rd_len = ntohs (msg->rd_len);
224 msg_len = ntohs (msg->gns_header.header.size);
225 name_len = ntohs (msg->name_len);
226 contains_sig = ntohs (msg->contains_sig);
227 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
229 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
230 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
233 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
235 if (msg_len != exp_msg_len)
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
238 msg_len, exp_msg_len);
243 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
244 name = (char *) &zone_key[1];
245 rd_tmp = &name[name_len];
246 rd_count = GNUNET_NAMESTORE_records_deserialize(&rd, rd_tmp, rd_len);
248 /* reset values if values not contained */
249 if (contains_sig == GNUNET_NO)
252 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
258 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
259 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
262 if (qe->proc != NULL)
264 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, rd, signature);
267 GNUNET_NAMESTORE_records_free(rd_count, rd);
269 /* Operation done, remove */
270 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
276 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
277 struct RecordPutResponseMessage* msg,
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
281 "RECORD_PUT_RESPONSE");
283 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
286 if (ntohs (msg->op_result) == GNUNET_OK)
289 if (qe->cont != NULL)
291 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
295 else if (ntohs (msg->op_result) == GNUNET_NO)
298 if (qe->cont != NULL)
300 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
309 /* Operation done, remove */
310 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
317 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
318 struct RecordCreateResponseMessage* msg,
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
322 "RECORD_CREATE_RESPONSE");
324 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
327 if (ntohs (msg->op_result) == GNUNET_OK)
330 if (qe->cont != NULL)
332 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
336 else if (ntohs (msg->op_result) == GNUNET_NO)
339 if (qe->cont != NULL)
341 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
350 /* Operation done, remove */
351 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
358 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
359 const struct GNUNET_MessageHeader *msg,
360 int type, size_t size)
363 /* handle different message type */
365 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
366 if (size < sizeof (struct LookupNameResponseMessage))
371 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
373 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
374 if (size != sizeof (struct RecordPutResponseMessage))
379 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
381 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
382 if (size != sizeof (struct RecordCreateResponseMessage))
387 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
396 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
397 struct ZoneIterationResponseMessage *msg,
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
401 "ZONE_ITERATION_RESPONSE");
404 if (ze->proc != NULL)
407 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
413 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
414 const struct GNUNET_MessageHeader *msg,
415 int type, size_t size)
418 /* handle different message type */
420 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
421 if (size < sizeof (struct ZoneIterationResponseMessage))
426 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
435 * Type of a function to call when we receive a message
438 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
439 * @param msg message received, NULL on timeout or fatal error
442 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
444 struct GNUNET_NAMESTORE_Handle *h = cls;
445 struct GNUNET_NAMESTORE_Header * gm;
446 struct GNUNET_NAMESTORE_QueueEntry *qe;
447 struct GNUNET_NAMESTORE_ZoneIterator *ze;
450 uint32_t r_id = UINT32_MAX;
458 size = ntohs (msg->size);
459 type = ntohs (msg->type);
461 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
464 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
465 GNUNET_TIME_UNIT_FOREVER_REL);
469 gm = (struct GNUNET_NAMESTORE_Header *) msg;
470 r_id = ntohl (gm->r_id);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
474 /* Find matching operation */
477 /* No matching pending operation found */
479 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
480 GNUNET_TIME_UNIT_FOREVER_REL);
484 /* Is it a record related operation ? */
485 for (qe = h->op_head; qe != NULL; qe = qe->next)
487 if (qe->op_id == r_id)
492 manage_record_operations (qe, msg, type, size);
495 /* Is it a zone iteration operation ? */
496 for (ze = h->z_head; ze != NULL; ze = ze->next)
498 if (ze->op_id == r_id)
503 manage_zone_operations (ze, msg, type, size);
506 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
507 GNUNET_TIME_UNIT_FOREVER_REL);
509 if (GNUNET_YES == h->reconnect)
516 * Transmit messages from the message queue to the service
517 * (if there are any, and if we are not already trying).
519 * @param h handle to use
522 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
526 * We can now transmit a message to NAMESTORE. Do it.
528 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
529 * @param size number of bytes we can transmit
530 * @param buf where to copy the messages
531 * @return number of bytes copied into buf
534 transmit_message_to_namestore (void *cls, size_t size, void *buf)
536 struct GNUNET_NAMESTORE_Handle *h = cls;
537 struct PendingMessage *p;
542 if ((size == 0) || (buf == NULL))
549 while ((NULL != (p = h->pending_head)) && (p->size <= size))
551 memcpy (&cbuf[ret], &p[1], p->size);
554 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
555 if (GNUNET_YES == p->is_init)
556 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
557 GNUNET_TIME_UNIT_FOREVER_REL);
566 * Transmit messages from the message queue to the service
567 * (if there are any, and if we are not already trying).
569 * @param h handle to use
572 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
574 struct PendingMessage *p;
578 if (NULL == (p = h->pending_head))
580 if (NULL == h->client)
581 return; /* currently reconnecting */
583 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
584 GNUNET_TIME_UNIT_FOREVER_REL,
585 GNUNET_NO, &transmit_message_to_namestore,
591 * Reconnect to namestore service.
593 * @param h the handle to the namestore service
596 reconnect (struct GNUNET_NAMESTORE_Handle *h)
598 struct PendingMessage *p;
599 struct StartMessage *init;
601 GNUNET_assert (NULL == h->client);
602 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
603 GNUNET_assert (NULL != h->client);
605 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
607 p = GNUNET_malloc (sizeof (struct PendingMessage) +
608 sizeof (struct StartMessage));
609 p->size = sizeof (struct StartMessage);
610 p->is_init = GNUNET_YES;
611 init = (struct StartMessage *) &p[1];
612 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
613 init->header.size = htons (sizeof (struct StartMessage));
614 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
620 * Re-establish the connection to the service.
622 * @param cls handle to use to re-connect.
623 * @param tc scheduler context
626 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
628 struct GNUNET_NAMESTORE_Handle *h = cls;
630 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
636 * Disconnect from service and then reconnect.
638 * @param h our handle
641 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
643 h->reconnect = GNUNET_NO;
644 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
646 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
652 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
654 uint32_t op_id = h->op_id;
660 * Initialize the connection with the NAMESTORE service.
662 * @param cfg configuration to use
663 * @return handle to the GNS service, or NULL on error
665 struct GNUNET_NAMESTORE_Handle *
666 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
668 struct GNUNET_NAMESTORE_Handle *h;
670 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
672 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
679 * Disconnect from the namestore service (and free associated
682 * @param h handle to the namestore
683 * @param drop set to GNUNET_YES to delete all data in namestore (!)
686 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
688 struct PendingMessage *p;
689 struct GNUNET_NAMESTORE_QueueEntry *q;
690 struct GNUNET_NAMESTORE_ZoneIterator *z;
692 GNUNET_assert (h != NULL);
694 while (NULL != (p = h->pending_head))
696 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
700 while (NULL != (q = h->op_head))
702 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
706 while (NULL != (z = h->z_head))
708 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
712 if (NULL != h->client)
714 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
717 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
719 GNUNET_SCHEDULER_cancel (h->reconnect_task);
720 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
728 * Store an item in the namestore. If the item is already present,
729 * the expiration time is updated to the max of the existing time and
730 * the new time. This API is used when we cache signatures from other
733 * @param h handle to the namestore
734 * @param zone_key public key of the zone
735 * @param name name that is being mapped (at most 255 characters long)
736 * @param expire when does the corresponding block in the DHT expire (until
737 * when should we never do a DHT lookup for the same name again)?
738 * @param rd_count number of entries in 'rd' array
739 * @param rd array of records with data to store
740 * @param signature signature for all the records in the zone under the given name
741 * @param cont continuation to call when done
742 * @param cont_cls closure for cont
743 * @return handle to abort the request
745 struct GNUNET_NAMESTORE_QueueEntry *
746 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
747 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
749 struct GNUNET_TIME_Absolute expire,
750 unsigned int rd_count,
751 const struct GNUNET_NAMESTORE_RecordData *rd,
752 const struct GNUNET_CRYPTO_RsaSignature *signature,
753 GNUNET_NAMESTORE_ContinuationWithStatus cont,
756 struct GNUNET_NAMESTORE_QueueEntry *qe;
757 struct PendingMessage *pe;
759 /* pointer to elements */
767 size_t rd_ser_len = 0;
768 size_t pubkey_len = 0;
771 GNUNET_assert (NULL != h);
772 GNUNET_assert (NULL != zone_key);
773 GNUNET_assert (NULL != name);
774 GNUNET_assert (NULL != rd);
775 GNUNET_assert (NULL != signature);
777 name_len = strlen(name) + 1;
785 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
788 qe->cont_cls = cont_cls;
790 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
793 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, rd_count, rd);
794 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
795 struct RecordPutMessage * msg;
796 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
798 /* create msg here */
799 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
801 pe->is_init = GNUNET_NO;
802 msg = (struct RecordPutMessage *) &pe[1];
803 zone_key_tmp = (char *) &msg[1];
804 name_tmp = (char *) &zone_key_tmp[pubkey_len];
805 rd_tmp = &name_tmp[name_len];
807 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
808 msg->gns_header.header.size = htons (msg_size);
809 msg->gns_header.r_id = htonl (rid);
810 msg->key_len = htons (pubkey_len);
811 memcpy (zone_key_tmp, zone_key, pubkey_len);
812 msg->signature = *signature;
813 msg->name_len = htons (name_len);
814 memcpy (name_tmp, name, name_len);
815 msg->expire = GNUNET_TIME_absolute_hton (expire);
816 msg->rd_len = htons (rd_ser_len);
819 memcpy (rd_tmp, rd_ser, rd_ser_len);
820 GNUNET_free (rd_ser);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
824 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
832 * Check if a signature is valid. This API is used by the GNS Block
833 * to validate signatures received from the network.
835 * @param public_key public key of the zone
836 * @param name name that is being mapped (at most 255 characters long)
837 * @param rd_count number of entries in 'rd' array
838 * @param rd array of records with data to store
839 * @param signature signature for all the records in the zone under the given name
840 * @return GNUNET_OK if the signature is valid
843 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
845 unsigned int rd_count,
846 const struct GNUNET_NAMESTORE_RecordData *rd,
847 const struct GNUNET_CRYPTO_RsaSignature *signature)
849 return GNUNET_SYSERR;
853 * Store an item in the namestore. If the item is already present,
854 * the expiration time is updated to the max of the existing time and
855 * the new time. This API is used by the authority of a zone.
857 * @param h handle to the namestore
858 * @param pkey private key of the zone
859 * @param name name that is being mapped (at most 255 characters long)
860 * @param rd record data to store
861 * @param cont continuation to call when done
862 * @param cont_cls closure for cont
863 * @return handle to abort the request
865 struct GNUNET_NAMESTORE_QueueEntry *
866 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
867 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
869 const struct GNUNET_NAMESTORE_RecordData *rd,
870 GNUNET_NAMESTORE_ContinuationWithStatus cont,
873 struct GNUNET_NAMESTORE_QueueEntry *qe;
874 struct PendingMessage *pe;
879 size_t rd_ser_len = 0;
885 GNUNET_assert (NULL != h);
886 GNUNET_assert (NULL != pkey);
887 GNUNET_assert (NULL != name);
888 GNUNET_assert (NULL != rd);
890 name_len = strlen(name) + 1;
898 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
901 qe->cont_cls = cont_cls;
903 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
906 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
907 GNUNET_assert (pkey_enc != NULL);
908 key_len = ntohs (pkey_enc->len);
909 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, 1, rd);
910 struct RecordCreateMessage * msg;
911 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
913 /* create msg here */
914 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
916 pe->is_init = GNUNET_NO;
917 msg = (struct RecordCreateMessage *) &pe[1];
919 pkey_tmp = (char *) &msg[1];
920 name_tmp = &pkey_tmp[key_len];
921 rd_tmp = &name_tmp[name_len];
923 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
924 msg->gns_header.header.size = htons (msg_size);
925 msg->gns_header.r_id = htonl (rid);
926 msg->name_len = htons (name_len);
927 msg->rd_len = htons (rd_ser_len);
928 msg->pkey_len = htons (key_len);
929 memcpy (pkey_tmp, pkey_enc, key_len);
930 memcpy (name_tmp, name, name_len);
931 memcpy (rd_tmp, rd_ser, rd_ser_len);
932 GNUNET_free (rd_ser);
933 GNUNET_free (pkey_enc);
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
937 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
944 * Explicitly remove some content from the database. The
945 * "cont"inuation will be called with status "GNUNET_OK" if content
946 * was removed, "GNUNET_NO" if no matching entry was found and
947 * "GNUNET_SYSERR" on all other types of errors.
948 * This API is used by the authority of a zone.
950 * @param h handle to the namestore
951 * @param pkey private key of the zone
952 * @param name name that is being mapped (at most 255 characters long)
953 * @param rd record data
954 * @param cont continuation to call when done
955 * @param cont_cls closure for cont
956 * @return handle to abort the request
958 struct GNUNET_NAMESTORE_QueueEntry *
959 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
960 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
962 const struct GNUNET_NAMESTORE_RecordData *rd,
963 GNUNET_NAMESTORE_ContinuationWithStatus cont,
966 struct GNUNET_NAMESTORE_QueueEntry *qe;
967 struct PendingMessage *pe;
971 size_t rd_ser_len = 0;
976 GNUNET_assert (NULL != h);
979 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
982 qe->cont_cls = cont_cls;
986 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, 1, rd);
987 struct RecordRemoveMessage * msg;
988 msg_size = sizeof (struct RecordRemoveMessage) + name_len + rd_ser_len;
990 /* create msg here */
991 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
993 pe->is_init = GNUNET_NO;
994 msg = (struct RecordRemoveMessage *) &pe[1];
996 name_tmp = (char *) &msg[1];
997 rd_tmp = &name_tmp[name_len];
999 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1000 msg->gns_header.header.size = htons (msg_size);
1001 msg->gns_header.r_id = htonl (rid);
1002 //msg->signature = *signature;
1003 msg->name_len = htons (name_len);
1004 memcpy (name_tmp, name, name_len);
1005 memcpy (rd_tmp, rd_ser, rd_ser_len);
1006 GNUNET_free (rd_ser);
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1010 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1017 * Get a result for a particular key from the namestore. The processor
1018 * will only be called once.
1020 * @param h handle to the namestore
1021 * @param zone zone to look up a record from
1022 * @param name name to look up
1023 * @param record_type desired record type, 0 for all
1024 * @param proc function to call on the matching records, or with
1025 * NULL (rd_count == 0) if there are no matching records
1026 * @param proc_cls closure for proc
1027 * @return a handle that can be used to
1030 struct GNUNET_NAMESTORE_QueueEntry *
1031 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1032 const GNUNET_HashCode *zone,
1034 uint32_t record_type,
1035 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1037 struct GNUNET_NAMESTORE_QueueEntry *qe;
1038 struct PendingMessage *pe;
1039 size_t msg_size = 0;
1040 size_t name_len = 0;
1043 GNUNET_assert (NULL != h);
1044 GNUNET_assert (NULL != zone);
1045 GNUNET_assert (NULL != name);
1047 name_len = strlen (name) + 1;
1048 if ((name_len == 0) || (name_len > 256))
1055 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1058 qe->proc_cls = proc_cls;
1060 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1063 msg_size = sizeof (struct LookupNameMessage) + name_len;
1064 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1066 /* create msg here */
1067 struct LookupNameMessage * msg;
1068 pe->size = msg_size;
1069 pe->is_init = GNUNET_NO;
1070 msg = (struct LookupNameMessage *) &pe[1];
1071 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1072 msg->gns_header.header.size = htons (msg_size);
1073 msg->gns_header.r_id = htonl (rid);
1074 msg->record_type = htonl (record_type);
1076 msg->name_len = htonl (name_len);
1077 memcpy (&msg[1], name, name_len);
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1081 /* transmit message */
1082 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1091 * Starts a new zone iteration (used to periodically PUT all of our
1092 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1093 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1094 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1095 * immediately, and then again after
1096 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1098 * @param h handle to the namestore
1099 * @param zone zone to access, NULL for all zones
1100 * @param must_have_flags flags that must be set for the record to be returned
1101 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1102 * @param proc function to call on each name from the zone; it
1103 * will be called repeatedly with a value (if available)
1104 * and always once at the end with a name of NULL.
1105 * @param proc_cls closure for proc
1106 * @return an iterator handle to use for iteration
1108 struct GNUNET_NAMESTORE_ZoneIterator *
1109 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1110 const GNUNET_HashCode *zone,
1111 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1112 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1113 GNUNET_NAMESTORE_RecordProcessor proc,
1116 struct GNUNET_NAMESTORE_ZoneIterator *it;
1117 struct PendingMessage *pe;
1118 size_t msg_size = 0;
1121 GNUNET_assert (NULL != h);
1122 GNUNET_assert (NULL != zone);
1125 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1128 it->proc_cls = proc;
1131 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1134 msg_size = sizeof (struct ZoneIterationStartMessage);
1135 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1137 /* create msg here */
1138 struct ZoneIterationStartMessage * msg;
1139 pe->size = msg_size;
1140 pe->is_init = GNUNET_NO;
1141 msg = (struct ZoneIterationStartMessage *) &pe[1];
1142 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1143 msg->gns_header.header.size = htons (msg_size);
1144 msg->gns_header.r_id = htonl (rid);
1146 msg->must_have_flags = ntohs (must_have_flags);
1147 msg->must_not_have_flags = ntohs (must_not_have_flags);
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1151 /* transmit message */
1152 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1160 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1161 * for the next record.
1163 * @param it the iterator
1166 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1168 struct GNUNET_NAMESTORE_Handle *h;
1169 struct PendingMessage *pe;
1170 size_t msg_size = 0;
1172 GNUNET_assert (NULL != it);
1176 msg_size = sizeof (struct ZoneIterationNextMessage);
1177 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1179 /* create msg here */
1180 struct ZoneIterationNextMessage * msg;
1181 pe->size = msg_size;
1182 pe->is_init = GNUNET_NO;
1183 msg = (struct ZoneIterationNextMessage *) &pe[1];
1184 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1185 msg->gns_header.header.size = htons (msg_size);
1186 msg->gns_header.r_id = htonl (it->op_id);
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1190 /* transmit message */
1191 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1197 * Stops iteration and releases the namestore handle for further calls.
1199 * @param it the iterator
1202 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1204 GNUNET_assert (NULL != it);
1205 struct PendingMessage *pe;
1206 size_t msg_size = 0;
1207 struct GNUNET_NAMESTORE_Handle *h = it->h;
1210 msg_size = sizeof (struct ZoneIterationStopMessage);
1211 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1213 /* create msg here */
1214 struct ZoneIterationStopMessage * msg;
1215 pe->size = msg_size;
1216 pe->is_init = GNUNET_NO;
1217 msg = (struct ZoneIterationStopMessage *) &pe[1];
1218 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1219 msg->gns_header.header.size = htons (msg_size);
1220 msg->gns_header.r_id = htonl (it->op_id);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1224 /* transmit message */
1225 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1231 * Cancel a namestore operation. The final callback from the
1232 * operation must not have been done yet.
1234 * @param qe operation to cancel
1237 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1239 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1241 GNUNET_assert (qe != NULL);
1243 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1248 /* end of namestore_api.c */