aboutsummaryrefslogtreecommitdiff
path: root/src/or/eventdns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/or/eventdns.c')
-rw-r--r--src/or/eventdns.c143
1 files changed, 54 insertions, 89 deletions
diff --git a/src/or/eventdns.c b/src/or/eventdns.c
index a889e803ed..b929303fd5 100644
--- a/src/or/eventdns.c
+++ b/src/or/eventdns.c
@@ -31,6 +31,7 @@
*/
#include "eventdns_tor.h"
+#include "../common/util.h"
#include <sys/types.h>
/* #define NDEBUG */
@@ -89,6 +90,7 @@
#include <stdarg.h>
#include "eventdns.h"
+
#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
@@ -173,8 +175,6 @@ struct evdns_request {
/* these objects are kept in a circular list */
struct evdns_request *next, *prev;
- u16 timeout_event_deleted; /**< Debugging: where was timeout_event
- * deleted? 0 for "it's added." */
struct event timeout_event;
u16 trans_id; /* the transaction id */
@@ -214,8 +214,6 @@ struct nameserver {
struct event event;
/* these objects are kept in a circular list */
struct nameserver *next, *prev;
- u16 timeout_event_deleted; /**< Debugging: where was timeout_event
- * deleted? 0 for "it's added." */
struct event timeout_event; /* used to keep the timeout for */
/* when we next probe this server. */
/* Valid if state == 0 */
@@ -474,51 +472,10 @@ sockaddr_eq(const struct sockaddr *sa1, const struct sockaddr *sa2,
return 1;
}
-/* for debugging bug 929. XXXX022 */
-static int
-_add_timeout_event(u16 *lineno, struct event *ev, struct timeval *to)
-{
- *lineno = 0;
- return evtimer_add(ev, to);
-}
-#define add_timeout_event(s, to) \
- (_add_timeout_event(&(s)->timeout_event_deleted, &(s)->timeout_event, (to)))
-
-/* for debugging bug 929. XXXX022 */
-static int
-_del_timeout_event(u16 *lineno, struct event *ev, int line)
-{
- if (*lineno) {
- log(EVDNS_LOG_DEBUG,
- "Duplicate timeout event_del from line %d: first call "
- "was at %d.", line, (int)*lineno);
- return 0;
- } else {
- *lineno = (u16)line;
- return event_del(ev);
- }
-}
-#define del_timeout_event(s) \
- (_del_timeout_event(&(s)->timeout_event_deleted, &(s)->timeout_event, \
- __LINE__))
-/* For debugging bug 929/957. XXXX022 */
-static int
-_del_timeout_event_if_set(u16 *lineno, struct event *ev, int line)
-{
- if (*lineno == 0) {
- log(EVDNS_LOG_DEBUG,
- "Event that I thought was non-added as of line %d "
- "was actually added on line %d",
- line, (int)*lineno);
- *lineno = line;
- return event_del(ev);
- }
- return 0;
-}
-#define del_timeout_event_if_set(s) \
- _del_timeout_event_if_set(&(s)->timeout_event_deleted, \
- &(s)->timeout_event, \
- __LINE__)
+#define add_timeout_event(s, to) \
+ (event_add(&(s)->timeout_event, (to)))
+#define del_timeout_event(s) \
+ (event_del(&(s)->timeout_event))
/* This walks the list of inflight requests to find the */
/* one with a matching transaction id. Returns NULL on */
@@ -555,7 +512,7 @@ static void
nameserver_probe_failed(struct nameserver *const ns) {
const struct timeval * timeout;
del_timeout_event(ns);
- CLEAR(&ns->timeout_event);
+
if (ns->state == 1) {
/* This can happen if the nameserver acts in a way which makes us mark */
/* it as bad and then starts sending good replies. */
@@ -567,8 +524,6 @@ nameserver_probe_failed(struct nameserver *const ns) {
global_nameserver_timeouts_length - 1)];
ns->failed_times++;
- del_timeout_event_if_set(ns);
- evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
if (add_timeout_event(ns, (struct timeval *) timeout) < 0) {
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
@@ -597,8 +552,6 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
ns->state = 0;
ns->failed_times = 1;
- del_timeout_event_if_set(ns);
- evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
if (add_timeout_event(ns, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
@@ -634,7 +587,6 @@ nameserver_up(struct nameserver *const ns) {
log(EVDNS_LOG_WARN, "Nameserver %s is back up",
debug_ntop((struct sockaddr *)&ns->address));
del_timeout_event(ns);
- CLEAR(&ns->timeout_event);
ns->state = 1;
ns->failed_times = 0;
ns->timedout = 0;
@@ -666,7 +618,6 @@ request_finished(struct evdns_request *const req, struct evdns_request **head) {
log(EVDNS_LOG_DEBUG, "Removing timeout for request %lx",
(unsigned long) req);
del_timeout_event(req);
- CLEAR(&req->timeout_event);
search_request_finished(req);
global_requests_inflight--;
@@ -1342,8 +1293,8 @@ server_port_read(struct evdns_server_port *s) {
static void
server_port_flush(struct evdns_server_port *port)
{
- while (port->pending_replies) {
- struct server_request *req = port->pending_replies;
+ struct server_request *req = port->pending_replies;
+ while (req) {
ssize_t r = sendto(port->socket, req->response, req->response_len, 0,
(struct sockaddr*) &req->addr, (socklen_t)req->addrlen);
if (r < 0) {
@@ -1355,6 +1306,9 @@ server_port_flush(struct evdns_server_port *port)
if (server_request_free(req)) {
/* we released the last reference to req->port. */
return;
+ } else {
+ assert(port->pending_replies != req);
+ req = port->pending_replies;
}
}
@@ -2046,7 +2000,6 @@ evdns_request_timeout_callback(int fd, short events, void *arg) {
* request_finished; that one already deletes the timeout event.
* XXXX021 port this change to libevent. */
del_timeout_event(req);
- CLEAR(&req->timeout_event);
evdns_request_transmit(req);
}
}
@@ -2109,8 +2062,7 @@ evdns_request_transmit(struct evdns_request *req) {
/* transmitted; we need to check for timeout. */
log(EVDNS_LOG_DEBUG,
"Setting timeout for request %lx", (unsigned long) req);
- del_timeout_event_if_set(req);
- evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req);
+
if (add_timeout_event(req, &global_timeout) < 0) {
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer for request %lx",
@@ -2225,7 +2177,6 @@ evdns_clear_nameservers_and_suspend(void)
(void) event_del(&server->event);
CLEAR(&server->event);
del_timeout_event(server);
- CLEAR(&server->timeout_event);
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
CLEAR(server);
@@ -2243,7 +2194,6 @@ evdns_clear_nameservers_and_suspend(void)
req->ns = NULL;
/* ???? What to do about searches? */
del_timeout_event(req);
- CLEAR(&req->timeout_event);
req->trans_id = 0;
req->transmit_me = 0;
@@ -2292,6 +2242,21 @@ evdns_resume(void)
}
static int
+sockaddr_is_loopback(const struct sockaddr *addr)
+{
+ static const char LOOPBACK_S6[16] =
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1";
+ if (addr->sa_family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in *)addr;
+ return (ntohl(sin->sin_addr.s_addr) & 0xff000000) == 0x7f000000;
+ } else if (addr->sa_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
+ return !memcmp(sin6->sin6_addr.s6_addr, LOOPBACK_S6, 16);
+ }
+ return 0;
+}
+
+static int
_evdns_nameserver_add_impl(const struct sockaddr *address,
socklen_t addrlen) {
/* first check to see if we already have this nameserver */
@@ -2318,7 +2283,8 @@ _evdns_nameserver_add_impl(const struct sockaddr *address,
if (!ns) return -1;
memset(ns, 0, sizeof(struct nameserver));
- ns->timeout_event_deleted = __LINE__;
+
+ evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
if (ns->socket < 0) { err = 1; goto out1; }
@@ -2331,7 +2297,8 @@ _evdns_nameserver_add_impl(const struct sockaddr *address,
fcntl(ns->socket, F_SETFL, O_NONBLOCK);
#endif
- if (global_bind_addr_is_set) {
+ if (global_bind_addr_is_set &&
+ !sockaddr_is_loopback((struct sockaddr*)&global_bind_address)) {
if (bind(ns->socket, (struct sockaddr *)&global_bind_address,
global_bind_addrlen) < 0) {
log(EVDNS_LOG_DEBUG, "Couldn't bind to outgoing address.");
@@ -2553,7 +2520,8 @@ request_new(int type, const char *name, int flags,
}
memset(req, 0, sizeof(struct evdns_request));
- req->timeout_event_deleted = __LINE__;
+
+ evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req);
if (global_randomize_case) {
unsigned i;
@@ -2941,14 +2909,6 @@ evdns_resolv_set_defaults(int flags) {
if (flags & DNS_OPTION_NAMESERVERS) evdns_nameserver_ip_add("127.0.0.1");
}
-#ifndef HAVE_STRTOK_R
-static char *
-strtok_r(char *s, const char *delim, char **state) {
- (void)state;
- return strtok(s, delim);
-}
-#endif
-
/* helper version of atoi which returns -1 on error */
static int
strtoint(const char *const str) {
@@ -3025,9 +2985,9 @@ static void
resolv_conf_parse_line(char *const start, int flags) {
char *strtok_state;
static const char *const delims = " \t";
-#define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
+#define NEXT_TOKEN tor_strtok_r(NULL, delims, &strtok_state)
- char *const first_token = strtok_r(start, delims, &strtok_state);
+ char *const first_token = tor_strtok_r(start, delims, &strtok_state);
if (!first_token) return;
if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
@@ -3171,14 +3131,13 @@ load_nameservers_with_getnetworkparams(void)
IP_ADDR_STRING *ns;
GetNetworkParams_fn_t fn;
- /* XXXX Possibly, we should hardcode the location of this DLL. */
- if (!(handle = LoadLibrary("iphlpapi.dll"))) {
+ if (!(handle = load_windows_system_library(TEXT("iphlpapi.dll")))) {
log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
/* right now status = 0, doesn't that mean "good" - mikec */
status = -1;
goto done;
}
- if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
+ if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, TEXT("GetNetworkParams")))) {
log(EVDNS_LOG_WARN, "Could not get address of function.");
/* same as above */
status = -1;
@@ -3241,9 +3200,10 @@ load_nameservers_with_getnetworkparams(void)
}
static int
-config_nameserver_from_reg_key(HKEY key, const char *subkey)
+config_nameserver_from_reg_key(HKEY key, const TCHAR *subkey)
{
char *buf;
+ char ansibuf[MAX_PATH] = {0};
DWORD bufsz = 0, type = 0;
int status = 0;
@@ -3255,24 +3215,30 @@ config_nameserver_from_reg_key(HKEY key, const char *subkey)
if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
== ERROR_SUCCESS && bufsz > 1) {
- status = evdns_nameserver_ip_add_line(buf);
+ wcstombs(ansibuf,(wchar_t*)buf,MAX_PATH);/*XXXX UNICODE */
+ status = evdns_nameserver_ip_add_line(ansibuf);
}
mm_free(buf);
return status;
}
-#define SERVICES_KEY "System\\CurrentControlSet\\Services\\"
-#define WIN_NS_9X_KEY SERVICES_KEY "VxD\\MSTCP"
-#define WIN_NS_NT_KEY SERVICES_KEY "Tcpip\\Parameters"
+#define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
+#define WIN_NS_9X_KEY SERVICES_KEY TEXT("VxD\\MSTCP")
+#define WIN_NS_NT_KEY SERVICES_KEY TEXT("Tcpip\\Parameters")
static int
load_nameservers_from_registry(void)
{
int found = 0;
int r;
+ OSVERSIONINFO info;
+ memset(&info, 0, sizeof(info));
+ info.dwOSVersionInfoSize = sizeof (info);
+ GetVersionEx(&info);
+
#define TRY(k, name) \
- if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
+ if (!found && config_nameserver_from_reg_key(k,TEXT(name)) == 0) { \
log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
found = 1; \
} else if (!found) { \
@@ -3280,7 +3246,7 @@ load_nameservers_from_registry(void)
#k,#name); \
}
- if (((int)GetVersion()) > 0) { /* NT */
+ if (info.dwMajorVersion >= 5) { /* NT */
HKEY nt_key = 0, interfaces_key = 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
@@ -3288,7 +3254,7 @@ load_nameservers_from_registry(void)
log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
return -1;
}
- r = RegOpenKeyEx(nt_key, "Interfaces", 0,
+ r = RegOpenKeyEx(nt_key, TEXT("Interfaces"), 0,
KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
&interfaces_key);
if (r != ERROR_SUCCESS) {
@@ -3384,8 +3350,7 @@ evdns_shutdown(int fail_requests)
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
(void) event_del(&server->event);
- if (server->state == 0)
- del_timeout_event(server);
+ del_timeout_event(server);
CLEAR(server);
mm_free(server);
if (server_next == server_head)