summaryrefslogtreecommitdiff
path: root/evdns.c
diff options
context:
space:
mode:
Diffstat (limited to 'evdns.c')
-rw-r--r--evdns.c155
1 files changed, 78 insertions, 77 deletions
diff --git a/evdns.c b/evdns.c
index 416137ba..41d8b6b2 100644
--- a/evdns.c
+++ b/evdns.c
@@ -48,8 +48,10 @@
* Version: 0.1b
*/
-#include <sys/types.h>
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include <sys/types.h>
#ifndef _FORTIFY_SOURCE
#define _FORTIFY_SOURCE 3
@@ -57,23 +59,23 @@
#include <string.h>
#include <fcntl.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_STDINT_H
+#ifdef EVENT__HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <limits.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdarg.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef _WIN32_IE
@@ -101,7 +103,7 @@
#include "ipv6-internal.h"
#include "util-internal.h"
#include "evthread-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <ctype.h>
#include <winsock2.h>
#include <windows.h>
@@ -113,7 +115,7 @@
#include <arpa/inet.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
@@ -251,7 +253,7 @@ struct evdns_server_port {
struct server_request *pending_replies;
struct event_base *event_base;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
void *lock;
#endif
};
@@ -353,7 +355,7 @@ struct evdns_base {
TAILQ_HEAD(hosts_list, hosts_entry) hostsdb;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
void *lock;
#endif
};
@@ -411,10 +413,10 @@ static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_request
static int strtoint(const char *const str);
-#ifdef _EVENT_DISABLE_THREAD_SUPPORT
-#define EVDNS_LOCK(base) _EVUTIL_NIL_STMT
-#define EVDNS_UNLOCK(base) _EVUTIL_NIL_STMT
-#define ASSERT_LOCKED(base) _EVUTIL_NIL_STMT
+#ifdef EVENT__DISABLE_THREAD_SUPPORT
+#define EVDNS_LOCK(base) EVUTIL_NIL_STMT_
+#define EVDNS_UNLOCK(base) EVUTIL_NIL_STMT_
+#define ASSERT_LOCKED(base) EVUTIL_NIL_STMT_
#else
#define EVDNS_LOCK(base) \
EVLOCK_LOCK((base)->lock, 0)
@@ -449,9 +451,9 @@ evdns_set_log_fn(evdns_debug_log_fn_type fn)
#define EVDNS_LOG_CHECK
#endif
-static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
+static void evdns_log_(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
static void
-_evdns_log(int warn, const char *fmt, ...)
+evdns_log_(int warn, const char *fmt, ...)
{
va_list args;
char buf[512];
@@ -470,7 +472,7 @@ _evdns_log(int warn, const char *fmt, ...)
}
-#define log _evdns_log
+#define log evdns_log_
/* This walks the list of inflight requests to find the */
/* one with a matching transaction id. Returns NULL on */
@@ -545,7 +547,7 @@ nameserver_probe_failed(struct nameserver *const ns) {
char addrbuf[128];
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
}
@@ -566,7 +568,7 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
if (!ns->state) return;
log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)),
msg);
@@ -584,7 +586,7 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
&base->global_nameserver_probe_initial_timeout) < 0) {
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
/* ???? Do more? */
@@ -620,7 +622,7 @@ nameserver_up(struct nameserver *const ns)
ASSERT_LOCKED(ns->base);
if (ns->state) return;
log(EVDNS_LOG_MSG, "Nameserver %s is back up",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
evtimer_del(&ns->timeout_event);
@@ -834,10 +836,10 @@ reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct repl
d->handle = req->handle;
}
- event_deferred_cb_init(&d->deferred, reply_run_callback,
+ event_deferred_cb_init_(&d->deferred, reply_run_callback,
req->user_pointer);
- event_deferred_cb_schedule(
- event_base_get_deferred_cb_queue(req->base->event_base),
+ event_deferred_cb_schedule_(
+ event_base_get_deferred_cb_queue_(req->base->event_base),
&d->deferred);
}
@@ -891,7 +893,7 @@ reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply)
*/
log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver"
"at %s; will allow the request to time out.",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&req->ns->address,
addrbuf, sizeof(addrbuf)));
break;
@@ -937,8 +939,8 @@ name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
int name_end = -1;
int j = *idx;
int ptr_count = 0;
-#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while (0)
-#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
+#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&t32_, packet + j, 4); j += 4; x = ntohl(t32_); } while (0)
+#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&t_, packet + j, 2); j += 2; x = ntohs(t_); } while (0)
#define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
char *cp = name_out;
@@ -992,8 +994,8 @@ name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
static int
reply_parse(struct evdns_base *base, u8 *packet, int length) {
int j = 0, k = 0; /* index into packet */
- u16 _t; /* used by the macros */
- u32 _t32; /* used by the macros */
+ u16 t_; /* used by the macros */
+ u32 t32_; /* used by the macros */
char tmp_name[256], cmp_name[256]; /* used by the macros */
int name_matches = 0;
@@ -1194,7 +1196,7 @@ static int
request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, ev_socklen_t addrlen)
{
int j = 0; /* index into packet */
- u16 _t; /* used by the macros */
+ u16 t_; /* used by the macros */
char tmp_name[256]; /* used by the macros */
int i;
@@ -1369,7 +1371,7 @@ nameserver_read(struct nameserver *ns) {
(struct sockaddr*)&ns->address, 0)) {
log(EVDNS_LOG_WARN, "Address mismatch on received "
"DNS packet. Apparent source was %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ss,
addrbuf, sizeof(addrbuf)));
return;
@@ -1459,7 +1461,7 @@ nameserver_write_waiting(struct nameserver *ns, char waiting) {
if (event_add(&ns->event, NULL) < 0) {
char addrbuf[128];
log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
/* ???? Do more? */
@@ -1580,20 +1582,20 @@ dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
struct dnslabel_table *table) {
const char *end = name + name_len;
int ref = 0;
- u16 _t;
+ u16 t_;
#define APPEND16(x) do { \
if (j + 2 > (off_t)buf_len) \
goto overflow; \
- _t = htons(x); \
- memcpy(buf + j, &_t, 2); \
+ t_ = htons(x); \
+ memcpy(buf + j, &t_, 2); \
j += 2; \
} while (0)
#define APPEND32(x) do { \
if (j + 4 > (off_t)buf_len) \
goto overflow; \
- _t32 = htonl(x); \
- memcpy(buf + j, &_t32, 4); \
+ t32_ = htonl(x); \
+ memcpy(buf + j, &t32_, 4); \
j += 4; \
} while (0)
@@ -1659,7 +1661,7 @@ evdns_request_data_build(const char *const name, const size_t name_len,
const u16 trans_id, const u16 type, const u16 class,
u8 *const buf, size_t buf_len) {
off_t j = 0; /* current offset into buf */
- u16 _t; /* used by the macros */
+ u16 t_; /* used by the macros */
APPEND16(trans_id);
APPEND16(0x0100); /* standard query, recusion needed */
@@ -1734,9 +1736,9 @@ evdns_close_server_port(struct evdns_server_port *port)
/* exported function */
int
-evdns_server_request_add_reply(struct evdns_server_request *_req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
+evdns_server_request_add_reply(struct evdns_server_request *req_, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
struct server_reply_item **itemp, *item;
int *countp;
int result = -1;
@@ -1871,8 +1873,8 @@ evdns_server_request_format_response(struct server_request *req, int err)
unsigned char buf[1500];
size_t buf_len = sizeof(buf);
off_t j = 0, r;
- u16 _t;
- u32 _t32;
+ u16 t_;
+ u32 t32_;
int i;
u16 flags;
struct dnslabel_table table;
@@ -1930,8 +1932,8 @@ evdns_server_request_format_response(struct server_request *req, int err)
if (r < 0)
goto overflow;
j = r;
- _t = htons( (short) (j-name_start) );
- memcpy(buf+len_idx, &_t, 2);
+ t_ = htons( (short) (j-name_start) );
+ memcpy(buf+len_idx, &t_, 2);
} else {
APPEND16(item->datalen);
if (j+item->datalen > (off_t)buf_len)
@@ -1964,9 +1966,9 @@ overflow:
/* exported function */
int
-evdns_server_request_respond(struct evdns_server_request *_req, int err)
+evdns_server_request_respond(struct evdns_server_request *req_, int err)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
struct evdns_server_port *port = req->port;
int r = -1;
@@ -2112,18 +2114,18 @@ server_port_free(struct evdns_server_port *port)
/* exported function */
int
-evdns_server_request_drop(struct evdns_server_request *_req)
+evdns_server_request_drop(struct evdns_server_request *req_)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
server_request_free(req);
return 0;
}
/* exported function */
int
-evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
+evdns_server_request_get_requesting_addr(struct evdns_server_request *req_, struct sockaddr *sa, int addr_len)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
if (addr_len < (int)req->addrlen)
return -1;
memcpy(sa, &(req->addr), req->addrlen);
@@ -2285,7 +2287,7 @@ nameserver_send_probe(struct nameserver *const ns) {
ASSERT_LOCKED(ns->base);
log(EVDNS_LOG_DEBUG, "Sending probe to %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
handle = mm_calloc(1, sizeof(*handle));
@@ -2442,7 +2444,7 @@ evdns_resume(void)
}
static int
-_evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
+evdns_nameserver_add_impl_(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
/* first check to see if we already have this nameserver */
const struct nameserver *server = base->server_head, *const started_at = base->server_head;
@@ -2470,13 +2472,12 @@ _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *addre
evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
- ns->socket = socket(address->sa_family, SOCK_DGRAM, 0);
+ ns->socket = evutil_socket_(address->sa_family,
+ SOCK_DGRAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
if (ns->socket < 0) { err = 1; goto out1; }
- evutil_make_socket_closeonexec(ns->socket);
- evutil_make_socket_nonblocking(ns->socket);
if (base->global_outgoing_addrlen &&
- !evutil_sockaddr_is_loopback(address)) {
+ !evutil_sockaddr_is_loopback_(address)) {
if (bind(ns->socket,
(struct sockaddr*)&base->global_outgoing_address,
base->global_outgoing_addrlen) < 0) {
@@ -2496,7 +2497,7 @@ _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *addre
}
log(EVDNS_LOG_DEBUG, "Added nameserver %s as %p",
- evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), ns);
+ evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), ns);
/* insert this nameserver into the list of them */
if (!base->server_head) {
@@ -2519,7 +2520,7 @@ out1:
event_debug_unassign(&ns->event);
mm_free(ns);
log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
- evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), err);
+ evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), err);
return err;
}
@@ -2533,7 +2534,7 @@ evdns_base_nameserver_add(struct evdns_base *base, unsigned long int address)
sin.sin_port = htons(53);
sin.sin_family = AF_INET;
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, (struct sockaddr*)&sin, sizeof(sin));
+ res = evdns_nameserver_add_impl_(base, (struct sockaddr*)&sin, sizeof(sin));
EVDNS_UNLOCK(base);
return res;
}
@@ -2585,7 +2586,7 @@ evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string)
sockaddr_setport(sa, 53);
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, sa, len);
+ res = evdns_nameserver_add_impl_(base, sa, len);
EVDNS_UNLOCK(base);
return res;
}
@@ -2604,7 +2605,7 @@ evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
int res;
EVUTIL_ASSERT(base);
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, sa, len);
+ res = evdns_nameserver_add_impl_(base, sa, len);
EVDNS_UNLOCK(base);
return res;
}
@@ -2712,7 +2713,7 @@ request_new(struct evdns_base *base, struct evdns_request *handle, int type,
strlcpy(namebuf, name, sizeof(namebuf));
evutil_secure_rng_get_bytes(randbits, (name_len+7)/8);
for (i = 0; i < name_len; ++i) {
- if (EVUTIL_ISALPHA(namebuf[i])) {
+ if (EVUTIL_ISALPHA_(namebuf[i])) {
if ((randbits[i >> 3] & (1<<(i & 7))))
namebuf[i] |= 0x20;
else
@@ -3242,7 +3243,7 @@ evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
if (flags & DNS_OPTION_NAMESERVERS) evdns_base_nameserver_ip_add(base,"127.0.0.1");
}
-#ifndef _EVENT_HAVE_STRTOK_R
+#ifndef EVENT__HAVE_STRTOK_R
static char *
strtok_r(char *s, const char *delim, char **state) {
char *cp, *start;
@@ -3507,7 +3508,7 @@ evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *con
static char *
evdns_get_default_hosts_filename(void)
{
-#ifdef WIN32
+#ifdef _WIN32
/* Windows is a little coy about where it puts its configuration
* files. Sure, they're _usually_ in C:\windows\system32, but
* there's no reason in principle they couldn't be in
@@ -3545,7 +3546,7 @@ evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char
mm_free(fname);
}
- if ((err = evutil_read_file(filename, &resolv, &n, 0)) < 0) {
+ if ((err = evutil_read_file_(filename, &resolv, &n, 0)) < 0) {
if (err == -1) {
/* No file. */
evdns_resolv_set_defaults(base, flags);
@@ -3589,7 +3590,7 @@ evdns_resolv_conf_parse(int flags, const char *const filename) {
}
-#ifdef WIN32
+#ifdef _WIN32
/* Add multiple nameservers from a space-or-comma-separated list. */
static int
evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
@@ -3632,7 +3633,7 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base)
GetNetworkParams_fn_t fn;
ASSERT_LOCKED(base);
- if (!(handle = evutil_load_windows_system_library(
+ if (!(handle = evutil_load_windows_system_library_(
TEXT("iphlpapi.dll")))) {
log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
status = -1;
@@ -3832,7 +3833,7 @@ evdns_base_new(struct event_base *event_base, int initialize_nameservers)
/* Give the evutil library a hook into its evdns-enabled
* functionality. We can't just call evdns_getaddrinfo directly or
* else libevent-core will depend on libevent-extras. */
- evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo);
+ evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo);
base = mm_malloc(sizeof(struct evdns_base));
if (base == NULL)
@@ -3869,7 +3870,7 @@ evdns_base_new(struct event_base *event_base, int initialize_nameservers)
if (initialize_nameservers) {
int r;
-#ifdef WIN32
+#ifdef _WIN32
r = evdns_base_config_windows_nameservers(base);
#else
r = evdns_base_resolv_conf_parse(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
@@ -4070,7 +4071,7 @@ evdns_base_load_hosts_impl(struct evdns_base *base, const char *hosts_fname)
ASSERT_LOCKED(base);
if (hosts_fname == NULL ||
- (err = evutil_read_file(hosts_fname, &str, &len, 0)) < 0) {
+ (err = evutil_read_file_(hosts_fname, &str, &len, 0)) < 0) {
char tmp[64];
strlcpy(tmp, "127.0.0.1 localhost", sizeof(tmp));
evdns_base_parse_hosts_line(base, tmp);
@@ -4389,7 +4390,7 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
for (i=0; i < count; ++i) {
struct evutil_addrinfo *ai;
memcpy(addrp, ((char*)addresses)+i*addrlen, addrlen);
- ai = evutil_new_addrinfo(sa, socklen, &data->hints);
+ ai = evutil_new_addrinfo_(sa, socklen, &data->hints);
if (!ai) {
if (other_req->r) {
evdns_cancel_request(NULL, other_req->r);
@@ -4402,7 +4403,7 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
free_getaddrinfo_request(data);
return;
}
- res = evutil_addrinfo_append(res, ai);
+ res = evutil_addrinfo_append_(res, ai);
}
if (other_req->r) {
@@ -4416,10 +4417,10 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
* results (if any) and return them. */
if (data->pending_result) {
if (req->type == DNS_IPv4_A)
- res = evutil_addrinfo_append(res,
+ res = evutil_addrinfo_append_(res,
data->pending_result);
else
- res = evutil_addrinfo_append(
+ res = evutil_addrinfo_append_(
data->pending_result, res);
data->pending_result = NULL;
}
@@ -4469,13 +4470,13 @@ evdns_getaddrinfo_fromhosts(struct evdns_base *base,
if ((e->addr.sa.sa_family == AF_INET && f == PF_INET6) ||
(e->addr.sa.sa_family == AF_INET6 && f == PF_INET))
continue;
- ai_new = evutil_new_addrinfo(&e->addr.sa, e->addrlen, hints);
+ ai_new = evutil_new_addrinfo_(&e->addr.sa, e->addrlen, hints);
if (!ai_new) {
n_found = 0;
goto out;
}
sockaddr_setport(ai_new->ai_addr, port);
- ai = evutil_addrinfo_append(ai, ai_new);
+ ai = evutil_addrinfo_append_(ai, ai_new);
}
EVDNS_UNLOCK(base);
out:
@@ -4530,14 +4531,14 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
hints.ai_family = PF_UNSPEC;
}
- evutil_adjust_hints_for_addrconfig(&hints);
+ evutil_adjust_hints_for_addrconfig_(&hints);
/* Now try to see if we _can_ answer immediately. */
/* (It would be nice to do this by calling getaddrinfo directly, with
* AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
* a reliable way to distinguish the "that wasn't a numeric host!" case
* from any other EAI_NONAME cases.) */
- err = evutil_getaddrinfo_common(nodename, servname, &hints, &res, &port);
+ err = evutil_getaddrinfo_common_(nodename, servname, &hints, &res, &port);
if (err != EVUTIL_EAI_NEED_RESOLVE) {
cb(err, res, arg);
return NULL;