xref: /freebsd/tests/sys/netinet/multicast-send.c (revision 36f2eda251713d4d38f41cf269876b069e3897ff)
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 <arpa/inet.h>
31 #include <net/if.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <err.h>
39 
40 static in_port_t
atop(const char * c)41 atop(const char *c)
42 {
43 	unsigned long ul;
44 
45 	errno = 0;
46 	if ((ul = strtol(c, NULL, 10)) < 1 || ul > IPPORT_MAX || errno != 0)
47 		err(1, "can't parse %s", c);
48 
49 	return ((in_port_t)ul);
50 }
51 
52 int
main(int argc,char * argv[])53 main(int argc, char *argv[])
54 {
55 	struct sockaddr_in src = {
56 		.sin_family = AF_INET,
57 		.sin_len = sizeof(struct sockaddr_in),
58 	}, dst = {
59 		.sin_family = AF_INET,
60 		.sin_len = sizeof(struct sockaddr_in),
61 	};
62 	struct ip_mreqn mreqn;
63 	struct in_addr in;
64 	int s;
65 	bool index;
66 
67 	if (argc < 7)
68 		errx(1, "Usage: %s src-IPv4 src-port dst-IPv4 dst-port "
69 		    "interface payload", argv[0]);
70 
71 	if (inet_pton(AF_INET, argv[1], &src.sin_addr) != 1)
72 		err(1, "inet_pton(%s) failed", argv[1]);
73 	src.sin_port = htons(atop(argv[2]));
74 	if (inet_pton(AF_INET, argv[3], &dst.sin_addr) != 1)
75 		err(1, "inet_pton(%s) failed", argv[3]);
76 	dst.sin_port = htons(atop(argv[4]));
77 	if (inet_pton(AF_INET, argv[5], &in) == 1)
78 		index = false;
79 	else if ((mreqn.imr_ifindex = if_nametoindex(argv[5])) > 0)
80 		index = true;
81 	else
82 		err(1, "if_nametoindex(%s) failed", argv[5]);
83 
84 	assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
85 	assert(bind(s, (struct sockaddr *)&src, sizeof(src)) == 0);
86 	if (index)
87 		assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &mreqn,
88 		    sizeof(mreqn)) == 0);
89 	else
90 		assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &in,
91 		    sizeof(in)) == 0);
92 	if (sendto(s, argv[6], strlen(argv[6]) + 1, 0, (struct sockaddr *)&dst,
93 	    sizeof(dst)) != (ssize_t)strlen(argv[6]) + 1)
94 		err(1, "sendto failed");
95 
96 	return (0);
97 }
98