Linux Network Programming-multicast

Source: Internet
Author: User
Tags joins htons

Overview

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.


IP Multicast (also multiprocessing broadcast or multicast) technology is a TCP/IP network technology that allows one or more hosts (multicast sources) to send a single packet to multiple hosts (at the same time) . Multicasting is one of the 3 basic destination address types for IPV6 packets, multicast is a point-to-multipoint communication, and IPV6 does not use multicast terminology in IPV4, but broadcasts as a special example of multicasting.


Multicast is one of the effective methods to save network bandwidth, which is a point-to-multipoint communication, and the data transmit and receive only in the same packet. in network applications, when a node's signal needs to be transmitted to multiple nodes, whether it is the use of repetitive point-to-point communication, or the use of broadcast mode, will seriously waste network bandwidth, only multicast is the best choice. Multicasting enables one or more multicast sources to send data packets only to a specific multicast group, and only the host that joins the multicast group can receive the packet.


IP multicast applications can be broadly categorized into three categories: Point-to- multipoint applications, multi-point-to- point applications , and multipoint-to-multipoint applications .

1) Point-to-multipoint application refers to the application form of a sender, multiple receivers, which is the most common form of multicast application. Typical applications include : Media broadcast, media push, information caching, event notification, status monitoring, and more.

2) Multi-point-to-point application refers to multiple senders, a recipient of the application form. Typically a two-way request-response application, any one end (multipoint or point) is likely to initiate a request. Typical applications include : Resource discovery, data collection, online auctions, information inquiries, and more.

3) Multi-point-to-multipoint application refers to the application form of multiple senders and multiple receivers. Typically, each recipient can receive data sent by multiple senders, and each sender can send the data to multiple recipients. Typical applications include : Multipoint conferencing, resource synchronization, parallel processing, collaborative processing, distance learning, discussion groups, distributed interactive Simulation (DIS), multiplayer games, and more.


Multicast address

IP multicast traffic must depend on IP multicast addresses, which in IPV4 is a class D IP address ranging from 224.0.0.0 to 239.255.255.255, and is divided into three categories: local link multicast address, reserved multicast address, and administrative rights multicast address.
1) The local link multicast address range in 224.0.0.0~224.0.0.255, which is reserved for routing protocol and other purposes, the router does not forward the IP packets belonging to this range;
2) the reserved multicast address is 224.0.1.0~238.255.255.255 and can be used on a global scale (e.g. Internet) or network protocol;
3) Administrative rights The multicast address is 239.0.0.0~239.255.255.255, which can be used internally by the organization, similar to a private IP address, cannot be used on the Internet, and can limit the multicast range.


Some multicast group addresses are identified by IANA as well-known addresses, and they are also treated as permanent host groups, similar to well-known ports in TCP and UDP. Similarly, these well-known multicast addresses are listed in the RFC's most recent allocation numbers, noting that the groups represented by these multicast addresses are permanent groups and that their group members are not permanent. These addresses are as follows:

224.0.0.1 All Multicast hosts

224.0.0.2 All multicast routers

224.0.0.4 DRMRP Router

224.0.0.5 Routers for all OSPF

224.0.0.6 OSPF Assignment Router

224.0.0.9 RPIV2 Router

224.0.0.10 EIGRP Router

224.0.0.13 PIM Router

224.0.0.22 IGMPv3

224.0.0.25 RGMP

224.0.1.1 NTP Network Time Protocol


mapping of multicast addresses to MAC addresses

All hosts that receive multicast packets using the same IP multicast address constitute a host group, also known as a multicast set . A member of a multicast group is subject to change at any time, a host can join or leave a multicast group at any time, the number of multicast group members and the geographical location is not limited, a host can also belong to several multicast groups.


This we can understand, multicast address is similar to QQ group number, multicast group is equivalent to QQ group, a host is equivalent to the members of the group.


the D-class address of the IPV4 is the multicast address . The IEEE points an Ethernet multicast group address to the IANA to support IP multicasting. The address of the block begins with 01:00:5e, the 25th bit is 0, and the low 23 bits are the low 23 bits of the IPV4 multicast address (class D address). IPV4 the mapping between multicast addresses and MAC addresses:



because the maximum 5bit in the multicast address (Class D address) is ignored during the mapping process, the multicast group corresponding to each Ethernet multicast address is not unique . 32 different multicast group numbers are mapped to an Ethernet address. For example, multicast addresses 224.128.64.32 (hex e0.80.40.20) and 224.0.64.32 (hex e0.00.40.20) are mapped to the same Ethernet address 01:00:5e:00:40:20.


since address mapping is not unique, the device driver or IP layer must filter the datagram. because the NIC may receive multicast data frames that the host does not want to receive, for example, if host 1 joins multicast as 224.128.64.32, host 2 joins the multicast as 224.0.64.32, we want to send information to the multicast group (host 2) where the 224.0.64.32 is located, the data When the network card is passed, 224.128.64.32 (host 1) and 224.0.64.32 (host 2) are receiving data from the multicast group, because their MAC address is 01:00:5e:00:40:20. At this point , if the NIC does not provide enough multicast data frame filtering, the device driver must receive all multicast data frames and then filter them.


Linux Multicast Programming

Set of interface options

int setsockopt (int sockfd, int level,int optname,

const void *optval, socklen_t optlen);



Successful execution returns 0, otherwise returns-1


1) Option Ip_multicast_loop

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:
unsigned int loop = 1;
The parameter loop is set to 0 suppress loopback, and set to 1 to allow loopback.
SetSockOpt (SOCKFD, IPPROTO_IP,

Ip_multicast_loop, &loop, sizeof (LOOP));


2) Options Ip_add_membership and Ip_drop_membership

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

struct IN_ADDR
{

in_addr_t s_addr;

}


struct IP_MREQ
{

struct IN_ADDR imn_multiaddr; Multicast group IP, similar to QQ group number

struct IN_ADDR imr_interface; The IP that will be added to the multicast group, similar to the QQ member number

};


Multicast can only be implemented with UDP or the original IP, not with TCP.


To join a multicast instance:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/ socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h > #include <sys/types.h>int main (int argc, char*argv[]) {int sockfd;//Socket file descriptor struct SOCKADDR_IN local_a Ddr Local address int err =-1; Char group[16] = "224.0.0.88";  Multicast group IP SOCKFD = socket (af_inet, SOCK_DGRAM, 0);  Establish socket if (SOCKFD = =-1) {perror ("socket ()");  return-1;  }//Initialize address memset (&local_addr, 0, sizeof (LOCAL_ADDR));  local_addr.sin_family = af_inet;  LOCAL_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);  Local_addr.sin_port = htons (8000);  Bind Socket Err = bind (SOCKFD, (struct sockaddr*) &local_addr, sizeof (LOCAL_ADDR));  if (Err < 0) {perror ("bind ()");  Return-2;  }//Set loopback permission, control data to allow loopback to local loopback interface int loop = 1;  Err = setsockopt (sockfd,ipproto_ip, Ip_multicast_loop,&loop, sizeof (LOOP)); if (Err < 0) {perror ("sEtsockopt (): Ip_multicast_loop ");  return-3; } struct Ip_mreq mreq; multicast address struct//join multicast group, equivalent to create a QQ group, someone joins this group mreq.imr_multiaddr.s_addr = inet_addr (group); multicast address, similar to QQ group number mreq.imr_interface.s_addr = Htonl (inaddr_any);//Add this machine to a multicast group, similar to someone joining this group//join a multicast Group err = setsockopt (SOCKFD, IPP  ROTO_IP, Ip_add_membership,&mreq, sizeof (mreq));  if (Err < 0) {perror ("setsockopt (): Ip_add_membership");  return-4;  } int times = 0;  int addr_len = 0;  Char buff[256] = {0}; int n = 0;  Loop receives the message of the broadcast Group, 5 times after exiting for (time = 0; times<5; times++) {addr_len = sizeof (LOCAL_ADDR);     memset (buff, 0, sizeof (buff));  Receive Data N = recvfrom (sockfd, buff, sizeof (buff), 0, (struct sockaddr*) &local_addr, &addr_len);  if (n==-1) {perror ("recvfrom ()");  } printf ("Recv%DST message from server:%s\n", times, Buff);   Sleep (2);    }//exit Broadcast Group Err = setsockopt (SOCKFD, ipproto_ip, Ip_drop_membership,&mreq, sizeof (mreq)); Close (SOCKFD);  return 0;  }


Sample test to send information to a multicast group:

 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h > #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h>#  Include <sys/types.h>int main (int argc, char*argv) {int sockfd;//Socket file descriptor struct sockaddr_in dest_addr;//target Ipchar Buf[] = "broadcast TEST DATA"; sockfd = socket (af_inet, SOCK_DGRAM, 0); Establish socket if (s = =-1) {perror ("socket ()"); return-1;}               Initialize Destination IP information memset (&dest_addr, 0, sizeof (DEST_ADDR));d est_addr.sin_family = af_inet; DEST_ADDR.SIN_ADDR.S_ADDR = inet_addr ("224.0.0.88"); Destination address, for multicast address Dest_addr.sin_port = htons (8000); The port of the multicast server is also 8000//send data to the multicast address while (1) {int n = sendto (SOCKFD, buf, strlen (BUF), 0, (struct sockaddr*) &dest_addr, Sizeo      F (dest_addr)), if (n < 0) {perror ("sendto ()"); return-2;} Sleep (1);} return 0;} 

Linux Network Programming-multicast

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.