Multicast IP technology for Linux Networks

Source: Internet
Author: User
Tags htons
The multicast IP technology of Linux Network-Linux Enterprise Application-Linux server application information. The following is a detailed description. Author: Cao jianghua

Multicast broadcast is an important tool used to establish a distributed system, such as online games, ICQ Chat construction, and Remote Video Conferencing Systems. The multicast broadcast program is similar to the UDP program that sends messages to a single Introducer. The difference is that multicast broadcast programs use special multicast IP addresses.

Directory

1. Configure Linux to support multicast IP addresses
Ii. use Linux multicast IP to broadcast data

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.
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.