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