net: add IPv4 multicast to UDPConn
notes:
Darwin is very particular about joining a multicast group if the
listneing socket is not created in "udp4" mode, the other supported
OS's are more flexible.
A simple example sets up a socket to listen on the mdns/bonjour
group 224.0.0.251:5353
// ensure the sock is udp4, and the IP is a 4 byte IPv4
socket, err := net.ListenUDP("udp4", &net.UDPAddr {
IP: net.IPv4zero,
// currently darwin will not allow you to bind to
// a port if it is already bound to another process
Port: 5353,
})
if err != nil {
log.Exitf("listen %s", err)
}
defer socket.Close()
err = socket.JoinGroup(net.IPv4(224, 0, 0, 251))
if err != nil {
log.Exitf("join group %s", err)
}
R=adg, rsc
CC=golang-dev
https://golang.org/cl/4066044
2011-02-16 13:07:13 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-06-06 16:38:56 -06:00
|
|
|
// +build !plan9
|
|
|
|
|
net: add IPv4 multicast to UDPConn
notes:
Darwin is very particular about joining a multicast group if the
listneing socket is not created in "udp4" mode, the other supported
OS's are more flexible.
A simple example sets up a socket to listen on the mdns/bonjour
group 224.0.0.251:5353
// ensure the sock is udp4, and the IP is a 4 byte IPv4
socket, err := net.ListenUDP("udp4", &net.UDPAddr {
IP: net.IPv4zero,
// currently darwin will not allow you to bind to
// a port if it is already bound to another process
Port: 5353,
})
if err != nil {
log.Exitf("listen %s", err)
}
defer socket.Close()
err = socket.JoinGroup(net.IPv4(224, 0, 0, 251))
if err != nil {
log.Exitf("join group %s", err)
}
R=adg, rsc
CC=golang-dev
https://golang.org/cl/4066044
2011-02-16 13:07:13 -07:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-02-22 20:16:43 -07:00
|
|
|
"errors"
|
2011-08-18 10:22:02 -06:00
|
|
|
"os"
|
2011-02-16 18:00:02 -07:00
|
|
|
"runtime"
|
net: add IPv4 multicast to UDPConn
notes:
Darwin is very particular about joining a multicast group if the
listneing socket is not created in "udp4" mode, the other supported
OS's are more flexible.
A simple example sets up a socket to listen on the mdns/bonjour
group 224.0.0.251:5353
// ensure the sock is udp4, and the IP is a 4 byte IPv4
socket, err := net.ListenUDP("udp4", &net.UDPAddr {
IP: net.IPv4zero,
// currently darwin will not allow you to bind to
// a port if it is already bound to another process
Port: 5353,
})
if err != nil {
log.Exitf("listen %s", err)
}
defer socket.Close()
err = socket.JoinGroup(net.IPv4(224, 0, 0, 251))
if err != nil {
log.Exitf("join group %s", err)
}
R=adg, rsc
CC=golang-dev
https://golang.org/cl/4066044
2011-02-16 13:07:13 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2012-02-22 20:16:43 -07:00
|
|
|
var multicastListenerTests = []struct {
|
2011-08-18 10:22:02 -06:00
|
|
|
net string
|
2012-01-31 09:53:26 -07:00
|
|
|
gaddr *UDPAddr
|
2011-08-18 10:22:02 -06:00
|
|
|
flags Flags
|
2012-02-22 20:16:43 -07:00
|
|
|
ipv6 bool // test with underlying AF_INET6 socket
|
2011-08-18 10:22:02 -06:00
|
|
|
}{
|
|
|
|
// cf. RFC 4727: Experimental Values in IPv4, IPv6, ICMPv4, ICMPv6, UDP, and TCP Headers
|
2012-02-22 20:16:43 -07:00
|
|
|
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
{"udp", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}, FlagUp | FlagLoopback, false},
|
|
|
|
{"udp", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}, 0, false},
|
|
|
|
{"udp", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}, 0, true},
|
|
|
|
|
|
|
|
{"udp4", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}, FlagUp | FlagLoopback, false},
|
|
|
|
{"udp4", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}, 0, false},
|
|
|
|
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff01::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff01::114"), Port: 12345}, 0, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff02::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff02::114"), Port: 12345}, 0, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff04::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff04::114"), Port: 12345}, 0, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff05::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff05::114"), Port: 12345}, 0, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff08::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff08::114"), Port: 12345}, 0, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}, FlagUp | FlagLoopback, true},
|
|
|
|
{"udp6", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}, 0, true},
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
|
|
|
|
2012-02-22 20:16:43 -07:00
|
|
|
// TestMulticastListener tests both single and double listen to a test
|
|
|
|
// listener with same address family, same group address and same port.
|
|
|
|
func TestMulticastListener(t *testing.T) {
|
2012-01-31 09:53:26 -07:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "netbsd", "openbsd", "plan9", "windows":
|
2012-03-05 23:41:17 -07:00
|
|
|
t.Logf("skipping test on %q", runtime.GOOS)
|
2011-04-10 18:39:21 -06:00
|
|
|
return
|
2012-01-31 10:42:56 -07:00
|
|
|
case "linux":
|
2012-02-13 22:31:21 -07:00
|
|
|
if runtime.GOARCH == "arm" || runtime.GOARCH == "alpha" {
|
2012-03-05 23:41:17 -07:00
|
|
|
t.Logf("skipping test on %q/%q", runtime.GOOS, runtime.GOARCH)
|
2012-01-31 10:42:56 -07:00
|
|
|
return
|
|
|
|
}
|
2011-04-10 18:39:21 -06:00
|
|
|
}
|
2011-02-18 11:48:13 -07:00
|
|
|
|
2012-02-22 20:16:43 -07:00
|
|
|
for _, tt := range multicastListenerTests {
|
2012-07-31 05:58:35 -06:00
|
|
|
if tt.ipv6 && (!*testIPv6 || !supportsIPv6 || os.Getuid() != 0) {
|
2011-08-18 10:22:02 -06:00
|
|
|
continue
|
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
ifi, err := availMulticastInterface(t, tt.flags)
|
2011-08-18 10:22:02 -06:00
|
|
|
if err != nil {
|
2012-02-22 20:16:43 -07:00
|
|
|
continue
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
c1, err := ListenMulticastUDP(tt.net, ifi, tt.gaddr)
|
2011-08-18 10:22:02 -06:00
|
|
|
if err != nil {
|
2012-02-22 20:16:43 -07:00
|
|
|
t.Fatalf("First ListenMulticastUDP failed: %v", err)
|
2012-01-10 17:53:32 -07:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
checkMulticastListener(t, err, c1, tt.gaddr)
|
|
|
|
c2, err := ListenMulticastUDP(tt.net, ifi, tt.gaddr)
|
2011-08-18 10:22:02 -06:00
|
|
|
if err != nil {
|
2012-02-22 20:16:43 -07:00
|
|
|
t.Fatalf("Second ListenMulticastUDP failed: %v", err)
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
checkMulticastListener(t, err, c2, tt.gaddr)
|
|
|
|
c2.Close()
|
|
|
|
c1.Close()
|
net: add IPv4 multicast to UDPConn
notes:
Darwin is very particular about joining a multicast group if the
listneing socket is not created in "udp4" mode, the other supported
OS's are more flexible.
A simple example sets up a socket to listen on the mdns/bonjour
group 224.0.0.251:5353
// ensure the sock is udp4, and the IP is a 4 byte IPv4
socket, err := net.ListenUDP("udp4", &net.UDPAddr {
IP: net.IPv4zero,
// currently darwin will not allow you to bind to
// a port if it is already bound to another process
Port: 5353,
})
if err != nil {
log.Exitf("listen %s", err)
}
defer socket.Close()
err = socket.JoinGroup(net.IPv4(224, 0, 0, 251))
if err != nil {
log.Exitf("join group %s", err)
}
R=adg, rsc
CC=golang-dev
https://golang.org/cl/4066044
2011-02-16 13:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
|
2012-02-22 20:16:43 -07:00
|
|
|
func TestSimpleMulticastListener(t *testing.T) {
|
2012-01-31 09:53:26 -07:00
|
|
|
switch runtime.GOOS {
|
2012-01-31 22:14:04 -07:00
|
|
|
case "plan9":
|
2012-03-05 23:41:17 -07:00
|
|
|
t.Logf("skipping test on %q", runtime.GOOS)
|
2012-01-14 22:19:44 -07:00
|
|
|
return
|
2012-03-06 22:02:07 -07:00
|
|
|
case "windows":
|
|
|
|
if testing.Short() || !*testExternal {
|
|
|
|
t.Logf("skipping test on windows to avoid firewall")
|
|
|
|
return
|
|
|
|
}
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
|
|
|
|
2012-02-22 20:16:43 -07:00
|
|
|
for _, tt := range multicastListenerTests {
|
2012-01-14 22:19:44 -07:00
|
|
|
if tt.ipv6 {
|
|
|
|
continue
|
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
tt.flags = FlagUp | FlagMulticast // for windows testing
|
|
|
|
ifi, err := availMulticastInterface(t, tt.flags)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c1, err := ListenMulticastUDP(tt.net, ifi, tt.gaddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("First ListenMulticastUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
checkSimpleMulticastListener(t, err, c1, tt.gaddr)
|
|
|
|
c2, err := ListenMulticastUDP(tt.net, ifi, tt.gaddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Second ListenMulticastUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
checkSimpleMulticastListener(t, err, c2, tt.gaddr)
|
|
|
|
c2.Close()
|
|
|
|
c1.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkMulticastListener(t *testing.T, err error, c *UDPConn, gaddr *UDPAddr) {
|
|
|
|
if !multicastRIBContains(t, gaddr.IP) {
|
2012-11-12 20:26:20 -07:00
|
|
|
t.Errorf("%q not found in RIB", gaddr.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
la := c.LocalAddr()
|
|
|
|
if la == nil {
|
|
|
|
t.Error("LocalAddr failed")
|
|
|
|
return
|
2012-02-22 20:16:43 -07:00
|
|
|
}
|
2012-11-12 20:26:20 -07:00
|
|
|
if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
|
|
|
|
t.Errorf("got %v; expected a proper address with non-zero port number", la)
|
|
|
|
return
|
2012-02-22 20:16:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSimpleMulticastListener(t *testing.T, err error, c *UDPConn, gaddr *UDPAddr) {
|
2012-11-12 20:26:20 -07:00
|
|
|
la := c.LocalAddr()
|
|
|
|
if la == nil {
|
|
|
|
t.Error("LocalAddr failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
|
|
|
|
t.Errorf("got %v; expected a proper address with non-zero port number", la)
|
|
|
|
return
|
2012-02-22 20:16:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func availMulticastInterface(t *testing.T, flags Flags) (*Interface, error) {
|
|
|
|
var ifi *Interface
|
|
|
|
if flags != Flags(0) {
|
2012-01-14 22:19:44 -07:00
|
|
|
ift, err := Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Interfaces failed: %v", err)
|
|
|
|
}
|
|
|
|
for _, x := range ift {
|
2012-02-22 20:16:43 -07:00
|
|
|
if x.Flags&flags == flags {
|
2012-01-14 22:19:44 -07:00
|
|
|
ifi = &x
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ifi == nil {
|
2012-02-22 20:16:43 -07:00
|
|
|
return nil, errors.New("an appropriate multicast interface not found")
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
}
|
|
|
|
return ifi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func multicastRIBContains(t *testing.T, ip IP) bool {
|
|
|
|
ift, err := Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Interfaces failed: %v", err)
|
|
|
|
}
|
|
|
|
for _, ifi := range ift {
|
|
|
|
ifmat, err := ifi.MulticastAddrs()
|
2012-01-14 22:19:44 -07:00
|
|
|
if err != nil {
|
2012-02-22 20:16:43 -07:00
|
|
|
t.Fatalf("MulticastAddrs failed: %v", err)
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
for _, ifma := range ifmat {
|
|
|
|
if ifma.(*IPAddr).IP.Equal(ip) {
|
|
|
|
return true
|
|
|
|
}
|
2012-02-18 18:50:03 -07:00
|
|
|
}
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|
2012-02-22 20:16:43 -07:00
|
|
|
return false
|
2012-01-14 22:19:44 -07:00
|
|
|
}
|