...

Source file src/k8s.io/kubernetes/third_party/forked/golang/net/dnsclient.go

Documentation: k8s.io/kubernetes/third_party/forked/golang/net

     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  // This package is copied from Go library net.
     6  // https://golang.org/src/net/dnsclient.go
     7  // The original private function reverseaddr
     8  // is exported as public function.
     9  
    10  package net
    11  
    12  import "net"
    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 := net.ParseIP(addr)
    19  	if ip == nil {
    20  		return "", &net.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  // Convert unsigned integer to decimal string.
    41  func uitoa(val uint) string {
    42  	if val == 0 { // avoid string allocation
    43  		return "0"
    44  	}
    45  	var buf [20]byte // big enough for 64bit value base 10
    46  	i := len(buf) - 1
    47  	for val >= 10 {
    48  		q := val / 10
    49  		buf[i] = byte('0' + val - q*10)
    50  		i--
    51  		val = q
    52  	}
    53  	// val < 10
    54  	buf[i] = byte('0' + val)
    55  	return string(buf[i:])
    56  }
    57  
    58  const hexDigit = "0123456789abcdef"
    59  

View as plain text