Multicast: From Theory to Practice
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.
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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Astronomy for KDE
- Profiles and RC Files
- Understanding Ceph and Its Place in the Market
- Maru OS Brings Debian to Your Phone
- Git 2.9 Released
- What's Our Next Fight?
- The Giant Zero, Part 0.x
- Snappy Moves to New Platforms
- OpenSwitch Finds a New Home
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide