forked from stripydog/kplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcast.c
450 lines (387 loc) · 13 KB
/
mcast.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* mcast.c
* This file is part of kplex
* Copyright Keith Young 2013 - 2014
* For copying information see the file COPYING distributed with this software
*
* Multicast interfaces
*/
#include "kplex.h"
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#define DEFMCASTQSIZE 64
struct if_mcast {
int fd;
struct sockaddr_storage maddr;
socklen_t asize;
union {
struct ip_mreq ipmr;
struct ipv6_mreq ip6mr;
} mr;
};
/*
* Duplicate multicast specific info
* Args: if_mcast to be duplicated (cast to void *)
* Returns: pointer to new if_mcast (cast to void *)
*/
void *ifdup_mcast(void *ifb)
{
struct if_mcast *oldif,*newif;
oldif = (struct if_mcast *)ifb;
if ((newif = (struct if_mcast *) malloc(sizeof(struct if_mcast)))
== (struct if_mcast *) NULL)
return(NULL);
(void) memcpy(newif, oldif, sizeof(struct if_mcast));
return((void *) newif);
}
void cleanup_mcast(iface_t *ifa)
{
struct if_mcast *ifb = (struct if_mcast *) ifa->info;
if (ifa->direction == IN) {
if (ifb->maddr.ss_family == AF_INET) {
if (setsockopt(ifb->fd,IPPROTO_IP,IP_DROP_MEMBERSHIP,
&ifb->mr.ipmr,sizeof(struct ip_mreq)) < 0)
logerr(errno,"IP_DROP_MEMBERSHIP failed");
} else if (setsockopt(ifb->fd,IPPROTO_IPV6,IPV6_LEAVE_GROUP,
&ifb->mr.ip6mr,sizeof(struct ipv6_mreq)) < 0) {
logerr(errno,"IPV6_LEAVE_GROUP failed");
}
}
/* iomutex should be locked in the cleanup routine */
if (!ifa->pair)
close(ifb->fd);
}
void write_mcast(struct iface *ifa)
{
struct if_mcast *ifb;
senblk_t *sptr;
int data=0;
struct msghdr msgh;
struct iovec iov[2];
ifb = (struct if_mcast *) ifa->info;
msgh.msg_name=(void *)&ifb->maddr;
msgh.msg_namelen=ifb->asize;
msgh.msg_control=NULL;
msgh.msg_controllen=msgh.msg_flags=0;
msgh.msg_iov=iov;
msgh.msg_iovlen=1;
if (ifa->tagflags) {
if ((iov[0].iov_base=malloc(TAGMAX)) == NULL) {
logerr(errno,"Disabing tag output on interface id %u (%s)",
ifa->id,(ifa->name)?ifa->name:"unlabelled");
ifa->tagflags=0;
} else {
msgh.msg_iovlen=2;
data=1;
}
}
for (;;) {
if ((sptr = next_senblk(ifa->q)) == NULL)
break;
if (senfilter(sptr,ifa->ofilter)) {
senblk_free(sptr,ifa->q);
continue;
}
if (ifa->tagflags)
if ((iov[0].iov_len = gettag(ifa,iov[0].iov_base,sptr)) == 0) {
logerr(errno,"Disabing tag output on interface id %u (%s)",
ifa->id,(ifa->name)?ifa->name:"unlabelled");
ifa->tagflags=0;
msgh.msg_iovlen=1;
data=0;
free(iov[0].iov_base);
}
iov[data].iov_base=sptr->data;
iov[data].iov_len=sptr->len;
if (sendmsg(ifb->fd,&msgh,0) < 0)
break;
senblk_free(sptr,ifa->q);
}
if (ifa->tagflags)
free(iov[0].iov_base);
iface_thread_exit(errno);
}
ssize_t read_mcast(iface_t *ifa, char *buf)
{
struct if_mcast *ifm = (struct if_mcast *) ifa->info;
struct sockaddr_storage src;
socklen_t sz = (socklen_t) sizeof(src);
return recvfrom(ifm->fd,(void *)buf,BUFSIZ,0,(struct sockaddr *) &src,&sz);
}
/* Check whether an address is multicast
* Args: pointer to struct sockaddr_storage
* Returns: -1 if address family not INET or INET6
* 0 if not a multicast address
* 2 if an IPv6 link local multicast address
* 3 if an IPv6 interface local multicast address
* 1 otherwise
*/
int is_multicast(struct sockaddr *s)
{
unsigned long addr;
switch (s->sa_family) {
case AF_INET:
addr=ntohl(((struct sockaddr_in *) s)->sin_addr.s_addr);
if ((addr & 0xff000000) == 0xe0000000)
return(2);
if ((addr & 0xf0000000) == 0xe0000000)
return(1);
return(0);
case AF_INET6:
if (((struct sockaddr_in6*) s)->sin6_addr.s6_addr[0] != 0xff)
return(0);
if ((((struct sockaddr_in6*) s)->sin6_addr.s6_addr[1] & 0x0f) == 2)
return(2);
if ((((struct sockaddr_in6*) s)->sin6_addr.s6_addr[1] & 0x0f) == 1)
return(3);
return(1);
default:
return(-1);
}
}
struct iface *init_mcast(struct iface *ifa)
{
struct if_mcast *ifm;
char *ifname;
struct addrinfo hints,*aptr,*abase;
struct ifaddrs *ifap,*ifp;
char *host,*service;
struct servent *svent;
size_t qsize = DEFMCASTQSIZE;
struct kopts *opt;
int ifindex,iffound=0;
int linklocal=0;
int on=1,off=0;
int err;
if ((ifm=malloc(sizeof(struct if_mcast))) == NULL) {
logerr(errno,"Could not allocate memory");
return(NULL);
}
memset(ifm,0,sizeof(struct if_mcast));
ifname=host=service=NULL;
for(opt=ifa->options;opt;opt=opt->next) {
if (!strcasecmp(opt->var,"device"))
ifname=opt->val;
else if (!strcasecmp(opt->var,"group"))
host=opt->val;
else if (!strcasecmp(opt->var,"port"))
service=opt->val;
else if (!strcasecmp(opt->var,"qsize")) {
if (!(qsize=atoi(opt->val))) {
logerr(0,"Invalid queue size specified: %s",opt->val);
return(NULL);
}
} else {
logerr(0,"Unknown interface option %s",opt->var);
return(NULL);
}
}
if (!host) {
logerr(0,"Must specify multicast address for multicast interfaces");
return(NULL);
}
if (!service) {
if ((svent = getservbyname("nmea-0183","udp")) != NULL)
service=svent->s_name;
else
service=DEFPORTSTRING;
}
memset((void *)&hints,0,sizeof(hints));
hints.ai_flags=0;
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_DGRAM;
hints.ai_protocol=IPPROTO_UDP;
if ((err=getaddrinfo(host,service,&hints,&abase))) {
logerr(0,"Lookup failed for address %s/service %s: %s",host,service,gai_strerror(err));
return(NULL);
}
for (aptr=abase;aptr;aptr=aptr->ai_next)
if (aptr->ai_family == AF_INET || aptr->ai_family == AF_INET6)
break;
if (!aptr) {
logerr(0,"No Suitable address found for %s/%s",host,service);
freeaddrinfo(abase);
return(NULL);
}
memcpy(&ifm->maddr,aptr->ai_addr,aptr->ai_addrlen);
ifm->asize=aptr->ai_addrlen;
freeaddrinfo(abase);
if (ifm->maddr.ss_family == AF_INET) {
memcpy(&ifm->mr.ipmr.imr_multiaddr,
&((struct sockaddr_in*) &ifm->maddr)->sin_addr,
sizeof(struct in_addr));
ifm->mr.ipmr.imr_interface.s_addr=INADDR_ANY;
} else if (ifm->maddr.ss_family == AF_INET6) {
memcpy(&ifm->mr.ip6mr.ipv6mr_multiaddr,
&((struct sockaddr_in6 *)&ifm->maddr)->sin6_addr,
sizeof(struct in6_addr));
} else {
logerr(0,"Unsupported address family %d\n",ifm->maddr.ss_family);
return(NULL);
}
/* Now process local bind address */
memset((void *)&hints,0,sizeof(hints));
hints.ai_flags=AI_PASSIVE;
hints.ai_family=ifm->maddr.ss_family;
hints.ai_socktype=SOCK_DGRAM;
hints.ai_protocol=IPPROTO_UDP;
if ((err=getaddrinfo(NULL,service,&hints,&aptr))) {
logerr(0,"Lookup failed for bind addresss: %s",gai_strerror(err));
return(NULL);
}
for (aptr=abase;aptr;aptr=aptr->ai_next)
if (aptr->ai_family == ifm->maddr.ss_family)
break;
if (!aptr) {
logerr(0,"No suitable address found for %s/%s",host,service);
freeaddrinfo(abase);
return(NULL);
}
switch (is_multicast((struct sockaddr *)&ifm->maddr)) {
case 0:
logerr(0,"%s is not a multicast address",host);
return(NULL);
case 1:
break;
case 2:
case 3:
/* 3 is strictly speaking interface local... */
linklocal++;
}
if ((ifm->fd=socket(ifm->maddr.ss_family,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
logerr(errno,"Could not create UDP socket");
return(NULL);
}
if (ifname) {
if (getifaddrs(&ifap) < 0) {
logerr(errno,"Error getting interface info");
return(NULL);
}
for (ifp=ifap;ifp;ifp=ifp->ifa_next) {
if (ifname && strcmp(ifname,ifp->ifa_name))
continue;
iffound++;
if (ifp->ifa_addr->sa_family == ifm->maddr.ss_family)
break;
}
if (!ifp) {
if (iffound)
logerr(0,"Interface %s has no suitable local address",ifname);
else if (ifname)
logerr(0,"No interface %s found",ifname);
return(NULL);
}
if ((ifindex=if_nametoindex(ifname)) == 0) {
logerr(0,"Can't determine interface index for %s",ifname);
return(NULL);
}
if (ifm->maddr.ss_family == AF_INET) {
memcpy(&ifm->mr.ipmr.imr_interface,
&((struct sockaddr_in *)ifp->ifa_addr)->sin_addr,
sizeof(struct in_addr));
} else {
ifm->mr.ip6mr.ipv6mr_interface=ifindex;
if (linklocal)
((struct sockaddr_in6 *)&ifm->maddr)->sin6_scope_id=ifindex;
}
freeifaddrs(ifap);
if (ifa->direction != IN) {
if (ifm->maddr.ss_family==AF_INET) {
if (setsockopt(ifm->fd,IPPROTO_IP,IP_MULTICAST_IF,
&ifm->mr.ipmr.imr_interface,sizeof(ifm->mr.ipmr.imr_interface)) < 0) {
logerr(errno,"Failed to set multicast interface");
return(NULL);
}
} else {
if (setsockopt(ifm->fd,IPPROTO_IPV6,IPV6_MULTICAST_IF,
&ifindex,sizeof(int)) < 0) {
logerr(errno,"Failed to set multicast interface");
return(NULL);
}
}
}
} else {
if (ifm->maddr.ss_family == AF_INET) {
ifm->mr.ipmr.imr_interface.s_addr=INADDR_ANY;
} else {
if (linklocal) {
if (((struct sockaddr_in6 *)&ifm->maddr)->sin6_scope_id == 0) {
logerr(0,"Must specify a device with link local multicast addresses");
return(NULL);
}
ifm->mr.ip6mr.ipv6mr_interface = ((struct sockaddr_in6 *)
&ifm->maddr)->sin6_scope_id;
} else {
ifm->mr.ip6mr.ipv6mr_interface=0;
}
}
}
if (setsockopt(ifm->fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0) {
logerr(errno,"Failed to set SO_REUSEADDR");
return(NULL);
}
#ifdef SO_REUSEPORT
if (setsockopt(ifm->fd,SOL_SOCKET,SO_REUSEPORT,&on,sizeof(on)) < 0) {
logerr(errno,"Failed to set SO_REUSEPORT");
return(NULL);
}
#endif
if (ifa->direction != OUT) {
if (ifm->maddr.ss_family==AF_INET) {
if (setsockopt(ifm->fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&ifm->mr.ipmr,
sizeof(struct ip_mreq)) < 0) {
logerr(errno,"Failed to join multicast group %s",host);
return(NULL);
}
} else {
if (setsockopt(ifm->fd,IPPROTO_IPV6,IPV6_JOIN_GROUP,
&ifm->mr.ip6mr,sizeof(struct ipv6_mreq)) < 0) {
logerr(errno,"Failed to join multicast group %s",host);
return(NULL);
}
}
}
if (ifa->direction == IN) {
if (bind(ifm->fd,aptr->ai_addr,aptr->ai_addrlen) < 0) {
logerr(errno,"Bind failed");
return(NULL);
}
}
if (ifa->direction != IN) {
/* write queue initialization */
if (init_q(ifa, qsize) < 0) {
logerr(errno,"Could not create queue");
return(NULL);
}
}
ifa->write=write_mcast;
ifa->read=do_read;
ifa->readbuf=read_mcast;
ifa->cleanup=cleanup_mcast;
ifa->info = (void *) ifm;
if (ifa->direction == BOTH) {
if (setsockopt(ifm->fd,
(ifm->maddr.ss_family == AF_INET)?IPPROTO_IP:IPPROTO_IPV6,
(ifm->maddr.ss_family == AF_INET)?
IP_MULTICAST_LOOP:IPV6_MULTICAST_LOOP,&off,
sizeof(off)) < 0) {
logerr(errno,"Failed to disable multicast loopback\nDon't use bi-directional interfaces with loopback interface");
return(NULL);
}
if ((ifa->next=ifdup(ifa)) == NULL) {
logerr(0,"Interface duplication failed");
return(NULL);
}
ifa->direction=OUT;
ifa->pair->direction=IN;
ifm = (struct if_mcast *) ifa->pair->info;
if (bind(ifm->fd,aptr->ai_addr,aptr->ai_addrlen) < 0){
logerr(errno,"Duplicate Bind failed");
return(NULL);
}
}
freeaddrinfo(abase);
free_options(ifa->options);
return(ifa);
}