Multicast, ip_multicast_ttl,ip_add_membership,ip_multicast_if,ip_drop_membership

Source: Internet
Author: User
Tags reserved socket switches

Original: http://blog.chinaunix.net/uid-28458801-id-5085099.html


11.3 Multicast

Unicast is used for end-to-end communication between two hosts, and broadcasts are used for data communication on all hosts on the entire LAN for a host. Unicast and broadcast are two extremes, either to communicate with a host, or to communicate with hosts on the entire LAN. In practice, it is often necessary to communicate with a specific set of hosts, rather than all hosts on the entire LAN, which is the purpose of multicasting.

11.3.1 Multicast Concept

Multicast, also known as "multicast", the network of the same service type host is logically grouped, the data sent and received when the data is only in the same group, the other hosts do not join this group can not send and receive the corresponding data.

When broadcasting over a wide area network, the switches and routers in them only replicate and forward data to the host that needs to obtain the data. The host can request to the router to join or exit a group, and routers and switches in the network selectively replicate and transmit data, transmitting data only to hosts within the group. This feature of multicasting allows data to be sent to multiple hosts at once, while ensuring that no other communication is affected by other hosts that do not need to join the group.

Multicast has the following advantages over traditional single-to-one unicast:

Hosts with the same business join the same data stream, sharing the same channel, saving bandwidth and server benefits, with the advantages of broadcasting without the bandwidth required for broadcasting.

The total bandwidth of the server is not limited by client bandwidth. Because the multicast protocol is determined by the receiver's needs to determine whether the traffic is forwarded, the server-side bandwidth is constant, regardless of the number of clients.

As with unicast, multicast is allowed to be transmitted over a wide area network that is the Internet, and broadcasts can only be performed on the same LAN.

The disadvantages of multicast:

Multicast has no error correction mechanism compared to unicast, which is difficult to compensate when an error occurs, but can be implemented in the application layer.

Multicast network support is flawed and requires the support of routers and network protocol stacks.

Multicast applications mainly include online video, online meetings and so on.

Multicast for 11.3.2 Wan

The multicast address is specific, and the D-class address is used for multicasting. The Class D IP address is the multicast IP address, which is the IP address between 224.0.0.0 and 239.255.255.255, and is classified as a local-connection multicast address, a reserved multicast address, and a management rights multicast address 3 class:

Local multicast address: Between 224.0.0.0~224.0.0.255, this is the address reserved for routing protocols and other purposes, and routers do not forward IP packets that fall into this range.

Reserved multicast addresses: Between 224.0.1.0~238.255.255.255, can be used globally (such as the Internet) or network protocols.

Manage rights multicast addresses: Between 239.0.0.0~239.255.255.255, available for internal use by the organization, similar to private IP addresses, not for the Internet, and can limit the multicast range. 11.3.3 Multicast programming is implemented using the setsockopt () function and the getsockopt () function, and the multicast option is the IP layer, with the option values and meanings shown in 11.5.


Table 11.5 Multicast-related options

Options for getsockopt ()/setsockopt ()

Meaning

Ip_multicast_ttl

Set the TTL value for multicast group data

Ip_add_membership

Join the multicast group on the specified interface

Ip_drop_membership

To exit a multicast group

Ip_multicast_if

Get default interface or set interface

Ip_multicast_loop

Prohibit Multicast data loopback

1. Option Ip_multicase_ttl

The option Ip_multicast_ttl allows you to set a time-out TTL that ranges from any value between 0~255, for example:

Click (here) to collapse or open unsigned char ttl=255;
SetSockOpt (s,ipproto_ip,ip_multicast_ttl,&ttl,sizeof (TTL));


2. Option Ip_multicast_if

Option ip_multicast_if is used to set the default default network interface for multicast, which is sent from a given network interface and the other network interface ignores this data. For example:

Click (here) to collapse or open the struct in_addraddr;
SetSockOpt (s,ipproto_ip,ip_multicast_if,&addr,sizeof (addr))


The parameter addr is the IP address of the desired multicast output interface, which is echoed back to the default interface using the Inaddr_any address.

By default, when the native sends multicast data to a network interface, at the IP layer, the data is echoed back to the local loopback interface, and the option Ip_multicast_loop is used to control whether the data is echoed back to the local loopback interface. For example:

Click (here) to collapse or open the unsigned char loop;
SetSockOpt (s,ipproto_ip,ip_multicast_loop,&loop,sizeof (LOOP))


The parameter loop is set to 0 suppress loopback, and set to 1 to allow loopback.

3. Options Ip_add_membership and Ip_drop_membership

Join or exit a multicast group, with options Ip_add_membership and ip_drop_member-ship, to control a variable of struct struct ip_mreq type, the struct Ip_mreq prototype is as follows:

Click (here) to collapse or open the struct ip_mreq
{
struct IN_ADDR imn_multiaddr; /* Broadcast Group IP address joined or exited */
struct IN_ADDR imr_interface; /* Network interface IP address to join or exit */
}


The option ip_add_membership is used to join a broadcast group and can then send data to or receive data from the broadcast group. The value of this option is MREQ structure, member IMN_MULTIADDR is the Broadcast Group IP address to join, member Imr_interface is the network interface IP address that the machine needs to join the broadcast group. For example:

Click (here) to collapse or open the struct ip_mreq mreq;
SetSockOpt (s,ipproto_ip,ip_add_membership,&mreq,sizeof (mreq))


Use the Ip_add_membership option to join only one network interface IP address to a multicast group at a time, but not one multicast group allows only one host IP address to join, you can call the ip_add_membership option multiple times to implement multiple IP addresses to join the same broadcast group, Or join multiple broadcast groups with the same IP address. When Imr_ interface is Inaddr_any, the default multicast interface is selected.

4. Option Ip_drop_membership

The option ip_drop_membership is used to exit from a broadcast group. For example:

Click (here) to collapse or open the struct ip_mreq mreq;
SetSockOpt (s,ipprotp_ip,ip_drop_membership,&mreq,sizeof (sreq))


Where Mreq contains the same value in Ip_add_membership.

5. Framework of multicast programming

For multicast programming, you need to follow a certain programming framework, the basic order of which is shown in Figure 11.6.

(Click to view larger image) Figure 11.6 Programming flow of multicast

The multicast program framework mainly includes socket initialization, setting the multicast timeout time, joining the multicast group, sending data, receiving data, and leaving several aspects from the multicast group. The steps are as follows:

(1) Create a socket.

(2) Then set the multicast parameters, such as the time-out TTL, the local loopback license loop, and so on.

(3) Join the multicast group.

(4) Sending and receiving data.

(5) Leave from the multicast group.

11.3.4 Multicast in the kernel

Multicast in the Linux kernel uses a struct struct ip_mc_socklist to connect the various aspects of multicasting, as shown in Figure 11.7.

(Click to view larger image) Figure 11.7 Multicast kernel structure

Click (here) to collapse or open the struct Inet_sock {
...
__u8 Mc_ttl; /* Multicast ttl*/
...
__u8 ...
Mc_loop:1; /* Multicast loopback settings */
int mc_index; /* Multicast Device serial number */
__be32 mc_addr; /* Multicast address */
struct Ip_mc_socklist *mc_list; /* Multicast Group Array */
...
}

The structure member Mc_ttl is used to control the TTL of multicast;

Struct Member Mc_loop Indicates whether the loopback is valid and is used to control the local sending of multicast data;

The structure member Mc_index is used to denote the serial number of the network device;

Struct member Mc_addr is used to save multicast addresses;

The struct member mc_list the group used to hold the multicast.

1. Structure Ip_mc_socklist

The prototype of the struct member mc_list is the struct ip_mc_socklist, which is defined as follows:

Click (here) to collapse or open the struct ip_mc_socklist
{
struct Ip_mc_socklist *next;
struct IP_MREQN multi;
unsigned int sfmode; /*mcast_{include,exclude}*/
struct Ip_sf_socklist *sflist;
}


The member parameter next points to the next node in the list.

The member parameter multi represents the group information, which is the local interface on which the multicast group is added.

The member parameter, Sfmode, is a filtering mode with a value of Mcast_include or Mcast_exclude, which indicates that only multicast datagrams for those sources listed by Sflist are received, and multicast datagrams for those sources listed by Sflist are not received.

The member parameter sflist is the source list.

2. Structure IP_MREQN

The prototype of the multi member is the struct struct ip_mreqn, which is defined as follows:

Click (here) to collapse or open the struct IP_MREQN
{
struct IN_ADDR imr_multiaddr; /* IP address for multicast group */
struct IN_ADDR imr_address; /* IP address of this address network interface */
int imr_ifindex; /* Network Interface serial number */
}


The two members of the struct are used to specify the group IP address of the joined multicast group, and the IP address of the local interface to which the group is to be added. The command word does not have the function of source filtering, it is equivalent to implementing IGMPV1 Multicast Join service interface.

3. Structure Ip_sf_socklist

The prototype of the member Sflist is the struct struct ip_sf_socklist, which is defined as follows:

Click (here) to collapse or open the struct ip_sf_socklist
{
unsigned int sl_max; /* Maximum capacity of the current SL_ADDR array */
unsigned int sl_count; /* Number of source addresses in the Source address list */
__u32 Sl_addr[0]; /* Source Address List */
}


The member parameter SL_ADDR represents the source address list;

The member parameter Sl_count represents the number of source addresses in the Source address list;

The member parameter Sl_max represents the maximum capacity (indeterminate) of the current sl_addr array.

4. Option Ip_add_membership

Option ip_add_membership is used to add a local IP address to a multicast group, in the kernel its processing process is shown in Figure 11.8, after the application layer calls the function setsockopt () function option Ip_add_membe-rship, The kernel is processed as follows, with the main call to function Ip_mc_join_group ().

(Click to view larger image) Figure 11.8 option Ip_add_membership kernel process

(1) Copy user data as kernel.

(2) Determine whether the broadcast IP address is legitimate.

(3) Find the network interface corresponding to the IP address.

(4) Find out if a multicast address already exists in the multicast list.

(5) Add this multicast address to the list.

(6) Returns the processing value.

5. Option Ip_drop_membership

Option Ip_drop_membership is used to take a local IP address out of a multicast group, in the kernel its processing process is shown in Figure 11.9, after the application layer calls the setsockopt () function option IP_DROP_ Membership, The kernel is processed as follows, with the main call to function Ip_mc_leave_group ().

(Click to view larger image) Figure 11.9 option Ip_drop_membership kernel process

(1) Copy the user data into the kernel.

(2) Find the network interface corresponding to the IP address.

(3) Find out if a multicast address already exists in the multicast list.

(4) Remove this multicast address from the source address.

(5) Remove this address structure from the multicast list.

(6) Returns the processing value.

11.3. Server side of 51 multicast examples

The following is an example of a multicast server. The programming of multicast server is simple, set up a packet socket, select multicast IP address and port, send data directly to this multicast address. Multicast server programming, do not require the server to join the multicast group, you can send data directly to a multicast group.


The following example continues to send data "broadcast TEST data" to the 8888 port of the multicast IP address "224.0.0.88", each time it is sent at an interval of 5s.

Click (here) to collapse or open/*
*broadcast_server.c-Multicast Service Program
*/
#define MCAST_PORT 8888;
#define MCAST_ADDR "224.0.0.88"//* a locally-connected multicast address, the router does not forward */
#define MCAST_DATA "Broadcast TEST data"/* Multi-Advertisement sent *
#define MCAST_INTERVAL 5/* Send interval time */
int main (int argc, CHAR*ARGV)
{
int s;
struct sockaddr_in mcast_addr;
s = socket (af_inet, SOCK_DGRAM, 0); /* Set up sockets */
if (s==-1)
{
Perror ("socket ()");
return-1;
}

memset (&mcast_addr, 0,sizeof (MCAST_ADDR));/* Initialize the IP multicast address to 0*/
mcast_addr.sin_family = af_inet; /* Set Protocol races behavior af*/
Mcast_addr.sin_addr.s_addr= inet_addr (MCAST_ADDR);/* Set Multicast IP address */
Mcast_addr.sin_port = htons (Mcast_port); /* Set the multicast port */

/* Send data to a multicast address */
while (1) {
int n = sendto (s,/* Socket DESCRIPTOR */
Mcast_data,/* data */
sizeof (MCAST_DATA),/* length */
0,
(struct sockaddr*) &mcast_addr,
sizeof (MCAST_ADDR));
if (n< 0)
{
Perror ("SendTo ()");
Return-2;
}

Sleep (Mcast_interval); /* Wait some time */
}

return 0;
}




11.3. Clients with 61 multicast examples

The multicast group has an IP address of 224.0.0.88 and a port of 8888, which is printed when the client receives multicast data.

Clients can accept multicast group data only after they join the multicast group, so the multicast client needs to join the multicast group before receiving data from the multicast group, and then exits the multicast group when it is finished.

Click (here) to collapse or open/*
*broadcast_client.c-Multicast Client
*/
#define MCAST_PORT 8888;
#define MCAST_ADDR "224.0.0.88"/* a locally-connected multicast address, the router does not forward */
#define MCAST_INTERVAL 5/* Send interval time */
#define BUFF_SIZE 256/* Receive buffer size */
int main (int argc, CHAR*ARGV

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.