How to read multicast datagrams but only from specific interface?

I’m trying to read multicast packets directed to 224.0.0.9:520, but only when received from interface eth2 (its IP address is 1.0.0.2/24).

My sample code is based on docs for golang.org/x/net/ipv4.

Roughly steps are:

  1. Find interface eth2 with: net.InterfaceByName(“eth2”)
  2. Bind socket to 1.0.0.2:520 with: net.ListenPacket(“udp”, “1.0.0.2:520”)
  3. Join multicast group 224.0.0.9: JoinGroup(ifi, 224.0.0.9)
  4. Read data with: ReadFrom(buf)

Full code is available here: http://play.golang.org/p/YOwu38OQdw

In order to test, I send UDP packets from the attached host (1.0.0.1/24):
$ echo x | nc -s 1.0.0.1 -u 224.0.0.9 520

Then I verify those packets are actually reaching the destination host interface:
$ sudo tcpdump -n -i eth2
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth2, link-type EN10MB (Ethernet), capture size 262144 bytes
12:08:34.745171 IP 1.0.0.1.51920 > 224.0.0.9.520: [|rip]

However my Go code running on host 1.0.0.2 is unable to read those packets:
$ sudo go run rip_mcast_listener.go
[sudo] password for lab:
2016/02/10 12:12:29 interfaceAdd: join error for ‘fe80::a00:27ff:fe52:9575’ on ‘eth2’: join: udp/[fe80::a00:27ff:fe52:9575]:520 listen error: listen udp [fe80::a00:27ff:fe52:9575]:520: bind: invalid argument
2016/02/10 12:12:29 main: waiting forever
2016/02/10 12:12:29 udpReader: reading from ‘1.0.0.2’ on ‘eth2’

I have also posted this question on stackoverflow: http://stackoverflow.com/questions/35300039/in-golang-how-to-receive-multicast-packets-with-socket-bound-to-specific-addres

go version is: go version go1.6rc2 linux/386

Can anyone please spot where the error is?

I see that you try to listen on a ipv6 address.

Yes, by scanning over all interface addresses, the code did hit an IPv6 address. That’s not harmful. Problem is, it seemingly succeeded in binding the IPv4 address plus UDP port (1.0.0.2:520) yet failed to read multicast datagrams. :frowning:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.