TCP/IP Programming Manual
.
imr6.ipv6mr_interface = if_index;
if (setsockopt( sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char *)&imr6, sizeof(imr6)) < 0)
perror("setsockopt: IPV6_JOIN_GROUP error");
The / variable has the following structure:
struct ipv6_mreq {
struct in6_addr ipv6mr_multiaddr; /*IP multicast address of group*/
unsigned int ipv6mr_interface; /*local interface index*/
};
Each multicast group membership is associated with a particular interface. It is possible to join the
same group on multiple interfaces. The ipv6mr_interface variable can be specified with a
value of 0, which allows an application to choose the default multicast interface. Alternatively,
specifying one of the host's local interfaces allows an application to select a particular,
multicast-capable interface. The maximum number of memberships that can be added on a single
socket is subject to the IPV6_MAX_MEMBERSHIPS value, which is defined in the <in6.h> header
file.
Dropping Membership in a Multicast Group
To drop membership in a particular multicast group use the IPV6_LEAVE_GROUP option to the
setsockopt library call (see setsockopt, setsockopt_nw (page 184)):
struct ipv6_mreq imr6;
if (setsockopt( sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &imr6,
sizeof(imr6)) < 0)
perror("setsockopt: IPV6_LEAVE_GROUP error");
The imr6 parameter contains the same structure values used for adding membership.
If multiple sockets request that a node join a particular multicast group, the node remains a member
of that multicast group until the last of those sockets is closed.
To receive multicast datagrams sent to a specific UDP port, the receiving socket must have bound
to that port using the bind library call. More than one process can receive UDP datagrams destined
for the same port if the bind library call (described in Chapter 4) is preceded by a setsockopt
library call that specifies the SO_REUSEPORT option.
Delivery of IP multicast datagrams to SOCK_RAW sockets is determined by the protocol type of the
destination.
Multicast Changes for IPv6 61