Commit e91bf2e9 authored by Mikio Hara's avatar Mikio Hara

net: make LocalAddr on multicast return group address

Fixes #3067.

R=golang-dev, rsc, rsc
CC=golang-dev
https://golang.org/cl/5656098
parent 9a59aec1
...@@ -81,6 +81,9 @@ func TestListenMulticastUDP(t *testing.T) { ...@@ -81,6 +81,9 @@ func TestListenMulticastUDP(t *testing.T) {
if !found { if !found {
t.Fatalf("%q not found in RIB", tt.gaddr.String()) t.Fatalf("%q not found in RIB", tt.gaddr.String())
} }
if c.LocalAddr().String() != tt.gaddr.String() {
t.Fatalf("LocalAddr returns %q, expected %q", c.LocalAddr().String(), tt.gaddr.String())
}
} }
} }
...@@ -114,6 +117,9 @@ func TestSimpleListenMulticastUDP(t *testing.T) { ...@@ -114,6 +117,9 @@ func TestSimpleListenMulticastUDP(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("ListenMulticastUDP failed: %v", err) t.Fatalf("ListenMulticastUDP failed: %v", err)
} }
if c.LocalAddr().String() != tt.gaddr.String() {
t.Fatalf("LocalAddr returns %q, expected %q", c.LocalAddr().String(), tt.gaddr.String())
}
c.Close() c.Close()
} }
} }
......
...@@ -33,13 +33,14 @@ func socket(net string, f, t, p int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -33,13 +33,14 @@ func socket(net string, f, t, p int, la, ra syscall.Sockaddr, toAddr func(syscal
return nil, err return nil, err
} }
var bla syscall.Sockaddr
if la != nil { if la != nil {
la, err = listenerSockaddr(s, f, la, toAddr) bla, err = listenerSockaddr(s, f, la, toAddr)
if err != nil { if err != nil {
closesocket(s) closesocket(s)
return nil, err return nil, err
} }
err = syscall.Bind(s, la) err = syscall.Bind(s, bla)
if err != nil { if err != nil {
closesocket(s) closesocket(s)
return nil, err return nil, err
...@@ -61,7 +62,12 @@ func socket(net string, f, t, p int, la, ra syscall.Sockaddr, toAddr func(syscal ...@@ -61,7 +62,12 @@ func socket(net string, f, t, p int, la, ra syscall.Sockaddr, toAddr func(syscal
} }
sa, _ := syscall.Getsockname(s) sa, _ := syscall.Getsockname(s)
laddr := toAddr(sa) var laddr Addr
if la != nil && bla != la {
laddr = toAddr(la)
} else {
laddr = toAddr(sa)
}
sa, _ = syscall.Getpeername(s) sa, _ = syscall.Getpeername(s)
raddr := toAddr(sa) raddr := toAddr(sa)
......
...@@ -262,7 +262,7 @@ func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, e ...@@ -262,7 +262,7 @@ func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, e
return nil, UnknownNetworkError(net) return nil, UnknownNetworkError(net)
} }
if gaddr == nil || gaddr.IP == nil { if gaddr == nil || gaddr.IP == nil {
return nil, &OpError{"listenmulticastudp", "udp", nil, errMissingAddress} return nil, &OpError{"listenmulticast", net, nil, errMissingAddress}
} }
fd, err := internetSocket(net, gaddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP) fd, err := internetSocket(net, gaddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
if err != nil { if err != 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