1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
17 // See go.dev/issue/31510
18 defaultTCPKeepAlive = 15 * time.Second
20 // For the moment, MultiPath TCP is not used by default
21 // See go.dev/issue/56539
22 defaultMPTCPEnabled = false
25 var multipathtcp = godebug.New("multipathtcp")
27 // mptcpStatus is a tristate for Multipath TCP, see go.dev/issue/56539
28 type mptcpStatus uint8
31 // The value 0 is the system default, linked to defaultMPTCPEnabled
32 mptcpUseDefault mptcpStatus = iota
37 func (m *mptcpStatus) get() bool {
45 // If MPTCP is forced via GODEBUG=multipathtcp=1
46 if multipathtcp.Value() == "1" {
47 multipathtcp.IncNonDefault()
52 return defaultMPTCPEnabled
55 func (m *mptcpStatus) set(use bool) {
63 // A Dialer contains options for connecting to an address.
65 // The zero value for each field is equivalent to dialing
66 // without that option. Dialing with the zero value of Dialer
67 // is therefore equivalent to just calling the Dial function.
69 // It is safe to call Dialer's methods concurrently.
71 // Timeout is the maximum amount of time a dial will wait for
72 // a connect to complete. If Deadline is also set, it may fail
75 // The default is no timeout.
77 // When using TCP and dialing a host name with multiple IP
78 // addresses, the timeout may be divided between them.
80 // With or without a timeout, the operating system may impose
81 // its own earlier timeout. For instance, TCP timeouts are
82 // often around 3 minutes.
85 // Deadline is the absolute point in time after which dials
86 // will fail. If Timeout is set, it may fail earlier.
87 // Zero means no deadline, or dependent on the operating system
88 // as with the Timeout option.
91 // LocalAddr is the local address to use when dialing an
92 // address. The address must be of a compatible type for the
93 // network being dialed.
94 // If nil, a local address is automatically chosen.
97 // DualStack previously enabled RFC 6555 Fast Fallback
98 // support, also known as "Happy Eyeballs", in which IPv4 is
99 // tried soon if IPv6 appears to be misconfigured and
102 // Deprecated: Fast Fallback is enabled by default. To
103 // disable, set FallbackDelay to a negative value.
106 // FallbackDelay specifies the length of time to wait before
107 // spawning a RFC 6555 Fast Fallback connection. That is, this
108 // is the amount of time to wait for IPv6 to succeed before
109 // assuming that IPv6 is misconfigured and falling back to
112 // If zero, a default delay of 300ms is used.
113 // A negative value disables Fast Fallback support.
114 FallbackDelay time.Duration
116 // KeepAlive specifies the interval between keep-alive
117 // probes for an active network connection.
118 // If zero, keep-alive probes are sent with a default value
119 // (currently 15 seconds), if supported by the protocol and operating
120 // system. Network protocols or operating systems that do
121 // not support keep-alives ignore this field.
122 // If negative, keep-alive probes are disabled.
123 KeepAlive time.Duration
125 // Resolver optionally specifies an alternate resolver to use.
128 // Cancel is an optional channel whose closure indicates that
129 // the dial should be canceled. Not all types of dials support
132 // Deprecated: Use DialContext instead.
133 Cancel <-chan struct{}
135 // If Control is not nil, it is called after creating the network
136 // connection but before actually dialing.
138 // Network and address parameters passed to Control function are not
139 // necessarily the ones passed to Dial. For example, passing "tcp" to Dial
140 // will cause the Control function to be called with "tcp4" or "tcp6".
142 // Control is ignored if ControlContext is not nil.
143 Control func(network, address string, c syscall.RawConn) error
145 // If ControlContext is not nil, it is called after creating the network
146 // connection but before actually dialing.
148 // Network and address parameters passed to ControlContext function are not
149 // necessarily the ones passed to Dial. For example, passing "tcp" to Dial
150 // will cause the ControlContext function to be called with "tcp4" or "tcp6".
152 // If ControlContext is not nil, Control is ignored.
153 ControlContext func(ctx context.Context, network, address string, c syscall.RawConn) error
155 // If mptcpStatus is set to a value allowing Multipath TCP (MPTCP) to be
156 // used, any call to Dial with "tcp(4|6)" as network will use MPTCP if
157 // supported by the operating system.
158 mptcpStatus mptcpStatus
161 func (d *Dialer) dualStack() bool { return d.FallbackDelay >= 0 }
163 func minNonzeroTime(a, b time.Time) time.Time {
167 if b.IsZero() || a.Before(b) {
173 // deadline returns the earliest of:
176 // - the context's deadline
178 // Or zero, if none of Timeout, Deadline, or context's deadline is set.
179 func (d *Dialer) deadline(ctx context.Context, now time.Time) (earliest time.Time) {
180 if d.Timeout != 0 { // including negative, for historical reasons
181 earliest = now.Add(d.Timeout)
183 if d, ok := ctx.Deadline(); ok {
184 earliest = minNonzeroTime(earliest, d)
186 return minNonzeroTime(earliest, d.Deadline)
189 func (d *Dialer) resolver() *Resolver {
190 if d.Resolver != nil {
193 return DefaultResolver
196 // partialDeadline returns the deadline to use for a single address,
197 // when multiple addresses are pending.
198 func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) {
199 if deadline.IsZero() {
202 timeRemaining := deadline.Sub(now)
203 if timeRemaining <= 0 {
204 return time.Time{}, errTimeout
206 // Tentatively allocate equal time to each remaining address.
207 timeout := timeRemaining / time.Duration(addrsRemaining)
208 // If the time per address is too short, steal from the end of the list.
209 const saneMinimum = 2 * time.Second
210 if timeout < saneMinimum {
211 if timeRemaining < saneMinimum {
212 timeout = timeRemaining
214 timeout = saneMinimum
217 return now.Add(timeout), nil
220 func (d *Dialer) fallbackDelay() time.Duration {
221 if d.FallbackDelay > 0 {
222 return d.FallbackDelay
224 return 300 * time.Millisecond
228 func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
229 i := last(network, ':')
230 if i < 0 { // no colon
232 case "tcp", "tcp4", "tcp6":
233 case "udp", "udp4", "udp6":
234 case "ip", "ip4", "ip6":
236 return "", 0, UnknownNetworkError(network)
238 case "unix", "unixgram", "unixpacket":
240 return "", 0, UnknownNetworkError(network)
242 return network, 0, nil
246 case "ip", "ip4", "ip6":
247 protostr := network[i+1:]
248 proto, i, ok := dtoi(protostr)
249 if !ok || i != len(protostr) {
250 proto, err = lookupProtocol(ctx, protostr)
255 return afnet, proto, nil
257 return "", 0, UnknownNetworkError(network)
260 // resolveAddrList resolves addr using hint and returns a list of
261 // addresses. The result contains at least one address when error is
263 func (r *Resolver) resolveAddrList(ctx context.Context, op, network, addr string, hint Addr) (addrList, error) {
264 afnet, _, err := parseNetwork(ctx, network, true)
268 if op == "dial" && addr == "" {
269 return nil, errMissingAddress
272 case "unix", "unixgram", "unixpacket":
273 addr, err := ResolveUnixAddr(afnet, addr)
277 if op == "dial" && hint != nil && addr.Network() != hint.Network() {
278 return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
280 return addrList{addr}, nil
282 addrs, err := r.internetAddrList(ctx, afnet, addr)
283 if err != nil || op != "dial" || hint == nil {
292 switch hint := hint.(type) {
295 wildcard = tcp.isWildcard()
298 wildcard = udp.isWildcard()
301 wildcard = ip.isWildcard()
304 for _, addr := range addrs {
305 if addr.Network() != hint.Network() {
306 return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
308 switch addr := addr.(type) {
310 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(tcp.IP) {
313 naddrs = append(naddrs, addr)
315 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(udp.IP) {
318 naddrs = append(naddrs, addr)
320 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(ip.IP) {
323 naddrs = append(naddrs, addr)
326 if len(naddrs) == 0 {
327 return nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: hint.String()}
332 // MultipathTCP reports whether MPTCP will be used.
334 // This method doesn't check if MPTCP is supported by the operating
336 func (d *Dialer) MultipathTCP() bool {
337 return d.mptcpStatus.get()
340 // SetMultipathTCP directs the Dial methods to use, or not use, MPTCP,
341 // if supported by the operating system. This method overrides the
342 // system default and the GODEBUG=multipathtcp=... setting if any.
344 // If MPTCP is not available on the host or not supported by the server,
345 // the Dial methods will fall back to TCP.
346 func (d *Dialer) SetMultipathTCP(use bool) {
347 d.mptcpStatus.set(use)
350 // Dial connects to the address on the named network.
352 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
353 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
354 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
357 // For TCP and UDP networks, the address has the form "host:port".
358 // The host must be a literal IP address, or a host name that can be
359 // resolved to IP addresses.
360 // The port must be a literal port number or a service name.
361 // If the host is a literal IPv6 address it must be enclosed in square
362 // brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80".
363 // The zone specifies the scope of the literal IPv6 address as defined
365 // The functions JoinHostPort and SplitHostPort manipulate a pair of
366 // host and port in this form.
367 // When using TCP, and the host resolves to multiple IP addresses,
368 // Dial will try each IP address in order until one succeeds.
372 // Dial("tcp", "golang.org:http")
373 // Dial("tcp", "192.0.2.1:http")
374 // Dial("tcp", "198.51.100.1:80")
375 // Dial("udp", "[2001:db8::1]:domain")
376 // Dial("udp", "[fe80::1%lo0]:53")
377 // Dial("tcp", ":80")
379 // For IP networks, the network must be "ip", "ip4" or "ip6" followed
380 // by a colon and a literal protocol number or a protocol name, and
381 // the address has the form "host". The host must be a literal IP
382 // address or a literal IPv6 address with zone.
383 // It depends on each operating system how the operating system
384 // behaves with a non-well known protocol number such as "0" or "255".
388 // Dial("ip4:1", "192.0.2.1")
389 // Dial("ip6:ipv6-icmp", "2001:db8::1")
390 // Dial("ip6:58", "fe80::1%lo0")
392 // For TCP, UDP and IP networks, if the host is empty or a literal
393 // unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for
394 // TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is
397 // For Unix networks, the address must be a file system path.
398 func Dial(network, address string) (Conn, error) {
400 return d.Dial(network, address)
403 // DialTimeout acts like Dial but takes a timeout.
405 // The timeout includes name resolution, if required.
406 // When using TCP, and the host in the address parameter resolves to
407 // multiple IP addresses, the timeout is spread over each consecutive
408 // dial, such that each is given an appropriate fraction of the time
411 // See func Dial for a description of the network and address
413 func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
414 d := Dialer{Timeout: timeout}
415 return d.Dial(network, address)
418 // sysDialer contains a Dial's parameters and configuration.
419 type sysDialer struct {
421 network, address string
422 testHookDialTCP func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)
425 // Dial connects to the address on the named network.
427 // See func Dial for a description of the network and address
430 // Dial uses context.Background internally; to specify the context, use
432 func (d *Dialer) Dial(network, address string) (Conn, error) {
433 return d.DialContext(context.Background(), network, address)
436 // DialContext connects to the address on the named network using
437 // the provided context.
439 // The provided Context must be non-nil. If the context expires before
440 // the connection is complete, an error is returned. Once successfully
441 // connected, any expiration of the context will not affect the
444 // When using TCP, and the host in the address parameter resolves to multiple
445 // network addresses, any dial timeout (from d.Timeout or ctx) is spread
446 // over each consecutive dial, such that each is given an appropriate
447 // fraction of the time to connect.
448 // For example, if a host has 4 IP addresses and the timeout is 1 minute,
449 // the connect to each single address will be given 15 seconds to complete
450 // before trying the next one.
452 // See func Dial for a description of the network and address
454 func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
458 deadline := d.deadline(ctx, time.Now())
459 if !deadline.IsZero() {
460 if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
461 subCtx, cancel := context.WithDeadline(ctx, deadline)
466 if oldCancel := d.Cancel; oldCancel != nil {
467 subCtx, cancel := context.WithCancel(ctx)
473 case <-subCtx.Done():
479 // Shadow the nettrace (if any) during resolve so Connect events don't fire for DNS lookups.
481 if trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace); trace != nil {
483 shadow.ConnectStart = nil
484 shadow.ConnectDone = nil
485 resolveCtx = context.WithValue(resolveCtx, nettrace.TraceKey{}, &shadow)
488 addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr)
490 return nil, &OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: err}
499 var primaries, fallbacks addrList
500 if d.dualStack() && network == "tcp" {
501 primaries, fallbacks = addrs.partition(isIPv4)
506 return sd.dialParallel(ctx, primaries, fallbacks)
509 // dialParallel races two copies of dialSerial, giving the first a
510 // head start. It returns the first established connection and
511 // closes the others. Otherwise it returns an error from the first
513 func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addrList) (Conn, error) {
514 if len(fallbacks) == 0 {
515 return sd.dialSerial(ctx, primaries)
518 returned := make(chan struct{})
519 defer close(returned)
521 type dialResult struct {
527 results := make(chan dialResult) // unbuffered
529 startRacer := func(ctx context.Context, primary bool) {
534 c, err := sd.dialSerial(ctx, ras)
536 case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
544 var primary, fallback dialResult
546 // Start the main racer.
547 primaryCtx, primaryCancel := context.WithCancel(ctx)
548 defer primaryCancel()
549 go startRacer(primaryCtx, true)
551 // Start the timer for the fallback racer.
552 fallbackTimer := time.NewTimer(sd.fallbackDelay())
553 defer fallbackTimer.Stop()
557 case <-fallbackTimer.C:
558 fallbackCtx, fallbackCancel := context.WithCancel(ctx)
559 defer fallbackCancel()
560 go startRacer(fallbackCtx, false)
562 case res := <-results:
563 if res.error == nil {
571 if primary.done && fallback.done {
572 return nil, primary.error
574 if res.primary && fallbackTimer.Stop() {
575 // If we were able to stop the timer, that means it
576 // was running (hadn't yet started the fallback), but
577 // we just got an error on the primary path, so start
578 // the fallback immediately (in 0 nanoseconds).
579 fallbackTimer.Reset(0)
585 // dialSerial connects to a list of addresses in sequence, returning
586 // either the first successful connection, or the first error.
587 func (sd *sysDialer) dialSerial(ctx context.Context, ras addrList) (Conn, error) {
588 var firstErr error // The error from the first address is most relevant.
590 for i, ra := range ras {
593 return nil, &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: mapErr(ctx.Err())}
598 if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
599 partialDeadline, err := partialDeadline(time.Now(), deadline, len(ras)-i)
603 firstErr = &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: err}
607 if partialDeadline.Before(deadline) {
608 var cancel context.CancelFunc
609 dialCtx, cancel = context.WithDeadline(ctx, partialDeadline)
614 c, err := sd.dialSingle(dialCtx, ra)
624 firstErr = &OpError{Op: "dial", Net: sd.network, Source: nil, Addr: nil, Err: errMissingAddress}
629 // dialSingle attempts to establish and returns a single connection to
630 // the destination address.
631 func (sd *sysDialer) dialSingle(ctx context.Context, ra Addr) (c Conn, err error) {
632 trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
635 if trace.ConnectStart != nil {
636 trace.ConnectStart(sd.network, raStr)
638 if trace.ConnectDone != nil {
639 defer func() { trace.ConnectDone(sd.network, raStr, err) }()
643 switch ra := ra.(type) {
645 la, _ := la.(*TCPAddr)
646 if sd.MultipathTCP() {
647 c, err = sd.dialMPTCP(ctx, la, ra)
649 c, err = sd.dialTCP(ctx, la, ra)
652 la, _ := la.(*UDPAddr)
653 c, err = sd.dialUDP(ctx, la, ra)
655 la, _ := la.(*IPAddr)
656 c, err = sd.dialIP(ctx, la, ra)
658 la, _ := la.(*UnixAddr)
659 c, err = sd.dialUnix(ctx, la, ra)
661 return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: sd.address}}
664 return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer
669 // ListenConfig contains options for listening to an address.
670 type ListenConfig struct {
671 // If Control is not nil, it is called after creating the network
672 // connection but before binding it to the operating system.
674 // Network and address parameters passed to Control method are not
675 // necessarily the ones passed to Listen. For example, passing "tcp" to
676 // Listen will cause the Control function to be called with "tcp4" or "tcp6".
677 Control func(network, address string, c syscall.RawConn) error
679 // KeepAlive specifies the keep-alive period for network
680 // connections accepted by this listener.
681 // If zero, keep-alives are enabled if supported by the protocol
682 // and operating system. Network protocols or operating systems
683 // that do not support keep-alives ignore this field.
684 // If negative, keep-alives are disabled.
685 KeepAlive time.Duration
687 // If mptcpStatus is set to a value allowing Multipath TCP (MPTCP) to be
688 // used, any call to Listen with "tcp(4|6)" as network will use MPTCP if
689 // supported by the operating system.
690 mptcpStatus mptcpStatus
693 // MultipathTCP reports whether MPTCP will be used.
695 // This method doesn't check if MPTCP is supported by the operating
697 func (lc *ListenConfig) MultipathTCP() bool {
698 return lc.mptcpStatus.get()
701 // SetMultipathTCP directs the Listen method to use, or not use, MPTCP,
702 // if supported by the operating system. This method overrides the
703 // system default and the GODEBUG=multipathtcp=... setting if any.
705 // If MPTCP is not available on the host or not supported by the client,
706 // the Listen method will fall back to TCP.
707 func (lc *ListenConfig) SetMultipathTCP(use bool) {
708 lc.mptcpStatus.set(use)
711 // Listen announces on the local network address.
713 // See func Listen for a description of the network and address
715 func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) {
716 addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
718 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
726 la := addrs.first(isIPv4)
727 switch la := la.(type) {
729 if sl.MultipathTCP() {
730 l, err = sl.listenMPTCP(ctx, la)
732 l, err = sl.listenTCP(ctx, la)
735 l, err = sl.listenUnix(ctx, la)
737 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
740 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // l is non-nil interface containing nil pointer
745 // ListenPacket announces on the local network address.
747 // See func ListenPacket for a description of the network and address
749 func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
750 addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
752 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
760 la := addrs.first(isIPv4)
761 switch la := la.(type) {
763 c, err = sl.listenUDP(ctx, la)
765 c, err = sl.listenIP(ctx, la)
767 c, err = sl.listenUnixgram(ctx, la)
769 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
772 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // c is non-nil interface containing nil pointer
777 // sysListener contains a Listen's parameters and configuration.
778 type sysListener struct {
780 network, address string
783 // Listen announces on the local network address.
785 // The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
787 // For TCP networks, if the host in the address parameter is empty or
788 // a literal unspecified IP address, Listen listens on all available
789 // unicast and anycast IP addresses of the local system.
790 // To only use IPv4, use network "tcp4".
791 // The address can use a host name, but this is not recommended,
792 // because it will create a listener for at most one of the host's IP
794 // If the port in the address parameter is empty or "0", as in
795 // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
796 // The Addr method of Listener can be used to discover the chosen
799 // See func Dial for a description of the network and address
802 // Listen uses context.Background internally; to specify the context, use
803 // ListenConfig.Listen.
804 func Listen(network, address string) (Listener, error) {
806 return lc.Listen(context.Background(), network, address)
809 // ListenPacket announces on the local network address.
811 // The network must be "udp", "udp4", "udp6", "unixgram", or an IP
812 // transport. The IP transports are "ip", "ip4", or "ip6" followed by
813 // a colon and a literal protocol number or a protocol name, as in
814 // "ip:1" or "ip:icmp".
816 // For UDP and IP networks, if the host in the address parameter is
817 // empty or a literal unspecified IP address, ListenPacket listens on
818 // all available IP addresses of the local system except multicast IP
820 // To only use IPv4, use network "udp4" or "ip4:proto".
821 // The address can use a host name, but this is not recommended,
822 // because it will create a listener for at most one of the host's IP
824 // If the port in the address parameter is empty or "0", as in
825 // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
826 // The LocalAddr method of PacketConn can be used to discover the
829 // See func Dial for a description of the network and address
832 // ListenPacket uses context.Background internally; to specify the context, use
833 // ListenConfig.ListenPacket.
834 func ListenPacket(network, address string) (PacketConn, error) {
836 return lc.ListenPacket(context.Background(), network, address)