Source file src/net/dial.go

     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.
     4  
     5  package net
     6  
     7  import (
     8  	"context"
     9  	"internal/nettrace"
    10  	"syscall"
    11  	"time"
    12  )
    13  
    14  // defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
    15  // See golang.org/issue/31510
    16  const (
    17  	defaultTCPKeepAlive = 15 * time.Second
    18  )
    19  
    20  // A Dialer contains options for connecting to an address.
    21  //
    22  // The zero value for each field is equivalent to dialing
    23  // without that option. Dialing with the zero value of Dialer
    24  // is therefore equivalent to just calling the Dial function.
    25  //
    26  // It is safe to call Dialer's methods concurrently.
    27  type Dialer struct {
    28  	// Timeout is the maximum amount of time a dial will wait for
    29  	// a connect to complete. If Deadline is also set, it may fail
    30  	// earlier.
    31  	//
    32  	// The default is no timeout.
    33  	//
    34  	// When using TCP and dialing a host name with multiple IP
    35  	// addresses, the timeout may be divided between them.
    36  	//
    37  	// With or without a timeout, the operating system may impose
    38  	// its own earlier timeout. For instance, TCP timeouts are
    39  	// often around 3 minutes.
    40  	Timeout time.Duration
    41  
    42  	// Deadline is the absolute point in time after which dials
    43  	// will fail. If Timeout is set, it may fail earlier.
    44  	// Zero means no deadline, or dependent on the operating system
    45  	// as with the Timeout option.
    46  	Deadline time.Time
    47  
    48  	// LocalAddr is the local address to use when dialing an
    49  	// address. The address must be of a compatible type for the
    50  	// network being dialed.
    51  	// If nil, a local address is automatically chosen.
    52  	LocalAddr Addr
    53  
    54  	// DualStack previously enabled RFC 6555 Fast Fallback
    55  	// support, also known as "Happy Eyeballs", in which IPv4 is
    56  	// tried soon if IPv6 appears to be misconfigured and
    57  	// hanging.
    58  	//
    59  	// Deprecated: Fast Fallback is enabled by default. To
    60  	// disable, set FallbackDelay to a negative value.
    61  	DualStack bool
    62  
    63  	// FallbackDelay specifies the length of time to wait before
    64  	// spawning a RFC 6555 Fast Fallback connection. That is, this
    65  	// is the amount of time to wait for IPv6 to succeed before
    66  	// assuming that IPv6 is misconfigured and falling back to
    67  	// IPv4.
    68  	//
    69  	// If zero, a default delay of 300ms is used.
    70  	// A negative value disables Fast Fallback support.
    71  	FallbackDelay time.Duration
    72  
    73  	// KeepAlive specifies the interval between keep-alive
    74  	// probes for an active network connection.
    75  	// If zero, keep-alive probes are sent with a default value
    76  	// (currently 15 seconds), if supported by the protocol and operating
    77  	// system. Network protocols or operating systems that do
    78  	// not support keep-alives ignore this field.
    79  	// If negative, keep-alive probes are disabled.
    80  	KeepAlive time.Duration
    81  
    82  	// Resolver optionally specifies an alternate resolver to use.
    83  	Resolver *Resolver
    84  
    85  	// Cancel is an optional channel whose closure indicates that
    86  	// the dial should be canceled. Not all types of dials support
    87  	// cancellation.
    88  	//
    89  	// Deprecated: Use DialContext instead.
    90  	Cancel <-chan struct{}
    91  
    92  	// If Control is not nil, it is called after creating the network
    93  	// connection but before actually dialing.
    94  	//
    95  	// Network and address parameters passed to Control method are not
    96  	// necessarily the ones passed to Dial. For example, passing "tcp" to Dial
    97  	// will cause the Control function to be called with "tcp4" or "tcp6".
    98  	//
    99  	// Control is ignored if ControlContext is not nil.
   100  	Control func(network, address string, c syscall.RawConn) error
   101  
   102  	// If ControlContext is not nil, it is called after creating the network
   103  	// connection but before actually dialing.
   104  	//
   105  	// Network and address parameters passed to Control method are not
   106  	// necessarily the ones passed to Dial. For example, passing "tcp" to Dial
   107  	// will cause the Control function to be called with "tcp4" or "tcp6".
   108  	//
   109  	// If ControlContext is not nil, Control is ignored.
   110  	ControlContext func(ctx context.Context, network, address string, c syscall.RawConn) error
   111  }
   112  
   113  func (d *Dialer) dualStack() bool { return d.FallbackDelay >= 0 }
   114  
   115  func minNonzeroTime(a, b time.Time) time.Time {
   116  	if a.IsZero() {
   117  		return b
   118  	}
   119  	if b.IsZero() || a.Before(b) {
   120  		return a
   121  	}
   122  	return b
   123  }
   124  
   125  // deadline returns the earliest of:
   126  //   - now+Timeout
   127  //   - d.Deadline
   128  //   - the context's deadline
   129  //
   130  // Or zero, if none of Timeout, Deadline, or context's deadline is set.
   131  func (d *Dialer) deadline(ctx context.Context, now time.Time) (earliest time.Time) {
   132  	if d.Timeout != 0 { // including negative, for historical reasons
   133  		earliest = now.Add(d.Timeout)
   134  	}
   135  	if d, ok := ctx.Deadline(); ok {
   136  		earliest = minNonzeroTime(earliest, d)
   137  	}
   138  	return minNonzeroTime(earliest, d.Deadline)
   139  }
   140  
   141  func (d *Dialer) resolver() *Resolver {
   142  	if d.Resolver != nil {
   143  		return d.Resolver
   144  	}
   145  	return DefaultResolver
   146  }
   147  
   148  // partialDeadline returns the deadline to use for a single address,
   149  // when multiple addresses are pending.
   150  func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) {
   151  	if deadline.IsZero() {
   152  		return deadline, nil
   153  	}
   154  	timeRemaining := deadline.Sub(now)
   155  	if timeRemaining <= 0 {
   156  		return time.Time{}, errTimeout
   157  	}
   158  	// Tentatively allocate equal time to each remaining address.
   159  	timeout := timeRemaining / time.Duration(addrsRemaining)
   160  	// If the time per address is too short, steal from the end of the list.
   161  	const saneMinimum = 2 * time.Second
   162  	if timeout < saneMinimum {
   163  		if timeRemaining < saneMinimum {
   164  			timeout = timeRemaining
   165  		} else {
   166  			timeout = saneMinimum
   167  		}
   168  	}
   169  	return now.Add(timeout), nil
   170  }
   171  
   172  func (d *Dialer) fallbackDelay() time.Duration {
   173  	if d.FallbackDelay > 0 {
   174  		return d.FallbackDelay
   175  	} else {
   176  		return 300 * time.Millisecond
   177  	}
   178  }
   179  
   180  func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
   181  	i := last(network, ':')
   182  	if i < 0 { // no colon
   183  		switch network {
   184  		case "tcp", "tcp4", "tcp6":
   185  		case "udp", "udp4", "udp6":
   186  		case "ip", "ip4", "ip6":
   187  			if needsProto {
   188  				return "", 0, UnknownNetworkError(network)
   189  			}
   190  		case "unix", "unixgram", "unixpacket":
   191  		default:
   192  			return "", 0, UnknownNetworkError(network)
   193  		}
   194  		return network, 0, nil
   195  	}
   196  	afnet = network[:i]
   197  	switch afnet {
   198  	case "ip", "ip4", "ip6":
   199  		protostr := network[i+1:]
   200  		proto, i, ok := dtoi(protostr)
   201  		if !ok || i != len(protostr) {
   202  			proto, err = lookupProtocol(ctx, protostr)
   203  			if err != nil {
   204  				return "", 0, err
   205  			}
   206  		}
   207  		return afnet, proto, nil
   208  	}
   209  	return "", 0, UnknownNetworkError(network)
   210  }
   211  
   212  // resolveAddrList resolves addr using hint and returns a list of
   213  // addresses. The result contains at least one address when error is
   214  // nil.
   215  func (r *Resolver) resolveAddrList(ctx context.Context, op, network, addr string, hint Addr) (addrList, error) {
   216  	afnet, _, err := parseNetwork(ctx, network, true)
   217  	if err != nil {
   218  		return nil, err
   219  	}
   220  	if op == "dial" && addr == "" {
   221  		return nil, errMissingAddress
   222  	}
   223  	switch afnet {
   224  	case "unix", "unixgram", "unixpacket":
   225  		addr, err := ResolveUnixAddr(afnet, addr)
   226  		if err != nil {
   227  			return nil, err
   228  		}
   229  		if op == "dial" && hint != nil && addr.Network() != hint.Network() {
   230  			return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
   231  		}
   232  		return addrList{addr}, nil
   233  	}
   234  	addrs, err := r.internetAddrList(ctx, afnet, addr)
   235  	if err != nil || op != "dial" || hint == nil {
   236  		return addrs, err
   237  	}
   238  	var (
   239  		tcp      *TCPAddr
   240  		udp      *UDPAddr
   241  		ip       *IPAddr
   242  		wildcard bool
   243  	)
   244  	switch hint := hint.(type) {
   245  	case *TCPAddr:
   246  		tcp = hint
   247  		wildcard = tcp.isWildcard()
   248  	case *UDPAddr:
   249  		udp = hint
   250  		wildcard = udp.isWildcard()
   251  	case *IPAddr:
   252  		ip = hint
   253  		wildcard = ip.isWildcard()
   254  	}
   255  	naddrs := addrs[:0]
   256  	for _, addr := range addrs {
   257  		if addr.Network() != hint.Network() {
   258  			return nil, &AddrError{Err: "mismatched local address type", Addr: hint.String()}
   259  		}
   260  		switch addr := addr.(type) {
   261  		case *TCPAddr:
   262  			if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(tcp.IP) {
   263  				continue
   264  			}
   265  			naddrs = append(naddrs, addr)
   266  		case *UDPAddr:
   267  			if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(udp.IP) {
   268  				continue
   269  			}
   270  			naddrs = append(naddrs, addr)
   271  		case *IPAddr:
   272  			if !wildcard && !addr.isWildcard() && !addr.IP.matchAddrFamily(ip.IP) {
   273  				continue
   274  			}
   275  			naddrs = append(naddrs, addr)
   276  		}
   277  	}
   278  	if len(naddrs) == 0 {
   279  		return nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: hint.String()}
   280  	}
   281  	return naddrs, nil
   282  }
   283  
   284  // Dial connects to the address on the named network.
   285  //
   286  // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
   287  // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
   288  // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
   289  // "unixpacket".
   290  //
   291  // For TCP and UDP networks, the address has the form "host:port".
   292  // The host must be a literal IP address, or a host name that can be
   293  // resolved to IP addresses.
   294  // The port must be a literal port number or a service name.
   295  // If the host is a literal IPv6 address it must be enclosed in square
   296  // brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80".
   297  // The zone specifies the scope of the literal IPv6 address as defined
   298  // in RFC 4007.
   299  // The functions JoinHostPort and SplitHostPort manipulate a pair of
   300  // host and port in this form.
   301  // When using TCP, and the host resolves to multiple IP addresses,
   302  // Dial will try each IP address in order until one succeeds.
   303  //
   304  // Examples:
   305  //
   306  //	Dial("tcp", "golang.org:http")
   307  //	Dial("tcp", "192.0.2.1:http")
   308  //	Dial("tcp", "198.51.100.1:80")
   309  //	Dial("udp", "[2001:db8::1]:domain")
   310  //	Dial("udp", "[fe80::1%lo0]:53")
   311  //	Dial("tcp", ":80")
   312  //
   313  // For IP networks, the network must be "ip", "ip4" or "ip6" followed
   314  // by a colon and a literal protocol number or a protocol name, and
   315  // the address has the form "host". The host must be a literal IP
   316  // address or a literal IPv6 address with zone.
   317  // It depends on each operating system how the operating system
   318  // behaves with a non-well known protocol number such as "0" or "255".
   319  //
   320  // Examples:
   321  //
   322  //	Dial("ip4:1", "192.0.2.1")
   323  //	Dial("ip6:ipv6-icmp", "2001:db8::1")
   324  //	Dial("ip6:58", "fe80::1%lo0")
   325  //
   326  // For TCP, UDP and IP networks, if the host is empty or a literal
   327  // unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for
   328  // TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is
   329  // assumed.
   330  //
   331  // For Unix networks, the address must be a file system path.
   332  func Dial(network, address string) (Conn, error) {
   333  	var d Dialer
   334  	return d.Dial(network, address)
   335  }
   336  
   337  // DialTimeout acts like Dial but takes a timeout.
   338  //
   339  // The timeout includes name resolution, if required.
   340  // When using TCP, and the host in the address parameter resolves to
   341  // multiple IP addresses, the timeout is spread over each consecutive
   342  // dial, such that each is given an appropriate fraction of the time
   343  // to connect.
   344  //
   345  // See func Dial for a description of the network and address
   346  // parameters.
   347  func DialTimeout(network, address string, timeout time.Duration) (Conn, error) {
   348  	d := Dialer{Timeout: timeout}
   349  	return d.Dial(network, address)
   350  }
   351  
   352  // sysDialer contains a Dial's parameters and configuration.
   353  type sysDialer struct {
   354  	Dialer
   355  	network, address string
   356  	testHookDialTCP  func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)
   357  }
   358  
   359  // Dial connects to the address on the named network.
   360  //
   361  // See func Dial for a description of the network and address
   362  // parameters.
   363  //
   364  // Dial uses context.Background internally; to specify the context, use
   365  // DialContext.
   366  func (d *Dialer) Dial(network, address string) (Conn, error) {
   367  	return d.DialContext(context.Background(), network, address)
   368  }
   369  
   370  // DialContext connects to the address on the named network using
   371  // the provided context.
   372  //
   373  // The provided Context must be non-nil. If the context expires before
   374  // the connection is complete, an error is returned. Once successfully
   375  // connected, any expiration of the context will not affect the
   376  // connection.
   377  //
   378  // When using TCP, and the host in the address parameter resolves to multiple
   379  // network addresses, any dial timeout (from d.Timeout or ctx) is spread
   380  // over each consecutive dial, such that each is given an appropriate
   381  // fraction of the time to connect.
   382  // For example, if a host has 4 IP addresses and the timeout is 1 minute,
   383  // the connect to each single address will be given 15 seconds to complete
   384  // before trying the next one.
   385  //
   386  // See func Dial for a description of the network and address
   387  // parameters.
   388  func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error) {
   389  	if ctx == nil {
   390  		panic("nil context")
   391  	}
   392  	deadline := d.deadline(ctx, time.Now())
   393  	if !deadline.IsZero() {
   394  		if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
   395  			subCtx, cancel := context.WithDeadline(ctx, deadline)
   396  			defer cancel()
   397  			ctx = subCtx
   398  		}
   399  	}
   400  	if oldCancel := d.Cancel; oldCancel != nil {
   401  		subCtx, cancel := context.WithCancel(ctx)
   402  		defer cancel()
   403  		go func() {
   404  			select {
   405  			case <-oldCancel:
   406  				cancel()
   407  			case <-subCtx.Done():
   408  			}
   409  		}()
   410  		ctx = subCtx
   411  	}
   412  
   413  	// Shadow the nettrace (if any) during resolve so Connect events don't fire for DNS lookups.
   414  	resolveCtx := ctx
   415  	if trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace); trace != nil {
   416  		shadow := *trace
   417  		shadow.ConnectStart = nil
   418  		shadow.ConnectDone = nil
   419  		resolveCtx = context.WithValue(resolveCtx, nettrace.TraceKey{}, &shadow)
   420  	}
   421  
   422  	addrs, err := d.resolver().resolveAddrList(resolveCtx, "dial", network, address, d.LocalAddr)
   423  	if err != nil {
   424  		return nil, &OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: err}
   425  	}
   426  
   427  	sd := &sysDialer{
   428  		Dialer:  *d,
   429  		network: network,
   430  		address: address,
   431  	}
   432  
   433  	var primaries, fallbacks addrList
   434  	if d.dualStack() && network == "tcp" {
   435  		primaries, fallbacks = addrs.partition(isIPv4)
   436  	} else {
   437  		primaries = addrs
   438  	}
   439  
   440  	return sd.dialParallel(ctx, primaries, fallbacks)
   441  }
   442  
   443  // dialParallel races two copies of dialSerial, giving the first a
   444  // head start. It returns the first established connection and
   445  // closes the others. Otherwise it returns an error from the first
   446  // primary address.
   447  func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addrList) (Conn, error) {
   448  	if len(fallbacks) == 0 {
   449  		return sd.dialSerial(ctx, primaries)
   450  	}
   451  
   452  	returned := make(chan struct{})
   453  	defer close(returned)
   454  
   455  	type dialResult struct {
   456  		Conn
   457  		error
   458  		primary bool
   459  		done    bool
   460  	}
   461  	results := make(chan dialResult) // unbuffered
   462  
   463  	startRacer := func(ctx context.Context, primary bool) {
   464  		ras := primaries
   465  		if !primary {
   466  			ras = fallbacks
   467  		}
   468  		c, err := sd.dialSerial(ctx, ras)
   469  		select {
   470  		case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
   471  		case <-returned:
   472  			if c != nil {
   473  				c.Close()
   474  			}
   475  		}
   476  	}
   477  
   478  	var primary, fallback dialResult
   479  
   480  	// Start the main racer.
   481  	primaryCtx, primaryCancel := context.WithCancel(ctx)
   482  	defer primaryCancel()
   483  	go startRacer(primaryCtx, true)
   484  
   485  	// Start the timer for the fallback racer.
   486  	fallbackTimer := time.NewTimer(sd.fallbackDelay())
   487  	defer fallbackTimer.Stop()
   488  
   489  	for {
   490  		select {
   491  		case <-fallbackTimer.C:
   492  			fallbackCtx, fallbackCancel := context.WithCancel(ctx)
   493  			defer fallbackCancel()
   494  			go startRacer(fallbackCtx, false)
   495  
   496  		case res := <-results:
   497  			if res.error == nil {
   498  				return res.Conn, nil
   499  			}
   500  			if res.primary {
   501  				primary = res
   502  			} else {
   503  				fallback = res
   504  			}
   505  			if primary.done && fallback.done {
   506  				return nil, primary.error
   507  			}
   508  			if res.primary && fallbackTimer.Stop() {
   509  				// If we were able to stop the timer, that means it
   510  				// was running (hadn't yet started the fallback), but
   511  				// we just got an error on the primary path, so start
   512  				// the fallback immediately (in 0 nanoseconds).
   513  				fallbackTimer.Reset(0)
   514  			}
   515  		}
   516  	}
   517  }
   518  
   519  // dialSerial connects to a list of addresses in sequence, returning
   520  // either the first successful connection, or the first error.
   521  func (sd *sysDialer) dialSerial(ctx context.Context, ras addrList) (Conn, error) {
   522  	var firstErr error // The error from the first address is most relevant.
   523  
   524  	for i, ra := range ras {
   525  		select {
   526  		case <-ctx.Done():
   527  			return nil, &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: mapErr(ctx.Err())}
   528  		default:
   529  		}
   530  
   531  		dialCtx := ctx
   532  		if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
   533  			partialDeadline, err := partialDeadline(time.Now(), deadline, len(ras)-i)
   534  			if err != nil {
   535  				// Ran out of time.
   536  				if firstErr == nil {
   537  					firstErr = &OpError{Op: "dial", Net: sd.network, Source: sd.LocalAddr, Addr: ra, Err: err}
   538  				}
   539  				break
   540  			}
   541  			if partialDeadline.Before(deadline) {
   542  				var cancel context.CancelFunc
   543  				dialCtx, cancel = context.WithDeadline(ctx, partialDeadline)
   544  				defer cancel()
   545  			}
   546  		}
   547  
   548  		c, err := sd.dialSingle(dialCtx, ra)
   549  		if err == nil {
   550  			return c, nil
   551  		}
   552  		if firstErr == nil {
   553  			firstErr = err
   554  		}
   555  	}
   556  
   557  	if firstErr == nil {
   558  		firstErr = &OpError{Op: "dial", Net: sd.network, Source: nil, Addr: nil, Err: errMissingAddress}
   559  	}
   560  	return nil, firstErr
   561  }
   562  
   563  // dialSingle attempts to establish and returns a single connection to
   564  // the destination address.
   565  func (sd *sysDialer) dialSingle(ctx context.Context, ra Addr) (c Conn, err error) {
   566  	trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
   567  	if trace != nil {
   568  		raStr := ra.String()
   569  		if trace.ConnectStart != nil {
   570  			trace.ConnectStart(sd.network, raStr)
   571  		}
   572  		if trace.ConnectDone != nil {
   573  			defer func() { trace.ConnectDone(sd.network, raStr, err) }()
   574  		}
   575  	}
   576  	la := sd.LocalAddr
   577  	switch ra := ra.(type) {
   578  	case *TCPAddr:
   579  		la, _ := la.(*TCPAddr)
   580  		c, err = sd.dialTCP(ctx, la, ra)
   581  	case *UDPAddr:
   582  		la, _ := la.(*UDPAddr)
   583  		c, err = sd.dialUDP(ctx, la, ra)
   584  	case *IPAddr:
   585  		la, _ := la.(*IPAddr)
   586  		c, err = sd.dialIP(ctx, la, ra)
   587  	case *UnixAddr:
   588  		la, _ := la.(*UnixAddr)
   589  		c, err = sd.dialUnix(ctx, la, ra)
   590  	default:
   591  		return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: sd.address}}
   592  	}
   593  	if err != nil {
   594  		return nil, &OpError{Op: "dial", Net: sd.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer
   595  	}
   596  	return c, nil
   597  }
   598  
   599  // ListenConfig contains options for listening to an address.
   600  type ListenConfig struct {
   601  	// If Control is not nil, it is called after creating the network
   602  	// connection but before binding it to the operating system.
   603  	//
   604  	// Network and address parameters passed to Control method are not
   605  	// necessarily the ones passed to Listen. For example, passing "tcp" to
   606  	// Listen will cause the Control function to be called with "tcp4" or "tcp6".
   607  	Control func(network, address string, c syscall.RawConn) error
   608  
   609  	// KeepAlive specifies the keep-alive period for network
   610  	// connections accepted by this listener.
   611  	// If zero, keep-alives are enabled if supported by the protocol
   612  	// and operating system. Network protocols or operating systems
   613  	// that do not support keep-alives ignore this field.
   614  	// If negative, keep-alives are disabled.
   615  	KeepAlive time.Duration
   616  }
   617  
   618  // Listen announces on the local network address.
   619  //
   620  // See func Listen for a description of the network and address
   621  // parameters.
   622  func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error) {
   623  	addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
   624  	if err != nil {
   625  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
   626  	}
   627  	sl := &sysListener{
   628  		ListenConfig: *lc,
   629  		network:      network,
   630  		address:      address,
   631  	}
   632  	var l Listener
   633  	la := addrs.first(isIPv4)
   634  	switch la := la.(type) {
   635  	case *TCPAddr:
   636  		l, err = sl.listenTCP(ctx, la)
   637  	case *UnixAddr:
   638  		l, err = sl.listenUnix(ctx, la)
   639  	default:
   640  		return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
   641  	}
   642  	if err != nil {
   643  		return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // l is non-nil interface containing nil pointer
   644  	}
   645  	return l, nil
   646  }
   647  
   648  // ListenPacket announces on the local network address.
   649  //
   650  // See func ListenPacket for a description of the network and address
   651  // parameters.
   652  func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error) {
   653  	addrs, err := DefaultResolver.resolveAddrList(ctx, "listen", network, address, nil)
   654  	if err != nil {
   655  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
   656  	}
   657  	sl := &sysListener{
   658  		ListenConfig: *lc,
   659  		network:      network,
   660  		address:      address,
   661  	}
   662  	var c PacketConn
   663  	la := addrs.first(isIPv4)
   664  	switch la := la.(type) {
   665  	case *UDPAddr:
   666  		c, err = sl.listenUDP(ctx, la)
   667  	case *IPAddr:
   668  		c, err = sl.listenIP(ctx, la)
   669  	case *UnixAddr:
   670  		c, err = sl.listenUnixgram(ctx, la)
   671  	default:
   672  		return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
   673  	}
   674  	if err != nil {
   675  		return nil, &OpError{Op: "listen", Net: sl.network, Source: nil, Addr: la, Err: err} // c is non-nil interface containing nil pointer
   676  	}
   677  	return c, nil
   678  }
   679  
   680  // sysListener contains a Listen's parameters and configuration.
   681  type sysListener struct {
   682  	ListenConfig
   683  	network, address string
   684  }
   685  
   686  // Listen announces on the local network address.
   687  //
   688  // The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
   689  //
   690  // For TCP networks, if the host in the address parameter is empty or
   691  // a literal unspecified IP address, Listen listens on all available
   692  // unicast and anycast IP addresses of the local system.
   693  // To only use IPv4, use network "tcp4".
   694  // The address can use a host name, but this is not recommended,
   695  // because it will create a listener for at most one of the host's IP
   696  // addresses.
   697  // If the port in the address parameter is empty or "0", as in
   698  // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
   699  // The Addr method of Listener can be used to discover the chosen
   700  // port.
   701  //
   702  // See func Dial for a description of the network and address
   703  // parameters.
   704  //
   705  // Listen uses context.Background internally; to specify the context, use
   706  // ListenConfig.Listen.
   707  func Listen(network, address string) (Listener, error) {
   708  	var lc ListenConfig
   709  	return lc.Listen(context.Background(), network, address)
   710  }
   711  
   712  // ListenPacket announces on the local network address.
   713  //
   714  // The network must be "udp", "udp4", "udp6", "unixgram", or an IP
   715  // transport. The IP transports are "ip", "ip4", or "ip6" followed by
   716  // a colon and a literal protocol number or a protocol name, as in
   717  // "ip:1" or "ip:icmp".
   718  //
   719  // For UDP and IP networks, if the host in the address parameter is
   720  // empty or a literal unspecified IP address, ListenPacket listens on
   721  // all available IP addresses of the local system except multicast IP
   722  // addresses.
   723  // To only use IPv4, use network "udp4" or "ip4:proto".
   724  // The address can use a host name, but this is not recommended,
   725  // because it will create a listener for at most one of the host's IP
   726  // addresses.
   727  // If the port in the address parameter is empty or "0", as in
   728  // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
   729  // The LocalAddr method of PacketConn can be used to discover the
   730  // chosen port.
   731  //
   732  // See func Dial for a description of the network and address
   733  // parameters.
   734  //
   735  // ListenPacket uses context.Background internally; to specify the context, use
   736  // ListenConfig.ListenPacket.
   737  func ListenPacket(network, address string) (PacketConn, error) {
   738  	var lc ListenConfig
   739  	return lc.ListenPacket(context.Background(), network, address)
   740  }
   741  

View as plain text