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.
15 // defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
16 // See go.dev/issue/31510
17 defaultTCPKeepAlive = 15 * time.Second
19 // For the moment, MultiPath TCP is not used by default
20 // See go.dev/issue/56539
21 defaultMPTCPEnabled = false
24 // mptcpStatus is a tristate for Multipath TCP, see go.dev/issue/56539
25 type mptcpStatus uint8
28 // The value 0 is the system default, linked to defaultMPTCPEnabled
29 mptcpUseDefault mptcpStatus = iota
34 func (m *mptcpStatus) get() bool {
42 return defaultMPTCPEnabled
45 func (m *mptcpStatus) set(use bool) {
53 // A Dialer contains options for connecting to an address.
55 // The zero value for each field is equivalent to dialing
56 // without that option. Dialing with the zero value of Dialer
57 // is therefore equivalent to just calling the Dial function.
59 // It is safe to call Dialer's methods concurrently.
61 // Timeout is the maximum amount of time a dial will wait for
62 // a connect to complete. If Deadline is also set, it may fail
65 // The default is no timeout.
67 // When using TCP and dialing a host name with multiple IP
68 // addresses, the timeout may be divided between them.
70 // With or without a timeout, the operating system may impose
71 // its own earlier timeout. For instance, TCP timeouts are
72 // often around 3 minutes.
75 // Deadline is the absolute point in time after which dials
76 // will fail. If Timeout is set, it may fail earlier.
77 // Zero means no deadline, or dependent on the operating system
78 // as with the Timeout option.
81 // LocalAddr is the local address to use when dialing an
82 // address. The address must be of a compatible type for the
83 // network being dialed.
84 // If nil, a local address is automatically chosen.
87 // DualStack previously enabled RFC 6555 Fast Fallback
88 // support, also known as "Happy Eyeballs", in which IPv4 is
89 // tried soon if IPv6 appears to be misconfigured and
92 // Deprecated: Fast Fallback is enabled by default. To
93 // disable, set FallbackDelay to a negative value.
96 // FallbackDelay specifies the length of time to wait before
97 // spawning a RFC 6555 Fast Fallback connection. That is, this
98 // is the amount of time to wait for IPv6 to succeed before
99 // assuming that IPv6 is misconfigured and falling back to
102 // If zero, a default delay of 300ms is used.
103 // A negative value disables Fast Fallback support.
104 FallbackDelay time.Duration
106 // KeepAlive specifies the interval between keep-alive
107 // probes for an active network connection.
108 // If zero, keep-alive probes are sent with a default value
109 // (currently 15 seconds), if supported by the protocol and operating
110 // system. Network protocols or operating systems that do
111 // not support keep-alives ignore this field.
112 // If negative, keep-alive probes are disabled.
113 KeepAlive time.Duration
115 // Resolver optionally specifies an alternate resolver to use.
118 // Cancel is an optional channel whose closure indicates that
119 // the dial should be canceled. Not all types of dials support
122 // Deprecated: Use DialContext instead.
123 Cancel <-chan struct{}
125 // If Control is not nil, it is called after creating the network
126 // connection but before actually dialing.
128 // Network and address parameters passed to Control function are not
129 // necessarily the ones passed to Dial. For example, passing "tcp" to Dial
130 // will cause the Control function to be called with "tcp4" or "tcp6".
132 // Control is ignored if ControlContext is not nil.
133 Control func(network, address string, c syscall.RawConn) error
135 // If ControlContext is not nil, it is called after creating the network
136 // connection but before actually dialing.
138 // Network and address parameters passed to ControlContext function are not
139 // necessarily the ones passed to Dial. For example, passing "tcp" to Dial
140 // will cause the ControlContext function to be called with "tcp4" or "tcp6".
142 // If ControlContext is not nil, Control is ignored.
143 ControlContext func(ctx context.Context, network, address string, c syscall.RawConn) error
145 // If mptcpStatus is set to a value allowing Multipath TCP (MPTCP) to be
146 // used, any call to Dial with "tcp(4|6)" as network will use MPTCP if
147 // supported by the operating system.
148 mptcpStatus mptcpStatus
151 func (d *Dialer) dualStack() bool { return d.FallbackDelay >= 0 }
153 func minNonzeroTime(a, b time.Time) time.Time {
157 if b.IsZero() || a.Before(b) {
163 // deadline returns the earliest of:
166 // - the context's deadline
168 // Or zero, if none of Timeout, Deadline, or context's deadline is set.
169 func (d *Dialer) deadline(ctx context.Context, now time.Time) (earliest time.Time) {
170 if d.Timeout != 0 { // including negative, for historical reasons
171 earliest = now.Add(d.Timeout)
173 if d, ok := ctx.Deadline(); ok {
174 earliest = minNonzeroTime(earliest, d)
176 return minNonzeroTime(earliest, d.Deadline)
179 func (d *Dialer) resolver() *Resolver {
180 if d.Resolver != nil {
183 return DefaultResolver
186 // partialDeadline returns the deadline to use for a single address,
187 // when multiple addresses are pending.
188 func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) {
189 if deadline.IsZero() {
192 timeRemaining := deadline.Sub(now)
193 if timeRemaining <= 0 {
194 return time.Time{}, errTimeout
196 // Tentatively allocate equal time to each remaining address.
197 timeout := timeRemaining / time.Duration(addrsRemaining)
198 // If the time per address is too short, steal from the end of the list.
199 const saneMinimum = 2 * time.Second
200 if timeout < saneMinimum {
201 if timeRemaining < saneMinimum {
202 timeout = timeRemaining
204 timeout = saneMinimum
207 return now.Add(timeout), nil
210 func (d *Dialer) fallbackDelay() time.Duration {
211 if d.FallbackDelay > 0 {
212 return d.FallbackDelay
214 return 300 * time.Millisecond
218 func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
219 i := last(network, ':')
220 if i < 0 { // no colon
222 case "tcp", "tcp4", "tcp6":
223 case "udp", "udp4", "udp6":
224 case "ip", "ip4", "ip6":
226 return "", 0, UnknownNetworkError(network)
228 case "unix", "unixgram", "unixpacket":
230 return "", 0, UnknownNetworkError(network)
232 return network, 0, nil
236 case "ip", "ip4", "ip6":
237 protostr := network[i+1:]
238 proto, i, ok := dtoi(protostr)
239 if !ok || i != len(protostr) {
240 proto, err = lookupProtocol(ctx, protostr)
245 return afnet, proto, nil
247 return "", 0, UnknownNetworkError(network)
250 // resolveAddrList resolves addr using hint and returns a list of
251 // addresses. The result contains at least one address when error is
253 func (r *Resolver) resolveAddrList(ctx context.Context, op, network, addr string, hint Addr) (addrList, error) {
254 afnet, _, err := parseNetwork(ctx, network, true)
258 if op == "dial" && addr == "" {
259 return nil, errMissingAddress
262 case "unix", "unixgram", "unixpacket":
263 addr, err := ResolveUnixAddr(afnet, addr)
267 if op == "dial" && hint != nil && addr.Network() != hint.Network() {
268 return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
270 return addrList{addr}, nil
272 addrs, err := r.internetAddrList(ctx, afnet, addr)
273 if err != nil || op != "dial" || hint == nil {
282 switch hint := hint.(type) {
285 wildcard = tcp.isWildcard()
288 wildcard = udp.isWildcard()
291 wildcard = ip.isWildcard()
294 for _, addr := range addrs {
295 if addr.Network() != hint.Network() {
296 return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
298 switch addr := addr.(type) {
300 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(tcp.IP) {
303 naddrs = append(naddrs, addr)
305 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(udp.IP) {
308 naddrs = append(naddrs, addr)
310 if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(ip.IP) {
313 naddrs = append(naddrs, addr)
316 if len(naddrs) == 0 {
317 return nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: hint.String()}
322 // MultipathTCP reports whether MPTCP will be used.
324 // This method doesn't check if MPTCP is supported by the operating
326 func (d *Dialer) MultipathTCP() bool {
327 return d.mptcpStatus.get()
330 // SetMultipathTCP directs the Dial methods to use, or not use, MPTCP,
331 // if supported by the operating system. This method overrides the
334 // If MPTCP is not available on the host or not supported by the server,
335 // the Dial methods will fall back to TCP.
336 func (d *Dialer) SetMultipathTCP(use bool) {
337 d.mptcpStatus.set(use)
340 // Dial connects to the address on the named network.
342 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
343 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
344 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
347 // For TCP and UDP networks, the address has the form "host:port".
348 // The host must be a literal IP address, or a host name that can be
349 // resolved to IP addresses.
350 // The port must be a literal port number or a service name.
351 // If the host is a literal IPv6 address it must be enclosed in square
352 // brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80".
353 // The zone specifies the scope of the literal IPv6 address as defined
355 // The functions JoinHostPort and SplitHostPort manipulate a pair of
356 // host and port in this form.
357 // When using TCP, and the host resolves to multiple IP addresses,
358 // Dial will try each IP address in order until one succeeds.
362 // Dial("tcp", "golang.org:http")
363 // Dial("tcp", "192.0.2.1:http")
364 // Dial("tcp", "198.51.100.1:80")
365 // Dial("udp", "[2001:db8::1]:domain")
366 // Dial("udp", "[fe80::1%lo0]:53")
367 // Dial("tcp", ":80")
369 // For IP networks, the network must be "ip", "ip4" or "ip6" followed
370 // by a colon and a literal protocol number or a protocol name, and
371 // the address has the form "host". The host must be a literal IP
372 // address or a literal IPv6 address with zone.
373 // It depends on each operating system how the operating system
374 // behaves with a non-well known protocol number such as "0" or "255".
378 // Dial("ip4:1", "192.0.2.1")
379 // Dial("ip6:ipv6-icmp", "2001:db8::1")
380 // Dial("ip6:58", "fe80::1%lo0")
382 // For TCP, UDP and IP networks, if the host is empty or a literal
383 // unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for
384 // TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is
387 // For Unix networks, the address must be a file system path.
388 func Dial(network, address string) (Conn, error) {
390 return d.Dial(network, address)
393 // DialTimeout acts like Dial but takes a timeout.
395 // The timeout includes name resolution, if required.
396 // When using TCP, and the host in the address parameter resolves to
397 // multiple IP addresses, the timeout is spread over each consecutive
398 // dial, such that each is given an appropriate fraction of the time
401 // See func Dial for a description of the network and address
403 func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
404 d := Dialer{Timeout: timeout}
405 return d.Dial(network, address)
408 // sysDialer contains a Dial's parameters and configuration.
409 type sysDialer struct {
411 network, address string
412 testHookDialTCP func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)
415 // Dial connects to the address on the named network.
417 // See func Dial for a description of the network and address
420 // Dial uses context.Background internally; to specify the context, use
422 func (d *Dialer) Dial(network, address string) (Conn, error) {
423 return d.DialContext(context.Background(), network, address)
426 // DialContext connects to the address on the named network using
427 // the provided context.
429 // The provided Context must be non-nil. If the context expires before
430 // the connection is complete, an error is returned. Once successfully
431 // connected, any expiration of the context will not affect the
434 // When using TCP, and the host in the address parameter resolves to multiple
435 // network addresses, any dial timeout (from d.Timeout or ctx) is spread
436 // over each consecutive dial, such that each is given an appropriate
437 // fraction of the time to connect.
438 // For example, if a host has 4 IP addresses and the timeout is 1 minute,
439 // the connect to each single address will be given 15 seconds to complete
440 // before trying the next one.
442 // See func Dial for a description of the network and address
444 func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
448 deadline := d.deadline(ctx, time.Now())
449 if !deadline.IsZero() {
450 if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
451 subCtx, cancel := context.WithDeadline(ctx, deadline)
456 if oldCancel := d.Cancel; oldCancel != nil {
457 subCtx, cancel := context.WithCancel(ctx)
463 case <-subCtx.Done():
469 // Shadow the nettrace (if any) during resolve so Connect events don't fire for DNS lookups.
471 if trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace); trace != nil {
473 shadow.ConnectStart = nil
474 shadow.ConnectDone = nil
475 resolveCtx = context.WithValue(resolveCtx, nettrace.TraceKey{}, &shadow)
478 addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr)
480 return nil, &OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: err}
489 var primaries, fallbacks addrList
490 if d.dualStack() && network == "tcp" {
491 primaries, fallbacks = addrs.partition(isIPv4)
496 return sd.dialParallel(ctx, primaries, fallbacks)
499 // dialParallel races two copies of dialSerial, giving the first a
500 // head start. It returns the first established connection and
501 // closes the others. Otherwise it returns an error from the first
503 func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addrList) (Conn, error) {
504 if len(fallbacks) == 0 {
505 return sd.dialSerial(ctx, primaries)
508 returned := make(chan struct{})
509 defer close(returned)
511 type dialResult struct {
517 results := make(chan dialResult) // unbuffered
519 startRacer := func(ctx context.Context, primary bool) {
524 c, err := sd.dialSerial(ctx, ras)
526 case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
534 var primary, fallback dialResult
536 // Start the main racer.
537 primaryCtx, primaryCancel := context.WithCancel(ctx)
538 defer primaryCancel()
539 go startRacer(primaryCtx, true)
541 // Start the timer for the fallback racer.
542 fallbackTimer := time.NewTimer(sd.fallbackDelay())
543 defer fallbackTimer.Stop()
547 case <-fallbackTimer.C:
548 fallbackCtx, fallbackCancel := context.WithCancel(ctx)
549 defer fallbackCancel()
550 go startRacer(fallbackCtx, false)
552 case res := <-results:
553 if res.error == nil {
561 if primary.done && fallback.done {
562 return nil, primary.error
564 if res.primary && fallbackTimer.Stop() {
565 // If we were able to stop the timer, that means it
566 // was running (hadn't yet started the fallback), but
567 // we just got an error on the primary path, so start
568 // the fallback immediately (in 0 nanoseconds).
569 fallbackTimer.Reset(0)
575 // dialSerial connects to a list of addresses in sequence, returning
576 // either the first successful connection, or the first error.
577 func (sd *sysDialer) dialSerial(ctx context.Context, ras addrList) (Conn, error) {
578 var firstErr error // The error from the first address is most relevant.
580 for i, ra := range ras {
583 return nil, &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: mapErr(ctx.Err())}
588 if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
589 partialDeadline, err := partialDeadline(time.Now(), deadline, len(ras)-i)
593 firstErr = &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: err}
597 if partialDeadline.Before(deadline) {
598 var cancel context.CancelFunc
599 dialCtx, cancel = context.WithDeadline(ctx, partialDeadline)
604 c, err := sd.dialSingle(dialCtx, ra)
614 firstErr = &OpError{Op: "dial", Net: sd.network, Source: nil, Addr: nil, Err: errMissingAddress}
619 // dialSingle attempts to establish and returns a single connection to
620 // the destination address.
621 func (sd *sysDialer) dialSingle(ctx context.Context, ra Addr) (c Conn, err error) {
622 trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
625 if trace.ConnectStart != nil {
626 trace.ConnectStart(sd.network, raStr)
628 if trace.ConnectDone != nil {
629 defer func() { trace.ConnectDone(sd.network, raStr, err) }()
633 switch ra := ra.(type) {
635 la, _ := la.(*TCPAddr)
636 if sd.MultipathTCP() {
637 c, err = sd.dialMPTCP(ctx, la, ra)
639 c, err = sd.dialTCP(ctx, la, ra)
642 la, _ := la.(*UDPAddr)
643 c, err = sd.dialUDP(ctx, la, ra)
645 la, _ := la.(*IPAddr)
646 c, err = sd.dialIP(ctx, la, ra)
648 la, _ := la.(*UnixAddr)
649 c, err = sd.dialUnix(ctx, la, ra)
651 return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: sd.address}}
654 return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer
659 // ListenConfig contains options for listening to an address.
660 type ListenConfig struct {
661 // If Control is not nil, it is called after creating the network
662 // connection but before binding it to the operating system.
664 // Network and address parameters passed to Control method are not
665 // necessarily the ones passed to Listen. For example, passing "tcp" to
666 // Listen will cause the Control function to be called with "tcp4" or "tcp6".
667 Control func(network, address string, c syscall.RawConn) error
669 // KeepAlive specifies the keep-alive period for network
670 // connections accepted by this listener.
671 // If zero, keep-alives are enabled if supported by the protocol
672 // and operating system. Network protocols or operating systems
673 // that do not support keep-alives ignore this field.
674 // If negative, keep-alives are disabled.
675 KeepAlive time.Duration
677 // If mptcpStatus is set to a value allowing Multipath TCP (MPTCP) to be
678 // used, any call to Listen with "tcp(4|6)" as network will use MPTCP if
679 // supported by the operating system.
680 mptcpStatus mptcpStatus
683 // MultipathTCP reports whether MPTCP will be used.
685 // This method doesn't check if MPTCP is supported by the operating
687 func (lc *ListenConfig) MultipathTCP() bool {
688 return lc.mptcpStatus.get()
691 // SetMultipathTCP directs the Listen method to use, or not use, MPTCP,
692 // if supported by the operating system. This method overrides the
695 // If MPTCP is not available on the host or not supported by the client,
696 // the Listen method will fall back to TCP.
697 func (lc *ListenConfig) SetMultipathTCP(use bool) {
698 lc.mptcpStatus.set(use)
701 // Listen announces on the local network address.
703 // See func Listen for a description of the network and address
705 func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) {
706 addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
708 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
716 la := addrs.first(isIPv4)
717 switch la := la.(type) {
719 if sl.MultipathTCP() {
720 l, err = sl.listenMPTCP(ctx, la)
722 l, err = sl.listenTCP(ctx, la)
725 l, err = sl.listenUnix(ctx, la)
727 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
730 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // l is non-nil interface containing nil pointer
735 // ListenPacket announces on the local network address.
737 // See func ListenPacket for a description of the network and address
739 func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
740 addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
742 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
750 la := addrs.first(isIPv4)
751 switch la := la.(type) {
753 c, err = sl.listenUDP(ctx, la)
755 c, err = sl.listenIP(ctx, la)
757 c, err = sl.listenUnixgram(ctx, la)
759 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
762 return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // c is non-nil interface containing nil pointer
767 // sysListener contains a Listen's parameters and configuration.
768 type sysListener struct {
770 network, address string
773 // Listen announces on the local network address.
775 // The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
777 // For TCP networks, if the host in the address parameter is empty or
778 // a literal unspecified IP address, Listen listens on all available
779 // unicast and anycast IP addresses of the local system.
780 // To only use IPv4, use network "tcp4".
781 // The address can use a host name, but this is not recommended,
782 // because it will create a listener for at most one of the host's IP
784 // If the port in the address parameter is empty or "0", as in
785 // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
786 // The Addr method of Listener can be used to discover the chosen
789 // See func Dial for a description of the network and address
792 // Listen uses context.Background internally; to specify the context, use
793 // ListenConfig.Listen.
794 func Listen(network, address string) (Listener, error) {
796 return lc.Listen(context.Background(), network, address)
799 // ListenPacket announces on the local network address.
801 // The network must be "udp", "udp4", "udp6", "unixgram", or an IP
802 // transport. The IP transports are "ip", "ip4", or "ip6" followed by
803 // a colon and a literal protocol number or a protocol name, as in
804 // "ip:1" or "ip:icmp".
806 // For UDP and IP networks, if the host in the address parameter is
807 // empty or a literal unspecified IP address, ListenPacket listens on
808 // all available IP addresses of the local system except multicast IP
810 // To only use IPv4, use network "udp4" or "ip4:proto".
811 // The address can use a host name, but this is not recommended,
812 // because it will create a listener for at most one of the host's IP
814 // If the port in the address parameter is empty or "0", as in
815 // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
816 // The LocalAddr method of PacketConn can be used to discover the
819 // See func Dial for a description of the network and address
822 // ListenPacket uses context.Background internally; to specify the context, use
823 // ListenConfig.ListenPacket.
824 func ListenPacket(network, address string) (PacketConn, error) {
826 return lc.ListenPacket(context.Background(), network, address)