1
0
mirror of https://github.com/golang/go synced 2024-11-20 09:34:52 -07:00

net: make LocalAddr on multicast UDPConn return a listening address

The package go.net/ipv4 allows to exist a single UDP listener
that join multiple different group addresses. That means that
LocalAddr on multicast UDPConn returns a first joined group
address is not desirable.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6822108
This commit is contained in:
Mikio Hara 2012-11-13 12:26:20 +09:00
parent 7e7b89f7d0
commit 0ae80785e6
2 changed files with 28 additions and 14 deletions

View File

@ -118,16 +118,29 @@ func TestSimpleMulticastListener(t *testing.T) {
func checkMulticastListener(t *testing.T, err error, c *UDPConn, gaddr *UDPAddr) {
if !multicastRIBContains(t, gaddr.IP) {
t.Fatalf("%q not found in RIB", gaddr.String())
t.Errorf("%q not found in RIB", gaddr.String())
return
}
if c.LocalAddr().String() != gaddr.String() {
t.Fatalf("LocalAddr returns %q, expected %q", c.LocalAddr().String(), gaddr.String())
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
}
}
func checkSimpleMulticastListener(t *testing.T, err error, c *UDPConn, gaddr *UDPAddr) {
if c.LocalAddr().String() != gaddr.String() {
t.Fatalf("LocalAddr returns %q, expected %q", c.LocalAddr().String(), gaddr.String())
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
}
}

View File

@ -33,13 +33,19 @@ func socket(net string, f, t, p int, ipv6only bool, ulsa, ursa syscall.Sockaddr,
return nil, err
}
var blsa syscall.Sockaddr
if ulsa != nil {
if blsa, err = listenerSockaddr(s, f, ulsa, toAddr); err != nil {
// We provide a socket that listens to a wildcard
// address with reusable UDP port when the given ulsa
// is an appropriate UDP multicast address prefix.
// This makes it possible for a single UDP listener
// to join multiple different group addresses, for
// multiple UDP listeners that listen on the same UDP
// port to join the same group address.
if ulsa, err = listenerSockaddr(s, f, ulsa, toAddr); err != nil {
closesocket(s)
return nil, err
}
if err = syscall.Bind(s, blsa); err != nil {
if err = syscall.Bind(s, ulsa); err != nil {
closesocket(s)
return nil, err
}
@ -64,12 +70,7 @@ func socket(net string, f, t, p int, ipv6only bool, ulsa, ursa syscall.Sockaddr,
}
lsa, _ := syscall.Getsockname(s)
var laddr Addr
if ulsa != nil && blsa != ulsa {
laddr = toAddr(ulsa)
} else {
laddr = toAddr(lsa)
}
laddr := toAddr(lsa)
rsa, _ := syscall.Getpeername(s)
raddr := toAddr(rsa)
fd.setAddr(laddr, raddr)