@@ -115,8 +115,10 @@ local function dns_lookup(dns_resolver, host, query_type, filter_type, records,
115
115
end
116
116
for rec in each_matching_record (packet , host , filter_type ) do
117
117
local t = rec :type ()
118
- if t == cqueues_dns_record .AAAA or t == cqueues_dns_record .A then
119
- table.insert (records , rec )
118
+ if t == cqueues_dns_record .AAAA then
119
+ table.insert (records , { family = cs .AF_INET6 , host = rec :addr () })
120
+ elseif t == cqueues_dns_record .A then
121
+ table.insert (records , { family = cs .AF_INET , host = rec :addr () })
120
122
end
121
123
end
122
124
end
@@ -166,11 +168,17 @@ local function connect(options, timeout)
166
168
}
167
169
end
168
170
171
+ local deadline = timeout and monotime ()+ timeout
172
+
169
173
local host = options .host
170
- if not path and not http_util .is_ip (host ) then
174
+ local records
175
+ if path then
176
+ records = { { family = family , path = path } }
177
+ elseif http_util .is_ip (host ) then
178
+ records = { { family = family , host = host } }
179
+ else
171
180
local dns_resolver = options .dns_resolver or cqueues_dns .getpool ()
172
- local deadline = timeout and monotime ()+ timeout
173
- local records = {}
181
+ records = {}
174
182
if family == cs .AF_UNSPEC then
175
183
dns_lookup (dns_resolver , host , cqueues_dns_record .AAAA , nil , records , timeout )
176
184
dns_lookup (dns_resolver , host , cqueues_dns_record .A , nil , records , deadline and deadline - monotime ())
@@ -179,28 +187,45 @@ local function connect(options, timeout)
179
187
elseif family == cs .AF_INET6 then
180
188
dns_lookup (dns_resolver , host , cqueues_dns_record .AAAA , cqueues_dns_record .AAAA , records , timeout )
181
189
end
182
- local rec = records [1 ]
183
- if not rec then
184
- return nil , " The name does not resolve for the supplied parameters"
185
- end
186
- host = rec :addr ()
187
190
timeout = deadline and deadline - monotime ()
188
191
end
189
192
190
- local s , err , errno = ca . fileresult ( cs . connect {
191
- family = family ;
192
- host = host ;
193
+ local connect_params = {
194
+ family = nil ;
195
+ host = nil ;
193
196
port = options .port ;
194
- path = path ;
197
+ path = nil ;
195
198
bind = bind ;
196
199
sendname = false ;
197
200
v6only = options .v6only ;
198
201
nodelay = true ;
199
- })
200
- if s == nil then
201
- return nil , err , errno
202
+ }
203
+
204
+ local lasterr , lasterrno = " The name does not resolve for the supplied parameters"
205
+ for _ , rec in ipairs (records ) do
206
+ connect_params .family = rec .family ;
207
+ connect_params .host = rec .host ;
208
+ connect_params .path = rec .path ;
209
+ local s
210
+ s , lasterr , lasterrno = ca .fileresult (cs .connect (connect_params ))
211
+ if s then
212
+ local c
213
+ c , lasterr , lasterrno = negotiate (s , options , timeout )
214
+ if c then
215
+ -- Force TCP connect to occur
216
+ local ok
217
+ ok , lasterr , lasterrno = c :connect (deadline and deadline - monotime ())
218
+ if ok then
219
+ return c
220
+ end
221
+ c :close ()
222
+ else
223
+ s :close ()
224
+ end
225
+ timeout = deadline and deadline - monotime ()
226
+ end
202
227
end
203
- return negotiate ( s , options , timeout )
228
+ return nil , lasterr , lasterrno
204
229
end
205
230
206
231
return {
0 commit comments