/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* 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>
#ifdef __RCSID
__RCSID("$NetBSD: mcast.c,v 1.4 2017/02/28 09:23:23 ozaki-r Exp $");
#else
extern const char *__progname;
#define getprogname() __progname
#endif
switch (ai->ai_family) {
case AF_INET:
s4 = (void *)ai->ai_addr;
assert(sizeof(*s4) == ai->ai_addrlen);
m4.imr_multiaddr = s4->sin_addr;
m4.imr_interface.s_addr = htonl(INADDR_ANY);
return setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&m4, sizeof(m4));
case AF_INET6:
s6 = (void *)ai->ai_addr;
/*
* Linux: Does not support the v6 ioctls on v4 mapped
* sockets but it does support the v4 ones and
* it works.
* MacOS/X: Supports the v6 ioctls on v4 mapped sockets,
* but does not work and also does not support
* the v4 ioctls. So no way to make multicasting
* work with mapped addresses.
* NetBSD: Supports both and works for both.
*/
if (bug && IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) {
memcpy(&m4.imr_multiaddr, &s6->sin6_addr.s6_addr[12],
sizeof(m4.imr_multiaddr));
m4.imr_interface.s_addr = htonl(INADDR_ANY);
return setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&m4, sizeof(m4));
}
assert(sizeof(*s6) == ai->ai_addrlen);
memset(&m6, 0, sizeof(m6));
#if 0
ifc = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&ifc, sizeof(ifc)) == -1)
return -1;
ifc = 224;
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&ifc, sizeof(ifc)) == -1)
return -1;
ifc = 1; /* XXX should pick a proper interface */
if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifc,
sizeof(ifc)) == -1)
return -1;
#else
ifc = 0; /* Let pick an appropriate interface */
#endif
m6.ipv6mr_interface = ifc;
m6.ipv6mr_multiaddr = s6->sin6_addr;
return setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&m6, sizeof(m6));
default:
errno = EOPNOTSUPP;
return -1;
}
}
static int
allowv4mapped(int s, struct addrinfo *ai)
{
struct sockaddr_in6 *s6;
int zero = 0;
s = -1;
for (ai = ai0; ai; ai = ai->ai_next) {
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (s == -1) {
lasterrno = errno;
cause = "socket";
continue;
}
if (allowv4mapped(s, ai) == -1) {
cause = "allow v4 mapped";
goto out;
}
if ((*f)(s, ai->ai_addr, ai->ai_addrlen) == -1) {
cause = f == bind ? "bind" : "connect";
goto out;
}
if ((f == bind || f == connector) && addmc(s, ai, bug) == -1) {
cause = "join group";
goto out;
}
*slen = ai->ai_addrlen;
break;
out:
lasterrno = errno;
close(s);
s = -1;
continue;
}
freeaddrinfo(ai0);
if (s == -1)
ERRX(EXIT_FAILURE, "%s (%s)", cause, strerror(lasterrno));
return s;
}
static int
synchronize(const int fd, bool waiter)
{
int syncmsg = 0;
int r;
struct pollfd pfd;
if (waiter) {
pfd.fd = fd;
pfd.events = POLLIN;
/* We use poll to avoid lock up when the peer died unexpectedly */
r = poll(&pfd, 1, 10000);
if (r == -1)
ERRX(EXIT_FAILURE, "poll (%s)", strerror(errno));
if (r == 0)
/* Timed out */
return -1;
switch ((pid = fork())) {
case 0:
receiver(syncfds[0], host, port, n, conn, bug);
return;
case -1:
ERRX(EXIT_FAILURE, "fork (%s)", strerror(errno));
default:
error = sender(syncfds[1], host, port, n, conn, bug);
again:
switch (waitpid(pid, &status, WNOHANG)) {
case -1:
ERRX(EXIT_FAILURE, "wait (%s)", strerror(errno));
case 0:
if (error == 0)
/*
* Receiver is still alive, but we know
* it will exit soon.
*/
goto again;
if (kill(pid, SIGTERM) == -1)
ERRX(EXIT_FAILURE, "kill (%s)",
strerror(errno));
goto again;
default:
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGTERM)
ERRX0(EXIT_FAILURE,
"receiver failed and was killed" \
"by sender");
else
ERRX(EXIT_FAILURE,
"receiver got signaled (%s)",
strsignal(WTERMSIG(status)));
} else if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
ERRX(EXIT_FAILURE,
"receiver exited with status %d",
WEXITSTATUS(status));
} else {
ERRX(EXIT_FAILURE,
"receiver exited with unexpected status %d",
status);
}
break;
}
return;
}
}
#ifndef ATF
int
main(int argc, char *argv[])
{
const char *host, *port;
int c;
size_t n;
bool conn, bug;
host = HOST_V4;
port = PORT_V4;
n = TOTAL;
bug = conn = false;
while ((c = getopt(argc, argv, "46bcdmn:s:")) != -1)
switch (c) {
case '4':
host = HOST_V4;
port = PORT_V4;
break;
case '6':
host = HOST_V6;
port = PORT_V6;
break;
case 'b':
bug = true;
break;
case 'c':
conn = true;
break;
case 'd':
debug++;
break;
case 'm':
host = HOST_V4MAPPED;
port = PORT_V4MAPPED;
break;
case 'n':
n = atoi(optarg);
break;
case 's':
nsleep = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-cdm46] [-n <tot>]"
" [-s <sleep>]",
getprogname());
return 1;
}