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_signatures.h"
34 #include "gnunet_namestore_service.h"
35 #include "namestore.h"
37 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
39 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
44 struct GNUNET_NAMESTORE_QueueEntry
46 struct GNUNET_NAMESTORE_QueueEntry *next;
47 struct GNUNET_NAMESTORE_QueueEntry *prev;
49 struct GNUNET_NAMESTORE_Handle *nsh;
53 GNUNET_NAMESTORE_ContinuationWithStatus cont;
56 GNUNET_NAMESTORE_RecordProcessor proc;
59 char *data; /*stub data pointer*/
66 struct GNUNET_NAMESTORE_ZoneIterator
68 struct GNUNET_NAMESTORE_ZoneIterator *next;
69 struct GNUNET_NAMESTORE_ZoneIterator *prev;
73 struct GNUNET_NAMESTORE_Handle *h;
74 GNUNET_NAMESTORE_RecordProcessor proc;
83 * Message in linked list we should send to the service. The
84 * actual binary message follows this struct.
92 struct PendingMessage *next;
97 struct PendingMessage *prev;
100 * Size of the message.
105 * Is this the 'START' message?
112 * Connection to the NAMESTORE service.
114 struct GNUNET_NAMESTORE_Handle
118 * Configuration to use.
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Socket (if available).
125 struct GNUNET_CLIENT_Connection *client;
128 * Currently pending transmission request (or NULL).
130 struct GNUNET_CLIENT_TransmitHandle *th;
135 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
138 * Pending messages to send to the service
141 struct PendingMessage * pending_head;
142 struct PendingMessage * pending_tail;
145 * Should we reconnect to service due to some serious error?
151 * Pending namestore queue entries
153 struct GNUNET_NAMESTORE_QueueEntry * op_head;
154 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
159 * Pending namestore zone iterator entries
161 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
162 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
165 struct GNUNET_NAMESTORE_SimpleRecord
170 struct GNUNET_NAMESTORE_SimpleRecord *next;
175 struct GNUNET_NAMESTORE_SimpleRecord *prev;
178 const GNUNET_HashCode *zone;
179 uint32_t record_type;
180 struct GNUNET_TIME_Absolute expiration;
181 enum GNUNET_NAMESTORE_RecordFlags flags;
188 * Disconnect from service and then reconnect.
190 * @param h our handle
193 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
196 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
197 struct LookupNameResponseMessage * msg,
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
201 "LOOKUP_NAME_RESPONSE");
203 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
208 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
209 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
210 struct GNUNET_TIME_Absolute expire;
215 int contains_sig = GNUNET_NO;
218 rd_len = ntohs (msg->rd_len);
219 rd_count = ntohs (msg->rd_count);
220 msg_len = ntohs (msg->gns_header.header.size);
221 name_len = ntohs (msg->name_len);
222 contains_sig = ntohs (msg->contains_sig);
223 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
225 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
226 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
229 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
231 if (msg_len != exp_msg_len)
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
234 msg_len, exp_msg_len);
239 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
240 name = (char *) &zone_key[1];
241 rd_tmp = &name[name_len];
243 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
244 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
246 /* reset values if values not contained */
247 if (contains_sig == GNUNET_NO)
250 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
254 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
255 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
258 if (qe->proc != NULL)
260 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
263 /* Operation done, remove */
264 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
270 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
271 struct RecordPutResponseMessage* msg,
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
275 "RECORD_PUT_RESPONSE");
277 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
280 if (ntohs (msg->op_result) == GNUNET_OK)
283 if (qe->cont != NULL)
285 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
289 else if (ntohs (msg->op_result) == GNUNET_NO)
292 if (qe->cont != NULL)
294 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
303 /* Operation done, remove */
304 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
311 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
312 struct RecordCreateResponseMessage* msg,
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
316 "RECORD_CREATE_RESPONSE");
318 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
321 "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
322 if (ntohs (msg->op_result) == GNUNET_YES)
324 if (qe->cont != NULL)
326 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
330 else if (ntohs (msg->op_result) == GNUNET_NO)
332 if (qe->cont != NULL)
334 qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
339 if (qe->cont != NULL)
341 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
345 /* Operation done, remove */
346 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
353 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
354 struct RecordRemoveResponseMessage* msg,
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
358 "RECORD_REMOVE_RESPONSE");
360 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
361 int res = ntohs (msg->op_result);
366 * 1 : No records for entry
367 * 2 : Could not find record to remove
368 * 3 : Failed to create new signature
369 * 4 : Failed to put new set of records in database
373 if (qe->cont != NULL)
375 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
380 if (qe->cont != NULL)
382 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
387 if (qe->cont != NULL)
389 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
394 if (qe->cont != NULL)
396 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
401 if (qe->cont != NULL)
403 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
411 /* Operation done, remove */
412 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
419 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
420 const struct GNUNET_MessageHeader *msg,
421 int type, size_t size)
424 /* handle different message type */
426 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
427 if (size < sizeof (struct LookupNameResponseMessage))
432 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
434 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
435 if (size != sizeof (struct RecordPutResponseMessage))
440 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
442 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
443 if (size != sizeof (struct RecordCreateResponseMessage))
448 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
450 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
451 if (size != sizeof (struct RecordRemoveResponseMessage))
456 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
465 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
466 struct ZoneIterationResponseMessage *msg,
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
470 "ZONE_ITERATION_RESPONSE");
473 if (ze->proc != NULL)
476 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
482 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
483 const struct GNUNET_MessageHeader *msg,
484 int type, size_t size)
487 /* handle different message type */
489 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
490 if (size < sizeof (struct ZoneIterationResponseMessage))
495 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
504 * Type of a function to call when we receive a message
507 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
508 * @param msg message received, NULL on timeout or fatal error
511 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
513 struct GNUNET_NAMESTORE_Handle *h = cls;
514 struct GNUNET_NAMESTORE_Header * gm;
515 struct GNUNET_NAMESTORE_QueueEntry *qe;
516 struct GNUNET_NAMESTORE_ZoneIterator *ze;
519 uint32_t r_id = UINT32_MAX;
527 size = ntohs (msg->size);
528 type = ntohs (msg->type);
530 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
533 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
534 GNUNET_TIME_UNIT_FOREVER_REL);
538 gm = (struct GNUNET_NAMESTORE_Header *) msg;
539 r_id = ntohl (gm->r_id);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
543 /* Find matching operation */
546 /* No matching pending operation found */
548 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
549 GNUNET_TIME_UNIT_FOREVER_REL);
553 /* Is it a record related operation ? */
554 for (qe = h->op_head; qe != NULL; qe = qe->next)
556 if (qe->op_id == r_id)
561 manage_record_operations (qe, msg, type, size);
564 /* Is it a zone iteration operation ? */
565 for (ze = h->z_head; ze != NULL; ze = ze->next)
567 if (ze->op_id == r_id)
572 manage_zone_operations (ze, msg, type, size);
575 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
576 GNUNET_TIME_UNIT_FOREVER_REL);
578 if (GNUNET_YES == h->reconnect)
585 * Transmit messages from the message queue to the service
586 * (if there are any, and if we are not already trying).
588 * @param h handle to use
591 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
595 * We can now transmit a message to NAMESTORE. Do it.
597 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
598 * @param size number of bytes we can transmit
599 * @param buf where to copy the messages
600 * @return number of bytes copied into buf
603 transmit_message_to_namestore (void *cls, size_t size, void *buf)
605 struct GNUNET_NAMESTORE_Handle *h = cls;
606 struct PendingMessage *p;
611 if ((size == 0) || (buf == NULL))
618 while ((NULL != (p = h->pending_head)) && (p->size <= size))
620 memcpy (&cbuf[ret], &p[1], p->size);
623 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
624 if (GNUNET_YES == p->is_init)
625 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
626 GNUNET_TIME_UNIT_FOREVER_REL);
635 * Transmit messages from the message queue to the service
636 * (if there are any, and if we are not already trying).
638 * @param h handle to use
641 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
643 struct PendingMessage *p;
647 if (NULL == (p = h->pending_head))
649 if (NULL == h->client)
650 return; /* currently reconnecting */
652 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
653 GNUNET_TIME_UNIT_FOREVER_REL,
654 GNUNET_NO, &transmit_message_to_namestore,
660 * Reconnect to namestore service.
662 * @param h the handle to the namestore service
665 reconnect (struct GNUNET_NAMESTORE_Handle *h)
667 struct PendingMessage *p;
668 struct StartMessage *init;
670 GNUNET_assert (NULL == h->client);
671 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
672 GNUNET_assert (NULL != h->client);
674 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
676 p = GNUNET_malloc (sizeof (struct PendingMessage) +
677 sizeof (struct StartMessage));
678 p->size = sizeof (struct StartMessage);
679 p->is_init = GNUNET_YES;
680 init = (struct StartMessage *) &p[1];
681 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
682 init->header.size = htons (sizeof (struct StartMessage));
683 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
689 * Re-establish the connection to the service.
691 * @param cls handle to use to re-connect.
692 * @param tc scheduler context
695 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
697 struct GNUNET_NAMESTORE_Handle *h = cls;
699 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
705 * Disconnect from service and then reconnect.
707 * @param h our handle
710 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
712 h->reconnect = GNUNET_NO;
713 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
715 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
721 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
723 uint32_t op_id = h->op_id;
729 * Initialize the connection with the NAMESTORE service.
731 * @param cfg configuration to use
732 * @return handle to the GNS service, or NULL on error
734 struct GNUNET_NAMESTORE_Handle *
735 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
737 struct GNUNET_NAMESTORE_Handle *h;
739 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
741 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
746 struct DisconnectContext
748 struct GNUNET_NAMESTORE_Handle *h;
753 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
755 struct PendingMessage *p;
756 struct GNUNET_NAMESTORE_QueueEntry *q;
757 struct GNUNET_NAMESTORE_ZoneIterator *z;
758 struct GNUNET_NAMESTORE_Handle *h = cls;
759 GNUNET_assert (h != NULL);
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
761 while (NULL != (p = h->pending_head))
763 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
767 while (NULL != (q = h->op_head))
770 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
774 while (NULL != (z = h->z_head))
776 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
780 if (NULL != h->client)
782 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
785 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
787 GNUNET_SCHEDULER_cancel (h->reconnect_task);
788 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
795 transmit_disconnect_to_namestore (void *cls, size_t size, void *buf)
797 struct DisconnectContext * d_ctx = cls;
798 struct DisconnectMessage d_msg;
799 struct GNUNET_NAMESTORE_Handle *h = d_ctx->h;
802 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_DISCONNECT);
803 d_msg.header.size = htons (sizeof (struct DisconnectMessage));
804 d_msg.drop = htonl (d_ctx->drop);
807 if ((size == 0) || (buf == NULL) || (size < sizeof (struct DisconnectMessage)))
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to service \n", "NAMESTORE_DISCONNECT");
815 memcpy (buf, &d_msg, sizeof (struct DisconnectMessage));
816 res = sizeof (struct DisconnectMessage);
819 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
825 * Disconnect from the namestore service (and free associated
828 * @param h handle to the namestore
829 * @param drop set to GNUNET_YES to delete all data in namestore (!)
832 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
836 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
839 if (h->client != NULL)
841 struct DisconnectContext *d_ctx = GNUNET_malloc (sizeof (struct DisconnectContext));
845 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, sizeof (struct DisconnectMessage),
846 GNUNET_TIME_UNIT_FOREVER_REL,
847 GNUNET_NO, &transmit_disconnect_to_namestore,
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not send disconnect notification to namestore service, we are not connected!\n");
853 if (GNUNET_YES == drop)
854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NAMESTORE will not drop content\n");
855 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
861 * Store an item in the namestore. If the item is already present,
862 * the expiration time is updated to the max of the existing time and
863 * the new time. This API is used when we cache signatures from other
866 * @param h handle to the namestore
867 * @param zone_key public key of the zone
868 * @param name name that is being mapped (at most 255 characters long)
869 * @param expire when does the corresponding block in the DHT expire (until
870 * when should we never do a DHT lookup for the same name again)?
871 * @param rd_count number of entries in 'rd' array
872 * @param rd array of records with data to store
873 * @param signature signature for all the records in the zone under the given name
874 * @param cont continuation to call when done
875 * @param cont_cls closure for cont
876 * @return handle to abort the request
878 struct GNUNET_NAMESTORE_QueueEntry *
879 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
880 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
882 struct GNUNET_TIME_Absolute expire,
883 unsigned int rd_count,
884 const struct GNUNET_NAMESTORE_RecordData *rd,
885 const struct GNUNET_CRYPTO_RsaSignature *signature,
886 GNUNET_NAMESTORE_ContinuationWithStatus cont,
889 struct GNUNET_NAMESTORE_QueueEntry *qe;
890 struct PendingMessage *pe;
892 /* pointer to elements */
899 size_t rd_ser_len = 0;
900 size_t pubkey_len = 0;
903 GNUNET_assert (NULL != h);
904 GNUNET_assert (NULL != zone_key);
905 GNUNET_assert (NULL != name);
906 GNUNET_assert (NULL != rd);
907 GNUNET_assert (NULL != signature);
909 name_len = strlen(name) + 1;
917 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
920 qe->cont_cls = cont_cls;
922 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
925 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
926 char rd_ser[rd_ser_len];
927 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
929 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
930 struct RecordPutMessage * msg;
931 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
933 /* create msg here */
934 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
936 pe->is_init = GNUNET_NO;
937 msg = (struct RecordPutMessage *) &pe[1];
938 zone_key_tmp = (char *) &msg[1];
939 name_tmp = (char *) &zone_key_tmp[pubkey_len];
940 rd_tmp = &name_tmp[name_len];
942 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
943 msg->gns_header.header.size = htons (msg_size);
944 msg->gns_header.r_id = htonl (rid);
945 msg->key_len = htons (pubkey_len);
946 memcpy (zone_key_tmp, zone_key, pubkey_len);
947 msg->signature = *signature;
948 msg->name_len = htons (name_len);
949 memcpy (name_tmp, name, name_len);
950 msg->expire = GNUNET_TIME_absolute_hton (expire);
951 msg->rd_len = htons (rd_ser_len);
952 msg->rd_count = htons (rd_count);
954 memcpy (rd_tmp, rd_ser, rd_ser_len);
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
958 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
966 * Check if a signature is valid. This API is used by the GNS Block
967 * to validate signatures received from the network.
969 * @param public_key public key of the zone
970 * @param name name that is being mapped (at most 255 characters long)
971 * @param rd_count number of entries in 'rd' array
972 * @param rd array of records with data to store
973 * @param signature signature for all the records in the zone under the given name
974 * @return GNUNET_OK if the signature is valid
977 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
979 unsigned int rd_count,
980 const struct GNUNET_NAMESTORE_RecordData *rd,
981 const struct GNUNET_CRYPTO_RsaSignature *signature)
983 int res = GNUNET_SYSERR;
984 size_t rd_ser_len = 0;
988 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
990 GNUNET_assert (public_key != NULL);
991 GNUNET_assert (name != NULL);
992 GNUNET_assert (signature != NULL);
994 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
995 char rd_ser[rd_ser_len];
996 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
998 name_len = strlen (name) + 1;
1000 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1001 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1002 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1003 name_tmp = (char *) &sig_purpose[1];
1004 rd_tmp = &name_tmp[name_len];
1005 memcpy (name_tmp, name, name_len);
1006 memcpy (rd_tmp, rd_ser, rd_ser_len);
1008 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1010 GNUNET_free (sig_purpose);
1016 * Store an item in the namestore. If the item is already present,
1017 * the expiration time is updated to the max of the existing time and
1018 * the new time. This API is used by the authority of a zone.
1020 * @param h handle to the namestore
1021 * @param pkey private key of the zone
1022 * @param name name that is being mapped (at most 255 characters long)
1023 * @param rd record data to store
1024 * @param cont continuation to call when done
1025 * @param cont_cls closure for cont
1026 * @return handle to abort the request
1028 struct GNUNET_NAMESTORE_QueueEntry *
1029 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1030 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1032 const struct GNUNET_NAMESTORE_RecordData *rd,
1033 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1036 struct GNUNET_NAMESTORE_QueueEntry *qe;
1037 struct PendingMessage *pe;
1041 size_t rd_ser_len = 0;
1042 size_t msg_size = 0;
1043 size_t name_len = 0;
1047 GNUNET_assert (NULL != h);
1048 GNUNET_assert (NULL != pkey);
1049 GNUNET_assert (NULL != name);
1050 GNUNET_assert (NULL != rd);
1052 name_len = strlen(name) + 1;
1060 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1063 qe->cont_cls = cont_cls;
1065 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1068 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1069 GNUNET_assert (pkey_enc != NULL);
1070 key_len = ntohs (pkey_enc->len);
1072 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1073 char rd_ser[rd_ser_len];
1074 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1076 struct RecordCreateMessage * msg;
1077 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1079 /* create msg here */
1080 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1081 pe->size = msg_size;
1082 pe->is_init = GNUNET_NO;
1083 msg = (struct RecordCreateMessage *) &pe[1];
1085 pkey_tmp = (char *) &msg[1];
1086 name_tmp = &pkey_tmp[key_len];
1087 rd_tmp = &name_tmp[name_len];
1089 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1090 msg->gns_header.header.size = htons (msg_size);
1091 msg->gns_header.r_id = htonl (rid);
1092 msg->name_len = htons (name_len);
1093 msg->rd_count = htons (1);
1094 msg->rd_len = htons (rd_ser_len);
1095 msg->pkey_len = htons (key_len);
1096 memcpy (pkey_tmp, pkey_enc, key_len);
1097 memcpy (name_tmp, name, name_len);
1098 memcpy (rd_tmp, rd_ser, rd_ser_len);
1099 GNUNET_free (pkey_enc);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1103 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1110 * Explicitly remove some content from the database. The
1111 * "cont"inuation will be called with status "GNUNET_OK" if content
1112 * was removed, "GNUNET_NO" if no matching entry was found and
1113 * "GNUNET_SYSERR" on all other types of errors.
1114 * This API is used by the authority of a zone.
1116 * @param h handle to the namestore
1117 * @param pkey private key of the zone
1118 * @param name name that is being mapped (at most 255 characters long)
1119 * @param rd record data
1120 * @param cont continuation to call when done
1121 * @param cont_cls closure for cont
1122 * @return handle to abort the request
1124 struct GNUNET_NAMESTORE_QueueEntry *
1125 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1126 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1128 const struct GNUNET_NAMESTORE_RecordData *rd,
1129 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1132 struct GNUNET_NAMESTORE_QueueEntry *qe;
1133 struct PendingMessage *pe;
1137 size_t rd_ser_len = 0;
1138 size_t msg_size = 0;
1139 size_t name_len = 0;
1143 GNUNET_assert (NULL != h);
1146 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1149 qe->cont_cls = cont_cls;
1151 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1154 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1155 GNUNET_assert (pkey_enc != NULL);
1156 key_len = ntohs (pkey_enc->len);
1158 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1159 char rd_ser[rd_ser_len];
1160 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1162 name_len = strlen (name) + 1;
1164 struct RecordRemoveMessage * msg;
1165 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1167 /* create msg here */
1168 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1169 pe->size = msg_size;
1170 pe->is_init = GNUNET_NO;
1171 msg = (struct RecordRemoveMessage *) &pe[1];
1173 pkey_tmp = (char *) &msg[1];
1174 name_tmp = &pkey_tmp[key_len];
1175 rd_tmp = &name_tmp[name_len];
1177 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1178 msg->gns_header.header.size = htons (msg_size);
1179 msg->gns_header.r_id = htonl (rid);
1180 msg->name_len = htons (name_len);
1181 msg->rd_len = htons (rd_ser_len);
1182 msg->rd_count = htons (1);
1183 msg->key_len = htons (key_len);
1184 memcpy (pkey_tmp, pkey_enc, key_len);
1185 memcpy (name_tmp, name, name_len);
1186 memcpy (rd_tmp, rd_ser, rd_ser_len);
1188 GNUNET_free (pkey_enc);
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1192 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1199 * Get a result for a particular key from the namestore. The processor
1200 * will only be called once.
1202 * @param h handle to the namestore
1203 * @param zone zone to look up a record from
1204 * @param name name to look up
1205 * @param record_type desired record type, 0 for all
1206 * @param proc function to call on the matching records, or with
1207 * NULL (rd_count == 0) if there are no matching records
1208 * @param proc_cls closure for proc
1209 * @return a handle that can be used to
1212 struct GNUNET_NAMESTORE_QueueEntry *
1213 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1214 const GNUNET_HashCode *zone,
1216 uint32_t record_type,
1217 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1219 struct GNUNET_NAMESTORE_QueueEntry *qe;
1220 struct PendingMessage *pe;
1221 size_t msg_size = 0;
1222 size_t name_len = 0;
1225 GNUNET_assert (NULL != h);
1226 GNUNET_assert (NULL != zone);
1227 GNUNET_assert (NULL != name);
1229 name_len = strlen (name) + 1;
1230 if ((name_len == 0) || (name_len > 256))
1237 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1240 qe->proc_cls = proc_cls;
1242 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1245 msg_size = sizeof (struct LookupNameMessage) + name_len;
1246 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1248 /* create msg here */
1249 struct LookupNameMessage * msg;
1250 pe->size = msg_size;
1251 pe->is_init = GNUNET_NO;
1252 msg = (struct LookupNameMessage *) &pe[1];
1253 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1254 msg->gns_header.header.size = htons (msg_size);
1255 msg->gns_header.r_id = htonl (rid);
1256 msg->record_type = htonl (record_type);
1258 msg->name_len = htonl (name_len);
1259 memcpy (&msg[1], name, name_len);
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1263 /* transmit message */
1264 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1273 * Starts a new zone iteration (used to periodically PUT all of our
1274 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1275 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1276 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1277 * immediately, and then again after
1278 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1280 * @param h handle to the namestore
1281 * @param zone zone to access, NULL for all zones
1282 * @param must_have_flags flags that must be set for the record to be returned
1283 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1284 * @param proc function to call on each name from the zone; it
1285 * will be called repeatedly with a value (if available)
1286 * and always once at the end with a name of NULL.
1287 * @param proc_cls closure for proc
1288 * @return an iterator handle to use for iteration
1290 struct GNUNET_NAMESTORE_ZoneIterator *
1291 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1292 const GNUNET_HashCode *zone,
1293 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1294 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1295 GNUNET_NAMESTORE_RecordProcessor proc,
1298 struct GNUNET_NAMESTORE_ZoneIterator *it;
1299 struct PendingMessage *pe;
1300 size_t msg_size = 0;
1303 GNUNET_assert (NULL != h);
1304 GNUNET_assert (NULL != zone);
1307 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1310 it->proc_cls = proc;
1313 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1316 msg_size = sizeof (struct ZoneIterationStartMessage);
1317 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1319 /* create msg here */
1320 struct ZoneIterationStartMessage * msg;
1321 pe->size = msg_size;
1322 pe->is_init = GNUNET_NO;
1323 msg = (struct ZoneIterationStartMessage *) &pe[1];
1324 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1325 msg->gns_header.header.size = htons (msg_size);
1326 msg->gns_header.r_id = htonl (rid);
1328 msg->must_have_flags = ntohs (must_have_flags);
1329 msg->must_not_have_flags = ntohs (must_not_have_flags);
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1333 /* transmit message */
1334 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1342 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1343 * for the next record.
1345 * @param it the iterator
1348 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1350 struct GNUNET_NAMESTORE_Handle *h;
1351 struct PendingMessage *pe;
1352 size_t msg_size = 0;
1354 GNUNET_assert (NULL != it);
1358 msg_size = sizeof (struct ZoneIterationNextMessage);
1359 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1361 /* create msg here */
1362 struct ZoneIterationNextMessage * msg;
1363 pe->size = msg_size;
1364 pe->is_init = GNUNET_NO;
1365 msg = (struct ZoneIterationNextMessage *) &pe[1];
1366 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1367 msg->gns_header.header.size = htons (msg_size);
1368 msg->gns_header.r_id = htonl (it->op_id);
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1372 /* transmit message */
1373 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1379 * Stops iteration and releases the namestore handle for further calls.
1381 * @param it the iterator
1384 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1386 GNUNET_assert (NULL != it);
1387 struct PendingMessage *pe;
1388 size_t msg_size = 0;
1389 struct GNUNET_NAMESTORE_Handle *h = it->h;
1392 msg_size = sizeof (struct ZoneIterationStopMessage);
1393 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1395 /* create msg here */
1396 struct ZoneIterationStopMessage * msg;
1397 pe->size = msg_size;
1398 pe->is_init = GNUNET_NO;
1399 msg = (struct ZoneIterationStopMessage *) &pe[1];
1400 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1401 msg->gns_header.header.size = htons (msg_size);
1402 msg->gns_header.r_id = htonl (it->op_id);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1406 /* transmit message */
1407 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1413 * Cancel a namestore operation. The final callback from the
1414 * operation must not have been done yet.
1416 * @param qe operation to cancel
1419 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1421 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1423 GNUNET_assert (qe != NULL);
1425 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1430 /* end of namestore_api.c */