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/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <arpa/inet.h>
44 * Regression test for multicast sockets and options:
46 * - Check the defaults for ttl, if, and loopback. Make sure they can be set
49 * - Check that adding and removing multicast addresses seems to work.
51 * - Send a test message over loop back multicast and make sure it arrives.
55 * Would be nice to use BPF or if_tap to actually check packet contents and
56 * layout, make sure that the ttl is set right, etc.
58 * Would be nice if attempts to use multicast options on TCP sockets returned
59 * an error, as the docs suggest it might.
63 #define WARN_SUCCESS 0x00000001 /* Set for TCP to warn on success. */
65 #define WARN_SUCCESS 0x00000000
69 * Multicast test address, picked arbitrarily. Will be used with the
72 #define TEST_MADDR "224.100.100.100"
75 * Test that a given IP socket option (optname) has a default value of
76 * 'defaultv', that we can set it to 'modifiedv', and use 'fakev' as a dummy
77 * value that shouldn't be returned at any point during the tests. Perform
78 * the tests on the raw socket, tcp socket, and upd socket passed.
79 * 'optstring' is used in printing warnings and errors as needed.
82 test_u_char(int optname, const char *optstring, u_char defaultv,
83 u_char modifiedv, u_char fakev, const char *socktype, int sock,
91 * Check that we read back the expected default.
96 ret = getsockopt(sock, IPPROTO_IP, optname, &uc, &socklen);
98 err(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s)",
100 if (ret == 0 && (flags & WARN_SUCCESS))
101 warnx("WARN: getsockopt(%s, IPPROTO_IP, %s) returned 0",
102 socktype, optstring);
104 errx(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s) default is "
105 "%d not %d", socktype, optstring, uc, defaultv);
108 * Set to a modifiedv value, read it back and make sure it got there.
111 ret = setsockopt(sock, IPPROTO_IP, optname, &uc, sizeof(uc));
113 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, %s)",
114 socktype, optstring);
115 if (ret == 0 && (flags & WARN_SUCCESS))
116 warnx("WARN: setsockopt(%s, IPPROTO_IP, %s) returned 0",
117 socktype, optstring);
120 socklen = sizeof(uc);
121 ret = getsockopt(sock, IPPROTO_IP, optname, &uc, &socklen);
123 err(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s)",
124 socktype, optstring);
125 if (ret == 0 && (flags & WARN_SUCCESS))
126 warnx("WARN: getsockopt(%s, IPPROTO_IP, %s) returned 0",
127 socktype, optstring);
129 errx(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s) set value is "
130 "%d not %d", socktype, optstring, uc, modifiedv);
134 * test_in_addr() is like test_u_char(), only it runs on a struct in_addr
138 test_in_addr(int optname, const char *optstring, struct in_addr defaultv,
139 struct in_addr modifiedv, struct in_addr fakev, const char *socktype,
147 * Check that we read back the expected default.
150 socklen = sizeof(ia);
152 ret = getsockopt(sock, IPPROTO_IP, optname, &ia, &socklen);
154 err(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s)",
155 socktype, optstring);
156 if (ret == 0 && (flags & WARN_SUCCESS))
157 warnx("WARN: getsockopt(%s, IPPROTO_IP, %s) returned 0",
158 socktype, optstring);
159 if (memcmp(&ia, &defaultv, sizeof(struct in_addr)))
160 errx(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s) default is "
161 "%s not %s", socktype, optstring, inet_ntoa(ia),
162 inet_ntoa(defaultv));
165 * Set to a modifiedv value, read it back and make sure it got there.
168 ret = setsockopt(sock, IPPROTO_IP, optname, &ia, sizeof(ia));
170 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, %s)",
171 socktype, optstring);
172 if (ret == 0 && (flags & WARN_SUCCESS))
173 warnx("WARN: setsockopt(%s, IPPROTO_IP, %s) returned 0",
174 socktype, optstring);
177 socklen = sizeof(ia);
178 ret = getsockopt(sock, IPPROTO_IP, optname, &ia, &socklen);
180 err(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s)",
181 socktype, optstring);
182 if (ret == 0 && (flags & WARN_SUCCESS))
183 warnx("WARN: getsockopt(%s, IPPROTO_IP, %s) returned 0",
184 socktype, optstring);
185 if (memcmp(&ia, &modifiedv, sizeof(struct in_addr)))
186 errx(-1, "FAIL: getsockopt(%s, IPPROTO_IP, %s) set value is "
187 "%s not %s", socktype, optstring, inet_ntoa(ia),
188 inet_ntoa(modifiedv));
192 test_ttl(int raw_sock, int tcp_sock, int udp_sock)
195 test_u_char(IP_MULTICAST_TTL, "IP_MULTICAST_TTL", 1, 2, 243,
196 "raw_sock", raw_sock, 0);
197 test_u_char(IP_MULTICAST_TTL, "IP_MULTICAST_TTL", 1, 2, 243,
198 "tcp_sock", tcp_sock, WARN_SUCCESS);
199 test_u_char(IP_MULTICAST_TTL, "IP_MULTICAST_TTL", 1, 2, 243,
200 "udp_sock", udp_sock, 0);
204 test_loop(int raw_sock, int tcp_sock, int udp_sock)
207 test_u_char(IP_MULTICAST_LOOP, "IP_MULTICAST_LOOP", 1, 0, 243,
208 "raw_sock", raw_sock, 0);
209 test_u_char(IP_MULTICAST_LOOP, "IP_MULTICAST_LOOP", 1, 0, 243,
210 "tcp_sock", tcp_sock, WARN_SUCCESS);
211 test_u_char(IP_MULTICAST_LOOP, "IP_MULTICAST_LOOP", 1, 0, 243,
212 "udp_sock", udp_sock, 0);
216 test_if(int raw_sock, int tcp_sock, int udp_sock)
218 struct in_addr defaultv, modifiedv, fakev;
220 defaultv.s_addr = inet_addr("0.0.0.0");
222 /* Should be valid on all hosts. */
223 modifiedv.s_addr = inet_addr("127.0.0.1");
225 /* Should not happen. */
226 fakev.s_addr = inet_addr("255.255.255.255");
228 test_in_addr(IP_MULTICAST_IF, "IP_MULTICAST_IF", defaultv, modifiedv,
229 fakev, "raw_sock", raw_sock, 0);
230 test_in_addr(IP_MULTICAST_IF, "IP_MULTICAST_IF", defaultv, modifiedv,
231 fakev, "tcp_sock", tcp_sock, WARN_SUCCESS);
232 test_in_addr(IP_MULTICAST_IF, "IP_MULTICAST_IF", defaultv, modifiedv,
233 fakev, "udp_sock", udp_sock, 0);
237 * Add a multicast address to an interface. Warn if appropriate. No query
238 * interface so can't check if it's there directly; instead we have to try
239 * to add it a second time and make sure we get back EADDRINUSE.
242 test_add_multi(int sock, const char *socktype, struct ip_mreq imr,
248 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
251 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
252 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_ADD_MEMBERSHIP "
253 "%s, %s)", socktype, buf, inet_ntoa(imr.imr_interface));
255 if (ret == 0 && (flags & WARN_SUCCESS)) {
256 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
257 warnx("WARN: setsockopt(%s, IPPROTO_IP, IP_ADD_MEMBERSHIP "
258 "%s, %s) returned 0", socktype, buf,
259 inet_ntoa(imr.imr_interface));
262 /* Try to add a second time to make sure it got there. */
263 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr,
266 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
267 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_ADD_MEMBERSHIP "
268 "%s, %s) dup returned 0", socktype, buf,
269 inet_ntoa(imr.imr_interface));
271 if (ret < 0 && errno != EADDRINUSE) {
272 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
273 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_ADD_MEMBERSHIP "
274 "%s, %s)", socktype, buf, inet_ntoa(imr.imr_interface));
279 * Drop a multicast address from an interface. Warn if appropriate. No
280 * query interface so can't check if it's gone directly; instead we have to
281 * try to drop it a second time and make sure we get back EADDRNOTAVAIL.
284 test_drop_multi(int sock, const char *socktype, struct ip_mreq imr,
290 ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &imr,
293 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
294 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_DROP_MEMBERSHIP "
295 "%s, %s)", socktype, buf, inet_ntoa(imr.imr_interface));
297 if (ret == 0 && (flags & WARN_SUCCESS)) {
298 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
299 warnx("WARN: setsockopt(%s, IPPROTO_IP, IP_DROP_MEMBERSHIP "
300 "%s, %s) returned 0", socktype, buf,
301 inet_ntoa(imr.imr_interface));
304 /* Try a second time to make sure it's gone. */
305 ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &imr,
308 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
309 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_DROP_MEMBERSHIP "
310 "%s, %s) returned 0", socktype, buf,
311 inet_ntoa(imr.imr_interface));
313 if (ret < 0 && errno != EADDRNOTAVAIL) {
314 strlcpy(buf, inet_ntoa(imr.imr_multiaddr), 128);
315 err(-1, "FAIL: setsockopt(%s, IPPROTO_IP, IP_DROP_MEMBERSHIP "
316 "%s, %s)", socktype, buf, inet_ntoa(imr.imr_interface));
321 * Should really also test trying to add an invalid address, delete one
322 * that's not there, etc.
325 test_addr(int raw_sock, int tcp_sock, int udp_sock)
330 imr.imr_multiaddr.s_addr = inet_addr(TEST_MADDR);
332 /* Localhost should be OK. */
333 imr.imr_interface.s_addr = inet_addr("127.0.0.1");
335 test_add_multi(raw_sock, "raw_sock", imr, 0);
336 test_drop_multi(raw_sock, "raw_sock", imr, 0);
338 test_add_multi(tcp_sock, "raw_sock", imr, WARN_SUCCESS);
339 test_drop_multi(tcp_sock, "raw_sock", imr, WARN_SUCCESS);
341 test_add_multi(udp_sock, "raw_sock", imr, 0);
342 test_drop_multi(udp_sock, "raw_sock", imr, 0);
346 * Test an actual simple UDP message - send a single byte to an address we're
347 * subscribed to, and hope to get it back. We create a new UDP socket for
348 * this purpose because we will need to bind it.
350 #define UDP_PORT 5012
354 struct sockaddr_in sin;
356 struct in_addr if_addr;
361 sock = socket(PF_INET, SOCK_DGRAM, 0);
363 err(-1, "FAIL: test_udp: socket(PF_INET, SOCK_DGRAM)");
365 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
366 err(-1, "FAIL: test_udp: fcntl(F_SETFL, O_NONBLOCK)");
368 bzero(&sin, sizeof(sin));
369 sin.sin_len = sizeof(sin);
370 sin.sin_family = AF_INET;
371 sin.sin_port = htons(UDP_PORT);
372 sin.sin_addr.s_addr = inet_addr(TEST_MADDR);
374 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
375 err(-1, "FAIL: test_udp: bind(udp_sock, 127.0.0.1:%d",
379 imr.imr_multiaddr.s_addr = inet_addr(TEST_MADDR);
381 /* Localhost should be OK. */
382 imr.imr_interface.s_addr = inet_addr("127.0.0.1");
385 * Tell socket what interface to send on -- use localhost.
387 if_addr.s_addr = inet_addr("127.0.0.1");
388 if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &if_addr,
389 sizeof(if_addr)) < 0)
390 err(-1, "test_udp: setsockopt(IPPROTO_IP, IP_MULTICAST_IF)");
392 test_add_multi(sock, "udp_sock", imr, 0);
394 bzero(&sin, sizeof(sin));
395 sin.sin_len = sizeof(sin);
396 sin.sin_family = AF_INET;
397 sin.sin_port = htons(UDP_PORT);
398 sin.sin_addr.s_addr = inet_addr(TEST_MADDR);
401 len = sizeof(message);
402 len = sendto(sock, &message, len, 0, (struct sockaddr *)&sin,
405 err(-1, "test_udp: sendto");
407 if (len != sizeof(message))
408 errx(-1, "test_udp: sendto: expected to send %d, instead %d",
409 sizeof(message), len);
413 len = recvfrom(sock, &message, sizeof(message), 0,
414 (struct sockaddr *)&sin, &len);
416 err(-1, "test_udp: recvfrom");
418 if (len != sizeof(message))
419 errx(-1, "test_udp: recvfrom: len %d != message len %d",
420 len, sizeof(message));
423 errx(-1, "test_udp: recvfrom: expected 'A', got '%c'",
426 test_drop_multi(sock, "udp_sock", imr, 0);
433 main(int argc, char *argv[])
435 int raw_sock, tcp_sock, udp_sock;
438 errx(-1, "FAIL: root privilege required");
440 raw_sock = socket(PF_INET, SOCK_RAW, 0);
442 err(-1, "FAIL: socket(PF_INET, SOCK_RAW)");
444 tcp_sock = socket(PF_INET, SOCK_STREAM, 0);
446 err(-1, "FAIL: socket(PF_INET, SOCK_STREAM)");
448 udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
450 err(-1, "FAIL: socket(PF_INET, SOCK_DGRAM)");
452 test_ttl(raw_sock, tcp_sock, udp_sock);
453 test_loop(raw_sock, tcp_sock, udp_sock);
454 test_if(raw_sock, tcp_sock, udp_sock);
455 test_addr(raw_sock, tcp_sock, udp_sock);