github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/resolvconf/dns/resolvconf.go (about)

     1  package dns
     2  
     3  import (
     4  	"regexp"
     5  )
     6  
     7  // IPLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
     8  const IPLocalhost = `((127\.([0-9]{1,3}\.){2}[0-9]{1,3})|(::1)$)`
     9  
    10  // IPv4Localhost is a regex pattern for IPv4 localhost address range.
    11  const IPv4Localhost = `(127\.([0-9]{1,3}\.){2}[0-9]{1,3})`
    12  
    13  var localhostIPRegexp = regexp.MustCompile(IPLocalhost)
    14  var localhostIPv4Regexp = regexp.MustCompile(IPv4Localhost)
    15  
    16  // IsLocalhost returns true if ip matches the localhost IP regular expression.
    17  // Used for determining if nameserver settings are being passed which are
    18  // localhost addresses
    19  func IsLocalhost(ip string) bool {
    20  	return localhostIPRegexp.MatchString(ip)
    21  }
    22  
    23  // IsIPv4Localhost returns true if ip matches the IPv4 localhost regular expression.
    24  func IsIPv4Localhost(ip string) bool {
    25  	return localhostIPv4Regexp.MatchString(ip)
    26  }