github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/net/lookup.go (about)

     1  // Copyright 2012 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  	"internal/singleflight"
    11  	"sync"
    12  )
    13  
    14  // protocols contains minimal mappings between internet protocol
    15  // names and numbers for platforms that don't have a complete list of
    16  // protocol numbers.
    17  //
    18  // See http://www.iana.org/assignments/protocol-numbers
    19  //
    20  // On Unix, this map is augmented by readProtocols via lookupProtocol.
    21  var protocols = map[string]int{
    22  	"icmp":      1,
    23  	"igmp":      2,
    24  	"tcp":       6,
    25  	"udp":       17,
    26  	"ipv6-icmp": 58,
    27  }
    28  
    29  // services contains minimal mappings between services names and port
    30  // numbers for platforms that don't have a complete list of port numbers
    31  // (some Solaris distros, nacl, etc).
    32  //
    33  // See https://www.iana.org/assignments/service-names-port-numbers
    34  //
    35  // On Unix, this map is augmented by readServices via goLookupPort.
    36  var services = map[string]map[string]int{
    37  	"udp": {
    38  		"domain": 53,
    39  	},
    40  	"tcp": {
    41  		"ftp":    21,
    42  		"ftps":   990,
    43  		"gopher": 70, // ʕ◔ϖ◔ʔ
    44  		"http":   80,
    45  		"https":  443,
    46  		"imap2":  143,
    47  		"imap3":  220,
    48  		"imaps":  993,
    49  		"pop3":   110,
    50  		"pop3s":  995,
    51  		"smtp":   25,
    52  		"ssh":    22,
    53  		"telnet": 23,
    54  	},
    55  }
    56  
    57  // dnsWaitGroup can be used by tests to wait for all DNS goroutines to
    58  // complete. This avoids races on the test hooks.
    59  var dnsWaitGroup sync.WaitGroup
    60  
    61  const maxProtoLength = len("RSVP-E2E-IGNORE") + 10 // with room to grow
    62  
    63  func lookupProtocolMap(name string) (int, error) {
    64  	var lowerProtocol [maxProtoLength]byte
    65  	n := copy(lowerProtocol[:], name)
    66  	lowerASCIIBytes(lowerProtocol[:n])
    67  	proto, found := protocols[string(lowerProtocol[:n])]
    68  	if !found || n != len(name) {
    69  		return 0, &AddrError{Err: "unknown IP protocol specified", Addr: name}
    70  	}
    71  	return proto, nil
    72  }
    73  
    74  // maxPortBufSize is the longest reasonable name of a service
    75  // (non-numeric port).
    76  // Currently the longest known IANA-unregistered name is
    77  // "mobility-header", so we use that length, plus some slop in case
    78  // something longer is added in the future.
    79  const maxPortBufSize = len("mobility-header") + 10
    80  
    81  func lookupPortMap(network, service string) (port int, error error) {
    82  	switch network {
    83  	case "tcp4", "tcp6":
    84  		network = "tcp"
    85  	case "udp4", "udp6":
    86  		network = "udp"
    87  	}
    88  
    89  	if m, ok := services[network]; ok {
    90  		var lowerService [maxPortBufSize]byte
    91  		n := copy(lowerService[:], service)
    92  		lowerASCIIBytes(lowerService[:n])
    93  		if port, ok := m[string(lowerService[:n])]; ok && n == len(service) {
    94  			return port, nil
    95  		}
    96  	}
    97  	return 0, &AddrError{Err: "unknown port", Addr: network + "/" + service}
    98  }
    99  
   100  // DefaultResolver is the resolver used by the package-level Lookup
   101  // functions and by Dialers without a specified Resolver.
   102  var DefaultResolver = &Resolver{}
   103  
   104  // A Resolver looks up names and numbers.
   105  //
   106  // A nil *Resolver is equivalent to a zero Resolver.
   107  type Resolver struct {
   108  	// PreferGo controls whether Go's built-in DNS resolver is preferred
   109  	// on platforms where it's available. It is equivalent to setting
   110  	// GODEBUG=netdns=go, but scoped to just this resolver.
   111  	PreferGo bool
   112  
   113  	// StrictErrors controls the behavior of temporary errors
   114  	// (including timeout, socket errors, and SERVFAIL) when using
   115  	// Go's built-in resolver. For a query composed of multiple
   116  	// sub-queries (such as an A+AAAA address lookup, or walking the
   117  	// DNS search list), this option causes such errors to abort the
   118  	// whole query instead of returning a partial result. This is
   119  	// not enabled by default because it may affect compatibility
   120  	// with resolvers that process AAAA queries incorrectly.
   121  	StrictErrors bool
   122  
   123  	// Dial optionally specifies an alternate dialer for use by
   124  	// Go's built-in DNS resolver to make TCP and UDP connections
   125  	// to DNS services. The host in the address parameter will
   126  	// always be a literal IP address and not a host name, and the
   127  	// port in the address parameter will be a literal port number
   128  	// and not a service name.
   129  	// If the Conn returned is also a PacketConn, sent and received DNS
   130  	// messages must adhere to RFC 1035 section 4.2.1, "UDP usage".
   131  	// Otherwise, DNS messages transmitted over Conn must adhere
   132  	// to RFC 7766 section 5, "Transport Protocol Selection".
   133  	// If nil, the default dialer is used.
   134  	Dial func(ctx context.Context, network, address string) (Conn, error)
   135  
   136  	// TODO(bradfitz): optional interface impl override hook
   137  	// TODO(bradfitz): Timeout time.Duration?
   138  }
   139  
   140  // LookupHost looks up the given host using the local resolver.
   141  // It returns a slice of that host's addresses.
   142  func LookupHost(host string) (addrs []string, err error) {
   143  	return DefaultResolver.LookupHost(context.Background(), host)
   144  }
   145  
   146  // LookupHost looks up the given host using the local resolver.
   147  // It returns a slice of that host's addresses.
   148  func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error) {
   149  	// Make sure that no matter what we do later, host=="" is rejected.
   150  	// ParseIP, for example, does accept empty strings.
   151  	if host == "" {
   152  		return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host}
   153  	}
   154  	if ip := ParseIP(host); ip != nil {
   155  		return []string{host}, nil
   156  	}
   157  	return r.lookupHost(ctx, host)
   158  }
   159  
   160  // LookupIP looks up host using the local resolver.
   161  // It returns a slice of that host's IPv4 and IPv6 addresses.
   162  func LookupIP(host string) ([]IP, error) {
   163  	addrs, err := DefaultResolver.LookupIPAddr(context.Background(), host)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  	ips := make([]IP, len(addrs))
   168  	for i, ia := range addrs {
   169  		ips[i] = ia.IP
   170  	}
   171  	return ips, nil
   172  }
   173  
   174  // LookupIPAddr looks up host using the local resolver.
   175  // It returns a slice of that host's IPv4 and IPv6 addresses.
   176  func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, error) {
   177  	// Make sure that no matter what we do later, host=="" is rejected.
   178  	// ParseIP, for example, does accept empty strings.
   179  	if host == "" {
   180  		return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host}
   181  	}
   182  	if ip := ParseIP(host); ip != nil {
   183  		return []IPAddr{{IP: ip}}, nil
   184  	}
   185  	trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
   186  	if trace != nil && trace.DNSStart != nil {
   187  		trace.DNSStart(host)
   188  	}
   189  	// The underlying resolver func is lookupIP by default but it
   190  	// can be overridden by tests. This is needed by net/http, so it
   191  	// uses a context key instead of unexported variables.
   192  	resolverFunc := r.lookupIP
   193  	if alt, _ := ctx.Value(nettrace.LookupIPAltResolverKey{}).(func(context.Context, string) ([]IPAddr, error)); alt != nil {
   194  		resolverFunc = alt
   195  	}
   196  
   197  	// We don't want a cancelation of ctx to affect the
   198  	// lookupGroup operation. Otherwise if our context gets
   199  	// canceled it might cause an error to be returned to a lookup
   200  	// using a completely different context.
   201  	lookupGroupCtx, lookupGroupCancel := context.WithCancel(context.Background())
   202  
   203  	dnsWaitGroup.Add(1)
   204  	ch, called := lookupGroup.DoChan(host, func() (interface{}, error) {
   205  		defer dnsWaitGroup.Done()
   206  		return testHookLookupIP(lookupGroupCtx, resolverFunc, host)
   207  	})
   208  	if !called {
   209  		dnsWaitGroup.Done()
   210  	}
   211  
   212  	select {
   213  	case <-ctx.Done():
   214  		// Our context was canceled. If we are the only
   215  		// goroutine looking up this key, then drop the key
   216  		// from the lookupGroup and cancel the lookup.
   217  		// If there are other goroutines looking up this key,
   218  		// let the lookup continue uncanceled, and let later
   219  		// lookups with the same key share the result.
   220  		// See issues 8602, 20703, 22724.
   221  		if lookupGroup.ForgetUnshared(host) {
   222  			lookupGroupCancel()
   223  		} else {
   224  			go func() {
   225  				<-ch
   226  				lookupGroupCancel()
   227  			}()
   228  		}
   229  		err := mapErr(ctx.Err())
   230  		if trace != nil && trace.DNSDone != nil {
   231  			trace.DNSDone(nil, false, err)
   232  		}
   233  		return nil, err
   234  	case r := <-ch:
   235  		lookupGroupCancel()
   236  		if trace != nil && trace.DNSDone != nil {
   237  			addrs, _ := r.Val.([]IPAddr)
   238  			trace.DNSDone(ipAddrsEface(addrs), r.Shared, r.Err)
   239  		}
   240  		return lookupIPReturn(r.Val, r.Err, r.Shared)
   241  	}
   242  }
   243  
   244  // lookupGroup merges LookupIPAddr calls together for lookups
   245  // for the same host. The lookupGroup key is is the LookupIPAddr.host
   246  // argument.
   247  // The return values are ([]IPAddr, error).
   248  var lookupGroup singleflight.Group
   249  
   250  // lookupIPReturn turns the return values from singleflight.Do into
   251  // the return values from LookupIP.
   252  func lookupIPReturn(addrsi interface{}, err error, shared bool) ([]IPAddr, error) {
   253  	if err != nil {
   254  		return nil, err
   255  	}
   256  	addrs := addrsi.([]IPAddr)
   257  	if shared {
   258  		clone := make([]IPAddr, len(addrs))
   259  		copy(clone, addrs)
   260  		addrs = clone
   261  	}
   262  	return addrs, nil
   263  }
   264  
   265  // ipAddrsEface returns an empty interface slice of addrs.
   266  func ipAddrsEface(addrs []IPAddr) []interface{} {
   267  	s := make([]interface{}, len(addrs))
   268  	for i, v := range addrs {
   269  		s[i] = v
   270  	}
   271  	return s
   272  }
   273  
   274  // LookupPort looks up the port for the given network and service.
   275  func LookupPort(network, service string) (port int, err error) {
   276  	return DefaultResolver.LookupPort(context.Background(), network, service)
   277  }
   278  
   279  // LookupPort looks up the port for the given network and service.
   280  func (r *Resolver) LookupPort(ctx context.Context, network, service string) (port int, err error) {
   281  	port, needsLookup := parsePort(service)
   282  	if needsLookup {
   283  		port, err = r.lookupPort(ctx, network, service)
   284  		if err != nil {
   285  			return 0, err
   286  		}
   287  	}
   288  	if 0 > port || port > 65535 {
   289  		return 0, &AddrError{Err: "invalid port", Addr: service}
   290  	}
   291  	return port, nil
   292  }
   293  
   294  // LookupCNAME returns the canonical name for the given host.
   295  // Callers that do not care about the canonical name can call
   296  // LookupHost or LookupIP directly; both take care of resolving
   297  // the canonical name as part of the lookup.
   298  //
   299  // A canonical name is the final name after following zero
   300  // or more CNAME records.
   301  // LookupCNAME does not return an error if host does not
   302  // contain DNS "CNAME" records, as long as host resolves to
   303  // address records.
   304  func LookupCNAME(host string) (cname string, err error) {
   305  	return DefaultResolver.lookupCNAME(context.Background(), host)
   306  }
   307  
   308  // LookupCNAME returns the canonical name for the given host.
   309  // Callers that do not care about the canonical name can call
   310  // LookupHost or LookupIP directly; both take care of resolving
   311  // the canonical name as part of the lookup.
   312  //
   313  // A canonical name is the final name after following zero
   314  // or more CNAME records.
   315  // LookupCNAME does not return an error if host does not
   316  // contain DNS "CNAME" records, as long as host resolves to
   317  // address records.
   318  func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string, err error) {
   319  	return r.lookupCNAME(ctx, host)
   320  }
   321  
   322  // LookupSRV tries to resolve an SRV query of the given service,
   323  // protocol, and domain name. The proto is "tcp" or "udp".
   324  // The returned records are sorted by priority and randomized
   325  // by weight within a priority.
   326  //
   327  // LookupSRV constructs the DNS name to look up following RFC 2782.
   328  // That is, it looks up _service._proto.name. To accommodate services
   329  // publishing SRV records under non-standard names, if both service
   330  // and proto are empty strings, LookupSRV looks up name directly.
   331  func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
   332  	return DefaultResolver.lookupSRV(context.Background(), service, proto, name)
   333  }
   334  
   335  // LookupSRV tries to resolve an SRV query of the given service,
   336  // protocol, and domain name. The proto is "tcp" or "udp".
   337  // The returned records are sorted by priority and randomized
   338  // by weight within a priority.
   339  //
   340  // LookupSRV constructs the DNS name to look up following RFC 2782.
   341  // That is, it looks up _service._proto.name. To accommodate services
   342  // publishing SRV records under non-standard names, if both service
   343  // and proto are empty strings, LookupSRV looks up name directly.
   344  func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error) {
   345  	return r.lookupSRV(ctx, service, proto, name)
   346  }
   347  
   348  // LookupMX returns the DNS MX records for the given domain name sorted by preference.
   349  func LookupMX(name string) ([]*MX, error) {
   350  	return DefaultResolver.lookupMX(context.Background(), name)
   351  }
   352  
   353  // LookupMX returns the DNS MX records for the given domain name sorted by preference.
   354  func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error) {
   355  	return r.lookupMX(ctx, name)
   356  }
   357  
   358  // LookupNS returns the DNS NS records for the given domain name.
   359  func LookupNS(name string) ([]*NS, error) {
   360  	return DefaultResolver.lookupNS(context.Background(), name)
   361  }
   362  
   363  // LookupNS returns the DNS NS records for the given domain name.
   364  func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error) {
   365  	return r.lookupNS(ctx, name)
   366  }
   367  
   368  // LookupTXT returns the DNS TXT records for the given domain name.
   369  func LookupTXT(name string) ([]string, error) {
   370  	return DefaultResolver.lookupTXT(context.Background(), name)
   371  }
   372  
   373  // LookupTXT returns the DNS TXT records for the given domain name.
   374  func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error) {
   375  	return r.lookupTXT(ctx, name)
   376  }
   377  
   378  // LookupAddr performs a reverse lookup for the given address, returning a list
   379  // of names mapping to that address.
   380  //
   381  // When using the host C library resolver, at most one result will be
   382  // returned. To bypass the host resolver, use a custom Resolver.
   383  func LookupAddr(addr string) (names []string, err error) {
   384  	return DefaultResolver.lookupAddr(context.Background(), addr)
   385  }
   386  
   387  // LookupAddr performs a reverse lookup for the given address, returning a list
   388  // of names mapping to that address.
   389  func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
   390  	return r.lookupAddr(ctx, addr)
   391  }