2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <netinet/in.h>
32 #if defined IP_ADD_MEMBERSHIP && defined IPV6_ADD_MEMBERSHIP \
33 && defined IPV6_JOIN_ANYCAST && defined HAVE_IF_INDEXTONAME
37 # include <sys/socket.h>
38 # include <arpa/inet.h>
44 static const char multi4addr[] = "224.0.0.3";
45 static const char multi6addr[] = "ff01::c";
46 static const char interface[] = "127.0.0.1";
48 TAIL_ALLOC_OBJECT_CONST_PTR(struct ip_mreq, m4);
49 TAIL_ALLOC_OBJECT_CONST_PTR(struct ipv6_mreq, m6);
53 inet_pton(AF_INET, multi4addr, &m4->imr_multiaddr);
54 inet_pton(AF_INET, interface, &m4->imr_interface);
55 inet_pton(AF_INET6, multi6addr, &m6->ipv6mr_multiaddr);
57 m6->ipv6mr_interface = if_nametoindex("lo");
58 if (!m6->ipv6mr_interface)
59 perror_msg_and_skip("lo");
62 if (socket(AF_INET, SOCK_DGRAM, 0))
63 perror_msg_and_skip("socket");
67 const char *str_level;
69 const char *str_optname;
74 ARG_STR(SOL_IP), ARG_STR(IP_ADD_MEMBERSHIP),
78 ARG_STR(SOL_IP), ARG_STR(IP_DROP_MEMBERSHIP),
82 ARG_STR(SOL_IPV6), ARG_STR(IPV6_ADD_MEMBERSHIP),
86 ARG_STR(SOL_IPV6), ARG_STR(IPV6_DROP_MEMBERSHIP),
90 ARG_STR(SOL_IPV6), ARG_STR(IPV6_JOIN_ANYCAST),
94 ARG_STR(SOL_IPV6), ARG_STR(IPV6_LEAVE_ANYCAST),
99 for (i = 0; i < ARRAY_SIZE(short_any); ++i) {
100 rc = setsockopt(0, short_any[i].level, short_any[i].optname,
101 short_any[i].optval, 1);
102 printf("setsockopt(0, %s, %s, \"\\%hho\", 1) = %s\n",
103 short_any[i].str_level, short_any[i].str_optname,
104 *(unsigned char *) short_any[i].optval,
107 rc = setsockopt(0, short_any[i].level, short_any[i].optname,
108 short_any[i].optval + 1, short_any[i].optsize);
109 printf("setsockopt(0, %s, %s, %p, %u) = %s\n",
110 short_any[i].str_level, short_any[i].str_optname,
111 short_any[i].optval + 1, short_any[i].optsize,
117 const char *str_optname;
119 { ARG_STR(IP_ADD_MEMBERSHIP) },
120 { ARG_STR(IP_DROP_MEMBERSHIP) }
122 { ARG_STR(IPV6_ADD_MEMBERSHIP) },
123 { ARG_STR(IPV6_DROP_MEMBERSHIP) },
124 { ARG_STR(IPV6_JOIN_ANYCAST) },
125 { ARG_STR(IPV6_LEAVE_ANYCAST) }
128 for (i = 0; i < ARRAY_SIZE(long_ip); ++i) {
129 rc = setsockopt(0, SOL_IP, long_ip[i].optname,
131 printf("setsockopt(0, SOL_IP, %s"
132 ", {imr_multiaddr=inet_addr(\"%s\")"
133 ", imr_interface=inet_addr(\"%s\")}, %u) = %s\n",
134 long_ip[i].str_optname, multi4addr,
135 interface, (unsigned) sizeof(*m4), sprintrc(rc));
138 for (i = 0; i < ARRAY_SIZE(long_ipv6); ++i) {
139 rc = setsockopt(0, SOL_IPV6, long_ipv6[i].optname,
141 printf("setsockopt(0, SOL_IPV6, %s"
142 ", {inet_pton(AF_INET6, \"%s\", &ipv6mr_multiaddr)"
143 ", ipv6mr_interface=if_nametoindex(\"lo\")}"
145 long_ipv6[i].str_optname, multi6addr,
146 (unsigned) sizeof(*m6), sprintrc(rc));
149 puts("+++ exited with 0 +++");
155 SKIP_MAIN_UNDEFINED("IP_ADD_MEMBERSHIP && IPV6_ADD_MEMBERSHIP"
156 " && IPV6_JOIN_ANYCAST && HAVE_IF_INDEXTONAME")