- test pass (not on sparc)
[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   for (nc = client_head; nc != NULL; nc = next)
105   {
106     next = nc->next;
107     for (no = nc->op_head; no != NULL; no = tmp)
108     {
109       GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
110       tmp = no->next;
111       GNUNET_free (no);
112     }
113
114     GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
115     GNUNET_free (nc);
116
117   }
118
119   GNUNET_SERVER_notification_context_destroy (snc);
120   snc = NULL;
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 /**
143  * Called whenever a client is disconnected.  Frees our
144  * resources associated with that client.
145  *
146  * @param cls closure
147  * @param client identification of the client
148  */
149 static void
150 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
151 {
152   struct GNUNET_NAMESTORE_ZoneIteration * no;
153   struct GNUNET_NAMESTORE_Client * nc;
154   if (NULL == client)
155     return;
156
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
158
159   nc = client_lookup (client);
160
161   if ((NULL == client) || (NULL == nc))
162     return;
163
164   for (no = nc->op_head; no != NULL; no = no->next)
165   {
166     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
167     GNUNET_free (no);
168   }
169
170   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
171   GNUNET_free (nc);
172 }
173
174 static void handle_start (void *cls,
175                           struct GNUNET_SERVER_Client * client,
176                           const struct GNUNET_MessageHeader * message)
177 {
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
179
180   struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
181   nc->client = client;
182   GNUNET_SERVER_notification_context_add (snc, client);
183   GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
184
185   GNUNET_SERVER_receive_done (client, GNUNET_OK);
186 }
187
188 struct LookupNameContext
189 {
190   struct GNUNET_NAMESTORE_Client *nc;
191   uint32_t request_id;
192   uint32_t record_type;
193 };
194
195
196
197
198 static void
199 handle_lookup_name_it (void *cls,
200     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
201     struct GNUNET_TIME_Absolute expire,
202     const char *name,
203     unsigned int rd_count,
204     const struct GNUNET_NAMESTORE_RecordData *rd,
205     const struct GNUNET_CRYPTO_RsaSignature *signature)
206 {
207   /* send response */
208   struct LookupNameContext *lnc = cls;
209   struct LookupNameResponseMessage *lnr_msg;
210
211   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
212   struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
213   char *rd_tmp;
214   char *name_tmp;
215   size_t rd_ser_len;
216   struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
219
220   size_t r_size = 0;
221
222   size_t name_len = 0;
223   if (NULL != name)
224     name_len = strlen(name) + 1;
225
226   int copied_elements = 0;
227   int contains_signature = 0;
228   int c;
229
230   /* count records to copy */
231   if (rd_count != 0)
232   {
233     if (lnc->record_type != 0)
234     {
235       /* special record type needed */
236       for (c = 0; c < rd_count; c ++)
237         if (rd[c].record_type == lnc->record_type)
238           copied_elements++; /* found matching record */
239       rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
240       copied_elements = 0;
241       for (c = 0; c < rd_count; c ++)
242       {
243         if (rd[c].record_type == lnc->record_type)
244         {
245           /* found matching record */
246           memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
247           copied_elements++;
248         }
249       }
250     }
251     else
252     {
253       copied_elements = rd_count;
254       rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
255     }
256   }
257   else
258   {
259     /* No results */
260     copied_elements = 0;
261     rd_selected = NULL;
262     expire = GNUNET_TIME_UNIT_ZERO_ABS;
263   }
264
265
266
267   rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
268   char rd_ser[rd_ser_len];
269   GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
270
271
272   if ((copied_elements == rd_count) && (signature != NULL))
273       contains_signature = GNUNET_YES;
274
275   if (rd_selected != rd)
276     GNUNET_free (rd_selected);
277
278   r_size = sizeof (struct LookupNameResponseMessage) +
279            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
280            name_len +
281            rd_ser_len +
282            contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
283
284   lnr_msg = GNUNET_malloc (r_size);
285
286   lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
287   lnr_msg->gns_header.header.size = ntohs (r_size);
288   lnr_msg->gns_header.r_id = htonl (lnc->request_id);
289   lnr_msg->rd_count = htons (rd_count);
290   lnr_msg->rd_len = htons (rd_ser_len);
291   lnr_msg->name_len = htons (name_len);
292   lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
293   lnr_msg->contains_sig = htons (contains_signature);
294
295   zone_key_tmp =  (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
296   name_tmp = (char *) &zone_key_tmp[1];
297   rd_tmp = &name_tmp[name_len];
298   signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_ser_len];
299
300   if (zone_key != NULL)
301     memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
302   else
303   {
304     struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
305     memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
306     memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
307   }
308   memcpy (name_tmp, name, name_len);
309   memcpy (rd_tmp, rd_ser, rd_ser_len);
310
311   if (GNUNET_YES == contains_signature)
312     memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
313   GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
314
315   GNUNET_free (lnr_msg);
316 }
317
318 static void handle_lookup_name (void *cls,
319                           struct GNUNET_SERVER_Client * client,
320                           const struct GNUNET_MessageHeader * message)
321 {
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
323   struct LookupNameContext lnc;
324   struct GNUNET_NAMESTORE_Client *nc;
325   size_t name_len;
326   char * name;
327   uint32_t rid = 0;
328   uint32_t type = 0;
329   int res;
330
331   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
332   {
333     GNUNET_break_op (0);
334     GNUNET_SERVER_receive_done (client, GNUNET_OK);
335     return;
336   }
337
338   nc = client_lookup(client);
339   if (nc == NULL)
340   {
341     GNUNET_break_op (0);
342     GNUNET_SERVER_receive_done (client, GNUNET_OK);
343     return;
344   }
345
346   struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
347   rid = ntohl (ln_msg->gns_header.r_id);
348   name_len = ntohl (ln_msg->name_len);
349   type = ntohl (ln_msg->record_type);
350
351   if ((name_len == 0) || (name_len > 256))
352   {
353     GNUNET_break_op (0);
354     GNUNET_SERVER_receive_done (client, GNUNET_OK);
355     return;
356   }
357
358   name = (char *) &ln_msg[1];
359   if (name[name_len -1] != '\0')
360   {
361     GNUNET_break_op (0);
362     GNUNET_SERVER_receive_done (client, GNUNET_OK);
363     return;
364   }
365
366   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
367
368   /* do the actual lookup */
369   lnc.request_id = rid;
370   lnc.nc = nc;
371   lnc.record_type = type;
372   res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
373
374   GNUNET_SERVER_receive_done (client, GNUNET_OK);
375 }
376
377 static void handle_record_put (void *cls,
378                           struct GNUNET_SERVER_Client * client,
379                           const struct GNUNET_MessageHeader * message)
380 {
381   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
382   struct GNUNET_NAMESTORE_Client *nc;
383   struct GNUNET_TIME_Absolute expire;
384   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
385   struct GNUNET_CRYPTO_RsaSignature *signature;
386   struct RecordPutResponseMessage rpr_msg;
387   size_t name_len;
388   size_t msg_size;
389   size_t msg_size_exp;
390   size_t key_len;
391   char * name;
392   char * rd_ser;
393   uint32_t rid = 0;
394   uint32_t rd_ser_len;
395   uint32_t rd_count;
396   int res = GNUNET_SYSERR;
397
398   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
399   {
400     GNUNET_break_op (0);
401     GNUNET_SERVER_receive_done (client, GNUNET_OK);
402     return;
403   }
404
405   nc = client_lookup (client);
406   if (nc == NULL)
407   {
408     GNUNET_break_op (0);
409     GNUNET_SERVER_receive_done (client, GNUNET_OK);
410     return;
411   }
412
413   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
414
415   rid = ntohl (rp_msg->gns_header.r_id);
416   msg_size = ntohs (rp_msg->gns_header.header.size);
417   key_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
418   name_len = ntohs (rp_msg->name_len);
419   rd_count = ntohs (rp_msg->rd_count);
420   rd_ser_len = ntohs(rp_msg->rd_len);
421   msg_size_exp = sizeof (struct RecordPutMessage) + key_len + name_len  + rd_ser_len;
422
423   if (msg_size != msg_size_exp)
424   {
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
426     GNUNET_break_op (0);
427     GNUNET_SERVER_receive_done (client, GNUNET_OK);
428     return;
429   }
430
431
432   if ((name_len == 0) || (name_len > 256))
433   {
434     GNUNET_break_op (0);
435     GNUNET_SERVER_receive_done (client, GNUNET_OK);
436     return;
437   }
438
439   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
440   name =  &((char *)zone_key)[key_len];
441
442   if (name[name_len -1] != '\0')
443   {
444     GNUNET_break_op (0);
445     GNUNET_SERVER_receive_done (client, GNUNET_OK);
446     return;
447   }
448
449   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
450   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
451
452   rd_ser = &name[name_len];
453   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
454   GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
455
456   GNUNET_HashCode zone_hash;
457   GNUNET_CRYPTO_hash (zone_key, key_len, &zone_hash);
458   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT ZONE HASH: `%s'\n", GNUNET_h2s_full(&zone_hash));
459
460
461   /* Database operation */
462   res = GSN_database->put_records(GSN_database->cls,
463                                 zone_key,
464                                 expire,
465                                 name,
466                                 rd_count, rd,
467                                 signature);
468
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
470       name, (res == GNUNET_OK) ? "OK" : "FAIL");
471
472   /* Send response */
473
474   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
475   rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
476   rpr_msg.op_id = rp_msg->gns_header.r_id;
477   rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
478   if (GNUNET_OK == res)
479     rpr_msg.op_result = htons (GNUNET_OK);
480   else
481     rpr_msg.op_result = htons (GNUNET_NO);
482   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
483
484   GNUNET_SERVER_receive_done (client, GNUNET_OK);
485 }
486
487 struct CreateRecordContext
488 {
489   struct GNUNET_NAMESTORE_RecordData *rd;
490   struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
491   struct GNUNET_TIME_Absolute expire;
492   uint32_t op_id;
493   struct GNUNET_NAMESTORE_Client *nc;
494 };
495
496 struct GNUNET_CRYPTO_RsaSignature *
497 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key, struct GNUNET_NAMESTORE_RecordData *rd, unsigned int rd_count)
498 {
499   struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
500   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
501   size_t rd_ser_len;
502
503   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
504   char rd_ser[rd_ser_len];
505   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
506
507   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len);
508
509   sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len);
510   sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
511   memcpy (&sig_purpose[1], rd_ser, rd_ser_len);
512
513   GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
514
515   GNUNET_free (sig_purpose);
516   return sig;
517 }
518
519 static void
520 handle_create_record_it (void *cls,
521     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
522     struct GNUNET_TIME_Absolute expire,
523     const char *name,
524     unsigned int rd_count,
525     const struct GNUNET_NAMESTORE_RecordData *rd,
526     const struct GNUNET_CRYPTO_RsaSignature *signature)
527 {
528   struct CreateRecordContext * crc = cls;
529   struct GNUNET_CRYPTO_RsaSignature *signature_new;
530   struct RecordCreateResponseMessage rcr_msg;
531   int res;
532
533   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, name);
534   struct GNUNET_NAMESTORE_RecordData *rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
535   memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
536
537   rd_new[rd_count] = *(crc->rd);
538
539   signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, rd_new, rd_count+1);
540
541   /* Database operation */
542   res = GSN_database->put_records(GSN_database->cls,
543                                 zone_key,
544                                 expire,
545                                 name,
546                                 rd_count +1, rd_new,
547                                 signature_new);
548
549   GNUNET_free (rd_new);
550   GNUNET_free (signature_new);
551
552   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name %u\n", res);
553   /* Send response */
554
555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
556   rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
557   rcr_msg.gns_header.r_id = htonl (crc->op_id);
558   rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
559   if (GNUNET_OK == res)
560     rcr_msg.op_result = htons (GNUNET_OK);
561   else
562     rcr_msg.op_result = htons (GNUNET_NO);
563   GNUNET_SERVER_notification_context_unicast (snc, crc->nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
564
565 }
566
567 static void handle_record_create (void *cls,
568                           struct GNUNET_SERVER_Client * client,
569                           const struct GNUNET_MessageHeader * message)
570 {
571   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
572   struct GNUNET_NAMESTORE_Client *nc;
573   struct CreateRecordContext crc;
574   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
575   GNUNET_HashCode pubkey_hash;
576   size_t name_len;
577   size_t msg_size;
578   size_t msg_size_exp;
579   size_t rd_ser_len;
580   size_t key_len;
581   uint32_t rid = 0;
582   char *pkey_tmp;
583   char *name_tmp;
584   char *rd_ser;
585   int rd_count;
586
587
588   int res = GNUNET_SYSERR;
589
590   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
591   {
592     GNUNET_break_op (0);
593     GNUNET_SERVER_receive_done (client, GNUNET_OK);
594     return;
595   }
596
597   nc = client_lookup(client);
598   if (nc == NULL)
599   {
600     GNUNET_break_op (0);
601     GNUNET_SERVER_receive_done (client, GNUNET_OK);
602     return;
603   }
604
605   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
606   rid = ntohl (rp_msg->gns_header.r_id);
607   name_len = ntohs (rp_msg->name_len);
608   msg_size = ntohs (message->size);
609   rd_count = ntohs (rp_msg->rd_count);
610   rd_ser_len = ntohs (rp_msg->rd_len);
611   key_len = ntohs (rp_msg->pkey_len);
612   msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
613
614   if (msg_size != msg_size_exp)
615   {
616     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
617     GNUNET_break_op (0);
618     GNUNET_SERVER_receive_done (client, GNUNET_OK);
619     return;
620   }
621
622   if ((name_len == 0) || (name_len > 256))
623   {
624     GNUNET_break_op (0);
625     GNUNET_SERVER_receive_done (client, GNUNET_OK);
626     return;
627   }
628
629   pkey_tmp = (char *) &rp_msg[1];
630   name_tmp = &pkey_tmp[key_len];
631   rd_ser = &name_tmp[name_len];
632
633   if (name_tmp[name_len -1] != '\0')
634   {
635     GNUNET_break_op (0);
636     GNUNET_SERVER_receive_done (client, GNUNET_OK);
637     return;
638   }
639
640   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
641   GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
642   GNUNET_assert (rd_count == 1);
643
644   /* Extracting and converting private key */
645   struct GNUNET_CRYPTO_RsaPrivateKey *pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
646   GNUNET_assert (pkey != NULL);
647   GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
648   GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
649
650   crc.pkey = pkey;
651   crc.rd = rd;
652   crc.nc = nc;
653   crc.op_id = rid;
654
655   /* Get existing records for name */
656   res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
657
658   GNUNET_CRYPTO_rsa_key_free(pkey);
659
660   GNUNET_SERVER_receive_done (client, GNUNET_OK);
661 }
662
663
664
665 static void handle_record_remove (void *cls,
666                           struct GNUNET_SERVER_Client * client,
667                           const struct GNUNET_MessageHeader * message)
668 {
669   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
670   struct GNUNET_NAMESTORE_Client *nc;
671   struct RecordRemoveResponseMessage rrr_msg;
672   size_t name_len;
673   size_t msg_size;
674   size_t msg_size_exp;
675   uint32_t rid = 0;
676
677   int res = GNUNET_SYSERR;
678
679   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
680   {
681     GNUNET_break_op (0);
682     GNUNET_SERVER_receive_done (client, GNUNET_OK);
683     return;
684   }
685
686   nc = client_lookup(client);
687   if (nc == NULL)
688   {
689     GNUNET_break_op (0);
690     GNUNET_SERVER_receive_done (client, GNUNET_OK);
691     return;
692   }
693
694   struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
695   rid = ntohl (rp_msg->gns_header.r_id);
696   name_len = ntohs (rp_msg->name_len);
697   msg_size = ntohs (message->size);
698   msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
699
700   if (msg_size != msg_size_exp)
701   {
702     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
703     GNUNET_break_op (0);
704     GNUNET_SERVER_receive_done (client, GNUNET_OK);
705     return;
706   }
707
708
709   if ((name_len == 0) || (name_len > 256))
710   {
711     GNUNET_break_op (0);
712     GNUNET_SERVER_receive_done (client, GNUNET_OK);
713     return;
714   }
715 /*
716   if (name[name_len -1] != '\0')
717   {
718     GNUNET_break_op (0);
719     GNUNET_SERVER_receive_done (client, GNUNET_OK);
720     return;
721   }
722   */
723   /* DO WORK HERE */
724
725   /* Send response */
726
727   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
728   rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
729   rrr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
730   rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
731   if (GNUNET_OK == res)
732     rrr_msg.op_result = htons (GNUNET_OK);
733   else
734     rrr_msg.op_result = htons (GNUNET_NO);
735   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
736
737   GNUNET_SERVER_receive_done (client, GNUNET_OK);
738 }
739
740 struct ZoneIterationProcResult
741 {
742   int have_zone_key;
743   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
744
745   int have_signature;
746   struct GNUNET_CRYPTO_RsaSignature signature;
747   struct GNUNET_TIME_Absolute expire;
748
749   int have_name;
750   char name[256];
751
752   size_t rd_ser_len;
753   char *rd_ser;
754 };
755
756
757 void zone_iteration_proc (void *cls,
758                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
759                          struct GNUNET_TIME_Absolute expire,
760                          const char *name,
761                          unsigned int rd_count,
762                          const struct GNUNET_NAMESTORE_RecordData *rd,
763                          const struct GNUNET_CRYPTO_RsaSignature *signature)
764 {
765   struct ZoneIterationProcResult *zipr = cls;
766   size_t len;
767   if (zone_key != NULL)
768   {
769     zipr->zone_key = *zone_key;
770     zipr->have_zone_key = GNUNET_YES;
771   }
772   else
773     zipr->have_zone_key = GNUNET_NO;
774
775   zipr->expire = expire;
776
777   if (name != NULL)
778   {
779     memcpy (zipr->name, name, strlen(name) + 1);
780     zipr->have_name = GNUNET_YES;
781   }
782   else
783     zipr->have_name = GNUNET_NO;
784
785   if (signature != NULL)
786   {
787     zipr->signature = *signature;
788     zipr->have_signature = GNUNET_YES;
789   }
790   else
791     zipr->have_signature = GNUNET_NO;
792
793   if ((rd_count > 0) && (rd != NULL))
794   {
795     len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
796     zipr->rd_ser = GNUNET_malloc (len);
797     GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
798     zipr->rd_ser_len = len;
799   }
800 }
801
802 static void handle_iteration_start (void *cls,
803                           struct GNUNET_SERVER_Client * client,
804                           const struct GNUNET_MessageHeader * message)
805 {
806   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
807
808   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
809   struct GNUNET_NAMESTORE_Client *nc;
810   struct GNUNET_NAMESTORE_ZoneIteration *zi;
811   struct ZoneIterationResponseMessage zir_msg;
812   struct ZoneIterationProcResult zipr;
813   int res;
814
815   nc = client_lookup(client);
816   if (nc == NULL)
817   {
818     GNUNET_break_op (0);
819     GNUNET_SERVER_receive_done (client, GNUNET_OK);
820     return;
821   }
822
823   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
824   zi->request_id = ntohl (zis_msg->gns_header.r_id);
825   zi->offset = 0;
826   zi->client = nc;
827   zi->zone = zis_msg->zone;
828
829   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
830
831   res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
832   switch (res) {
833     case GNUNET_OK:
834       /* GNUNET_OK on success */
835
836       break;
837     case GNUNET_SYSERR:
838       /* GNUNET_SYSERR on error */
839       break;
840     case GNUNET_NO:
841       /* GNUNET_NO if there were no results, */
842       break;
843     default:
844       break;
845   }
846
847
848
849   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
850   zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
851   zir_msg.gns_header.r_id = htonl(zi->request_id);
852   zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
853
854
855   GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
856
857
858   GNUNET_SERVER_receive_done (client, GNUNET_OK);
859 }
860
861 static void handle_iteration_stop (void *cls,
862                           struct GNUNET_SERVER_Client * client,
863                           const struct GNUNET_MessageHeader * message)
864 {
865   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
866
867   struct GNUNET_NAMESTORE_Client *nc;
868   struct GNUNET_NAMESTORE_ZoneIteration *zi;
869   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
870   uint32_t rid;
871
872   nc = client_lookup(client);
873   if (nc == NULL)
874   {
875     GNUNET_break_op (0);
876     GNUNET_SERVER_receive_done (client, GNUNET_OK);
877     return;
878   }
879
880   rid = ntohl (zis_msg->gns_header.r_id);
881   for (zi = nc->op_head; zi != NULL; zi = zi->next)
882   {
883     if (zi->request_id == rid)
884       break;
885   }
886   if (zi == NULL)
887   {
888     GNUNET_break_op (0);
889     GNUNET_SERVER_receive_done (client, GNUNET_OK);
890     return;
891   }
892
893   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
894   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
895   GNUNET_free (zi);
896
897   GNUNET_SERVER_receive_done (client, GNUNET_OK);
898 }
899
900 static void handle_iteration_next (void *cls,
901                           struct GNUNET_SERVER_Client * client,
902                           const struct GNUNET_MessageHeader * message)
903 {
904   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
905
906   struct GNUNET_NAMESTORE_Client *nc;
907   struct GNUNET_NAMESTORE_ZoneIteration *zi;
908   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
909   uint32_t rid;
910   int res;
911
912   nc = client_lookup(client);
913   if (nc == NULL)
914   {
915     GNUNET_break_op (0);
916     GNUNET_SERVER_receive_done (client, GNUNET_OK);
917     return;
918   }
919
920   rid = ntohl (zis_msg->gns_header.r_id);
921   for (zi = nc->op_head; zi != NULL; zi = zi->next)
922   {
923     if (zi->request_id == rid)
924       break;
925   }
926   if (zi == NULL)
927   {
928     GNUNET_break_op (0);
929     GNUNET_SERVER_receive_done (client, GNUNET_OK);
930     return;
931   }
932
933   zi->offset++;
934   res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
935 }
936
937
938
939 /**
940  * Process template requests.
941  *
942  * @param cls closure
943  * @param server the initialized server
944  * @param cfg configuration to use
945  */
946 static void
947 run (void *cls, struct GNUNET_SERVER_Handle *server,
948      const struct GNUNET_CONFIGURATION_Handle *cfg)
949 {
950   char * database;
951
952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
953
954   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
955     {&handle_start, NULL,
956      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
957     {&handle_lookup_name, NULL,
958      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
959     {&handle_record_put, NULL,
960     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
961     {&handle_record_create, NULL,
962      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
963     {&handle_record_remove, NULL,
964      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
965     {&handle_iteration_start, NULL,
966      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
967     {&handle_iteration_stop, NULL,
968      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
969     {&handle_iteration_next, NULL,
970      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
971     {NULL, NULL, 0, 0}
972   };
973
974   GSN_cfg = cfg;
975
976   /* Loading database plugin */
977   if (GNUNET_OK !=
978       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
979                                              &database))
980     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
981
982   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
983   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
984   if (GSN_database == NULL)
985     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
986         db_lib_name);
987   GNUNET_free (database);
988
989   /* Configuring server handles */
990   GNUNET_SERVER_add_handlers (server, handlers);
991   snc = GNUNET_SERVER_notification_context_create (server, 16);
992   GNUNET_SERVER_disconnect_notify (server,
993                                    &client_disconnect_notification,
994                                    NULL);
995
996   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
997                                 NULL);
998
999 }
1000
1001
1002 /**
1003  * The main function for the template service.
1004  *
1005  * @param argc number of arguments from the command line
1006  * @param argv command line arguments
1007  * @return 0 ok, 1 on error
1008  */
1009 int
1010 main (int argc, char *const *argv)
1011 {
1012   return (GNUNET_OK ==
1013           GNUNET_SERVICE_run (argc, argv, "namestore",
1014                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1015 }
1016
1017 /* end of gnunet-service-namestore.c */