-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
receiver.c
329 lines (278 loc) · 7.7 KB
/
receiver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Join multicast group(s) and display receive progress
*
* Copyright (C) 2004 David Stevens <dlstevens()us!ibm!com>
* Copyright (C) 2008-2020 Joachim Wiberg <troglobit()gmail!com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <errno.h>
#include <poll.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "mcjoin.h"
int num_joins = 0;
static int alloc_socket(inet_addr_t group)
{
inet_addr_t ina = { 0 };
int sd, val, proto;
ina.ss_family = group.ss_family;
inet_addr_set_port(&ina, inet_addr_get_port(&group));
#ifdef AF_INET6
if (group.ss_family == AF_INET6)
proto = IPPROTO_IPV6;
else
#endif
proto = IPPROTO_IP;
sd = socket(group.ss_family, SOCK_DGRAM, IPPROTO_UDP);
if (sd < 0) {
ERROR("Failed opening socket(): %s", strerror(errno));
return -1;
}
val = 1;
#ifdef SO_REUSEPORT
if (setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)))
ERROR("Failed enabling SO_REUSEPORT: %s", strerror(errno));
#endif
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)))
ERROR("Failed enabling SO_REUSEADDR: %s", strerror(errno));
#ifdef AF_INET6
if (proto == IPPROTO_IPV6) {
if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)))
ERROR("Failed enabling IPV6_RECVPKTINFO: %s", strerror(errno));
#ifdef IPV6_MULTICAST_ALL
val = 0;
if (setsockopt(sd, proto, IPV6_MULTICAST_ALL, &val, sizeof(val)))
ERROR("Failed disabling IP_MULTICAST_ALL: %s", strerror(errno));
#endif
}
#endif
else {
val = 1;
#if defined(IP_PKTINFO) || !defined(IP_RECVDSTADDR)
if (setsockopt(sd, SOL_IP, IP_PKTINFO, &val, sizeof(val)))
ERROR("Failed enabling IP_PKTINFO: %s", strerror(errno));
#elif defined(IP_RECVDSTADDR)
if (setsockopt(sd, proto, IP_RECVDSTADDR, &val, sizeof(val)))
ERROR("Failed enabling IP_RECVDSTADDR: %s", strerror(errno));
#endif
#ifdef IP_MULTICAST_ALL
val = 0;
if (setsockopt(sd, proto, IP_MULTICAST_ALL, &val, sizeof(val)))
ERROR("Failed disabling IP_MULTICAST_ALL: %s", strerror(errno));
#endif
}
if (bind(sd, (struct sockaddr *)&ina, inet_addrlen(&ina))) {
ERROR("Failed binding to socket: %s", strerror(errno));
close(sd);
return -1;
}
return sd;
}
static int join_group(struct gr *sg)
{
struct group_source_req gsr;
struct group_req gr;
char src[INET_ADDRSTR_LEN] = "*";
char grp[INET_ADDRSTR_LEN];
size_t len;
void *arg;
int ifindex;
int sd, op, proto;
/* Index port with id if IP_MULTICAST_ALL fails */
sd = alloc_socket(sg->grp);
if (sd < 0)
return 1;
ifindex = if_nametoindex(iface);
if (!ifindex) {
ERROR("invalid interface: %s", iface);
goto error;
}
DEBUG("Added iface %s, idx %d", iface, ifindex);
#ifdef AF_INET6
if (sg->grp.ss_family == AF_INET6)
proto = IPPROTO_IPV6;
else
#endif
proto = IPPROTO_IP;
if (sg->source) {
gsr.gsr_interface = ifindex;
gsr.gsr_source = sg->src;
gsr.gsr_group = sg->grp;
op = MCAST_JOIN_SOURCE_GROUP;
arg = &gsr;
len = sizeof(gsr);
} else {
gr.gr_interface = ifindex;
gr.gr_group = sg->grp;
op = MCAST_JOIN_GROUP;
arg = &gr;
len = sizeof(gr);
}
if (sg->source)
inet_address(&sg->src, src, sizeof(src));
inet_address(&sg->grp, grp, sizeof(grp));
PRINT("Joining (%s,%s) on %s, ifindex: %d, sd: %d", src, grp, iface, ifindex, sd);
if (setsockopt(sd, proto, op, arg, len)) {
ERROR("Failed %s group (%s,%s) on sd %d ... %d: %s",
src, grp, "joining", sd, errno, strerror(errno));
goto error;
}
sg->sd = sd;
return 0;
error:
close(sd);
return 1;
}
struct in_addr *find_dstaddr(struct msghdr *msgh)
{
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(msgh); cmsg; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
#if defined(IP_PKTINFO) || !defined(IP_RECVDSTADDR)
if (cmsg->cmsg_level == SOL_IP &&
cmsg->cmsg_type == IP_PKTINFO)
return &((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_addr;
#elif defined(IP_RECVDSTADDR)
if (cmsg->cmsg_level == IPPROTO_IP &&
cmsg->cmsg_type == IP_RECVDSTADDR)
return (struct in_addr *)CMSG_DATA(cmsg);
#endif
}
return NULL;
}
struct in6_addr *find_dstaddr6(struct msghdr *msgh)
{
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(msgh); cmsg; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
#if defined(IPV6_PKTINFO)
if (cmsg->cmsg_level == IPPROTO_IPV6 &&
cmsg->cmsg_type == IPV6_PKTINFO)
return &((struct in6_pktinfo *)CMSG_DATA(cmsg))->ipi6_addr;
#endif
}
return NULL;
}
/*
* rcvmsg() wrapper which uses out-of-band info to verify expected
* destination address (multicast group)
*/
static ssize_t recv_mcast(int id)
{
struct sockaddr_storage src;
struct in_addr *dstaddr;
struct in6_addr *dstaddr6;
struct msghdr msgh;
struct iovec iov[1];
char addr[INET6_ADDRSTRLEN];
char cmbuf[0x100];
char buf[BUFSZ];
const char *dst;
size_t seq = 0;
ssize_t bytes;
int pid = 0;
char *ptr;
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
memset(&msgh, 0, sizeof(msgh));
msgh.msg_name = &src;
msgh.msg_namelen = sizeof(src);
msgh.msg_iov = iov;
msgh.msg_iovlen = NELEMS(iov);
msgh.msg_control = cmbuf;
msgh.msg_controllen = sizeof(cmbuf);
bytes = recvmsg(groups[id].sd, &msgh, MSG_DONTWAIT);
if (bytes < 0)
return -1;
dstaddr = find_dstaddr(&msgh);
if (dstaddr)
dst = inet_ntop(AF_INET, dstaddr, addr, sizeof(addr));
#ifdef AF_INET6
else {
dstaddr6 = find_dstaddr6(&msgh);
if (!dstaddr6)
return -1;
dst = inet_ntop(AF_INET6, dstaddr6, addr, sizeof(addr));
}
#endif
buf[bytes] = 0;
ptr = strstr(buf, MAGIC_KEY);
if (ptr)
pid = atoi(ptr + strlen(MAGIC_KEY));
ptr = strstr(buf, SEQ_KEY);
if (ptr)
seq = atoi(ptr + strlen(SEQ_KEY));
DEBUG("Count %5zu, our PID %d, sender PID %d, group %s, seq: %zu, msg: %s",
groups[id].count, getpid(), pid, groups[id].group, seq, buf);
if (strcmp(dst, groups[id].group)) {
ERROR("Packet for group %s received on wrong socket, expected group %s.",
dst, groups[id].group);
return -1;
}
if (groups[id].seq != seq) {
DEBUG("group seq %zu vs seq %zu", groups[id].seq, seq);
groups[id].gaps++;
}
groups[id].seq = seq + 1; /* Next expected sequence number */
groups[id].count++;
groups[id].status[STATUS_POS] = '.'; /* XXX: Use increasing dot size for more hits? */
return 0;
}
int receiver_init(void)
{
size_t i;
timer_init(plotter_show);
for (i = 0; i < group_num; i++) {
if (join_group(&groups[i]))
return 1;
}
return 0;
}
int receiver(int count)
{
struct pollfd pfd[MAX_NUM_GROUPS];
int rc = 0;
size_t i;
for (i = 0; i < group_num; i++) {
pfd[i].fd = groups[i].sd;
pfd[i].events = POLLIN;
pfd[i].revents = 0;
}
while (running && !winchg) {
rc = poll(pfd, group_num, -1);
if (rc <= 0) {
rc = 0;
continue;
}
for (i = 0; i < group_num; i++) {
if (pfd[i].revents)
recv_mcast(i);
}
if (count > 0) {
size_t total = count * group_num;
for (i = 0; i < group_num; i++)
total -= groups[i].count;
if (total <= 0) {
running = 0;
break;
}
}
}
return rc;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/