Merge pull request #1613 from thijsterlouw/proper_resolvconf_parsing

Proper resolv.conf parsing
This commit is contained in:
Michael Crosby 2013-08-30 12:10:45 -07:00
commit 84431ec03c
2 changed files with 29 additions and 3 deletions

View file

@ -781,21 +781,37 @@ func GetResolvConf() ([]byte, error) {
// CheckLocalDns looks into the /etc/resolv.conf,
// it returns true if there is a local nameserver or if there is no nameserver.
func CheckLocalDns(resolvConf []byte) bool {
if !bytes.Contains(resolvConf, []byte("nameserver")) {
var parsedResolvConf = StripComments(resolvConf, []byte("#"))
if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
return true
}
for _, ip := range [][]byte{
[]byte("127.0.0.1"),
[]byte("127.0.1.1"),
} {
if bytes.Contains(resolvConf, ip) {
if bytes.Contains(parsedResolvConf, ip) {
return true
}
}
return false
}
// StripComments parses input into lines and strips away comments.
func StripComments(input []byte, commentMarker []byte) []byte {
lines := bytes.Split(input, []byte("\n"))
var output []byte
for _, currentLine := range lines {
var commentIndex = bytes.Index(currentLine, commentMarker)
if ( commentIndex == -1 ) {
output = append(output, currentLine...)
} else {
output = append(output, currentLine[:commentIndex]...)
}
output = append(output, []byte("\n")...)
}
return output
}
func ParseHost(host string, port int, addr string) string {
if strings.HasPrefix(addr, "unix://") {
return addr

View file

@ -323,6 +323,16 @@ func TestCheckLocalDns(t *testing.T) {
nameserver 10.0.2.3
search dotcloud.net`: false,
`# Dynamic
#nameserver 127.0.0.1
nameserver 10.0.2.3
search dotcloud.net`: false,
`# Dynamic
nameserver 10.0.2.3 #not used 127.0.1.1
search dotcloud.net`: false,
`# Dynamic
#nameserver 10.0.2.3
#search dotcloud.net`: true,
`# Dynamic
nameserver 127.0.0.1
search dotcloud.net`: true,
`# Dynamic