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