-more fixes
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file gns/gnunet-service-gns.c
23  * @brief GNUnet GNS service
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gns.h"
34
35
36 /* TODO into gnunet_protocols */
37 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
38 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
39
40 struct GNUNET_GNS_QueryRecordList
41 {
42   /**
43    * DLL
44    */
45   struct GNUNET_GNS_QueryRecordList * next;
46   struct GNUNET_GNS_QueryRecordList * prev;
47
48   struct GNUNET_DNSPARSER_Record * record;
49 };
50
51 /**
52  * A result list for namestore queries
53  */
54 struct GNUNET_GNS_PendingQuery
55 {
56   /* the answer packet */
57   struct GNUNET_DNSPARSER_Packet *answer;
58
59   /* records to put into answer packet */
60   struct GNUNET_GNS_QueryRecordList * records_head;
61   struct GNUNET_GNS_QueryRecordList * records_tail;
62
63   int num_records;
64   int num_authority_records; //FIXME are all of our replies auth?
65   
66   /* the dns request id */
67   int id; // FIXME can handle->request_id also be used here?
68
69   /* the request handle to reply to */
70   struct GNUNET_DNS_RequestHandle *request_handle;
71
72   /* hast this query been answered? */
73   int answered;
74 };
75
76
77 /**
78  * Our handle to the DNS handler library
79  */
80 struct GNUNET_DNS_Handle *dns_handle;
81 struct GNUNET_DNS_Handle *dns_res_handle;
82
83 /**
84  * Our handle to the namestore service
85  */
86 struct GNUNET_NAMESTORE_Handle *namestore_handle;
87
88 /**
89  * The configuration the GNS service is running with
90  */
91 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
92
93 /**
94  * Our notification context.
95  */
96 static struct GNUNET_SERVER_NotificationContext *nc;
97
98 /**
99  * Our zone hash
100  */
101 const GNUNET_HashCode *my_zone;
102
103 /**
104  * Task run during shutdown.
105  *
106  * @param cls unused
107  * @param tc unused
108  */
109 static void
110 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   GNUNET_DNS_disconnect(dns_handle);
113   GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
114 }
115
116 /**
117  * Phase 2 of resolution.
118  */
119 void
120 lookup_dht()
121 {
122 }
123
124 void
125 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
126 {
127   struct GNUNET_GNS_QueryRecordList *i;
128   struct GNUNET_DNSPARSER_Packet *packet;
129   struct GNUNET_DNSPARSER_Flags dnsflags;
130   int j;
131   size_t len;
132   int ret;
133   char *buf;
134   
135   packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
136   packet->answers =
137     GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
138   
139   len = sizeof(struct GNUNET_DNSPARSER_Record*);
140   j = 0;
141   for (i=answer->records_head; i != NULL; i=i->next)
142   {
143     memcpy(&packet->answers[j], 
144            i->record,
145            sizeof (struct GNUNET_DNSPARSER_Record));
146     GNUNET_free(i->record);
147     j++;
148   }
149   
150   /* FIXME how to handle auth, additional etc */
151   packet->num_answers = answer->num_records;
152   packet->num_authority_records = answer->num_authority_records;
153
154   dnsflags.authoritative_answer = 1;
155   dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_YXDOMAIN; //not sure
156   dnsflags.query_or_response = 1;
157   packet->flags = dnsflags;
158
159   packet->id = answer->id;
160   ret = GNUNET_DNSPARSER_pack (packet,
161                                1024, /* FIXME magic from dns redirector */
162                                &buf,
163                                &len);
164   if (ret == GNUNET_OK)
165   {
166     GNUNET_DNS_request_answer(answer->request_handle,
167                               len,
168                               buf);
169     GNUNET_free(answer);
170     //FIXME return code, free datastructures
171   }
172   else
173   {
174     fprintf(stderr, "Error building DNS response! (ret=%d)", ret);
175   }
176 }
177
178 static void
179 process_ns_result(void* cls, const GNUNET_HashCode *zone,
180                   const char *name, uint32_t record_type,
181                   struct GNUNET_TIME_Absolute expiration,
182                   enum GNUNET_NAMESTORE_RecordFlags flags,
183                   const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
184                   size_t size, const void *data)
185 {
186   struct GNUNET_GNS_PendingQuery *query;
187   struct GNUNET_GNS_QueryRecordList *qrecord;
188   struct GNUNET_DNSPARSER_Record *record;
189   query = (struct GNUNET_GNS_PendingQuery *) cls;
190
191
192   if (NULL == data)
193   {
194     /**
195      * Last result received (or none)
196      * Do we have what we need to answer?
197      * If not -> DHT Phase
198      * FIXME extract to func
199      */
200     if (query->answered)
201       reply_to_dns(query);
202     else
203       lookup_dht(); //TODO
204
205   }
206   else
207   {
208     /**
209      * New result
210      */
211     qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
212     record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
213     qrecord->record = record;
214
215     record->name = (char*)name;
216     /* FIXME for gns records this requires the dnsparser to be modified!
217      * or use RAW
218      * */
219     //record->data = data; FIXME!
220     record->expiration_time = expiration;
221     record->type = record_type;
222     record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
223
224     if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
225     {
226       query->num_authority_records++;
227     }
228
229     query->num_records++;
230
231     //FIXME watch for leaks
232     GNUNET_CONTAINER_DLL_insert(query->records_head,
233                                 query->records_tail,
234                                 qrecord);
235   }
236 }
237
238
239 void
240 handle_dns_response(void *cls,
241                    struct GNUNET_DNS_RequestHandle *rh,
242                    size_t request_length,
243                    const char *request)
244 {
245   fprintf (stderr, "This is a response!\n");
246   GNUNET_DNS_request_forward (rh);
247 }
248
249 void
250 lookup_namestore(char* name, uint16_t id, uint16_t type)
251 {
252   struct GNUNET_GNS_PendingQuery *answer;
253   
254   /**
255    * Do db lookup here. Make dht lookup if necessary 
256    * FIXME for now only local lookups for our zone!
257    */
258   fprintf (stderr, "This is .gnunet (%s)!\n", name);
259   answer = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
260   answer->id = id;
261   
262   GNUNET_NAMESTORE_lookup_name(namestore_handle,
263                                my_zone,
264                                name,
265                                type,
266                                &process_ns_result,
267                                answer);
268 }
269
270 /**
271  * The DNS request handler
272  * Phase 1 of resolution. Lookup local namestore.
273  *
274  * @param cls closure
275  * @param rh request handle to user for reply
276  * @param request_length number of bytes in request
277  * @param request udp payload of the DNS request
278  */
279 void
280 handle_dns_request(void *cls,
281                    struct GNUNET_DNS_RequestHandle *rh,
282                    size_t request_length,
283                    const char *request)
284 {
285   /**
286    * parse request for tld
287    */
288   struct GNUNET_DNSPARSER_Packet *p;
289   int namelen;
290   int i;
291   char *tail;
292
293   fprintf (stderr, "request hijacked!\n");
294   p = GNUNET_DNSPARSER_parse (request, request_length);
295   
296   if (NULL == p)
297   {
298     fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
299     GNUNET_DNS_request_forward (rh);
300     return;
301   }
302   
303   /**
304    * Check tld and decide if we or
305    * legacy dns is responsible
306    */
307   for (i=0;i<p->num_queries;i++)
308   {
309     namelen = strlen(p->queries[i].name);
310     
311     if (namelen < 7) /* this can't be .gnunet */
312       continue;
313     /**
314      * FIXME off by 1?
315      * Move our tld/root to config file
316      * Generate fake DNS reply that replaces .gnunet with .org for testing?
317      */
318     tail = p->queries[i].name+(namelen-7);
319     if (0 == strcmp(tail, ".gnunet"))
320     {
321       /* FIXME we need to answer to ALL queries in ONE response...
322        * Like this we only answer one...
323        */
324       lookup_namestore(p->queries[i].name, p->id, p->queries[i].type);
325     }
326     else
327     {
328       /**
329        * This request does not concern us. Forward to real DNS.
330        */
331       GNUNET_DNS_request_forward (rh);
332     }
333   }
334 }
335
336 /*TODO*/
337 static void
338 handle_client_record_lookup(void *cls,
339                             struct GNUNET_SERVER_Client *client,
340                             const struct GNUNET_MessageHeader *message)
341 {
342 }
343
344 /**
345  * test function
346  */
347 void
348 put_some_records(void)
349 {
350   /* put a few records into namestore */
351   char* ipA = "1.2.3.4";
352   char* ipB = "5.6.7.8";
353   GNUNET_NAMESTORE_record_put (namestore_handle,
354                                my_zone,
355                                "alice.gnunet",
356                                GNUNET_GNS_RECORD_TYPE_A,
357                                GNUNET_TIME_absolute_get_forever(),
358                                GNUNET_NAMESTORE_RF_AUTHORITY,
359                                NULL, //sig loc
360                                strlen (ipA),
361                                ipA,
362                                NULL,
363                                NULL);
364   GNUNET_NAMESTORE_record_put (namestore_handle,
365                                my_zone,
366                                "bob.gnunet",
367                                GNUNET_GNS_RECORD_TYPE_A,
368                                GNUNET_TIME_absolute_get_forever(),
369                                GNUNET_NAMESTORE_RF_AUTHORITY,
370                                NULL, //sig loc
371                                strlen (ipB),
372                                ipB,
373                                NULL,
374                                NULL);
375 }
376
377 /**
378  * Process GNS requests.
379  *
380  * @param cls closure
381  * @param server the initialized server
382  * @param c configuration to use
383  */
384 static void
385 run (void *cls, struct GNUNET_SERVER_Handle *server,
386      const struct GNUNET_CONFIGURATION_Handle *c)
387 {
388   /* The IPC message types */
389   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
390     /* callback, cls, type, size */
391     {&handle_client_record_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP,
392       0},
393     {NULL, NULL, 0, 0}
394   };
395   
396   nc = GNUNET_SERVER_notification_context_create (server, 1);
397
398   /* FIXME - do some config parsing 
399    *       - Maybe only hijack dns if option is set (HIJACK_DNS=1)
400    */
401
402   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
403                                 NULL);
404   /**
405    * Do gnunet dns init here
406    */
407   dns_handle = GNUNET_DNS_connect(c,
408                                   GNUNET_DNS_FLAG_PRE_RESOLUTION,
409                                   &handle_dns_request, /* rh */
410                                   NULL); /* Closure */
411   
412   dns_res_handle = GNUNET_DNS_connect(c,
413                                       GNUNET_DNS_FLAG_PRE_RESOLUTION,
414                                       &handle_dns_response, /* rh */
415                                       NULL); /* Closure */
416   /**
417    * handle to our local namestore
418    */
419   namestore_handle = GNUNET_NAMESTORE_connect(c);
420
421   if (NULL == namestore_handle)
422   {
423     //FIXME do error handling;
424   }
425
426   put_some_records();
427
428   GNUNET_SERVER_add_handlers (server, handlers);
429   /**
430    * Esp the lookup would require to keep track of the clients' context
431    * See dht.
432    * GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
433    **/
434 }
435
436
437 /**
438  * The main function for the GNS service.
439  *
440  * @param argc number of arguments from the command line
441  * @param argv command line arguments
442  * @return 0 ok, 1 on error
443  */
444 int
445 main (int argc, char *const *argv)
446 {
447   int ret;
448
449   ret =
450       (GNUNET_OK ==
451        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
452                            NULL)) ? 0 : 1;
453   return ret;
454 }
455
456 /* end of gnunet-service-gns.c */