IP Multicasting with Socat

Introduction

Multicasting (and broadcasting which is also discussed in this article) provides a means to direct a single packet to more than one host. Special addresses are defined for this purpose and are handled specially by network adapters, networking hardware, and IP stacks.

IPv4 specifications provide broadcasting and multicasting; IPv6 provides multicasting but replaces broadcasting by special multicast modes. UNIX domain sockets do not know broadcasting or multicasting.

The following examples use UDP/IPv4 only. However, they can easily be adapted for raw IPv4 sockets. IPv6 multicasting has not yet been successfully used with socat; please contact the author if you have positive experiences or ideas that go beyond IPV6_ADD_MEMBERSHIP.

All multicast examples presented in this document use multicast address 224.1.0.1; it can be replaced by any valid IPv4 multicast address (except all-systems).

We assume a local network with address 192.168.10.0 and mask 255.255.255.0; an eventual "client" has 192.168.10.1, example "server" and example peer have 192.168.10.2 in all examples. Change these addresses and mask to your own requirements.

All the following examples work bidirectionally except when otherwise noticed. For "clients" we just use STDIO, and for "servers" we use EXEC:hostname which ignores its input but shows us which host the reply comes from. Replace these socat addresses with what is appropriate for your needs (e.g. shell script invocations). Port 6666 can be replaced with any other port (but for ports < 1024 root privilege might be required).

Different kinds of broadcast addresses exist: 255.255.255.255 is local network only; for the IPv4 network 192.168.10.0/24 the "official" broadcast address is 192.168.10.255; the network address 192.168.10.0 is also interpreted as broadcast by some hosts. The two latter forms are routed by gateways. In the following examples we only use broadcast address 192.168.10.255.

Example 1: Multicast client and servers

This example builds something like a "supervisor" or "client" that communicates with a set of "servers". The supervisor may send packets to the multicast address, and the servers may send response packets. Note that the servers would also respond to other clients' requests.

Multicast server:

socat \ UDP4-RECVFROM:6666,ip-add-membership=224.1.0.1:192.168.10.2,fork \ EXEC:hostname

This command receives multicast packets addressed to 224.1.0.1 and forks a child process for each. The child processes may each send one or more reply packets back to the particular sender. 192.168.10.2 means the address of the interface where multicasts should be received. Run this command on a number of hosts, and they will all respond in parallel.

Multicast client:

socat \ STDIO \ UDP4-DATAGRAM:224.1.0.1:6666,range=192.168.10.0/24

This process transfers data from stdin to the multicast address, and transfers packets received from the local network to stdout. It does not matter in which direction the first data is passed. A packet from the network is accepted by the IP stack for our socket if:

Of these packets, socat handles only those matching the following criteria:

Example 2: Broadcast client and servers

Broadcast server:

socat \ UDP4-RECVFROM:6666,broadcast,fork \ EXEC:hostname

This command receives packets addressed to a local broadcast address and forks a child process for each. The child processes may each send one or more reply packets back to the particular sender. Run this command on a number of hosts, and they will all respond in parallel.

Broadcast client:

socat \ STDIO \ UDP4-DATAGRAM:192.168.10.255:6666,broadcast,range=192.168.10.0/24

This process transfers data from stdin to the broadcast address, and transfers packets received from the local network to stdout. It does not matter in which direction the first data is passed. A packet from the network is accepted by the IP stack for our socket if:

Of these packets, socat handles only those matching the following criteria:

The broadcast option is only required for sending or receiving local broadcasts.

Example 3: Multicast peers

It is possible to combine multicast sender and receiver in one socat address. This allows to start processes on different hosts on the local network that will communicate symmetrically, so each process can send messages that are received by all the other ones.

socat \ STDIO \ UDP4-DATAGRAM:224.1.0.1:6666,bind=:6666,range=192.168.10.0/24,ip-add-membership=224.1.0.1:192.168.10.2

This command is valid for host 192.168.10.2; adapt this address to the particular interface addresses of the hosts.

Starting this process opens a socket on port 6666 that will receive packets directed to multicast address 224.1.0.1. Only packets with matching source address and source port 6666