Module Name: src
Committed By: gson
Date: Mon Jan 6 14:50:33 UTC 2014
Modified Files:
src/distrib/sets/lists/debug: mi
src/distrib/sets/lists/tests: mi
src/tests/lib/libc/net: Makefile t_hostent.sh
Added Files:
src/tests/lib/libc/net: h_dns_server.c
Log Message:
Make t_hostent test cases that query the DNS work on systems with no
Internet connectivity, by hijacking DNS queries and /etc/resolv.conf
accesses using librumphijack and providing a minimal DNS server to
answer the queries.
To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/distrib/sets/lists/debug/mi
cvs rdiff -u -r1.553 -r1.554 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.8 -r1.9 src/tests/lib/libc/net/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/net/h_dns_server.c
cvs rdiff -u -r1.3 -r1.4 src/tests/lib/libc/net/t_hostent.sh
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/sets/lists/debug/mi
diff -u src/distrib/sets/lists/debug/mi:1.45 src/distrib/sets/lists/debug/mi:1.46
--- src/distrib/sets/lists/debug/mi:1.45 Sat Dec 28 02:32:01 2013
+++ src/distrib/sets/lists/debug/mi Mon Jan 6 14:50:32 2014
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.45 2013/12/28 02:32:01 rjs Exp $
+# $NetBSD: mi,v 1.46 2014/01/06 14:50:32 gson Exp $
./etc/mtree/set.debug comp-sys-root
./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib
@@ -1801,6 +1801,7 @@
./usr/libdata/debug/usr/tests/lib/libc/locale/t_wcstod.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/locale/t_wctomb.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/net/getaddrinfo/h_gai.debug tests-lib-debug debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/net/h_dns_server.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/net/h_hostent.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/net/h_nsd_recurse.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/net/h_protoent.debug tests-lib-debug debug,atf
Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.553 src/distrib/sets/lists/tests/mi:1.554
--- src/distrib/sets/lists/tests/mi:1.553 Thu Jan 2 16:46:40 2014
+++ src/distrib/sets/lists/tests/mi Mon Jan 6 14:50:32 2014
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.553 2014/01/02 16:46:40 tho Exp $
+# $NetBSD: mi,v 1.554 2014/01/06 14:50:32 gson Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -1978,6 +1978,7 @@
./usr/tests/lib/libc/net/getaddrinfo/data/unsup_fam.exp tests-lib-tests atf
./usr/tests/lib/libc/net/getaddrinfo/h_gai tests-lib-tests atf
./usr/tests/lib/libc/net/getaddrinfo/t_getaddrinfo tests-lib-tests atf
+./usr/tests/lib/libc/net/h_dns_server tests-lib-tests atf
./usr/tests/lib/libc/net/h_hostent tests-lib-tests atf
./usr/tests/lib/libc/net/h_nsd_recurse tests-lib-tests atf
./usr/tests/lib/libc/net/h_protoent tests-lib-tests atf
Index: src/tests/lib/libc/net/Makefile
diff -u src/tests/lib/libc/net/Makefile:1.8 src/tests/lib/libc/net/Makefile:1.9
--- src/tests/lib/libc/net/Makefile:1.8 Fri Aug 16 15:29:45 2013
+++ src/tests/lib/libc/net/Makefile Mon Jan 6 14:50:32 2014
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.8 2013/08/16 15:29:45 christos Exp $
+# $NetBSD: Makefile,v 1.9 2014/01/06 14:50:32 gson Exp $
.include <bsd.own.mk>
@@ -28,6 +28,7 @@ PROGS+= h_nsd_recurse
PROGS+= h_protoent
PROGS+= h_servent
PROGS+= h_hostent
+PROGS+= h_dns_server
CPPFLAGS.h_hostent.c += -I${NETBSDSRCDIR}/lib/libc/net
# For easy debugging, without installing libc
Index: src/tests/lib/libc/net/t_hostent.sh
diff -u src/tests/lib/libc/net/t_hostent.sh:1.3 src/tests/lib/libc/net/t_hostent.sh:1.4
--- src/tests/lib/libc/net/t_hostent.sh:1.3 Fri Dec 20 20:22:26 2013
+++ src/tests/lib/libc/net/t_hostent.sh Mon Jan 6 14:50:32 2014
@@ -1,4 +1,4 @@
-# $NetBSD: t_hostent.sh,v 1.3 2013/12/20 20:22:26 christos Exp $
+# $NetBSD: t_hostent.sh,v 1.4 2014/01/06 14:50:32 gson Exp $
#
# Copyright (c) 2008 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -41,6 +41,26 @@ l4="localhost"
al4="127.0.0.1"
loc4="name=$l4, length=4, addrtype=2, aliases=[localhost. localhost.localdomain.] addr_list=[$al4]\n"
+# Hijack DNS traffic using a single rump server instance and a DNS
+# server listening on its loopback address. Also hijack file system
+# call to /etc, mapping them to the root file system of the rump
+# server, so that we can control the contents of /etc/resolv.conf.
+
+start_dns_server() {
+ export RUMP_SERVER=unix:///tmp/rumpserver
+ rump_server -lrumpvfs -lrumpdev -lrumpnet \
+ -lrumpnet_net -lrumpnet_netinet -lrumpnet_local \
+ $RUMP_SERVER
+ HIJACK_DNS="LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK='path=/etc,socket=inet:inet6'"
+ eval $HIJACK_DNS sh -c 'echo nameserver 127.0.0.1 >/etc/resolv.conf'
+ eval $HIJACK_DNS $(atf_get_srcdir)/h_dns_server 4
+}
+
+stop_dns_server() {
+ kill $(cat dns_server_4.pid)
+ rump.halt
+}
+
atf_test_case gethostbyname4
gethostbyname4_head()
{
@@ -48,7 +68,9 @@ gethostbyname4_head()
}
gethostbyname4_body()
{
- atf_check -o inline:"$ans4" -x "$(atf_get_srcdir)/h_hostent -t auto -4 $n4"
+ start_dns_server
+ atf_check -o inline:"$ans4" -x "$HIJACK_DNS $(atf_get_srcdir)/h_hostent -t auto -4 $n4"
+ stop_dns_server
}
atf_test_case gethostbyname6
@@ -58,7 +80,9 @@ gethostbyname6_head()
}
gethostbyname6_body()
{
- atf_check -o inline:"$ans6" -x "$(atf_get_srcdir)/h_hostent -t auto -6 $n6"
+ start_dns_server
+ atf_check -o inline:"$ans6" -x "$HIJACK_DNS $(atf_get_srcdir)/h_hostent -t auto -6 $n6"
+ stop_dns_server
}
atf_test_case gethostbyaddr4
@@ -68,7 +92,9 @@ gethostbyaddr4_head()
}
gethostbyaddr4_body()
{
- atf_check -o inline:"$ans4" -x "$(atf_get_srcdir)/h_hostent -t auto -a $a4"
+ start_dns_server
+ atf_check -o inline:"$ans4" -x "$HIJACK_DNS $(atf_get_srcdir)/h_hostent -t auto -a $a4"
+ stop_dns_server
}
atf_test_case gethostbyaddr6
@@ -78,7 +104,9 @@ gethostbyaddr6_head()
}
gethostbyaddr6_body()
{
- atf_check -o inline:"$ans6" -x "$(atf_get_srcdir)/h_hostent -t auto -a $a6"
+ start_dns_server
+ atf_check -o inline:"$ans6" -x "$HIJACK_DNS $(atf_get_srcdir)/h_hostent -t auto -a $a6"
+ stop_dns_server
}
atf_test_case hostsbynamelookup4
@@ -133,7 +161,9 @@ dnsbynamelookup4_head()
dnsbynamelookup4_body()
{
local dir=$(atf_get_srcdir)
- atf_check -o inline:"$ans4" -x "$dir/h_hostent -t dns -4 $n4"
+ start_dns_server
+ atf_check -o inline:"$ans4" -x "$HIJACK_DNS $dir/h_hostent -t dns -4 $n4"
+ stop_dns_server
}
atf_test_case dnsbynamelookup6
@@ -144,7 +174,9 @@ dnsbynamelookup6_head()
dnsbynamelookup6_body()
{
local dir=$(atf_get_srcdir)
- atf_check -o inline:"$ans6" -x "$dir/h_hostent -t dns -6 $n6"
+ start_dns_server
+ atf_check -o inline:"$ans6" -x "$HIJACK_DNS $dir/h_hostent -t dns -6 $n6"
+ stop_dns_server
}
atf_test_case dnsbyaddrlookup4
@@ -155,7 +187,9 @@ dnsbyaddrlookup4_head()
dnsbyaddrlookup4_body()
{
local dir=$(atf_get_srcdir)
- atf_check -o inline:"$ans4" -x "$dir/h_hostent -t dns -4 -a $a4"
+ start_dns_server
+ atf_check -o inline:"$ans4" -x "$HIJACK_DNS $dir/h_hostent -t dns -4 -a $a4"
+ stop_dns_server
}
atf_test_case dnsbyaddrlookup6
@@ -166,7 +200,9 @@ dnsbyaddrlookup6_head()
dnsbyaddrlookup6_body()
{
local dir=$(atf_get_srcdir)
- atf_check -o inline:"$ans6" -x "$dir/h_hostent -t dns -6 -a $a6"
+ start_dns_server
+ atf_check -o inline:"$ans6" -x "$HIJACK_DNS $dir/h_hostent -t dns -6 -a $a6"
+ stop_dns_server
}
atf_init_test_cases()
Added files:
Index: src/tests/lib/libc/net/h_dns_server.c
diff -u /dev/null src/tests/lib/libc/net/h_dns_server.c:1.1
--- /dev/null Mon Jan 6 14:50:33 2014
+++ src/tests/lib/libc/net/h_dns_server.c Mon Jan 6 14:50:32 2014
@@ -0,0 +1,338 @@
+/* $NetBSD: h_dns_server.c,v 1.1 2014/01/06 14:50:32 gson Exp $ */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Andreas Gustafsson.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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.
+ */
+
+/*
+ * A minimal DNS server capable of providing canned answers to the
+ * specific queries issued by t_hostent.sh and nothing more.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: h_dns_server.c,v 1.1 2014/01/06 14:50:32 gson Exp $");
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <memory.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <netinet6/in6.h>
+
+union sockaddr_either {
+ struct sockaddr s;
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+};
+
+/* A DNS question and its corresponding answer */
+
+struct dns_data {
+ size_t qname_size;
+ const char *qname; /* Wire-encode question name */
+ int qtype;
+ size_t answer_size;
+ const char *answer; /* One wire-encoded answer RDATA */
+};
+
+/* Convert C string constant to length + data pair */
+#define STR_DATA(s) sizeof(s) - 1, s
+
+/* Canned DNS queestion-answer pairs */
+struct dns_data data[] = {
+ /* Forward mappings */
+ /* localhost IN A -> 127.0.0.1 */
+ { STR_DATA("\011localhost\000"), 1,
+ STR_DATA("\177\000\000\001") },
+ /* localhost IN AAAA -> ::1 */
+ { STR_DATA("\011localhost\000"), 28,
+ STR_DATA("\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") },
+ /* sixthavenue.astron.com IN A -> 38.117.134.16 */
+ { STR_DATA("\013sixthavenue\006astron\003com\000"), 1,
+ STR_DATA("\046\165\206\020") },
+ /* sixthavenue.astron.com IN AAAA -> 2620:106:3003:1f00:3e4a:92ff:fef4:e180 */
+ { STR_DATA("\013sixthavenue\006astron\003com\000"), 28,
+ STR_DATA("\x26\x20\x01\x06\x30\x03\x1f\x00\x3e\x4a\x92\xff\xfe\xf4\xe1\x80") },
+ /* Reverse mappings */
+ { STR_DATA("\0011\0010\0010\003127\007in-addr\004arpa\000"), 12,
+ STR_DATA("\011localhost\000") },
+ { STR_DATA("\0011\0010\0010\0010\0010\0010\0010\0010"
+ "\0010\0010\0010\0010\0010\0010\0010\0010"
+ "\0010\0010\0010\0010\0010\0010\0010\0010"
+ "\0010\0010\0010\0010\0010\0010\0010\0010"
+ "\003ip6\004arpa\000"), 12,
+ STR_DATA("\011localhost\000") },
+ { STR_DATA("\00216\003134\003117\00238"
+ "\007in-addr\004arpa\000"), 12,
+ STR_DATA("\013sixthavenue\006astron\003com\000") },
+ { STR_DATA("\0010\0018\0011\001e\0014\001f\001e\001f"
+ "\001f\001f\0012\0019\001a\0014\001e\0013"
+ "\0010\0010\001f\0011\0013\0010\0010\0013"
+ "\0016\0010\0011\0010\0010\0012\0016\0012"
+ "\003ip6\004arpa\000"), 12,
+ STR_DATA("\013sixthavenue\006astron\003com\000") },
+ /* End marker */
+ { STR_DATA(""), 0, STR_DATA("") }
+};
+
+/*
+ * Compare two DNS names for equality. If equal, return their
+ * length, and if not, return zero. Does not handle compression.
+ */
+static int
+name_eq(const unsigned char *a, const unsigned char *b) {
+ const unsigned char *a_save = a;
+ for (;;) {
+ int i;
+ int lena = *a++;
+ int lenb = *b++;
+ if (lena != lenb)
+ return 0;
+ if (lena == 0)
+ return a - a_save;
+ for (i = 0; i < lena; i++)
+ if (tolower(a[i]) != tolower(b[i]))
+ return 0;
+ a += lena;
+ b += lena;
+ }
+}
+
+/* XXX the daemon2_* functions should be in a library */
+
+int __deamon2_detach_pipe[2];
+
+static int
+daemon2_fork(void)
+{
+ int r;
+ int fd;
+ int i;
+
+ /*
+ * Set up the pipe, making sure the write end does not
+ * get allocated one of the file descriptors that will
+ * be closed in deamon2_detach().
+ */
+ for (i = 0; i < 3; i++) {
+ r = pipe(__deamon2_detach_pipe);
+ if (r < 0)
+ return -1;
+ if (__deamon2_detach_pipe[1] <= STDERR_FILENO &&
+ (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
+ (void)dup2(fd, __deamon2_detach_pipe[0]);
+ (void)dup2(fd, __deamon2_detach_pipe[1]);
+ if (fd > STDERR_FILENO)
+ (void)close(fd);
+ continue;
+ }
+ break;
+ }
+
+ r = fork();
+ if (r < 0) {
+ return -1;
+ } else if (r == 0) {
+ /* child */
+ close(__deamon2_detach_pipe[0]);
+ return 0;
+ }
+ /* Parent */
+
+ (void) close(__deamon2_detach_pipe[1]);
+
+ for (;;) {
+ char dummy;
+ r = read(__deamon2_detach_pipe[0], &dummy, 1);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+ _exit(1);
+ } else if (r == 0) {
+ _exit(1);
+ } else { /* r > 0 */
+ _exit(0);
+ }
+ }
+}
+
+static int
+deamon2_detach(int nochdir, int noclose)
+{
+ int r;
+ int fd;
+
+ if (setsid() == -1)
+ return -1;
+
+ if (!nochdir)
+ (void)chdir("/");
+
+ if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
+ (void)dup2(fd, STDIN_FILENO);
+ (void)dup2(fd, STDOUT_FILENO);
+ (void)dup2(fd, STDERR_FILENO);
+ if (fd > STDERR_FILENO)
+ (void)close(fd);
+ }
+
+ while (1) {
+ r = write(__deamon2_detach_pipe[1], "", 1);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+ /* May get "broken pipe" here if parent is killed */
+ return -1;
+ } else if (r == 0) {
+ /* Should not happen */
+ return -1;
+ } else {
+ break;
+ }
+ }
+
+ (void) close(__deamon2_detach_pipe[1]);
+
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ int s, r, protocol;
+ union sockaddr_either saddr;
+ struct dns_data *dp;
+ unsigned char *p;
+ char pidfile_name[40];
+ FILE *f;
+ int one = 1;
+
+ daemon2_fork();
+
+ if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
+ errx(1, "usage: dns_server 4 | 6");
+ s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+ if (s < 0)
+ err(1, "socket");
+ if (protocol == '4') {
+ memset(&saddr.sin, 0, sizeof(saddr.sin));
+ saddr.sin.sin_family = AF_INET;
+ saddr.sin.sin_len = sizeof(saddr.sin);
+ saddr.sin.sin_port = htons(53);
+ saddr.sin.sin_addr.s_addr = INADDR_ANY;
+ } else {
+ static struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
+ memset(&saddr.sin6, 0, sizeof(saddr.sin6));
+ saddr.sin6.sin6_family = AF_INET6;
+ saddr.sin6.sin6_len = sizeof(saddr.sin6);
+ saddr.sin6.sin6_port = htons(53);
+ saddr.sin6.sin6_addr = loopback;
+ }
+
+ r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
+ if (r < 0)
+ err(1, "setsockopt");
+
+ r = bind(s,
+ (struct sockaddr *) &saddr,
+ protocol == '4' ? sizeof(struct sockaddr_in) :
+ sizeof(struct sockaddr_in6));
+ if (r < 0)
+ err(1, "bind");
+
+ snprintf(pidfile_name, sizeof pidfile_name,
+ "dns_server_%c.pid", protocol);
+ f = fopen(pidfile_name, "w");
+ fprintf(f, "%d", getpid());
+ fclose(f);
+ deamon2_detach(0, 0);
+
+ for (;;) {
+ unsigned char buf[512];
+ union sockaddr_either from;
+ ssize_t nrecv, nsent;
+ socklen_t fromlen =
+ protocol == '4' ? sizeof(struct sockaddr_in) :
+ sizeof(struct sockaddr_in6);
+ memset(buf, 0, sizeof buf);
+ nrecv = recvfrom(s, buf, sizeof buf, 0, &from.s, &fromlen);
+ if (nrecv < 0)
+ err(1, "recvfrom");
+ if (nrecv < 12)
+ continue; /* Too short */
+ if ((buf[2] & 0x80) != 0)
+ continue; /* Not a query */
+ if (!(buf[4] == 0 && buf[5] == 1))
+ continue; /* QDCOUNT is not 1 */
+
+ for (dp = data; dp->qname_size != 0; dp++) {
+ int qtype, qclass;
+ p = buf + 12; /* Point to QNAME */
+ int n = name_eq(p, (const unsigned char *) dp->qname);
+ if (n == 0)
+ continue; /* Name does not match */
+ p += n; /* Skip QNAME */
+ qtype = *p++ << 8;
+ qtype |= *p++;
+ if (qtype != dp->qtype)
+ continue;
+ qclass = *p++ << 8;
+ qclass |= *p++;
+ if (qclass != 1) /* IN */
+ continue;
+ goto found;
+ }
+ continue;
+ found:
+ buf[2] |= 0x80; /* QR */
+ buf[3] |= 0x80; /* RA */
+ memset(buf + 6, 0, 6); /* Clear ANCOUNT, NSCOUNT, ARCOUNT */
+ buf[7] = 1; /* ANCOUNT */
+ memcpy(p, dp->qname, dp->qname_size);
+ p += dp->qname_size;
+ *p++ = dp->qtype >> 8;
+ *p++ = dp->qtype & 0xFF;
+ *p++ = 0;
+ *p++ = 1; /* IN */
+ memset(p, 0, 4); /* TTL = 0 */
+ p += 4;
+ *p++ = 0; /* RDLENGTH MSB */
+ *p++ = dp->answer_size; /* RDLENGTH LSB */
+ memcpy(p, dp->answer, dp->answer_size);
+ p += dp->answer_size;
+ nsent = sendto(s, buf, p - buf, 0, &from.s, fromlen);
+ if (nsent != p - buf)
+ warn("sendto");
+ }
+}