joining a remote multicast (udp) group

1,730 views
Skip to first unread message

Mario Kozjak

unread,
Sep 25, 2014, 4:37:31 PM9/25/14
to golan...@googlegroups.com
Hi, all!

I still can't find a way to join a remote multicast address with Go.
What are the needed methods for joining/binding to it?
Is it net.Dial? net.DialUDP?
Do I use net.Read afterwards? net.ReadFromUDP?

All I'm getting here is zero packets.

Do I need to do something between those two steps?

Obviously, doing net.ListenMulticastUDP and ReadFromUDP would only get me some packets from 0.0.0.0:xxxx, even though I've defined a remote addr with it...

Kinda stuck. Can anyone shed some light on this topic, maybe?

The multicast group I'm trying to join to is definitely reachable on the machine I'm running the Go application.

Thank you in advace!
mk

Dave Cheney

unread,
Sep 25, 2014, 7:30:36 PM9/25/14
to golan...@googlegroups.com

John Waycott

unread,
Sep 26, 2014, 11:03:17 AM9/26/14
to golan...@googlegroups.com
net.ListenMulticastUDP is the correct call for joining a multicast group. You should specify the actual multicast address, not 0.0.0.0, maybe that's the problem. Then use net.ReadFromUDP to read packets.

If you are trying to read  packets sent from another application running on the same machine, you will need to turn on multicast looping using syscall.SetSockOptInt otherwise you won't get any packets, like this:

err = syscall.SetsockoptInt(int(f.Fd()), syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, 1)


where f is socket connection file descriptor (conn.File()).

Mario Kozjak

unread,
Sep 26, 2014, 4:47:26 PM9/26/14
to golan...@googlegroups.com
Dave,

ipv4 package really helped, thank you!

John, thank you for the tip! How can I be sure that I'm getting the right package if I run multiple goroutines joining to multicast groups that arrive from the same remote server? Seems like I can only compare the port from UDPAddr which I'm getting from net.ReadFromUDP. I cannot be sure that way.

Or is there something I'm missing?

Thank you, guys!
mk

Mario Kozjak

unread,
Sep 26, 2014, 4:56:23 PM9/26/14
to golan...@googlegroups.com
Precisely, what if I'm getting multiple multicast streams from the same remote port and same remote host address? In example, 232.50.100.200:1234 and 232.50.100.210:1234 arriving from 88.88.88.88. They would appear as from 88.88.88.88:1234

John Waycott

unread,
Sep 27, 2014, 9:42:00 AM9/27/14
to golan...@googlegroups.com


On Friday, September 26, 2014 1:56:23 PM UTC-7, Mario Kozjak wrote:
Precisely, what if I'm getting multiple multicast streams from the same remote port and same remote host address? In example, 232.50.100.200:1234 and 232.50.100.210:1234 arriving from 88.88.88.88. They would appear as from 88.88.88.88:1234

For each group you want to join, create a separate connection. Then run a goroutine for each connection. So, in your example, you will have a connection for 232.50.100.200:1234 and another one for 232.50.100.210.1234, each reading from its own goroutine.

Mario Kozjak

unread,
Sep 27, 2014, 6:55:04 PM9/27/14
to golan...@googlegroups.com
Hi, John!

I just can't get it to work the way you described. Here is an example application: http://play.golang.org/p/X4A2HotceH

On line 111 I'm launching a goroutine where I would connect to a certain multicast address with net.ListenMulticastUDP on line 48.
Because net.ReadFromUDP on line 61 returns *UDPAddr, and that's host's IP, I am trying to compare it against the needed host's IP. In the example above, that would be "88.88.88.88" (also, I included the port, because I know on which port I'm listening.

What I'm getting here is all the traffic from 88.88.88.88:22222. I'm getting packets from both 100.200 and 100.210 (ports are false, but that's just for the sake of an example) and I only defined a certain multicast with net.ListenMulticastUDP. There is no way I can distinguish between different packet sources (they all arrive from the same port, but different address (Which I cannot find with 'net' library, as it seems. And I can easily find it with 'ipv4's .Dst.Equal(group)) from a remote host 88.88.88.88.

With regards,
mk

Mario Kozjak

unread,
Sep 27, 2014, 7:14:05 PM9/27/14
to golan...@googlegroups.com

Mikio Hara

unread,
Sep 27, 2014, 7:34:25 PM9/27/14
to Mario Kozjak, golang-nuts
On Sun, Sep 28, 2014 at 8:14 AM, Mario Kozjak <kozj...@gmail.com> wrote:

> I believe this is the issue:
> https://groups.google.com/forum/#!msg/golang-nuts/6J-Howedc8M/GYEYR1QqQJYJ

no, the difference btw any-source multicast and source-specific
multicast is simply (*, G) vs. (S, G); and some functionality that
enables to operate multicast-source filter.

John Waycott

unread,
Sep 28, 2014, 1:32:27 PM9/28/14
to golan...@googlegroups.com
Mario,

I think the problem is the SetReadDeadline() call in testMcast. The for loop will fail after two seconds because of the deadline timeout. SetReadDeadline() is the time after which any reads will fail; the deadline is not reset after each read.  Try moving the call to SetReadDeadline right after for for statement on line 59.

John

Mario Kozjak

unread,
Sep 28, 2014, 1:41:59 PM9/28/14
to golan...@googlegroups.com
John,

No, that definitely isn't a problem. I definitely want it to stop after n seconds. I've already tried raising that value to some ridiculous value and still, I am not getting what I want.

What would I get if I did reset the timeout all over again? Sniffing would never end, and that's not what I want.

Anyways, the default two seconds is definitely enough to capture the needed multicast...

Thank you for helping!
Mkozjak

Mikio Hara

unread,
Sep 28, 2014, 11:19:33 PM9/28/14
to Mario Kozjak, golang-nuts
On Mon, Sep 29, 2014 at 2:41 AM, Mario Kozjak <kozj...@gmail.com> wrote:

> I am not getting what I want.

net.ListenMulticastUDP is just designed for single-homed, single
any-source multicast group applications. you need to make two separate
udp endpoints with different 5-tuples if you have a reason to use
net.ListenMulticastUDP. otherwise, using go.net/ipv4 and/or
go.net/ipv6 would make things a bit flexible.

Mario Kozjak

unread,
Sep 29, 2014, 2:53:54 AM9/29/14
to golan...@googlegroups.com
Thank you, Mikio!

Ok, everything's clear now.

Regards,
Mkozjak

Reply all
Reply to author
Forward
0 new messages