add a simple regression test for the DNS resolver; requires internet access.

do some KNF on evdns.c; add checks to prevent potential buffer overflows.
fix one memory leak.


svn:r230
This commit is contained in:
Niels Provos 2006-08-27 20:04:20 +00:00
parent e80e52ceb7
commit 07c3fb5067
5 changed files with 147 additions and 21 deletions

66
evdns.c
View File

@ -279,6 +279,7 @@
#include <stdarg.h> #include <stdarg.h>
#include "evdns.h" #include "evdns.h"
#include "log.h"
#ifndef HOST_NAME_MAX #ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255 #define HOST_NAME_MAX 255
@ -849,7 +850,8 @@ transaction_id_pick(void) {
#ifdef DNS_USE_CPU_CLOCK_FOR_ID #ifdef DNS_USE_CPU_CLOCK_FOR_ID
struct timespec ts; struct timespec ts;
const u16 trans_id = ts.tv_nsec & 0xffff; const u16 trans_id = ts.tv_nsec & 0xffff;
if (clock_gettime(CLOCK_PROF, &ts)) abort(); if (clock_gettime(CLOCK_MONOTONIC, &ts))
event_err(1, "clock_gettime");
#endif #endif
#ifdef DNS_USE_GETTIMEOFDAY_FOR_ID #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
@ -1037,15 +1039,21 @@ evdns_request_len(const int name_len) {
// //
// Returns the amount of space used. Negative on error. // Returns the amount of space used. Negative on error.
static int static int
evdns_request_data_build(const char *const name, const int name_len, const u16 trans_id, evdns_request_data_build(const char *const name, const int name_len,
const u16 type, const u16 class, const u16 trans_id, const u16 type, const u16 class,
u8 *const buf) { u8 *const buf, size_t buf_len) {
int j = 0; // current offset into buf int j = 0; // current offset into buf
u16 _t; // used by the macros u16 _t; // used by the macros
u8 *labels; u8 *labels;
int labels_len; int labels_len;
#define APPEND16(x) do { _t = htons(x); memcpy(buf + j, &_t, 2); j += 2; } while(0); #define APPEND16(x) do { \
if (j + 2 > buf_len) \
return (-1); \
_t = htons(x); \
memcpy(buf + j, &_t, 2); j += 2; \
} while(0)
APPEND16(trans_id); APPEND16(trans_id);
APPEND16(0x0100); // standard query, recusion needed APPEND16(0x0100); // standard query, recusion needed
APPEND16(1); // one question APPEND16(1); // one question
@ -1054,17 +1062,26 @@ evdns_request_data_build(const char *const name, const int name_len, const u16 t
APPEND16(0); // no additional APPEND16(0); // no additional
labels = (u8 *) malloc(name_len + 2); labels = (u8 *) malloc(name_len + 2);
if (!labels) return -1; if (labels == NULL)
return (-1);
labels_len = dnsname_to_labels(labels, name, name_len); labels_len = dnsname_to_labels(labels, name, name_len);
if (labels_len < 0) return labels_len; if (labels_len < 0) {
free(labels);
return (labels_len);
}
if (j + labels_len > buf_len) {
free(labels);
return (-1);
}
memcpy(buf + j, labels, labels_len); memcpy(buf + j, labels, labels_len);
j += labels_len; j += labels_len;
free(labels);
APPEND16(type); APPEND16(type);
APPEND16(class); APPEND16(class);
#undef APPEND16 #undef APPEND16
return j; return (j);
} }
// this is a libevent callback function which is called when a request // this is a libevent callback function which is called when a request
@ -1396,14 +1413,17 @@ string_num_dots(const char *s) {
} }
static struct request * static struct request *
request_new(const char *name, int flags, evdns_callback_type callback, void *ptr) { request_new(const char *name, int flags,
const char issuing_now = (global_requests_inflight < global_max_requests_inflight) ? 1 : 0; evdns_callback_type callback, void *ptr) {
const char issuing_now =
(global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
const int name_len = strlen(name); const int name_len = strlen(name);
const int request_max_len = evdns_request_len(name_len); const int request_max_len = evdns_request_len(name_len);
const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff; const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
// the request data is alloced in a single block with the header // 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); struct request *const req =
(struct request *) malloc(sizeof(struct request) + request_max_len);
int rlen; int rlen;
(void) flags; (void) flags;
@ -1412,9 +1432,12 @@ request_new(const char *name, int flags, evdns_callback_type callback, void *ptr
// request data lives just after the header // request data lives just after the header
req->request = ((u8 *) req) + sizeof(struct request); req->request = ((u8 *) req) + sizeof(struct request);
req->request_appended = 1; // denotes that the request data shouldn't be free()ed // denotes that the request data shouldn't be free()ed
rlen = evdns_request_data_build(name, name_len, trans_id, TYPE_A, CLASS_INET, req->request); req->request_appended = 1;
if (rlen < 0) goto err1; rlen = evdns_request_data_build(name, name_len, trans_id,
TYPE_A, CLASS_INET, req->request, request_max_len);
if (rlen < 0)
goto err1;
req->request_len = rlen; req->request_len = rlen;
req->trans_id = trans_id; req->trans_id = trans_id;
req->tx_count = 0; req->tx_count = 0;
@ -1444,15 +1467,18 @@ request_submit(struct request *const req) {
} }
// exported function // exported function
int evdns_resolve(const char *name, int flags, evdns_callback_type callback, void *ptr) { int evdns_resolve(const char *name, int flags,
evdns_callback_type callback, void *ptr) {
log("Resolve requested for %s", name); log("Resolve requested for %s", name);
if (flags & DNS_QUERY_NO_SEARCH) { if (flags & DNS_QUERY_NO_SEARCH) {
struct request *const req = request_new(name, flags, callback, ptr); struct request *const req =
if (!req) return 1; request_new(name, flags, callback, ptr);
if (req == NULL)
return (1);
request_submit(req); request_submit(req);
return 0; return (0);
} else { } else {
return search_request_new(name, flags, callback, ptr); return (search_request_new(name, flags, callback, ptr));
} }
} }

View File

@ -13,7 +13,7 @@ test_init_SOURCES = test-init.c
test_eof_SOURCES = test-eof.c test_eof_SOURCES = test-eof.c
test_weof_SOURCES = test-weof.c test_weof_SOURCES = test-weof.c
test_time_SOURCES = test-time.c test_time_SOURCES = test-time.c
regress_SOURCES = regress.c regress.h regress_http.c \ regress_SOURCES = regress.c regress.h regress_http.c regress_dns.c \
regress.gen.c regress.gen.h regress.gen.c regress.gen.h
bench_SOURCES = bench.c bench_SOURCES = bench.c

View File

@ -882,6 +882,8 @@ main (int argc, char **argv)
event_base = event_init(); event_base = event_init();
http_suite(); http_suite();
dns_suite();
test_simpleread(); test_simpleread();

View File

@ -34,6 +34,8 @@ extern "C" {
void http_suite(void); void http_suite(void);
void http_basic_test(void); void http_basic_test(void);
void dns_suite(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

96
test/regress_dns.c Normal file
View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2003-2006 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
#ifndef WIN32
#include <sys/socket.h>
#include <sys/signal.h>
#include <unistd.h>
#endif
#include <netdb.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "event.h"
#include "evdns.h"
#include "log.h"
static int dns_ok = 0;
void
dns_gethostbyname_cb(int result, char type, int count, int ttl,
void *addresses, void *arg)
{
if (result == DNS_ERR_NONE)
dns_ok = 1;
event_loopexit(NULL);
}
void
dns_gethostbyname()
{
fprintf(stdout, "Simple DNS resolve: ");
evdns_resolve("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
event_dispatch();
if (dns_ok) {
fprintf(stdout, "OK\n");
} else {
fprintf(stdout, "FAILED\n");
exit(1);
}
}
void
dns_suite(void)
{
#ifdef WIN32
evdns_config_windows_nameservers();
#else
evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
#endif
dns_gethostbyname();
evdns_clear_nameservers_and_suspend();
}