1 /*- 2 * Copyright (c) 2010-2011 Juniper Networks, Inc. 3 * All rights reserved. 4 * 5 * This software was developed by Robert N. M. Watson under contract 6 * to Juniper Networks, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * This is a test tool for IP divert sockets. For the time being, it just 34 * exercise creation and binding of sockets, rather than their divert 35 * behaviour. It would be highly desirable to broaden this test tool to 36 * include packet injection and diversion. 37 */ 38 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 42 #include <netinet/in.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 static void 52 ok(const char *test) 53 { 54 55 fprintf(stderr, "%s: OK\n", test); 56 } 57 58 static void 59 fail(const char *test, const char *note) 60 { 61 62 fprintf(stderr, "%s - %s: FAIL (%s)\n", test, note, strerror(errno)); 63 exit(1); 64 } 65 66 static void 67 failx(const char *test, const char *note) 68 { 69 70 fprintf(stderr, "%s - %s: FAIL\n", test, note); 71 exit(1); 72 } 73 74 static int 75 ipdivert_create(const char *test) 76 { 77 int s; 78 79 s = socket(PF_DIVERT, SOCK_RAW, 0); 80 if (s < 0) 81 fail(test, "socket"); 82 return (s); 83 } 84 85 static void 86 ipdivert_close(const char *test, int s) 87 { 88 89 if (close(s) < 0) 90 fail(test, "close"); 91 } 92 93 static void 94 ipdivert_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 116 int 117 main(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