xref: /freebsd/tests/sys/netinet/sendto-IP_MULTICAST_IF.c (revision 0849f1634a70099b90256ceece52a598eeb3280e)
1*0849f163SGleb Smirnoff /*-
2*0849f163SGleb Smirnoff  * SPDX-License-Identifier: BSD-2-Clause
3*0849f163SGleb Smirnoff  *
4*0849f163SGleb Smirnoff  * Copyright (c) 2025 Gleb Smirnoff <glebius@FreeBSD.org>
5*0849f163SGleb Smirnoff  *
6*0849f163SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
7*0849f163SGleb Smirnoff  * modification, are permitted provided that the following conditions
8*0849f163SGleb Smirnoff  * are met:
9*0849f163SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
10*0849f163SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
11*0849f163SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
12*0849f163SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
13*0849f163SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
14*0849f163SGleb Smirnoff  *
15*0849f163SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*0849f163SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*0849f163SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*0849f163SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*0849f163SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*0849f163SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*0849f163SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*0849f163SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*0849f163SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*0849f163SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*0849f163SGleb Smirnoff  * SUCH DAMAGE.
26*0849f163SGleb Smirnoff  */
27*0849f163SGleb Smirnoff 
28*0849f163SGleb Smirnoff #include <sys/socket.h>
29*0849f163SGleb Smirnoff #include <netinet/in.h>
30*0849f163SGleb Smirnoff #include <arpa/inet.h>
31*0849f163SGleb Smirnoff #include <assert.h>
32*0849f163SGleb Smirnoff #include <err.h>
33*0849f163SGleb Smirnoff 
34*0849f163SGleb Smirnoff int
main(int argc,char * argv[])35*0849f163SGleb Smirnoff main(int argc, char *argv[])
36*0849f163SGleb Smirnoff {
37*0849f163SGleb Smirnoff 	struct sockaddr_in sin = {
38*0849f163SGleb Smirnoff 		.sin_family = AF_INET,
39*0849f163SGleb Smirnoff 		.sin_len = sizeof(struct sockaddr_in),
40*0849f163SGleb Smirnoff 	};
41*0849f163SGleb Smirnoff 	struct in_addr in;
42*0849f163SGleb Smirnoff 	int s, rv;
43*0849f163SGleb Smirnoff 
44*0849f163SGleb Smirnoff 	if (argc < 2)
45*0849f163SGleb Smirnoff 		errx(1, "Usage: %s IPv4-address", argv[0]);
46*0849f163SGleb Smirnoff 
47*0849f163SGleb Smirnoff 	if (inet_pton(AF_INET, argv[1], &in) != 1)
48*0849f163SGleb Smirnoff 		err(1, "inet_pton(%s) failed", argv[1]);
49*0849f163SGleb Smirnoff 
50*0849f163SGleb Smirnoff 	assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
51*0849f163SGleb Smirnoff 	assert(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
52*0849f163SGleb Smirnoff 	assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &in, sizeof(in))
53*0849f163SGleb Smirnoff 	  == 0);
54*0849f163SGleb Smirnoff 	/* RFC 6676 */
55*0849f163SGleb Smirnoff 	assert(inet_pton(AF_INET, "233.252.0.1", &sin.sin_addr) == 1);
56*0849f163SGleb Smirnoff 	sin.sin_port = htons(6676);
57*0849f163SGleb Smirnoff 	rv = sendto(s, &sin, sizeof(sin), 0,
58*0849f163SGleb Smirnoff 	    (struct sockaddr *)&sin, sizeof(sin));
59*0849f163SGleb Smirnoff 	if (rv != sizeof(sin))
60*0849f163SGleb Smirnoff 		err(1, "sendto failed");
61*0849f163SGleb Smirnoff 
62*0849f163SGleb Smirnoff 	return (0);
63*0849f163SGleb Smirnoff }
64