Skip to content

Commit

Permalink
net: revise for go1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jan 27, 2019
1 parent 461c9e3 commit 447668e
Show file tree
Hide file tree
Showing 74 changed files with 2,766 additions and 153 deletions.
2 changes: 1 addition & 1 deletion gosrc/net/addrselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

// Minimal RFC 6724 address selection.

Expand Down
2 changes: 1 addition & 1 deletion gosrc/net/cgo_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func cgoLookupPort(ctx context.Context, network, service string) (port int, err
return 0, nil, false
}

func cgoLookupIP(ctx context.Context, name string) (addrs []IPAddr, err error, completed bool) {
func cgoLookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error, completed bool) {
return nil, nil, false
}

Expand Down
37 changes: 21 additions & 16 deletions gosrc/net/cgo_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type reverseLookupResult struct {
}

func cgoLookupHost(ctx context.Context, name string) (hosts []string, err error, completed bool) {
addrs, err, completed := cgoLookupIP(ctx, name)
addrs, err, completed := cgoLookupIP(ctx, "ip", name)
for _, addr := range addrs {
hosts = append(hosts, addr.String())
}
Expand All @@ -69,13 +69,11 @@ func cgoLookupPort(ctx context.Context, network, service string) (port int, err
default:
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true
}
if len(network) >= 4 {
switch network[3] {
case '4':
hints.ai_family = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
switch ipVersion(network) {
case '4':
hints.ai_family = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
if ctx.Done() == nil {
port, err := cgoLookupServicePort(&hints, network, service)
Expand Down Expand Up @@ -135,13 +133,20 @@ func cgoPortLookup(result chan<- portLookupResult, hints *C.struct_addrinfo, net
result <- portLookupResult{port, err}
}

func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error) {
func cgoLookupIPCNAME(network, name string) (addrs []IPAddr, cname string, err error) {
acquireThread()
defer releaseThread()

var hints C.struct_addrinfo
hints.ai_flags = cgoAddrInfoFlags
hints.ai_socktype = C.SOCK_STREAM
hints.ai_family = C.AF_UNSPEC
switch ipVersion(network) {
case '4':
hints.ai_family = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}

h := make([]byte, len(name)+1)
copy(h, name)
Expand Down Expand Up @@ -197,18 +202,18 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error) {
return addrs, cname, nil
}

func cgoIPLookup(result chan<- ipLookupResult, name string) {
addrs, cname, err := cgoLookupIPCNAME(name)
func cgoIPLookup(result chan<- ipLookupResult, network, name string) {
addrs, cname, err := cgoLookupIPCNAME(network, name)
result <- ipLookupResult{addrs, cname, err}
}

func cgoLookupIP(ctx context.Context, name string) (addrs []IPAddr, err error, completed bool) {
func cgoLookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error, completed bool) {
if ctx.Done() == nil {
addrs, _, err = cgoLookupIPCNAME(name)
addrs, _, err = cgoLookupIPCNAME(network, name)
return addrs, err, true
}
result := make(chan ipLookupResult, 1)
go cgoIPLookup(result, name)
go cgoIPLookup(result, network, name)
select {
case r := <-result:
return r.addrs, r.err, true
Expand All @@ -219,11 +224,11 @@ func cgoLookupIP(ctx context.Context, name string) (addrs []IPAddr, err error, c

func cgoLookupCNAME(ctx context.Context, name string) (cname string, err error, completed bool) {
if ctx.Done() == nil {
_, cname, err = cgoLookupIPCNAME(name)
_, cname, err = cgoLookupIPCNAME("ip", name)
return cname, err, true
}
result := make(chan ipLookupResult, 1)
go cgoIPLookup(result, name)
go cgoIPLookup(result, "ip", name)
select {
case r := <-result:
return r.cname, r.err, true
Expand Down
7 changes: 4 additions & 3 deletions gosrc/net/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

package net

import (
"internal/bytealg"
"os"
"runtime"
"sync"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrde
if c.forceCgoLookupHost || c.resolv.unknownOpt || c.goos == "android" {
return fallbackOrder
}
if byteIndex(hostname, '\\') != -1 || byteIndex(hostname, '%') != -1 {
if bytealg.IndexByteString(hostname, '\\') != -1 || bytealg.IndexByteString(hostname, '%') != -1 {
// Don't deal with special form hostnames with backslashes
// or '%'.
return fallbackOrder
Expand Down Expand Up @@ -301,7 +302,7 @@ func goDebugNetDNS() (dnsMode string, debugLevel int) {
dnsMode = s
}
}
if i := byteIndex(goDebug, '+'); i != -1 {
if i := bytealg.IndexByteString(goDebug, '+'); i != -1 {
parsePart(goDebug[:i])
parsePart(goDebug[i+1:])
return
Expand Down
37 changes: 26 additions & 11 deletions gosrc/net/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,31 @@ type Dialer struct {
// If nil, a local address is automatically chosen.
LocalAddr Addr

// DualStack enables RFC 6555-compliant "Happy Eyeballs"
// dialing when the network is "tcp" and the host in the
// address parameter resolves to both IPv4 and IPv6 addresses.
// This allows a client to tolerate networks where one address
// family is silently broken.
// DualStack previously enabled RFC 6555 Fast Fallback
// support, also known as "Happy Eyeballs", in which IPv4 is
// tried soon if IPv6 appears to be misconfigured and
// hanging.
//
// Deprecated: Fast Fallback is enabled by default. To
// disable, set FallbackDelay to a negative value.
DualStack bool

// FallbackDelay specifies the length of time to wait before
// spawning a fallback connection, when DualStack is enabled.
// spawning a RFC 6555 Fast Fallback connection. That is, this
// is the amount of time to wait for IPv6 to succeed before
// assuming that IPv6 is misconfigured and falling back to
// IPv4.
//
// If zero, a default delay of 300ms is used.
// A negative value disables Fast Fallback support.
FallbackDelay time.Duration

// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are not enabled. Network protocols
// If zero, keep-alives are enabled if supported by the protocol
// and operating system. Network protocols or operating systems
// that do not support keep-alives ignore this field.
// If negative, keep-alives are disabled.
KeepAlive time.Duration

// Resolver optionally specifies an alternate resolver to use.
Expand All @@ -81,6 +90,8 @@ type Dialer struct {
Control func(network, address string, c syscall.RawConn) error
}

func (d *Dialer) dualStack() bool { return d.FallbackDelay >= 0 }

func minNonzeroTime(a, b time.Time) time.Time {
if a.IsZero() {
return b
Expand Down Expand Up @@ -393,7 +404,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn
}

var primaries, fallbacks addrList
if d.DualStack && network == "tcp" {
if d.dualStack() && network == "tcp" {
primaries, fallbacks = addrs.partition(isIPv4)
} else {
primaries = addrs
Expand All @@ -409,10 +420,14 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn
return nil, err
}

if tc, ok := c.(*TCPConn); ok && d.KeepAlive > 0 {
if tc, ok := c.(*TCPConn); ok && d.KeepAlive >= 0 {
setKeepAlive(tc.fd, true)
setKeepAlivePeriod(tc.fd, d.KeepAlive)
testHookSetKeepAlive()
ka := d.KeepAlive
if d.KeepAlive == 0 {
ka = 15 * time.Second
}
setKeepAlivePeriod(tc.fd, ka)
testHookSetKeepAlive(ka)
}
return c, nil
}
Expand Down
17 changes: 9 additions & 8 deletions gosrc/net/dnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math/rand"
"sort"

"golang_org/x/net/dns/dnsmessage"
"internal/x/net/dns/dnsmessage"
)

// reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
Expand All @@ -27,10 +27,10 @@ func reverseaddr(addr string) (arpa string, err error) {
// Add it, in reverse, to the buffer
for i := len(ip) - 1; i >= 0; i-- {
v := ip[i]
buf = append(buf, hexDigit[v&0xF])
buf = append(buf, '.')
buf = append(buf, hexDigit[v>>4])
buf = append(buf, '.')
buf = append(buf, hexDigit[v&0xF],
'.',
hexDigit[v>>4],
'.')
}
// Append "ip6.arpa." and return (buf already has the final .)
buf = append(buf, "ip6.arpa."...)
Expand Down Expand Up @@ -75,15 +75,15 @@ func isDomainName(s string) bool {
}

last := byte('.')
ok := false // Ok once we've seen a letter.
nonNumeric := false // true once we've seen a letter or hyphen
partlen := 0
for i := 0; i < len(s); i++ {
c := s[i]
switch {
default:
return false
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
ok = true
nonNumeric = true
partlen++
case '0' <= c && c <= '9':
// fine
Expand All @@ -94,6 +94,7 @@ func isDomainName(s string) bool {
return false
}
partlen++
nonNumeric = true
case c == '.':
// Byte before dot cannot be dot, dash.
if last == '.' || last == '-' {
Expand All @@ -110,7 +111,7 @@ func isDomainName(s string) bool {
return false
}

return ok
return nonNumeric
}

// absDomainName returns an absolute domain name which ends with a
Expand Down
5 changes: 2 additions & 3 deletions gosrc/net/dnsclient_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

// DNS client: see RFC 1035.
// Has to be linked into package net for Dial.

// TODO(rsc):
// Could potentially handle many outstanding lookups faster.
// Could have a small cache.
// Random UDP source port (net.Dial should do that for us).
// Random request IDs.

Expand All @@ -24,7 +23,7 @@ import (
"sync"
"time"

"golang_org/x/net/dns/dnsmessage"
"internal/x/net/dns/dnsmessage"
)

var (
Expand Down
5 changes: 3 additions & 2 deletions gosrc/net/dnsconfig_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

// Read system DNS config from /etc/resolv.conf

package net

import (
"internal/bytealg"
"os"
"sync/atomic"
"time"
Expand Down Expand Up @@ -155,7 +156,7 @@ func dnsDefaultSearch() []string {
// best effort
return nil
}
if i := byteIndex(hn, '.'); i >= 0 && i < len(hn)-1 {
if i := bytealg.IndexByteString(hn, '.'); i >= 0 && i < len(hn)-1 {
return []string{ensureRooted(hn[i+1:])}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion gosrc/net/error_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows

package net

Expand Down
2 changes: 1 addition & 1 deletion gosrc/net/error_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd js linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js linux netbsd openbsd solaris

package net

Expand Down
2 changes: 1 addition & 1 deletion gosrc/net/fd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
// +build aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris

package net

Expand Down
Loading

0 comments on commit 447668e

Please sign in to comment.