2017-03-15 21:13:17 +01:00
|
|
|
/* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2007-05-24 22:31:30 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2016-02-27 18:08:24 +01:00
|
|
|
* \file dnsserv.c
|
2016-10-24 17:47:40 +02:00
|
|
|
* \brief Implements client-side DNS proxy server code.
|
|
|
|
*
|
|
|
|
* When a user enables the DNSPort configuration option to have their local
|
|
|
|
* Tor client handle DNS requests, this module handles it. It functions as a
|
|
|
|
* "DNS Server" on the client side, which client applications use.
|
|
|
|
*
|
|
|
|
* Inbound DNS requests are represented as entry_connection_t here (since
|
|
|
|
* that's how Tor represents client-side streams), which are kept associated
|
|
|
|
* with an evdns_server_request structure as exposed by Libevent's
|
|
|
|
* evdns code.
|
|
|
|
*
|
|
|
|
* Upon receiving a DNS request, libevent calls our evdns_server_callback()
|
|
|
|
* function here, which causes this module to create an entry_connection_t
|
|
|
|
* request as appropriate. Later, when that request is answered,
|
|
|
|
* connection_edge.c calls dnsserv_resolved() so we can finish up and tell the
|
|
|
|
* DNS client.
|
2007-05-24 22:31:30 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2010-07-21 12:26:18 +02:00
|
|
|
#include "dnsserv.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2009-06-04 07:05:23 +02:00
|
|
|
#include <event2/dns.h>
|
|
|
|
#include <event2/dns_compat.h>
|
2012-06-15 15:37:40 +02:00
|
|
|
/* XXXX this implies we want an improved evdns */
|
2009-06-04 07:05:23 +02:00
|
|
|
#include <event2/dns_struct.h>
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Helper function: called by evdns whenever the client sends a request to our
|
2007-05-29 19:31:13 +02:00
|
|
|
* DNSPort. We need to eventually answer the request <b>req</b>.
|
|
|
|
*/
|
2007-05-24 22:31:30 +02:00
|
|
|
static void
|
2011-07-06 22:03:47 +02:00
|
|
|
evdns_server_callback(struct evdns_server_request *req, void *data_)
|
2007-05-24 22:31:30 +02:00
|
|
|
{
|
2011-07-06 22:03:47 +02:00
|
|
|
const listener_connection_t *listener = data_;
|
2011-07-20 18:38:13 +02:00
|
|
|
entry_connection_t *entry_conn;
|
2007-05-24 22:31:30 +02:00
|
|
|
edge_connection_t *conn;
|
|
|
|
int i = 0;
|
2014-04-08 04:03:19 +02:00
|
|
|
struct evdns_server_question *q = NULL, *supported_q = NULL;
|
2007-05-24 22:31:30 +02:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
int addrlen;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t tor_addr;
|
|
|
|
uint16_t port;
|
2007-05-24 22:31:30 +02:00
|
|
|
int err = DNS_ERR_NONE;
|
2007-05-29 19:31:13 +02:00
|
|
|
char *q_name;
|
2007-05-24 22:31:30 +02:00
|
|
|
|
|
|
|
tor_assert(req);
|
2011-07-06 22:03:47 +02:00
|
|
|
|
2007-05-24 22:31:30 +02:00
|
|
|
log_info(LD_APP, "Got a new DNS request!");
|
|
|
|
|
2007-06-01 01:40:35 +02:00
|
|
|
req->flags |= 0x80; /* set RA */
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/* First, check whether the requesting address matches our SOCKSPolicy. */
|
2007-05-24 22:31:30 +02:00
|
|
|
if ((addrlen = evdns_server_request_get_requesting_addr(req,
|
2008-02-22 20:09:45 +01:00
|
|
|
(struct sockaddr*)&addr, (socklen_t)sizeof(addr))) < 0) {
|
2007-05-24 22:31:30 +02:00
|
|
|
log_warn(LD_APP, "Couldn't get requesting address.");
|
|
|
|
evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
|
|
|
|
return;
|
|
|
|
}
|
2007-06-02 14:44:54 +02:00
|
|
|
(void) addrlen;
|
2007-05-24 22:31:30 +02:00
|
|
|
sa = (struct sockaddr*) &addr;
|
2008-08-07 21:13:35 +02:00
|
|
|
if (tor_addr_from_sockaddr(&tor_addr, sa, &port)<0) {
|
2008-08-05 22:08:19 +02:00
|
|
|
log_warn(LD_APP, "Requesting address wasn't recognized.");
|
2007-05-24 22:31:30 +02:00
|
|
|
evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
|
|
|
|
return;
|
|
|
|
}
|
2007-06-17 17:59:27 +02:00
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
if (!socks_policy_permits_address(&tor_addr)) {
|
2007-05-24 22:31:30 +02:00
|
|
|
log_warn(LD_APP, "Rejecting DNS request from disallowed IP.");
|
|
|
|
evdns_server_request_respond(req, DNS_ERR_REFUSED);
|
|
|
|
return;
|
|
|
|
}
|
2007-05-29 19:31:13 +02:00
|
|
|
|
|
|
|
/* Now, let's find the first actual question of a type we can answer in this
|
|
|
|
* DNS request. It makes us a little noncompliant to act like this; we
|
|
|
|
* should fix that eventually if it turns out to make a difference for
|
|
|
|
* anybody. */
|
2007-05-24 22:31:30 +02:00
|
|
|
if (req->nquestions == 0) {
|
|
|
|
log_info(LD_APP, "No questions in DNS request; sending back nil reply.");
|
|
|
|
evdns_server_request_respond(req, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req->nquestions > 1) {
|
|
|
|
log_info(LD_APP, "Got a DNS request with more than one question; I only "
|
|
|
|
"handle one question at a time for now. Skipping the extras.");
|
|
|
|
}
|
|
|
|
for (i = 0; i < req->nquestions; ++i) {
|
2009-02-11 18:22:29 +01:00
|
|
|
if (req->questions[i]->dns_question_class != EVDNS_CLASS_INET)
|
2007-05-24 22:31:30 +02:00
|
|
|
continue;
|
|
|
|
switch (req->questions[i]->type) {
|
|
|
|
case EVDNS_TYPE_A:
|
2012-11-15 04:20:18 +01:00
|
|
|
case EVDNS_TYPE_AAAA:
|
2007-05-24 22:31:30 +02:00
|
|
|
case EVDNS_TYPE_PTR:
|
2014-04-08 04:03:19 +02:00
|
|
|
/* We always pick the first one of these questions, if there is
|
|
|
|
one. */
|
|
|
|
if (! supported_q)
|
2016-05-04 20:45:09 +02:00
|
|
|
supported_q = req->questions[i];
|
2014-04-08 04:03:19 +02:00
|
|
|
break;
|
2007-05-24 22:31:30 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 04:03:19 +02:00
|
|
|
if (supported_q)
|
|
|
|
q = supported_q;
|
2007-05-24 22:31:30 +02:00
|
|
|
if (!q) {
|
|
|
|
log_info(LD_APP, "None of the questions we got were ones we're willing "
|
2011-06-12 11:57:31 +02:00
|
|
|
"to support. Sending NOTIMPL.");
|
|
|
|
evdns_server_request_respond(req, DNS_ERR_NOTIMPL);
|
2007-05-24 22:31:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-05-29 19:31:13 +02:00
|
|
|
|
|
|
|
/* Make sure the name isn't too long: This should be impossible, I think. */
|
2007-05-24 22:31:30 +02:00
|
|
|
if (err == DNS_ERR_NONE && strlen(q->name) > MAX_SOCKS_ADDR_LEN-1)
|
|
|
|
err = DNS_ERR_FORMAT;
|
|
|
|
|
2014-04-08 04:03:19 +02:00
|
|
|
if (err != DNS_ERR_NONE || !supported_q) {
|
|
|
|
/* We got an error? There's no question we're willing to answer? Then
|
|
|
|
* send back an answer immediately; we're done. */
|
2007-05-24 22:31:30 +02:00
|
|
|
evdns_server_request_respond(req, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/* Make a new dummy AP connection, and attach the request to it. */
|
2011-07-20 18:38:13 +02:00
|
|
|
entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
|
|
|
|
conn = ENTRY_TO_EDGE_CONN(entry_conn);
|
2015-11-27 18:54:57 +01:00
|
|
|
CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
|
2011-07-20 18:55:42 +02:00
|
|
|
TO_CONN(conn)->state = AP_CONN_STATE_RESOLVE_WAIT;
|
2007-07-10 19:14:51 +02:00
|
|
|
conn->is_dns_request = 1;
|
2007-06-17 17:59:27 +02:00
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_copy(&TO_CONN(conn)->addr, &tor_addr);
|
|
|
|
TO_CONN(conn)->port = port;
|
2016-03-28 22:36:51 +02:00
|
|
|
TO_CONN(conn)->address = tor_addr_to_str_dup(&tor_addr);
|
2007-06-17 17:59:27 +02:00
|
|
|
|
2014-04-08 04:03:19 +02:00
|
|
|
if (q->type == EVDNS_TYPE_A || q->type == EVDNS_TYPE_AAAA ||
|
|
|
|
q->type == EVDNS_QTYPE_ALL) {
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
|
2014-04-08 04:03:19 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(q->type == EVDNS_TYPE_PTR);
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
|
2014-04-08 04:03:19 +02:00
|
|
|
}
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2016-09-20 21:00:24 +02:00
|
|
|
/* This serves our DNS port so enable DNS request by default. */
|
|
|
|
entry_conn->entry_cfg.dns_request = 1;
|
2014-04-08 04:03:19 +02:00
|
|
|
if (q->type == EVDNS_TYPE_A || q->type == EVDNS_QTYPE_ALL) {
|
2015-01-03 22:11:23 +01:00
|
|
|
entry_conn->entry_cfg.ipv4_traffic = 1;
|
|
|
|
entry_conn->entry_cfg.ipv6_traffic = 0;
|
|
|
|
entry_conn->entry_cfg.prefer_ipv6 = 0;
|
2014-03-27 22:41:43 +01:00
|
|
|
} else if (q->type == EVDNS_TYPE_AAAA) {
|
2015-01-03 22:11:23 +01:00
|
|
|
entry_conn->entry_cfg.ipv4_traffic = 0;
|
|
|
|
entry_conn->entry_cfg.ipv6_traffic = 1;
|
|
|
|
entry_conn->entry_cfg.prefer_ipv6 = 1;
|
2014-03-27 22:41:43 +01:00
|
|
|
}
|
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
strlcpy(entry_conn->socks_request->address, q->name,
|
|
|
|
sizeof(entry_conn->socks_request->address));
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
entry_conn->socks_request->listener_type = listener->base_.type;
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->dns_server_request = req;
|
2015-01-03 22:11:23 +01:00
|
|
|
entry_conn->entry_cfg.isolation_flags = listener->entry_cfg.isolation_flags;
|
|
|
|
entry_conn->entry_cfg.session_group = listener->entry_cfg.session_group;
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->nym_epoch = get_signewnym_epoch();
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
if (connection_add(ENTRY_TO_CONN(entry_conn)) < 0) {
|
2008-01-14 20:00:23 +01:00
|
|
|
log_warn(LD_APP, "Couldn't register dummy connection for DNS request");
|
|
|
|
evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
|
2017-11-21 14:39:07 +01:00
|
|
|
connection_free_(ENTRY_TO_CONN(entry_conn));
|
2008-01-14 20:00:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-13 21:50:18 +02:00
|
|
|
|
2013-03-18 20:13:59 +01:00
|
|
|
control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
|
2007-07-05 16:50:05 +02:00
|
|
|
|
2010-08-06 21:29:15 +02:00
|
|
|
/* Now, unless a controller asked us to leave streams unattached,
|
|
|
|
* throw the connection over to get rewritten (which will
|
|
|
|
* answer it immediately if it's in the cache, or completely bogus, or
|
|
|
|
* automapped), and then attached to a circuit. */
|
2007-05-29 19:31:13 +02:00
|
|
|
log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
|
2009-12-15 23:23:36 +01:00
|
|
|
escaped_safe_str_client(q->name));
|
2007-05-29 19:31:13 +02:00
|
|
|
q_name = tor_strdup(q->name); /* q could be freed in rewrite_and_attach */
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
|
2007-05-29 19:31:13 +02:00
|
|
|
/* Now, the connection is marked if it was bad. */
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2010-08-06 21:29:15 +02:00
|
|
|
log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
|
2009-12-15 23:23:36 +01:00
|
|
|
escaped_safe_str_client(q_name));
|
2007-05-29 19:31:13 +02:00
|
|
|
tor_free(q_name);
|
2007-05-24 22:31:30 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Helper function: called whenever the client sends a resolve request to our
|
2007-07-10 19:13:24 +02:00
|
|
|
* controller. We need to eventually answer the request <b>req</b>.
|
2008-02-05 22:39:40 +01:00
|
|
|
* Returns 0 if the controller will be getting (or has gotten) an event in
|
|
|
|
* response; -1 if we couldn't launch the request.
|
2007-07-10 19:13:24 +02:00
|
|
|
*/
|
2008-02-05 22:39:40 +01:00
|
|
|
int
|
2013-03-18 20:39:11 +01:00
|
|
|
dnsserv_launch_request(const char *name, int reverse,
|
|
|
|
control_connection_t *control_conn)
|
2007-07-10 19:13:24 +02:00
|
|
|
{
|
2011-07-20 18:38:13 +02:00
|
|
|
entry_connection_t *entry_conn;
|
2007-07-10 19:13:24 +02:00
|
|
|
edge_connection_t *conn;
|
|
|
|
char *q_name;
|
|
|
|
|
|
|
|
/* Make a new dummy AP connection, and attach the request to it. */
|
2011-07-20 18:38:13 +02:00
|
|
|
entry_conn = entry_connection_new(CONN_TYPE_AP, AF_INET);
|
|
|
|
conn = ENTRY_TO_EDGE_CONN(entry_conn);
|
2015-11-27 18:54:57 +01:00
|
|
|
CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
|
2012-10-12 18:22:13 +02:00
|
|
|
conn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
|
2007-07-10 19:13:24 +02:00
|
|
|
|
2013-03-18 20:13:59 +01:00
|
|
|
tor_addr_copy(&TO_CONN(conn)->addr, &control_conn->base_.addr);
|
2013-06-01 00:35:51 +02:00
|
|
|
#ifdef AF_UNIX
|
|
|
|
/*
|
2016-03-28 22:36:51 +02:00
|
|
|
* The control connection can be AF_UNIX and if so tor_addr_to_str_dup will
|
2013-06-01 00:35:51 +02:00
|
|
|
* unhelpfully say "<unknown address type>"; say "(Tor_internal)"
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
if (control_conn->base_.socket_family == AF_UNIX) {
|
|
|
|
TO_CONN(conn)->port = 0;
|
|
|
|
TO_CONN(conn)->address = tor_strdup("(Tor_internal)");
|
|
|
|
} else {
|
|
|
|
TO_CONN(conn)->port = control_conn->base_.port;
|
2016-03-28 22:36:51 +02:00
|
|
|
TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
|
2013-06-01 00:35:51 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(AF_UNIX)) */
|
2013-03-18 20:13:59 +01:00
|
|
|
TO_CONN(conn)->port = control_conn->base_.port;
|
2016-03-28 22:36:51 +02:00
|
|
|
TO_CONN(conn)->address = tor_addr_to_str_dup(&control_conn->base_.addr);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(AF_UNIX) */
|
2013-03-18 20:13:59 +01:00
|
|
|
|
2007-07-10 19:14:51 +02:00
|
|
|
if (reverse)
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
|
2007-07-10 19:13:24 +02:00
|
|
|
else
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->socks_request->command = SOCKS_COMMAND_RESOLVE;
|
2007-07-10 19:14:51 +02:00
|
|
|
|
|
|
|
conn->is_dns_request = 1;
|
2007-07-10 19:13:24 +02:00
|
|
|
|
2011-07-20 18:55:42 +02:00
|
|
|
strlcpy(entry_conn->socks_request->address, name,
|
|
|
|
sizeof(entry_conn->socks_request->address));
|
2011-07-08 22:00:19 +02:00
|
|
|
|
2011-09-07 20:13:57 +02:00
|
|
|
entry_conn->socks_request->listener_type = CONN_TYPE_CONTROL_LISTENER;
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->original_dest_address = tor_strdup(name);
|
2015-01-03 22:11:23 +01:00
|
|
|
entry_conn->entry_cfg.session_group = SESSION_GROUP_CONTROL_RESOLVE;
|
2011-07-20 18:55:42 +02:00
|
|
|
entry_conn->nym_epoch = get_signewnym_epoch();
|
2015-01-03 22:11:23 +01:00
|
|
|
entry_conn->entry_cfg.isolation_flags = ISO_DEFAULT;
|
2007-07-10 19:13:24 +02:00
|
|
|
|
2008-01-14 20:00:23 +01:00
|
|
|
if (connection_add(TO_CONN(conn))<0) {
|
|
|
|
log_warn(LD_APP, "Couldn't register dummy connection for RESOLVE request");
|
2017-11-21 14:39:07 +01:00
|
|
|
connection_free_(TO_CONN(conn));
|
2008-02-05 22:39:40 +01:00
|
|
|
return -1;
|
2008-01-14 20:00:23 +01:00
|
|
|
}
|
2007-07-10 19:13:24 +02:00
|
|
|
|
2013-03-18 20:13:59 +01:00
|
|
|
control_event_stream_status(entry_conn, STREAM_EVENT_NEW_RESOLVE, 0);
|
|
|
|
|
2010-08-06 21:29:15 +02:00
|
|
|
/* Now, unless a controller asked us to leave streams unattached,
|
|
|
|
* throw the connection over to get rewritten (which will
|
|
|
|
* answer it immediately if it's in the cache, or completely bogus, or
|
|
|
|
* automapped), and then attached to a circuit. */
|
2007-07-10 19:13:24 +02:00
|
|
|
log_info(LD_APP, "Passing request for %s to rewrite_and_attach.",
|
2009-12-15 23:23:36 +01:00
|
|
|
escaped_safe_str_client(name));
|
2007-07-10 19:13:24 +02:00
|
|
|
q_name = tor_strdup(name); /* q could be freed in rewrite_and_attach */
|
2011-07-20 18:55:42 +02:00
|
|
|
connection_ap_rewrite_and_attach_if_allowed(entry_conn, NULL, NULL);
|
2007-07-10 19:13:24 +02:00
|
|
|
/* Now, the connection is marked if it was bad. */
|
|
|
|
|
2010-08-06 21:29:15 +02:00
|
|
|
log_info(LD_APP, "Passed request for %s to rewrite_and_attach_if_allowed.",
|
2009-12-15 23:23:36 +01:00
|
|
|
escaped_safe_str_client(q_name));
|
2007-07-10 19:13:24 +02:00
|
|
|
tor_free(q_name);
|
2008-02-05 22:39:40 +01:00
|
|
|
return 0;
|
2007-07-10 19:13:24 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** If there is a pending request on <b>conn</b> that's waiting for an answer,
|
|
|
|
* send back an error and free the request. */
|
2007-05-24 22:31:30 +02:00
|
|
|
void
|
2011-07-20 18:55:42 +02:00
|
|
|
dnsserv_reject_request(entry_connection_t *conn)
|
2007-05-24 22:31:30 +02:00
|
|
|
{
|
2007-05-29 19:31:13 +02:00
|
|
|
if (conn->dns_server_request) {
|
|
|
|
evdns_server_request_respond(conn->dns_server_request,
|
|
|
|
DNS_ERR_SERVERFAILED);
|
|
|
|
conn->dns_server_request = NULL;
|
|
|
|
}
|
2007-05-24 22:31:30 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:26 +01:00
|
|
|
/** Look up the original name that corresponds to 'addr' in req. We use this
|
2016-12-10 01:13:11 +01:00
|
|
|
* to preserve case in order to facilitate clients using 0x20-hacks to avoid
|
2008-10-29 16:31:26 +01:00
|
|
|
* DNS poisoning. */
|
|
|
|
static const char *
|
|
|
|
evdns_get_orig_address(const struct evdns_server_request *req,
|
|
|
|
int rtype, const char *addr)
|
|
|
|
{
|
|
|
|
int i, type;
|
|
|
|
|
|
|
|
switch (rtype) {
|
|
|
|
case RESOLVED_TYPE_IPV4:
|
|
|
|
type = EVDNS_TYPE_A;
|
|
|
|
break;
|
|
|
|
case RESOLVED_TYPE_HOSTNAME:
|
|
|
|
type = EVDNS_TYPE_PTR;
|
|
|
|
break;
|
|
|
|
case RESOLVED_TYPE_IPV6:
|
|
|
|
type = EVDNS_TYPE_AAAA;
|
|
|
|
break;
|
2016-10-26 18:52:34 +02:00
|
|
|
case RESOLVED_TYPE_ERROR:
|
|
|
|
case RESOLVED_TYPE_ERROR_TRANSIENT:
|
|
|
|
/* Addr doesn't matter, since we're not sending it back in the reply.*/
|
|
|
|
return addr;
|
2008-10-29 16:31:26 +01:00
|
|
|
default:
|
|
|
|
tor_fragile_assert();
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < req->nquestions; ++i) {
|
|
|
|
const struct evdns_server_question *q = req->questions[i];
|
|
|
|
if (q->type == type && !strcasecmp(q->name, addr))
|
|
|
|
return q->name;
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Tell the dns request waiting for an answer on <b>conn</b> that we have an
|
|
|
|
* answer of type <b>answer_type</b> (RESOLVE_TYPE_IPV4/IPV6/ERR), of length
|
|
|
|
* <b>answer_len</b>, in <b>answer</b>, with TTL <b>ttl</b>. Doesn't do
|
|
|
|
* any caching; that's handled elsewhere. */
|
2007-05-24 22:31:30 +02:00
|
|
|
void
|
2011-07-20 18:55:42 +02:00
|
|
|
dnsserv_resolved(entry_connection_t *conn,
|
2007-05-24 22:31:30 +02:00
|
|
|
int answer_type,
|
|
|
|
size_t answer_len,
|
|
|
|
const char *answer,
|
|
|
|
int ttl)
|
|
|
|
{
|
|
|
|
struct evdns_server_request *req = conn->dns_server_request;
|
2008-10-29 16:31:26 +01:00
|
|
|
const char *name;
|
2007-05-24 22:31:30 +02:00
|
|
|
int err = DNS_ERR_NONE;
|
|
|
|
if (!req)
|
|
|
|
return;
|
2008-11-07 02:48:55 +01:00
|
|
|
name = evdns_get_orig_address(req, answer_type,
|
|
|
|
conn->socks_request->address);
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX Re-do; this is dumb. */
|
2007-05-24 22:31:30 +02:00
|
|
|
if (ttl < 60)
|
|
|
|
ttl = 60;
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/* The evdns interface is: add a bunch of reply items (corresponding to one
|
|
|
|
* or more of the questions in the request); then, call
|
|
|
|
* evdns_server_request_respond. */
|
2007-05-24 22:31:30 +02:00
|
|
|
if (answer_type == RESOLVED_TYPE_IPV6) {
|
2012-11-15 04:29:08 +01:00
|
|
|
evdns_server_request_add_aaaa_reply(req,
|
|
|
|
name,
|
|
|
|
1, answer, ttl);
|
2007-05-24 22:31:30 +02:00
|
|
|
} else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4 &&
|
|
|
|
conn->socks_request->command == SOCKS_COMMAND_RESOLVE) {
|
2007-07-10 19:41:26 +02:00
|
|
|
evdns_server_request_add_a_reply(req,
|
2008-10-29 16:31:26 +01:00
|
|
|
name,
|
2011-01-10 22:00:48 +01:00
|
|
|
1, answer, ttl);
|
2007-05-24 22:31:30 +02:00
|
|
|
} else if (answer_type == RESOLVED_TYPE_HOSTNAME &&
|
2011-01-15 19:09:12 +01:00
|
|
|
answer_len < 256 &&
|
2007-05-24 22:31:30 +02:00
|
|
|
conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
|
2007-07-10 19:41:26 +02:00
|
|
|
char *ans = tor_strndup(answer, answer_len);
|
|
|
|
evdns_server_request_add_ptr_reply(req, NULL,
|
2008-10-29 16:31:26 +01:00
|
|
|
name,
|
2011-01-10 22:18:32 +01:00
|
|
|
ans, ttl);
|
2007-07-10 19:41:26 +02:00
|
|
|
tor_free(ans);
|
2007-06-01 01:40:35 +02:00
|
|
|
} else if (answer_type == RESOLVED_TYPE_ERROR) {
|
2007-07-10 19:41:26 +02:00
|
|
|
err = DNS_ERR_NOTEXIST;
|
2007-06-01 01:40:35 +02:00
|
|
|
} else { /* answer_type == RESOLVED_TYPE_ERROR_TRANSIENT */
|
2007-07-10 19:41:26 +02:00
|
|
|
err = DNS_ERR_SERVERFAILED;
|
2007-05-24 22:31:30 +02:00
|
|
|
}
|
|
|
|
|
2007-07-10 19:41:26 +02:00
|
|
|
evdns_server_request_respond(req, err);
|
2007-07-10 19:14:51 +02:00
|
|
|
|
2007-05-24 22:31:30 +02:00
|
|
|
conn->dns_server_request = NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Set up the evdns server port for the UDP socket on <b>conn</b>, which
|
2007-05-29 19:31:13 +02:00
|
|
|
* must be an AP_DNS_LISTENER */
|
2007-05-24 22:31:30 +02:00
|
|
|
void
|
|
|
|
dnsserv_configure_listener(connection_t *conn)
|
|
|
|
{
|
2011-07-05 23:11:22 +02:00
|
|
|
listener_connection_t *listener_conn;
|
2007-05-24 22:31:30 +02:00
|
|
|
tor_assert(conn);
|
2011-05-23 06:17:48 +02:00
|
|
|
tor_assert(SOCKET_OK(conn->s));
|
2007-05-29 19:31:13 +02:00
|
|
|
tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
|
2007-05-24 22:31:30 +02:00
|
|
|
|
2011-07-05 23:11:22 +02:00
|
|
|
listener_conn = TO_LISTENER_CONN(conn);
|
|
|
|
listener_conn->dns_server_port =
|
2011-07-06 22:03:47 +02:00
|
|
|
tor_evdns_add_server_port(conn->s, 0, evdns_server_callback,
|
|
|
|
listener_conn);
|
2007-05-24 22:31:30 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Free the evdns server port for <b>conn</b>, which must be an
|
|
|
|
* AP_DNS_LISTENER. */
|
2007-05-24 22:31:30 +02:00
|
|
|
void
|
|
|
|
dnsserv_close_listener(connection_t *conn)
|
|
|
|
{
|
2011-07-05 23:11:22 +02:00
|
|
|
listener_connection_t *listener_conn;
|
2007-05-29 19:31:13 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
|
|
|
|
|
2011-07-05 23:11:22 +02:00
|
|
|
listener_conn = TO_LISTENER_CONN(conn);
|
|
|
|
|
|
|
|
if (listener_conn->dns_server_port) {
|
|
|
|
evdns_close_server_port(listener_conn->dns_server_port);
|
|
|
|
listener_conn->dns_server_port = NULL;
|
2007-05-29 19:31:13 +02:00
|
|
|
}
|
2007-05-24 22:31:30 +02:00
|
|
|
}
|
|
|
|
|