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