2 This file is part of GNUnet.
3 (C) 2009 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/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "gnunet_signatures.h"
32 #include "namestore.h"
37 * A namestore operation.
39 struct GNUNET_NAMESTORE_ZoneIteration
41 struct GNUNET_NAMESTORE_ZoneIteration *next;
42 struct GNUNET_NAMESTORE_ZoneIteration *prev;
44 struct GNUNET_NAMESTORE_Client * client;
57 struct GNUNET_NAMESTORE_Client
59 struct GNUNET_NAMESTORE_Client *next;
60 struct GNUNET_NAMESTORE_Client *prev;
62 struct GNUNET_SERVER_Client * client;
64 struct GNUNET_NAMESTORE_ZoneIteration *op_head;
65 struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
71 * Configuration handle.
73 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
75 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
78 * Our notification context.
80 static struct GNUNET_SERVER_NotificationContext *snc;
82 static char *db_lib_name;
84 static struct GNUNET_NAMESTORE_Client *client_head;
85 static struct GNUNET_NAMESTORE_Client *client_tail;
89 * Task run during shutdown.
95 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
99 struct GNUNET_NAMESTORE_ZoneIteration * no;
100 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
101 struct GNUNET_NAMESTORE_Client * nc;
102 struct GNUNET_NAMESTORE_Client * next;
104 GNUNET_SERVER_notification_context_destroy (snc);
107 for (nc = client_head; nc != NULL; nc = next)
110 for (no = nc->op_head; no != NULL; no = tmp)
113 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
117 GNUNET_SERVER_client_drop(nc->client);
118 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
122 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
123 GNUNET_free (db_lib_name);
126 static struct GNUNET_NAMESTORE_Client *
127 client_lookup (struct GNUNET_SERVER_Client *client)
129 struct GNUNET_NAMESTORE_Client * nc;
131 GNUNET_assert (NULL != client);
133 for (nc = client_head; nc != NULL; nc = nc->next)
135 if (client == nc->client)
143 * Called whenever a client is disconnected. Frees our
144 * resources associated with that client.
147 * @param client identification of the client
150 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
152 struct GNUNET_NAMESTORE_ZoneIteration * no;
153 struct GNUNET_NAMESTORE_Client * nc;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
159 nc = client_lookup (client);
161 if ((NULL == client) || (NULL == nc))
164 for (no = nc->op_head; no != NULL; no = no->next)
166 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
170 GNUNET_SERVER_client_drop(nc->client);
171 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
175 static void handle_start (void *cls,
176 struct GNUNET_SERVER_Client * client,
177 const struct GNUNET_MessageHeader * message)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
181 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
183 GNUNET_SERVER_notification_context_add (snc, client);
184 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
185 GNUNET_SERVER_client_keep (client);
186 GNUNET_SERVER_receive_done (client, GNUNET_OK);
189 struct LookupNameContext
191 struct GNUNET_NAMESTORE_Client *nc;
193 uint32_t record_type;
194 GNUNET_HashCode *zone;
202 handle_lookup_name_it (void *cls,
203 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
204 struct GNUNET_TIME_Absolute expire,
206 unsigned int rd_count,
207 const struct GNUNET_NAMESTORE_RecordData *rd,
208 const struct GNUNET_CRYPTO_RsaSignature *signature)
211 struct LookupNameContext *lnc = cls;
212 struct LookupNameResponseMessage *lnr_msg;
214 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
215 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
219 struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
227 name_len = strlen(name) + 1;
229 int copied_elements = 0;
230 int contains_signature = 0;
233 /* count records to copy */
236 if (lnc->record_type != 0)
238 /* special record type needed */
239 for (c = 0; c < rd_count; c ++)
240 if (rd[c].record_type == lnc->record_type)
241 copied_elements++; /* found matching record */
242 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
244 for (c = 0; c < rd_count; c ++)
246 if (rd[c].record_type == lnc->record_type)
248 /* found matching record */
249 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
256 copied_elements = rd_count;
257 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
265 expire = GNUNET_TIME_UNIT_ZERO_ABS;
270 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
271 char rd_ser[rd_ser_len];
272 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
275 copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
277 if ((copied_elements == rd_count) && (signature != NULL))
278 contains_signature = GNUNET_YES;
280 if (rd_selected != rd)
281 GNUNET_free (rd_selected);
283 r_size = sizeof (struct LookupNameResponseMessage) +
284 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
287 contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
291 lnr_msg = GNUNET_malloc (r_size);
293 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
294 lnr_msg->gns_header.header.size = ntohs (r_size);
295 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
296 lnr_msg->rd_count = htons (rd_count);
297 lnr_msg->rd_len = htons (rd_ser_len);
298 lnr_msg->name_len = htons (name_len);
299 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
300 lnr_msg->contains_sig = htons (contains_signature);
302 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
303 name_tmp = (char *) &zone_key_tmp[1];
304 rd_tmp = &name_tmp[name_len];
305 signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_ser_len];
307 if (zone_key != NULL)
308 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
311 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
312 memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
313 memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
315 memcpy (name_tmp, name, name_len);
316 memcpy (rd_tmp, rd_ser, rd_ser_len);
318 if (GNUNET_YES == contains_signature)
319 memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
320 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
322 GNUNET_free (lnr_msg);
325 static void handle_lookup_name (void *cls,
326 struct GNUNET_SERVER_Client * client,
327 const struct GNUNET_MessageHeader * message)
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
330 struct LookupNameContext lnc;
331 struct GNUNET_NAMESTORE_Client *nc;
338 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
341 GNUNET_SERVER_receive_done (client, GNUNET_OK);
345 nc = client_lookup(client);
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
353 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
354 rid = ntohl (ln_msg->gns_header.r_id);
355 name_len = ntohl (ln_msg->name_len);
356 type = ntohl (ln_msg->record_type);
358 if ((name_len == 0) || (name_len > 256))
361 GNUNET_SERVER_receive_done (client, GNUNET_OK);
365 name = (char *) &ln_msg[1];
366 if (name[name_len -1] != '\0')
369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
375 /* do the actual lookup */
376 lnc.request_id = rid;
378 lnc.record_type = type;
380 lnc.zone = &ln_msg->zone;
381 res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
383 GNUNET_SERVER_receive_done (client, GNUNET_OK);
386 static void handle_record_put (void *cls,
387 struct GNUNET_SERVER_Client * client,
388 const struct GNUNET_MessageHeader * message)
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
391 struct GNUNET_NAMESTORE_Client *nc;
392 struct GNUNET_TIME_Absolute expire;
393 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
394 struct GNUNET_CRYPTO_RsaSignature *signature;
395 struct RecordPutResponseMessage rpr_msg;
405 int res = GNUNET_SYSERR;
407 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
414 nc = client_lookup (client);
418 GNUNET_SERVER_receive_done (client, GNUNET_OK);
422 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
424 rid = ntohl (rp_msg->gns_header.r_id);
425 msg_size = ntohs (rp_msg->gns_header.header.size);
426 key_len = ntohs (rp_msg->key_len);
427 name_len = ntohs (rp_msg->name_len);
428 rd_count = ntohs (rp_msg->rd_count);
429 rd_ser_len = ntohs(rp_msg->rd_len);
431 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
438 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
445 msg_size_exp = sizeof (struct RecordPutMessage) + key_len + name_len + rd_ser_len;
446 if (msg_size != msg_size_exp)
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
453 if ((name_len == 0) || (name_len > 256))
456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
460 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
461 name = &((char *)zone_key)[key_len];
463 if (name[name_len -1] != '\0')
466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
470 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
471 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
473 rd_ser = &name[name_len];
474 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
475 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
476 if (res != GNUNET_OK)
483 GNUNET_HashCode zone_hash;
484 GNUNET_CRYPTO_hash (zone_key, key_len, &zone_hash);
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
488 /* Database operation */
489 res = GSN_database->put_records(GSN_database->cls,
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
497 name, (res == GNUNET_OK) ? "OK" : "FAIL");
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
502 rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
503 rpr_msg.op_id = rp_msg->gns_header.r_id;
504 rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
505 if (GNUNET_OK == res)
506 rpr_msg.op_result = htons (GNUNET_OK);
508 rpr_msg.op_result = htons (GNUNET_NO);
509 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
511 GNUNET_SERVER_receive_done (client, GNUNET_OK);
514 struct CreateRecordContext
516 struct GNUNET_NAMESTORE_RecordData *rd;
517 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
518 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
519 struct GNUNET_TIME_Absolute expire;
526 handle_create_record_it (void *cls,
527 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
528 struct GNUNET_TIME_Absolute expire,
530 unsigned int rd_count,
531 const struct GNUNET_NAMESTORE_RecordData *rd,
532 const struct GNUNET_CRYPTO_RsaSignature *signature)
534 struct CreateRecordContext * crc = cls;
535 struct GNUNET_CRYPTO_RsaSignature *signature_new;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
539 struct GNUNET_NAMESTORE_RecordData *rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
540 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
542 rd_new[rd_count] = *(crc->rd);
544 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, crc->name, rd_new, rd_count+1);
545 if (NULL == signature_new)
551 /* Database operation */
552 res = GSN_database->put_records(GSN_database->cls,
553 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
558 GNUNET_break (GNUNET_OK == res);
560 GNUNET_free (rd_new);
561 GNUNET_free (signature_new);
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %s\n", crc->name, (res == GNUNET_OK) ? "SUCCESS" : "FAIL");
567 static void handle_record_create (void *cls,
568 struct GNUNET_SERVER_Client * client,
569 const struct GNUNET_MessageHeader * message)
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
572 struct GNUNET_NAMESTORE_Client *nc;
573 struct CreateRecordContext crc;
574 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
575 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
576 struct RecordCreateResponseMessage rcr_msg;
577 GNUNET_HashCode pubkey_hash;
589 int res = GNUNET_SYSERR;
591 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
598 nc = client_lookup(client);
602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
606 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
607 rid = ntohl (rp_msg->gns_header.r_id);
608 name_len = ntohs (rp_msg->name_len);
609 msg_size = ntohs (message->size);
610 rd_count = ntohs (rp_msg->rd_count);
611 rd_ser_len = ntohs (rp_msg->rd_len);
612 key_len = ntohs (rp_msg->pkey_len);
613 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
615 if (msg_size != msg_size_exp)
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
619 GNUNET_SERVER_receive_done (client, GNUNET_OK);
623 if ((name_len == 0) || (name_len > 256))
626 GNUNET_SERVER_receive_done (client, GNUNET_OK);
630 pkey_tmp = (char *) &rp_msg[1];
631 name_tmp = &pkey_tmp[key_len];
632 rd_ser = &name_tmp[name_len];
634 if (name_tmp[name_len -1] != '\0')
637 GNUNET_SERVER_receive_done (client, GNUNET_OK);
641 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
642 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
643 if ((res != GNUNET_OK) || (rd_count != 1))
649 /* Extracting and converting private key */
650 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
651 GNUNET_assert (pkey != NULL);
652 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
653 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
662 /* Get existing records for name */
663 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
664 if (res != GNUNET_SYSERR)
666 GNUNET_CRYPTO_rsa_key_free(pkey);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
671 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
672 rcr_msg.gns_header.r_id = htonl (rid);
673 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
674 if ((GNUNET_OK == res) && (crc.res == GNUNET_OK))
675 rcr_msg.op_result = htons (GNUNET_OK);
677 rcr_msg.op_result = htons (GNUNET_NO);
678 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
680 GNUNET_SERVER_receive_done (client, GNUNET_OK);
684 struct RemoveRecordContext
686 struct GNUNET_NAMESTORE_RecordData *rd;
687 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
692 handle_record_remove_it (void *cls,
693 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
694 struct GNUNET_TIME_Absolute expire,
696 unsigned int rd_count,
697 const struct GNUNET_NAMESTORE_RecordData *rd,
698 const struct GNUNET_CRYPTO_RsaSignature *signature)
700 struct RemoveRecordContext *rrc = cls;
701 unsigned int rd_count_new = rd_count -1;
702 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
705 int found = GNUNET_NO;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
711 /* Could not find record to remove */
716 /* Find record to remove */
718 for (c = 0; c < rd_count; c++)
720 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
721 (rd[c].flags == rrc->rd->flags) &&
722 (rd[c].record_type == rrc->rd->record_type) &&
723 (rd[c].data_size == rrc->rd->data_size) &&
724 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
736 if ((c2 != rd_count_new) || (found == GNUNET_NO))
738 /* Could not find record to remove */
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
744 /* Create new signature */
745 struct GNUNET_CRYPTO_RsaSignature * new_signature;
746 new_signature = GNUNET_NAMESTORE_create_signature (rrc->pkey, name, rd_new, rd_count_new);
748 if (new_signature == NULL)
750 /* Signature failed */
756 res = GSN_database->put_records(GSN_database->cls,
760 rd_count_new, rd_new,
762 GNUNET_free (new_signature);
764 if (GNUNET_OK != res)
766 /* Could put records into database */
774 static void handle_record_remove (void *cls,
775 struct GNUNET_SERVER_Client * client,
776 const struct GNUNET_MessageHeader * message)
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
779 struct GNUNET_NAMESTORE_Client *nc;
780 struct RecordRemoveResponseMessage rrr_msg;
781 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
782 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
783 GNUNET_HashCode pubkey_hash;
784 char * pkey_tmp = NULL;
785 char * name_tmp = NULL;
786 char * rd_ser = NULL;
789 size_t rd_ser_len = 0;
791 size_t msg_size_exp = 0;
795 int res = GNUNET_SYSERR;
797 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
800 GNUNET_SERVER_receive_done (client, GNUNET_OK);
804 nc = client_lookup(client);
808 GNUNET_SERVER_receive_done (client, GNUNET_OK);
812 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
813 rid = ntohl (rr_msg->gns_header.r_id);
814 name_len = ntohs (rr_msg->name_len);
815 rd_ser_len = ntohs (rr_msg->rd_len);
816 rd_count = ntohs (rr_msg->rd_count);
817 key_len = ntohs (rr_msg->key_len);
818 msg_size = ntohs (message->size);
820 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
827 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
830 GNUNET_SERVER_receive_done (client, GNUNET_OK);
834 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
835 if (msg_size != msg_size_exp)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
843 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
850 pkey_tmp = (char *) &rr_msg[1];
851 name_tmp = &pkey_tmp[key_len];
852 rd_ser = &name_tmp[name_len];
855 if ((name_len == 0) || (name_len > 256))
858 GNUNET_SERVER_receive_done (client, GNUNET_OK);
862 if (name_tmp[name_len -1] != '\0')
865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
869 /* Extracting and converting private key */
870 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
871 GNUNET_assert (pkey != NULL);
872 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
873 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
875 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
876 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
877 if ((res != GNUNET_OK) || (rd_count != 1))
883 struct RemoveRecordContext rrc;
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
889 /* Database operation */
890 res = GSN_database->iterate_records (GSN_database->cls,
894 handle_record_remove_it, &rrc);
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
897 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
903 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
904 rrr_msg.gns_header.r_id = rr_msg->gns_header.r_id;
905 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
906 rrr_msg.op_result = htons (res);
907 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
909 GNUNET_CRYPTO_rsa_key_free (pkey);
911 GNUNET_SERVER_receive_done (client, GNUNET_OK);
914 struct ZoneIterationProcResult
917 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
920 struct GNUNET_CRYPTO_RsaSignature signature;
921 struct GNUNET_TIME_Absolute expire;
931 void zone_iteration_proc (void *cls,
932 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
933 struct GNUNET_TIME_Absolute expire,
935 unsigned int rd_count,
936 const struct GNUNET_NAMESTORE_RecordData *rd,
937 const struct GNUNET_CRYPTO_RsaSignature *signature)
939 struct ZoneIterationProcResult *zipr = cls;
941 if (zone_key != NULL)
943 zipr->zone_key = *zone_key;
944 zipr->have_zone_key = GNUNET_YES;
947 zipr->have_zone_key = GNUNET_NO;
949 zipr->expire = expire;
953 memcpy (zipr->name, name, strlen(name) + 1);
954 zipr->have_name = GNUNET_YES;
957 zipr->have_name = GNUNET_NO;
959 if (signature != NULL)
961 zipr->signature = *signature;
962 zipr->have_signature = GNUNET_YES;
965 zipr->have_signature = GNUNET_NO;
967 if ((rd_count > 0) && (rd != NULL))
969 len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
970 zipr->rd_ser = GNUNET_malloc (len);
971 GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
972 zipr->rd_ser_len = len;
976 static void handle_iteration_start (void *cls,
977 struct GNUNET_SERVER_Client * client,
978 const struct GNUNET_MessageHeader * message)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
982 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
983 struct GNUNET_NAMESTORE_Client *nc;
984 struct GNUNET_NAMESTORE_ZoneIteration *zi;
985 struct ZoneIterationResponseMessage zir_msg;
986 struct ZoneIterationProcResult zipr;
989 nc = client_lookup(client);
993 GNUNET_SERVER_receive_done (client, GNUNET_OK);
997 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
998 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1001 zi->zone = zis_msg->zone;
1003 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1005 res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
1008 /* GNUNET_OK on success */
1012 /* GNUNET_SYSERR on error */
1015 /* GNUNET_NO if there were no results, */
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
1024 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1025 zir_msg.gns_header.r_id = htonl(zi->request_id);
1026 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1029 GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1035 static void handle_iteration_stop (void *cls,
1036 struct GNUNET_SERVER_Client * client,
1037 const struct GNUNET_MessageHeader * message)
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1041 struct GNUNET_NAMESTORE_Client *nc;
1042 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1043 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1046 nc = client_lookup(client);
1049 GNUNET_break_op (0);
1050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1054 rid = ntohl (zis_msg->gns_header.r_id);
1055 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1057 if (zi->request_id == rid)
1062 GNUNET_break_op (0);
1063 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1067 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1074 static void handle_iteration_next (void *cls,
1075 struct GNUNET_SERVER_Client * client,
1076 const struct GNUNET_MessageHeader * message)
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1080 struct GNUNET_NAMESTORE_Client *nc;
1081 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1082 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1086 nc = client_lookup(client);
1089 GNUNET_break_op (0);
1090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1094 rid = ntohl (zis_msg->gns_header.r_id);
1095 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1097 if (zi->request_id == rid)
1102 GNUNET_break_op (0);
1103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1108 res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
1114 * Process template requests.
1116 * @param cls closure
1117 * @param server the initialized server
1118 * @param cfg configuration to use
1121 run (void *cls, struct GNUNET_SERVER_Handle *server,
1122 const struct GNUNET_CONFIGURATION_Handle *cfg)
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1128 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1129 {&handle_start, NULL,
1130 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1131 {&handle_lookup_name, NULL,
1132 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1133 {&handle_record_put, NULL,
1134 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1135 {&handle_record_create, NULL,
1136 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1137 {&handle_record_remove, NULL,
1138 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1139 {&handle_iteration_start, NULL,
1140 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1141 {&handle_iteration_stop, NULL,
1142 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
1143 {&handle_iteration_next, NULL,
1144 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1150 /* Loading database plugin */
1152 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1156 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1157 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1158 if (GSN_database == NULL)
1159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1161 GNUNET_free (database);
1163 /* Configuring server handles */
1164 GNUNET_SERVER_add_handlers (server, handlers);
1165 snc = GNUNET_SERVER_notification_context_create (server, 16);
1166 GNUNET_SERVER_disconnect_notify (server,
1167 &client_disconnect_notification,
1170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1177 * The main function for the template service.
1179 * @param argc number of arguments from the command line
1180 * @param argv command line arguments
1181 * @return 0 ok, 1 on error
1184 main (int argc, char *const *argv)
1186 return (GNUNET_OK ==
1187 GNUNET_SERVICE_run (argc, argv, "namestore",
1188 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1191 /* end of gnunet-service-namestore.c */