Commit e94475ea authored by Fazlul Shahriar's avatar Fazlul Shahriar Committed by Brad Fitzpatrick

net: fix multicast and IPv6 related issues on Plan 9

Fix issues that make these tests pass:
- TestDialerLocalAddr: return error if local address is not IPv4 for
"tcp4" network.
- TestInterfaceAddrs, TestInterfaceUnicastAddrs: don't assume each
interface has only one address. It may have more than one or none.
- TestConcurrentPreferGoResolversDial: should be skipped on Plan 9.
- TestListenMulticastUDP: remove IP from `announce` command and don't
mix IPv4 address with IPv6 address in `addmulti` command.

Fixes #34931

Change-Id: Ie0fdfe19ea282e5d6d6c938bf3c9139f8f5b0308
Reviewed-on: https://go-review.googlesource.com/c/go/+/201397
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent f6c624a2
...@@ -143,8 +143,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) { ...@@ -143,8 +143,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
ifcs = []Interface{*ifi} ifcs = []Interface{*ifi}
} }
addrs := make([]Addr, len(ifcs)) var addrs []Addr
for i, ifc := range ifcs { for _, ifc := range ifcs {
status := ifc.Name + "/status" status := ifc.Name + "/status"
statusf, err := open(status) statusf, err := open(status)
if err != nil { if err != nil {
...@@ -157,12 +157,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) { ...@@ -157,12 +157,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
if _, ok := statusf.readLine(); !ok { if _, ok := statusf.readLine(); !ok {
return nil, errors.New("cannot read header line for interface: " + status) return nil, errors.New("cannot read header line for interface: " + status)
} }
line, ok := statusf.readLine()
if !ok {
return nil, errors.New("cannot read IP address for interface: " + status)
}
// This assumes only a single address for the interface. for line, ok := statusf.readLine(); ok; line, ok = statusf.readLine() {
fields := getFields(line) fields := getFields(line)
if len(fields) < 1 { if len(fields) < 1 {
return nil, errors.New("cannot parse IP address for interface: " + status) return nil, errors.New("cannot parse IP address for interface: " + status)
...@@ -189,7 +185,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) { ...@@ -189,7 +185,8 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
mask = CIDRMask(pfxlen, 8*IPv6len) mask = CIDRMask(pfxlen, 8*IPv6len)
} }
addrs[i] = &IPNet{IP: ip, Mask: mask} addrs = append(addrs, &IPNet{IP: ip, Mask: mask})
}
} }
return addrs, nil return addrs, nil
......
...@@ -227,7 +227,7 @@ func listenPlan9(ctx context.Context, net string, laddr Addr) (fd *netFD, err er ...@@ -227,7 +227,7 @@ func listenPlan9(ctx context.Context, net string, laddr Addr) (fd *netFD, err er
_, err = f.WriteString("announce " + dest) _, err = f.WriteString("announce " + dest)
if err != nil { if err != nil {
f.Close() f.Close()
return nil, err return nil, &OpError{Op: "announce", Net: net, Source: laddr, Addr: nil, Err: err}
} }
laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local") laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local")
if err != nil { if err != nil {
......
...@@ -972,10 +972,11 @@ func (lcr *lookupCustomResolver) dial() func(ctx context.Context, network, addre ...@@ -972,10 +972,11 @@ func (lcr *lookupCustomResolver) dial() func(ctx context.Context, network, addre
// TestConcurrentPreferGoResolversDial tests that multiple resolvers with the // TestConcurrentPreferGoResolversDial tests that multiple resolvers with the
// PreferGo option used concurrently are all dialed properly. // PreferGo option used concurrently are all dialed properly.
func TestConcurrentPreferGoResolversDial(t *testing.T) { func TestConcurrentPreferGoResolversDial(t *testing.T) {
// The windows implementation of the resolver does not use the Dial // The windows and plan9 implementation of the resolver does not use
// function. // the Dial function.
if runtime.GOOS == "windows" { switch runtime.GOOS {
t.Skip("skip on windows") case "windows", "plan9":
t.Skipf("skip on %v", runtime.GOOS)
} }
testenv.MustHaveExternalNetwork(t) testenv.MustHaveExternalNetwork(t)
......
...@@ -23,7 +23,12 @@ func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPCo ...@@ -23,7 +23,12 @@ func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPCo
func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) { func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
switch sd.network { switch sd.network {
case "tcp", "tcp4", "tcp6": case "tcp4":
// Plan 9 doesn't complain about [::]:0->127.0.0.1, so it's up to us.
if laddr != nil && len(laddr.IP) != 0 && laddr.IP.To4() == nil {
return nil, &AddrError{Err: "non-IPv4 local address", Addr: laddr.String()}
}
case "tcp", "tcp6":
default: default:
return nil, UnknownNetworkError(sd.network) return nil, UnknownNetworkError(sd.network)
} }
......
...@@ -109,7 +109,9 @@ func (sl *sysListener) listenUDP(ctx context.Context, laddr *UDPAddr) (*UDPConn, ...@@ -109,7 +109,9 @@ func (sl *sysListener) listenUDP(ctx context.Context, laddr *UDPAddr) (*UDPConn,
} }
func (sl *sysListener) listenMulticastUDP(ctx context.Context, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) { func (sl *sysListener) listenMulticastUDP(ctx context.Context, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
l, err := listenPlan9(ctx, sl.network, gaddr) // Plan 9 does not like announce command with a multicast address,
// so do not specify an IP address when listening.
l, err := listenPlan9(ctx, sl.network, &UDPAddr{IP: nil, Port: gaddr.Port, Zone: gaddr.Zone})
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -129,11 +131,13 @@ func (sl *sysListener) listenMulticastUDP(ctx context.Context, ifi *Interface, g ...@@ -129,11 +131,13 @@ func (sl *sysListener) listenMulticastUDP(ctx context.Context, ifi *Interface, g
return nil, err return nil, err
} }
} }
have4 := gaddr.IP.To4() != nil
for _, addr := range addrs { for _, addr := range addrs {
if ipnet, ok := addr.(*IPNet); ok { if ipnet, ok := addr.(*IPNet); ok && (ipnet.IP.To4() != nil) == have4 {
_, err = l.ctl.WriteString("addmulti " + ipnet.IP.String() + " " + gaddr.IP.String()) _, err = l.ctl.WriteString("addmulti " + ipnet.IP.String() + " " + gaddr.IP.String())
if err != nil { if err != nil {
return nil, err return nil, &OpError{Op: "addmulti", Net: "", Source: nil, Addr: ipnet, Err: err}
} }
} }
} }
......
...@@ -36,7 +36,7 @@ func TestListenMulticastUDP(t *testing.T) { ...@@ -36,7 +36,7 @@ func TestListenMulticastUDP(t *testing.T) {
c1, err := ListenMulticastUDP("udp4", mifc, &UDPAddr{IP: ParseIP("224.0.0.254")}) c1, err := ListenMulticastUDP("udp4", mifc, &UDPAddr{IP: ParseIP("224.0.0.254")})
if err != nil { if err != nil {
t.Fatalf("multicast not working on %s", runtime.GOOS) t.Fatalf("multicast not working on %s: %v", runtime.GOOS, err)
} }
c1addr := c1.LocalAddr().(*UDPAddr) c1addr := c1.LocalAddr().(*UDPAddr)
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