mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-20 10:12:15 +01:00
r8753@Kushana: nickm | 2006-09-07 12:23:25 -0400
Make eventdns spacing consistant svn:r8347
This commit is contained in:
parent
5d773af545
commit
4246490b81
@ -298,7 +298,6 @@
|
||||
#undef MIN
|
||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||||
|
||||
|
||||
#if 0
|
||||
#ifdef __USE_ISOC99B
|
||||
// libevent doesn't work without this
|
||||
@ -373,9 +372,9 @@ struct nameserver {
|
||||
struct event event;
|
||||
// these objects are kept in a circular list
|
||||
struct nameserver *next, *prev;
|
||||
struct event timeout_event; // used to keep the timeout for
|
||||
// when we next probe this server.
|
||||
// Valid if state == 0
|
||||
struct event timeout_event; // used to keep the timeout for
|
||||
// when we next probe this server.
|
||||
// Valid if state == 0
|
||||
char state; // zero if we think that this server is down
|
||||
char choaked; // true if we have an EAGAIN from this server's socket
|
||||
char write_waiting; // true if we are waiting for EV_WRITE events
|
||||
@ -425,13 +424,12 @@ static void request_submit(struct request *req);
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
static int
|
||||
last_error(int sock)
|
||||
{
|
||||
last_error(int sock) {
|
||||
int optval, optvallen=sizeof(optval);
|
||||
int err = WSAGetLastError();
|
||||
if (err == WSAEWOULDBLOCK && sock >= 0) {
|
||||
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
|
||||
&optvallen))
|
||||
&optvallen))
|
||||
return err;
|
||||
if (optval)
|
||||
return optval;
|
||||
@ -440,23 +438,21 @@ last_error(int sock)
|
||||
|
||||
}
|
||||
static int
|
||||
error_is_eagain(int err)
|
||||
{
|
||||
error_is_eagain(int err) {
|
||||
return err == EAGAIN || err == WSAEWOULDBLOCK;
|
||||
}
|
||||
static int
|
||||
inet_aton(const char *c, struct in_addr *addr)
|
||||
{
|
||||
uint32_t r;
|
||||
if (strcmp(c, "255.255.255.255") == 0) {
|
||||
addr->s_addr = 0xffffffffu;
|
||||
} else {
|
||||
r = inet_addr(c);
|
||||
if (r == INADDR_NONE)
|
||||
return 0;
|
||||
addr->s_addr = r;
|
||||
}
|
||||
return 1;
|
||||
inet_aton(const char *c, struct in_addr *addr) {
|
||||
uint32_t r;
|
||||
if (strcmp(c, "255.255.255.255") == 0) {
|
||||
addr->s_addr = 0xffffffffu;
|
||||
} else {
|
||||
r = inet_addr(c);
|
||||
if (r == INADDR_NONE)
|
||||
return 0;
|
||||
addr->s_addr = r;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#define CLOSE_SOCKET(x) closesocket(x)
|
||||
#else
|
||||
@ -470,8 +466,7 @@ inet_aton(const char *c, struct in_addr *addr)
|
||||
|
||||
#ifndef NDEBUG
|
||||
static const char *
|
||||
debug_ntoa(u32 address)
|
||||
{
|
||||
debug_ntoa(u32 address) {
|
||||
static char buf[32];
|
||||
u32 a = ntohl(address);
|
||||
sprintf(buf, "%d.%d.%d.%d",
|
||||
@ -486,9 +481,8 @@ debug_ntoa(u32 address)
|
||||
static eventdns_debug_log_fn_type eventdns_log_fn = NULL;
|
||||
|
||||
void
|
||||
eventdns_set_log_fn(eventdns_debug_log_fn_type fn)
|
||||
{
|
||||
eventdns_log_fn = fn;
|
||||
eventdns_set_log_fn(eventdns_debug_log_fn_type fn) {
|
||||
eventdns_log_fn = fn;
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
@ -499,21 +493,20 @@ eventdns_set_log_fn(eventdns_debug_log_fn_type fn)
|
||||
|
||||
static void _eventdns_log(int warn, const char *fmt, ...) EVENTDNS_LOG_CHECK;
|
||||
static void
|
||||
_eventdns_log(int warn, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
static char buf[512];
|
||||
if (!eventdns_log_fn)
|
||||
return;
|
||||
va_start(args,fmt);
|
||||
_eventdns_log(int warn, const char *fmt, ...) {
|
||||
va_list args;
|
||||
static char buf[512];
|
||||
if (!eventdns_log_fn)
|
||||
return;
|
||||
va_start(args,fmt);
|
||||
#ifdef MS_WINDOWS
|
||||
_vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
_vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
#else
|
||||
vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
#endif
|
||||
buf[sizeof(buf)-1] = '\0';
|
||||
eventdns_log_fn(warn, buf);
|
||||
va_end(args);
|
||||
buf[sizeof(buf)-1] = '\0';
|
||||
eventdns_log_fn(warn, buf);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#define log _eventdns_log
|
||||
@ -540,8 +533,8 @@ request_find_from_trans_id(u16 trans_id) {
|
||||
static void
|
||||
nameserver_prod_callback(int fd, short events, void *arg) {
|
||||
struct nameserver *const ns = (struct nameserver *) arg;
|
||||
(void)fd;
|
||||
(void)events;
|
||||
(void)fd;
|
||||
(void)events;
|
||||
|
||||
nameserver_send_probe(ns);
|
||||
}
|
||||
@ -560,17 +553,17 @@ nameserver_probe_failed(struct nameserver *const ns) {
|
||||
}
|
||||
|
||||
timeout =
|
||||
&global_nameserver_timeouts[MIN(ns->failed_times,
|
||||
global_nameserver_timeouts_length - 1)];
|
||||
&global_nameserver_timeouts[MIN(ns->failed_times,
|
||||
global_nameserver_timeouts_length - 1)];
|
||||
ns->failed_times++;
|
||||
|
||||
evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
|
||||
if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) {
|
||||
log(EVENTDNS_LOG_WARN,
|
||||
"Error from libevent when adding timer event for %s",
|
||||
debug_ntoa(ns->address));
|
||||
// ???? Do more?
|
||||
}
|
||||
log(EVENTDNS_LOG_WARN,
|
||||
"Error from libevent when adding timer event for %s",
|
||||
debug_ntoa(ns->address));
|
||||
// ???? Do more?
|
||||
}
|
||||
}
|
||||
|
||||
// called when a nameserver has been deemed to have failed. For example, too
|
||||
@ -596,10 +589,10 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
|
||||
evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
|
||||
if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
|
||||
log(EVENTDNS_LOG_WARN,
|
||||
"Error from libevent when adding timer event for %s",
|
||||
"Error from libevent when adding timer event for %s",
|
||||
debug_ntoa(ns->address));
|
||||
// ???? Do more?
|
||||
}
|
||||
// ???? Do more?
|
||||
}
|
||||
|
||||
// walk the list of inflight requests to see if any can be reassigned to
|
||||
// a different server. Requests in the waiting queue don't have a
|
||||
@ -627,7 +620,7 @@ static void
|
||||
nameserver_up(struct nameserver *const ns) {
|
||||
if (ns->state) return;
|
||||
log(EVENTDNS_LOG_WARN, "Nameserver %s is back up",
|
||||
debug_ntoa(ns->address));
|
||||
debug_ntoa(ns->address));
|
||||
evtimer_del(&ns->timeout_event);
|
||||
ns->state = 1;
|
||||
ns->failed_times = 0;
|
||||
@ -657,7 +650,7 @@ request_finished(struct request *const req, struct request **head) {
|
||||
}
|
||||
|
||||
log(EVENTDNS_LOG_DEBUG, "Removing timeout for request %lx",
|
||||
(unsigned long) req);
|
||||
(unsigned long) req);
|
||||
evtimer_del(&req->timeout_event);
|
||||
|
||||
search_request_finished(req);
|
||||
@ -749,13 +742,14 @@ reply_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply)
|
||||
return;
|
||||
case TYPE_PTR:
|
||||
if (reply) {
|
||||
char *name = reply->data.ptr.name;
|
||||
char *name = reply->data.ptr.name;
|
||||
req->user_callback(DNS_ERR_NONE, DNS_PTR, 1, ttl,
|
||||
&name, req->user_pointer);
|
||||
} else
|
||||
&name, req->user_pointer);
|
||||
} else {
|
||||
req->user_callback(err, 0, 0, 0, NULL,
|
||||
req->user_pointer);
|
||||
return;
|
||||
req->user_pointer);
|
||||
}
|
||||
return;
|
||||
}
|
||||
assert(0);
|
||||
}
|
||||
@ -821,8 +815,7 @@ reply_handle(struct request *const req,
|
||||
}
|
||||
|
||||
static inline int
|
||||
name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len)
|
||||
{
|
||||
name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
|
||||
int name_end = -1;
|
||||
int j = *idx;
|
||||
#define GET32(x) do { if (j + 4 > length) return -1; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0);
|
||||
@ -840,15 +833,15 @@ name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len)
|
||||
|
||||
for(;;) {
|
||||
u8 label_len;
|
||||
if (j >= length) return -1;
|
||||
if (j >= length) return -1;
|
||||
GET8(label_len);
|
||||
if (!label_len) break;
|
||||
if (label_len & 0xc0) {
|
||||
u8 ptr_low;
|
||||
GET8(ptr_low);
|
||||
if (name_end < 0) name_end = j;
|
||||
j = (((int)label_len & 0x3f) << 8) + ptr_low;
|
||||
if (j < 0 || j >= length) return -1;
|
||||
if (name_end < 0) name_end = j;
|
||||
j = (((int)label_len & 0x3f) << 8) + ptr_low;
|
||||
if (j < 0 || j >= length) return -1;
|
||||
continue;
|
||||
}
|
||||
if (label_len > 63) return -1;
|
||||
@ -894,7 +887,7 @@ reply_parse(u8 *packet, int length) {
|
||||
req = request_find_from_trans_id(trans_id);
|
||||
if (!req) return -1;
|
||||
// XXXX should the other return points also call reply_handle? -NM
|
||||
// log("reqparse: trans was %d\n", (int)trans_id);
|
||||
// log("reqparse: trans was %d\n", (int)trans_id);
|
||||
|
||||
memset(&reply, 0, sizeof(reply));
|
||||
|
||||
@ -907,10 +900,10 @@ reply_parse(u8 *packet, int length) {
|
||||
// if (!answers) return; // must have an answer of some form
|
||||
|
||||
// This macro skips a name in the DNS reply.
|
||||
#define SKIP_NAME \
|
||||
do { tmp_name[0] = '\0'; \
|
||||
if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0) \
|
||||
return -1; \
|
||||
#define SKIP_NAME \
|
||||
do { tmp_name[0] = '\0'; \
|
||||
if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0) \
|
||||
return -1; \
|
||||
} while(0);
|
||||
|
||||
reply.type = req->request_type;
|
||||
@ -921,14 +914,14 @@ reply_parse(u8 *packet, int length) {
|
||||
// <label:name><u16:type><u16:class>
|
||||
SKIP_NAME;
|
||||
j += 4;
|
||||
if (j >= length) return -1;
|
||||
if (j >= length) return -1;
|
||||
}
|
||||
|
||||
// now we have the answer section which looks like
|
||||
// <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
|
||||
for (i = 0; i < answers; ++i) {
|
||||
u16 type, class;
|
||||
//int pre = j;
|
||||
//int pre = j;
|
||||
|
||||
// XXX I'd be more comfortable if we actually checked the name
|
||||
// here. -NM
|
||||
@ -938,14 +931,14 @@ reply_parse(u8 *packet, int length) {
|
||||
GET32(ttl);
|
||||
GET16(datalength);
|
||||
|
||||
// log("@%d, Name %s, type %d, class %d, j=%d", pre, tmp_name, (int)type, (int)class, j);
|
||||
// log("@%d, Name %s, type %d, class %d, j=%d", pre, tmp_name, (int)type, (int)class, j);
|
||||
|
||||
if (type == TYPE_A && class == CLASS_INET) {
|
||||
int addrcount, addrtocopy;
|
||||
if (req->request_type != TYPE_A) {
|
||||
j += datalength; continue;
|
||||
}
|
||||
// XXXX do something sane with malformed A answers.
|
||||
// XXXX do something sane with malformed A answers.
|
||||
addrcount = datalength >> 2; // each IP address is 4 bytes
|
||||
addrtocopy = MIN(MAX_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
|
||||
ttl_r = MIN(ttl_r, ttl);
|
||||
@ -962,7 +955,7 @@ reply_parse(u8 *packet, int length) {
|
||||
j += datalength; continue;
|
||||
}
|
||||
if (name_parse(packet, length, &j, reply.data.ptr.name,
|
||||
sizeof(reply.data.ptr.name))<0)
|
||||
sizeof(reply.data.ptr.name))<0)
|
||||
return -1;
|
||||
reply.have_answer = 1;
|
||||
break;
|
||||
@ -972,7 +965,7 @@ reply_parse(u8 *packet, int length) {
|
||||
}
|
||||
// XXXX Implement me. -NM
|
||||
j += datalength;
|
||||
} else {
|
||||
} else {
|
||||
// skip over any other type of resource
|
||||
j += datalength;
|
||||
}
|
||||
@ -1072,7 +1065,7 @@ nameserver_read(struct nameserver *ns) {
|
||||
u8 packet[1500];
|
||||
|
||||
for (;;) {
|
||||
const int r = recv(ns->socket, packet, sizeof(packet), 0);
|
||||
const int r = recv(ns->socket, packet, sizeof(packet), 0);
|
||||
if (r < 0) {
|
||||
int err = last_error(ns->socket);
|
||||
if (error_is_eagain(err)) return;
|
||||
@ -1093,12 +1086,12 @@ nameserver_write_waiting(struct nameserver *ns, char waiting) {
|
||||
ns->write_waiting = waiting;
|
||||
(void) event_del(&ns->event);
|
||||
event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
|
||||
nameserver_ready_callback, ns);
|
||||
nameserver_ready_callback, ns);
|
||||
if (event_add(&ns->event, NULL) < 0) {
|
||||
log(EVENTDNS_LOG_WARN, "Error from libevent when adding event for %s",
|
||||
debug_ntoa(ns->address));
|
||||
// ???? Do more?
|
||||
}
|
||||
log(EVENTDNS_LOG_WARN, "Error from libevent when adding event for %s",
|
||||
debug_ntoa(ns->address));
|
||||
// ???? Do more?
|
||||
}
|
||||
}
|
||||
|
||||
// a callback function. Called by libevent when the kernel says that
|
||||
@ -1106,7 +1099,7 @@ nameserver_write_waiting(struct nameserver *ns, char waiting) {
|
||||
static void
|
||||
nameserver_ready_callback(int fd, short events, void *arg) {
|
||||
struct nameserver *ns = (struct nameserver *) arg;
|
||||
(void)fd;
|
||||
(void)fd;
|
||||
|
||||
if (events & EV_WRITE) {
|
||||
ns->choaked = 0;
|
||||
@ -1130,8 +1123,8 @@ nameserver_ready_callback(int fd, short events, void *arg) {
|
||||
// -1 label was > 63 bytes
|
||||
// -2 name was > 255 bytes
|
||||
static int
|
||||
dnsname_to_labels(u8 *const buf, const char *name, const int name_len) { \
|
||||
const char *end = name + name_len; \
|
||||
dnsname_to_labels(u8 *const buf, const char *name, const int name_len) {
|
||||
const char *end = name + name_len;
|
||||
int j = 0; // current offset into buf
|
||||
|
||||
if (name_len > 255) return -2;
|
||||
@ -1183,8 +1176,8 @@ eventdns_request_len(const int name_len) {
|
||||
// Returns the amount of space used. Negative on error.
|
||||
static int
|
||||
eventdns_request_data_build(const char *const name, const int name_len, const u16 trans_id,
|
||||
const u16 type, const u16 class,
|
||||
u8 *const buf) {
|
||||
const u16 type, const u16 class,
|
||||
u8 *const buf) {
|
||||
int j = 0; // current offset into buf
|
||||
u16 _t; // used by the macros
|
||||
u8 *labels;
|
||||
@ -1199,15 +1192,15 @@ eventdns_request_data_build(const char *const name, const int name_len, const u1
|
||||
APPEND16(0); // no additional
|
||||
|
||||
labels = (u8 *) malloc(name_len + 2);
|
||||
if (!labels) return -1;
|
||||
if (!labels) return -1;
|
||||
labels_len = dnsname_to_labels(labels, name, name_len);
|
||||
if (labels_len < 0) {
|
||||
free(labels);
|
||||
return labels_len;
|
||||
}
|
||||
free(labels);
|
||||
return labels_len;
|
||||
}
|
||||
memcpy(buf + j, labels, labels_len);
|
||||
j += labels_len;
|
||||
free(labels);
|
||||
free(labels);
|
||||
|
||||
APPEND16(type);
|
||||
APPEND16(class);
|
||||
@ -1221,8 +1214,8 @@ eventdns_request_data_build(const char *const name, const int name_len, const u1
|
||||
static void
|
||||
eventdns_request_timeout_callback(int fd, short events, void *arg) {
|
||||
struct request *const req = (struct request *) arg;
|
||||
(void) fd;
|
||||
(void) events;
|
||||
(void) fd;
|
||||
(void) events;
|
||||
|
||||
log(EVENTDNS_LOG_DEBUG, "Request %lx timed out", (unsigned long) arg);
|
||||
|
||||
@ -1298,14 +1291,14 @@ eventdns_request_transmit(struct request *req) {
|
||||
default:
|
||||
// all ok
|
||||
log(EVENTDNS_LOG_DEBUG,
|
||||
"Setting timeout for request %lx", (unsigned long) req);
|
||||
"Setting timeout for request %lx", (unsigned long) req);
|
||||
evtimer_set(&req->timeout_event, eventdns_request_timeout_callback, req);
|
||||
if (evtimer_add(&req->timeout_event, &global_timeout) < 0) {
|
||||
log(EVENTDNS_LOG_WARN,
|
||||
"Error from libevent when adding timer for "
|
||||
"Error from libevent when adding timer for "
|
||||
"request %lx", (unsigned long) req);
|
||||
// ???? Do more?
|
||||
}
|
||||
}
|
||||
req->tx_count++;
|
||||
req->transmit_me = 0;
|
||||
return retcode;
|
||||
@ -1315,10 +1308,10 @@ eventdns_request_transmit(struct request *req) {
|
||||
static void
|
||||
nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
|
||||
struct nameserver *const ns = (struct nameserver *) arg;
|
||||
(void) type;
|
||||
(void) count;
|
||||
(void) ttl;
|
||||
(void) addresses;
|
||||
(void) type;
|
||||
(void) count;
|
||||
(void) ttl;
|
||||
(void) addresses;
|
||||
|
||||
if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
|
||||
// this is a good reply
|
||||
@ -1334,7 +1327,7 @@ nameserver_send_probe(struct nameserver *const ns) {
|
||||
|
||||
log(EVENTDNS_LOG_DEBUG, "Sending probe to %s", debug_ntoa(ns->address));
|
||||
req = request_new(TYPE_A, "www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
|
||||
if (!req) return;
|
||||
if (!req) return;
|
||||
// we force this into the inflight queue no matter what
|
||||
request_trans_id_set(req, transaction_id_pick());
|
||||
req->ns = ns;
|
||||
@ -1366,8 +1359,7 @@ eventdns_transmit(void) {
|
||||
|
||||
// exported function
|
||||
int
|
||||
eventdns_count_nameservers(void)
|
||||
{
|
||||
eventdns_count_nameservers(void) {
|
||||
const struct nameserver *server = server_head;
|
||||
int n = 0;
|
||||
if (!server)
|
||||
@ -1381,8 +1373,7 @@ eventdns_count_nameservers(void)
|
||||
|
||||
// exported function
|
||||
int
|
||||
eventdns_clear_nameservers_and_suspend(void)
|
||||
{
|
||||
eventdns_clear_nameservers_and_suspend(void) {
|
||||
struct nameserver *server = server_head, *started_at = server_head;
|
||||
struct request *req = req_head, *req_started_at = req_head;
|
||||
|
||||
@ -1431,8 +1422,7 @@ eventdns_clear_nameservers_and_suspend(void)
|
||||
|
||||
// exported function
|
||||
int
|
||||
eventdns_resume(void)
|
||||
{
|
||||
eventdns_resume(void) {
|
||||
eventdns_requests_pump_waiting_queue();
|
||||
return 0;
|
||||
}
|
||||
@ -1454,19 +1444,19 @@ eventdns_nameserver_add(unsigned long int address) {
|
||||
}
|
||||
|
||||
ns = (struct nameserver *) malloc(sizeof(struct nameserver));
|
||||
if (!ns) return -1;
|
||||
if (!ns) return -1;
|
||||
|
||||
memset(ns, 0, sizeof(struct nameserver));
|
||||
|
||||
ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (ns->socket < 0) { err = 1; goto out1; }
|
||||
#ifdef MS_WINDOWS
|
||||
{
|
||||
{
|
||||
u_long nonblocking = 1;
|
||||
ioctlsocket(ns->socket, FIONBIO, &nonblocking);
|
||||
}
|
||||
#else
|
||||
fcntl(ns->socket, F_SETFL, O_NONBLOCK);
|
||||
fcntl(ns->socket, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
sin.sin_addr.s_addr = address;
|
||||
sin.sin_port = htons(53);
|
||||
@ -1480,9 +1470,9 @@ eventdns_nameserver_add(unsigned long int address) {
|
||||
ns->state = 1;
|
||||
event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
|
||||
if (event_add(&ns->event, NULL) < 0) {
|
||||
err = 2;
|
||||
goto out2;
|
||||
}
|
||||
err = 2;
|
||||
goto out2;
|
||||
}
|
||||
|
||||
log(EVENTDNS_LOG_DEBUG, "Added nameserver %s", debug_ntoa(address));
|
||||
|
||||
@ -1503,12 +1493,12 @@ eventdns_nameserver_add(unsigned long int address) {
|
||||
|
||||
return 0;
|
||||
|
||||
out2:
|
||||
out2:
|
||||
CLOSE_SOCKET(ns->socket);
|
||||
out1:
|
||||
out1:
|
||||
free(ns);
|
||||
log(EVENTDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
|
||||
debug_ntoa(address), err);
|
||||
debug_ntoa(address), err);
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -1555,9 +1545,9 @@ request_new(int type, const char *name, int flags, eventdns_callback_type callba
|
||||
// the request data is alloced in a single block with the header
|
||||
struct request *const req = (struct request *) malloc(sizeof(struct request) + request_max_len);
|
||||
int rlen;
|
||||
(void) flags;
|
||||
(void) flags;
|
||||
|
||||
if (!req) return NULL;
|
||||
if (!req) return NULL;
|
||||
memset(req, 0, sizeof(struct request));
|
||||
|
||||
// request data lives just after the header
|
||||
@ -1575,7 +1565,7 @@ request_new(int type, const char *name, int flags, eventdns_callback_type callba
|
||||
req->next = req->prev = NULL;
|
||||
|
||||
return req;
|
||||
err1:
|
||||
err1:
|
||||
free(req->request);
|
||||
return NULL;
|
||||
}
|
||||
@ -1614,10 +1604,10 @@ int eventdns_resolve_reverse(struct in_addr *in, int flags, eventdns_callback_ty
|
||||
assert(in);
|
||||
a = ntohl(in->s_addr);
|
||||
sprintf(buf, "%d.%d.%d.%d.in-addr.arpa",
|
||||
(int)(u8)((a )&0xff),
|
||||
(int)(u8)((a>>8 )&0xff),
|
||||
(int)(u8)((a>>16)&0xff),
|
||||
(int)(u8)((a>>24)&0xff));
|
||||
(int)(u8)((a )&0xff),
|
||||
(int)(u8)((a>>8 )&0xff),
|
||||
(int)(u8)((a>>16)&0xff),
|
||||
(int)(u8)((a>>24)&0xff));
|
||||
log(EVENTDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
|
||||
req = request_new(TYPE_PTR, buf, flags, callback, ptr);
|
||||
if (!req) return 1;
|
||||
@ -1665,7 +1655,7 @@ search_state_decref(struct search_state *const state) {
|
||||
}
|
||||
free(state);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static struct search_state *
|
||||
search_state_new(void) {
|
||||
@ -2048,11 +2038,12 @@ eventdns_nameserver_ip_add_line(const char *ips) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef DWORD (WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
|
||||
|
||||
// Use the windows GetNetworkParams interface in iphlpapi.dll to
|
||||
// figure out what our nameservers are.
|
||||
static int
|
||||
load_nameservers_with_getnetworkparams(void)
|
||||
{
|
||||
load_nameservers_with_getnetworkparams(void) {
|
||||
// Based on MSDN examples and inspection of c-ares code.
|
||||
FIXED_INFO *fixed;
|
||||
HMODULE handle = 0;
|
||||
@ -2060,28 +2051,28 @@ load_nameservers_with_getnetworkparams(void)
|
||||
void *buf = NULL;
|
||||
int status = 0, r, added_any;
|
||||
IP_ADDR_STRING *ns;
|
||||
DWORD (WINAPI *fn)(FIXED_INFO*, DWORD*);
|
||||
GetNetworkparams_fn_t fn;
|
||||
|
||||
if (!(handle = LoadLibrary("iphlpapi.dll"))) {
|
||||
log(EVENTDNS_LOG_WARN,"Could not open iphlpapi.dll");
|
||||
//right now status = 0, doesn't that mean "good" - mikec
|
||||
status = -1;
|
||||
goto done;
|
||||
log(EVENTDNS_LOG_WARN,"Could not open iphlpapi.dll");
|
||||
//right now status = 0, doesn't that mean "good" - mikec
|
||||
status = -1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!(fn =
|
||||
(DWORD (WINAPI*)(FIXED_INFO*,DWORD*))
|
||||
GetProcAddress(handle, "GetNetworkParams"))) {
|
||||
log(EVENTDNS_LOG_WARN,"Could not get address of function.");
|
||||
//same as above
|
||||
status = -1;
|
||||
goto done;
|
||||
(GetNetworkParams_fn_t)
|
||||
GetProcAddress(handle, "GetNetworkParams"))) {
|
||||
log(EVENTDNS_LOG_WARN,"Could not get address of function.");
|
||||
//same as above
|
||||
status = -1;
|
||||
goto done;
|
||||
}
|
||||
|
||||
buf = malloc(size);
|
||||
if (!buf) {
|
||||
status = 4;
|
||||
goto done;
|
||||
status = 4;
|
||||
goto done;
|
||||
}
|
||||
fixed = buf;
|
||||
r = fn(fixed, &size);
|
||||
@ -2096,9 +2087,9 @@ load_nameservers_with_getnetworkparams(void)
|
||||
fixed = buf;
|
||||
r = fn(fixed, &size);
|
||||
if (r != ERROR_SUCCESS) {
|
||||
log(EVENTDNS_LOG_DEBUG,"fn() failed.");
|
||||
status = -1;
|
||||
goto done;
|
||||
log(EVENTDNS_LOG_DEBUG,"fn() failed.");
|
||||
status = -1;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2106,24 +2097,24 @@ load_nameservers_with_getnetworkparams(void)
|
||||
added_any = 0;
|
||||
ns = &(fixed->DnsServerList);
|
||||
while (ns) {
|
||||
r = eventdns_nameserver_ip_add_line(ns->IpAddress.String);
|
||||
if (r) {
|
||||
log(EVENTDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
|
||||
(ns->IpAddress.String),(int)GetLastError());
|
||||
status = r;
|
||||
goto done;
|
||||
} else {
|
||||
log(EVENTDNS_LOG_DEBUG,"Succesfully added %s as nameserver",ns->IpAddress.String);
|
||||
}
|
||||
r = eventdns_nameserver_ip_add_line(ns->IpAddress.String);
|
||||
if (r) {
|
||||
log(EVENTDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
|
||||
(ns->IpAddress.String),(int)GetLastError());
|
||||
status = r;
|
||||
goto done;
|
||||
} else {
|
||||
log(EVENTDNS_LOG_DEBUG,"Succesfully added %s as nameserver",ns->IpAddress.String);
|
||||
}
|
||||
|
||||
added_any++;
|
||||
ns = ns->Next;
|
||||
added_any++;
|
||||
ns = ns->Next;
|
||||
}
|
||||
|
||||
if (!added_any) {
|
||||
//should we ever get here? - mikec
|
||||
log(EVENTDNS_LOG_DEBUG,"No name servers added.");
|
||||
status = -1;
|
||||
//should we ever get here? - mikec
|
||||
log(EVENTDNS_LOG_DEBUG,"No name servers added.");
|
||||
status = -1;
|
||||
}
|
||||
|
||||
done:
|
||||
@ -2135,8 +2126,7 @@ 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 char *subkey) {
|
||||
char *buf;
|
||||
DWORD bufsz = 0, type = 0;
|
||||
int status = 0;
|
||||
@ -2162,31 +2152,31 @@ config_nameserver_from_reg_key(HKEY key, const char *subkey)
|
||||
#define WIN_NS_NT_KEY SERVICES_KEY "Tcpip\\Parameters"
|
||||
|
||||
static int
|
||||
load_nameservers_from_registry(void)
|
||||
{
|
||||
load_nameservers_from_registry(void) {
|
||||
int found = 0;
|
||||
int r;
|
||||
#define TRY(k, name) \
|
||||
if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
|
||||
log(EVENTDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
|
||||
found = 1; \
|
||||
} else {\
|
||||
if (!found)\
|
||||
log(EVENTDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s",#k,#name);\
|
||||
#define TRY(k, name) \
|
||||
if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
|
||||
log(EVENTDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
|
||||
found = 1; \
|
||||
} else { \
|
||||
if (!found) \
|
||||
log(EVENTDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
|
||||
#k,#name); \
|
||||
}
|
||||
|
||||
if (((int)GetVersion()) > 0) { /* NT */
|
||||
HKEY nt_key = 0, interfaces_key = 0;
|
||||
|
||||
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
|
||||
KEY_READ, &nt_key) != ERROR_SUCCESS){
|
||||
KEY_READ, &nt_key) != ERROR_SUCCESS) {
|
||||
log(EVENTDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
r = RegOpenKeyEx(nt_key, "Interfaces", 0,
|
||||
KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
|
||||
&interfaces_key);
|
||||
KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
|
||||
&interfaces_key);
|
||||
|
||||
if (r != ERROR_SUCCESS ) {
|
||||
log(EVENTDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
|
||||
@ -2202,7 +2192,7 @@ load_nameservers_from_registry(void)
|
||||
} else {
|
||||
HKEY win_key = 0;
|
||||
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
|
||||
KEY_READ, &win_key) != ERROR_SUCCESS) {
|
||||
KEY_READ, &win_key) != ERROR_SUCCESS) {
|
||||
log(EVENTDNS_LOG_DEBUG,"Couldn't open registry key, %d",(int)GetLastError());
|
||||
return -1;
|
||||
|
||||
@ -2215,14 +2205,12 @@ load_nameservers_from_registry(void)
|
||||
log(EVENTDNS_LOG_WARN,"Didn't find any nameservers.");
|
||||
}
|
||||
|
||||
|
||||
return found ? 0 : -1;
|
||||
#undef TRY
|
||||
}
|
||||
|
||||
int
|
||||
eventdns_config_windows_nameservers(void)
|
||||
{
|
||||
eventdns_config_windows_nameservers(void) {
|
||||
if (load_nameservers_with_getnetworkparams() == 0) {
|
||||
return 0;
|
||||
}
|
||||
@ -2232,30 +2220,30 @@ eventdns_config_windows_nameservers(void)
|
||||
#endif
|
||||
|
||||
#ifdef EVENTDNS_MAIN
|
||||
void main_callback(int result, char type, int count, int ttl,
|
||||
void *addrs, void *orig)
|
||||
{
|
||||
void
|
||||
main_callback(int result, char type, int count, int ttl,
|
||||
void *addrs, void *orig) {
|
||||
char *n = (char*)orig;
|
||||
int i;
|
||||
for (i = 0; i < count; ++i) {
|
||||
if (type == DNS_IPv4_A) {
|
||||
if (type == DNS_IPv4_A) {
|
||||
printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
|
||||
} else if (type == DNS_PTR) {
|
||||
printf("%s: %s\n", n, ((char**)addrs)[i]);
|
||||
}
|
||||
}
|
||||
if (!count) {
|
||||
printf("%s: No answer (%d)\n", n, result);
|
||||
}
|
||||
fflush(stdout);
|
||||
if (!count) {
|
||||
printf("%s: No answer (%d)\n", n, result);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void logfn(const char *msg)
|
||||
{
|
||||
void
|
||||
logfn(const char *msg) {
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
}
|
||||
int main(int c, char **v)
|
||||
{
|
||||
int
|
||||
main(int c, char **v) {
|
||||
int idx;
|
||||
int reverse = 0, verbose = 1;
|
||||
if (c<2) {
|
||||
@ -2263,37 +2251,43 @@ int main(int c, char **v)
|
||||
return 1;
|
||||
}
|
||||
idx = 1;
|
||||
while (idx < c && v[idx][0] == '-') {
|
||||
if (!strcmp(v[idx], "-x"))
|
||||
reverse = 1;
|
||||
else if (!strcmp(v[idx], "-v"))
|
||||
verbose = 1;
|
||||
else
|
||||
fprintf(stderr, "Unknown option %s\n", v[idx]);
|
||||
++idx;
|
||||
}
|
||||
while (idx < c && v[idx][0] == '-') {
|
||||
if (!strcmp(v[idx], "-x"))
|
||||
reverse = 1;
|
||||
else if (!strcmp(v[idx], "-v"))
|
||||
verbose = 1;
|
||||
else
|
||||
fprintf(stderr, "Unknown option %s\n", v[idx]);
|
||||
++idx;
|
||||
}
|
||||
event_init();
|
||||
if (verbose)
|
||||
eventdns_set_log_fn(logfn);
|
||||
if (verbose)
|
||||
eventdns_set_log_fn(logfn);
|
||||
eventdns_resolv_conf_parse(DNS_OPTION_NAMESERVERS, "/etc/resolv.conf");
|
||||
for (; idx < c; ++idx) {
|
||||
if (reverse) {
|
||||
struct in_addr addr;
|
||||
if (!inet_aton(v[idx], &addr)) {
|
||||
fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
|
||||
fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
|
||||
continue;
|
||||
}
|
||||
fprintf(stderr, "resolving %s...\n",v[idx]);
|
||||
eventdns_resolve_reverse(&addr, 0, main_callback, v[idx]);
|
||||
}
|
||||
fprintf(stderr, "resolving %s...\n",v[idx]);
|
||||
eventdns_resolve_reverse(&addr, 0, main_callback, v[idx]);
|
||||
} else {
|
||||
fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
|
||||
fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
|
||||
eventdns_resolve_ipv4(v[idx], 0, main_callback, v[idx]);
|
||||
}
|
||||
}
|
||||
fflush(stdout);
|
||||
fflush(stdout);
|
||||
event_dispatch();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
// Local Variables:
|
||||
// tab-width: 4
|
||||
// c-basic-offset: 4
|
||||
// indent-tabs-mode: t
|
||||
// End:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user