-
Notifications
You must be signed in to change notification settings - Fork 0
/
joingrp.c
69 lines (63 loc) · 1.87 KB
/
joingrp.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
/*
* Copyright (C) 2005 Stig Venaas <[email protected]>
* $Id:$
*
* Permission to use, copy, modify, and 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.
*/
#include "ssmping.h"
void joingroup(int s, struct sockaddr *grp, uint32_t intface, struct sockaddr *ifaddr) {
int e;
#ifdef MCAST_JOIN_GROUP
int level;
socklen_t addrlen;
struct group_req greq;
#endif
if (grp && ifaddr && ifaddr->sa_family != grp->sa_family) {
fprintf(stderr, "joingroup failed, group and interface must be of same address family\n");
exit(1);
}
switch (grp->sa_family) {
case AF_INET:
#ifdef MCAST_JOIN_GROUP
addrlen = sizeof(struct sockaddr_in);
level = IPPROTO_IP;
#else
{
struct ip_mreq mreq;
memset(&mreq, 0, sizeof(mreq));
mreq.imr_multiaddr = ((struct sockaddr_in *)grp)->sin_addr;
if (ifaddr)
mreq.imr_interface = ((struct sockaddr_in *)ifaddr)->sin_addr;
e = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
}
#endif
break;
case AF_INET6:
#ifdef MCAST_JOIN_GROUP
addrlen = sizeof(struct sockaddr_in6);
level = IPPROTO_IPV6;
#else
{
struct ipv6_mreq mreq6;
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_multiaddr = ((struct sockaddr_in6 *)grp)->sin6_addr;
mreq6.ipv6mr_interface = intface;
e = setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq6, sizeof(mreq6));
}
#endif
break;
default:
fprintf(stderr, "joingroup failed, unsupported address family\n");
exit(1);
}
#ifdef MCAST_JOIN_GROUP
memset(&greq, 0, sizeof(greq));
memcpy(&greq.gr_group, grp, addrlen);
greq.gr_interface = intface;
e = setsockopt(s, level, MCAST_JOIN_GROUP, (char *)&greq, sizeof(greq));
#endif
if (e < 0)
errx("Failed to join multicast group");
}