Linux built-in or open source program to join multicast group?

I'm using tcpdump to capture multicast packets and had to code up a custom program to join multicast feeds so tcpdump will "see" the packets. Just wondering if netcat or any other applications can perform this function instead?


Solution 1:

You can do this using the ip maddr add command.


SYNTAX

ip maddr [ add | del ] MULTIADDR dev STRING 

DESCRIPTION

It attaches/detaches a static link layer multicast address to listen on the interface. Note that it is impossible to join protocol multicast groups statically. This command only manages link layer addresses.

address LLADDRESS (default)
    the link layer multicast address. 
dev NAME
    the device to join/leave this multicast address. 

EXAMPLES

Example for a wired connection:

ip maddr add ff02::fb dev eth0

Example for a wireless connection:

ip maddr add 224.0.0.251 dev wlan0

Solution 2:

One can use socat to subscribe to groups. This works nicely for both L2 and L3 subscription:

socat STDIO  UDP4-DATAGRAM:239.101.1.68:8889,\
  ip-add-membership=239.0.1.68:10.100.201.1

This will subscribe to group 239.0.1.68 using the interface with address 10.100.201.1. The UDP4-DATAGRAM:239.101.1.68:8889 bit listens for packets on a dummy group and udp port that should not receive any data to prevent socat from also outputting everything to stdout. If, instead, you want to direct the payload to stdout, change that group and port to be actual group and port that you want to subscribe to.

Multiple comma-separated ip-add-membership directives can be specified to subscribe to multiple groups at the same time. When socat exits, it seems to clear out the IGMP subscriptions too.

Solution 3:

In addition to socat answer, here is a heavyweight solution - smcroute. This application run as a daemon and can be controlled on the fly:

smcroutectl join eth0 239.1.1.27
smcroutectl leave eth0 239.1.1.27