@@ -107,6 +107,19 @@ local function each_matching_record(pkt, name, type)
107107 return pkt :grep (params )
108108end
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+
110123local function connect (options , timeout )
111124 local bind = options .bind
112125 if bind ~= nil then
@@ -134,51 +147,20 @@ local function connect(options, timeout)
134147 local dns_resolver = options .dns_resolver
135148 if dns_resolver then
136149 local deadline = timeout and monotime ()+ timeout
137- local hostv4 , hostv6
138- if family == nil or family == cs .AF_UNSPEC or family == cs .AF_INET6 then
139- -- Query for AAAA record
140- local packet = ca .fileresult (dns_resolver :query (host , cqueues_dns_record .AAAA , nil , timeout ))
141- if packet then
142- -- If IPv6 explicitly requested then filter down to only AAAA records
143- local type = (family == cs .AF_INET6 ) and cqueues_dns_record .AAAA or nil
144- for rec in each_matching_record (packet , host , type ) do
145- local t = rec :type ()
146- if t == cqueues_dns_record .AAAA then
147- hostv6 = rec :addr ()
148- break
149- elseif t == cqueues_dns_record .A then
150- hostv4 = rec :addr ()
151- break
152- end
153- end
154- end
155- end
156- if (hostv4 == nil and hostv6 == nil ) and (family == nil or family == cs .AF_UNSPEC or family == cs .AF_INET ) then
157- -- Query for A record
158- local packet = ca .fileresult (dns_resolver :query (host , cqueues_dns_record .A , nil , deadline and deadline - monotime ()))
159- if packet then
160- -- If IPv4 explicitly requested then filter down to only A records
161- -- Skip AAAA if we already have hostv6
162- local type = (family == cs .AF_INET or hostv6 ) and cqueues_dns_record .A or nil
163- for rec in each_matching_record (packet , host , type ) do
164- local t = rec :type ()
165- if t == cqueues_dns_record .A then
166- hostv4 = rec :addr ()
167- break
168- elseif t == cqueues_dns_record .AAAA then
169- hostv6 = rec :addr ()
170- break
171- end
172- end
173- end
150+ local records = {}
151+ if family == nil or family == cs .AF_UNSPEC then
152+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , nil , timeout )
153+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , nil , deadline and deadline - monotime ())
154+ elseif family == cs .AF_INET then
155+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , cqueues_dns_record .A , timeout )
156+ elseif family == cs .AF_INET6 then
157+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , cqueues_dns_record .AAAA , timeout )
174158 end
175- if hostv6 then
176- host = hostv6
177- elseif hostv4 then
178- host = hostv4
179- else
159+ local rec = records [1 ]
160+ if not rec then
180161 return nil , " The name does not resolve for the supplied parameters"
181162 end
163+ host = rec :addr ()
182164 timeout = deadline and deadline - monotime ()
183165 end
184166 end
0 commit comments