Multicast (1) from network programming for Microsoft Windows, second edition .)

Source: Internet
Author: User
Tags htons

 

After reading this, you will know how IGMPv3 is implemented... Multicasting with setsockopt

Originally, the only way to join or leave a multicast group was
Via the setsockopt API. WinSock 2 introduces
Protocol-independent method of multicasting with the wsajoinleaf API (discussed in the next section),
As we will soon see, the setsockopt method is
Much more flexible even though it is more closely tied to the protocol being
Used.

IPv4

There are two socket options that control joining and leaving
Groups: ip_add_membership and ip_drop_membership. The socket option level is ipproto_ip. The input parameter is a struct ip_mreq structure, which is defined

struct ip_mreq {   struct in_addr imr_multiaddr;   /* IP multicast address of group */   struct in_addr imr_interface;   /* local IP address of interface */};

The imr_multiaddr Field
Is the 32-bit IPv4 address of the multicast group in network-byte order and
Imr_interface is the 32-bit IPv4 address
The Local interface on which to join the multicast group (also specified in
Network-byte order). The following code snippet joined strates joining a multicast
Group.

SOCKET      s;SOCKADDR_IN    localif;struct ip_mreq mreq;s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);localif.sin_family = AF_INET;localif.sin_port   = htons(5150);localif.sin_addr.s_addr = htonl(INADDR_ANY);bind(s, (SOCKADDR *)&localif, sizeof(localif));mreq.imr_interface.s_addr = inet_addr("157.124.22.104");mreq.imr_multiaddr.s_addr = inet_addr("234.5.6.7");setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));

Note that the socket shoshould be bound to the wildcard
Address (inaddr_any) before joining the group. In this example, the socket is
Joined to the multicast group 234.5.6.7 on the local interface 157.124.22.104.
Multiple groups may be joined on the same socket on the same or different
Interface.

Once one or more multicast groups are joined, the ip_drop_membership option is used to leave
Particle Group. Again, the struct ip_mreq
Structure is the input parameter. The local interface and multicast group
Drop are the arguments of the structure. For example, given the code sample you
Just saw, the following code drops the multicast group previusly joined:

// Join the group as shown abovemreq.imr_interface.s_addr = inet_addr("157.124.22.104");mreq.imr_multiaddr.s_addr = inet_addr("234.5.6.7");setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq));

Finally, if the application exits or the socket is closed,
Any multicast groups joined by that process or socket are cleaned up.

An IPv4 multicasting sample that uses setsockopt is provided on the companion cd in
Directory IP-SETSOCKOPT.

IPv4 with multicast Sourcing

IP source multicasting is available on systems that support
IGMPv3 protocol and allows a socket to join a multicast group on an interface
While specifying a set of source addresses to accept data from. There are two
Possible modes in which a socket may join a group. The first is the include mode, in which a socket joins a group
Specifying N number of valid source addresses to accept data from. The other
Mode is exclude, in which a socket joins
Group specifying to accept data from anyone failed t
The N source addresses listed. Depending on which mode is used, the socket
Options differ.

To join a multicast group while using the include mode, the socket options are ip_add_source_membership and ip_drop_source_membership. The first step is to add
One or more sources. Both socket options take a struct ip_mreq_source structure, which is defined
As

struct ip_mreq_source {   struct in_addr imr_multiaddr;   /* IP multicast address of group */   struct in_addr imr_sourceaddr;  /* IP address of source          */   struct in_addr imr_interface;   /* local IP address of interface */};

The imr_multiaddr and
Imr_interface fields are the same as in
Struct ip_mreq structure. The new field imr_sourceaddr specifies the 32-bit IP address
The source to accept data from. If there are multiple valid sources, then
Ip_add_source_membership is called again
The same multicast address and interface with the other valid source.
Following code sample joins a multicast group on a local interface with two
Valid sources:

SOCKET                s;SOCKADDR_IN           localif;struct ip_mreq_source mreqsrc;s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);localif.sin_family = AF_INET;localif.sin_port   = htons(5150);localif.sin_addr.s_addr = htonl(INADDR_ANY);bind(s, (SOCKADDR *)&localif, sizeof(localif));mreqsrc.imr_interface.s_addr = inet_addr("157.124.22.104");mreqsrc.imr_multiaddr.s_addr = inet_addr("234.5.6.7");mreqsrc.imr_sourceaddr.s_addr = inet_addr("172.138.104.10");setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *)&mreqsrc, sizeof(mreqsrc));mreqsrc.imr_sourceaddr.s_addr = inet_addr("172.141.87.101");setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,(char *)&mreqsrc, sizeof(mreqsrc));

To remove a source from the include set, the ip_drop_source _ membership is called with
Multicast Group, Local interface, and source to be removed.

To join a multicast group that excludes one or more sources,
Multicast Group is joined with ip_add_membership. Using ip_add_membership to join a group is equivalent
Joining a group in the exclude mode before t
That no one is excluded. data sent to the joined group is accepted regardless
The source. Once the group is joined, then the ip_block_source option is called to exclude
Given source. Again, the struct ip_mreq_source
Structure is the input parameter that specifies the source to block.
Following example joins a group and then excludes a single source:

SOCKET                s;SOCKADDR_IN           localif;struct ip_mreq        mreq;struct ip_mreq_source mreqsrc;s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);localif.sin_family = AF_INET;localif.sin_port   = htons(5150);localif.sin_addr.s_addr = htonl(INADDR_ANY);bind(s, (SOCKADDR *)&localif, sizeof(localif));// Join a group - the filter is EXCLUDE nonemreq.imr_interface.s_addr = inet_addr("157.124.22.104");mreq.imr_multiaddr.s_addr = inet_addr("234.5.6.7");setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq,  sizeof(mreq));mreqsrc.imr_interface = mreq.imr_interface;mreqsrc.imr_multiaddr = mreq.imr_multiaddr;mreqsrc.imr_sourceaddr.s_addr = inet_addr("172.138.104.10");setsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, (char *)&mreqsrc,  sizeof(mreqsrc));

If after some point, the application wishes to accept data from
A source previusly blocked, it may remove that source from the exclude set
Calling setsockopt with ip_unblock_source. A struct ip_mreq_source is the input parameter that
Specifies the source to accept data from.

An IPv4 source multicasting sample that uses setsockopt is provided on the companion cd in
Directory IP-SOURCE.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.