168349Sobrien/*-
268349Sobrien * Copyright (c) 2010-2011 Juniper Networks, Inc.
3290152Sdelphij * All rights reserved.
468349Sobrien *
568349Sobrien * This software was developed by Robert N. M. Watson under contract
668349Sobrien * to Juniper Networks, Inc.
768349Sobrien *
868349Sobrien * Redistribution and use in source and binary forms, with or without
968349Sobrien * modification, are permitted provided that the following conditions
1068349Sobrien * are met:
1168349Sobrien * 1. Redistributions of source code must retain the above copyright
1268349Sobrien *    notice, this list of conditions and the following disclaimer.
1368349Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1468349Sobrien *    notice, this list of conditions and the following disclaimer in the
1568349Sobrien *    documentation and/or other materials provided with the distribution.
1668349Sobrien *
17186690Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1868349Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19186690Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2068349Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21186690Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22159764Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2368349Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24159764Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25159764Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26159764Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2768349Sobrien * SUCH DAMAGE.
2868349Sobrien *
2968349Sobrien * $FreeBSD$
3068349Sobrien */
3168349Sobrien
3268349Sobrien/*
33159764Sobrien * This is a test tool for IP divert sockets.  For the time being, it just
3468349Sobrien * exercise creation and binding of sockets, rather than their divert
3568349Sobrien * behaviour.  It would be highly desirable to broaden this test tool to
36159764Sobrien * include packet injection and diversion.
37159764Sobrien */
38159764Sobrien
39159764Sobrien#include <sys/types.h>
40159764Sobrien#include <sys/socket.h>
41159764Sobrien
42159764Sobrien#include <netinet/in.h>
43159764Sobrien
44159764Sobrien#include <err.h>
45159764Sobrien#include <errno.h>
4668349Sobrien#include <stdio.h>
4768349Sobrien#include <stdlib.h>
48133359Sobrien#include <string.h>
49133359Sobrien#include <unistd.h>
50159764Sobrien
51159764Sobrienstatic void
52159764Sobrienok(const char *test)
53159764Sobrien{
54159764Sobrien
55226048Sobrien	fprintf(stderr, "%s: OK\n", test);
56226048Sobrien}
57226048Sobrien
58226048Sobrienstatic void
59226048Sobrienfail(const char *test, const char *note)
60226048Sobrien{
61226048Sobrien
62159764Sobrien	fprintf(stderr, "%s - %s: FAIL (%s)\n", test, note, strerror(errno));
63159764Sobrien	exit(1);
64159764Sobrien}
65290152Sdelphij
66159764Sobrienstatic void
67159764Sobrienfailx(const char *test, const char *note)
68159764Sobrien{
69159764Sobrien
70159764Sobrien	fprintf(stderr, "%s - %s: FAIL\n", test, note);
71159764Sobrien	exit(1);
72159764Sobrien}
73290152Sdelphij
74static int
75ipdivert_create(const char *test)
76{
77	int s;
78
79	s = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
80	if (s < 0)
81		fail(test, "socket");
82	return (s);
83}
84
85static void
86ipdivert_close(const char *test, int s)
87{
88
89	if (close(s) < 0)
90		fail(test, "close");
91}
92
93static void
94ipdivert_bind(const char *test, int s, u_short port, int expect)
95{
96	struct sockaddr_in sin;
97	int err;
98
99	bzero(&sin, sizeof(sin));
100	sin.sin_family = AF_INET;
101	sin.sin_addr.s_addr = htonl(INADDR_ANY);
102	sin.sin_port = htons(port);
103
104	err = bind(s, (struct sockaddr *)&sin, sizeof(sin));
105	if (err < 0) {
106		if (expect == 0)
107			fail(test, "bind");
108		if (errno != expect)
109			fail(test, "bind");
110	} else {
111		if (expect != 0)
112			failx(test, "bind");
113	}
114}
115
116int
117main(int argc, char *argv[])
118{
119	const char *test;
120	int s1, s2;
121
122	/*
123	 * First test: create and close an IP divert socket.
124	 */
125	test = "create_close";
126	s1 = ipdivert_create(test);
127	ipdivert_close(test, s1);
128	ok(test);
129
130	/*
131	 * Second test: create, bind, and close an IP divert socket.
132	 */
133	test = "create_bind_close";
134	s1 = ipdivert_create(test);
135	ipdivert_bind(test, s1, 1000, 0);
136	ipdivert_close(test, s1);
137	ok(test);
138
139	/*
140	 * Third test: create two sockets, bind to different ports, and close.
141	 * This should succeed due to non-conflict on the port numbers.
142	 */
143	test = "create2_bind2_close2";
144	s1 = ipdivert_create(test);
145	s2 = ipdivert_create(test);
146	ipdivert_bind(test, s1, 1000, 0);
147	ipdivert_bind(test, s2, 1001, 0);
148	ipdivert_close(test, s1);
149	ipdivert_close(test, s2);
150	ok(test);
151
152	/*
153	 * Fourth test: create two sockets, bind to the *same* port, and
154	 * close.  This should fail due to conflicting port numbers.
155	 */
156	test = "create2_bind2_conflict_close2";
157	s1 = ipdivert_create(test);
158	s2 = ipdivert_create(test);
159	ipdivert_bind(test, s1, 1000, 0);
160	ipdivert_bind(test, s2, 1000, EADDRINUSE);
161	ipdivert_close(test, s1);
162	ipdivert_close(test, s2);
163	ok(test);
164
165	return (0);
166}
167