Module Name:    src
Committed By:   pooka
Date:           Sun Jul 25 21:39:21 UTC 2010

Modified Files:
        src/tests/net/icmp: t_forward.c
Added Files:
        src/tests/net/config: netconfig.c

Log Message:
make interface/routing configuration a bit more generic


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/tests/net/config/netconfig.c
cvs rdiff -u -r1.2 -r1.3 src/tests/net/icmp/t_forward.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/net/icmp/t_forward.c
diff -u src/tests/net/icmp/t_forward.c:1.2 src/tests/net/icmp/t_forward.c:1.3
--- src/tests/net/icmp/t_forward.c:1.2	Sun Jul 18 12:43:22 2010
+++ src/tests/net/icmp/t_forward.c	Sun Jul 25 21:39:21 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_forward.c,v 1.2 2010/07/18 12:43:22 pooka Exp $	*/
+/*	$NetBSD: t_forward.c,v 1.3 2010/07/25 21:39:21 pooka Exp $	*/
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: t_forward.c,v 1.2 2010/07/18 12:43:22 pooka Exp $");
+__RCSID("$NetBSD: t_forward.c,v 1.3 2010/07/25 21:39:21 pooka Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -58,6 +58,7 @@
 #include <unistd.h>
 
 #include "../../h_macros.h"
+#include "../config/netconfig.c"
 
 static void
 configure_interface(const char *busname, const char *addr, const char *mask,
@@ -173,12 +174,14 @@
 	extern int rumpns_ip_defttl;
 	struct sockaddr_in sin;
 	char payload[1024];
+	char ifname[IFNAMSIZ];
 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
 	int nv;
 	int s;
 
-	configure_interface("bus1", "1.0.0.1", "255.255.255.0", "1.0.0.255");
-	configure_routing("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
+	netcfg_rump_makeshmif("bus1", ifname);
+	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0", "1.0.0.255");
+	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
 
 	/* set global ttl to 1 */
 	nv = 1;

Added files:

Index: src/tests/net/config/netconfig.c
diff -u /dev/null src/tests/net/config/netconfig.c:1.1
--- /dev/null	Sun Jul 25 21:39:21 2010
+++ src/tests/net/config/netconfig.c	Sun Jul 25 21:39:20 2010
@@ -0,0 +1,158 @@
+/*	$NetBSD: netconfig.c,v 1.1 2010/07/25 21:39:20 pooka Exp $	*/
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * 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.
+ *
+ * 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.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: netconfig.c,v 1.1 2010/07/25 21:39:20 pooka Exp $");
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+
+#include <net/route.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <string.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include "../../h_macros.h"
+
+static void
+netcfg_rump_makeshmif(const char *busname, char *ifname)
+{
+	int rv, ifnum;
+
+	if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
+		atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
+	}
+	sprintf(ifname, "shmif%d", ifnum);
+}
+
+static void
+netcfg_rump_if(const char *ifname,
+	const char *addr, const char *mask, const char *bcast)
+{
+	struct ifaliasreq ia;
+	struct sockaddr_in *sin;
+	int s, rv;
+
+	s = -1;
+	if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+		atf_tc_fail_errno("if config socket");
+	}
+
+	/* Address */
+	memset(&ia, 0, sizeof(ia));
+	strcpy(ia.ifra_name, ifname);
+	sin = (struct sockaddr_in *)&ia.ifra_addr;
+	sin->sin_family = AF_INET;
+	sin->sin_len = sizeof(struct sockaddr_in);
+	sin->sin_addr.s_addr = inet_addr(addr);
+
+	/* Netmask */
+	sin = (struct sockaddr_in *)&ia.ifra_mask;
+	sin->sin_family = AF_INET;
+	sin->sin_len = sizeof(struct sockaddr_in);
+	sin->sin_addr.s_addr = inet_addr(mask);
+
+	/* Broadcast address */
+	sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
+	sin->sin_family = AF_INET;
+	sin->sin_len = sizeof(struct sockaddr_in);
+	sin->sin_addr.s_addr = inet_addr(bcast);
+
+	rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
+	if (rv) {
+		atf_tc_fail_errno("SIOCAIFADDR");
+	}
+	rump_sys_close(s);
+}
+
+static void __unused
+netcfg_rump_route(const char *dst, const char *mask, const char *gw)
+{
+	size_t len;
+	struct {
+		struct rt_msghdr m_rtm;
+		uint8_t m_space[512];
+	} m_rtmsg;
+#define rtm m_rtmsg.m_rtm
+	uint8_t *bp = m_rtmsg.m_space;
+	struct sockaddr_in sinstore;
+	int s, rv;
+
+	s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
+	if (s == -1) {
+		atf_tc_fail_errno("routing socket");
+	}
+
+	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
+	rtm.rtm_type = RTM_ADD;
+	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
+	rtm.rtm_version = RTM_VERSION;
+	rtm.rtm_seq = 2;
+	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
+
+	/* dst */
+	memset(&sinstore, 0, sizeof(sinstore));
+	sinstore.sin_family = AF_INET;
+	sinstore.sin_len = sizeof(sinstore);
+	sinstore.sin_addr.s_addr = inet_addr(dst);
+	memcpy(bp, &sinstore, sizeof(sinstore));
+	bp += sizeof(sinstore);
+
+	/* gw */
+	memset(&sinstore, 0, sizeof(sinstore));
+	sinstore.sin_family = AF_INET;
+	sinstore.sin_len = sizeof(sinstore);
+	sinstore.sin_addr.s_addr = inet_addr(gw);
+	memcpy(bp, &sinstore, sizeof(sinstore));
+	bp += sizeof(sinstore);
+
+	/* netmask */
+	memset(&sinstore, 0, sizeof(sinstore));
+	sinstore.sin_family = AF_INET;
+	sinstore.sin_len = sizeof(sinstore);
+	sinstore.sin_addr.s_addr = inet_addr(mask);
+	memcpy(bp, &sinstore, sizeof(sinstore));
+	bp += sizeof(sinstore);
+
+	len = bp - (uint8_t *)&m_rtmsg;
+	rtm.rtm_msglen = len;
+
+	rv = rump_sys_write(s, &m_rtmsg, len);
+	if (rv != (int)len) {
+		atf_tc_fail_errno("write routing message");
+	}
+	rump_sys_close(s);
+}

Reply via email to