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