Explain what works, what does not and why.
Provide compatible code so that it compiles on Linux and MacOS/X with -DTEST. We should check more OS's and see if they are broken too.
This commit is contained in:
parent
384bcc25f3
commit
76ec08071f
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: t_mcast.c,v 1.4 2014/10/12 18:56:57 christos Exp $ */
|
||||
/* $NetBSD: t_mcast.c,v 1.5 2014/10/12 19:49:01 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2014 The NetBSD Foundation, Inc.
|
||||
|
@ -29,15 +29,22 @@
|
|||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: t_mcast.c,v 1.4 2014/10/12 18:56:57 christos Exp $");
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: t_mcast.c,v 1.5 2014/10/12 19:49:01 christos Exp $");
|
||||
#else
|
||||
extern const char *__progname;
|
||||
#define getprogname() __progname
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -62,7 +69,7 @@ __RCSID("$NetBSD: t_mcast.c,v 1.4 2014/10/12 18:56:57 christos Exp $");
|
|||
#define SKIPX(ev, msg, ...) errx(ev, msg, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
static int debug;
|
||||
static int debug = 1;
|
||||
|
||||
#define TOTAL 10
|
||||
#define PORT_V4MAPPED "6666"
|
||||
|
@ -90,7 +97,16 @@ addmc(int s, struct addrinfo *ai, bool bug)
|
|||
&m4, sizeof(m4));
|
||||
case AF_INET6:
|
||||
s6 = (void *)ai->ai_addr;
|
||||
// XXX: Linux does not support the v6 ioctls on v4 sockets!
|
||||
/*
|
||||
* Linux: Does not support the v6 ioctls on v4 mapped
|
||||
* sockets but it does support the v4 ones and
|
||||
* it works.
|
||||
* MacOS/X: Dupports 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));
|
||||
|
@ -137,7 +153,8 @@ connector(int fd, const struct sockaddr *sa, socklen_t slen)
|
|||
|
||||
static int
|
||||
getsocket(const char *host, const char *port,
|
||||
int (*f)(int, const struct sockaddr *, socklen_t), bool bug)
|
||||
int (*f)(int, const struct sockaddr *, socklen_t), socklen_t *slen,
|
||||
bool bug)
|
||||
{
|
||||
int e, s;
|
||||
struct addrinfo hints, *ai0, *ai;
|
||||
|
@ -170,6 +187,7 @@ getsocket(const char *host, const char *port,
|
|||
cause = "join group";
|
||||
goto out;
|
||||
}
|
||||
*slen = ai->ai_addrlen;
|
||||
break;
|
||||
out:
|
||||
close(s);
|
||||
|
@ -189,15 +207,16 @@ sender(const char *host, const char *port, size_t n, bool conn, bool bug)
|
|||
ssize_t l;
|
||||
size_t seq;
|
||||
char buf[64];
|
||||
socklen_t slen;
|
||||
|
||||
s = getsocket(host, port, conn ? connect : connector, bug);
|
||||
s = getsocket(host, port, conn ? connect : connector, &slen, bug);
|
||||
for (seq = 0; seq < n; seq++) {
|
||||
time_t t = time(&t);
|
||||
snprintf(buf, sizeof(buf), "%zu: %-24.24s", seq, ctime(&t));
|
||||
if (debug)
|
||||
printf("sending: %s\n", buf);
|
||||
l = conn ? send(s, buf, sizeof(buf), 0) :
|
||||
sendto(s, buf, sizeof(buf), 0, (void *)&ss, ss.ss_len);
|
||||
sendto(s, buf, sizeof(buf), 0, (void *)&ss, slen);
|
||||
if (l == -1)
|
||||
ERRX(EXIT_FAILURE, "send (%s)", strerror(errno));
|
||||
usleep(100);
|
||||
|
@ -214,13 +233,12 @@ receiver(const char *host, const char *port, size_t n, bool conn, bool bug)
|
|||
struct pollfd pfd;
|
||||
socklen_t slen;
|
||||
|
||||
s = getsocket(host, port, conn ? bind : connector, bug);
|
||||
s = getsocket(host, port, conn ? bind : connector, &slen, bug);
|
||||
pfd.fd = s;
|
||||
pfd.events = POLLIN;
|
||||
for (seq = 0; seq < n; seq++) {
|
||||
if (poll(&pfd, 1, 1000) == -1)
|
||||
ERRX(EXIT_FAILURE, "poll (%s)", strerror(errno));
|
||||
slen = ss.ss_len;
|
||||
l = conn ? recv(s, buf, sizeof(buf), 0) :
|
||||
recvfrom(s, buf, sizeof(buf), 0, (void *)&ss, &slen);
|
||||
if (l == -1)
|
||||
|
@ -233,7 +251,10 @@ receiver(const char *host, const char *port, size_t n, bool conn, bool bug)
|
|||
static void
|
||||
run(const char *host, const char *port, size_t n, bool conn, bool bug)
|
||||
{
|
||||
switch (fork()) {
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
switch ((pid = fork())) {
|
||||
case 0:
|
||||
receiver(host, port, n, conn, bug);
|
||||
return;
|
||||
|
@ -242,6 +263,22 @@ run(const char *host, const char *port, size_t n, bool conn, bool bug)
|
|||
default:
|
||||
usleep(100);
|
||||
sender(host, port, n, conn, bug);
|
||||
usleep(100);
|
||||
again:
|
||||
switch (waitpid(pid, &status, WNOHANG)) {
|
||||
case -1:
|
||||
ERRX(EXIT_FAILURE, "wait (%s)", strerror(errno));
|
||||
case 0:
|
||||
if (kill(pid, SIGTERM) == -1)
|
||||
ERRX(EXIT_FAILURE, "kill (%s)",
|
||||
strerror(errno));
|
||||
goto again;
|
||||
default:
|
||||
if (status != 0)
|
||||
ERRX(EXIT_FAILURE, "pid exited with %d",
|
||||
status);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -260,7 +297,7 @@ main(int argc, char *argv[])
|
|||
n = TOTAL;
|
||||
bug = conn = false;
|
||||
|
||||
while ((c = getopt(argc, argv, "46cdmn:")) != -1)
|
||||
while ((c = getopt(argc, argv, "46bcdmn:")) != -1)
|
||||
switch (c) {
|
||||
case '4':
|
||||
host = HOST_V4;
|
||||
|
@ -391,10 +428,16 @@ ATF_TP_ADD_TCS(tp)
|
|||
ATF_TP_ADD_TC(tp, connmappedinet4);
|
||||
ATF_TP_ADD_TC(tp, connmappedbuginet4);
|
||||
ATF_TP_ADD_TC(tp, conninet6);
|
||||
#if 0
|
||||
/*
|
||||
* The receiver does not get any packets on unconnected sockets,
|
||||
* but the ioctl's work. Is my code wrong?
|
||||
*/
|
||||
ATF_TP_ADD_TC(tp, unconninet4);
|
||||
ATF_TP_ADD_TC(tp, unconnmappedinet4);
|
||||
ATF_TP_ADD_TC(tp, unconnmappedbuginet4);
|
||||
ATF_TP_ADD_TC(tp, unconninet6);
|
||||
#endif
|
||||
|
||||
return atf_no_error();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue