-new short hashes, new short hash cmp
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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  *
23  * TODO:
24  *    - Write xquery and block plugin
25  *    - The smaller FIXME issues all around
26  *
27  * @file gns/gnunet-service-gns.c
28  * @brief GNUnet GNS service
29  * @author Martin Schanzenbach
30  */
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
40 #include "gns.h"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet-service-gns_interceptor.h"
43
44 /* FIXME move to proper header in include */
45 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
46 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
47 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
48 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
49 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
50 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
51
52
53 /**
54  * Handle to a shorten operation from api
55  */
56 struct ClientShortenHandle
57 {
58   /* the requesting client that */
59   struct GNUNET_SERVER_Client *client;
60
61   /* request id */
62   uint64_t unique_id;
63
64   /* request type */
65   enum GNUNET_GNS_RecordType type;
66
67   /* name to shorten */
68   char* name;
69
70 };
71
72
73 /**
74  * Handle to a get auhtority operation from api
75  */
76 struct ClientGetAuthHandle
77 {
78   /* the requesting client that */
79   struct GNUNET_SERVER_Client *client;
80
81   /* request id */
82   uint64_t unique_id;
83
84   /* name to lookup authority */
85   char* name;
86
87 };
88
89
90 /**
91  * Handle to a lookup operation from api
92  */
93 struct ClientLookupHandle
94 {
95   /* the requesting client that */
96   struct GNUNET_SERVER_Client *client;
97
98   /* request id */
99   uint64_t unique_id;
100
101   /* request type */
102   enum GNUNET_GNS_RecordType type;
103
104   /* the name to look up */
105   char* name; //Needed?
106 };
107
108 /**
109  * Our handle to the DHT
110  */
111 static struct GNUNET_DHT_Handle *dht_handle;
112
113 /**
114  * Our zone's private key
115  */
116 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
117
118 /**
119  * Our handle to the namestore service
120  * FIXME maybe need a second handle for iteration
121  */
122 struct GNUNET_NAMESTORE_Handle *namestore_handle;
123
124 /**
125  * Handle to iterate over our authoritative zone in namestore
126  */
127 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
128
129 /**
130  * The configuration the GNS service is running with
131  */
132 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
133
134 /**
135  * Our notification context.
136  */
137 static struct GNUNET_SERVER_NotificationContext *nc;
138
139 /**
140  * Our zone hash
141  */
142 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
143
144 /**
145  * Useful for zone update for DHT put
146  */
147 static int num_public_records =  3600;
148
149 /* dht update interval FIXME define? */
150 static struct GNUNET_TIME_Relative dht_update_interval;
151
152 /* zone update task */
153 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
154
155 /**
156  * Task run during shutdown.
157  *
158  * @param cls unused
159  * @param tc unused
160  */
161 static void
162 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164
165   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
166              "Shutting down!");
167   /* Kill zone task for it may make the scheduler hang */
168   if (zone_update_taskid)
169     GNUNET_SCHEDULER_cancel(zone_update_taskid);
170   
171   GNUNET_SERVER_notification_context_destroy (nc);
172   
173   gns_interceptor_stop();
174
175   GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
176   GNUNET_DHT_disconnect(dht_handle);
177 }
178
179
180 /**
181  * Method called periodicattluy that triggers
182  * iteration over root zone
183  *
184  * @param cls closure
185  * @param tc task context
186  */
187 static void
188 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190   GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
191 }
192
193 /**
194  * Continuation for DHT put
195  *
196  * @param cls closure
197  * @param tc task context
198  */
199 static void
200 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
201 {
202   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
203 }
204
205 /* prototype */
206 static void
207 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
208
209 /**
210  * Function used to put all records successively into the DHT.
211  *
212  * @param cls the closure (NULL)
213  * @param key the public key of the authority (ours)
214  * @param expiration lifetime of the namestore entry
215  * @param name the name of the records
216  * @param rd_count the number of records in data
217  * @param rd the record data
218  * @param signature the signature for the record data
219  */
220 static void
221 put_gns_record(void *cls,
222                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
223                 struct GNUNET_TIME_Absolute expiration,
224                 const char *name,
225                 unsigned int rd_count,
226                 const struct GNUNET_NAMESTORE_RecordData *rd,
227                 const struct GNUNET_CRYPTO_RsaSignature *signature)
228 {
229   
230   struct GNSNameRecordBlock *nrb;
231   struct GNUNET_CRYPTO_ShortHashCode name_hash;
232   GNUNET_HashCode xor_hash;
233   GNUNET_HashCode name_hash_double;
234   GNUNET_HashCode zone_hash_double;
235   struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
236   uint32_t rd_payload_length;
237   char* nrb_data = NULL;
238   size_t namelen;
239
240   /* we're done */
241   if (NULL == name)
242   {
243     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
244                "Zone iteration finished. Rescheduling put in %ds\n",
245                GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
246     zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
247                                         GNUNET_TIME_relative_multiply(
248                                             GNUNET_TIME_UNIT_SECONDS,
249                                             GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL
250                                             ),
251                                             &update_zone_dht_start,
252                                             NULL);
253     return;
254   }
255   
256   namelen = strlen(name) + 1;
257   
258   if (signature == NULL)
259   {
260     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
261                "No signature for %s record data provided! Skipping...\n",
262                name);
263     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
264                                                    NULL);
265     return;
266
267   }
268   
269   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
270              "Putting records for %s into the DHT\n", name);
271   
272   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
273   
274   nrb = GNUNET_malloc(rd_payload_length + namelen
275                       + sizeof(struct GNSNameRecordBlock));
276   
277   nrb->signature = *signature;
278   
279   nrb->public_key = *key;
280
281   nrb->rd_count = htonl(rd_count);
282   
283   memcpy(&nrb[1], name, namelen);
284
285   nrb_data = (char*)&nrb[1];
286   nrb_data += namelen;
287
288   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
289
290   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
291                                                 rd,
292                                                 rd_payload_length,
293                                                 nrb_data))
294   {
295     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
296                "Record serialization failed! Skipping...\n");
297     GNUNET_free(nrb);
298     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
299                                                    NULL);
300     return;
301   }
302
303
304   /*
305    * calculate DHT key: H(name) xor H(pubkey)
306    */
307   GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
308   GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
309   GNUNET_CRYPTO_short_hash_double (&zone_hash, &zone_hash_double);
310   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
311   GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
312
313   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
314              "putting records for %s under key: %s with size %d\n",
315              name, (char*)&xor_hash_string, rd_payload_length);
316
317   GNUNET_DHT_put (dht_handle, &xor_hash,
318                   DHT_GNS_REPLICATION_LEVEL,
319                   GNUNET_DHT_RO_NONE,
320                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
321                   rd_payload_length,
322                   (char*)nrb,
323                   expiration,
324                   DHT_OPERATION_TIMEOUT,
325                   &record_dht_put,
326                   NULL); //cls for cont
327   
328   num_public_records++;
329
330   /**
331    * Reschedule periodic put
332    */
333   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
334                                 &update_zone_dht_next,
335                                 NULL);
336
337   GNUNET_free(nrb);
338
339 }
340
341 /**
342  * Periodically iterate over our zone and store everything in dht
343  *
344  * @param cls NULL
345  * @param tc task context
346  */
347 static void
348 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
349 {
350   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
351   if (0 == num_public_records)
352   {
353     dht_update_interval = GNUNET_TIME_relative_multiply(
354                                             GNUNET_TIME_UNIT_SECONDS,
355                                             GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
356     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
357                "No records in db. Adjusted DHT update interval to %ds\n",
358                GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
359   }
360   else
361   {
362     
363     dht_update_interval = GNUNET_TIME_relative_multiply(
364                                                       GNUNET_TIME_UNIT_SECONDS,
365                                                      (3600/num_public_records));
366     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
367                "Adjusted DHT update interval to %ds!\n",
368                (3600/num_public_records));
369   }
370
371   /* start counting again */
372   num_public_records = 0;
373   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
374                                                  &zone_hash,
375                                                  GNUNET_NAMESTORE_RF_AUTHORITY,
376                                                  GNUNET_NAMESTORE_RF_PRIVATE,
377                                                  &put_gns_record,
378                                                  NULL);
379 }
380
381
382 /* END DHT ZONE PROPAGATION */
383
384 /**
385  * Send shorten response back to client
386  * 
387  * @param name the shortened name result or NULL if cannot be shortened
388  * @param csh the handle to the shorten request
389  */
390 static void
391 send_shorten_response(void* cls, const char* name)
392 {
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
394               "SHORTEN_RESULT", name);
395   struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
396   struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
397   
398   if (name == NULL)
399   {
400     name = "";
401   }
402
403   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
404                        + strlen(name) + 1);
405   
406   rmsg->id = csh->unique_id;
407   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
408   rmsg->header.size = 
409     htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
410           strlen(name) + 1);
411
412   strcpy((char*)&rmsg[1], name);
413
414   GNUNET_SERVER_notification_context_unicast (nc, csh->client,
415                               (const struct GNUNET_MessageHeader *) rmsg,
416                               GNUNET_NO);
417   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
418   
419   GNUNET_free(rmsg);
420   GNUNET_free_non_null(csh->name);
421   GNUNET_free(csh);
422
423 }
424
425 /**
426  * Handle a shorten message from the api
427  *
428  * @param cls the closure
429  * @param client the client
430  * @param message the message
431  */
432 static void handle_shorten(void *cls,
433                            struct GNUNET_SERVER_Client * client,
434                            const struct GNUNET_MessageHeader * message)
435 {
436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
437
438   size_t msg_size = 0;
439   struct ClientShortenHandle *csh;
440   const char* name;
441
442   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
443   {
444     GNUNET_break_op (0);
445     GNUNET_SERVER_receive_done (client, GNUNET_OK);
446     return;
447   }
448
449   GNUNET_SERVER_notification_context_add (nc, client);
450
451   struct GNUNET_GNS_ClientShortenMessage *sh_msg =
452     (struct GNUNET_GNS_ClientShortenMessage *) message;
453   
454   msg_size = ntohs(message->size);
455
456   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
457   {
458     GNUNET_break_op (0);
459     GNUNET_SERVER_receive_done (client, GNUNET_OK);
460     return;
461   }
462
463   csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
464   csh->client = client;
465   csh->unique_id = sh_msg->id;
466   
467   name = (char*)&sh_msg[1];
468
469   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
470     csh->name = NULL;
471     send_shorten_response(csh, name);
472     return;
473   }
474   
475   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
476              GNUNET_GNS_TLD) != 0)
477   {
478     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479                 "%s is not our domain. Returning\n", name);
480     csh->name = NULL;
481     send_shorten_response(csh, name);
482     return;
483   }
484   
485   csh->name = GNUNET_malloc(strlen(name)
486                             - strlen(GNUNET_GNS_TLD) + 1);
487   memset(csh->name, 0,
488          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
489   memcpy(csh->name, name,
490          strlen(name)-strlen(GNUNET_GNS_TLD));
491
492   /* Start shortening */
493   gns_resolver_shorten_name(zone_hash, name, &send_shorten_response, csh);
494 }
495
496
497 /**
498  * Send get authority response back to client
499  * 
500  * @param name the shortened name result or NULL if cannot be shortened
501  * @param cah the handle to the get authority request
502  */
503 static void
504 send_get_auth_response(void *cls, const char* name)
505 {
506   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
507               "GET_AUTH_RESULT", name);
508   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
509   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
510   
511   if (name == NULL)
512   {
513     name = "";
514   }
515
516   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
517                        + strlen(name) + 1);
518   
519   rmsg->id = cah->unique_id;
520   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
521   rmsg->header.size = 
522     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
523           strlen(name) + 1);
524
525   strcpy((char*)&rmsg[1], name);
526
527   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
528                               (const struct GNUNET_MessageHeader *) rmsg,
529                               GNUNET_NO);
530   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
531   
532   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
533
534   GNUNET_free(rmsg);
535   GNUNET_free_non_null(cah->name);
536   GNUNET_free(cah);
537
538   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
539
540 }
541
542
543 /**
544  * Handle a get authority message from the api
545  *
546  * @param cls the closure
547  * @param client the client
548  * @param message the message
549  */
550 static void handle_get_authority(void *cls,
551                            struct GNUNET_SERVER_Client * client,
552                            const struct GNUNET_MessageHeader * message)
553 {
554   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
555
556   size_t msg_size = 0;
557   struct ClientGetAuthHandle *cah;
558   const char* name;
559
560   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
561   {
562     GNUNET_break_op (0);
563     GNUNET_SERVER_receive_done (client, GNUNET_OK);
564     return;
565   }
566
567   GNUNET_SERVER_notification_context_add (nc, client);
568
569   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
570     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
571   
572   msg_size = ntohs(message->size);
573
574   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
575   {
576     GNUNET_break_op (0);
577     GNUNET_SERVER_receive_done (client, GNUNET_OK);
578     return;
579   }
580   
581   name = (char*)&sh_msg[1];
582
583   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
584   cah->client = client;
585   cah->unique_id = sh_msg->id;
586
587   if (strlen(name) < strlen(GNUNET_GNS_TLD))
588   {
589     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590                 "%s is too short. Returning\n", name);
591     cah->name = NULL;
592     send_get_auth_response(cah, name);
593     return;
594   }
595
596   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
597              GNUNET_GNS_TLD) != 0)
598   {
599     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600                 "%s is not our domain. Returning\n", name);
601     cah->name = NULL;
602     send_get_auth_response(cah, name);
603     return;
604   }
605
606   if (strcmp(name, GNUNET_GNS_TLD) == 0)
607   {
608     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609                 "%s is us. Returning\n", name);
610     cah->name = NULL;
611     send_get_auth_response(cah, name);
612     return;
613   }
614   
615   cah->name = GNUNET_malloc(strlen(name)
616                             - strlen(GNUNET_GNS_TLD) + 1);
617   memset(cah->name, 0,
618          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
619   memcpy(cah->name, name,
620          strlen(name)-strlen(GNUNET_GNS_TLD));
621
622   /* Start delegation resolution in our namestore */
623   gns_resolver_get_authority(zone_hash, name, &send_get_auth_response, cah);
624 }
625
626
627 /**
628  * Reply to client with the result from our lookup.
629  *
630  * @param cls the closure (our client lookup handle)
631  * @param rh the request handle of the lookup
632  * @param rd_count the number of records
633  * @param rd the record data
634  */
635 static void
636 send_lookup_response(void* cls,
637                      uint32_t rd_count,
638                      const struct GNUNET_NAMESTORE_RecordData *rd)
639 {
640   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
641   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
642   size_t len;
643   
644   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
645               "LOOKUP_RESULT", rd_count);
646   
647   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
648   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
649   
650   rmsg->id = clh->unique_id;
651   rmsg->rd_count = htonl(rd_count);
652   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
653   rmsg->header.size = 
654     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
655
656   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
657   
658   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
659                                 (const struct GNUNET_MessageHeader *) rmsg,
660                                 GNUNET_NO);
661   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
662   
663   GNUNET_free(rmsg);
664   GNUNET_free(clh->name);
665   GNUNET_free(clh);
666
667 }
668
669
670 /**
671  * Handle lookup requests from client
672  *
673  * @param cls the closure
674  * @param client the client
675  * @param message the message
676  */
677 static void
678 handle_lookup(void *cls,
679               struct GNUNET_SERVER_Client * client,
680               const struct GNUNET_MessageHeader * message)
681 {
682   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
683
684   size_t msg_size = 0;
685   size_t namelen;
686   char* name;
687   struct ClientLookupHandle *clh;
688
689   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
690   {
691     GNUNET_break_op (0);
692     GNUNET_SERVER_receive_done (client, GNUNET_OK);
693     return;
694   }
695
696   GNUNET_SERVER_notification_context_add (nc, client);
697
698   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
699     (struct GNUNET_GNS_ClientLookupMessage *) message;
700   
701   msg_size = ntohs(message->size);
702
703   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
704   {
705     GNUNET_break_op (0);
706     GNUNET_SERVER_receive_done (client, GNUNET_OK);
707     return;
708   }
709   
710   name = (char*)&sh_msg[1];
711   namelen = strlen(name)+1;
712   clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
713   clh->client = client;
714   clh->name = GNUNET_malloc(namelen);
715   strcpy(clh->name, name);
716   clh->unique_id = sh_msg->id;
717   clh->type = ntohl(sh_msg->type);
718   
719   gns_resolver_lookup_record(zone_hash, clh->type, name,
720                              zone_key,
721                              &send_lookup_response, clh);
722 }
723
724
725
726 /**
727  * Process GNS requests.
728  *
729  * @param cls closure)
730  * @param server the initialized server
731  * @param c configuration to use
732  */
733 static void
734 run (void *cls, struct GNUNET_SERVER_Handle *server,
735      const struct GNUNET_CONFIGURATION_Handle *c)
736 {
737   
738   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
739   
740   char* keyfile;
741   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
742
743   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
744     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
745     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
746     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
747   };
748
749   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
750                                              "ZONEKEY", &keyfile))
751   {
752     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
753                 "No private key for root zone specified!\n");
754     GNUNET_SCHEDULER_shutdown(0);
755     return;
756   }
757
758   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
759              "Using keyfile %s for root zone.\n", keyfile);
760
761   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
762   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
763
764   GNUNET_CRYPTO_short_hash(&pkey,
765                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
766                      &zone_hash);
767   GNUNET_free(keyfile);
768   
769   /**
770    * handle to our local namestore
771    */
772   namestore_handle = GNUNET_NAMESTORE_connect(c);
773
774   if (NULL == namestore_handle)
775   {
776     //FIXME do error handling;
777     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
778                "Failed to connect to the namestore!\n");
779     GNUNET_SCHEDULER_shutdown(0);
780     return;
781   }
782   
783   /**
784    * handle to the dht
785    */
786   dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
787
788   if (NULL == dht_handle)
789   {
790     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
791   }
792
793   if (gns_resolver_init(namestore_handle, dht_handle) == GNUNET_SYSERR)
794   {
795     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
796                "Unable to initialize resolver!\n");
797     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
798     return;
799   }
800
801   if (GNUNET_YES ==
802       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
803                                             "HIJACK_DNS"))
804   {
805     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
806                "DNS hijacking enabled... connecting to service.\n");
807
808     if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
809     {
810       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
811                "Failed to enable the dns interceptor!\n");
812     }
813   }
814
815   //put_some_records(); //FIXME for testing
816   
817   /**
818    * Schedule periodic put
819    * for our records
820    * We have roughly an hour for all records;
821    */
822   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
823                                                       1);
824   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
825
826   GNUNET_SERVER_add_handlers (server, handlers);
827   
828   //FIXME
829   //GNUNET_SERVER_disconnect_notify (server,
830   //                                 &client_disconnect_notification,
831   //                                 NULL);
832
833   nc = GNUNET_SERVER_notification_context_create (server, 1);
834
835   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
836                                 NULL);
837 }
838
839
840 /**
841  * The main function for the GNS service.
842  *
843  * @param argc number of arguments from the command line
844  * @param argv command line arguments
845  * @return 0 ok, 1 on error
846  */
847 int
848 main (int argc, char *const *argv)
849 {
850   int ret;
851
852   ret =
853       (GNUNET_OK ==
854        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
855                            NULL)) ? 0 : 1;
856   return ret;
857 }
858
859 /* end of gnunet-service-gns.c */