Linux vmi284606.contaboserver.net 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64
Apache/2.4.57 (Ubuntu)
: 167.86.127.34 | : 216.73.217.31
Cant Read [ /etc/named.conf ]
7.2.24-0ubuntu0.18.04.17
root
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
local /
go /
src /
net /
[ HOME SHELL ]
Name
Size
Permission
Action
http
[ DIR ]
drwxr-xr-x
internal
[ DIR ]
drwxr-xr-x
mail
[ DIR ]
drwxr-xr-x
rpc
[ DIR ]
drwxr-xr-x
smtp
[ DIR ]
drwxr-xr-x
testdata
[ DIR ]
drwxr-xr-x
textproto
[ DIR ]
drwxr-xr-x
url
[ DIR ]
drwxr-xr-x
addrselect.go
9.51
KB
-rw-r--r--
addrselect_test.go
6.2
KB
-rw-r--r--
cgo_aix.go
578
B
-rw-r--r--
cgo_android.go
268
B
-rw-r--r--
cgo_bsd.go
348
B
-rw-r--r--
cgo_linux.go
635
B
-rw-r--r--
cgo_netbsd.go
272
B
-rw-r--r--
cgo_openbsd.go
272
B
-rw-r--r--
cgo_resnew.go
577
B
-rw-r--r--
cgo_resold.go
571
B
-rw-r--r--
cgo_socknew.go
748
B
-rw-r--r--
cgo_sockold.go
838
B
-rw-r--r--
cgo_solaris.go
339
B
-rw-r--r--
cgo_stub.go
1.06
KB
-rw-r--r--
cgo_unix.go
9.53
KB
-rw-r--r--
cgo_unix_test.go
1.89
KB
-rw-r--r--
cgo_windows.go
398
B
-rw-r--r--
conf.go
8.88
KB
-rw-r--r--
conf_netcgo.go
369
B
-rw-r--r--
conf_test.go
9.59
KB
-rw-r--r--
conn_test.go
1.77
KB
-rw-r--r--
dial.go
22.44
KB
-rw-r--r--
dial_test.go
27
KB
-rw-r--r--
dial_unix_test.go
2.74
KB
-rw-r--r--
dnsclient.go
5.34
KB
-rw-r--r--
dnsclient_test.go
1.54
KB
-rw-r--r--
dnsclient_unix.go
21.86
KB
-rw-r--r--
dnsclient_unix_test.go
45.17
KB
-rw-r--r--
dnsconfig_unix.go
5.47
KB
-rw-r--r--
dnsconfig_unix_test.go
6.73
KB
-rw-r--r--
dnsname_test.go
1.98
KB
-rw-r--r--
error_plan9.go
224
B
-rw-r--r--
error_plan9_test.go
398
B
-rw-r--r--
error_posix.go
583
B
-rw-r--r--
error_posix_test.go
980
B
-rw-r--r--
error_test.go
19.39
KB
-rw-r--r--
error_unix.go
421
B
-rw-r--r--
error_unix_test.go
672
B
-rw-r--r--
error_windows.go
355
B
-rw-r--r--
error_windows_test.go
483
B
-rw-r--r--
example_test.go
3.16
KB
-rw-r--r--
external_test.go
4.07
KB
-rw-r--r--
fd_plan9.go
3.56
KB
-rw-r--r--
fd_unix.go
7.36
KB
-rw-r--r--
fd_windows.go
6.49
KB
-rw-r--r--
file.go
1.69
KB
-rw-r--r--
file_plan9.go
2.68
KB
-rw-r--r--
file_stub.go
485
B
-rw-r--r--
file_test.go
6.53
KB
-rw-r--r--
file_unix.go
2.51
KB
-rw-r--r--
file_windows.go
521
B
-rw-r--r--
hook.go
628
B
-rw-r--r--
hook_plan9.go
283
B
-rw-r--r--
hook_unix.go
734
B
-rw-r--r--
hook_windows.go
875
B
-rw-r--r--
hosts.go
3.1
KB
-rw-r--r--
hosts_test.go
4.65
KB
-rw-r--r--
interface.go
7.14
KB
-rw-r--r--
interface_aix.go
4.41
KB
-rw-r--r--
interface_bsd.go
2.74
KB
-rw-r--r--
interface_bsd_test.go
1.42
KB
-rw-r--r--
interface_bsdvar.go
711
B
-rw-r--r--
interface_darwin.go
1.29
KB
-rw-r--r--
interface_freebsd.go
1.42
KB
-rw-r--r--
interface_linux.go
6.9
KB
-rw-r--r--
interface_linux_test.go
3.65
KB
-rw-r--r--
interface_plan9.go
4.59
KB
-rw-r--r--
interface_solaris.go
2.46
KB
-rw-r--r--
interface_stub.go
808
B
-rw-r--r--
interface_test.go
9.67
KB
-rw-r--r--
interface_unix_test.go
4.69
KB
-rw-r--r--
interface_windows.go
5.41
KB
-rw-r--r--
ip.go
16.61
KB
-rw-r--r--
ip_test.go
23.96
KB
-rw-r--r--
iprawsock.go
7.08
KB
-rw-r--r--
iprawsock_plan9.go
874
B
-rw-r--r--
iprawsock_posix.go
3.45
KB
-rw-r--r--
iprawsock_test.go
5.62
KB
-rw-r--r--
ipsock.go
8.77
KB
-rw-r--r--
ipsock_plan9.go
6.25
KB
-rw-r--r--
ipsock_posix.go
6.16
KB
-rw-r--r--
ipsock_test.go
6.81
KB
-rw-r--r--
listen_test.go
21.32
KB
-rw-r--r--
lookup.go
15.45
KB
-rw-r--r--
lookup_fake.go
1.64
KB
-rw-r--r--
lookup_plan9.go
7.68
KB
-rw-r--r--
lookup_test.go
29.14
KB
-rw-r--r--
lookup_unix.go
8.78
KB
-rw-r--r--
lookup_windows.go
11.74
KB
-rw-r--r--
lookup_windows_test.go
8
KB
-rw-r--r--
mac.go
1.88
KB
-rw-r--r--
mac_test.go
3.26
KB
-rw-r--r--
main_cloexec_test.go
669
B
-rw-r--r--
main_conf_test.go
915
B
-rw-r--r--
main_noconf_test.go
493
B
-rw-r--r--
main_plan9_test.go
392
B
-rw-r--r--
main_posix_test.go
1.37
KB
-rw-r--r--
main_test.go
7.11
KB
-rw-r--r--
main_unix_test.go
1.24
KB
-rw-r--r--
main_windows_test.go
1.08
KB
-rw-r--r--
mockserver_test.go
11.23
KB
-rw-r--r--
net.go
20.9
KB
-rw-r--r--
net_fake.go
5.34
KB
-rw-r--r--
net_test.go
12.1
KB
-rw-r--r--
net_windows_test.go
15.94
KB
-rw-r--r--
netgo_unix_test.go
631
B
-rw-r--r--
nss.go
3.71
KB
-rw-r--r--
nss_test.go
3.33
KB
-rw-r--r--
packetconn_test.go
3.56
KB
-rw-r--r--
parse.go
7.82
KB
-rw-r--r--
parse_test.go
2.14
KB
-rw-r--r--
pipe.go
5.59
KB
-rw-r--r--
pipe_test.go
1.2
KB
-rw-r--r--
platform_test.go
4.59
KB
-rw-r--r--
port.go
1.46
KB
-rw-r--r--
port_test.go
1.34
KB
-rw-r--r--
port_unix.go
1.28
KB
-rw-r--r--
protoconn_test.go
7.22
KB
-rw-r--r--
rawconn.go
1.89
KB
-rw-r--r--
rawconn_stub_test.go
622
B
-rw-r--r--
rawconn_test.go
4.52
KB
-rw-r--r--
rawconn_unix_test.go
3.15
KB
-rw-r--r--
rawconn_windows_test.go
3.26
KB
-rw-r--r--
sendfile_linux.go
1.1
KB
-rw-r--r--
sendfile_stub.go
329
B
-rw-r--r--
sendfile_test.go
5.69
KB
-rw-r--r--
sendfile_unix_alt.go
1.98
KB
-rw-r--r--
sendfile_windows.go
1.02
KB
-rw-r--r--
server_test.go
11.55
KB
-rw-r--r--
sock_bsd.go
898
B
-rw-r--r--
sock_cloexec.go
1.46
KB
-rw-r--r--
sock_linux.go
644
B
-rw-r--r--
sock_plan9.go
262
B
-rw-r--r--
sock_posix.go
6.83
KB
-rw-r--r--
sock_stub.go
378
B
-rw-r--r--
sock_windows.go
1.16
KB
-rw-r--r--
sockaddr_posix.go
955
B
-rw-r--r--
sockopt_aix.go
1.32
KB
-rw-r--r--
sockopt_bsd.go
2.09
KB
-rw-r--r--
sockopt_linux.go
1.14
KB
-rw-r--r--
sockopt_plan9.go
406
B
-rw-r--r--
sockopt_posix.go
2.67
KB
-rw-r--r--
sockopt_solaris.go
1.14
KB
-rw-r--r--
sockopt_stub.go
738
B
-rw-r--r--
sockopt_windows.go
1.34
KB
-rw-r--r--
sockoptip_bsdvar.go
848
B
-rw-r--r--
sockoptip_linux.go
735
B
-rw-r--r--
sockoptip_posix.go
1.43
KB
-rw-r--r--
sockoptip_stub.go
949
B
-rw-r--r--
sockoptip_windows.go
819
B
-rw-r--r--
splice_linux.go
1.09
KB
-rw-r--r--
splice_stub.go
286
B
-rw-r--r--
splice_test.go
10.51
KB
-rw-r--r--
sys_cloexec.go
977
B
-rw-r--r--
tcpsock.go
9.85
KB
-rw-r--r--
tcpsock_plan9.go
2.13
KB
-rw-r--r--
tcpsock_posix.go
5.27
KB
-rw-r--r--
tcpsock_test.go
17.51
KB
-rw-r--r--
tcpsock_unix_test.go
2.38
KB
-rw-r--r--
tcpsockopt_darwin.go
770
B
-rw-r--r--
tcpsockopt_dragonfly.go
698
B
-rw-r--r--
tcpsockopt_openbsd.go
365
B
-rw-r--r--
tcpsockopt_plan9.go
503
B
-rw-r--r--
tcpsockopt_posix.go
491
B
-rw-r--r--
tcpsockopt_solaris.go
1.15
KB
-rw-r--r--
tcpsockopt_stub.go
393
B
-rw-r--r--
tcpsockopt_unix.go
712
B
-rw-r--r--
tcpsockopt_windows.go
741
B
-rw-r--r--
timeout_test.go
23.18
KB
-rw-r--r--
udpsock.go
8.7
KB
-rw-r--r--
udpsock_plan9.go
3.77
KB
-rw-r--r--
udpsock_plan9_test.go
1.31
KB
-rw-r--r--
udpsock_posix.go
4.09
KB
-rw-r--r--
udpsock_test.go
11.18
KB
-rw-r--r--
unixsock.go
10.17
KB
-rw-r--r--
unixsock_linux_test.go
2.29
KB
-rw-r--r--
unixsock_plan9.go
1.24
KB
-rw-r--r--
unixsock_posix.go
5.81
KB
-rw-r--r--
unixsock_test.go
10.58
KB
-rw-r--r--
unixsock_windows_test.go
2.07
KB
-rw-r--r--
write_unix_test.go
1.59
KB
-rw-r--r--
writev_test.go
5.01
KB
-rw-r--r--
writev_unix.go
706
B
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : dial.go
// Copyright 2010 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" "internal/nettrace" "internal/poll" "syscall" "time" ) // defaultTCPKeepAlive is a default constant value for TCPKeepAlive times // See golang.org/issue/31510 const ( defaultTCPKeepAlive = 15 * time.Second ) // A Dialer contains options for connecting to an address. // // The zero value for each field is equivalent to dialing // without that option. Dialing with the zero value of Dialer // is therefore equivalent to just calling the Dial function. // // It is safe to call Dialer's methods concurrently. type Dialer struct { // Timeout is the maximum amount of time a dial will wait for // a connect to complete. If Deadline is also set, it may fail // earlier. // // The default is no timeout. // // When using TCP and dialing a host name with multiple IP // addresses, the timeout may be divided between them. // // With or without a timeout, the operating system may impose // its own earlier timeout. For instance, TCP timeouts are // often around 3 minutes. Timeout time.Duration // Deadline is the absolute point in time after which dials // will fail. If Timeout is set, it may fail earlier. // Zero means no deadline, or dependent on the operating system // as with the Timeout option. Deadline time.Time // LocalAddr is the local address to use when dialing an // address. The address must be of a compatible type for the // network being dialed. // If nil, a local address is automatically chosen. LocalAddr Addr // 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 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 interval between keep-alive // probes for an active network connection. // If zero, keep-alive probes are sent with a default value // (currently 15 seconds), 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-alive probes are disabled. KeepAlive time.Duration // Resolver optionally specifies an alternate resolver to use. Resolver *Resolver // Cancel is an optional channel whose closure indicates that // the dial should be canceled. Not all types of dials support // cancellation. // // Deprecated: Use DialContext instead. Cancel <-chan struct{} // If Control is not nil, it is called after creating the network // connection but before actually dialing. // // Network and address parameters passed to Control method are not // necessarily the ones passed to Dial. For example, passing "tcp" to Dial // will cause the Control function to be called with "tcp4" or "tcp6". 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 } if b.IsZero() || a.Before(b) { return a } return b } // deadline returns the earliest of: // - now+Timeout // - d.Deadline // - the context's deadline // Or zero, if none of Timeout, Deadline, or context's deadline is set. func (d *Dialer) deadline(ctx context.Context, now time.Time) (earliest time.Time) { if d.Timeout != 0 { // including negative, for historical reasons earliest = now.Add(d.Timeout) } if d, ok := ctx.Deadline(); ok { earliest = minNonzeroTime(earliest, d) } return minNonzeroTime(earliest, d.Deadline) } func (d *Dialer) resolver() *Resolver { if d.Resolver != nil { return d.Resolver } return DefaultResolver } // partialDeadline returns the deadline to use for a single address, // when multiple addresses are pending. func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) { if deadline.IsZero() { return deadline, nil } timeRemaining := deadline.Sub(now) if timeRemaining <= 0 { return time.Time{}, poll.ErrTimeout } // Tentatively allocate equal time to each remaining address. timeout := timeRemaining / time.Duration(addrsRemaining) // If the time per address is too short, steal from the end of the list. const saneMinimum = 2 * time.Second if timeout < saneMinimum { if timeRemaining < saneMinimum { timeout = timeRemaining } else { timeout = saneMinimum } } return now.Add(timeout), nil } func (d *Dialer) fallbackDelay() time.Duration { if d.FallbackDelay > 0 { return d.FallbackDelay } else { return 300 * time.Millisecond } } func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) { i := last(network, ':') if i < 0 { // no colon switch network { case "tcp", "tcp4", "tcp6": case "udp", "udp4", "udp6": case "ip", "ip4", "ip6": if needsProto { return "", 0, UnknownNetworkError(network) } case "unix", "unixgram", "unixpacket": default: return "", 0, UnknownNetworkError(network) } return network, 0, nil } afnet = network[:i] switch afnet { case "ip", "ip4", "ip6": protostr := network[i+1:] proto, i, ok := dtoi(protostr) if !ok || i != len(protostr) { proto, err = lookupProtocol(ctx, protostr) if err != nil { return "", 0, err } } return afnet, proto, nil } return "", 0, UnknownNetworkError(network) } // resolveAddrList resolves addr using hint and returns a list of // addresses. The result contains at least one address when error is // nil. func (r *Resolver) resolveAddrList(ctx context.Context, op, network, addr string, hint Addr) (addrList, error) { afnet, _, err := parseNetwork(ctx, network, true) if err != nil { return nil, err } if op == "dial" && addr == "" { return nil, errMissingAddress } switch afnet { case "unix", "unixgram", "unixpacket": addr, err := ResolveUnixAddr(afnet, addr) if err != nil { return nil, err } if op == "dial" && hint != nil && addr.Network() != hint.Network() { return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()} } return addrList{addr}, nil } addrs, err := r.internetAddrList(ctx, afnet, addr) if err != nil || op != "dial" || hint == nil { return addrs, err } var ( tcp *TCPAddr udp *UDPAddr ip *IPAddr wildcard bool ) switch hint := hint.(type) { case *TCPAddr: tcp = hint wildcard = tcp.isWildcard() case *UDPAddr: udp = hint wildcard = udp.isWildcard() case *IPAddr: ip = hint wildcard = ip.isWildcard() } naddrs := addrs[:0] for _, addr := range addrs { if addr.Network() != hint.Network() { return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()} } switch addr := addr.(type) { case *TCPAddr: if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(tcp.IP) { continue } naddrs = append(naddrs, addr) case *UDPAddr: if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(udp.IP) { continue } naddrs = append(naddrs, addr) case *IPAddr: if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(ip.IP) { continue } naddrs = append(naddrs, addr) } } if len(naddrs) == 0 { return nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: hint.String()} } return naddrs, nil } // Dial connects to the address on the named network. // // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and // "unixpacket". // // For TCP and UDP networks, the address has the form "host:port". // The host must be a literal IP address, or a host name that can be // resolved to IP addresses. // The port must be a literal port number or a service name. // If the host is a literal IPv6 address it must be enclosed in square // brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80". // The zone specifies the scope of the literal IPv6 address as defined // in RFC 4007. // The functions JoinHostPort and SplitHostPort manipulate a pair of // host and port in this form. // When using TCP, and the host resolves to multiple IP addresses, // Dial will try each IP address in order until one succeeds. // // Examples: // Dial("tcp", "golang.org:http") // Dial("tcp", "192.0.2.1:http") // Dial("tcp", "198.51.100.1:80") // Dial("udp", "[2001:db8::1]:domain") // Dial("udp", "[fe80::1%lo0]:53") // Dial("tcp", ":80") // // For IP networks, the network must be "ip", "ip4" or "ip6" followed // by a colon and a literal protocol number or a protocol name, and // the address has the form "host". The host must be a literal IP // address or a literal IPv6 address with zone. // It depends on each operating system how the operating system // behaves with a non-well known protocol number such as "0" or "255". // // Examples: // Dial("ip4:1", "192.0.2.1") // Dial("ip6:ipv6-icmp", "2001:db8::1") // Dial("ip6:58", "fe80::1%lo0") // // For TCP, UDP and IP networks, if the host is empty or a literal // unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for // TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is // assumed. // // For Unix networks, the address must be a file system path. func Dial(network, address string) (Conn, error) { var d Dialer return d.Dial(network, address) } // DialTimeout acts like Dial but takes a timeout. // // The timeout includes name resolution, if required. // When using TCP, and the host in the address parameter resolves to // multiple IP addresses, the timeout is spread over each consecutive // dial, such that each is given an appropriate fraction of the time // to connect. // // See func Dial for a description of the network and address // parameters. func DialTimeout(network, address string, timeout time.Duration) (Conn, error) { d := Dialer{Timeout: timeout} return d.Dial(network, address) } // sysDialer contains a Dial's parameters and configuration. type sysDialer struct { Dialer network, address string } // Dial connects to the address on the named network. // // See func Dial for a description of the network and address // parameters. func (d *Dialer) Dial(network, address string) (Conn, error) { return d.DialContext(context.Background(), network, address) } // DialContext connects to the address on the named network using // the provided context. // // The provided Context must be non-nil. If the context expires before // the connection is complete, an error is returned. Once successfully // connected, any expiration of the context will not affect the // connection. // // When using TCP, and the host in the address parameter resolves to multiple // network addresses, any dial timeout (from d.Timeout or ctx) is spread // over each consecutive dial, such that each is given an appropriate // fraction of the time to connect. // For example, if a host has 4 IP addresses and the timeout is 1 minute, // the connect to each single address will be given 15 seconds to complete // before trying the next one. // // See func Dial for a description of the network and address // parameters. func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) { if ctx == nil { panic("nil context") } deadline := d.deadline(ctx, time.Now()) if !deadline.IsZero() { if d, ok := ctx.Deadline(); !ok || deadline.Before(d) { subCtx, cancel := context.WithDeadline(ctx, deadline) defer cancel() ctx = subCtx } } if oldCancel := d.Cancel; oldCancel != nil { subCtx, cancel := context.WithCancel(ctx) defer cancel() go func() { select { case <-oldCancel: cancel() case <-subCtx.Done(): } }() ctx = subCtx } // Shadow the nettrace (if any) during resolve so Connect events don't fire for DNS lookups. resolveCtx := ctx if trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace); trace != nil { shadow := *trace shadow.ConnectStart = nil shadow.ConnectDone = nil resolveCtx = context.WithValue(resolveCtx, nettrace.TraceKey{}, &shadow) } addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr) if err != nil { return nil, &OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: err} } sd := &sysDialer{ Dialer: *d, network: network, address: address, } var primaries, fallbacks addrList if d.dualStack() && network == "tcp" { primaries, fallbacks = addrs.partition(isIPv4) } else { primaries = addrs } var c Conn if len(fallbacks) > 0 { c, err = sd.dialParallel(ctx, primaries, fallbacks) } else { c, err = sd.dialSerial(ctx, primaries) } if err != nil { return nil, err } if tc, ok := c.(*TCPConn); ok && d.KeepAlive >= 0 { setKeepAlive(tc.fd, true) ka := d.KeepAlive if d.KeepAlive == 0 { ka = defaultTCPKeepAlive } setKeepAlivePeriod(tc.fd, ka) testHookSetKeepAlive(ka) } return c, nil } // dialParallel races two copies of dialSerial, giving the first a // head start. It returns the first established connection and // closes the others. Otherwise it returns an error from the first // primary address. func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addrList) (Conn, error) { if len(fallbacks) == 0 { return sd.dialSerial(ctx, primaries) } returned := make(chan struct{}) defer close(returned) type dialResult struct { Conn error primary bool done bool } results := make(chan dialResult) // unbuffered startRacer := func(ctx context.Context, primary bool) { ras := primaries if !primary { ras = fallbacks } c, err := sd.dialSerial(ctx, ras) select { case results <- dialResult{Conn: c, error: err, primary: primary, done: true}: case <-returned: if c != nil { c.Close() } } } var primary, fallback dialResult // Start the main racer. primaryCtx, primaryCancel := context.WithCancel(ctx) defer primaryCancel() go startRacer(primaryCtx, true) // Start the timer for the fallback racer. fallbackTimer := time.NewTimer(sd.fallbackDelay()) defer fallbackTimer.Stop() for { select { case <-fallbackTimer.C: fallbackCtx, fallbackCancel := context.WithCancel(ctx) defer fallbackCancel() go startRacer(fallbackCtx, false) case res := <-results: if res.error == nil { return res.Conn, nil } if res.primary { primary = res } else { fallback = res } if primary.done && fallback.done { return nil, primary.error } if res.primary && fallbackTimer.Stop() { // If we were able to stop the timer, that means it // was running (hadn't yet started the fallback), but // we just got an error on the primary path, so start // the fallback immediately (in 0 nanoseconds). fallbackTimer.Reset(0) } } } } // dialSerial connects to a list of addresses in sequence, returning // either the first successful connection, or the first error. func (sd *sysDialer) dialSerial(ctx context.Context, ras addrList) (Conn, error) { var firstErr error // The error from the first address is most relevant. for i, ra := range ras { select { case <-ctx.Done(): return nil, &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: mapErr(ctx.Err())} default: } dialCtx := ctx if deadline, hasDeadline := ctx.Deadline(); hasDeadline { partialDeadline, err := partialDeadline(time.Now(), deadline, len(ras)-i) if err != nil { // Ran out of time. if firstErr == nil { firstErr = &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: err} } break } if partialDeadline.Before(deadline) { var cancel context.CancelFunc dialCtx, cancel = context.WithDeadline(ctx, partialDeadline) defer cancel() } } c, err := sd.dialSingle(dialCtx, ra) if err == nil { return c, nil } if firstErr == nil { firstErr = err } } if firstErr == nil { firstErr = &OpError{Op: "dial", Net: sd.network, Source: nil, Addr: nil, Err: errMissingAddress} } return nil, firstErr } // dialSingle attempts to establish and returns a single connection to // the destination address. func (sd *sysDialer) dialSingle(ctx context.Context, ra Addr) (c Conn, err error) { trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace) if trace != nil { raStr := ra.String() if trace.ConnectStart != nil { trace.ConnectStart(sd.network, raStr) } if trace.ConnectDone != nil { defer func() { trace.ConnectDone(sd.network, raStr, err) }() } } la := sd.LocalAddr switch ra := ra.(type) { case *TCPAddr: la, _ := la.(*TCPAddr) c, err = sd.dialTCP(ctx, la, ra) case *UDPAddr: la, _ := la.(*UDPAddr) c, err = sd.dialUDP(ctx, la, ra) case *IPAddr: la, _ := la.(*IPAddr) c, err = sd.dialIP(ctx, la, ra) case *UnixAddr: la, _ := la.(*UnixAddr) c, err = sd.dialUnix(ctx, la, ra) default: return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: sd.address}} } if err != nil { return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer } return c, nil } // ListenConfig contains options for listening to an address. type ListenConfig struct { // If Control is not nil, it is called after creating the network // connection but before binding it to the operating system. // // Network and address parameters passed to Control method are not // necessarily the ones passed to Listen. For example, passing "tcp" to // Listen will cause the Control function to be called with "tcp4" or "tcp6". Control func(network, address string, c syscall.RawConn) error // KeepAlive specifies the keep-alive period for network // connections accepted by this listener. // 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 } // Listen announces on the local network address. // // See func Listen for a description of the network and address // parameters. func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) { addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil) if err != nil { return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err} } sl := &sysListener{ ListenConfig: *lc, network: network, address: address, } var l Listener la := addrs.first(isIPv4) switch la := la.(type) { case *TCPAddr: l, err = sl.listenTCP(ctx, la) case *UnixAddr: l, err = sl.listenUnix(ctx, la) default: return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}} } if err != nil { return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // l is non-nil interface containing nil pointer } return l, nil } // ListenPacket announces on the local network address. // // See func ListenPacket for a description of the network and address // parameters. func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) { addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil) if err != nil { return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err} } sl := &sysListener{ ListenConfig: *lc, network: network, address: address, } var c PacketConn la := addrs.first(isIPv4) switch la := la.(type) { case *UDPAddr: c, err = sl.listenUDP(ctx, la) case *IPAddr: c, err = sl.listenIP(ctx, la) case *UnixAddr: c, err = sl.listenUnixgram(ctx, la) default: return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}} } if err != nil { return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // c is non-nil interface containing nil pointer } return c, nil } // sysListener contains a Listen's parameters and configuration. type sysListener struct { ListenConfig network, address string } // Listen announces on the local network address. // // The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket". // // For TCP networks, if the host in the address parameter is empty or // a literal unspecified IP address, Listen listens on all available // unicast and anycast IP addresses of the local system. // To only use IPv4, use network "tcp4". // The address can use a host name, but this is not recommended, // because it will create a listener for at most one of the host's IP // addresses. // If the port in the address parameter is empty or "0", as in // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. // The Addr method of Listener can be used to discover the chosen // port. // // See func Dial for a description of the network and address // parameters. func Listen(network, address string) (Listener, error) { var lc ListenConfig return lc.Listen(context.Background(), network, address) } // ListenPacket announces on the local network address. // // The network must be "udp", "udp4", "udp6", "unixgram", or an IP // transport. The IP transports are "ip", "ip4", or "ip6" followed by // a colon and a literal protocol number or a protocol name, as in // "ip:1" or "ip:icmp". // // For UDP and IP networks, if the host in the address parameter is // empty or a literal unspecified IP address, ListenPacket listens on // all available IP addresses of the local system except multicast IP // addresses. // To only use IPv4, use network "udp4" or "ip4:proto". // The address can use a host name, but this is not recommended, // because it will create a listener for at most one of the host's IP // addresses. // If the port in the address parameter is empty or "0", as in // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. // The LocalAddr method of PacketConn can be used to discover the // chosen port. // // See func Dial for a description of the network and address // parameters. func ListenPacket(network, address string) (PacketConn, error) { var lc ListenConfig return lc.ListenPacket(context.Background(), network, address) }
Close