github.com/zxy12/golang151_with_comment@v0.0.0-20190507085033-721809559d3c/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 "internal/singleflight" 9 "time" 10 ) 11 12 // protocols contains minimal mappings between internet protocol 13 // names and numbers for platforms that don't have a complete list of 14 // protocol numbers. 15 // 16 // See http://www.iana.org/assignments/protocol-numbers 17 var protocols = map[string]int{ 18 "icmp": 1, "ICMP": 1, 19 "igmp": 2, "IGMP": 2, 20 "tcp": 6, "TCP": 6, 21 "udp": 17, "UDP": 17, 22 "ipv6-icmp": 58, "IPV6-ICMP": 58, "IPv6-ICMP": 58, 23 } 24 25 // LookupHost looks up the given host using the local resolver. 26 // It returns an array of that host's addresses. 27 func LookupHost(host string) (addrs []string, err error) { 28 // Make sure that no matter what we do later, host=="" is rejected. 29 // ParseIP, for example, does accept empty strings. 30 if host == "" { 31 return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host} 32 } 33 if ip := ParseIP(host); ip != nil { 34 return []string{host}, nil 35 } 36 return lookupHost(host) 37 } 38 39 // LookupIP looks up host using the local resolver. 40 // It returns an array of that host's IPv4 and IPv6 addresses. 41 func LookupIP(host string) (ips []IP, err error) { 42 // Make sure that no matter what we do later, host=="" is rejected. 43 // ParseIP, for example, does accept empty strings. 44 if host == "" { 45 return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host} 46 } 47 if ip := ParseIP(host); ip != nil { 48 return []IP{ip}, nil 49 } 50 addrs, err := lookupIPMerge(host) 51 if err != nil { 52 return 53 } 54 ips = make([]IP, len(addrs)) 55 for i, addr := range addrs { 56 ips[i] = addr.IP 57 } 58 return 59 } 60 61 var lookupGroup singleflight.Group 62 63 // lookupIPMerge wraps lookupIP, but makes sure that for any given 64 // host, only one lookup is in-flight at a time. The returned memory 65 // is always owned by the caller. 66 func lookupIPMerge(host string) (addrs []IPAddr, err error) { 67 addrsi, err, shared := lookupGroup.Do(host, func() (interface{}, error) { 68 return testHookLookupIP(lookupIP, host) 69 }) 70 return lookupIPReturn(addrsi, err, shared) 71 } 72 73 // lookupIPReturn turns the return values from singleflight.Do into 74 // the return values from LookupIP. 75 func lookupIPReturn(addrsi interface{}, err error, shared bool) ([]IPAddr, error) { 76 if err != nil { 77 return nil, err 78 } 79 addrs := addrsi.([]IPAddr) 80 if shared { 81 clone := make([]IPAddr, len(addrs)) 82 copy(clone, addrs) 83 addrs = clone 84 } 85 return addrs, nil 86 } 87 88 // lookupIPDeadline looks up a hostname with a deadline. 89 func lookupIPDeadline(host string, deadline time.Time) (addrs []IPAddr, err error) { 90 if deadline.IsZero() { 91 return lookupIPMerge(host) 92 } 93 94 // We could push the deadline down into the name resolution 95 // functions. However, the most commonly used implementation 96 // calls getaddrinfo, which has no timeout. 97 98 timeout := deadline.Sub(time.Now()) 99 if timeout <= 0 { 100 return nil, errTimeout 101 } 102 t := time.NewTimer(timeout) 103 defer t.Stop() 104 105 ch := lookupGroup.DoChan(host, func() (interface{}, error) { 106 return testHookLookupIP(lookupIP, host) 107 }) 108 109 select { 110 case <-t.C: 111 // The DNS lookup timed out for some reason. Force 112 // future requests to start the DNS lookup again 113 // rather than waiting for the current lookup to 114 // complete. See issue 8602. 115 lookupGroup.Forget(host) 116 117 return nil, errTimeout 118 119 case r := <-ch: 120 return lookupIPReturn(r.Val, r.Err, r.Shared) 121 } 122 } 123 124 // LookupPort looks up the port for the given network and service. 125 func LookupPort(network, service string) (port int, err error) { 126 if n, i, ok := dtoi(service, 0); ok && i == len(service) { 127 return n, nil 128 } 129 return lookupPort(network, service) 130 } 131 132 // LookupCNAME returns the canonical DNS host for the given name. 133 // Callers that do not care about the canonical name can call 134 // LookupHost or LookupIP directly; both take care of resolving 135 // the canonical name as part of the lookup. 136 func LookupCNAME(name string) (cname string, err error) { 137 return lookupCNAME(name) 138 } 139 140 // LookupSRV tries to resolve an SRV query of the given service, 141 // protocol, and domain name. The proto is "tcp" or "udp". 142 // The returned records are sorted by priority and randomized 143 // by weight within a priority. 144 // 145 // LookupSRV constructs the DNS name to look up following RFC 2782. 146 // That is, it looks up _service._proto.name. To accommodate services 147 // publishing SRV records under non-standard names, if both service 148 // and proto are empty strings, LookupSRV looks up name directly. 149 func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) { 150 return lookupSRV(service, proto, name) 151 } 152 153 // LookupMX returns the DNS MX records for the given domain name sorted by preference. 154 func LookupMX(name string) (mxs []*MX, err error) { 155 return lookupMX(name) 156 } 157 158 // LookupNS returns the DNS NS records for the given domain name. 159 func LookupNS(name string) (nss []*NS, err error) { 160 return lookupNS(name) 161 } 162 163 // LookupTXT returns the DNS TXT records for the given domain name. 164 func LookupTXT(name string) (txts []string, err error) { 165 return lookupTXT(name) 166 } 167 168 // LookupAddr performs a reverse lookup for the given address, returning a list 169 // of names mapping to that address. 170 func LookupAddr(addr string) (names []string, err error) { 171 return lookupAddr(addr) 172 }