a0107055c117b5b1248985b0697a715bcf0710af
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file namestore/gnunet-service-namestore.c
23  * @brief namestore for the GNUnet naming system
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
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"
33
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
35
36 /**
37  * A namestore operation.
38  */
39 struct GNUNET_NAMESTORE_ZoneIteration
40 {
41   struct GNUNET_NAMESTORE_ZoneIteration *next;
42   struct GNUNET_NAMESTORE_ZoneIteration *prev;
43
44   struct GNUNET_NAMESTORE_Client * client;
45
46   int has_zone;
47
48   GNUNET_HashCode zone;
49
50   uint64_t request_id;
51   uint32_t offset;
52
53
54
55 };
56
57
58 /**
59  * A namestore client
60  */
61 struct GNUNET_NAMESTORE_Client
62 {
63   struct GNUNET_NAMESTORE_Client *next;
64   struct GNUNET_NAMESTORE_Client *prev;
65
66   struct GNUNET_SERVER_Client * client;
67
68   struct GNUNET_NAMESTORE_ZoneIteration *op_head;
69   struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
70 };
71
72 struct GNUNET_NAMESTORE_CryptoContainer
73 {
74   char * filename;
75
76   GNUNET_HashCode zone;
77   struct GNUNET_CRYPTO_RsaPrivateKey *privkey;
78   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
79 };
80
81
82 /**
83 * Configuration handle.
84 */
85 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
86
87 /**
88 * Database handle
89 */
90 struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
91
92 /**
93 * Zonefile directory
94 */
95 static char *zonefile_directory;
96
97 static char *db_lib_name;
98
99
100 /**
101  * Our notification context.
102  */
103 static struct GNUNET_SERVER_NotificationContext *snc;
104
105 static struct GNUNET_NAMESTORE_Client *client_head;
106 static struct GNUNET_NAMESTORE_Client *client_tail;
107
108 struct GNUNET_CONTAINER_MultiHashMap *zonekeys;
109
110
111 /**
112  * Write zonefile to disk
113  * @param file where to write
114  * @param ret the key
115  *
116  * @return GNUNET_OK on success, GNUNET_SYSERR on fail
117  */
118
119 int write_key_to_file (const char *filename, struct GNUNET_NAMESTORE_CryptoContainer *c)
120 {
121   struct GNUNET_CRYPTO_RsaPrivateKey *ret = c->privkey;
122   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *enc;
123   struct GNUNET_DISK_FileHandle *fd;
124
125   if (GNUNET_YES == GNUNET_DISK_file_test (filename))
126   {
127     GNUNET_HashCode zone;
128     struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
129     struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
130
131     privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
132     if (privkey == NULL)
133     {
134       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135            _("File zone `%s' but corrupt content already exists, failed to write! \n"), GNUNET_h2s (&zone));
136       return GNUNET_SYSERR;
137     }
138
139     GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
140     GNUNET_CRYPTO_hash(&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone);
141     GNUNET_CRYPTO_rsa_key_free(privkey);
142
143     if (0 == memcmp (&zone, &c->zone, sizeof(zone)))
144     {
145       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
146            _("File zone `%s' containing this key already exists\n"), GNUNET_h2s (&zone));
147       return GNUNET_OK;
148     }
149     else
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152            _("File zone `%s' but different zone key already exists, failed to write! \n"), GNUNET_h2s (&zone));
153       return GNUNET_OK;
154     }
155   }
156   fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS, GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
157   if (NULL == fd)
158   {
159     if (errno == EEXIST)
160     {
161       if (GNUNET_YES != GNUNET_DISK_file_test (filename))
162       {
163         /* must exist but not be accessible, fail for good! */
164         if (0 != ACCESS (filename, R_OK))
165           LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
166         else
167           GNUNET_break (0);   /* what is going on!? */
168         return GNUNET_SYSERR;
169       }
170     }
171     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
172     return GNUNET_SYSERR;
173   }
174
175   if (GNUNET_YES != GNUNET_DISK_file_lock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded), GNUNET_YES))
176     return GNUNET_SYSERR;
177   enc = GNUNET_CRYPTO_rsa_encode_key (ret);
178   GNUNET_assert (enc != NULL);
179   GNUNET_assert (ntohs (enc->len) == GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
180   GNUNET_free (enc);
181   GNUNET_DISK_file_sync (fd);
182   if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded)))
183     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
184   GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
185
186   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187        _("Stored zonekey for zone `%s' in file `%s'\n"),GNUNET_h2s(&c->zone), c->filename);
188   return GNUNET_OK;
189 }
190
191 int zone_to_disk_it (void *cls,
192                      const GNUNET_HashCode * key,
193                      void *value)
194 {
195   struct GNUNET_NAMESTORE_CryptoContainer * c = value;
196
197   if (c->filename != NULL)
198     write_key_to_file(c->filename, c);
199   else
200   {
201     GNUNET_asprintf(&c->filename, "%s/%s.zkey", zonefile_directory, GNUNET_h2s_full (&c->zone));
202     write_key_to_file(c->filename, c);
203   }
204
205   GNUNET_CONTAINER_multihashmap_remove (zonekeys, key, value);;
206   GNUNET_CRYPTO_rsa_key_free(c->privkey);
207   GNUNET_free (c->pubkey);
208   GNUNET_free(c->filename);
209   GNUNET_free (c);
210
211   return GNUNET_OK;
212 }
213
214
215 struct GNUNET_TIME_Absolute
216 get_block_expiration_time (unsigned int rd_count, const struct GNUNET_NAMESTORE_RecordData *rd)
217 {
218   int c;
219   struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get_forever();
220   if (NULL == rd)
221     return GNUNET_TIME_absolute_get_zero();
222   for (c = 0; c < rd_count; c++)
223   {
224     if (rd[c].expiration.abs_value < expire.abs_value)
225       expire = rd[c].expiration;
226   }
227   return expire;
228 }
229
230 /**
231  * Task run during shutdown.
232  *
233  * @param cls unused
234  * @param tc unused
235  */
236 static void
237 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 {
239   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
240   struct GNUNET_NAMESTORE_ZoneIteration * no;
241   struct GNUNET_NAMESTORE_ZoneIteration * tmp;
242   struct GNUNET_NAMESTORE_Client * nc;
243   struct GNUNET_NAMESTORE_Client * next;
244
245   GNUNET_SERVER_notification_context_destroy (snc);
246   snc = NULL;
247
248   GNUNET_CONTAINER_multihashmap_iterate(zonekeys, &zone_to_disk_it, NULL);
249   GNUNET_CONTAINER_multihashmap_destroy(zonekeys);
250
251   for (nc = client_head; nc != NULL; nc = next)
252   {
253     next = nc->next;
254     for (no = nc->op_head; no != NULL; no = tmp)
255     {
256       GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
257       tmp = no->next;
258       GNUNET_free (no);
259     }
260     GNUNET_SERVER_client_drop(nc->client);
261     GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
262     GNUNET_free (nc);
263   }
264
265   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
266   GNUNET_free (db_lib_name);
267   GNUNET_free_non_null(zonefile_directory);
268 }
269
270 static struct GNUNET_NAMESTORE_Client *
271 client_lookup (struct GNUNET_SERVER_Client *client)
272 {
273   struct GNUNET_NAMESTORE_Client * nc;
274
275   GNUNET_assert (NULL != client);
276
277   for (nc = client_head; nc != NULL; nc = nc->next)
278   {
279     if (client == nc->client)
280       break;
281   }
282   return nc;
283 }
284
285 /**
286  * Called whenever a client is disconnected.  Frees our
287  * resources associated with that client.
288  *
289  * @param cls closure
290  * @param client identification of the client
291  */
292 static void
293 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
294 {
295   struct GNUNET_NAMESTORE_ZoneIteration * no;
296   struct GNUNET_NAMESTORE_Client * nc;
297   if (NULL == client)
298     return;
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
301
302   nc = client_lookup (client);
303
304   if ((NULL == client) || (NULL == nc))
305     return;
306
307   for (no = nc->op_head; no != NULL; no = no->next)
308   {
309     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
310     GNUNET_free (no);
311   }
312
313
314   GNUNET_SERVER_client_drop(nc->client);
315   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
316   GNUNET_free (nc);
317   nc = NULL;
318 }
319
320
321
322 static void handle_start (void *cls,
323                           struct GNUNET_SERVER_Client * client,
324                           const struct GNUNET_MessageHeader * message)
325 {
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
327
328   struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
329   nc->client = client;
330   GNUNET_SERVER_notification_context_add (snc, client);
331   GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
332   GNUNET_SERVER_client_keep (client);
333   GNUNET_SERVER_receive_done (client, GNUNET_OK);
334 }
335
336 struct LookupNameContext
337 {
338   struct GNUNET_NAMESTORE_Client *nc;
339   uint32_t request_id;
340   uint32_t record_type;
341   GNUNET_HashCode *zone;
342   char * name;
343 };
344
345 void drop_iterator (void *cls,
346                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
347                    struct GNUNET_TIME_Absolute expire,
348                    const char *name,
349                    unsigned int rd_len,
350                    const struct GNUNET_NAMESTORE_RecordData *rd,
351                    const struct GNUNET_CRYPTO_RsaSignature *signature)
352 {
353   GNUNET_HashCode zone_hash;
354   int * stop = cls;
355   if (NULL != zone_key)
356   {
357     GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_hash);
358     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting zone `%s'\n", GNUNET_h2s (&zone_hash));
359     GSN_database->delete_zone (GSN_database->cls, &zone_hash);
360   }
361   else
362   {
363     (*stop) = GNUNET_YES;
364   }
365 }
366
367
368 static void
369 handle_lookup_name_it (void *cls,
370     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
371     struct GNUNET_TIME_Absolute expire,
372     const char *name,
373     unsigned int rd_count,
374     const struct GNUNET_NAMESTORE_RecordData *rd,
375     const struct GNUNET_CRYPTO_RsaSignature *signature)
376 {
377   /* send response */
378   struct LookupNameContext *lnc = cls;
379   struct LookupNameResponseMessage *lnr_msg;
380   struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
381   struct GNUNET_NAMESTORE_CryptoContainer *cc;
382   struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
383   struct GNUNET_TIME_Absolute e;
384   GNUNET_HashCode zone_key_hash;
385   char *rd_tmp;
386   char *name_tmp;
387   size_t rd_ser_len;
388   size_t r_size = 0;
389   size_t name_len = 0;
390
391   int copied_elements = 0;
392   int contains_signature = GNUNET_NO;
393   int authoritative = GNUNET_NO;
394   int c;
395
396   if (NULL != name)
397     name_len = strlen(name) + 1;
398
399   /* count records to copy */
400   if (rd_count != 0)
401   {
402     if (lnc->record_type != 0)
403     {
404       /* special record type needed */
405       for (c = 0; c < rd_count; c ++)
406         if (rd[c].record_type == lnc->record_type)
407           copied_elements++; /* found matching record */
408       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
409           copied_elements, lnc->record_type, lnc->name, GNUNET_h2s(lnc->zone));
410       rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
411       copied_elements = 0;
412       for (c = 0; c < rd_count; c ++)
413       {
414         if (rd[c].record_type == lnc->record_type)
415         {
416           /* found matching record */
417           memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
418           copied_elements++;
419         }
420       }
421     }
422     else
423     {
424       copied_elements = rd_count;
425       rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
426     }
427   }
428   else
429   {
430     /* No results */
431     copied_elements = 0;
432     rd_selected = NULL;
433     expire = GNUNET_TIME_UNIT_ZERO_ABS;
434   }
435
436   rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
437   char rd_ser[rd_ser_len];
438   GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
439
440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
441       copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
442
443   if ((copied_elements == rd_count) && (NULL != signature))
444     contains_signature = GNUNET_YES; /* returning all records, so include signature */
445   else
446     contains_signature = GNUNET_NO; /* returning not all records, so do not include signature */
447
448
449   if ((NULL != zone_key) && (copied_elements == rd_count))
450   {
451     GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
452     if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &zone_key_hash))
453     {
454       cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &zone_key_hash);
455       e = get_block_expiration_time(rd_count, rd);
456       signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
457       GNUNET_assert (signature_new != NULL);
458       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for name `%s' with %u records in zone `%s'\n",name, copied_elements, GNUNET_h2s(&zone_key_hash));
459       authoritative = GNUNET_YES;
460     }
461     else
462       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am not authoritative for name `%s' in zone `%s'\n",name, GNUNET_h2s(&zone_key_hash));
463   }
464
465   r_size = sizeof (struct LookupNameResponseMessage) +
466            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
467            name_len +
468            rd_ser_len;
469
470   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
471   lnr_msg = GNUNET_malloc (r_size);
472   lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
473   lnr_msg->gns_header.header.size = ntohs (r_size);
474   lnr_msg->gns_header.r_id = htonl (lnc->request_id);
475   lnr_msg->rd_count = htons (copied_elements);
476   lnr_msg->rd_len = htons (rd_ser_len);
477   lnr_msg->name_len = htons (name_len);
478   lnr_msg->expire = GNUNET_TIME_absolute_hton(get_block_expiration_time(copied_elements, rd_selected));
479
480   if (rd_selected != rd)
481     GNUNET_free (rd_selected);
482
483   if (zone_key != NULL)
484     lnr_msg->public_key = (*zone_key);
485   else
486     memset(&lnr_msg->public_key, '\0', sizeof (lnr_msg->public_key));
487
488   if (GNUNET_YES == authoritative)
489   { /* use new created signature */
490     lnr_msg->contains_sig = htons (GNUNET_YES);
491     GNUNET_assert (signature_new != NULL);
492     lnr_msg->signature = *signature_new;
493     GNUNET_free (signature_new);
494   }
495   else if (GNUNET_YES == contains_signature)
496   {
497     /* use existing signature */
498     lnr_msg->contains_sig = htons (GNUNET_YES);
499     GNUNET_assert (signature != NULL);
500     lnr_msg->signature = *signature;
501   }
502   else
503   {
504     /* use no signature */
505     memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
506   }
507
508   name_tmp = (char *) &lnr_msg[1];
509   rd_tmp = &name_tmp[name_len];
510
511   memcpy (name_tmp, name, name_len);
512   memcpy (rd_tmp, rd_ser, rd_ser_len);
513
514   GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
515   GNUNET_free (lnr_msg);
516 }
517
518 static void handle_lookup_name (void *cls,
519                           struct GNUNET_SERVER_Client * client,
520                           const struct GNUNET_MessageHeader * message)
521 {
522   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
523   struct LookupNameContext lnc;
524   struct GNUNET_NAMESTORE_Client *nc;
525   size_t name_len;
526   char * name;
527   uint32_t rid = 0;
528   uint32_t type = 0;
529
530   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
531   {
532     GNUNET_break_op (0);
533     GNUNET_SERVER_receive_done (client, GNUNET_OK);
534     return;
535   }
536
537   nc = client_lookup(client);
538   if (nc == NULL)
539   {
540     GNUNET_break_op (0);
541     GNUNET_SERVER_receive_done (client, GNUNET_OK);
542     return;
543   }
544
545   struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
546   rid = ntohl (ln_msg->gns_header.r_id);
547   name_len = ntohl (ln_msg->name_len);
548   type = ntohl (ln_msg->record_type);
549
550   if ((name_len == 0) || (name_len > 256))
551   {
552     GNUNET_break_op (0);
553     GNUNET_SERVER_receive_done (client, GNUNET_OK);
554     return;
555   }
556
557   name = (char *) &ln_msg[1];
558   if (name[name_len -1] != '\0')
559   {
560     GNUNET_break_op (0);
561     GNUNET_SERVER_receive_done (client, GNUNET_OK);
562     return;
563   }
564
565   if (0 == type)
566     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
567   else
568     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up records with type %u for name `%s' in zone `%s'\n", type, name, GNUNET_h2s(&ln_msg->zone));
569
570   /* do the actual lookup */
571   lnc.request_id = rid;
572   lnc.nc = nc;
573   lnc.record_type = type;
574   lnc.name = name;
575   lnc.zone = &ln_msg->zone;
576   GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
577
578   GNUNET_SERVER_receive_done (client, GNUNET_OK);
579 }
580
581 static void handle_record_put (void *cls,
582                           struct GNUNET_SERVER_Client * client,
583                           const struct GNUNET_MessageHeader * message)
584 {
585   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
586   struct GNUNET_NAMESTORE_Client *nc;
587   struct GNUNET_TIME_Absolute expire;
588   struct GNUNET_CRYPTO_RsaSignature *signature;
589   struct RecordPutResponseMessage rpr_msg;
590   size_t name_len;
591   size_t msg_size;
592   size_t msg_size_exp;
593   char * name;
594   char * rd_ser;
595   uint32_t rid = 0;
596   uint32_t rd_ser_len;
597   uint32_t rd_count;
598   int res = GNUNET_SYSERR;
599
600   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
601   {
602     GNUNET_break_op (0);
603     GNUNET_SERVER_receive_done (client, GNUNET_OK);
604     return;
605   }
606
607   nc = client_lookup (client);
608   if (nc == NULL)
609   {
610     GNUNET_break_op (0);
611     GNUNET_SERVER_receive_done (client, GNUNET_OK);
612     return;
613   }
614
615   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
616
617   rid = ntohl (rp_msg->gns_header.r_id);
618   msg_size = ntohs (rp_msg->gns_header.header.size);
619   name_len = ntohs (rp_msg->name_len);
620   rd_count = ntohs (rp_msg->rd_count);
621   rd_ser_len = ntohs(rp_msg->rd_len);
622
623   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
624   {
625     GNUNET_break_op (0);
626     GNUNET_SERVER_receive_done (client, GNUNET_OK);
627     return;
628   }
629
630   if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
631   {
632     GNUNET_break_op (0);
633     GNUNET_SERVER_receive_done (client, GNUNET_OK);
634     return;
635   }
636
637   msg_size_exp = sizeof (struct RecordPutMessage) +  name_len  + rd_ser_len;
638   if (msg_size != msg_size_exp)
639   {
640     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
641     GNUNET_break_op (0);
642     GNUNET_SERVER_receive_done (client, GNUNET_OK);
643     return;
644   }
645   if ((name_len == 0) || (name_len > 256))
646   {
647     GNUNET_break_op (0);
648     GNUNET_SERVER_receive_done (client, GNUNET_OK);
649     return;
650   }
651
652   name = (char *) &rp_msg[1];
653
654   if (name[name_len -1] != '\0')
655   {
656     GNUNET_break_op (0);
657     GNUNET_SERVER_receive_done (client, GNUNET_OK);
658     return;
659   }
660
661   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
662   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
663
664   rd_ser = &name[name_len];
665   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
666   res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
667   if (res != GNUNET_OK)
668   {
669     GNUNET_break_op (0);
670     goto send;
671   }
672
673   GNUNET_HashCode zone_hash;
674   GNUNET_CRYPTO_hash (&rp_msg->public_key, sizeof (rp_msg->public_key), &zone_hash);
675
676   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
677
678   /* Database operation */
679   res = GSN_database->put_records(GSN_database->cls,
680                                 &rp_msg->public_key,
681                                 expire,
682                                 name,
683                                 rd_count, rd,
684                                 signature);
685
686   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
687       name, (res == GNUNET_OK) ? "OK" : "FAIL");
688
689   /* Send response */
690 send:
691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
692   rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
693   rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
694   rpr_msg.gns_header.r_id = htonl (rid);
695   rpr_msg.op_result = htonl (res);
696   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
697
698   GNUNET_SERVER_receive_done (client, GNUNET_OK);
699 }
700
701 struct CreateRecordContext
702 {
703   struct GNUNET_NAMESTORE_RecordData *rd;
704   struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
705   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
706   struct GNUNET_TIME_Absolute expire;
707   char *name;
708   int res;
709 };
710
711
712 static void
713 handle_create_record_it (void *cls,
714     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
715     struct GNUNET_TIME_Absolute expire,
716     const char *name,
717     unsigned int rd_count,
718     const struct GNUNET_NAMESTORE_RecordData *rd,
719     const struct GNUNET_CRYPTO_RsaSignature *signature)
720 {
721   struct CreateRecordContext * crc = cls;
722   struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
723   struct GNUNET_CRYPTO_RsaSignature dummy_signature;
724   struct GNUNET_TIME_Absolute block_expiration;
725   int res;
726   int exist = GNUNET_SYSERR;
727   int update = GNUNET_NO;
728   int c;
729   int rd_count_new = 0;
730
731   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
732
733   for (c = 0; c < rd_count; c++)
734   {
735
736     if ((crc->rd->record_type == rd[c].record_type) &&
737         (crc->rd->data_size == rd[c].data_size) &&
738         (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
739     {
740       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
741       exist = c;
742       if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
743         update = GNUNET_YES;
744        break;
745     }
746   }
747
748   if (exist == GNUNET_SYSERR)
749     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New record does not exist for name `%s'!\n", crc->name);
750
751   if (exist == GNUNET_SYSERR)
752   {
753     rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
754     memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
755     rd_count_new = rd_count + 1;
756     rd_new[rd_count] = *(crc->rd);
757   }
758   else if (update == GNUNET_NO)
759   {
760     /* Exact same record already exists */
761     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
762     res = GNUNET_NO;
763     goto end;
764   }
765   else if (update == GNUNET_YES)
766   {
767     /* Update record */
768     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
769     rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
770     memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
771     rd_count_new = rd_count;
772     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
773     rd_new[exist].expiration = crc->rd->expiration;
774   }
775
776   block_expiration = GNUNET_TIME_absolute_max(crc->expire, expire);
777   if (block_expiration.abs_value != expire.abs_value)
778     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated block expiration time\n");
779
780   memset (&dummy_signature, '\0', sizeof (dummy_signature));
781
782   /* Database operation */
783   GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
784   res = GSN_database->put_records(GSN_database->cls,
785                                 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
786                                 block_expiration,
787                                 crc->name,
788                                 rd_count_new, rd_new,
789                                 &dummy_signature);
790   GNUNET_break (GNUNET_OK == res);
791   if (res == GNUNET_OK)
792     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully put record for `%s' in database \n", crc->name);
793   else
794     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to put record for `%s' in database \n", crc->name);
795   res = GNUNET_YES;
796
797 end:
798   GNUNET_free_non_null (rd_new);
799
800   switch (res) {
801     case GNUNET_SYSERR:
802        /* failed to create the record */
803        crc->res = GNUNET_SYSERR;
804       break;
805     case GNUNET_YES:
806       /* database operations OK */
807       if (GNUNET_YES == update)
808         /* we updated an existing record */
809         crc->res = GNUNET_NO;
810       else
811         /* we created a new record */
812         crc->res = GNUNET_YES;
813       break;
814     case GNUNET_NO:
815         /* identical entry existed, so we did nothing */
816         crc->res = GNUNET_NO;
817       break;
818     default:
819       break;
820   }
821
822   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
823
824 }
825
826 static void handle_record_create (void *cls,
827                           struct GNUNET_SERVER_Client * client,
828                           const struct GNUNET_MessageHeader * message)
829 {
830   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
831   struct GNUNET_NAMESTORE_Client *nc;
832   struct GNUNET_NAMESTORE_CryptoContainer *cc;
833   struct CreateRecordContext crc;
834   struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
835   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
836   struct RecordCreateResponseMessage rcr_msg;
837   GNUNET_HashCode pubkey_hash;
838   size_t name_len;
839   size_t msg_size;
840   size_t msg_size_exp;
841   size_t rd_ser_len;
842   size_t key_len;
843   uint32_t rid = 0;
844   char *pkey_tmp;
845   char *name_tmp;
846   char *rd_ser;
847   int rd_count;
848
849   int res = GNUNET_SYSERR;
850   crc.res = GNUNET_SYSERR;
851
852   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
853   {
854     GNUNET_break_op (0);
855     GNUNET_SERVER_receive_done (client, GNUNET_OK);
856     return;
857   }
858
859   nc = client_lookup(client);
860   if (nc == NULL)
861   {
862     GNUNET_break_op (0);
863     GNUNET_SERVER_receive_done (client, GNUNET_OK);
864     return;
865   }
866
867   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
868   rid = ntohl (rp_msg->gns_header.r_id);
869   name_len = ntohs (rp_msg->name_len);
870   msg_size = ntohs (message->size);
871   rd_count = ntohs (rp_msg->rd_count);
872   rd_ser_len = ntohs (rp_msg->rd_len);
873   key_len = ntohs (rp_msg->pkey_len);
874   msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
875
876   if (msg_size != msg_size_exp)
877   {
878     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
879     GNUNET_break_op (0);
880     GNUNET_SERVER_receive_done (client, GNUNET_OK);
881     return;
882   }
883
884   if ((name_len == 0) || (name_len > 256))
885   {
886     GNUNET_break_op (0);
887     GNUNET_SERVER_receive_done (client, GNUNET_OK);
888     return;
889   }
890
891   pkey_tmp = (char *) &rp_msg[1];
892   name_tmp = &pkey_tmp[key_len];
893   rd_ser = &name_tmp[name_len];
894
895   if (name_tmp[name_len -1] != '\0')
896   {
897     GNUNET_break_op (0);
898     GNUNET_SERVER_receive_done (client, GNUNET_OK);
899     return;
900   }
901
902   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
903
904   res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
905   if ((res != GNUNET_OK) || (rd_count != 1))
906   {
907     GNUNET_break_op (0);
908     goto send;
909   }
910
911   /* Extracting and converting private key */
912   pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
913   GNUNET_assert (pkey != NULL);
914   GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
915   GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
916
917   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &pubkey_hash))
918   {
919     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_h2s(&pubkey_hash));
920
921     cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
922     cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
923     cc->pubkey = GNUNET_malloc(sizeof (pub));
924     memcpy (cc->pubkey, &pub, sizeof(pub));
925     cc->zone = pubkey_hash;
926
927     GNUNET_CONTAINER_multihashmap_put(zonekeys, &pubkey_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
928   }
929
930   crc.expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
931   crc.res = GNUNET_SYSERR;
932   crc.pkey = pkey;
933   crc.pubkey = &pub;
934   crc.rd = rd;
935   crc.name = name_tmp;
936
937   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
938
939   /* Get existing records for name */
940   res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
941   if (res != GNUNET_SYSERR)
942     res = GNUNET_OK;
943   GNUNET_CRYPTO_rsa_key_free(pkey);
944   pkey = NULL;
945
946   /* Send response */
947 send:
948   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
949   rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
950   rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
951   rcr_msg.gns_header.r_id = htonl (rid);
952   if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
953     rcr_msg.op_result = htonl (GNUNET_YES);
954   else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
955     rcr_msg.op_result = htonl (GNUNET_NO);
956   else
957     rcr_msg.op_result = htonl (GNUNET_SYSERR);
958   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
959
960   GNUNET_SERVER_receive_done (client, GNUNET_OK);
961 }
962
963
964 struct RemoveRecordContext
965 {
966   struct GNUNET_NAMESTORE_RecordData *rd;
967   struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
968   uint16_t op_res;
969 };
970
971 static void
972 handle_record_remove_it (void *cls,
973     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
974     struct GNUNET_TIME_Absolute expire,
975     const char *name,
976     unsigned int rd_count,
977     const struct GNUNET_NAMESTORE_RecordData *rd,
978     const struct GNUNET_CRYPTO_RsaSignature *signature)
979 {
980   struct RemoveRecordContext *rrc = cls;
981   unsigned int c;
982   int res;
983   int found;
984   unsigned int rd_count_new;
985
986   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
987
988   if (rd_count == 0)
989   {
990     /* Could not find record to remove */
991     rrc->op_res = 1;
992     return;
993   }
994
995   /* Find record to remove */
996   found = GNUNET_SYSERR;
997   for (c = 0; c < rd_count; c++)
998   {
999     if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
1000         (rd[c].flags == rrc->rd->flags) &&
1001         (rd[c].record_type == rrc->rd->record_type) &&
1002         (rd[c].data_size == rrc->rd->data_size) &&
1003         (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
1004         {
1005           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
1006           found = c;
1007           break;
1008         }
1009   }
1010   if (GNUNET_SYSERR == found)
1011   {
1012     /* Could not find record to remove */
1013     rrc->op_res = 2;
1014     return;
1015   }
1016
1017   rd_count_new = rd_count -1;
1018   struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
1019
1020   unsigned int c2 = 0;
1021   for (c = 0; c < rd_count; c++)
1022   {
1023     if (c != found)
1024     {
1025       GNUNET_assert (c2 < rd_count_new);
1026       rd_new[c2] = rd[c];
1027       c2++;
1028     }
1029   }
1030
1031   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
1032
1033   /* Create dummy signature */
1034   struct GNUNET_CRYPTO_RsaSignature dummy_signature;
1035   memset (&dummy_signature, '\0', sizeof (dummy_signature));
1036
1037
1038   /* Put records */
1039   res = GSN_database->put_records(GSN_database->cls,
1040                                   zone_key,
1041                                   expire,
1042                                   name,
1043                                   rd_count_new, rd_new,
1044                                   &dummy_signature);
1045   if (GNUNET_OK != res)
1046   {
1047     /* Could put records into database */
1048     rrc->op_res = 4;
1049     return;
1050   }
1051
1052   rrc->op_res = 0;
1053 }
1054
1055 static void handle_record_remove (void *cls,
1056                           struct GNUNET_SERVER_Client * client,
1057                           const struct GNUNET_MessageHeader * message)
1058 {
1059   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
1060   struct GNUNET_NAMESTORE_Client *nc;
1061   struct RecordRemoveResponseMessage rrr_msg;
1062   struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
1063   struct GNUNET_NAMESTORE_CryptoContainer *cc = NULL;
1064   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1065   GNUNET_HashCode pubkey_hash;
1066   char * pkey_tmp = NULL;
1067   char * name_tmp = NULL;
1068   char * rd_ser = NULL;
1069   size_t key_len = 0;
1070   size_t name_len = 0;
1071   size_t rd_ser_len = 0;
1072   size_t msg_size = 0;
1073   size_t msg_size_exp = 0;
1074   uint32_t rd_count;
1075   uint32_t rid = 0;
1076
1077   int res = GNUNET_SYSERR;
1078
1079   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
1080   {
1081     GNUNET_break_op (0);
1082     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1083     return;
1084   }
1085
1086   nc = client_lookup(client);
1087   if (nc == NULL)
1088   {
1089     GNUNET_break_op (0);
1090     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1091     return;
1092   }
1093
1094   struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
1095   rid = ntohl (rr_msg->gns_header.r_id);
1096   name_len = ntohs (rr_msg->name_len);
1097   rd_ser_len = ntohs (rr_msg->rd_len);
1098   rd_count = ntohs (rr_msg->rd_count);
1099   key_len = ntohs (rr_msg->pkey_len);
1100   msg_size = ntohs (message->size);
1101
1102   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1103   {
1104     GNUNET_break_op (0);
1105     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1106     return;
1107   }
1108
1109   if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1110   {
1111     GNUNET_break_op (0);
1112     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1113     return;
1114   }
1115
1116   msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1117   if (msg_size != msg_size_exp)
1118   {
1119     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
1120     GNUNET_break_op (0);
1121     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1122     return;
1123   }
1124
1125   if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1126   {
1127     GNUNET_break_op (0);
1128     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1129     return;
1130   }
1131
1132   pkey_tmp = (char *) &rr_msg[1];
1133   name_tmp = &pkey_tmp[key_len];
1134   rd_ser = &name_tmp[name_len];
1135
1136
1137   if ((name_len == 0) || (name_len > 256))
1138   {
1139     GNUNET_break_op (0);
1140     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1141     return;
1142   }
1143
1144   if (name_tmp[name_len -1] != '\0')
1145   {
1146     GNUNET_break_op (0);
1147     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1148     return;
1149   }
1150
1151   /* Extracting and converting private key */
1152   pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1153   GNUNET_assert (pkey != NULL);
1154   GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
1155   GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
1156
1157   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &pubkey_hash))
1158   {
1159     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_h2s(&pubkey_hash));
1160     cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1161     cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1162     cc->pubkey = GNUNET_malloc(sizeof (pub));
1163     memcpy (cc->pubkey, &pub, sizeof(pub));
1164     cc->zone = pubkey_hash;
1165
1166     GNUNET_CONTAINER_multihashmap_put(zonekeys, &pubkey_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1167   }
1168
1169   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
1170   res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
1171   if ((res != GNUNET_OK) || (rd_count != 1))
1172   {
1173     GNUNET_break_op (0);
1174     goto send;
1175   }
1176
1177   struct RemoveRecordContext rrc;
1178   rrc.rd = rd;
1179   rrc.pkey = pkey;
1180
1181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
1182
1183   /* Database operation */
1184   res = GSN_database->iterate_records (GSN_database->cls,
1185                                        &pubkey_hash,
1186                                        name_tmp,
1187                                        0,
1188                                        handle_record_remove_it, &rrc);
1189
1190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
1191       name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
1192   res = rrc.op_res;
1193
1194   /* Send response */
1195 send:
1196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
1197   rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
1198   rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
1199   rrr_msg.gns_header.r_id = htonl (rid);
1200   rrr_msg.op_result = htonl (res);
1201   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
1202
1203   GNUNET_CRYPTO_rsa_key_free (pkey);
1204
1205   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1206 }
1207
1208
1209 struct ZoneToNameCtx
1210 {
1211   struct GNUNET_NAMESTORE_Client *nc;
1212   uint32_t rid;
1213 };
1214
1215 static void
1216 handle_zone_to_name_it (void *cls,
1217     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1218     struct GNUNET_TIME_Absolute expire,
1219     const char *name,
1220     unsigned int rd_count,
1221     const struct GNUNET_NAMESTORE_RecordData *rd,
1222     const struct GNUNET_CRYPTO_RsaSignature *signature)
1223 {
1224   struct ZoneToNameCtx * ztn_ctx = cls;
1225   struct ZoneToNameResponseMessage *ztnr_msg;
1226   int16_t res = GNUNET_SYSERR;
1227   uint16_t name_len = 0;
1228   uint16_t rd_ser_len = 0 ;
1229   int32_t contains_sig = 0;
1230   size_t msg_size = 0;
1231
1232   char *rd_ser = NULL;
1233   char *name_tmp;
1234   char *rd_tmp;
1235   char *sig_tmp;
1236
1237   if ((zone_key != NULL) && (name != NULL))
1238   {
1239     /* found result */
1240     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1241     res = GNUNET_YES;
1242     name_len = strlen (name) +1;
1243   }
1244   else
1245   {
1246     /* no result found */
1247     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1248     res = GNUNET_NO;
1249     name_len = 0;
1250   }
1251
1252   if (rd_count > 0)
1253   {
1254     rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1255     rd_ser = GNUNET_malloc (rd_ser_len);
1256     GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1257   }
1258   else
1259     rd_ser_len = 0;
1260
1261   if (signature != NULL)
1262     contains_sig = GNUNET_YES;
1263   else
1264     contains_sig = GNUNET_NO;
1265
1266
1267
1268   msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1269   ztnr_msg = GNUNET_malloc (msg_size);
1270   memset(ztnr_msg, 0, msg_size);
1271
1272   name_tmp = (char *) &ztnr_msg[1];
1273   rd_tmp = &name_tmp[name_len];
1274   sig_tmp = &rd_tmp[rd_ser_len];
1275
1276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1277   ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1278   ztnr_msg->gns_header.header.size = htons (msg_size);
1279   ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1280   ztnr_msg->res = htons (res);
1281   ztnr_msg->rd_len = htons (rd_ser_len);
1282   ztnr_msg->rd_count = htons (rd_count);
1283   ztnr_msg->name_len = htons (name_len);
1284   ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1285   if (zone_key != NULL)
1286     ztnr_msg->zone_key = *zone_key;
1287   else
1288     memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1289
1290   if ((name_len > 0) && (name != NULL))
1291     memcpy (name_tmp, name, name_len);
1292
1293   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name is `%s', has %u records, rd ser len %u msg_size %u\n", name, rd_count, rd_ser_len, msg_size);
1294   if ((rd_ser_len > 0) && (rd_ser != NULL))
1295     memcpy (rd_tmp, rd_ser, rd_ser_len);
1296   if ((GNUNET_YES == contains_sig) && (signature != NULL))
1297     memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1298
1299   GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1300   GNUNET_free (ztnr_msg);
1301   GNUNET_free_non_null (rd_ser);
1302 }
1303
1304
1305 static void handle_zone_to_name (void *cls,
1306                           struct GNUNET_SERVER_Client * client,
1307                           const struct GNUNET_MessageHeader * message)
1308 {
1309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1310   struct GNUNET_NAMESTORE_Client *nc;
1311   struct ZoneToNameCtx ztn_ctx;
1312   size_t msg_size = 0;
1313   uint32_t rid = 0;
1314
1315   if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1316   {
1317     GNUNET_break_op (0);
1318     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1319     return;
1320   }
1321
1322   nc = client_lookup(client);
1323   if (nc == NULL)
1324   {
1325     GNUNET_break_op (0);
1326     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1327     return;
1328   }
1329
1330   struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1331
1332   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1333   {
1334     GNUNET_break_op (0);
1335     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1336     return;
1337   }
1338
1339   rid = ntohl (ztn_msg->gns_header.r_id);
1340
1341   ztn_ctx.rid = rid;
1342   ztn_ctx.nc = nc;
1343
1344   char * z_tmp = strdup (GNUNET_h2s (&ztn_msg->zone));
1345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1346       z_tmp,
1347       GNUNET_h2s (&ztn_msg->value_zone));
1348   GNUNET_free (z_tmp);
1349
1350   GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1351
1352   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1353 }
1354
1355 struct ZoneIterationProcResult
1356 {
1357   int have_zone_key;
1358   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1359
1360   int have_signature;
1361   struct GNUNET_CRYPTO_RsaSignature signature;
1362   struct GNUNET_TIME_Absolute expire;
1363
1364   int have_name;
1365   char name[256];
1366
1367   size_t rd_ser_len;
1368   char *rd_ser;
1369 };
1370
1371
1372 void zone_iteration_proc (void *cls,
1373                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1374                          struct GNUNET_TIME_Absolute expire,
1375                          const char *name,
1376                          unsigned int rd_count,
1377                          const struct GNUNET_NAMESTORE_RecordData *rd,
1378                          const struct GNUNET_CRYPTO_RsaSignature *signature)
1379 {
1380   struct GNUNET_NAMESTORE_ZoneIteration *zi = cls;
1381   struct GNUNET_NAMESTORE_Client *nc = zi->client;
1382   struct GNUNET_NAMESTORE_CryptoContainer * cc;
1383   struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
1384   struct GNUNET_TIME_Absolute e;
1385   GNUNET_HashCode zone_key_hash;
1386   int authoritative = GNUNET_NO;
1387
1388   if ((zone_key == NULL) && (name == NULL))
1389   {
1390     struct ZoneIterationResponseMessage zir_msg;
1391     if (zi->has_zone == GNUNET_YES)
1392       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for zone `%s'\n", GNUNET_h2s(&zi->zone));
1393     else
1394       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for all zones\n");
1395
1396     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message\n", "ZONE_ITERATION_RESPONSE");
1397     zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1398     zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1399     zir_msg.gns_header.r_id = htonl(zi->request_id);
1400     zir_msg.expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_zero());
1401     zir_msg.name_len = htons (0);
1402     zir_msg.reserved = htons (0);
1403     zir_msg.rd_count = htons (0);
1404     zir_msg.rd_len = htons (0);
1405     memset (&zir_msg.public_key, '\0', sizeof (zir_msg.public_key));
1406     memset (&zir_msg.signature, '\0', sizeof (zir_msg.signature));
1407     GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1408
1409     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing zone iterator\n");
1410     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1411     GNUNET_free (zi);
1412     return;
1413   }
1414   else
1415   {
1416     struct ZoneIterationResponseMessage *zir_msg;
1417     if (zi->has_zone == GNUNET_YES)
1418       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over zone `%s'\n",
1419           name, GNUNET_h2s(&zi->zone));
1420     if (zi->has_zone == GNUNET_NO)
1421       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over all zones\n",
1422           name);
1423
1424     size_t name_len;
1425     size_t rd_ser_len;
1426     size_t msg_size;
1427     char *name_tmp;
1428     char *rd_tmp;
1429     name_len = strlen (name) +1;
1430
1431     rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1432     char rd_ser[rd_ser_len];
1433     GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1434     msg_size = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_ser_len;
1435     zir_msg = GNUNET_malloc(msg_size);
1436
1437     name_tmp = (char *) &zir_msg[1];
1438     rd_tmp = &name_tmp[name_len];
1439
1440     GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
1441     if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &zone_key_hash))
1442     {
1443       cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &zone_key_hash);
1444       e = get_block_expiration_time(rd_count, rd);
1445       expire = e;
1446       signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
1447       GNUNET_assert (signature_new != NULL);
1448       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for `%s' in zone `%s' with %u records  and expiration %llu\n", name, GNUNET_h2s(&zone_key_hash), rd_count, e.abs_value);
1449       authoritative = GNUNET_YES;
1450     }
1451
1452
1453     zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1454     zir_msg->gns_header.header.size = htons (msg_size);
1455     zir_msg->gns_header.r_id = htonl(zi->request_id);
1456     zir_msg->expire = GNUNET_TIME_absolute_hton(expire);
1457     zir_msg->reserved = htons (0);
1458     zir_msg->name_len = htons (name_len);
1459     zir_msg->rd_count = htons (rd_count);
1460     zir_msg->rd_len = htons (rd_ser_len);
1461     if ((GNUNET_YES == authoritative) && (NULL != signature_new))
1462     {
1463       zir_msg->signature = *signature_new;
1464       GNUNET_free (signature_new);
1465     }
1466     else
1467       zir_msg->signature = *signature;
1468     GNUNET_assert (NULL != zone_key);
1469     if (zone_key != NULL)
1470       zir_msg->public_key = *zone_key;
1471     memcpy (name_tmp, name, name_len);
1472     memcpy (rd_tmp, rd_ser, rd_ser_len);
1473
1474     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message with size %u\n", "ZONE_ITERATION_RESPONSE", msg_size);
1475     GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) zir_msg, GNUNET_NO);
1476     GNUNET_free (zir_msg);
1477   }
1478 }
1479
1480 static void handle_iteration_start (void *cls,
1481                           struct GNUNET_SERVER_Client * client,
1482                           const struct GNUNET_MessageHeader * message)
1483 {
1484   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1485
1486   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1487   struct GNUNET_NAMESTORE_Client *nc;
1488   struct GNUNET_NAMESTORE_ZoneIteration *zi;
1489
1490   nc = client_lookup(client);
1491   if (nc == NULL)
1492   {
1493     GNUNET_break_op (0);
1494     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1495     return;
1496   }
1497
1498   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1499   zi->request_id = ntohl (zis_msg->gns_header.r_id);
1500   zi->offset = 0;
1501   zi->client = nc;
1502   zi->zone = zis_msg->zone;
1503
1504   GNUNET_HashCode dummy;
1505   GNUNET_HashCode *zone_tmp;
1506   memset (&dummy, '\0', sizeof (dummy));
1507   if (0 == memcmp (&dummy, &zis_msg->zone, sizeof (dummy)))
1508   {
1509     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over all zones\n");
1510     zi->has_zone = GNUNET_NO;
1511     zone_tmp = NULL;
1512   }
1513   else
1514   {
1515     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over zone  `%s'\n", GNUNET_h2s (&zis_msg->zone));
1516     zi->has_zone = GNUNET_YES;
1517     zone_tmp = &zis_msg->zone;
1518   }
1519
1520   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1521
1522   GSN_database->iterate_records (GSN_database->cls, zone_tmp , NULL, zi->offset , &zone_iteration_proc, zi);
1523   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1524 }
1525
1526 static void handle_iteration_stop (void *cls,
1527                           struct GNUNET_SERVER_Client * client,
1528                           const struct GNUNET_MessageHeader * message)
1529 {
1530   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1531
1532   struct GNUNET_NAMESTORE_Client *nc;
1533   struct GNUNET_NAMESTORE_ZoneIteration *zi;
1534   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1535   uint32_t rid;
1536
1537   nc = client_lookup(client);
1538   if (nc == NULL)
1539   {
1540     GNUNET_break_op (0);
1541     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1542     return;
1543   }
1544
1545   rid = ntohl (zis_msg->gns_header.r_id);
1546   for (zi = nc->op_head; zi != NULL; zi = zi->next)
1547   {
1548     if (zi->request_id == rid)
1549       break;
1550   }
1551   if (zi == NULL)
1552   {
1553     GNUNET_break_op (0);
1554     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1555     return;
1556   }
1557
1558   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1559   if (GNUNET_YES == zi->has_zone)
1560     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1561   else
1562     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration all zones\n");
1563   GNUNET_free (zi);
1564
1565   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1566 }
1567
1568 static void handle_iteration_next (void *cls,
1569                           struct GNUNET_SERVER_Client * client,
1570                           const struct GNUNET_MessageHeader * message)
1571 {
1572   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1573
1574   struct GNUNET_NAMESTORE_Client *nc;
1575   struct GNUNET_NAMESTORE_ZoneIteration *zi;
1576   GNUNET_HashCode *zone_tmp;
1577   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1578   uint32_t rid;
1579
1580   nc = client_lookup(client);
1581   if (nc == NULL)
1582   {
1583     GNUNET_break_op (0);
1584     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1585     return;
1586   }
1587
1588   rid = ntohl (zis_msg->gns_header.r_id);
1589   for (zi = nc->op_head; zi != NULL; zi = zi->next)
1590   {
1591     if (zi->request_id == rid)
1592       break;
1593   }
1594   if (zi == NULL)
1595   {
1596     GNUNET_break_op (0);
1597     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1598     return;
1599   }
1600
1601   if (GNUNET_YES == zi->has_zone)
1602     zone_tmp = &zi->zone;
1603   else
1604     zone_tmp = NULL;
1605
1606   zi->offset++;
1607   GSN_database->iterate_records (GSN_database->cls, zone_tmp, NULL, zi->offset , &zone_iteration_proc, zi);
1608   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1609 }
1610
1611 int zonekey_file_it (void *cls, const char *filename)
1612 {
1613   int *counter = cls;
1614    if ((filename != NULL) && (NULL != strstr(filename, ".zkey")))
1615    {
1616      struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
1617      struct GNUNET_NAMESTORE_CryptoContainer *c;
1618      privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
1619      if (privkey == NULL)
1620        return GNUNET_OK;
1621
1622      c = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1623      c->pubkey = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1624      c->privkey = privkey;
1625      GNUNET_CRYPTO_rsa_key_get_public(privkey, c->pubkey);
1626      GNUNET_CRYPTO_hash(c->pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &c->zone);
1627
1628      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found zonefile for zone `%s'\n", GNUNET_h2s (&c->zone));
1629
1630      GNUNET_CONTAINER_multihashmap_put(zonekeys, &c->zone, c, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1631      (*counter) ++;
1632    }
1633    return GNUNET_OK;
1634 }
1635
1636
1637 /**
1638  * Process template requests.
1639  *
1640  * @param cls closure
1641  * @param server the initialized server
1642  * @param cfg configuration to use
1643  */
1644 static void
1645 run (void *cls, struct GNUNET_SERVER_Handle *server,
1646      const struct GNUNET_CONFIGURATION_Handle *cfg)
1647 {
1648   char * database;
1649   int counter = 0;
1650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1651
1652   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1653     {&handle_start, NULL,
1654      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1655     {&handle_lookup_name, NULL,
1656      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1657     {&handle_record_put, NULL,
1658     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1659     {&handle_record_create, NULL,
1660      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1661     {&handle_record_remove, NULL,
1662      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1663     {&handle_zone_to_name, NULL,
1664       GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1665     {&handle_iteration_start, NULL,
1666      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1667     {&handle_iteration_next, NULL,
1668      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1669      {&handle_iteration_stop, NULL,
1670       GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1671     {NULL, NULL, 0, 0}
1672   };
1673
1674   GSN_cfg = cfg;
1675
1676   /* Load private keys from disk */
1677   if (GNUNET_OK !=
1678       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore", "zonefile_directory",
1679                                              &zonefile_directory))
1680   {
1681     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No directory to load zonefiles specified in configuration\n"));
1682     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1683     return;
1684   }
1685
1686   if (GNUNET_NO == GNUNET_DISK_file_test (zonefile_directory))
1687   {
1688     if (GNUNET_SYSERR == GNUNET_DISK_directory_create (zonefile_directory))
1689     {
1690       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Creating directory `%s' for zone files failed!\n"), zonefile_directory);
1691       GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1692       return;
1693     }
1694     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created directory `%s' for zone files\n", zonefile_directory);
1695   }
1696
1697   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scanning directory `%s' for zone files\n", zonefile_directory);
1698   zonekeys = GNUNET_CONTAINER_multihashmap_create (10);
1699   GNUNET_DISK_directory_scan (zonefile_directory, zonekey_file_it, &counter);
1700   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u zone files\n", counter);
1701
1702   /* Loading database plugin */
1703   if (GNUNET_OK !=
1704       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1705                                              &database))
1706     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1707
1708   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1709   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1710   GNUNET_free (database);
1711   if (GSN_database == NULL)
1712   {
1713     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1714         db_lib_name);
1715     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1716     return;
1717   }
1718
1719   /* Configuring server handles */
1720   GNUNET_SERVER_add_handlers (server, handlers);
1721   snc = GNUNET_SERVER_notification_context_create (server, 16);
1722   GNUNET_SERVER_disconnect_notify (server,
1723                                    &client_disconnect_notification,
1724                                    NULL);
1725
1726   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1727                                 NULL);
1728
1729 }
1730
1731
1732 /**
1733  * The main function for the template service.
1734  *
1735  * @param argc number of arguments from the command line
1736  * @param argv command line arguments
1737  * @return 0 ok, 1 on error
1738  */
1739 int
1740 main (int argc, char *const *argv)
1741 {
1742   return (GNUNET_OK ==
1743           GNUNET_SERVICE_run (argc, argv, "namestore",
1744                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1745 }
1746
1747 /* end of gnunet-service-namestore.c */
1748