-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  * @file gns/gnunet-service-gns.c
24  * @brief GNUnet GNS service
25  * @author Martin Schanzenbach
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "block_gns.h"
37 #include "gns.h"
38 #include "gnunet-service-gns_resolver.h"
39 #include "gnunet-service-gns_interceptor.h"
40
41 /* FIXME move to proper header in include */
42 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
43 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
44 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
45 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
46 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
47 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
48
49
50 /**
51  * Handle to a shorten operation from api
52  */
53 struct ClientShortenHandle
54 {
55
56   /* DLL */
57   struct ClientShortenHandle *next;
58
59   /* DLL */
60   struct ClientShortenHandle *prev;
61
62   /* the requesting client that */
63   struct GNUNET_SERVER_Client *client;
64
65   /* request id */
66   uint64_t unique_id;
67
68   /* request type */
69   enum GNUNET_GNS_RecordType type;
70
71   /* name to shorten */
72   char name[MAX_DNS_NAME_LENGTH];
73
74   /* name of private zone (relative to root) */
75   char private_zone_id[MAX_DNS_NAME_LENGTH];
76   
77   /* name of shorten zone (relative to root) */
78   char shorten_zone_id[MAX_DNS_NAME_LENGTH];
79   
80   /* root zone */
81   struct GNUNET_CRYPTO_ShortHashCode root_zone;
82
83   /* private zone */
84   struct GNUNET_CRYPTO_ShortHashCode private_zone;
85   
86   /* shorten zone */
87   struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
88
89   /* Namestore lookup task */
90   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
91
92 };
93
94
95 /**
96  * Handle to a get auhtority operation from api
97  */
98 struct ClientGetAuthHandle
99 {
100   /* the requesting client that */
101   struct GNUNET_SERVER_Client *client;
102
103   /* request id */
104   uint64_t unique_id;
105
106   /* name to lookup authority */
107   char* name;
108
109 };
110
111
112 /**
113  * Handle to a lookup operation from api
114  */
115 struct ClientLookupHandle
116 {
117
118   /* the requesting client that */
119   struct GNUNET_SERVER_Client *client;
120
121   /* The zone we look up in */
122   struct GNUNET_CRYPTO_ShortHashCode zone;
123
124   /* Do we only want to lookup from local cache? */
125   int only_cached;
126
127   /* request id */
128   uint64_t unique_id;
129
130   /* request type */
131   enum GNUNET_GNS_RecordType type;
132
133   /* optional zone private key used for shorten */
134   struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
135
136   /* the name to look up */
137   char* name; //Needed?
138 };
139
140 /**
141  * Our handle to the DHT
142  */
143 static struct GNUNET_DHT_Handle *dht_handle;
144
145 /**
146  * Our zone's private key
147  */
148 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
149
150 /**
151  * Our handle to the namestore service
152  * FIXME maybe need a second handle for iteration
153  */
154 struct GNUNET_NAMESTORE_Handle *namestore_handle;
155
156 /**
157  * Handle to iterate over our authoritative zone in namestore
158  */
159 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
160
161 /**
162  * The configuration the GNS service is running with
163  */
164 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
165
166 /**
167  * Our notification context.
168  */
169 static struct GNUNET_SERVER_NotificationContext *nc;
170
171 /**
172  * Our zone hash
173  */
174 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
175
176 /**
177  * Useful for zone update for DHT put
178  */
179 static int num_public_records;
180
181 /**
182  * update interval in seconds
183  */
184 static unsigned long long max_record_put_interval;
185
186 static struct GNUNET_TIME_Relative dht_max_update_interval;
187
188 /* dht update interval FIXME define? */
189 static struct GNUNET_TIME_Relative record_put_interval;
190
191 /* zone update task */
192 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
193
194 /* automatic pkey import for name shortening */
195 static int auto_import_pkey;
196
197 /* lookup timeout */
198 static struct GNUNET_TIME_Relative default_lookup_timeout;
199
200 /* ipv6 support */
201 static int v6_enabled;
202
203 /* ipv4 support */
204 static int v4_enabled;
205
206 /* Shorten DLL for cancelling NS requests */
207 static struct ClientShortenHandle *csh_head;
208
209 /* Shorten DLL for cancelling NS requests */
210 static struct ClientShortenHandle *csh_tail;
211
212 /* Statistics handle */
213 static struct GNUNET_STATISTICS_Handle *statistics;
214
215 /**
216  * Send shorten response back to client
217  * 
218  * @param cls the closure containing a client shorten handle
219  * @param name the shortened name result or NULL if cannot be shortened
220  */
221 static void
222 send_shorten_response(void* cls, const char* name);
223
224
225 /**
226  * Continue shutdown
227  */
228 static void
229 on_resolver_cleanup(void)
230 {
231   if (NULL != statistics)
232     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
233
234   if (NULL != namestore_iter)
235     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
236   GNUNET_NAMESTORE_disconnect(namestore_handle);
237   GNUNET_DHT_disconnect(dht_handle);
238 }
239
240 /**
241  * Task run during shutdown.
242  *
243  * @param cls unused
244  * @param tc unused
245  */
246 static void
247 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 {
249   struct ClientShortenHandle *csh_tmp = csh_head;
250
251   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
252              "Shutting down!");
253   
254   while (csh_tmp != NULL)
255   {
256     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
257     send_shorten_response (csh_tmp, csh_tmp->name);
258   }
259   
260   GNUNET_SERVER_notification_context_destroy (nc);
261   
262   gns_interceptor_stop();
263   gns_resolver_cleanup(&on_resolver_cleanup);
264   /* Kill zone task for it may make the scheduler hang */
265   if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
266     GNUNET_SCHEDULER_cancel(zone_update_taskid);
267 }
268
269
270 /**
271  * Method called periodicattluy that triggers
272  * iteration over root zone
273  *
274  * @param cls closure
275  * @param tc task context
276  */
277 static void
278 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
279 {
280   zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
281   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
282 }
283
284 /**
285  * Continuation for DHT put
286  *
287  * @param cls closure
288  * @param success GNUNET_OK if the PUT was transmitted,
289  *                GNUNET_NO on timeout,
290  *                GNUNET_SYSERR on disconnect from service
291  *                after the PUT message was transmitted
292  *                (so we don't know if it was received or not)
293  */
294 static void
295 record_dht_put(void *cls, int success)
296 {
297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
298 }
299
300 /* prototype */
301 static void
302 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
303
304 /**
305  * Function used to put all records successively into the DHT.
306  *
307  * @param cls the closure (NULL)
308  * @param key the public key of the authority (ours)
309  * @param expiration lifetime of the namestore entry
310  * @param name the name of the records
311  * @param rd_count the number of records in data
312  * @param rd the record data
313  * @param signature the signature for the record data
314  */
315 static void
316 put_gns_record(void *cls,
317                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
318                 struct GNUNET_TIME_Absolute expiration,
319                 const char *name,
320                 unsigned int rd_count,
321                 const struct GNUNET_NAMESTORE_RecordData *rd,
322                 const struct GNUNET_CRYPTO_RsaSignature *signature)
323 {
324   
325   struct GNSNameRecordBlock *nrb;
326   struct GNUNET_CRYPTO_ShortHashCode name_hash;
327   struct GNUNET_CRYPTO_ShortHashCode zhash;
328   struct GNUNET_HashCode xor_hash;
329   struct GNUNET_HashCode name_hash_double;
330   struct GNUNET_HashCode zone_hash_double;
331   uint32_t rd_payload_length;
332   char* nrb_data = NULL;
333   size_t namelen;
334
335   /* we're done */
336   if (NULL == name)
337   {
338     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
339                "Zone iteration finished. Rescheduling put in %llus\n",
340                (unsigned long long) dht_max_update_interval.rel_value / 1000LL);
341     namestore_iter = NULL;
342     zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_max_update_interval,
343                                             &update_zone_dht_start,
344                                             NULL);
345     GNUNET_STATISTICS_update (statistics,
346                               "Number of zone iterations", 1, GNUNET_NO);
347     return;
348   }
349   
350   namelen = strlen(name) + 1;
351   
352   if (signature == NULL)
353   {
354     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
355                "No signature for %s record data provided! Skipping...\n",
356                name);
357     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
358                                                    NULL);
359     return;
360
361   }
362   
363   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
364              "Putting records for %s into the DHT\n", name);
365   
366   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
367   
368   nrb = GNUNET_malloc(rd_payload_length + namelen
369                       + sizeof(struct GNSNameRecordBlock));
370   
371   nrb->signature = *signature;
372   
373   nrb->public_key = *key;
374
375   nrb->rd_count = htonl(rd_count);
376   
377   memcpy(&nrb[1], name, namelen);
378
379   nrb_data = (char*)&nrb[1];
380   nrb_data += namelen;
381
382   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
383
384   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
385                                                 rd,
386                                                 rd_payload_length,
387                                                 nrb_data))
388   {
389     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
390                "Record serialization failed! Skipping...\n");
391     GNUNET_free(nrb);
392     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
393                                                    NULL);
394     return;
395   }
396
397
398   /*
399    * calculate DHT key: H(name) xor H(pubkey)
400    */
401   GNUNET_CRYPTO_short_hash(key,
402                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
403                      &zhash);
404   GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
405   GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
406   GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
407   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
408
409   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
410              "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
411
412   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
413              "putting records for %s under key: %s with size %d\n",
414              name, GNUNET_h2s (&xor_hash), rd_payload_length);
415   
416   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
417              "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
418
419   GNUNET_STATISTICS_update (statistics,
420                             "Record data set put into DHT", 1, GNUNET_NO);
421
422   /* FIXME: keep return value to possibly cancel? */
423   GNUNET_DHT_put (dht_handle, &xor_hash,
424                   DHT_GNS_REPLICATION_LEVEL,
425                   GNUNET_DHT_RO_NONE,
426                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
427                   rd_payload_length,
428                   (char*)nrb,
429                   expiration,
430                   DHT_OPERATION_TIMEOUT,
431                   &record_dht_put,
432                   NULL); //cls for cont
433   
434   num_public_records++;
435
436   /**
437    * Reschedule periodic put
438    */
439   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
440                                 &update_zone_dht_next,
441                                 NULL);
442
443   GNUNET_free(nrb);
444
445 }
446
447 /**
448  * Periodically iterate over our zone and store everything in dht
449  *
450  * @param cls NULL
451  * @param tc task context
452  */
453 static void
454 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
455 {
456   unsigned long long interval = 0;
457
458   zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
459
460   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
461   if (0 == num_public_records)
462   {
463     /**
464      * If no records are known (startup) or none present
465      * we can safely set the interval to 1s
466      */
467     record_put_interval = GNUNET_TIME_relative_multiply(
468                                             GNUNET_TIME_UNIT_SECONDS,
469                                             1);
470     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
471                "No records in db. Adjusted record put interval to 1s\n");
472     GNUNET_STATISTICS_set (statistics,
473                            "Current PUT interval (sec)", 1,
474                            GNUNET_NO);
475   }
476   else
477   {
478     interval = max_record_put_interval/num_public_records;
479     if (interval == 0)
480       interval = 1;
481     record_put_interval = GNUNET_TIME_relative_multiply(
482                                   GNUNET_TIME_UNIT_SECONDS,
483                                   interval);
484     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
485                "Adjusted DHT update interval to %ds!\n",
486                interval);
487     GNUNET_STATISTICS_set (statistics,
488                            "Current PUT interval (sec)", interval,
489                            GNUNET_NO);
490   }
491
492   /* start counting again */
493   num_public_records = 0;
494   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
495                                                  NULL, //All zones
496                                                  GNUNET_NAMESTORE_RF_AUTHORITY,
497                                                  GNUNET_NAMESTORE_RF_PRIVATE,
498                                                  &put_gns_record,
499                                                  NULL);
500 }
501
502 /* END DHT ZONE PROPAGATION */
503
504 /**
505  * Send shorten response back to client
506  * 
507  * @param cls the closure containing a client shorten handle
508  * @param name the shortened name result or NULL if cannot be shortened
509  */
510 static void
511 send_shorten_response(void* cls, const char* name)
512 {
513   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
514               "SHORTEN_RESULT", name);
515   struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
516   struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
517   
518   if (name == NULL)
519   {
520     name = "";
521   }
522
523   GNUNET_STATISTICS_update (statistics,
524                             "Name shorten results", 1, GNUNET_NO);
525
526   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
527                        + strlen(name) + 1);
528   
529   rmsg->id = csh->unique_id;
530   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
531   rmsg->header.size = 
532     htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
533           strlen(name) + 1);
534
535   strcpy((char*)&rmsg[1], name);
536
537   GNUNET_SERVER_notification_context_unicast (nc, csh->client,
538                               (const struct GNUNET_MessageHeader *) rmsg,
539                               GNUNET_NO);
540   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
541
542   if (NULL != csh->namestore_task)
543     GNUNET_NAMESTORE_cancel (csh->namestore_task);
544   
545   GNUNET_free(rmsg);
546   GNUNET_free(csh);
547
548 }
549
550
551 /**
552  * Lookup the zone infos and shorten name
553  *
554  * @param cls the client shorten handle
555  * @param key key of the zone
556  * @param expiration expiration of record
557  * @param name name found or null if no result
558  * @param rd_count number of records found
559  * @param rd record data
560  * @param signature
561  *
562  */
563 static void
564 process_shorten_in_private_zone_lookup (void *cls,
565                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
566                       struct GNUNET_TIME_Absolute expiration,
567                       const char *name,
568                       unsigned int rd_count,
569                       const struct GNUNET_NAMESTORE_RecordData *rd,
570                       const struct GNUNET_CRYPTO_RsaSignature *signature)
571 {
572   struct ClientShortenHandle *csh = cls;
573   csh->namestore_task = NULL;
574   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
575   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
576
577   if (0 == strcmp (csh->private_zone_id, ""))
578     pzone = NULL;
579   
580   if (rd_count == 0)
581   {
582     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583                 "No shorten zone in private zone!\n");
584
585     strcpy (csh->shorten_zone_id, "");
586     szone = NULL;
587
588   }
589   else
590   {
591
592     GNUNET_assert (rd_count == 1);
593
594     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595                 "Shorten zone %s found in private zone %s\n",
596                 name, csh->private_zone_id);
597
598     sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
599   }
600   
601   GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
602
603   gns_resolver_shorten_name (&csh->root_zone,
604                              pzone,
605                              szone,
606                              csh->name,
607                              csh->private_zone_id,
608                              csh->shorten_zone_id,
609                              &send_shorten_response, csh);
610
611 }
612
613
614 /**
615  * Lookup the zone infos and shorten name
616  *
617  * @param cls the shorten handle
618  * @param key key of the zone
619  * @param expiration expiration of record
620  * @param name name found or null if no result
621  * @param rd_count number of records found
622  * @param rd record data
623  * @param signature
624  *
625  */
626 static void
627 process_shorten_in_root_zone_lookup (void *cls,
628                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
629                       struct GNUNET_TIME_Absolute expiration,
630                       const char *name,
631                       unsigned int rd_count,
632                       const struct GNUNET_NAMESTORE_RecordData *rd,
633                       const struct GNUNET_CRYPTO_RsaSignature *signature)
634 {
635   struct ClientShortenHandle *csh = cls;
636   csh->namestore_task = NULL;
637   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
638   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
639   
640   if (0 == strcmp (csh->private_zone_id, ""))
641     pzone = NULL;
642
643   if (rd_count == 0)
644   {
645     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646                 "No shorten zone in zone and no private zone!\n");
647
648     strcpy (csh->shorten_zone_id, "");
649
650     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
651     szone = NULL;
652
653     gns_resolver_shorten_name (&csh->root_zone,
654                                pzone,
655                                szone,
656                                csh->name,
657                                csh->private_zone_id,
658                                csh->shorten_zone_id,
659                                &send_shorten_response, csh);
660     return;
661   }
662
663   GNUNET_assert (rd_count == 1);
664
665   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666               "Private zone %s found in root zone\n", name);
667
668   strcpy (csh->private_zone_id, name);
669
670   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
671                                   pzone,
672                                   szone,
673                                   &process_shorten_in_private_zone_lookup,
674                                   csh);
675 }
676
677
678 /**
679  * Lookup the zone infos and shorten name
680  *
681  * @param cls the shorten handle
682  * @param key key of the zone
683  * @param expiration expiration of record
684  * @param name name found or null if no result
685  * @param rd_count number of records found
686  * @param rd record data
687  * @param signature
688  *
689  */
690 static void
691 process_private_in_root_zone_lookup (void *cls,
692                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
693                       struct GNUNET_TIME_Absolute expiration,
694                       const char *name,
695                       unsigned int rd_count,
696                       const struct GNUNET_NAMESTORE_RecordData *rd,
697                       const struct GNUNET_CRYPTO_RsaSignature *signature)
698 {
699   struct ClientShortenHandle *csh = cls;
700   csh->namestore_task = NULL;
701
702   if (rd_count == 0)
703   {
704     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705                 "No private zone in root zone\n");
706
707     strcpy (csh->private_zone_id, "");
708   
709     csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
710                                   &csh->root_zone,
711                                   &csh->shorten_zone,
712                                   &process_shorten_in_root_zone_lookup,
713                                   csh);
714     return;
715   }
716
717   GNUNET_assert (rd_count == 1);
718
719   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720               "Private zone %s found in root zone\n", name);
721
722   strcpy (csh->private_zone_id, name);
723
724   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
725                                   &csh->private_zone,
726                                   &csh->shorten_zone,
727                                   &process_shorten_in_private_zone_lookup,
728                                   csh);
729 }
730
731 /**
732  * Lookup the zone infos and shorten name
733  *
734  * @param csh the shorten handle
735  *
736  */
737 static void
738 start_shorten_name (struct ClientShortenHandle *csh)
739 {
740   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
741               "Looking for private zone name in root zone\n");
742
743   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
744                                   &csh->root_zone,
745                                   &csh->private_zone,
746                                   &process_private_in_root_zone_lookup,
747                                   csh);
748 }
749
750
751 /**
752  * Handle a shorten message from the api
753  *
754  * @param cls the closure
755  * @param client the client
756  * @param message the message
757  */
758 static void handle_shorten (void *cls,
759                             struct GNUNET_SERVER_Client * client,
760                             const struct GNUNET_MessageHeader * message)
761 {
762   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
763
764   size_t msg_size = 0;
765   struct ClientShortenHandle *csh;
766   char name[MAX_DNS_NAME_LENGTH];
767   char* nameptr = name;
768
769   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
770   {
771     GNUNET_break_op (0);
772     GNUNET_SERVER_receive_done (client, GNUNET_OK);
773     return;
774   }
775
776
777   struct GNUNET_GNS_ClientShortenMessage *sh_msg =
778     (struct GNUNET_GNS_ClientShortenMessage *) message;
779   
780   msg_size = ntohs(message->size);
781
782   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
783   {
784     GNUNET_break_op (0);
785     GNUNET_SERVER_receive_done (client, GNUNET_OK);
786     return;
787   }
788
789   csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
790   csh->client = client;
791   csh->unique_id = sh_msg->id;
792
793   GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
794   
795   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
796
797   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
798     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
799                "SHORTEN: %s is too short", name);
800     send_shorten_response(csh, name);
801     return;
802   }
803
804   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
805     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
806                "SHORTEN: %s is too long", name);
807     send_shorten_response(csh, name);
808     return;
809   }
810   
811   if (!is_gnunet_tld(name) && !is_zkey_tld(name))
812   {
813     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814                 "%s is not our domain. Returning\n", name);
815     send_shorten_response(csh, name);
816     return;
817   }
818
819   csh->shorten_zone = sh_msg->shorten_zone;
820   csh->private_zone = sh_msg->private_zone;
821
822   strcpy (csh->name, name);
823   
824   GNUNET_SERVER_notification_context_add (nc, client);
825   
826   if (1 == ntohl(sh_msg->use_default_zone))
827     csh->root_zone = zone_hash; //Default zone
828   else
829     csh->root_zone = sh_msg->zone;
830
831   start_shorten_name (csh);
832
833   GNUNET_STATISTICS_update (statistics,
834                             "Name shorten attempts", 1, GNUNET_NO);
835   
836 }
837
838
839 /**
840  * Send get authority response back to client
841  * 
842  * @param cls the closure containing a client get auth handle
843  * @param name the shortened name result or NULL if cannot be shortened
844  */
845 static void
846 send_get_auth_response(void *cls, const char* name)
847 {
848   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
849               "GET_AUTH_RESULT", name);
850   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
851   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
852   
853   if (name != NULL)
854   {
855     GNUNET_STATISTICS_update (statistics,
856                               "Authorities resolved", 1, GNUNET_NO);
857   }
858   
859   if (name == NULL)
860   {
861     name = "";
862   }
863
864   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
865                        + strlen(name) + 1);
866   
867   rmsg->id = cah->unique_id;
868   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
869   rmsg->header.size = 
870     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
871           strlen(name) + 1);
872
873   strcpy((char*)&rmsg[1], name);
874
875   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
876                               (const struct GNUNET_MessageHeader *) rmsg,
877                               GNUNET_NO);
878   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
879   
880   GNUNET_free(rmsg);
881   GNUNET_free_non_null(cah->name);
882   GNUNET_free(cah);
883
884   
885   
886 }
887
888
889 /**
890  * Handle a get authority message from the api
891  *
892  * @param cls the closure
893  * @param client the client
894  * @param message the message
895  */
896 static void handle_get_authority(void *cls,
897                            struct GNUNET_SERVER_Client * client,
898                            const struct GNUNET_MessageHeader * message)
899 {
900   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
901
902   size_t msg_size = 0;
903   struct ClientGetAuthHandle *cah;
904   char name[MAX_DNS_NAME_LENGTH];
905   char* nameptr = name;
906
907
908   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
909   {
910     GNUNET_break_op (0);
911     GNUNET_SERVER_receive_done (client, GNUNET_OK);
912     return;
913   }
914
915   GNUNET_SERVER_notification_context_add (nc, client);
916
917   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
918     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
919   
920   msg_size = ntohs(message->size);
921
922   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
923   {
924     GNUNET_break_op (0);
925     GNUNET_SERVER_receive_done (client, GNUNET_OK);
926     return;
927   }
928   
929   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
930
931
932   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
933   cah->client = client;
934   cah->unique_id = sh_msg->id;
935
936   if (strlen(name) < strlen(GNUNET_GNS_TLD))
937   {
938     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939                 "GET_AUTH: %s is too short. Returning\n", name);
940     cah->name = NULL;
941     send_get_auth_response(cah, name);
942     return;
943   }
944   
945   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
946     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
947                "GET_AUTH: %s is too long", name);
948     cah->name = NULL;
949     send_get_auth_response(cah, name);
950     return;
951   }
952   
953   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
954              GNUNET_GNS_TLD) != 0)
955   {
956     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957                 "GET_AUTH: %s is not our domain. Returning\n", name);
958     cah->name = NULL;
959     send_get_auth_response(cah, name);
960     return;
961   }
962
963   if (strcmp(name, GNUNET_GNS_TLD) == 0)
964   {
965     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966                 "GET_AUTH: %s is us. Returning\n", name);
967     cah->name = NULL;
968     send_get_auth_response(cah, name);
969     return;
970   }
971   
972   cah->name = GNUNET_malloc(strlen(name)
973                             - strlen(GNUNET_GNS_TLD) + 1);
974   memset(cah->name, 0,
975          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
976   memcpy(cah->name, name,
977          strlen(name)-strlen(GNUNET_GNS_TLD));
978
979   /* Start delegation resolution in our namestore */
980   gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
981
982   GNUNET_STATISTICS_update (statistics,
983                             "Authority lookup attempts", 1, GNUNET_NO);
984 }
985
986
987
988 /**
989  * Reply to client with the result from our lookup.
990  *
991  * @param cls the closure (our client lookup handle)
992  * @param rd_count the number of records
993  * @param rd the record data
994  */
995 static void
996 send_lookup_response(void* cls,
997                      uint32_t rd_count,
998                      const struct GNUNET_NAMESTORE_RecordData *rd)
999 {
1000   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1001   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1002   size_t len;
1003   
1004   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1005               "LOOKUP_RESULT", rd_count);
1006   
1007   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1008   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1009   
1010   rmsg->id = clh->unique_id;
1011   rmsg->rd_count = htonl(rd_count);
1012   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1013   rmsg->header.size = 
1014     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1015   
1016   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1017   
1018   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1019                                 (const struct GNUNET_MessageHeader *) rmsg,
1020                                 GNUNET_NO);
1021   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1022   
1023   GNUNET_free(rmsg);
1024   GNUNET_free(clh->name);
1025   
1026   if (NULL != clh->shorten_key)
1027     GNUNET_free(clh->shorten_key);
1028
1029   GNUNET_free(clh);
1030
1031   GNUNET_STATISTICS_update (statistics,
1032                             "Completed lookups", 1, GNUNET_NO);
1033
1034   if (rd != NULL)
1035   {
1036     GNUNET_STATISTICS_update (statistics,
1037                               "Records resolved", rd_count, GNUNET_NO);
1038   }
1039
1040 }
1041
1042
1043 /**
1044  * Handle lookup requests from client
1045  *
1046  * @param cls the closure
1047  * @param client the client
1048  * @param message the message
1049  */
1050 static void
1051 handle_lookup(void *cls,
1052               struct GNUNET_SERVER_Client * client,
1053               const struct GNUNET_MessageHeader * message)
1054 {
1055   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1056
1057   size_t msg_size = 0;
1058   size_t namelen;
1059   char name[MAX_DNS_NAME_LENGTH];
1060   struct ClientLookupHandle *clh;
1061   char* nameptr = name;
1062   int only_cached;
1063   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1064   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1065   char* tmp_pkey;
1066
1067   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1068   {
1069     GNUNET_break_op (0);
1070     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1071     return;
1072   }
1073
1074   GNUNET_SERVER_notification_context_add (nc, client);
1075
1076   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1077     (struct GNUNET_GNS_ClientLookupMessage *) message;
1078   
1079   msg_size = ntohs(message->size);
1080
1081   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1082   {
1083     GNUNET_break_op (0);
1084     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1085     return;
1086   }
1087
1088   if (1 == ntohl(sh_msg->have_key))
1089   {
1090     pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1091     tmp_pkey = (char*)&sh_msg[1];
1092     key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1093     GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1094   }
1095   else
1096   {
1097     key = NULL;
1098     GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1099   }
1100   
1101   namelen = strlen(name)+1;
1102   clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1103   memset (clh, 0, sizeof (struct ClientLookupHandle));
1104   clh->client = client;
1105   clh->name = GNUNET_malloc(namelen);
1106   strcpy(clh->name, name);
1107   clh->unique_id = sh_msg->id;
1108   clh->type = ntohl(sh_msg->type);
1109   clh->shorten_key = key;
1110
1111   only_cached = ntohl(sh_msg->only_cached);
1112   
1113   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1114     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1115                "LOOKUP: %s is too long", name);
1116     clh->name = NULL;
1117     send_lookup_response(clh, 0, NULL);
1118     return;
1119   }
1120
1121   if ((clh->type == GNUNET_GNS_RECORD_A) &&
1122       (GNUNET_OK != v4_enabled))
1123   {
1124     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1125                "LOOKUP: Query for A record but AF_INET not supported!");
1126     clh->name = NULL;
1127     send_lookup_response(clh, 0, NULL);
1128     return;
1129   }
1130   
1131   if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1132       (GNUNET_OK != v6_enabled))
1133   {
1134     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1135                "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1136     clh->name = NULL;
1137     send_lookup_response(clh, 0, NULL);
1138     return;
1139   }
1140   
1141   if (1 == ntohl(sh_msg->use_default_zone))
1142     clh->zone = zone_hash; //Default zone
1143   else
1144     clh->zone = sh_msg->zone;
1145   
1146   if (GNUNET_YES == auto_import_pkey)
1147   {
1148     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1149                                 clh->shorten_key,
1150                                 default_lookup_timeout,
1151                                 clh->only_cached,
1152                                 &send_lookup_response, clh);  
1153   }
1154   else
1155   {
1156     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1157                                 NULL,
1158                                 default_lookup_timeout,
1159                                 only_cached,
1160                                 &send_lookup_response, clh);
1161   }
1162
1163   GNUNET_STATISTICS_update (statistics,
1164                             "Record lookup attempts", 1, GNUNET_NO);
1165 }
1166
1167 /**
1168  * Test if the given AF is supported by this system.
1169  *
1170  * @param af to test
1171  * @return GNUNET_OK if the AF is supported
1172  */
1173 static int
1174 test_af (int af)
1175 {
1176   int s;
1177
1178   s = socket (af, SOCK_STREAM, 0);
1179   if (-1 == s)
1180   {
1181     if (EAFNOSUPPORT == errno)
1182       return GNUNET_NO;
1183     fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1184     return GNUNET_SYSERR;
1185   }
1186   close (s);
1187   return GNUNET_OK;
1188 }
1189
1190 /**
1191  * Process GNS requests.
1192  *
1193  * @param cls closure)
1194  * @param server the initialized server
1195  * @param c configuration to use
1196  */
1197 static void
1198 run (void *cls, struct GNUNET_SERVER_Handle *server,
1199      const struct GNUNET_CONFIGURATION_Handle *c)
1200 {
1201   
1202   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1203   
1204   char* keyfile;
1205   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1206   unsigned long long max_parallel_bg_queries = 0;
1207   unsigned long long default_lookup_timeout_secs = 0;
1208   int ignore_pending = GNUNET_NO;
1209
1210   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1211     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1212     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1213     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1214   };
1215
1216   GNS_cfg = c;
1217
1218   v6_enabled = test_af (AF_INET6);
1219   v4_enabled = test_af (AF_INET);
1220
1221   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1222                                              "ZONEKEY", &keyfile))
1223   {
1224     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1225                 "No private key for root zone specified!\n");
1226     GNUNET_SCHEDULER_shutdown ();
1227     return;
1228   }
1229
1230   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1231              "Using keyfile %s for root zone.\n", keyfile);
1232
1233   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1234   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1235
1236   GNUNET_CRYPTO_short_hash(&pkey,
1237                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1238                      &zone_hash);
1239   GNUNET_free(keyfile);
1240   
1241   /**
1242    * handle to our local namestore
1243    */
1244   namestore_handle = GNUNET_NAMESTORE_connect(c);
1245
1246   if (NULL == namestore_handle)
1247   {
1248     //FIXME do error handling;
1249     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1250                "Failed to connect to the namestore!\n");
1251     GNUNET_SCHEDULER_shutdown ();
1252     return;
1253   }
1254   
1255   
1256
1257   auto_import_pkey = GNUNET_NO;
1258
1259   if (GNUNET_YES ==
1260       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1261                                             "AUTO_IMPORT_PKEY"))
1262   {
1263     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1264                "Automatic PKEY import is enabled.\n");
1265     auto_import_pkey = GNUNET_YES;
1266
1267   }
1268
1269   dht_max_update_interval.rel_value = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL; // yuck
1270
1271   if (GNUNET_OK ==
1272       GNUNET_CONFIGURATION_get_value_time (c, "gns",
1273                                            "ZONE_PUT_INTERVAL",
1274                                            &dht_max_update_interval))
1275   {
1276     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1277                 "DHT zone update interval: %llu\n",
1278                 (unsigned long long) dht_max_update_interval.rel_value);
1279   }
1280   
1281   max_record_put_interval = 1;
1282
1283   if (GNUNET_OK ==
1284       GNUNET_CONFIGURATION_get_value_number (c, "gns",
1285                                              "RECORD_PUT_INTERVAL",
1286                                              &max_record_put_interval))
1287   {
1288     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1289                "Record put interval: %llu\n",
1290                max_record_put_interval);
1291   }
1292   
1293   if (GNUNET_OK ==
1294       GNUNET_CONFIGURATION_get_value_number (c, "gns",
1295                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
1296                                             &max_parallel_bg_queries))
1297   {
1298     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1299                "Number of allowed parallel background queries: %llu\n",
1300                max_parallel_bg_queries);
1301   }
1302
1303   if (GNUNET_YES ==
1304       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1305                                             "AUTO_IMPORT_CONFIRMATION_REQ"))
1306   {
1307     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1308                "Auto import requires user confirmation\n");
1309     ignore_pending = GNUNET_YES;
1310   }
1311
1312   if (GNUNET_OK ==
1313       GNUNET_CONFIGURATION_get_value_number(c, "gns",
1314                                             "DEFAULT_LOOKUP_TIMEOUT",
1315                                             &default_lookup_timeout_secs))
1316   {
1317     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1318                "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1319     default_lookup_timeout = GNUNET_TIME_relative_multiply(
1320                                             GNUNET_TIME_UNIT_SECONDS,
1321                                             default_lookup_timeout_secs);
1322   }
1323   
1324   /**
1325    * handle to the dht
1326    */
1327   dht_handle = GNUNET_DHT_connect(c,
1328                        //max_parallel_bg_queries); //FIXME get ht_len from cfg
1329                        1024);
1330
1331   if (NULL == dht_handle)
1332   {
1333     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1334   }
1335   
1336   if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1337                         max_parallel_bg_queries,
1338                         ignore_pending)
1339       == GNUNET_SYSERR)
1340   {
1341     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1342                "Unable to initialize resolver!\n");
1343     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1344     return;
1345   }
1346
1347   if (GNUNET_YES ==
1348       GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1349   {
1350     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1351                "DNS hijacking enabled... connecting to service.\n");
1352
1353     if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1354     {
1355       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1356                "Failed to enable the dns interceptor!\n");
1357     }
1358   }
1359   
1360   /**
1361    * Schedule periodic put
1362    * for our records
1363    * We have roughly an hour for all records;
1364    */
1365   record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1366                                                       1);
1367   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1368
1369   GNUNET_SERVER_add_handlers (server, handlers);
1370   
1371   //FIXME
1372   //GNUNET_SERVER_disconnect_notify (server,
1373   //                                 &client_disconnect_notification,
1374   //                                 NULL);
1375
1376   statistics = GNUNET_STATISTICS_create ("gns", c);
1377
1378   nc = GNUNET_SERVER_notification_context_create (server, 1);
1379
1380   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1381                                 NULL);
1382 }
1383
1384
1385 /**
1386  * The main function for the GNS service.
1387  *
1388  * @param argc number of arguments from the command line
1389  * @param argv command line arguments
1390  * @return 0 ok, 1 on error
1391  */
1392 int
1393 main (int argc, char *const *argv)
1394 {
1395   int ret;
1396
1397   ret =
1398       (GNUNET_OK ==
1399        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1400                            NULL)) ? 0 : 1;
1401   return ret;
1402 }
1403
1404 /* end of gnunet-service-gns.c */