Added NameList function to ClientConfig (#452)

This commit is contained in:
James Hartig 2017-02-13 15:16:50 -05:00 committed by Miek Gieben
parent dadd480c0d
commit 672033dedc
2 changed files with 69 additions and 0 deletions

View File

@ -97,3 +97,35 @@ func ClientConfigFromFile(resolvconf string) (*ClientConfig, error) {
}
return c, nil
}
// NameList returns all of the names that should be queried based on the
// config. It is based off of go's net/dns name building, but it does not
// check the length of the resulting names.
func (c *ClientConfig) NameList(name string) []string {
// if this domain is already fully qualified, no append needed.
if IsFqdn(name) {
return []string{name}
}
// Check to see if the name has more labels than Ndots. Do this before making
// the domain fully qualified.
hasNdots := CountLabel(name) > c.Ndots
// Make the domain fully qualified.
name = Fqdn(name)
// Make a list of names based off search.
names := []string{}
// If name has enough dots, try that first.
if hasNdots {
names = append(names, name)
}
for _, s := range c.Search {
names = append(names, Fqdn(name+s))
}
// If we didn't have enough dots, try after suffixes.
if !hasNdots {
names = append(names, name)
}
return names
}

View File

@ -48,3 +48,40 @@ func testConfig(t *testing.T, data string) {
func TestNameserver(t *testing.T) { testConfig(t, normal) }
func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) }
func TestNameList(t *testing.T) {
cfg := ClientConfig{
Ndots: 1,
}
// fqdn should be only result returned
names := cfg.NameList("miek.nl.")
if len(names) != 1 {
t.Errorf("NameList returned != 1 names: %v", names)
} else if names[0] != "miek.nl." {
t.Errorf("NameList didn't return sent fqdn domain: %v", names[0])
}
cfg.Search = []string{
"test",
}
// Sent domain has NDots and search
names = cfg.NameList("miek.nl")
if len(names) != 2 {
t.Errorf("NameList returned != 2 names: %v", names)
} else if names[0] != "miek.nl." {
t.Errorf("NameList didn't return sent domain first: %v", names[0])
} else if names[1] != "miek.nl.test." {
t.Errorf("NameList didn't return search last: %v", names[1])
}
cfg.Ndots = 2
// Sent domain has less than NDots and search
names = cfg.NameList("miek.nl")
if len(names) != 2 {
t.Errorf("NameList returned != 2 names: %v", names)
} else if names[0] != "miek.nl.test." {
t.Errorf("NameList didn't return search first: %v", names[0])
} else if names[1] != "miek.nl." {
t.Errorf("NameList didn't return sent domain last: %v", names[1])
}
}