From 672033dedc09500ca4d340760d0b80b9c0b198bd Mon Sep 17 00:00:00 2001 From: James Hartig Date: Mon, 13 Feb 2017 15:16:50 -0500 Subject: [PATCH] Added NameList function to ClientConfig (#452) --- clientconfig.go | 32 ++++++++++++++++++++++++++++++++ clientconfig_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/clientconfig.go b/clientconfig.go index cfa9ad0b..0a1f5a92 100644 --- a/clientconfig.go +++ b/clientconfig.go @@ -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 +} diff --git a/clientconfig_test.go b/clientconfig_test.go index 63bc5c81..7755a8a6 100644 --- a/clientconfig_test.go +++ b/clientconfig_test.go @@ -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]) + } +}