mirror of
https://github.com/golang/go
synced 2024-11-18 16:24:42 -07:00
e94475eab1
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: Brad Fitzpatrick <bradfitz@golang.org>
93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
// Copyright 2009 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 net
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
|
|
return genericReadFrom(c, r)
|
|
}
|
|
|
|
func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
if testHookDialTCP != nil {
|
|
return testHookDialTCP(ctx, sd.network, laddr, raddr)
|
|
}
|
|
return sd.doDialTCP(ctx, laddr, raddr)
|
|
}
|
|
|
|
func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
switch sd.network {
|
|
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:
|
|
return nil, UnknownNetworkError(sd.network)
|
|
}
|
|
if raddr == nil {
|
|
return nil, errMissingAddress
|
|
}
|
|
fd, err := dialPlan9(ctx, sd.network, laddr, raddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newTCPConn(fd), nil
|
|
}
|
|
|
|
func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil && ln.fd.ctl != nil }
|
|
|
|
func (ln *TCPListener) accept() (*TCPConn, error) {
|
|
fd, err := ln.fd.acceptPlan9()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tc := newTCPConn(fd)
|
|
if ln.lc.KeepAlive >= 0 {
|
|
setKeepAlive(fd, true)
|
|
ka := ln.lc.KeepAlive
|
|
if ln.lc.KeepAlive == 0 {
|
|
ka = defaultTCPKeepAlive
|
|
}
|
|
setKeepAlivePeriod(fd, ka)
|
|
}
|
|
return tc, nil
|
|
}
|
|
|
|
func (ln *TCPListener) close() error {
|
|
if err := ln.fd.pfd.Close(); err != nil {
|
|
return err
|
|
}
|
|
if _, err := ln.fd.ctl.WriteString("hangup"); err != nil {
|
|
ln.fd.ctl.Close()
|
|
return err
|
|
}
|
|
if err := ln.fd.ctl.Close(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ln *TCPListener) file() (*os.File, error) {
|
|
f, err := ln.dup()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
|
|
fd, err := listenPlan9(ctx, sl.network, laddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &TCPListener{fd: fd, lc: sl.ListenConfig}, nil
|
|
}
|