Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netinet/multicast-receive.c
39483 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2025 Gleb Smirnoff <[email protected]>
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 <limits.h>
40
#include <err.h>
41
42
static in_port_t
43
atop(const char *c)
44
{
45
unsigned long ul;
46
47
errno = 0;
48
if ((ul = strtol(c, NULL, 10)) < 1 || ul > IPPORT_MAX || errno != 0)
49
err(1, "can't parse %s", c);
50
51
return ((in_port_t)ul);
52
}
53
54
int
55
main(int argc, char *argv[])
56
{
57
char buf[IP_MAXPACKET + 1];
58
struct sockaddr_in sin = {
59
.sin_family = AF_INET,
60
.sin_len = sizeof(struct sockaddr_in),
61
};
62
socklen_t slen = sizeof(struct sockaddr_in);
63
struct in_addr maddr, ifaddr;
64
ssize_t len;
65
int s, ifindex;
66
bool index;
67
68
if (argc < 4)
69
usage:
70
errx(1, "Usage: %s (ip_mreq|ip_mreqn|group_req) "
71
"IPv4-group port interface", argv[0]);
72
73
if (inet_pton(AF_INET, argv[2], &maddr) != 1)
74
err(1, "inet_pton(%s) failed", argv[2]);
75
sin.sin_port = htons(atop(argv[3]));
76
if (inet_pton(AF_INET, argv[4], &ifaddr) == 1)
77
index = false;
78
else if ((ifindex = if_nametoindex(argv[4])) > 0)
79
index = true;
80
else if (strcmp(argv[4], "0") == 0) {
81
ifindex = 0;
82
index = true;
83
} else
84
err(1, "if_nametoindex(%s) failed", argv[4]);
85
86
assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
87
assert(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
88
89
if (strcmp(argv[1], "ip_mreq") == 0) {
90
if (index)
91
errx(1, "ip_mreq doesn't accept index");
92
struct ip_mreq mreq = {
93
.imr_multiaddr = maddr,
94
.imr_interface = ifaddr,
95
};
96
assert(setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
97
sizeof(mreq)) == 0);
98
} else if (strcmp(argv[1], "ip_mreqn") == 0) {
99
/*
100
* ip_mreqn shall be used with index, but for testing
101
* purposes accept address too.
102
*/
103
struct ip_mreqn mreqn = {
104
.imr_multiaddr = maddr,
105
.imr_address = index ? (struct in_addr){ 0 } : ifaddr,
106
.imr_ifindex = index ? ifindex : 0,
107
};
108
assert(setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn,
109
sizeof(mreqn)) == 0);
110
} else if (strcmp(argv[1], "group_req") == 0) {
111
if (!index)
112
errx(1, "group_req expects index");
113
struct group_req greq = { .gr_interface = ifindex };
114
struct sockaddr_in *gsa = (struct sockaddr_in *)&greq.gr_group;
115
116
gsa->sin_family = AF_INET;
117
gsa->sin_len = sizeof(struct sockaddr_in);
118
gsa->sin_addr = maddr;
119
assert(setsockopt(s, IPPROTO_IP, MCAST_JOIN_GROUP, &greq,
120
sizeof(greq)) == 0);
121
} else
122
goto usage;
123
124
assert((len = recvfrom(s, buf, sizeof(buf) - 1, 0,
125
(struct sockaddr *)&sin, &slen)) > 0);
126
buf[len] = '\0';
127
printf("%s:%u %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
128
129
return (0);
130
}
131
132