xref: /freebsd/tests/sys/netinet/multicast-receive.c (revision 7c31cdfa408a4ebce7a3e10d29056a15c28bc092)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2025 Gleb Smirnoff <glebius@FreeBSD.org>
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <limits.h>
41 #include <err.h>
42 
43 static in_port_t
44 atop(const char *c)
45 {
46 	unsigned long ul;
47 
48 	errno = 0;
49 	if ((ul = strtol(c, NULL, 10)) < 1 || ul > IPPORT_MAX || errno != 0)
50 		err(1, "can't parse %s", c);
51 
52 	return ((in_port_t)ul);
53 }
54 
55 int
56 main(int argc, char *argv[])
57 {
58 	char buf[IP_MAXPACKET + 1];
59 	struct sockaddr_in sin = {
60 		.sin_family = AF_INET,
61 		.sin_len = sizeof(struct sockaddr_in),
62 	};
63 	socklen_t slen = sizeof(struct sockaddr_in);
64 	struct in_addr maddr, ifaddr;
65 	ssize_t len;
66 	int s, ifindex;
67 	bool index;
68 
69 	if (argc < 4)
70 usage:
71 		errx(1, "Usage: %s (ip_mreq|ip_mreqn|group_req) "
72 		    "IPv4-group port interface", argv[0]);
73 
74 	if (inet_pton(AF_INET, argv[2], &maddr) != 1)
75 		err(1, "inet_pton(%s) failed", argv[2]);
76 	sin.sin_port = htons(atop(argv[3]));
77 	if (inet_pton(AF_INET, argv[4], &ifaddr) == 1)
78 		index = false;
79 	else if ((ifindex = if_nametoindex(argv[4])) > 0)
80 		index = true;
81 	else if (strcmp(argv[4], "0") == 0) {
82 		ifindex = 0;
83 		index = true;
84 	} else
85 		err(1, "if_nametoindex(%s) failed", argv[4]);
86 
87 	assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
88 	assert(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
89 
90 	if (strcmp(argv[1], "ip_mreq") == 0) {
91 		if (index)
92 			errx(1, "ip_mreq doesn't accept index");
93 		struct ip_mreq mreq = {
94 			.imr_multiaddr = maddr,
95 			.imr_interface = ifaddr,
96 		};
97 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
98 		    sizeof(mreq)) != 0)
99 			err(EX_OSERR, "setsockopt");
100 	} else if (strcmp(argv[1], "ip_mreqn") == 0) {
101 		/*
102 		 * ip_mreqn shall be used with index, but for testing
103 		 * purposes accept address too.
104 		 */
105 		struct ip_mreqn mreqn = {
106 			.imr_multiaddr = maddr,
107 			.imr_address = index ? (struct in_addr){ 0 } : ifaddr,
108 			.imr_ifindex = index ? ifindex : 0,
109 		};
110 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn,
111 		    sizeof(mreqn)) != 0)
112 			err(EX_OSERR, "setsockopt");
113 	} else if (strcmp(argv[1], "group_req") == 0) {
114 		if (!index)
115 			errx(1, "group_req expects index");
116 		struct group_req greq = { .gr_interface = ifindex };
117 		struct sockaddr_in *gsa = (struct sockaddr_in *)&greq.gr_group;
118 
119 		gsa->sin_family = AF_INET;
120 		gsa->sin_len = sizeof(struct sockaddr_in);
121 		gsa->sin_addr = maddr;
122 		if (setsockopt(s, IPPROTO_IP, MCAST_JOIN_GROUP, &greq,
123 		    sizeof(greq)) != 0)
124 			err(EX_OSERR, "setsockopt");
125 	} else
126 		goto usage;
127 
128 	assert((len = recvfrom(s, buf, sizeof(buf) - 1, 0,
129 	    (struct sockaddr *)&sin, &slen)) > 0);
130 	buf[len] = '\0';
131 	printf("%s:%u %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
132 
133 	return (0);
134 }
135