2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file gns/gnunet-service-gns.c
23 * @brief GNUnet GNS service
24 * @author Martin Schanzenbach
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"
36 /* TODO into gnunet_protocols */
37 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
38 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
40 struct GNUNET_GNS_QueryRecordList
45 struct GNUNET_GNS_QueryRecordList * next;
46 struct GNUNET_GNS_QueryRecordList * prev;
48 struct GNUNET_DNSPARSER_Record * record;
52 * A result list for namestore queries
54 struct GNUNET_GNS_PendingQuery
56 /* the answer packet */
57 struct GNUNET_DNSPARSER_Packet *answer;
59 /* records to put into answer packet */
60 struct GNUNET_GNS_QueryRecordList * records_head;
61 struct GNUNET_GNS_QueryRecordList * records_tail;
64 int num_authority_records; //FIXME are all of our replies auth?
66 /* the dns request id */
67 int id; // FIXME can handle->request_id also be used here?
69 /* the request handle to reply to */
70 struct GNUNET_DNS_RequestHandle *request_handle;
72 /* hast this query been answered? */
78 * Our handle to the DNS handler library
80 struct GNUNET_DNS_Handle *dns_handle;
81 struct GNUNET_DNS_Handle *dns_res_handle;
84 * Our handle to the namestore service
86 struct GNUNET_NAMESTORE_Handle *namestore_handle;
89 * The configuration the GNS service is running with
91 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
94 * Our notification context.
96 static struct GNUNET_SERVER_NotificationContext *nc;
101 const GNUNET_HashCode *my_zone;
104 * Task run during shutdown.
110 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 GNUNET_DNS_disconnect(dns_handle);
113 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
117 * Phase 2 of resolution.
125 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
127 struct GNUNET_GNS_QueryRecordList *i;
128 struct GNUNET_DNSPARSER_Packet *packet;
129 struct GNUNET_DNSPARSER_Flags dnsflags;
135 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
137 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
139 len = sizeof(struct GNUNET_DNSPARSER_Record*);
141 for (i=answer->records_head; i != NULL; i=i->next)
143 memcpy(&packet->answers[j],
145 sizeof (struct GNUNET_DNSPARSER_Record));
146 GNUNET_free(i->record);
150 /* FIXME how to handle auth, additional etc */
151 packet->num_answers = answer->num_records;
152 packet->num_authority_records = answer->num_authority_records;
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;
159 packet->id = answer->id;
160 ret = GNUNET_DNSPARSER_pack (packet,
161 1024, /* FIXME magic from dns redirector */
164 if (ret == GNUNET_OK)
166 GNUNET_DNS_request_answer(answer->request_handle,
170 //FIXME return code, free datastructures
174 fprintf(stderr, "Error building DNS response! (ret=%d)", ret);
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)
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;
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
211 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
212 record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
213 qrecord->record = record;
215 record->name = (char*)name;
216 /* FIXME for gns records this requires the dnsparser to be modified!
219 //record->data = data; FIXME!
220 record->expiration_time = expiration;
221 record->type = record_type;
222 record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
224 if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
226 query->num_authority_records++;
229 query->num_records++;
231 //FIXME watch for leaks
232 GNUNET_CONTAINER_DLL_insert(query->records_head,
240 handle_dns_response(void *cls,
241 struct GNUNET_DNS_RequestHandle *rh,
242 size_t request_length,
245 fprintf (stderr, "This is a response!\n");
246 GNUNET_DNS_request_forward (rh);
250 lookup_namestore(char* name, uint16_t id, uint16_t type)
252 struct GNUNET_GNS_PendingQuery *answer;
255 * Do db lookup here. Make dht lookup if necessary
256 * FIXME for now only local lookups for our zone!
258 fprintf (stderr, "This is .gnunet (%s)!\n", name);
259 answer = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
262 GNUNET_NAMESTORE_lookup_name(namestore_handle,
271 * The DNS request handler
272 * Phase 1 of resolution. Lookup local namestore.
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
280 handle_dns_request(void *cls,
281 struct GNUNET_DNS_RequestHandle *rh,
282 size_t request_length,
286 * parse request for tld
288 struct GNUNET_DNSPARSER_Packet *p;
293 fprintf (stderr, "request hijacked!\n");
294 p = GNUNET_DNSPARSER_parse (request, request_length);
298 fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n");
299 GNUNET_DNS_request_forward (rh);
304 * Check tld and decide if we or
305 * legacy dns is responsible
307 for (i=0;i<p->num_queries;i++)
309 namelen = strlen(p->queries[i].name);
311 if (namelen < 7) /* this can't be .gnunet */
315 * Move our tld/root to config file
316 * Generate fake DNS reply that replaces .gnunet with .org for testing?
318 tail = p->queries[i].name+(namelen-7);
319 if (0 == strcmp(tail, ".gnunet"))
321 /* FIXME we need to answer to ALL queries in ONE response...
322 * Like this we only answer one...
324 lookup_namestore(p->queries[i].name, p->id, p->queries[i].type);
329 * This request does not concern us. Forward to real DNS.
331 GNUNET_DNS_request_forward (rh);
338 handle_client_record_lookup(void *cls,
339 struct GNUNET_SERVER_Client *client,
340 const struct GNUNET_MessageHeader *message)
348 put_some_records(void)
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,
356 GNUNET_GNS_RECORD_TYPE_A,
357 GNUNET_TIME_absolute_get_forever(),
358 GNUNET_NAMESTORE_RF_AUTHORITY,
364 GNUNET_NAMESTORE_record_put (namestore_handle,
367 GNUNET_GNS_RECORD_TYPE_A,
368 GNUNET_TIME_absolute_get_forever(),
369 GNUNET_NAMESTORE_RF_AUTHORITY,
378 * Process GNS requests.
381 * @param server the initialized server
382 * @param c configuration to use
385 run (void *cls, struct GNUNET_SERVER_Handle *server,
386 const struct GNUNET_CONFIGURATION_Handle *c)
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,
396 nc = GNUNET_SERVER_notification_context_create (server, 1);
398 /* FIXME - do some config parsing
399 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
402 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
405 * Do gnunet dns init here
407 dns_handle = GNUNET_DNS_connect(c,
408 GNUNET_DNS_FLAG_PRE_RESOLUTION,
409 &handle_dns_request, /* rh */
412 dns_res_handle = GNUNET_DNS_connect(c,
413 GNUNET_DNS_FLAG_PRE_RESOLUTION,
414 &handle_dns_response, /* rh */
417 * handle to our local namestore
419 namestore_handle = GNUNET_NAMESTORE_connect(c);
421 if (NULL == namestore_handle)
423 //FIXME do error handling;
428 GNUNET_SERVER_add_handlers (server, handlers);
430 * Esp the lookup would require to keep track of the clients' context
432 * GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
438 * The main function for the GNS service.
440 * @param argc number of arguments from the command line
441 * @param argv command line arguments
442 * @return 0 ok, 1 on error
445 main (int argc, char *const *argv)
451 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
456 /* end of gnunet-service-gns.c */