2 This file is part of GNUnet.
3 (C) 2012 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.
21 * @file gnunet-dns2gns.c
22 * @brief DNS server that translates DNS requests to GNS
23 * @author Christian Grothoff
26 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_gns_service.h>
31 * Timeout for DNS requests.
33 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
36 * Data kept per request.
41 * Socket to use for sending the reply.
43 struct GNUNET_NETWORK_Handle *lsock;
46 * Destination address to use.
51 * Initially, this is the DNS request, it will then be
52 * converted to the DNS response.
54 struct GNUNET_DNSPARSER_Packet *packet;
57 * Our GNS request handle.
59 struct GNUNET_GNS_LookupRequest *lookup;
62 * Task run on timeout or shutdown to clean up without
65 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
68 * Number of bytes in 'addr'.
76 * Handle to GNS resolver.
78 struct GNUNET_GNS_Handle *gns;
81 * Listen socket for IPv4.
83 static struct GNUNET_NETWORK_Handle *listen_socket4;
86 * Listen socket for IPv6.
88 static struct GNUNET_NETWORK_Handle *listen_socket6;
91 * Task for IPv4 socket.
93 static GNUNET_SCHEDULER_TaskIdentifier t4;
96 * Task for IPv6 socket.
98 static GNUNET_SCHEDULER_TaskIdentifier t6;
102 * Task run on shutdown. Cleans up everything.
105 * @param tc scheduler context
108 do_shutdown (void *cls,
109 const struct GNUNET_SCHEDULER_TaskContext *tc)
111 if (GNUNET_SCHEDULER_NO_TASK != t4)
112 GNUNET_SCHEDULER_cancel (t4);
113 if (GNUNET_SCHEDULER_NO_TASK != t6)
114 GNUNET_SCHEDULER_cancel (t6);
115 if (NULL != listen_socket4)
117 GNUNET_NETWORK_socket_close (listen_socket4);
118 listen_socket4 = NULL;
120 if (NULL != listen_socket6)
122 GNUNET_NETWORK_socket_close (listen_socket6);
123 listen_socket6 = NULL;
125 GNUNET_GNS_disconnect (gns);
131 * Send the response for the given request and clean up.
133 * @param request context for the request.
136 send_response (struct Request *request)
142 GNUNET_DNSPARSER_pack (request->packet,
143 UINT16_MAX /* is this not too much? */,
147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
148 _("Failed to pack DNS response into UDP packet!\n"));
153 GNUNET_NETWORK_socket_sendto (request->lsock,
157 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
160 GNUNET_SCHEDULER_cancel (request->timeout_task);
161 GNUNET_DNSPARSER_free_packet (request->packet);
162 GNUNET_free (request);
167 * Task run on timeout. Cleans up request.
169 * @param cls 'struct Request' of the request to clean up
170 * @param tc scheduler context
173 do_timeout (void *cls,
174 const struct GNUNET_SCHEDULER_TaskContext *tc)
176 struct Request *request = cls;
178 if (NULL != request->packet)
179 GNUNET_DNSPARSER_free_packet (request->packet);
180 if (NULL != request->lookup)
181 GNUNET_GNS_cancel_lookup_request (request->lookup);
182 GNUNET_free (request);
187 * Iterator called on obtained result for a GNS
191 * @param rd_count number of records
192 * @param rd the records in reply
195 result_processor (void *cls,
197 const struct GNUNET_NAMESTORE_RecordData *rd)
199 struct Request *request = cls;
200 struct GNUNET_DNSPARSER_Packet *packet;
202 struct GNUNET_DNSPARSER_Record rec;
204 request->lookup = NULL;
205 packet = request->packet;
206 packet->flags.query_or_response = 1;
207 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
208 packet->flags.checking_disabled = 0;
209 packet->flags.authenticated_data = 1;
210 packet->flags.zero = 0;
211 packet->flags.recursion_available = 1;
212 packet->flags.message_truncated = 0;
213 packet->flags.authoritative_answer = 0;
214 packet->flags.opcode = GNUNET_DNSPARSER_OPCODE_STATUS; // ???
215 for (i=0;i<rd_count;i++)
217 switch (rd[i].record_type)
219 case GNUNET_DNSPARSER_TYPE_A:
220 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
221 rec.name = GNUNET_strdup (packet->queries[0].name);
222 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
223 rec.type = GNUNET_DNSPARSER_TYPE_A;
224 rec.data.raw.data = GNUNET_malloc (sizeof (struct in_addr));
225 memcpy (rec.data.raw.data,
228 rec.data.raw.data_len = sizeof (struct in_addr);
229 GNUNET_array_append (packet->answers,
233 case GNUNET_DNSPARSER_TYPE_AAAA:
234 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
235 rec.name = GNUNET_strdup (packet->queries[0].name);
236 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
237 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
238 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
239 memcpy (rec.data.raw.data,
242 rec.data.raw.data_len = sizeof (struct in6_addr);
243 GNUNET_array_append (packet->answers,
247 case GNUNET_DNSPARSER_TYPE_CNAME:
248 rec.name = GNUNET_strdup (packet->queries[0].name);
249 rec.data.hostname = strdup (rd[i].data);
250 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
251 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
252 memcpy (rec.data.hostname,
255 GNUNET_array_append (packet->answers,
264 send_response (request);
269 * Handle DNS request.
271 * @param lsock socket to use for sending the reply
272 * @param addr address to use for sending the reply
273 * @param addr_len number of bytes in addr
274 * @param udp_msg DNS request payload
275 * @param udp_msg_size number of bytes in udp_msg
278 handle_request (struct GNUNET_NETWORK_Handle *lsock,
284 struct Request *request;
285 struct GNUNET_DNSPARSER_Packet *packet;
288 enum GNUNET_GNS_RecordType type;
291 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 _("Received malformed DNS request from %s\n"),
296 GNUNET_a2s (addr, addr_len));
299 if ( (0 != packet->flags.query_or_response) ||
300 (0 != packet->num_answers) ||
301 (0 != packet->num_authority_records) ||
302 (0 != packet->num_additional_records) )
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 _("Received malformed DNS request from %s\n"),
306 GNUNET_a2s (addr, addr_len));
307 GNUNET_DNSPARSER_free_packet (packet);
310 if ( (1 != packet->num_queries) )
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313 _("Received unsupported DNS request from %s\n"),
314 GNUNET_a2s (addr, addr_len));
315 GNUNET_DNSPARSER_free_packet (packet);
318 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
319 request->lsock = lsock;
320 request->packet = packet;
321 request->addr = &request[1];
322 request->addr_len = addr_len;
323 memcpy (&request[1], addr, addr_len);
324 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
327 name = GNUNET_strdup (packet->queries[0].name);
328 name_len = strlen (name);
330 if ( (name_len > strlen (".zkey.eu")) &&
331 (0 == strcasecmp (".zkey.eu",
332 &name[name_len - strlen (".zkey.eu")])) )
334 if (0 == strcasecmp ("fcfs.zkey.eu",
335 &name[name_len - strlen ("fcfs.zkey.eu")]))
337 name[name_len - strlen ("zkey.eu")] = '\0';
338 strcat (name, ".gnunet");
341 name[name_len - strlen (".eu")] = '\0';
342 name_len = strlen (name);
344 if ( (name_len > strlen (".gnunet")) &&
345 (0 == strcasecmp (".gnunet",
346 &name[name_len - strlen (".gnunet")])) )
347 use_gns = GNUNET_YES;
349 if ( (name_len > strlen (".zkey")) &&
350 (0 == strcasecmp (".zkey",
351 &name[name_len - strlen (".zkey")])) )
352 use_gns = GNUNET_YES;
354 if (GNUNET_YES == use_gns)
356 type = packet->queries[0].type;
357 request->lookup = GNUNET_GNS_lookup (gns,
367 /* FIXME: do traditional *DNS* lookup; note that
368 gnunet-service-dns already has code to do this;
369 factor into library to share! Why not use GNUNET_RESOLVER here?*/
377 * Task to read IPv4 DNS packets.
379 * @param cls the 'listen_socket4'
380 * @param tc scheduler context
383 read_dns4 (void *cls,
384 const struct GNUNET_SCHEDULER_TaskContext *tc)
386 struct sockaddr_in v4;
390 GNUNET_assert (listen_socket4 == cls);
391 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
395 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
396 return; /* shutdown? */
397 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
401 return; /* read error!? */
406 addrlen = sizeof (v4);
407 GNUNET_break (size ==
408 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
411 (struct sockaddr *) &v4,
413 handle_request (listen_socket4, &v4, addrlen,
420 * Task to read IPv6 DNS packets.
422 * @param cls the 'listen_socket6'
423 * @param tc scheduler context
426 read_dns6 (void *cls,
427 const struct GNUNET_SCHEDULER_TaskContext *tc)
429 struct sockaddr_in6 v6;
433 GNUNET_assert (listen_socket6 == cls);
434 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
438 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
439 return; /* shutdown? */
440 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
444 return; /* read error!? */
449 addrlen = sizeof (v6);
450 GNUNET_break (size ==
451 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
454 (struct sockaddr *) &v6,
456 handle_request (listen_socket6, &v6, addrlen,
463 * Main function that will be run.
466 * @param args remaining command-line arguments
467 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
468 * @param cfg configuration
471 run (void *cls, char *const *args, const char *cfgfile,
472 const struct GNUNET_CONFIGURATION_Handle *cfg)
474 gns = GNUNET_GNS_connect (cfg);
477 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
480 if (NULL != listen_socket4)
482 struct sockaddr_in v4;
484 memset (&v4, 0, sizeof (v4));
485 v4.sin_family = AF_INET;
486 #if HAVE_SOCKADDR_IN_SIN_LEN
487 v4.sin_len = sizeof (v4);
489 v4.sin_port = htons (53);
491 GNUNET_NETWORK_socket_bind (listen_socket4,
492 (struct sockaddr *) &v4,
495 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
496 GNUNET_NETWORK_socket_close (listen_socket4);
497 listen_socket4 = NULL;
500 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
503 if (NULL != listen_socket6)
505 struct sockaddr_in6 v6;
507 memset (&v6, 0, sizeof (v6));
508 v6.sin6_family = AF_INET6;
509 #if HAVE_SOCKADDR_IN_SIN_LEN
510 v6.sin6_len = sizeof (v6);
512 v6.sin6_port = htons (53);
514 GNUNET_NETWORK_socket_bind (listen_socket6,
515 (struct sockaddr *) &v6,
518 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
519 GNUNET_NETWORK_socket_close (listen_socket6);
520 listen_socket6 = NULL;
523 if ( (NULL == listen_socket4) &&
524 (NULL == listen_socket6) )
526 if (NULL != listen_socket4)
527 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
531 if (NULL != listen_socket6)
532 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
537 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
543 * The main function for the fcfs daemon.
545 * @param argc number of arguments from the command line
546 * @param argv command line arguments
547 * @return 0 ok, 1 on error
553 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
554 GNUNET_GETOPT_OPTION_END
558 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
561 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
564 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
565 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
567 &run, NULL)) ? 0 : 1;
572 /* end of gnunet-dns2gns.c */