Configure and use multicast IP technology for Linux networks

Source: Internet
Author: User
Tags htons
Article title: configure and use multicast IP technology for Linux networks. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
For example, the IP address of the local computer is 127.0.0.1. its multicast address is 224.0.0.1. This is defined by RCF 1390. To send IP multicast data, the sender needs to determine a suitable multicast address, which represents a group. IPv4 multicast addresses use class d ip addresses to determine multicast groups. In the Internet, the multicast address ranges from 224.0.0.0 to 234.255.255.255. The important addresses are:
  
224.0.0.1-all multicast hosts in the CIDR block
  
224.0.0.2-all vrouters supporting multicast in the CIDR block
  
224.0.0.4-all DVMRP routers in the CIDR block
  
224.0.0.5-all OSPF routers
  
224.0.0.6-all OSPF assigned routers
  
224.0.0.9-all r00002 routers
  
In the IPv6 address space, 1/256 of the address space is allocated to multicast addresses. A ff (11111111) value indicates that the address is a multicast address. The identifier segment is always set to 0 and retained. When the fourth T id is set to 0, it indicates a permanent multicast address. When the t id is set to 1, it indicates a non-permanent multicast address, which acts as a temporary multicast address.
  
   1. configure Linux to support multicast IP addresses
By default, most Linux releases disable multicast IP support. To use the multicast interface in Linux, you need to re-configure and compile the Linux kernel. The configuration steps are as follows:
  
1. cd/usr/src/linux
  
2. make menuconfig
  
3. Select network options
  
4. select IP: Enable Multicasting IP
  
5. save and exit from menuconfig
  
6. run: make dep; make clean; make bzlmage
  
7. cp/vmlinuz/vdimLz_good
  
8. cparch/i386/boot/zImage/vmlinzz
  
9. cd/etc
  
10. edit lilo. conf and add new kernel options for/vmlinuz_good.
  
11. run li1o
  
After the Linux kernel is compiled, run the following command as a super user:
  
# Router add? Net 224.0.0.0 netmask 224.0.0.0 dev lo
  
Check whether the command is added to the system. run the following command:
  
# Route? EKernel IP routing table
Destination implements wary Genmask Flags MSS Window irtt Iface
10.0.0.0*255.255.255.0 U 0 0 0 eth0
127.0.0.0*255.0.0.0 U 0 0 0 lo
BASE_ADDRESS> MC * 240.0.0.0 U 0 0 0 lo
Default 10.0.0.1 0.0.0.0 UG 0 0 0 eth0
  
The multicast address 224.0.0.1 is displayed. The configuration is successful.
  
   II. use Linux multicast IP to broadcast data
1. First, create a multicast program on the server:
  
Code and explanation of the server program:
  
/** Broadcast. c-An IP multicast server */# include # Include # Include # Include # Include # Include Int port = 6789; int main (void) {int socket_descriptor; struct sockaddr_in address;/* first sets up the interface */socket_descriptor = socket (AF_INET, SOCK_DGRAM, 0 ); if (socket_descriptor =-1) {perror ("Opening socket"); exit (EXIT_FAILURE);}/* initialize IP multicast address */memset (& address, 0, sizeof (address); address. sin_family = AF_INET; address. sin_addr.s_addr = inet_addr ("224.0.0.1"); address. sin_port = htons (port);/* start IP multicast */while (1) {if (sendto (socket_descriptor, "test from broadcast ", sizeof ("test from broadcast"), 0, (struct sockaddr *) & address, sizeof (address) <0) {perror ("sendto "); exit (EXIT_FAILURE);} sleep (2);} exit (EXIT_SUCCESS );}
  
2. create a program multicast IP broadcast for the Linux client
After a broadcasting server is created, a client listening program is required to listen to multicast IP broadcast. the following work must be done in the program:
  
(1) when the multicast party listens, the client sends a notification to the Linux kernel that each specified set of interfaces is added to the multicast IP Broadcast Group.
  
(2) the listener must run the same set of interfaces for different processes on the same Linux computer.
  
(3) compile and configure the Danjiang port to send broadcast information to the same Linux host. This function is used to test the broadcast program and listener program on the same Linux host, which is easy to debug.
  
Client program code and explanation
  
/** Listen. c-An IP multicast client */# include # Include # Include # Include # Include # Include Char * host_name = "224.0.0.1";/* multicast IP address */int port = 6789; int main (void) {struct ip_mreq command; int loop = 1; /* multicast loop */int iter = 0; int sin_len; char message [256]; int socket_descriptor; struct sockaddr_in sin; struct hostent * server_host_name; if (server_host_name = gethostbyname (host_name) = 0) {perror ("gethostbyname"); exit (EXIT_FAILURE);}/* bzero (& sin, sizeof (sin); */memset (& sin, 0, sizeof (sin); sin. sin_family = AF_INET; sin. sin_addr.s_addr = htonl (INADDR_ANY); sin. sin_port = htons (port); if (socket_descriptor = socket (PF_INET, SOCK_DGRAM, 0) =-1) {perror ("socket"); exit (EXIT_FAILURE );} /* before calling bind, set the set interface option to enable multicast IP support */loop = 1; if (setsockopt (socket_descriptor, SOL_SOCKET, SO_REUSEADDR, & loop, sizeof (loop )) <0) {perror ("setsockopt: SO_REUSEADDR"); exit (EXIT_FAILURE );} If (bind (socket_descriptor, (struct sockaddr *) & sin, sizeof (sin) <0) {perror ("bind"); exit (EXIT_FAILURE );} /* Broadcast setting interface on the same host to facilitate testing of multicast IP broadcast on a single development system */loop = 1; if (setsockopt (socket_descriptor, IPPROTO_IP, IP_MULTICAST_LOOP, & loop, sizeof (loop) <0) {perror ("setsockopt: IP_MULTICAST_LOOP"); exit (EXIT_FAILURE);}/* join a Broadcast Group. Further tell the Linux kernel that a specific set of interfaces will accept broadcast data */command. imr_multiaddr.s_addr = inet_addr ("224.0.0.1"); command. imr_interface.s_addr = htonl (INADDR_ANY); if (command. condition =-1) {perror ("224.0.0.1 not a legal multicast address"); exit (EXIT_FAILURE);} if (setsockopt (socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, & command, sizeof (command) <0) {perror ("setsockopt: IP_ADD_MEMBERSHIP");} while (iter ++ <8) {sin_len = sizeof (sin ); if (recvfrom (socket_descriptor, message, 256, 0, (struct sockaddr *) & sin, & sin_len) =-1) {perror ("recvfrom ");} printf ("Response # %-2d from server: % s \ n", iter, message); sleep (2 );} /* exit after receiving 8 broadcasts */if (setsockopt (socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP, & command, sizeof (command) <0) {perror ("setsockopt: IP_DROP_MEMBERSHIP ");} close (socket_descriptor); exit (EXIT_SUCCESS );}
  
3. run the Linux multicast IP program
  
Run the program. open two terminal windows and enter the above source code in each window. In a window, type make to compile the executable files broadcast and listen, and create a Makfiles file by GUN make. GUN make is a tool in Linux that automatically generates and maintains the target program.
  
In one window, execute./broadcast and execute./listen in another window to start the listener. you should see the following output:
  
#. /ListenResponse #1 form sever: test from broadcast Response #2 form sever: test from broadcast Response #3 form sever: test from broadcast Response #4 form sever: test from broadcast Response #5 form sever: test from broadcast Response #6 form sever: test from broadcast Response #7 form sever: test from broadcast Response #8 form sever: test from broadcast #
  
4. Summary
  
Linux multicast IP is an introduction to efficiently sending messages to price processes at the same time. In multicast transmission, data is sent to the receiver's multicast address, instead of the unicast address of each receiver. the sender only sends one copy of data, copy the data from the source node to the target node. Multicast IP addresses are now widely used in online games and video broadcast.

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.