Multicast: From Theory to Practice

Broadcasting over the Internet—a look at developing applications for this new technology.
Receiving Traffic

Your radio or TV must be tuned to receive the channel you want to hear. In a similar way, you must “tune” your kernel so that it knows which multicast groups are of interest. This is known as “subscribing” the host to a particular group. Note it is the host, not the process, that is subscribed. Processes are bound using bind to a particular multicast group/port pair and must tell the kernel they want to receive traffic for that group. The kernel then knows it must not drop packets for that group. When it receives them, it makes copies for all processes bound to that multicast address and port pair. When the last process that remains subscribed to the group “drops membership”, the kernel stops sending these packets to the upper layer protocols and ignores them again.

In short, if you want to receive traffic from a multicast group, you must take the following steps:

  • Create the socket (lines 71 to 74).

  • Bind the group/port (lines 81 to 84).

  • Optionally, use the SO_REUSEADDR option (lines 76 to 79), so that more than one process can bind the same group and port on the same machine, i.e., have multiple receivers.

  • Join the group (lines 87 to 92).

The IP_ADD_MEMBERSHIP option expects a pointer to a struct ip_mreq. This structure is defined in netinet/in.h. The first field, imr_multiaddr, contains the group address you want to join. The second, imr_interface, holds the IP address of the interface to which the group will be joined. This is a key point: membership is associated with both groups and interfaces. You do not just join a group; you join a group on a network interface. If your host is multi-homed, you can join the same group on all your network interfaces, on one of them or even on some of them. This way, the application will get packets sent for that group and received on that particular interface.

Normally, you want to receive traffic for that group and you don't care which interface received it. In those cases, fill the imr_interface field with the INADDR_ANY wild card (see line 88).

When you are done, you might want to drop membership (stop being a member of that group), although this is not strictly necessary if you are going to close the socket right afterward. The kernel will drop membership for you on all groups the socket was subscribed to when you close it.

If your process drops membership for a particular group but keeps the socket bound, it will keep receiving that group's traffic as long as any other process in the host remains a member. Joining a multicast group only tells the IP and data link layers (which in some cases explicitly tells the hardware) to accept multicast datagrams destined to that group; it is not a per-process membership, but a per-host membership.

The rest is easy; we fork and let the parent send messages (lines 123 to 137) and the child receive them (lines 104 to 122). As we told it not to loop back, we do not see our own messages. Change the IP_MULTICAST_LOOP option, and you'll find you are talking to yourself.

Conclusion

Feel free to test, modify and enhance this example program. You'll probably see that there are certain subtleties not fully addressed in the text. It is difficult to cover everything in a short article, but you can check and complete it by reading the Multicast HOWTO (tldp.org/HOWTO/Multicast-HOWTO.html).

All listings referred to in this article are available by anonymous download in the file ftp.linuxjournal.com/pub/lj/listings/issue65/3041.tgz.

Juan-Mariano de Goyeneche (jmseyas@dit.upm.es) moved to GNU/Linux quickly when he realized that it was much easier to debug and modify programs when one has the sources. While he finishes his educational career, he collaborates with the Telematic Systems Department (DIT) at UPM, Spain, working with CSCW multicast applications. He is the author of the “Multicast over TCP/IP HOWTO”.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix