2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/linker.h>
32 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
48 * Regression test to reproduce problems associated with the removal of a
49 * network interface being used by an active multicast socket. This proves
50 * to be somewhat complicated, as we need a multicast-capable synthetic
51 * network device that can be torn down on demand, in order that the test
52 * program can open a multicast socket, join a group on the interface, tear
53 * down the interface, and then close the multicast socket. We use the
54 * if_disc ("discard") synthetic interface for this purpose.
56 * Because potential solutions to this problem require separate handling for
57 * different IP socket types, we actually run the test twice: once for UDP
58 * sockets, and once for raw IP sockets.
62 * XXX: The following hopefully don't conflict with the local configuration.
64 #define MULTICAST_IP "224.100.100.100"
65 #define DISC_IP "192.0.2.100"
66 #define DISC_MASK "255.255.255.0"
67 #define DISC_IFNAME "disc"
68 #define DISC_IFUNIT 100
76 if (kldload("if_disc") < 0) {
81 warn("disc_setup: kldload(if_disc)");
86 s = socket(PF_INET, SOCK_RAW, 0);
88 warn("disc_setup: socket(PF_INET, SOCK_RAW, 0)");
92 bzero(&ifr, sizeof(ifr));
93 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", DISC_IFNAME,
96 if (ioctl(s, SIOCIFCREATE, &ifr) < 0) {
97 warn("disc_setup: ioctl(%s, SIOCIFCREATE)", ifr.ifr_name);
112 s = socket(PF_INET, SOCK_RAW, 0);
114 warn("disc_done: socket(PF_INET, SOCK_RAW, 0)");
118 bzero(&ifr, sizeof(ifr));
119 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", DISC_IFNAME,
122 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
123 warn("disc_done: ioctl(%s, SIOCIFDESTROY)", ifr.ifr_name);
128 * Configure an IP address and netmask on a network interface.
131 ifconfig_inet(char *ifname, int ifunit, char *ip, char *netmask)
133 struct sockaddr_in *sinp;
134 struct ifaliasreq ifra;
137 s = socket(PF_INET, SOCK_RAW, 0);
139 warn("ifconfig_inet: socket(PF_INET, SOCK_RAW, 0)");
143 bzero(&ifra, sizeof(ifra));
144 snprintf(ifra.ifra_name, sizeof(ifra.ifra_name), "%s%d", ifname,
147 sinp = (struct sockaddr_in *)&ifra.ifra_addr;
148 sinp->sin_family = AF_INET;
149 sinp->sin_len = sizeof(ifra.ifra_addr);
150 sinp->sin_addr.s_addr = inet_addr(ip);
152 sinp = (struct sockaddr_in *)&ifra.ifra_mask;
153 sinp->sin_family = AF_INET;
154 sinp->sin_len = sizeof(ifra.ifra_addr);
155 sinp->sin_addr.s_addr = inet_addr(netmask);
157 if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
158 warn("ifconfig_inet: ioctl(%s%d, SIOCAIFADDR, %s)", ifname,
169 multicast_open(int *sockp, int type, const char *type_string)
174 sock = socket(PF_INET, type, 0);
176 warn("multicast_test: socket(PF_INET, %s, 0)", type_string);
180 bzero(&imr, sizeof(imr));
181 imr.imr_multiaddr.s_addr = inet_addr(MULTICAST_IP);
182 imr.imr_interface.s_addr = inet_addr(DISC_IP);
184 if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
186 warn("multicast_test: setsockopt(IPPROTO_IP, "
187 "IP_ADD_MEMBERSHIP, {%s, %s})", MULTICAST_IP, DISC_IP);
197 multicast_close(int udp_socket)
204 test_sock_type(int type, const char *type_string)
208 if (disc_setup() < 0)
211 if (ifconfig_inet(DISC_IFNAME, DISC_IFUNIT, DISC_IP, DISC_MASK) < 0) {
216 if (multicast_open(&sock, type, type_string) < 0) {
222 * Tear down the interface first, then close the multicast socket and
223 * see if we make it to the end of the function.
226 multicast_close(sock);
228 printf("test_sock_type(%s) passed\n", type_string);
234 main(int argc, char *argv[])
237 if (test_sock_type(SOCK_RAW, "SOCK_RAW") < 0)
240 if (test_sock_type(SOCK_DGRAM, "SOCK_DGRAM") < 0)