xref: /freebsd/tests/sys/netinet/sendto-IP_MULTICAST_IF.c (revision 0849f1634a70099b90256ceece52a598eeb3280e)
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 <assert.h>
32 #include <err.h>
33 
34 int
main(int argc,char * argv[])35 main(int argc, char *argv[])
36 {
37 	struct sockaddr_in sin = {
38 		.sin_family = AF_INET,
39 		.sin_len = sizeof(struct sockaddr_in),
40 	};
41 	struct in_addr in;
42 	int s, rv;
43 
44 	if (argc < 2)
45 		errx(1, "Usage: %s IPv4-address", argv[0]);
46 
47 	if (inet_pton(AF_INET, argv[1], &in) != 1)
48 		err(1, "inet_pton(%s) failed", argv[1]);
49 
50 	assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
51 	assert(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
52 	assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &in, sizeof(in))
53 	  == 0);
54 	/* RFC 6676 */
55 	assert(inet_pton(AF_INET, "233.252.0.1", &sin.sin_addr) == 1);
56 	sin.sin_port = htons(6676);
57 	rv = sendto(s, &sin, sizeof(sin), 0,
58 	    (struct sockaddr *)&sin, sizeof(sin));
59 	if (rv != sizeof(sin))
60 		err(1, "sendto failed");
61 
62 	return (0);
63 }
64