-new test, fixes, config
[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
42 #define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45
46 /* Ignore for now not used anyway and probably never will */
47 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
48 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
49
50 /**
51  * Handle to a currenty pending resolution
52  */
53 struct GNUNET_GNS_ResolverHandle
54 {
55   /* The name to resolve */
56   char *name;
57
58   /* the request handle to reply to */
59   struct GNUNET_DNS_RequestHandle *request_handle;
60   
61   /* the dns parser packet received */
62   struct GNUNET_DNSPARSER_Packet *packet;
63   
64   /* the query parsed from the packet */
65
66   struct GNUNET_DNSPARSER_Query *query;
67   
68   /* has this query been answered? how many matches */
69   int answered;
70
71   /* the authoritative zone to query */
72   GNUNET_HashCode authority;
73
74   /* the name of the authoritative zone to query */
75   char *authority_name;
76
77   /**
78    * we have an authority in namestore that
79    * may be able to resolve
80    */
81   int authority_found;
82
83   /* a handle for dht lookups. should be NULL if no lookups are in progress */
84   struct GNUNET_DHT_GetHandle *get_handle;
85
86   /* timeout task for dht lookups */
87   GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
88
89 };
90
91
92 /**
93  * Our handle to the DNS handler library
94  */
95 struct GNUNET_DNS_Handle *dns_handle;
96
97 /**
98  * Our handle to the DHT
99  */
100 struct GNUNET_DHT_Handle *dht_handle;
101
102 /**
103  * Our zone's private key
104  */
105 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
106
107 /**
108  * Our handle to the namestore service
109  * FIXME maybe need a second handle for iteration
110  */
111 struct GNUNET_NAMESTORE_Handle *namestore_handle;
112
113 /**
114  * Handle to iterate over our authoritative zone in namestore
115  */
116 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
117
118 /**
119  * The configuration the GNS service is running with
120  */
121 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
122
123 /**
124  * Our notification context.
125  */
126 static struct GNUNET_SERVER_NotificationContext *nc;
127
128 /**
129  * Our zone hash
130  */
131 GNUNET_HashCode zone_hash;
132
133 /**
134  * Our tld. Maybe get from config file
135  */
136 const char* gnunet_tld = ".gnunet";
137
138 /**
139  * Useful for zone update for DHT put
140  */
141 static int num_public_records =  3600;
142 struct GNUNET_TIME_Relative dht_update_interval;
143 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
144
145 /* Prototypes */
146 static void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer,
147                          uint32_t rd_count,
148                          const struct GNUNET_NAMESTORE_RecordData *rd);
149 static void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
150
151 /**
152  * Task run during shutdown.
153  *
154  * @param cls unused
155  * @param tc unused
156  */
157 static void
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   /* Kill zone task for it may make the scheduler hang */
161   if (zone_update_taskid)
162     GNUNET_SCHEDULER_cancel(zone_update_taskid);
163
164   GNUNET_DNS_disconnect(dns_handle);
165   GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
166   GNUNET_DHT_disconnect(dht_handle);
167 }
168
169 /**
170  * Callback when record data is put into namestore
171  *
172  * @param cls the closure
173  * @param success GNUNET_OK on success
174  * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
175  */
176 void
177 on_namestore_record_put_result(void *cls,
178                                int32_t success,
179                                const char *emsg)
180 {
181   if (GNUNET_NO == success)
182   {
183     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
184     return;
185   }
186   else if (GNUNET_YES == success)
187   {
188     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189                "records successfully put in namestore\n");
190     return;
191   }
192
193   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
194              "Error putting records into namestore: %s\n", emsg);
195 }
196
197 /**
198  * Handle timeout for DHT requests
199  *
200  * @param cls the request handle as closure
201  * @param tc the task context
202  */
203 static void
204 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 {
206   struct GNUNET_GNS_ResolverHandle *rh = cls;
207
208   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
209              "dht lookup for query %s (type=%d) timed out.\n",
210              rh->name, rh->query->type);
211
212   GNUNET_DHT_get_stop (rh->get_handle);
213   reply_to_dns(rh, 0, NULL);
214 }
215
216 /**
217  * Function called when we get a result from the dht
218  * for our query
219  *
220  * @param cls the request handle
221  * @param exp lifetime
222  * @param key the key the record was stored under
223  * @param get_path get path
224  * @param get_path_length get path length
225  * @param put_path put path
226  * @param put_path_length put path length
227  * @param type the block type
228  * @param size the size of the record
229  * @param data the record data
230  */
231 static void
232 process_authority_dht_result(void* cls,
233                  struct GNUNET_TIME_Absolute exp,
234                  const GNUNET_HashCode * key,
235                  const struct GNUNET_PeerIdentity *get_path,
236                  unsigned int get_path_length,
237                  const struct GNUNET_PeerIdentity *put_path,
238                  unsigned int put_path_length,
239                  enum GNUNET_BLOCK_Type type,
240                  size_t size, const void *data)
241 {
242   struct GNUNET_GNS_ResolverHandle *rh;
243   struct GNSNameRecordBlock *nrb;
244   uint32_t num_records;
245   char* name = NULL;
246   char* rd_data = (char*) data;
247   int i;
248   int rd_size;
249   GNUNET_HashCode zone, name_hash;
250   
251   if (data == NULL)
252     return;
253   
254   //FIXME check expiration?
255   
256   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
257   nrb = (struct GNSNameRecordBlock*)data;
258   
259   /* stop dht lookup and timeout task */
260   GNUNET_DHT_get_stop (rh->get_handle);
261   GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
262
263   rh->get_handle = NULL;
264   num_records = ntohl(nrb->rd_count);
265   name = (char*)&nrb[1];
266   {
267     struct GNUNET_NAMESTORE_RecordData rd[num_records];
268     
269     rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
270     rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
271   
272     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
273                                                                rd_data,
274                                                                num_records,
275                                                                rd))
276     {
277       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
278       return;
279     }
280
281     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
282                "Got name: %s (wanted %s)\n", name, rh->authority_name);
283     for (i=0; i<num_records; i++)
284     {
285     
286       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
287                 "Got name: %s (wanted %s)\n", name, rh->authority_name);
288       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
289                  "Got type: %d (wanted %d)\n",
290                  rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
291       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
292                  "Got data length: %d\n", rd[i].data_size);
293       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294                  "Got flag %d\n", rd[i].flags);
295
296       if ((strcmp(name, rh->authority_name) == 0) &&
297           (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
298       {
299         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
300         rh->answered = 1;
301         GNUNET_CRYPTO_hash(
302                    (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
303                    rd[i].data_size,
304                    &rh->authority);
305       }
306
307     }
308
309
310     GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
311     GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
312
313     /* Save to namestore */
314     if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
315     {
316       GNUNET_NAMESTORE_record_put (namestore_handle,
317                                  &nrb->public_key,
318                                  name,
319                                  exp,
320                                  num_records,
321                                  rd,
322                                  &nrb->signature,
323                                  &on_namestore_record_put_result, //cont
324                                  NULL); //cls
325     }
326   }
327   
328   if (rh->answered)
329   {
330     rh->answered = 0;
331     resolve_name(rh);
332     return;
333   }
334   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
335   reply_to_dns(rh, 0, NULL);
336 }
337
338 /**
339  * Start DHT lookup for a name -> PKEY (compare NS) record in
340  * query->authority's zone
341  *
342  * @param rh the pending gns query
343  * @param name the name of the PKEY record
344  */
345 static void
346 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
347 {
348   uint32_t xquery;
349   GNUNET_HashCode name_hash;
350   GNUNET_HashCode lookup_key;
351
352   GNUNET_CRYPTO_hash(rh->authority_name,
353                      strlen(rh->authority_name),
354                      &name_hash);
355   GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
356
357   rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
358                                                        &dht_lookup_timeout, rh);
359
360   xquery = htonl(GNUNET_GNS_RECORD_PKEY);
361   //FIXME how long to wait for results?
362   rh->get_handle = GNUNET_DHT_get_start(dht_handle,
363                        DHT_OPERATION_TIMEOUT,
364                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
365                        &lookup_key,
366                        DHT_GNS_REPLICATION_LEVEL,
367                        GNUNET_DHT_RO_NONE,
368                        &xquery,
369                        sizeof(xquery),
370                        &process_authority_dht_result,
371                        rh);
372
373 }
374
375 /**
376  * Function called when we get a result from the dht
377  * for our query
378  *
379  * @param cls the request handle
380  * @param exp lifetime
381  * @param key the key the record was stored under
382  * @param get_path get path
383  * @param get_path_length get path length
384  * @param put_path put path
385  * @param put_path_length put path length
386  * @param type the block type
387  * @param size the size of the record
388  * @param data the record data
389  */
390 static void
391 process_name_dht_result(void* cls,
392                  struct GNUNET_TIME_Absolute exp,
393                  const GNUNET_HashCode * key,
394                  const struct GNUNET_PeerIdentity *get_path,
395                  unsigned int get_path_length,
396                  const struct GNUNET_PeerIdentity *put_path,
397                  unsigned int put_path_length,
398                  enum GNUNET_BLOCK_Type type,
399                  size_t size, const void *data)
400 {
401   struct GNUNET_GNS_ResolverHandle *rh;
402   struct GNSNameRecordBlock *nrb;
403   uint32_t num_records;
404   char* name = NULL;
405   char* rd_data = (char*)data;
406   int i;
407   int rd_size;
408   
409   GNUNET_HashCode zone, name_hash;
410   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
411   
412   if (data == NULL)
413     return;
414
415   //FIXME maybe check expiration here, check block type
416   
417   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
418   nrb = (struct GNSNameRecordBlock*)data;
419   
420   /* stop lookup and timeout task */
421   GNUNET_DHT_get_stop (rh->get_handle);
422   GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
423   
424   rh->get_handle = NULL;
425   name = (char*)&nrb[1];
426   num_records = ntohl(nrb->rd_count);
427   {
428     struct GNUNET_NAMESTORE_RecordData rd[num_records];
429
430     rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
431     rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
432   
433     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
434                                                                rd_data,
435                                                                num_records,
436                                                                rd))
437     {
438       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
439       return;
440     }
441
442     for (i=0; i<num_records; i++)
443     {
444       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
445                "Got name: %s (wanted %s)\n", name, rh->name);
446       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447                "Got type: %d (wanted %d)\n",
448                rd[i].record_type, rh->query->type);
449       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
450                "Got data length: %d\n", rd[i].data_size);
451       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
452                "Got flag %d\n", rd[i].flags);
453     
454      if ((strcmp(name, rh->name) == 0) &&
455          (rd[i].record_type == rh->query->type))
456       {
457         rh->answered++;
458       }
459
460     }
461
462     GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
463     GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
464   
465     /**
466      * FIXME check pubkey against existing key in namestore?
467      * https://gnunet.org/bugs/view.php?id=2179
468      */
469
470     /* Save to namestore */
471     GNUNET_NAMESTORE_record_put (namestore_handle,
472                                  &nrb->public_key,
473                                  name,
474                                  exp,
475                                  num_records,
476                                  rd,
477                                  &nrb->signature,
478                                  &on_namestore_record_put_result, //cont
479                                  NULL); //cls
480   
481     if (rh->answered)
482       reply_to_dns(rh, num_records, rd);
483     else
484       reply_to_dns(rh, 0, NULL);
485   }
486
487 }
488
489
490
491
492 /**
493  * Start DHT lookup for a (name -> query->record_type) record in
494  * query->authority's zone
495  *
496  * @param rh the pending gns query context
497  * @param name the name to query record
498  */
499 static void
500 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
501 {
502   uint32_t xquery;
503   GNUNET_HashCode name_hash;
504   GNUNET_HashCode lookup_key;
505   struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
506
507   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
508   GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
509   GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
510   
511   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
512              "starting dht lookup for %s with key: %s\n",
513              name, (char*)&lookup_key_string);
514
515   rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
516                                                       &dht_lookup_timeout, rh);
517
518   xquery = htonl(rh->query->type);
519   //FIXME how long to wait for results?
520   rh->get_handle = GNUNET_DHT_get_start(dht_handle, 
521                        DHT_OPERATION_TIMEOUT,
522                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
523                        &lookup_key,
524                        DHT_GNS_REPLICATION_LEVEL,
525                        GNUNET_DHT_RO_NONE,
526                        &xquery, 
527                        sizeof(xquery),
528                        &process_name_dht_result,
529                        rh);
530
531 }
532
533 //Prototype
534 static void
535 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
536
537 /**
538  * This is a callback function that should give us only PKEY
539  * records. Used to query the namestore for the authority (PKEY)
540  * for 'name'
541  *
542  * @param cls the pending query
543  * @param key the key of the zone we did the lookup
544  * @param expiration expiration date of the record data set in the namestore
545  * @param name the name for which we need an authority
546  * @param rd_count the number of records with 'name'
547  * @param rd the record data
548  * @param signature the signature of the authority for the record data
549  */
550 static void
551 process_authority_lookup(void* cls,
552                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
553                    struct GNUNET_TIME_Absolute expiration,
554                    const char *name,
555                    unsigned int rd_count,
556                    const struct GNUNET_NAMESTORE_RecordData *rd,
557                    const struct GNUNET_CRYPTO_RsaSignature *signature)
558 {
559   struct GNUNET_GNS_ResolverHandle *rh;
560   struct GNUNET_TIME_Relative remaining_time;
561   GNUNET_HashCode zone;
562   
563   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
564              rd_count);
565
566   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
567   GNUNET_CRYPTO_hash(key,
568                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
569                      &zone);
570   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
571   
572   /**
573    * No authority found in namestore.
574    */
575   if (rd_count == 0)
576   {
577     /**
578      * We did not find an authority in the namestore
579      * _IF_ the current authoritative zone is us we cannot resolve
580      * _ELSE_ we can still check the _expired_ dht
581      */
582     if ((0 != GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash)) &&
583         (remaining_time.rel_value == 0))
584     {
585       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
586                  "Authority %s unknown in namestore, trying dht\n",
587                  rh->authority_name);
588       resolve_authority_dht(rh);
589       return;
590     }
591     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
592                rh->authority_name);
593     reply_to_dns(rh, 0, NULL);
594     return;
595   }
596
597   //Note only 1 pkey should have been returned.. anything else would be strange
598   /**
599    * We found an authority that may be able to help us
600    * move on with query
601    */
602   int i;
603   for (i=0; i<rd_count;i++)
604   {
605   
606     if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
607       continue;
608     
609       if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
610           == 0)
611       {
612         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
613         if (remaining_time.rel_value == 0)
614         {
615           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
616                      "This dht entry is expired. Refreshing\n");
617           resolve_authority_dht(rh);
618         }
619
620         continue;
621       }
622
623       /**
624        * Resolve rest of query with new authority
625        */
626       GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
627       GNUNET_CRYPTO_hash(rd[i].data,
628                          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
629                          &rh->authority);
630       resolve_name(rh);
631       return;
632       
633   }
634     
635   /**
636    * no answers found
637    */
638   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
639              "Authority lookup successful but no PKEY... never get here\n");
640   reply_to_dns(rh, 0, NULL);
641 }
642
643
644 /**
645  * Reply to client with the result from our lookup.
646  *
647  * @param rh the request handle of the lookup
648  * @param rd_count the number of records to return
649  * @param rd the record data
650  */
651 static void
652 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
653              const struct GNUNET_NAMESTORE_RecordData *rd)
654 {
655   int i;
656   size_t len;
657   int ret;
658   char *buf;
659   struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
660   struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
661   struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
662   packet->answers = answer_records;
663   packet->additional_records = additional_records;
664   
665   /**
666    * Put records in the DNS packet and modify it
667    * to a response
668    */
669   len = sizeof(struct GNUNET_DNSPARSER_Record*);
670   for (i=0; i < rd_count; i++)
671   {
672     
673     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
674                "Adding type %d to DNS response\n", rd[i].record_type);
675     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
676     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
677     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
678     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
679     
680     if (rd[i].record_type == rh->query->type)
681     {
682       answer_records[i].name = rh->query->name;
683       answer_records[i].type = rd[i].record_type;
684       answer_records[i].data.raw.data_len = rd[i].data_size;
685       answer_records[i].data.raw.data = (char*)rd[i].data;
686       answer_records[i].expiration_time = rd[i].expiration;
687       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
688     }
689     else
690     {
691       additional_records[i].name = rh->query->name;
692       additional_records[i].type = rd[i].record_type;
693       additional_records[i].data.raw.data_len = rd[i].data_size;
694       additional_records[i].data.raw.data = (char*)rd[i].data;
695       additional_records[i].expiration_time = rd[i].expiration;
696       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
697     }
698   }
699   
700   packet->num_answers = rh->answered;
701   packet->num_additional_records = rd_count-(rh->answered);
702   
703   if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
704     packet->flags.authoritative_answer = 1;
705   else
706     packet->flags.authoritative_answer = 0;
707
708   if (rd == NULL)
709     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
710   else
711     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
712   
713   packet->flags.query_or_response = 1;
714
715   
716   /**
717    * Reply to DNS
718    */
719   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
720              "Building DNS response\n");
721   ret = GNUNET_DNSPARSER_pack (packet,
722                                1024, /* FIXME magic from dns redirector */
723                                &buf,
724                                &len);
725   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
726              "Built DNS response! (ret=%d,len=%d)\n", ret, len);
727   if (ret == GNUNET_OK)
728   {
729     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
730                "Answering DNS request\n");
731     GNUNET_DNS_request_answer(rh->request_handle,
732                               len,
733                               buf);
734     //GNUNET_free(answer);
735     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
736   }
737   else
738   {
739     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
740                "Error building DNS response! (ret=%d)", ret);
741   }
742
743   GNUNET_free(rh->name);
744   GNUNET_free(rh);
745 }
746
747
748 /**
749  * Namestore calls this function if we have record for this name.
750  * (or with rd_count=0 to indicate no matches)
751  *
752  * @param cls the pending query
753  * @param key the key of the zone we did the lookup
754  * @param expiration expiration date of the namestore entry
755  * @param name the name for which we need an authority
756  * @param rd_count the number of records with 'name'
757  * @param rd the record data
758  * @param signature the signature of the authority for the record data
759  */
760 static void
761 process_authoritative_result(void* cls,
762                   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
763                   struct GNUNET_TIME_Absolute expiration,
764                   const char *name, unsigned int rd_count,
765                   const struct GNUNET_NAMESTORE_RecordData *rd,
766                   const struct GNUNET_CRYPTO_RsaSignature *signature)
767 {
768   struct GNUNET_GNS_ResolverHandle *rh;
769   struct GNUNET_TIME_Relative remaining_time;
770   GNUNET_HashCode zone;
771
772   rh = (struct GNUNET_GNS_ResolverHandle *) cls;
773   GNUNET_CRYPTO_hash(key,
774                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
775                      &zone);
776   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
777
778   if (rd_count == 0)
779   {
780     /**
781      * Lookup terminated and no results
782      * -> DHT Phase unless data is recent
783      */
784     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
785                "Namestore lookup for %s terminated without results\n", name);
786     
787     /**
788      * if this is not our zone we cannot rely on the namestore to be
789      * complete. -> Query DHT
790      */
791     if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
792     {
793       if (remaining_time.rel_value == 0)
794       {
795         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
796                    "trying dht...\n");
797         resolve_name_dht(rh, name);
798         return;
799       }
800       else
801       {
802         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
803                    "Record is still recent. No DHT lookup\n");
804       }
805     }
806
807     /**
808      * Our zone and no result? Cannot resolve TT
809      */
810     GNUNET_assert(rh->answered == 0);
811     reply_to_dns(rh, 0, NULL);
812     return;
813
814   }
815   else
816   {
817     
818     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
819                "Processing additional result %s from namestore\n", name);
820     int i;
821     for (i=0; i<rd_count;i++)
822     {
823       
824       if ((strcmp(name, rh->query->name) == 0)
825           && (rd[i].record_type != rh->query->type))
826         continue;
827       
828       if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
829           == 0)
830       {
831         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
832         continue;
833       }
834       
835       rh->answered++;
836       
837     }
838     
839     /**
840      * no answers found
841      * consult dht if expired
842      */
843     if ((remaining_time.rel_value == 0) && (rh->answered == 0))
844     {
845       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
846                  "This dht entry is old. Refreshing.\n");
847       resolve_name_dht(rh, name);
848       return;
849     }
850     
851     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
852                rh->answered);
853
854     reply_to_dns(rh, rd_count, rd);
855   }
856 }
857
858 /**
859  * Determine if this name is canonical.
860  * i.e.
861  * a.b.gnunet  = not canonical
862  * a           = canonical
863  *
864  * @param name the name to test
865  * @return 1 if canonical
866  */
867 static int
868 is_canonical(char* name)
869 {
870   uint32_t len = strlen(name);
871   int i;
872
873   for (i=0; i<len; i++)
874   {
875     if (*(name+i) == '.')
876       return 0;
877   }
878   return 1;
879 }
880
881 /**
882  * Move one level up in the domain hierarchy and return the
883  * passed top level domain.
884  *
885  * @param name the domain
886  * @return the tld
887  */
888 static char*
889 pop_tld(char* name)
890 {
891   uint32_t len;
892
893   if (is_canonical(name))
894     return NULL;
895
896   for (len = strlen(name); len > 0; len--)
897   {
898     if (*(name+len) == '.')
899       break;
900   }
901
902   if (len == 0)
903     return NULL;
904
905   name[len] = '\0';
906
907   return (name+len+1);
908 }
909
910
911 /**
912  * The first phase of resolution.
913  * First check if the name is canonical.
914  * If it is then try to resolve directly.
915  * If not then we first have to resolve the authoritative entities.
916  *
917  * @param rh the pending lookup
918  */
919 static void
920 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
921 {
922   if (is_canonical(rh->name))
923   {
924     /* We only need to check the current zone's ns */
925     GNUNET_NAMESTORE_lookup_record(namestore_handle,
926                                &rh->authority,
927                                rh->name,
928                                rh->query->type,
929                                &process_authoritative_result,
930                                rh);
931   }
932   else
933   {
934     /* We have to resolve the authoritative entity first */
935     rh->authority_name = pop_tld(rh->name);
936     GNUNET_NAMESTORE_lookup_record(namestore_handle,
937                                  &rh->authority,
938                                  rh->authority_name,
939                                  GNUNET_GNS_RECORD_PKEY,
940                                  &process_authority_lookup,
941                                  rh);
942   }
943 }
944
945 /**
946  * Entry point for name resolution
947  * Setup a new query and try to resolve
948  *
949  * @param request the request handle of the DNS request from a client
950  * @param p the DNS query packet we received
951  * @param q the DNS query we received parsed from p
952  */
953 static void
954 start_resolution(struct GNUNET_DNS_RequestHandle *request,
955                  struct GNUNET_DNSPARSER_Packet *p,
956                  struct GNUNET_DNSPARSER_Query *q)
957 {
958   struct GNUNET_GNS_ResolverHandle *rh;
959   
960   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961               "Starting resolution for %s (type=%d)!\n",
962               q->name, q->type);
963   
964   rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
965   rh->packet = p;
966   rh->query = q;
967   rh->authority = zone_hash;
968   
969   rh->name = GNUNET_malloc(strlen(q->name)
970                               - strlen(gnunet_tld) + 1);
971   memset(rh->name, 0,
972          strlen(q->name)-strlen(gnunet_tld) + 1);
973   memcpy(rh->name, q->name,
974          strlen(q->name)-strlen(gnunet_tld));
975
976   rh->request_handle = request;
977
978   /* Start resolution in our zone */
979   resolve_name(rh);
980 }
981
982 /**
983  * The DNS request handler
984  * Called for every incoming DNS request.
985  *
986  * @param cls closure
987  * @param rh request handle to user for reply
988  * @param request_length number of bytes in request
989  * @param request udp payload of the DNS request
990  */
991 static void
992 handle_dns_request(void *cls,
993                    struct GNUNET_DNS_RequestHandle *rh,
994                    size_t request_length,
995                    const char *request)
996 {
997   struct GNUNET_DNSPARSER_Packet *p;
998   char *tldoffset;
999
1000   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1001   p = GNUNET_DNSPARSER_parse (request, request_length);
1002   
1003   if (NULL == p)
1004   {
1005     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1006                 "Received malformed DNS packet, leaving it untouched\n");
1007     GNUNET_DNS_request_forward (rh);
1008     return;
1009   }
1010   
1011   /**
1012    * Check tld and decide if we or
1013    * legacy dns is responsible
1014    *
1015    * FIXME now in theory there could be more than 1 query in the request
1016    * but if this is case we get into trouble:
1017    * either we query the GNS or the DNS. We cannot do both!
1018    * So I suggest to either only allow a single query per request or
1019    * only allow GNS or DNS requests.
1020    * The way it is implemented here now is buggy and will lead to erratic
1021    * behaviour (if multiple queries are present).
1022    */
1023   if (p->num_queries == 0)
1024   {
1025     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026                 "No Queries in DNS packet... forwarding\n");
1027     GNUNET_DNS_request_forward (rh);
1028   }
1029
1030   if (p->num_queries > 1)
1031   {
1032     /* Note: We could also look for .gnunet */
1033     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034                 ">1 queriy in DNS packet... odd. We only process #1\n");
1035   }
1036
1037   
1038   /**
1039    * Check for .gnunet
1040    */
1041   tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1042
1043   while ((*tldoffset) != '.')
1044     tldoffset--;
1045   
1046   if (0 == strcmp(tldoffset, gnunet_tld))
1047   {
1048     start_resolution(rh, p, p->queries);
1049   }
1050   else
1051   {
1052     /**
1053      * This request does not concern us. Forward to real DNS.
1054      */
1055     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1056                "Request for %s is forwarded to DNS\n", p->queries[0].name);
1057     GNUNET_DNS_request_forward (rh);
1058   }
1059
1060 }
1061
1062 /**
1063  * test function that stores some data in the namestore
1064  * This will also be replaced by a test progrm that
1065  * directl interfaces with the namestore
1066  */
1067 static void
1068 put_some_records(void)
1069 {
1070   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Populating namestore\n");
1071   /* put an A record into namestore FIXME use gnunet.org */
1072   char* ipB = "5.6.7.8";
1073
1074   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
1075   struct GNUNET_NAMESTORE_RecordData rdb_web;
1076
1077   GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
1078
1079   rdb_web.data_size = sizeof(struct in_addr);
1080   rdb_web.data = web;
1081   rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
1082   rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1083   
1084   GNUNET_NAMESTORE_record_create (namestore_handle,
1085                                   zone_key,
1086                                   "www",
1087                                   &rdb_web,
1088                                   NULL,
1089                                   NULL);
1090 }
1091
1092 /**
1093  * Method called periodicattluy that triggers
1094  * iteration over root zone
1095  *
1096  * @param cls closure
1097  * @param tc task context
1098  */
1099 static void
1100 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 {
1102   GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1103 }
1104
1105 /**
1106  * Continuation for DHT put
1107  *
1108  * @param cls closure
1109  * @param tc task context
1110  */
1111 static void
1112 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1113 {
1114   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1115 }
1116
1117 /* prototype */
1118 static void
1119 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1120
1121 /**
1122  * Function used to put all records successively into the DHT.
1123  *
1124  * @param cls the closure (NULL)
1125  * @param key the public key of the authority (ours)
1126  * @param expiration lifetime of the namestore entry
1127  * @param name the name of the records
1128  * @param rd_count the number of records in data
1129  * @param rd the record data
1130  * @param signature the signature for the record data
1131  */
1132 static void
1133 put_gns_record(void *cls,
1134                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1135                 struct GNUNET_TIME_Absolute expiration,
1136                 const char *name,
1137                 unsigned int rd_count,
1138                 const struct GNUNET_NAMESTORE_RecordData *rd,
1139                 const struct GNUNET_CRYPTO_RsaSignature *signature)
1140 {
1141   
1142   struct GNSNameRecordBlock *nrb;
1143   GNUNET_HashCode name_hash;
1144   GNUNET_HashCode xor_hash;
1145   struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1146   uint32_t rd_payload_length;
1147   char* nrb_data = NULL;
1148
1149   /* we're done */
1150   if (NULL == name)
1151   {
1152     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1153     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1154     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1155                                                    NULL);
1156     return;
1157   }
1158   
1159   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1160              "Putting records for %s into the DHT\n", name);
1161   
1162   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1163   
1164   nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1 
1165                       + sizeof(struct GNSNameRecordBlock));
1166   
1167   if (signature != NULL)
1168     nrb->signature = *signature;
1169   
1170   nrb->public_key = *key;
1171
1172   nrb->rd_count = htonl(rd_count);
1173   
1174   memset(&nrb[1], 0, strlen(name) + 1);
1175   memcpy(&nrb[1], name, strlen(name));
1176
1177   nrb_data = (char*)&nrb[1];
1178   nrb_data += strlen(name) + 1;
1179
1180   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1181                                                 rd,
1182                                                 rd_payload_length,
1183                                                 nrb_data))
1184   {
1185     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Recor serialization failed!\n");
1186   }
1187
1188
1189   /*
1190    * calculate DHT key: H(name) xor H(pubkey)
1191    */
1192   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1193   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1194   GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1195   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196              "putting records for %s under key: %s with size %d\n",
1197              name, (char*)&xor_hash_string, rd_payload_length);
1198
1199   GNUNET_DHT_put (dht_handle, &xor_hash,
1200                   DHT_GNS_REPLICATION_LEVEL,
1201                   GNUNET_DHT_RO_NONE,
1202                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1203                   rd_payload_length,
1204                   (char*)nrb,
1205                   expiration,
1206                   DHT_OPERATION_TIMEOUT,
1207                   &record_dht_put,
1208                   NULL); //cls for cont
1209   
1210   num_public_records++;
1211
1212   /**
1213    * Reschedule periodic put
1214    */
1215   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1216                                 &update_zone_dht_next,
1217                                 NULL);
1218
1219 }
1220
1221 /**
1222  * Puts a single trusted entity into the
1223  * namestore. Will be replaced in a testcase
1224  * that directly interacts with a persistent
1225  * namestore.
1226  *
1227  * @param name name of entity
1228  * @param keyfile keyfile
1229  */
1230 static void
1231 put_trusted(char* name, char* keyfile)
1232 {
1233   struct GNUNET_NAMESTORE_RecordData rd;
1234   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1235   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1236   pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1237
1238   key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1239   GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1240   rd.data = pkey;
1241   rd.expiration = GNUNET_TIME_absolute_get_forever ();
1242   rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1243   rd.record_type = GNUNET_GNS_RECORD_PKEY;
1244
1245   GNUNET_NAMESTORE_record_create (namestore_handle,
1246                                   zone_key,
1247                                   name,
1248                                   &rd,
1249                                   NULL,
1250                                   NULL);
1251 }
1252
1253
1254
1255 /**
1256  * Periodically iterate over our zone and store everything in dht
1257  *
1258  * @param cls NULL
1259  * @param tc task context
1260  */
1261 static void
1262 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1263 {
1264   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1265   if (0 == num_public_records)
1266   {
1267     dht_update_interval = GNUNET_TIME_relative_multiply(
1268                                                       GNUNET_TIME_UNIT_SECONDS,
1269                                                       1);
1270   }
1271   else
1272   {
1273     dht_update_interval = GNUNET_TIME_relative_multiply(
1274                                                       GNUNET_TIME_UNIT_SECONDS,
1275                                                      (3600/num_public_records));
1276   }
1277   num_public_records = 0; //start counting again
1278   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1279                                                           &zone_hash,
1280                                                           GNUNET_NAMESTORE_RF_AUTHORITY,
1281                                                           GNUNET_NAMESTORE_RF_PRIVATE,
1282                                                           &put_gns_record,
1283                                                           NULL);
1284 }
1285
1286 /**
1287  * Process GNS requests.
1288  *
1289  * @param cls closure
1290  * @param server the initialized server
1291  * @param c configuration to use
1292  */
1293 static void
1294 run (void *cls, struct GNUNET_SERVER_Handle *server,
1295      const struct GNUNET_CONFIGURATION_Handle *c)
1296 {
1297   
1298   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1299
1300   char* keyfile;
1301   char* trusted_entities;
1302   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1303
1304   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1305                                              "ZONEKEY", &keyfile))
1306   {
1307     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1308                 "No private key for root zone specified%s!\n", keyfile);
1309     GNUNET_SCHEDULER_shutdown(0);
1310     return;
1311   }
1312
1313   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1314   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1315
1316   GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1317                      &zone_hash);
1318   
1319   nc = GNUNET_SERVER_notification_context_create (server, 1);
1320
1321   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1322                                 NULL);
1323
1324   if (GNUNET_YES ==
1325       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1326                                             "HIJACK_DNS"))
1327   {
1328     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1329                "DNS hijacking enabled... connecting to service.\n");
1330     /**
1331      * Do gnunet dns init here
1332      */
1333     dns_handle = GNUNET_DNS_connect(c,
1334                                     GNUNET_DNS_FLAG_PRE_RESOLUTION,
1335                                     &handle_dns_request, /* rh */
1336                                     NULL); /* Closure */
1337     if (NULL == dns_handle)
1338     {
1339       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1340                "Failed to connect to the dnsservice!\n");
1341     }
1342   }
1343
1344   
1345
1346   /**
1347    * handle to our local namestore
1348    */
1349   namestore_handle = GNUNET_NAMESTORE_connect(c);
1350
1351   if (NULL == namestore_handle)
1352   {
1353     //FIXME do error handling;
1354     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1355                "Failed to connect to the namestore!\n");
1356     GNUNET_SCHEDULER_shutdown(0);
1357     return;
1358   }
1359   
1360   /**
1361    * handle to the dht
1362    */
1363   dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1364
1365   if (NULL == dht_handle)
1366   {
1367     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1368   }
1369
1370   //put_some_records(); //FIXME for testing
1371   
1372   /**
1373    * Schedule periodic put
1374    * for our records
1375    * We have roughly an hour for all records;
1376    */
1377   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1378                                                       1);
1379   //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1380
1381 }
1382
1383
1384 /**
1385  * The main function for the GNS service.
1386  *
1387  * @param argc number of arguments from the command line
1388  * @param argv command line arguments
1389  * @return 0 ok, 1 on error
1390  */
1391 int
1392 main (int argc, char *const *argv)
1393 {
1394   int ret;
1395
1396   ret =
1397       (GNUNET_OK ==
1398        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1399                            NULL)) ? 0 : 1;
1400   return ret;
1401 }
1402
1403 /* end of gnunet-service-gns.c */