Branch
Hash :
19b3fd0b
Author :
Date :
2021-04-07T20:39:18
evdns: add ability to get CNAME Add new flag (DNS_CNAME_CALLBACK) for evdns_base_resolve_ipv4()/evdns_base_resolve_ipv6(). If set, you will get one more callback with type == DNS_CNAME and CNAME in addrs argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
* Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
*
* 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.
*/
#include "../util-internal.h"
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#endif
#include "event2/event-config.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "event2/dns.h"
#include "event2/dns_struct.h"
#include "event2/event.h"
#include "event2/event_compat.h"
#include "event2/util.h"
#include "event2/listener.h"
#include "event2/bufferevent.h"
#include "log-internal.h"
#include "regress.h"
#include "regress_testutils.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
/* globals */
static struct evdns_server_port *udp_dns_port;
evutil_socket_t udp_dns_sock = -1;
static struct evdns_server_port *tcp_dns_port;
evutil_socket_t tcp_dns_sock = -1;
/* Helper: return the port that a socket is bound on, in host order. */
int
regress_get_socket_port(evutil_socket_t fd)
{
struct sockaddr_storage ss;
ev_socklen_t socklen = sizeof(ss);
if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
return -1;
if (ss.ss_family == AF_INET)
return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
else if (ss.ss_family == AF_INET6)
return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
else
return -1;
}
struct evdns_server_port *
regress_get_udp_dnsserver(struct event_base *base,
ev_uint16_t *portnum,
evutil_socket_t *psock,
evdns_request_callback_fn_type cb,
void *arg)
{
struct evdns_server_port *port = NULL;
evutil_socket_t sock;
struct sockaddr_in my_addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
tt_abort_perror("socket");
}
evutil_make_socket_nonblocking(sock);
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(*portnum);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
evutil_closesocket(sock);
tt_abort_perror("bind");
}
port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
if (!*portnum)
*portnum = regress_get_socket_port(sock);
if (psock)
*psock = sock;
return port;
end:
return NULL;
}
struct evdns_server_port *
regress_get_tcp_dnsserver(struct event_base *base,
ev_uint16_t *portnum,
evutil_socket_t *psock,
evdns_request_callback_fn_type cb,
void *arg)
{
struct evdns_server_port *port = NULL;
evutil_socket_t sock;
struct sockaddr_in my_addr;
struct evconnlistener *listener;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(*portnum);
my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
listener = evconnlistener_new_bind(base, NULL, NULL,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 128,
(struct sockaddr*)&my_addr, sizeof(my_addr));
if (!listener)
goto end;
port = evdns_add_server_port_with_listener(base, listener, 0, cb, arg);
if (!port)
goto end;
sock = evconnlistener_get_fd(listener);
if (!*portnum)
*portnum = regress_get_socket_port(sock);
if (psock)
*psock = sock;
return port;
end:
if (listener)
evconnlistener_free(listener);
return NULL;
}
void
regress_clean_dnsserver(void)
{
if (udp_dns_port) {
evdns_close_server_port(udp_dns_port);
udp_dns_port = NULL;
}
if (udp_dns_sock >= 0) {
evutil_closesocket(udp_dns_sock);
udp_dns_sock = -1;
}
if (tcp_dns_port) {
evdns_close_server_port(tcp_dns_port);
tcp_dns_port = NULL;
}
if (tcp_dns_sock >= 0) {
evutil_closesocket(tcp_dns_sock);
tcp_dns_sock = -1;
}
}
static void strtolower(char *s)
{
while (*s) {
*s = EVUTIL_TOLOWER_(*s);
++s;
}
}
void
regress_dns_server_cb(struct evdns_server_request *req, void *data)
{
struct regress_dns_server_table *tab = data;
char *question;
if (req->nquestions != 1)
TT_DIE(("Only handling one question at a time; got %d",
req->nquestions));
question = req->questions[0]->name;
while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
strcmp("*", tab->q))
++tab;
if (tab->q == NULL)
TT_DIE(("Unexpected question: '%s'", question));
++tab->seen;
if (tab->lower)
strtolower(question);
if (!strcmp(tab->anstype, "err")) {
int err = atoi(tab->ans);
if (DNS_ERR_TIMEOUT == err) {
tt_assert(! evdns_server_request_drop(req));
} else {
tt_assert(! evdns_server_request_respond(req, err));
}
return;
} else if (!strcmp(tab->anstype, "errsoa")) {
int err = atoi(tab->ans);
char soa_record[] =
"\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
"\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
"\x77\xde\x5e\xba" /* serial */
"\x00\x00\x1c\x20" /* refreshtime = 2h */
"\x00\x00\x0e\x10" /* retry = 1h */
"\x00\x12\x75\x00" /* expiration = 14d */
"\x00\x00\x0e\x10" /* min.ttl = 1h */
;
evdns_server_request_add_reply(
req, EVDNS_AUTHORITY_SECTION,
"example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
42, sizeof(soa_record) - 1, 0, soa_record);
tt_assert(! evdns_server_request_respond(req, err));
return;
} else if (!strcmp(tab->anstype, "A")) {
struct in_addr in[2048];
int count = parse_csv_address_list(tab->ans, AF_INET, in, ARRAY_SIZE(in));
evdns_server_request_add_a_reply(req, question, count, in, 100);
} else if (!strcmp(tab->anstype, "AAAA")) {
struct in6_addr in6;
if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
TT_DIE(("Bad AAAA value %s in table", tab->ans));
}
evdns_server_request_add_aaaa_reply(req,
question, 1, &in6.s6_addr, 100);
} else if (!strcmp(tab->anstype, "CNAME")) {
struct in_addr in;
evutil_inet_pton(AF_INET, "11.22.33.44", &in);
evdns_server_request_add_a_reply(req, question, 1, &in, 100);
evdns_server_request_add_cname_reply(req, question, tab->ans, 100);
} else {
TT_DIE(("Weird table entry with type '%s'", tab->anstype));
}
tt_assert(! evdns_server_request_respond(req, 0))
return;
end:
tt_want(! evdns_server_request_drop(req));
}
int
regress_dnsserver(struct event_base *base, ev_uint16_t *port,
struct regress_dns_server_table *udp_seach_table,
struct regress_dns_server_table *tcp_seach_table)
{
if (!udp_seach_table && !tcp_seach_table)
goto error;
if (tcp_seach_table) {
tcp_dns_port = regress_get_tcp_dnsserver(base, port, &tcp_dns_sock,
regress_dns_server_cb, tcp_seach_table);
if (!tcp_dns_port)
goto error;
}
if (udp_seach_table) {
udp_dns_port = regress_get_udp_dnsserver(base, port, &udp_dns_sock,
regress_dns_server_cb, udp_seach_table);
if (!udp_dns_port)
goto error;
}
return 1;
error:
regress_clean_dnsserver();
return 0;
}
int
regress_get_listener_addr(struct evconnlistener *lev,
struct sockaddr *sa, ev_socklen_t *socklen)
{
evutil_socket_t s = evconnlistener_get_fd(lev);
if (s <= 0)
return -1;
return getsockname(s, sa, socklen);
}
int
parse_csv_address_list(const char *s, int family, void *addrs, size_t addrs_size)
{
int i = 0;
char *token;
char buf[16384];
void *next_addr;
tt_assert(family == AF_INET || family == AF_INET6);
tt_assert(strlen(s) < ARRAY_SIZE(buf));
strcpy(buf, s);
token = strtok(buf, ",");
do {
tt_assert((unsigned)i < addrs_size);
next_addr = (family == AF_INET) ? (void *)((struct in_addr*)addrs + i)
: (void *)((struct in6_addr*)addrs + i);
if (!evutil_inet_pton(AF_INET, token, next_addr)) {
TT_DIE(("Bad %s value %s in table", (family == AF_INET) ? "A" :"AAAA", token));
}
++i;
token = strtok (NULL, ",");
} while (token);
end:
return i;
}