github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/net/lookup_unix.go (about)

     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  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package net
     8  
     9  import (
    10  	"context"
    11  	"sync"
    12  )
    13  
    14  var onceReadProtocols sync.Once
    15  
    16  // readProtocols loads contents of /etc/protocols into protocols map
    17  // for quick access.
    18  func readProtocols() {
    19  	file, err := open("/etc/protocols")
    20  	if err != nil {
    21  		return
    22  	}
    23  	defer file.close()
    24  
    25  	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
    26  		// tcp    6   TCP    # transmission control protocol
    27  		if i := byteIndex(line, '#'); i >= 0 {
    28  			line = line[0:i]
    29  		}
    30  		f := getFields(line)
    31  		if len(f) < 2 {
    32  			continue
    33  		}
    34  		if proto, _, ok := dtoi(f[1]); ok {
    35  			if _, ok := protocols[f[0]]; !ok {
    36  				protocols[f[0]] = proto
    37  			}
    38  			for _, alias := range f[2:] {
    39  				if _, ok := protocols[alias]; !ok {
    40  					protocols[alias] = proto
    41  				}
    42  			}
    43  		}
    44  	}
    45  }
    46  
    47  // lookupProtocol looks up IP protocol name in /etc/protocols and
    48  // returns correspondent protocol number.
    49  func lookupProtocol(_ context.Context, name string) (int, error) {
    50  	onceReadProtocols.Do(readProtocols)
    51  	return lookupProtocolMap(name)
    52  }
    53  
    54  func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
    55  	order := systemConf().hostLookupOrder(host)
    56  	if !r.PreferGo && order == hostLookupCgo {
    57  		if addrs, err, ok := cgoLookupHost(ctx, host); ok {
    58  			return addrs, err
    59  		}
    60  		// cgo not available (or netgo); fall back to Go's DNS resolver
    61  		order = hostLookupFilesDNS
    62  	}
    63  	return r.goLookupHostOrder(ctx, host, order)
    64  }
    65  
    66  func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
    67  	if r.PreferGo {
    68  		return r.goLookupIP(ctx, host)
    69  	}
    70  	order := systemConf().hostLookupOrder(host)
    71  	if order == hostLookupCgo {
    72  		if addrs, err, ok := cgoLookupIP(ctx, host); ok {
    73  			return addrs, err
    74  		}
    75  		// cgo not available (or netgo); fall back to Go's DNS resolver
    76  		order = hostLookupFilesDNS
    77  	}
    78  	addrs, _, err = r.goLookupIPCNAMEOrder(ctx, host, order)
    79  	return
    80  }
    81  
    82  func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
    83  	if !r.PreferGo && systemConf().canUseCgo() {
    84  		if port, err, ok := cgoLookupPort(ctx, network, service); ok {
    85  			if err != nil {
    86  				// Issue 18213: if cgo fails, first check to see whether we
    87  				// have the answer baked-in to the net package.
    88  				if port, err := goLookupPort(network, service); err == nil {
    89  					return port, nil
    90  				}
    91  			}
    92  			return port, err
    93  		}
    94  	}
    95  	return goLookupPort(network, service)
    96  }
    97  
    98  func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
    99  	if !r.PreferGo && systemConf().canUseCgo() {
   100  		if cname, err, ok := cgoLookupCNAME(ctx, name); ok {
   101  			return cname, err
   102  		}
   103  	}
   104  	return r.goLookupCNAME(ctx, name)
   105  }
   106  
   107  func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
   108  	var target string
   109  	if service == "" && proto == "" {
   110  		target = name
   111  	} else {
   112  		target = "_" + service + "._" + proto + "." + name
   113  	}
   114  	cname, rrs, err := r.lookup(ctx, target, dnsTypeSRV)
   115  	if err != nil {
   116  		return "", nil, err
   117  	}
   118  	srvs := make([]*SRV, len(rrs))
   119  	for i, rr := range rrs {
   120  		rr := rr.(*dnsRR_SRV)
   121  		srvs[i] = &SRV{Target: rr.Target, Port: rr.Port, Priority: rr.Priority, Weight: rr.Weight}
   122  	}
   123  	byPriorityWeight(srvs).sort()
   124  	return cname, srvs, nil
   125  }
   126  
   127  func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
   128  	_, rrs, err := r.lookup(ctx, name, dnsTypeMX)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	mxs := make([]*MX, len(rrs))
   133  	for i, rr := range rrs {
   134  		rr := rr.(*dnsRR_MX)
   135  		mxs[i] = &MX{Host: rr.Mx, Pref: rr.Pref}
   136  	}
   137  	byPref(mxs).sort()
   138  	return mxs, nil
   139  }
   140  
   141  func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) {
   142  	_, rrs, err := r.lookup(ctx, name, dnsTypeNS)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	nss := make([]*NS, len(rrs))
   147  	for i, rr := range rrs {
   148  		nss[i] = &NS{Host: rr.(*dnsRR_NS).Ns}
   149  	}
   150  	return nss, nil
   151  }
   152  
   153  func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
   154  	_, rrs, err := r.lookup(ctx, name, dnsTypeTXT)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	txts := make([]string, len(rrs))
   159  	for i, rr := range rrs {
   160  		txts[i] = rr.(*dnsRR_TXT).Txt
   161  	}
   162  	return txts, nil
   163  }
   164  
   165  func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
   166  	if !r.PreferGo && systemConf().canUseCgo() {
   167  		if ptrs, err, ok := cgoLookupPTR(ctx, addr); ok {
   168  			return ptrs, err
   169  		}
   170  	}
   171  	return r.goLookupPTR(ctx, addr)
   172  }