@@ -42,6 +42,33 @@ void Context::capacity(size_t value)
4242 _capacity = std::min ((size_t )IdGenerator::capacity (), std::max (size_t (1 ), value));
4343}
4444
45+ /* *
46+ * Should the search path be respected?
47+ * @param domain the domain to lookup
48+ * @param handler handler that is already in use
49+ * @return bool
50+ */
51+ bool Context::searchable (const char *domain, DNS::Handler *handler) const
52+ {
53+ // length of the lookup
54+ size_t length = strlen (domain);
55+
56+ // empty domains fail anyway
57+ if (length == 0 ) return false ;
58+
59+ // canonical domains don't go into recursion
60+ if (domain[length-1 ] == ' .' ) return false ;
61+
62+ // count the dots
63+ size_t ndots = std::count (domain, domain + length + 1 , ' .' );
64+
65+ // compare with the 'ndots' setting
66+ if (ndots >= _ndots) return false ;
67+
68+ // do not do recursion (if the current handler already is a SearchLookup)
69+ return dynamic_cast <SearchLookup*>(handler) == nullptr ;
70+ }
71+
4572/* *
4673 * Do a dns lookup
4774 * @param domain the record name to look for
@@ -52,13 +79,9 @@ void Context::capacity(size_t value)
5279 */
5380Operation *Context::query (const char *domain, ns_type type, const Bits &bits, DNS::Handler *handler)
5481{
55- // count the dots
56- size_t ndots = std::count (domain, domain + strlen (domain) + 1 , ' .' );
57-
58- // if the searchpath contains less then ndots dots, and we are not already wrapped
59- // we wrap the call in a searchlookuphandler, to retry the call with the appended searchpaths
60- if (!dynamic_cast <SearchLookup*>(handler) && ndots < _ndots && !_searchpaths.empty ()) return new SearchLookup (this , type, bits, domain, handler);
61-
82+ // check if we should respect the search path
83+ if (searchable (domain, handler)) return new SearchLookup (this , type, bits, domain, handler);
84+
6285 // for A and AAAA lookups we also check the /etc/hosts file
6386 if (type == ns_t_a && _hosts.lookup (domain, 4 )) return add (new LocalLookup (this , _hosts, domain, type, handler));
6487 if (type == ns_t_aaaa && _hosts.lookup (domain, 6 )) return add (new LocalLookup (this , _hosts, domain, type, handler));
@@ -76,6 +99,8 @@ Operation *Context::query(const char *domain, ns_type type, const Bits &bits, DN
7699 }
77100}
78101
102+
103+
79104/* *
80105 * Do a reverse IP lookup, this is only meaningful for PTR lookups
81106 * @param ip the ip address to lookup
0 commit comments