A Tutorial on IP Multicast


Introduction

This tutorial assumes basic familiarity with the socket programming abstraction found in many variants of the UNIX Operating System. This tutorial will illustrate how to use sockets to join an IP multicast group and send and receive data from multicast groups.


Multicast Groups and Addresses

Every IP multicast group has a group address. IP multicast provides only open groups: That is, it is not necessary to be a member of a group in order to send datagrams to the group.

Multicast address are like IP addresses used for single hosts, and is written in the same way: A.B.C.D. Multicast addresses will never clash with host addresses because a portion of the IP address space is specifically reserved for multicast. This reserved range consists of addresses from 224.0.0.0 to 239.255.255.255. However, the multicast addresses from 224.0.0.0 to 224.0.0.255 are reserved for multicast routing information; Application programs should use multicast addresses outside this range.


Time-To-Live (TTL) for Multicast Packets

The IP multicast routing protocol uses the Time To Live (TTL) field of IP datagrams to decide how "far" from a sending host a given multicast packet should be forwarded. The default TTL for multicast datagrams is 1, which will result in multicast packets going only to other hosts on the local network. A setsockopt(2) call may be used to change the TTL:

	unsigned char ttl;

	setsockopt(sock,IPPROTO_IP,IP_MULTICAST_TTL,&ttl,sizeof(ttl));

As the values of the TTL field increase, routers will expand the number of hops they will forward a multicast packet. To provide meaningful scope control, multicast routers enforce the following "thresholds" on forwarding based on the TTL field:

0
restricted to the same host
1
restricted to the same subnet
32
restricted to the same site
64
restricted to the same region
128
restricted to the same continent
255
unrestricted


Sending Multicast Datagrams

Sending a multicast datagram is easy: The sender simply specifies a multicast address as the destination of an ordinary sendto(2) system call.


Receiving Multicast Datagrams

To receive multicast packets, an application must first request that the host join a particular multicast group. This is done using another call to setsockopt(2):

	struct ip_mreq mreq;

	setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));

The definition of struct ip_mreq is as follows:

	struct ip_mreq {
	    struct in_addr imr_multiaddr; /* multicast group to join */
	    struct in_addr imr_interface; /* interface to join on */
	}

For now, imr_interface can be safely set to INADDR_ANY to join on the default multicast network interface.

In addition to the host part of an IP multicast address, there is also a port number, as in TCP or UDP sockets. This port number information is used by the kernel to decide which port on the local machine to route packets to. However, unlike in TCP or UDP, there can be many sockets which receive IP multicast packets off a single local port. Binding the socket to a local port is done with bind(2), and is done in the same manner as binding to a UDP address.

After the bind(2) has been performed and we have used setsockopt(2) to join a multicast group, we are ready to receive. An ordinary recvfrom(2) call may be used to read datagrams off of the receive socket.


An Example of Multicast Programming: "Hello, World!"

Here is an example which illustrates all the facilities described here for using multicast. It consists of two programs: sender and listener. The listener program simply echoes everything it receives to its standard out, and the sender multicasts "hello, world!" to everyone else in the multicast group.