1/*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2017 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
16 *
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.
27 */
28
29#include "tests.h"
30#include <netinet/in.h>
31
32#if defined IP_ADD_MEMBERSHIP && defined IPV6_ADD_MEMBERSHIP \
33 && defined IPV6_JOIN_ANYCAST && defined HAVE_IF_INDEXTONAME
34
35# include <stdio.h>
36# include <unistd.h>
37# include <sys/socket.h>
38# include <arpa/inet.h>
39# include <net/if.h>
40
41int
42main(void)
43{
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";
47
48	TAIL_ALLOC_OBJECT_CONST_PTR(struct ip_mreq, m4);
49	TAIL_ALLOC_OBJECT_CONST_PTR(struct ipv6_mreq, m6);
50	unsigned int i;
51	int rc;
52
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);
56
57	m6->ipv6mr_interface = if_nametoindex("lo");
58	if (!m6->ipv6mr_interface)
59		perror_msg_and_skip("lo");
60
61	(void) close(0);
62	if (socket(AF_INET, SOCK_DGRAM, 0))
63		perror_msg_and_skip("socket");
64
65	struct {
66		int level;
67		const char *str_level;
68		int optname;
69		const char *str_optname;
70		void *optval;
71		unsigned int optsize;
72	} short_any[] = {
73		{
74			ARG_STR(SOL_IP), ARG_STR(IP_ADD_MEMBERSHIP),
75			m4, sizeof(*m4)
76		},
77		{
78			ARG_STR(SOL_IP), ARG_STR(IP_DROP_MEMBERSHIP),
79			m4, sizeof(*m4)
80		},
81		{
82			ARG_STR(SOL_IPV6), ARG_STR(IPV6_ADD_MEMBERSHIP),
83			m6, sizeof(*m6)
84		},
85		{
86			ARG_STR(SOL_IPV6), ARG_STR(IPV6_DROP_MEMBERSHIP),
87			m6, sizeof(*m6)
88		},
89		{
90			ARG_STR(SOL_IPV6), ARG_STR(IPV6_JOIN_ANYCAST),
91			m6, sizeof(*m6)
92		},
93		{
94			ARG_STR(SOL_IPV6), ARG_STR(IPV6_LEAVE_ANYCAST),
95			m6, sizeof(*m6)
96		}
97	};
98
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,
105		       sprintrc(rc));
106
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,
112		       sprintrc(rc));
113	}
114
115	struct {
116		int optname;
117		const char *str_optname;
118	} long_ip[] = {
119		{ ARG_STR(IP_ADD_MEMBERSHIP) },
120		{ ARG_STR(IP_DROP_MEMBERSHIP) }
121	}, long_ipv6[] = {
122		{ ARG_STR(IPV6_ADD_MEMBERSHIP) },
123		{ ARG_STR(IPV6_DROP_MEMBERSHIP) },
124		{ ARG_STR(IPV6_JOIN_ANYCAST) },
125		{ ARG_STR(IPV6_LEAVE_ANYCAST) }
126	};
127
128	for (i = 0; i < ARRAY_SIZE(long_ip); ++i) {
129		rc = setsockopt(0, SOL_IP, long_ip[i].optname,
130				m4, sizeof(*m4));
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));
136	}
137
138	for (i = 0; i < ARRAY_SIZE(long_ipv6); ++i) {
139		rc = setsockopt(0, SOL_IPV6, long_ipv6[i].optname,
140				m6, sizeof(*m6));
141		printf("setsockopt(0, SOL_IPV6, %s"
142		       ", {inet_pton(AF_INET6, \"%s\", &ipv6mr_multiaddr)"
143		       ", ipv6mr_interface=if_nametoindex(\"lo\")}"
144		       ", %u) = %s\n",
145		       long_ipv6[i].str_optname, multi6addr,
146		       (unsigned) sizeof(*m6), sprintrc(rc));
147	}
148
149	puts("+++ exited with 0 +++");
150	return 0;
151}
152
153#else
154
155SKIP_MAIN_UNDEFINED("IP_ADD_MEMBERSHIP && IPV6_ADD_MEMBERSHIP"
156		    " && IPV6_JOIN_ANYCAST && HAVE_IF_INDEXTONAME")
157
158#endif
159