From a8769e1df5f2e8b9aeb7f13dbdd26be7db086545 Mon Sep 17 00:00:00 2001 From: Karan Sharma Date: Mon, 15 Feb 2021 10:11:08 +0530 Subject: [PATCH] fix: set ndots correctly fixes https://github.com/mr-karan/doggo/issues/13 --- cmd/doggo/cli.go | 2 +- cmd/doggo/nameservers.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/doggo/cli.go b/cmd/doggo/cli.go index deebc4d..dd47d02 100644 --- a/cmd/doggo/cli.go +++ b/cmd/doggo/cli.go @@ -42,7 +42,7 @@ func main() { // Resolver Options f.Int("timeout", 5, "Sets the timeout for a query to T seconds. The default timeout is 5 seconds.") f.Bool("search", true, "Use the search list provided in resolv.conf. It sets the `ndots` parameter as well unless overridden by `ndots` flag.") - f.Int("ndots", 0, "Specify the ndots parameter. Default value is taken from resolv.conf and fallbacks to 1 if ndots statement is missing in resolv.conf") + f.Int("ndots", -1, "Specify the ndots parameter. Default value is taken from resolv.conf and fallbacks to 1 if ndots statement is missing in resolv.conf") f.BoolP("ipv4", "4", false, "Use IPv4 only") f.BoolP("ipv6", "6", false, "Use IPv6 only") diff --git a/cmd/doggo/nameservers.go b/cmd/doggo/nameservers.go index 72c1bac..9ef279f 100644 --- a/cmd/doggo/nameservers.go +++ b/cmd/doggo/nameservers.go @@ -35,6 +35,8 @@ func (hub *Hub) loadNameservers() error { } } + // Set `ndots` to the user specified value. + hub.ResolverOpts.Ndots = hub.QueryFlags.Ndots // fallback to system nameserver // in case no nameserver is specified by user. if len(hub.Nameservers) == 0 { @@ -42,8 +44,9 @@ func (hub *Hub) loadNameservers() error { if err != nil { return fmt.Errorf("error fetching system default nameserver") } - // override if user hasn't specified any value. - if hub.QueryFlags.Ndots == 0 { + // `-1` indicates the flag is not set. + // use from config if user hasn't specified any value. + if hub.ResolverOpts.Ndots == -1 { hub.ResolverOpts.Ndots = ndots } if len(search) > 0 && hub.QueryFlags.UseSearchList { @@ -51,6 +54,11 @@ func (hub *Hub) loadNameservers() error { } hub.Nameservers = append(hub.Nameservers, ns...) } + // if the user hasn't given any override of `ndots` AND has + // given a custom nameserver. Set `ndots` to 1 as the fallback value + if hub.ResolverOpts.Ndots == -1 { + hub.ResolverOpts.Ndots = 1 + } return nil }