...

Source file src/net/dnsclient.go

     1	// Copyright 2009 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		"math/rand"
     9		"sort"
    10	
    11		"golang.org/x/net/dns/dnsmessage"
    12	)
    13	
    14	// reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
    15	// address addr suitable for rDNS (PTR) record lookup or an error if it fails
    16	// to parse the IP address.
    17	func reverseaddr(addr string) (arpa string, err error) {
    18		ip := ParseIP(addr)
    19		if ip == nil {
    20			return "", &DNSError{Err: "unrecognized address", Name: addr}
    21		}
    22		if ip.To4() != nil {
    23			return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
    24		}
    25		// Must be IPv6
    26		buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
    27		// Add it, in reverse, to the buffer
    28		for i := len(ip) - 1; i >= 0; i-- {
    29			v := ip[i]
    30			buf = append(buf, hexDigit[v&0xF],
    31				'.',
    32				hexDigit[v>>4],
    33				'.')
    34		}
    35		// Append "ip6.arpa." and return (buf already has the final .)
    36		buf = append(buf, "ip6.arpa."...)
    37		return string(buf), nil
    38	}
    39	
    40	func equalASCIIName(x, y dnsmessage.Name) bool {
    41		if x.Length != y.Length {
    42			return false
    43		}
    44		for i := 0; i < int(x.Length); i++ {
    45			a := x.Data[i]
    46			b := y.Data[i]
    47			if 'A' <= a && a <= 'Z' {
    48				a += 0x20
    49			}
    50			if 'A' <= b && b <= 'Z' {
    51				b += 0x20
    52			}
    53			if a != b {
    54				return false
    55			}
    56		}
    57		return true
    58	}
    59	
    60	// isDomainName checks if a string is a presentation-format domain name
    61	// (currently restricted to hostname-compatible "preferred name" LDH labels and
    62	// SRV-like "underscore labels"; see golang.org/issue/12421).
    63	func isDomainName(s string) bool {
    64		// See RFC 1035, RFC 3696.
    65		// Presentation format has dots before every label except the first, and the
    66		// terminal empty label is optional here because we assume fully-qualified
    67		// (absolute) input. We must therefore reserve space for the first and last
    68		// labels' length octets in wire format, where they are necessary and the
    69		// maximum total length is 255.
    70		// So our _effective_ maximum is 253, but 254 is not rejected if the last
    71		// character is a dot.
    72		l := len(s)
    73		if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
    74			return false
    75		}
    76	
    77		last := byte('.')
    78		nonNumeric := false // true once we've seen a letter or hyphen
    79		partlen := 0
    80		for i := 0; i < len(s); i++ {
    81			c := s[i]
    82			switch {
    83			default:
    84				return false
    85			case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
    86				nonNumeric = true
    87				partlen++
    88			case '0' <= c && c <= '9':
    89				// fine
    90				partlen++
    91			case c == '-':
    92				// Byte before dash cannot be dot.
    93				if last == '.' {
    94					return false
    95				}
    96				partlen++
    97				nonNumeric = true
    98			case c == '.':
    99				// Byte before dot cannot be dot, dash.
   100				if last == '.' || last == '-' {
   101					return false
   102				}
   103				if partlen > 63 || partlen == 0 {
   104					return false
   105				}
   106				partlen = 0
   107			}
   108			last = c
   109		}
   110		if last == '-' || partlen > 63 {
   111			return false
   112		}
   113	
   114		return nonNumeric
   115	}
   116	
   117	// absDomainName returns an absolute domain name which ends with a
   118	// trailing dot to match pure Go reverse resolver and all other lookup
   119	// routines.
   120	// See golang.org/issue/12189.
   121	// But we don't want to add dots for local names from /etc/hosts.
   122	// It's hard to tell so we settle on the heuristic that names without dots
   123	// (like "localhost" or "myhost") do not get trailing dots, but any other
   124	// names do.
   125	func absDomainName(b []byte) string {
   126		hasDots := false
   127		for _, x := range b {
   128			if x == '.' {
   129				hasDots = true
   130				break
   131			}
   132		}
   133		if hasDots && b[len(b)-1] != '.' {
   134			b = append(b, '.')
   135		}
   136		return string(b)
   137	}
   138	
   139	// An SRV represents a single DNS SRV record.
   140	type SRV struct {
   141		Target   string
   142		Port     uint16
   143		Priority uint16
   144		Weight   uint16
   145	}
   146	
   147	// byPriorityWeight sorts SRV records by ascending priority and weight.
   148	type byPriorityWeight []*SRV
   149	
   150	func (s byPriorityWeight) Len() int { return len(s) }
   151	func (s byPriorityWeight) Less(i, j int) bool {
   152		return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
   153	}
   154	func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
   155	
   156	// shuffleByWeight shuffles SRV records by weight using the algorithm
   157	// described in RFC 2782.
   158	func (addrs byPriorityWeight) shuffleByWeight() {
   159		sum := 0
   160		for _, addr := range addrs {
   161			sum += int(addr.Weight)
   162		}
   163		for sum > 0 && len(addrs) > 1 {
   164			s := 0
   165			n := rand.Intn(sum)
   166			for i := range addrs {
   167				s += int(addrs[i].Weight)
   168				if s > n {
   169					if i > 0 {
   170						addrs[0], addrs[i] = addrs[i], addrs[0]
   171					}
   172					break
   173				}
   174			}
   175			sum -= int(addrs[0].Weight)
   176			addrs = addrs[1:]
   177		}
   178	}
   179	
   180	// sort reorders SRV records as specified in RFC 2782.
   181	func (addrs byPriorityWeight) sort() {
   182		sort.Sort(addrs)
   183		i := 0
   184		for j := 1; j < len(addrs); j++ {
   185			if addrs[i].Priority != addrs[j].Priority {
   186				addrs[i:j].shuffleByWeight()
   187				i = j
   188			}
   189		}
   190		addrs[i:].shuffleByWeight()
   191	}
   192	
   193	// An MX represents a single DNS MX record.
   194	type MX struct {
   195		Host string
   196		Pref uint16
   197	}
   198	
   199	// byPref implements sort.Interface to sort MX records by preference
   200	type byPref []*MX
   201	
   202	func (s byPref) Len() int           { return len(s) }
   203	func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
   204	func (s byPref) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
   205	
   206	// sort reorders MX records as specified in RFC 5321.
   207	func (s byPref) sort() {
   208		for i := range s {
   209			j := rand.Intn(i + 1)
   210			s[i], s[j] = s[j], s[i]
   211		}
   212		sort.Sort(s)
   213	}
   214	
   215	// An NS represents a single DNS NS record.
   216	type NS struct {
   217		Host string
   218	}
   219	

View as plain text