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