Skip to content

Commit 879eeef

Browse files
committed
http/client: Collect result of all dns lookups before proceeding
1 parent 4f302c4 commit 879eeef

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

http/client.lua

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ local function each_matching_record(pkt, name, type)
107107
return pkt:grep(params)
108108
end
109109

110+
local function dns_lookup(records, dns_resolver, host, query_type, filter_type, timeout)
111+
local packet = dns_resolver:query(host, query_type, nil, timeout)
112+
if not packet then
113+
return
114+
end
115+
for rec in each_matching_record(packet, host, filter_type) do
116+
local t = rec:type()
117+
if t == cqueues_dns_record.AAAA or t == cqueues_dns_record.A then
118+
table.insert(records, rec)
119+
end
120+
end
121+
end
122+
110123
local function connect(options, timeout)
111124
local family = options.family
112125
local path = options.path
@@ -115,51 +128,20 @@ local function connect(options, timeout)
115128
local dns_resolver = options.dns_resolver
116129
if dns_resolver then
117130
local deadline = timeout and monotime()+timeout
118-
local hostv4, hostv6
119-
if family == nil or family == cs.AF_UNSPEC or family == cs.AF_INET6 then
120-
-- Query for AAAA record
121-
local packet = ca.fileresult(dns_resolver:query(host, cqueues_dns_record.AAAA, nil, timeout))
122-
if packet then
123-
-- If IPv6 explicitly requested then filter down to only AAAA records
124-
local type = (family == cs.AF_INET6) and cqueues_dns_record.AAAA or nil
125-
for rec in each_matching_record(packet, host, type) do
126-
local t = rec:type()
127-
if t == cqueues_dns_record.AAAA then
128-
hostv6 = rec:addr()
129-
break
130-
elseif t == cqueues_dns_record.A then
131-
hostv4 = rec:addr()
132-
break
133-
end
134-
end
135-
end
136-
end
137-
if (hostv4 == nil and hostv6 == nil) and (family == nil or family == cs.AF_UNSPEC or family == cs.AF_INET) then
138-
-- Query for A record
139-
local packet = ca.fileresult(dns_resolver:query(host, cqueues_dns_record.A, nil, deadline and deadline-monotime()))
140-
if packet then
141-
-- If IPv4 explicitly requested then filter down to only A records
142-
-- Skip AAAA if we already have hostv6
143-
local type = (family == cs.AF_INET or hostv6) and cqueues_dns_record.A or nil
144-
for rec in each_matching_record(packet, host, type) do
145-
local t = rec:type()
146-
if t == cqueues_dns_record.A then
147-
hostv4 = rec:addr()
148-
break
149-
elseif t == cqueues_dns_record.AAAA then
150-
hostv6 = rec:addr()
151-
break
152-
end
153-
end
154-
end
131+
local records = {}
132+
if family == nil or family == cs.AF_UNSPEC then
133+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, nil, timeout)
134+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, nil, deadline and deadline-monotime())
135+
elseif family == cs.AF_INET then
136+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
137+
elseif family == cs.AF_INET6 then
138+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
155139
end
156-
if hostv6 then
157-
host = hostv6
158-
elseif hostv4 then
159-
host = hostv4
160-
else
140+
local rec = records[1]
141+
if not rec then
161142
return nil, "The name does not resolve for the supplied parameters"
162143
end
144+
host = rec:addr()
163145
timeout = deadline and deadline-monotime()
164146
end
165147
end

0 commit comments

Comments
 (0)