2012-09-26 16:03:09 +04:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
|
|
|
package ipv4_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2014-10-07 01:35:16 +04:00
|
|
|
|
2014-11-10 01:04:43 +03:00
|
|
|
"golang.org/x/net/internal/nettest"
|
|
|
|
"golang.org/x/net/ipv4"
|
2012-09-26 16:03:09 +04:00
|
|
|
)
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
var packetConnMulticastSocketOptionTests = []struct {
|
|
|
|
net, proto, addr string
|
|
|
|
gaddr net.Addr
|
|
|
|
}{
|
|
|
|
{"udp4", "", "224.0.0.0:0", &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}}, // see RFC 4727
|
|
|
|
{"ip4", ":icmp", "0.0.0.0", &net.IPAddr{IP: net.IPv4(224, 0, 0, 250)}}, // see RFC 4727
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
func TestPacketConnMulticastSocketOptions(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
2014-10-30 08:15:42 +03:00
|
|
|
case "nacl", "plan9", "solaris":
|
2014-04-27 14:26:58 +04:00
|
|
|
t.Skipf("not supported on %q", runtime.GOOS)
|
|
|
|
}
|
2014-10-08 01:54:30 +04:00
|
|
|
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
|
2014-04-27 14:26:58 +04:00
|
|
|
if ifi == nil {
|
|
|
|
t.Skipf("not available on %q", runtime.GOOS)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
for _, tt := range packetConnMulticastSocketOptionTests {
|
|
|
|
if tt.net == "ip4" && os.Getuid() != 0 {
|
|
|
|
t.Skip("must be root")
|
|
|
|
}
|
|
|
|
c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
|
2012-09-26 16:03:09 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("net.ListenPacket failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
testMulticastSocketOptions(t, ipv4.NewPacketConn(c), ifi, tt.gaddr)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
func TestRawConnMulticastSocketOptions(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
2014-10-30 08:15:42 +03:00
|
|
|
case "nacl", "plan9", "solaris":
|
2014-04-27 14:26:58 +04:00
|
|
|
t.Skipf("not supported on %q", runtime.GOOS)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
if os.Getuid() != 0 {
|
2013-04-04 09:37:01 +04:00
|
|
|
t.Skip("must be root")
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
2014-10-08 01:54:30 +04:00
|
|
|
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
|
2014-04-27 14:26:58 +04:00
|
|
|
if ifi == nil {
|
|
|
|
t.Skipf("not available on %q", runtime.GOOS)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("net.ListenPacket failed: %v", err)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
2014-04-27 14:26:58 +04:00
|
|
|
defer c.Close()
|
2012-09-26 16:03:09 +04:00
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
r, err := ipv4.NewRawConn(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ipv4.NewRawConn failed: %v", err)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
testMulticastSocketOptions(t, r, ifi, &net.IPAddr{IP: net.IPv4(224, 0, 0, 250)}) /// see RFC 4727
|
|
|
|
}
|
|
|
|
|
|
|
|
type testIPv4MulticastConn interface {
|
|
|
|
MulticastTTL() (int, error)
|
|
|
|
SetMulticastTTL(ttl int) error
|
|
|
|
MulticastLoopback() (bool, error)
|
|
|
|
SetMulticastLoopback(bool) error
|
|
|
|
JoinGroup(*net.Interface, net.Addr) error
|
|
|
|
LeaveGroup(*net.Interface, net.Addr) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMulticastSocketOptions(t *testing.T, c testIPv4MulticastConn, ifi *net.Interface, gaddr net.Addr) {
|
|
|
|
const ttl = 255
|
|
|
|
if err := c.SetMulticastTTL(ttl); err != nil {
|
2012-09-26 16:03:09 +04:00
|
|
|
t.Fatalf("ipv4.PacketConn.SetMulticastTTL failed: %v", err)
|
|
|
|
}
|
|
|
|
if v, err := c.MulticastTTL(); err != nil {
|
|
|
|
t.Fatalf("ipv4.PacketConn.MulticastTTL failed: %v", err)
|
2014-04-27 14:26:58 +04:00
|
|
|
} else if v != ttl {
|
|
|
|
t.Fatalf("got unexpected multicast TTL value %v; expected %v", v, ttl)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
for _, toggle := range []bool{true, false} {
|
|
|
|
if err := c.SetMulticastLoopback(toggle); err != nil {
|
|
|
|
t.Fatalf("ipv4.PacketConn.SetMulticastLoopback failed: %v", err)
|
|
|
|
}
|
|
|
|
if v, err := c.MulticastLoopback(); err != nil {
|
|
|
|
t.Fatalf("ipv4.PacketConn.MulticastLoopback failed: %v", err)
|
|
|
|
} else if v != toggle {
|
|
|
|
t.Fatalf("got unexpected multicast loopback %v; expected %v", v, toggle)
|
|
|
|
}
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 14:26:58 +04:00
|
|
|
if err := c.JoinGroup(ifi, gaddr); err != nil {
|
|
|
|
t.Fatalf("ipv4.PacketConn.JoinGroup(%v, %v) failed: %v", ifi, gaddr, err)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
2014-04-27 14:26:58 +04:00
|
|
|
if err := c.LeaveGroup(ifi, gaddr); err != nil {
|
|
|
|
t.Fatalf("ipv4.PacketConn.LeaveGroup(%v, %v) failed: %v", ifi, gaddr, err)
|
2012-09-26 16:03:09 +04:00
|
|
|
}
|
|
|
|
}
|