-fixes
[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 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 GNUNET_HashCode 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   GNUNET_HashCode name_hash;
232   GNUNET_HashCode xor_hash;
233   struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
234   uint32_t rd_payload_length;
235   char* nrb_data = NULL;
236   size_t namelen;
237
238   /* we're done */
239   if (NULL == name)
240   {
241     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
242                "Zone iteration finished. Rescheduling put in %ds\n",
243                GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
244     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
245     zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
246                                         GNUNET_TIME_relative_multiply(
247                                             GNUNET_TIME_UNIT_SECONDS,
248                                             GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL
249                                             ),
250                                             &update_zone_dht_start,
251                                             NULL);
252     return;
253   }
254   
255   namelen = strlen(name) + 1;
256   
257   if (signature == NULL)
258   {
259     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
260                "No signature for %s record data provided! Skipping...\n",
261                name);
262     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
263                                                    NULL);
264     return;
265
266   }
267   
268   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
269              "Putting records for %s into the DHT\n", name);
270   
271   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
272   
273   nrb = GNUNET_malloc(rd_payload_length + namelen
274                       + sizeof(struct GNSNameRecordBlock));
275   
276   nrb->signature = *signature;
277   
278   nrb->public_key = *key;
279
280   nrb->rd_count = htonl(rd_count);
281   
282   memcpy(&nrb[1], name, namelen);
283
284   nrb_data = (char*)&nrb[1];
285   nrb_data += namelen;
286
287   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
288
289   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
290                                                 rd,
291                                                 rd_payload_length,
292                                                 nrb_data))
293   {
294     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
295                "Record serialization failed! Skipping...\n");
296     GNUNET_free(nrb);
297     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
298                                                    NULL);
299     return;
300   }
301
302
303   /*
304    * calculate DHT key: H(name) xor H(pubkey)
305    */
306   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
307   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
308   GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
309
310   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
311              "putting records for %s under key: %s with size %d\n",
312              name, (char*)&xor_hash_string, rd_payload_length);
313
314   GNUNET_DHT_put (dht_handle, &xor_hash,
315                   DHT_GNS_REPLICATION_LEVEL,
316                   GNUNET_DHT_RO_NONE,
317                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
318                   rd_payload_length,
319                   (char*)nrb,
320                   expiration,
321                   DHT_OPERATION_TIMEOUT,
322                   &record_dht_put,
323                   NULL); //cls for cont
324   
325   num_public_records++;
326
327   /**
328    * Reschedule periodic put
329    */
330   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
331                                 &update_zone_dht_next,
332                                 NULL);
333
334   GNUNET_free(nrb);
335
336 }
337
338 /**
339  * Periodically iterate over our zone and store everything in dht
340  *
341  * @param cls NULL
342  * @param tc task context
343  */
344 static void
345 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
348   if (0 == num_public_records)
349   {
350     dht_update_interval = GNUNET_TIME_relative_multiply(
351                                             GNUNET_TIME_UNIT_SECONDS,
352                                             GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
353     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
354                "No records in db. Adjusted DHT update interval to %ds\n",
355                GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
356   }
357   else
358   {
359     
360     dht_update_interval = GNUNET_TIME_relative_multiply(
361                                                       GNUNET_TIME_UNIT_SECONDS,
362                                                      (3600/num_public_records));
363     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
364                "Adjusted DHT update interval to %ds!\n",
365                (3600/num_public_records));
366   }
367
368   /* start counting again */
369   num_public_records = 0;
370   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
371                                                  &zone_hash,
372                                                  GNUNET_NAMESTORE_RF_AUTHORITY,
373                                                  GNUNET_NAMESTORE_RF_PRIVATE,
374                                                  &put_gns_record,
375                                                  NULL);
376 }
377
378
379 /* END DHT ZONE PROPAGATION */
380
381 /**
382  * Send shorten response back to client
383  * 
384  * @param name the shortened name result or NULL if cannot be shortened
385  * @param csh the handle to the shorten request
386  */
387 static void
388 send_shorten_response(void* cls, const char* name)
389 {
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
391               "SHORTEN_RESULT", name);
392   struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
393   struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
394   
395   if (name == NULL)
396   {
397     name = "";
398   }
399
400   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
401                        + strlen(name) + 1);
402   
403   rmsg->id = csh->unique_id;
404   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
405   rmsg->header.size = 
406     htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
407           strlen(name) + 1);
408
409   strcpy((char*)&rmsg[1], name);
410
411   GNUNET_SERVER_notification_context_unicast (nc, csh->client,
412                               (const struct GNUNET_MessageHeader *) rmsg,
413                               GNUNET_NO);
414   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
415   
416   GNUNET_free(rmsg);
417   GNUNET_free_non_null(csh->name);
418   GNUNET_free(csh);
419
420 }
421
422 /**
423  * Handle a shorten message from the api
424  *
425  * @param cls the closure
426  * @param client the client
427  * @param message the message
428  */
429 static void handle_shorten(void *cls,
430                            struct GNUNET_SERVER_Client * client,
431                            const struct GNUNET_MessageHeader * message)
432 {
433   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
434
435   size_t msg_size = 0;
436   struct ClientShortenHandle *csh;
437   const char* name;
438
439   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
440   {
441     GNUNET_break_op (0);
442     GNUNET_SERVER_receive_done (client, GNUNET_OK);
443     return;
444   }
445
446   GNUNET_SERVER_notification_context_add (nc, client);
447
448   struct GNUNET_GNS_ClientShortenMessage *sh_msg =
449     (struct GNUNET_GNS_ClientShortenMessage *) message;
450   
451   msg_size = ntohs(message->size);
452
453   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
454   {
455     GNUNET_break_op (0);
456     GNUNET_SERVER_receive_done (client, GNUNET_OK);
457     return;
458   }
459
460   csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
461   csh->client = client;
462   csh->unique_id = sh_msg->id;
463   
464   name = (char*)&sh_msg[1];
465
466   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
467     csh->name = NULL;
468     send_shorten_response(csh, name);
469     return;
470   }
471   
472   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
473              GNUNET_GNS_TLD) != 0)
474   {
475     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476                 "%s is not our domain. Returning\n", name);
477     csh->name = NULL;
478     send_shorten_response(csh, name);
479     return;
480   }
481   
482   csh->name = GNUNET_malloc(strlen(name)
483                             - strlen(GNUNET_GNS_TLD) + 1);
484   memset(csh->name, 0,
485          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
486   memcpy(csh->name, name,
487          strlen(name)-strlen(GNUNET_GNS_TLD));
488
489   /* Start shortening */
490   gns_resolver_shorten_name(zone_hash, name, &send_shorten_response, csh);
491 }
492
493
494 /**
495  * Send get authority response back to client
496  * 
497  * @param name the shortened name result or NULL if cannot be shortened
498  * @param cah the handle to the get authority request
499  */
500 static void
501 send_get_auth_response(void *cls, const char* name)
502 {
503   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
504               "GET_AUTH_RESULT", name);
505   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
506   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
507   
508   if (name == NULL)
509   {
510     name = "";
511   }
512
513   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
514                        + strlen(name) + 1);
515   
516   rmsg->id = cah->unique_id;
517   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
518   rmsg->header.size = 
519     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
520           strlen(name) + 1);
521
522   strcpy((char*)&rmsg[1], name);
523
524   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
525                               (const struct GNUNET_MessageHeader *) rmsg,
526                               GNUNET_NO);
527   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
528   
529   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
530
531   GNUNET_free(rmsg);
532   GNUNET_free_non_null(cah->name);
533   GNUNET_free(cah);
534
535   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
536
537 }
538
539
540 /**
541  * Handle a get authority message from the api
542  *
543  * @param cls the closure
544  * @param client the client
545  * @param message the message
546  */
547 static void handle_get_authority(void *cls,
548                            struct GNUNET_SERVER_Client * client,
549                            const struct GNUNET_MessageHeader * message)
550 {
551   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
552
553   size_t msg_size = 0;
554   struct ClientGetAuthHandle *cah;
555   const char* name;
556
557   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
558   {
559     GNUNET_break_op (0);
560     GNUNET_SERVER_receive_done (client, GNUNET_OK);
561     return;
562   }
563
564   GNUNET_SERVER_notification_context_add (nc, client);
565
566   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
567     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
568   
569   msg_size = ntohs(message->size);
570
571   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
572   {
573     GNUNET_break_op (0);
574     GNUNET_SERVER_receive_done (client, GNUNET_OK);
575     return;
576   }
577   
578   name = (char*)&sh_msg[1];
579
580   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
581   cah->client = client;
582   cah->unique_id = sh_msg->id;
583
584   if (strlen(name) < strlen(GNUNET_GNS_TLD))
585   {
586     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587                 "%s is too short. Returning\n", name);
588     cah->name = NULL;
589     send_get_auth_response(cah, name);
590     return;
591   }
592
593   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
594              GNUNET_GNS_TLD) != 0)
595   {
596     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597                 "%s is not our domain. Returning\n", name);
598     cah->name = NULL;
599     send_get_auth_response(cah, name);
600     return;
601   }
602   
603   cah->name = GNUNET_malloc(strlen(name)
604                             - strlen(GNUNET_GNS_TLD) + 1);
605   memset(cah->name, 0,
606          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
607   memcpy(cah->name, name,
608          strlen(name)-strlen(GNUNET_GNS_TLD));
609
610   /* Start delegation resolution in our namestore */
611   gns_resolver_get_authority(zone_hash, name, &send_get_auth_response, cah);
612 }
613
614
615 /**
616  * Reply to client with the result from our lookup.
617  *
618  * @param cls the closure (our client lookup handle)
619  * @param rh the request handle of the lookup
620  * @param rd_count the number of records
621  * @param rd the record data
622  */
623 static void
624 send_lookup_response(void* cls,
625                      uint32_t rd_count,
626                      const struct GNUNET_NAMESTORE_RecordData *rd)
627 {
628   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
629   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
630   size_t len;
631   
632   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
633               "LOOKUP_RESULT", rd_count);
634   
635   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
636   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
637   
638   rmsg->id = clh->unique_id;
639   rmsg->rd_count = htonl(rd_count);
640   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
641   rmsg->header.size = 
642     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
643
644   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
645   
646   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
647                                 (const struct GNUNET_MessageHeader *) rmsg,
648                                 GNUNET_NO);
649   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
650   
651   GNUNET_free(rmsg);
652   GNUNET_free(clh->name);
653   GNUNET_free(clh);
654
655 }
656
657
658 /**
659  * Handle lookup requests from client
660  *
661  * @param cls the closure
662  * @param client the client
663  * @param message the message
664  */
665 static void
666 handle_lookup(void *cls,
667               struct GNUNET_SERVER_Client * client,
668               const struct GNUNET_MessageHeader * message)
669 {
670   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
671
672   size_t msg_size = 0;
673   size_t namelen;
674   char* name;
675   struct ClientLookupHandle *clh;
676
677   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
678   {
679     GNUNET_break_op (0);
680     GNUNET_SERVER_receive_done (client, GNUNET_OK);
681     return;
682   }
683
684   GNUNET_SERVER_notification_context_add (nc, client);
685
686   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
687     (struct GNUNET_GNS_ClientLookupMessage *) message;
688   
689   msg_size = ntohs(message->size);
690
691   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
692   {
693     GNUNET_break_op (0);
694     GNUNET_SERVER_receive_done (client, GNUNET_OK);
695     return;
696   }
697   
698   name = (char*)&sh_msg[1];
699   namelen = strlen(name)+1;
700   clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
701   clh->client = client;
702   clh->name = GNUNET_malloc(namelen);
703   strcpy(clh->name, name);
704   clh->unique_id = sh_msg->id;
705   clh->type = ntohl(sh_msg->type);
706   
707   gns_resolver_lookup_record(zone_hash, clh->type, name,
708                              &send_lookup_response, clh);
709 }
710
711
712
713 /**
714  * Process GNS requests.
715  *
716  * @param cls closure)
717  * @param server the initialized server
718  * @param c configuration to use
719  */
720 static void
721 run (void *cls, struct GNUNET_SERVER_Handle *server,
722      const struct GNUNET_CONFIGURATION_Handle *c)
723 {
724   
725   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
726   
727   char* keyfile;
728   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
729
730   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
731     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
732     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
733     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
734   };
735
736   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
737                                              "ZONEKEY", &keyfile))
738   {
739     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
740                 "No private key for root zone specified!\n");
741     GNUNET_SCHEDULER_shutdown(0);
742     return;
743   }
744
745   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
746              "Using keyfile %s for root zone.\n", keyfile);
747
748   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
749   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
750
751   GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
752                      &zone_hash);
753   GNUNET_free(keyfile);
754   
755
756   if (GNUNET_YES ==
757       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
758                                             "HIJACK_DNS"))
759   {
760     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
761                "DNS hijacking enabled... connecting to service.\n");
762
763     if (gns_interceptor_init(zone_hash, c) == GNUNET_SYSERR)
764     {
765       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
766                "Failed to enable the dns interceptor!\n");
767     }
768   }
769
770   
771
772   /**
773    * handle to our local namestore
774    */
775   namestore_handle = GNUNET_NAMESTORE_connect(c);
776
777   if (NULL == namestore_handle)
778   {
779     //FIXME do error handling;
780     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
781                "Failed to connect to the namestore!\n");
782     GNUNET_SCHEDULER_shutdown(0);
783     return;
784   }
785   
786   /**
787    * handle to the dht
788    */
789   dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
790
791   if (NULL == dht_handle)
792   {
793     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
794   }
795
796   //put_some_records(); //FIXME for testing
797   
798   /**
799    * Schedule periodic put
800    * for our records
801    * We have roughly an hour for all records;
802    */
803   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
804                                                       1);
805   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
806
807   GNUNET_SERVER_add_handlers (server, handlers);
808   
809   //FIXME
810   //GNUNET_SERVER_disconnect_notify (server,
811   //                                 &client_disconnect_notification,
812   //                                 NULL);
813
814   nc = GNUNET_SERVER_notification_context_create (server, 1);
815
816   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
817                                 NULL);
818 }
819
820
821 /**
822  * The main function for the GNS service.
823  *
824  * @param argc number of arguments from the command line
825  * @param argv command line arguments
826  * @return 0 ok, 1 on error
827  */
828 int
829 main (int argc, char *const *argv)
830 {
831   int ret;
832
833   ret =
834       (GNUNET_OK ==
835        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
836                            NULL)) ? 0 : 1;
837   return ret;
838 }
839
840 /* end of gnunet-service-gns.c */