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