Commit fca50820 authored by Mikio Hara's avatar Mikio Hara Committed by Russ Cox

net: join and leave a IPv6 group address, on a specific interface

This CL changes both JoinGroup and LeaveGroup methods
to take an interface as an argument for enabling IPv6
group address join/leave, join a group address on a
specific interface.

R=rsc, dave
CC=golang-dev
https://golang.org/cl/4815074
parent 72e83483
...@@ -14,6 +14,7 @@ GOFILES=\ ...@@ -14,6 +14,7 @@ GOFILES=\
httpserver.go\ httpserver.go\
main.go\ main.go\
netdial.go\ netdial.go\
netudpgroup.go\
oserrorstring.go\ oserrorstring.go\
osopen.go\ osopen.go\
procattr.go\ procattr.go\
......
// 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.
package main
import (
"go/ast"
)
var netudpgroupFix = fix{
"netudpgroup",
netudpgroup,
`Adapt 1-argument calls of net.(*UDPConn).JoinGroup, LeaveGroup to use 2-argument form.
http://codereview.appspot.com/4815074
`,
}
func init() {
register(netudpgroupFix)
}
func netudpgroup(f *ast.File) bool {
if !imports(f, "net") {
return false
}
fixed := false
for _, d := range f.Decls {
fd, ok := d.(*ast.FuncDecl)
if !ok {
continue
}
walk(fd.Body, func(n interface{}) {
ce, ok := n.(*ast.CallExpr)
if !ok {
return
}
se, ok := ce.Fun.(*ast.SelectorExpr)
if !ok || len(ce.Args) != 1 {
return
}
switch se.Sel.String() {
case "JoinGroup", "LeaveGroup":
// c.JoinGroup(a) -> c.JoinGroup(nil, a)
// c.LeaveGroup(a) -> c.LeaveGroup(nil, a)
arg := ce.Args[0]
ce.Args = make([]ast.Expr, 2)
ce.Args[0] = ast.NewIdent("nil")
ce.Args[1] = arg
fixed = true
}
})
}
return fixed
}
// 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.
package main
func init() {
addTestCases(netudpgroupTests)
}
var netudpgroupTests = []testCase{
{
Name: "netudpgroup.0",
In: `package main
import "net"
func f() {
err := x.JoinGroup(gaddr)
err = y.LeaveGroup(gaddr)
}
`,
Out: `package main
import "net"
func f() {
err := x.JoinGroup(nil, gaddr)
err = y.LeaveGroup(nil, gaddr)
}
`,
},
}
...@@ -6,13 +6,33 @@ package net ...@@ -6,13 +6,33 @@ package net
import ( import (
"flag" "flag"
"os"
"runtime" "runtime"
"testing" "testing"
) )
var multicast = flag.Bool("multicast", false, "enable multicast tests") var multicast = flag.Bool("multicast", false, "enable multicast tests")
func TestMulticastJoinAndLeave(t *testing.T) { var joinAndLeaveGroupUDPTests = []struct {
net string
laddr IP
gaddr IP
flags Flags
ipv6 bool
}{
// cf. RFC 4727: Experimental Values in IPv4, IPv6, ICMPv4, ICMPv6, UDP, and TCP Headers
{"udp", IPv4zero, IPv4(224, 0, 0, 254), (FlagUp | FlagLoopback), false},
{"udp4", IPv4zero, IPv4(224, 0, 0, 254), (FlagUp | FlagLoopback), false},
{"udp", IPv6unspecified, ParseIP("ff0e::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff01::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff02::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff04::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff05::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff08::114"), (FlagUp | FlagLoopback), true},
{"udp6", IPv6unspecified, ParseIP("ff0e::114"), (FlagUp | FlagLoopback), true},
}
func TestJoinAndLeaveGroupUDP(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return return
} }
...@@ -21,53 +41,51 @@ func TestMulticastJoinAndLeave(t *testing.T) { ...@@ -21,53 +41,51 @@ func TestMulticastJoinAndLeave(t *testing.T) {
return return
} }
addr := &UDPAddr{ for _, tt := range joinAndLeaveGroupUDPTests {
IP: IPv4zero, var (
Port: 0, ifi *Interface
} found bool
// open a UDPConn )
conn, err := ListenUDP("udp4", addr) if tt.ipv6 && (!supportsIPv6 || os.Getuid() != 0) {
if err != nil { continue
t.Fatal(err) }
} ift, err := Interfaces()
defer conn.Close() if err != nil {
t.Fatalf("Interfaces() failed: %v", err)
// try to join group }
mcast := IPv4(224, 0, 0, 254) for _, x := range ift {
err = conn.JoinGroup(mcast) if x.Flags&tt.flags == tt.flags {
if err != nil { ifi = &x
t.Fatal(err) break
} }
}
// try to leave group if ifi == nil {
err = conn.LeaveGroup(mcast) t.Logf("an appropriate multicast interface not found")
if err != nil { return
t.Fatal(err) }
} c, err := ListenUDP(tt.net, &UDPAddr{IP: tt.laddr})
} if err != nil {
t.Fatal(err)
func TestJoinFailureWithIPv6Address(t *testing.T) { }
if !*multicast { defer c.Close()
t.Logf("test disabled; use --multicast to enable") if err := c.JoinGroup(ifi, tt.gaddr); err != nil {
return t.Fatal(err)
} }
addr := &UDPAddr{ ifmat, err := ifi.MulticastAddrs()
IP: IPv4zero, if err != nil {
Port: 0, t.Fatalf("MulticastAddrs() failed: %v", err)
} }
for _, ifma := range ifmat {
// open a UDPConn if ifma.(*IPAddr).IP.Equal(tt.gaddr) {
conn, err := ListenUDP("udp4", addr) found = true
if err != nil { break
t.Fatal(err) }
} }
defer conn.Close() if !found {
t.Fatalf("%q not found in RIB", tt.gaddr.String())
// try to join group }
mcast := ParseIP("ff02::1") if err := c.LeaveGroup(ifi, tt.gaddr); err != nil {
err = conn.JoinGroup(mcast) t.Fatal(err)
if err == nil { }
t.Fatal("JoinGroup succeeded, should fail")
} }
t.Logf("%s", err)
} }
...@@ -169,17 +169,18 @@ func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error) { ...@@ -169,17 +169,18 @@ func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error) {
return &UDPConn{*l.plan9Conn()}, nil return &UDPConn{*l.plan9Conn()}, nil
} }
// JoinGroup joins the IPv4 multicast group named by addr. // JoinGroup joins the IP multicast group named by addr on ifi,
// The UDPConn must use the "udp4" network. // which specifies the interface to join. JoinGroup uses the
func (c *UDPConn) JoinGroup(addr IP) os.Error { // default multicast interface if ifi is nil.
func (c *UDPConn) JoinGroup(ifi *Interface, addr IP) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL
} }
return os.EPLAN9 return os.EPLAN9
} }
// LeaveGroup exits the IPv4 multicast group named by addr. // LeaveGroup exits the IP multicast group named by addr on ifi.
func (c *UDPConn) LeaveGroup(addr IP) os.Error { func (c *UDPConn) LeaveGroup(ifi *Interface, addr IP) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
package net package net
import ( import (
"bytes"
"os" "os"
"syscall" "syscall"
) )
...@@ -252,43 +253,94 @@ func (c *UDPConn) BindToDevice(device string) os.Error { ...@@ -252,43 +253,94 @@ func (c *UDPConn) BindToDevice(device string) os.Error {
// Closing c does not affect f, and closing f does not affect c. // Closing c does not affect f, and closing f does not affect c.
func (c *UDPConn) File() (f *os.File, err os.Error) { return c.fd.dup() } func (c *UDPConn) File() (f *os.File, err os.Error) { return c.fd.dup() }
var errInvalidMulticast = os.NewError("invalid IPv4 multicast address") // JoinGroup joins the IP multicast group named by addr on ifi,
// which specifies the interface to join. JoinGroup uses the
// default multicast interface if ifi is nil.
func (c *UDPConn) JoinGroup(ifi *Interface, addr IP) os.Error {
if !c.ok() {
return os.EINVAL
}
ip := addr.To4()
if ip != nil {
return joinIPv4GroupUDP(c, ifi, ip)
}
return joinIPv6GroupUDP(c, ifi, addr)
}
// JoinGroup joins the IPv4 multicast group named by addr. // LeaveGroup exits the IP multicast group named by addr on ifi.
// The UDPConn must use the "udp4" network. func (c *UDPConn) LeaveGroup(ifi *Interface, addr IP) os.Error {
func (c *UDPConn) JoinGroup(addr IP) os.Error {
if !c.ok() { if !c.ok() {
return os.EINVAL return os.EINVAL
} }
ip := addr.To4() ip := addr.To4()
if ip == nil { if ip != nil {
return &OpError{"joingroup", "udp", &IPAddr{ip}, errInvalidMulticast} return leaveIPv4GroupUDP(c, ifi, ip)
} }
mreq := &syscall.IPMreq{ return leaveIPv6GroupUDP(c, ifi, addr)
Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}, }
func joinIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) os.Error {
mreq := &syscall.IPMreq{Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}}
if err := setIPv4InterfaceToJoin(mreq, ifi); err != nil {
return &OpError{"joinipv4group", "udp", &IPAddr{ip}, err}
} }
err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq)) if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq)); err != nil {
if err != nil { return &OpError{"joinipv4group", "udp", &IPAddr{ip}, err}
return &OpError{"joingroup", "udp", &IPAddr{ip}, err}
} }
return nil return nil
} }
// LeaveGroup exits the IPv4 multicast group named by addr. func leaveIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) os.Error {
func (c *UDPConn) LeaveGroup(addr IP) os.Error { mreq := &syscall.IPMreq{Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}}
if !c.ok() { if err := setIPv4InterfaceToJoin(mreq, ifi); err != nil {
return os.EINVAL return &OpError{"leaveipv4group", "udp", &IPAddr{ip}, err}
} }
ip := addr.To4() if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_DROP_MEMBERSHIP, mreq)); err != nil {
if ip == nil { return &OpError{"leaveipv4group", "udp", &IPAddr{ip}, err}
return &OpError{"leavegroup", "udp", &IPAddr{ip}, errInvalidMulticast}
} }
mreq := &syscall.IPMreq{ return nil
Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}, }
func setIPv4InterfaceToJoin(mreq *syscall.IPMreq, ifi *Interface) os.Error {
if ifi == nil {
return nil
} }
err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_DROP_MEMBERSHIP, mreq)) ifat, err := ifi.Addrs()
if err != nil { if err != nil {
return &OpError{"leavegroup", "udp", &IPAddr{ip}, err} return err
}
for _, ifa := range ifat {
if x := ifa.(*IPAddr).IP.To4(); x != nil {
copy(mreq.Interface[:], x)
break
}
}
if bytes.Equal(mreq.Multiaddr[:], IPv4zero) {
return os.EINVAL
}
return nil
}
func joinIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) os.Error {
mreq := &syscall.IPv6Mreq{}
copy(mreq.Multiaddr[:], ip)
if ifi != nil {
mreq.Interface = uint32(ifi.Index)
}
if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPv6Mreq(c.fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_JOIN_GROUP, mreq)); err != nil {
return &OpError{"joinipv6group", "udp", &IPAddr{ip}, err}
}
return nil
}
func leaveIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) os.Error {
mreq := &syscall.IPv6Mreq{}
copy(mreq.Multiaddr[:], ip)
if ifi != nil {
mreq.Interface = uint32(ifi.Index)
}
if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPv6Mreq(c.fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_LEAVE_GROUP, mreq)); err != nil {
return &OpError{"leaveipv6group", "udp", &IPAddr{ip}, err}
} }
return nil return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment