Source file src/net/lookup_unix.go

     1  // Copyright 2011 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  //go:build unix || js || wasip1
     6  
     7  package net
     8  
     9  import (
    10  	"context"
    11  	"internal/bytealg"
    12  	"sync"
    13  )
    14  
    15  // readProtocolsOnce loads contents of /etc/protocols into protocols map
    16  // for quick access.
    17  var readProtocolsOnce = sync.OnceFunc(func() {
    18  	file, err := open("/etc/protocols")
    19  	if err != nil {
    20  		return
    21  	}
    22  	defer file.close()
    23  
    24  	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
    25  		// tcp    6   TCP    # transmission control protocol
    26  		if i := bytealg.IndexByteString(line, '#'); i >= 0 {
    27  			line = line[0:i]
    28  		}
    29  		f := getFields(line)
    30  		if len(f) < 2 {
    31  			continue
    32  		}
    33  		if proto, _, ok := dtoi(f[1]); ok {
    34  			if _, ok := protocols[f[0]]; !ok {
    35  				protocols[f[0]] = proto
    36  			}
    37  			for _, alias := range f[2:] {
    38  				if _, ok := protocols[alias]; !ok {
    39  					protocols[alias] = proto
    40  				}
    41  			}
    42  		}
    43  	}
    44  })
    45  
    46  // lookupProtocol looks up IP protocol name in /etc/protocols and
    47  // returns correspondent protocol number.
    48  func lookupProtocol(_ context.Context, name string) (int, error) {
    49  	readProtocolsOnce()
    50  	return lookupProtocolMap(name)
    51  }
    52  
    53  func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
    54  	order, conf := systemConf().hostLookupOrder(r, host)
    55  	if order == hostLookupCgo {
    56  		return cgoLookupHost(ctx, host)
    57  	}
    58  	return r.goLookupHostOrder(ctx, host, order, conf)
    59  }
    60  
    61  func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) {
    62  	order, conf := systemConf().hostLookupOrder(r, host)
    63  	if order == hostLookupCgo {
    64  		return cgoLookupIP(ctx, network, host)
    65  	}
    66  	ips, _, err := r.goLookupIPCNAMEOrder(ctx, network, host, order, conf)
    67  	return ips, err
    68  }
    69  
    70  func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
    71  	// Port lookup is not a DNS operation.
    72  	// Prefer the cgo resolver if possible.
    73  	if !systemConf().mustUseGoResolver(r) {
    74  		port, err := cgoLookupPort(ctx, network, service)
    75  		if err != nil {
    76  			// Issue 18213: if cgo fails, first check to see whether we
    77  			// have the answer baked-in to the net package.
    78  			if port, err := goLookupPort(network, service); err == nil {
    79  				return port, nil
    80  			}
    81  		}
    82  		return port, err
    83  	}
    84  	return goLookupPort(network, service)
    85  }
    86  
    87  func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
    88  	order, conf := systemConf().hostLookupOrder(r, name)
    89  	if order == hostLookupCgo {
    90  		if cname, err, ok := cgoLookupCNAME(ctx, name); ok {
    91  			return cname, err
    92  		}
    93  	}
    94  	return r.goLookupCNAME(ctx, name, order, conf)
    95  }
    96  
    97  func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
    98  	return r.goLookupSRV(ctx, service, proto, name)
    99  }
   100  
   101  func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
   102  	return r.goLookupMX(ctx, name)
   103  }
   104  
   105  func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) {
   106  	return r.goLookupNS(ctx, name)
   107  }
   108  
   109  func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
   110  	return r.goLookupTXT(ctx, name)
   111  }
   112  
   113  func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
   114  	order, conf := systemConf().addrLookupOrder(r, addr)
   115  	if order == hostLookupCgo {
   116  		return cgoLookupPTR(ctx, addr)
   117  	}
   118  	return r.goLookupPTR(ctx, addr, order, conf)
   119  }
   120  

View as plain text