github.com/cilium/cilium@v1.16.2/pkg/fqdn/dns/dns.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  // Based on code from github.com/miekg/dns which is:
     5  //
     6  // Copyright 2009 The Go Authors. All rights reserved.
     7  // Copyright 2011 Miek Gieben. All rights reserved.
     8  // Copyright 2014 CloudFlare. All rights reserved.
     9  
    10  package dns
    11  
    12  import "strings"
    13  
    14  // These functions were copied and adapted from github.com/miekg/dns.
    15  
    16  // isFQDN reports whether the domain name s is fully qualified.
    17  func isFQDN(s string) bool {
    18  	s2 := strings.TrimSuffix(s, ".")
    19  	if s == s2 {
    20  		return false
    21  	}
    22  
    23  	i := strings.LastIndexFunc(s2, func(r rune) bool {
    24  		return r != '\\'
    25  	})
    26  
    27  	// Test whether we have an even number of escape sequences before
    28  	// the dot or none.
    29  	return (len(s2)-i)%2 != 0
    30  }
    31  
    32  // FQDN returns the fully qualified domain name from s.
    33  // If s is already fully qualified, it behaves as the identity function.
    34  func FQDN(s string) string {
    35  	if isFQDN(s) {
    36  		return strings.ToLower(s)
    37  	}
    38  	return strings.ToLower(s) + "."
    39  }