• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "private-lib-core.h"
26 
27 void
lws_client_conn_wait_timeout(lws_sorted_usec_list_t * sul)28 lws_client_conn_wait_timeout(lws_sorted_usec_list_t *sul)
29 {
30 	struct lws *wsi = lws_container_of(sul, struct lws,
31 					   sul_connect_timeout);
32 
33 	/*
34 	 * This is used to constrain the time we're willing to wait for a
35 	 * connection before giving up on it and retrying.
36 	 */
37 
38 	lwsl_wsi_info(wsi, "connect wait timeout has fired");
39 	lws_client_connect_3_connect(wsi, NULL, NULL, 0, NULL);
40 }
41 
42 void
lws_client_dns_retry_timeout(lws_sorted_usec_list_t * sul)43 lws_client_dns_retry_timeout(lws_sorted_usec_list_t *sul)
44 {
45 	struct lws *wsi = lws_container_of(sul, struct lws,
46 					   sul_connect_timeout);
47 
48 	/*
49 	 * This limits the amount of dns lookups we will try before
50 	 * giving up and failing... it reuses sul_connect_timeout, which
51 	 * isn't officially used until we connected somewhere.
52 	 */
53 
54 	lwsl_wsi_info(wsi, "dns retry");
55 	if (!lws_client_connect_2_dnsreq(wsi))
56 		lwsl_wsi_notice(wsi, "DNS lookup failed");
57 }
58 
59 /*
60  * Figure out if an ongoing connect() has arrived at a final disposition or not
61  *
62  * We can check using getsockopt if our connect actually completed.
63  * Posix connect() allows nonblocking to redo the connect to
64  * find out if it succeeded.
65  */
66 
67 typedef enum {
68 	LCCCR_CONNECTED			= 1,
69 	LCCCR_CONTINUE			= 0,
70 	LCCCR_FAILED			= -1,
71 } lcccr_t;
72 
73 static lcccr_t
lws_client_connect_check(struct lws * wsi,int * real_errno)74 lws_client_connect_check(struct lws *wsi, int *real_errno)
75 {
76 	int en = 0;
77 #if !defined(WIN32)
78 	int e;
79 	socklen_t sl = sizeof(e);
80 #endif
81 
82 	(void)en;
83 
84 	/*
85 	 * This resets SO_ERROR after reading it.  If there's an error
86 	 * condition, the connect definitively failed.
87 	 */
88 
89 #if !defined(WIN32)
90 	if (!getsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_ERROR, &e, &sl)) {
91 		en = LWS_ERRNO;
92 		if (!e) {
93 			lwsl_wsi_debug(wsi, "getsockopt: conn OK errno %d", en);
94 
95 			return LCCCR_CONNECTED;
96 		}
97 
98 		lwsl_wsi_notice(wsi, "getsockopt fd %d says e %d",
99 							wsi->desc.sockfd, e);
100 
101 		*real_errno = e;
102 
103 		return LCCCR_FAILED;
104 	}
105 
106 #else
107 
108 	if (!connect(wsi->desc.sockfd, (const struct sockaddr *)&wsi->sa46_peer.sa4,
109 #if defined(WIN32)
110 				sizeof(struct sockaddr)))
111 #else
112 				0))
113 #endif
114 
115 		return LCCCR_CONNECTED;
116 
117 	en = LWS_ERRNO;
118 
119 	if (en == WSAEISCONN) /* already connected */
120 		return LCCCR_CONNECTED;
121 
122 	if (en == WSAEALREADY) {
123 		/* reset the POLLOUT wait */
124 		if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
125 			lwsl_wsi_notice(wsi, "pollfd failed");
126 	}
127 
128 	if (!en || en == WSAEINVAL ||
129 		   en == WSAEWOULDBLOCK ||
130 		   en == WSAEALREADY) {
131 		lwsl_wsi_debug(wsi, "errno %d", en);
132 		return LCCCR_CONTINUE;
133 	}
134 #endif
135 
136 	lwsl_wsi_notice(wsi, "connect check FAILED: %d",
137 			*real_errno || en);
138 
139 	return LCCCR_FAILED;
140 }
141 
142 /*
143  * We come here to fire off a connect, and to check its disposition later.
144  *
145  * If it did not complete before the individual attempt timeout, we will try to
146  * connect again with the next dns result.
147  */
148 
149 struct lws *
lws_client_connect_3_connect(struct lws * wsi,const char * ads,const struct addrinfo * result,int n,void * opaque)150 lws_client_connect_3_connect(struct lws *wsi, const char *ads,
151 			     const struct addrinfo *result, int n, void *opaque)
152 {
153 #if defined(LWS_WITH_UNIX_SOCK)
154 	struct sockaddr_un sau;
155 #endif
156 	struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
157 	const char *cce = "Unable to connect", *iface;
158 	const struct sockaddr *psa = NULL;
159 	uint16_t port = wsi->conn_port;
160 	lws_dns_sort_t *curr;
161 	ssize_t plen = 0;
162 	lws_dll2_t *d;
163 	char dcce[48];
164 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
165 	int cfail;
166 #endif
167 	int m, af = 0;
168 
169 	/*
170 	 * If we come here with result set, we need to convert getaddrinfo
171 	 * results to a lws_dns_sort_t list one time and free the results.
172 	 *
173 	 * We use this pattern because ASYNC_DNS will callback here with the
174 	 * results when it gets them (and may come here more than once, eg, for
175 	 * AAAA then A or vice-versa)
176 	 */
177 
178 	if (result) {
179 		lws_sul_cancel(&wsi->sul_connect_timeout);
180 
181 #if defined(LWS_WITH_CONMON)
182 		/* append a copy from before the sorting */
183 		lws_conmon_append_copy_new_dns_results(wsi, result);
184 #endif
185 
186 		lws_sort_dns(wsi, result);
187 #if defined(LWS_WITH_SYS_ASYNC_DNS)
188 		lws_async_dns_freeaddrinfo(&result);
189 #else
190 		freeaddrinfo((struct addrinfo *)result);
191 #endif
192 		result = NULL;
193 	}
194 
195 #if defined(LWS_WITH_UNIX_SOCK)
196 	memset(&sau, 0, sizeof(sau));
197 #endif
198 
199 	/*
200 	 * async dns calls back here for everybody who cares when it gets a
201 	 * result... but if we are piggybacking, we do not want to connect
202 	 * ourselves
203 	 */
204 
205 	if (!lws_dll2_is_detached(&wsi->dll2_cli_txn_queue))
206 		return wsi;
207 
208 	if (n &&  /* calling back with a problem */
209 	    !wsi->dns_sorted_list.count && /* there's no results */
210 	    !lws_socket_is_valid(wsi->desc.sockfd) && /* no attempt ongoing */
211 	    !wsi->speculative_connect_owner.count /* no spec attempt */ ) {
212 		lwsl_wsi_notice(wsi, "dns lookup failed %d", n);
213 
214 		/*
215 		 * DNS lookup itself failed... let's try again until we
216 		 * timeout
217 		 */
218 
219 		lwsi_set_state(wsi, LRS_UNCONNECTED);
220 		lws_sul_schedule(wsi->a.context, 0, &wsi->sul_connect_timeout,
221 				 lws_client_dns_retry_timeout,
222 						 LWS_USEC_PER_SEC);
223 		return wsi;
224 
225 //		cce = "dns lookup failed";
226 //		goto oom4;
227 	}
228 
229 	/*
230 	 * We come back here again when we think the connect() may have
231 	 * completed one way or the other, we can't proceed until we know we
232 	 * actually connected.
233 	 */
234 
235 	if (lwsi_state(wsi) == LRS_WAITING_CONNECT &&
236 	    lws_socket_is_valid(wsi->desc.sockfd)) {
237 
238 		if (!wsi->dns_sorted_list.count &&
239 		    !wsi->sul_connect_timeout.list.owner)
240 			/* no dns results and no ongoing timeout for one */
241 			goto connect_to;
242 
243 		/*
244 		 * If the connection failed, the OS-level errno may be
245 		 * something like EINPROGRESS rather than the actual problem
246 		 * that prevented a connection. This value will represent the
247 		 * ��real�� problem that we should report to the caller.
248 		 */
249 		int real_errno = 0;
250 
251 		switch (lws_client_connect_check(wsi, &real_errno)) {
252 		case LCCCR_CONNECTED:
253 			/*
254 			 * Oh, it has happened...
255 			 */
256 			goto conn_good;
257 		case LCCCR_CONTINUE:
258 			return NULL;
259 		default:
260 			if (!real_errno)
261 				real_errno = LWS_ERRNO;
262 			lws_snprintf(dcce, sizeof(dcce), "conn fail: %d",
263 				     real_errno);
264 
265 			cce = dcce;
266 			lwsl_wsi_debug(wsi, "%s", dcce);
267 			lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
268 			goto try_next_dns_result_fds;
269 		}
270 	}
271 
272 #if defined(LWS_WITH_UNIX_SOCK)
273 
274 	if (ads && *ads == '+') {
275 		ads++;
276 		memset(&wsi->sa46_peer, 0, sizeof(wsi->sa46_peer));
277 		af = sau.sun_family = AF_UNIX;
278 		strncpy(sau.sun_path, ads, sizeof(sau.sun_path));
279 		sau.sun_path[sizeof(sau.sun_path) - 1] = '\0';
280 
281 		lwsl_wsi_info(wsi, "Unix skt: %s", ads);
282 
283 		if (sau.sun_path[0] == '@')
284 			sau.sun_path[0] = '\0';
285 
286 		goto ads_known;
287 	}
288 #endif
289 
290 #if defined(LWS_WITH_SYS_ASYNC_DNS)
291 	if (n == LADNS_RET_FAILED) {
292 		lwsl_wsi_notice(wsi, "adns failed %s", ads);
293 		/*
294 		 * Caller that is giving us LADNS_RET_FAILED will deal
295 		 * with cleanup
296 		 */
297 		return NULL;
298 	}
299 #endif
300 
301 	/*
302 	 * Let's try directly connecting to each of the results in turn until
303 	 * one works, or we run out of results...
304 	 *
305 	 * We have a sorted dll2 list with the head one most preferable
306 	 */
307 
308 next_dns_result:
309 
310 	cce = "Unable to connect";
311 
312 	if (!wsi->dns_sorted_list.count)
313 		goto failed1;
314 
315 	/*
316 	 * Copy the wsi head sorted dns result into the wsi->sa46_peer, and
317 	 * remove and free the original from the sorted list
318 	 */
319 
320 	d = lws_dll2_get_head(&wsi->dns_sorted_list);
321 	curr = lws_container_of(d, lws_dns_sort_t, list);
322 
323 	lws_dll2_remove(&curr->list);
324 	wsi->sa46_peer = curr->dest;
325 #if defined(LWS_WITH_NETLINK)
326 	wsi->peer_route_uidx = curr->uidx;
327 	lwsl_wsi_info(wsi, "peer_route_uidx %d", wsi->peer_route_uidx);
328 #endif
329 
330 	lws_free(curr);
331 
332 	sa46_sockport(&wsi->sa46_peer, htons(port));
333 
334 	psa = sa46_sockaddr(&wsi->sa46_peer);
335 	n = (int)sa46_socklen(&wsi->sa46_peer);
336 
337 #if defined(LWS_WITH_UNIX_SOCK)
338 ads_known:
339 #endif
340 
341 	/*
342 	 * Now we prepared psa, if not already connecting, create the related
343 	 * socket and add to the fds
344 	 */
345 
346 	if (!lws_socket_is_valid(wsi->desc.sockfd)) {
347 
348 		if (wsi->a.context->event_loop_ops->check_client_connect_ok &&
349 		    wsi->a.context->event_loop_ops->check_client_connect_ok(wsi)
350 		) {
351 			cce = "waiting for event loop watcher to close";
352 			goto oom4;
353 		}
354 
355 #if defined(LWS_WITH_UNIX_SOCK)
356 		af = 0;
357 		if (wsi->unix_skt) {
358 			af = AF_UNIX;
359 			wsi->desc.sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
360 		}
361 		else
362 #endif
363 		{
364 			af = wsi->sa46_peer.sa4.sin_family;
365 			wsi->desc.sockfd = socket(wsi->sa46_peer.sa4.sin_family,
366 						  SOCK_STREAM, 0);
367 		}
368 
369 		if (!lws_socket_is_valid(wsi->desc.sockfd)) {
370 			lws_snprintf(dcce, sizeof(dcce),
371 				     "conn fail: skt creation: errno %d",
372 								LWS_ERRNO);
373 			cce = dcce;
374 			lwsl_wsi_warn(wsi, "%s", dcce);
375 			goto try_next_dns_result;
376 		}
377 
378 		if (lws_plat_set_socket_options(wsi->a.vhost, wsi->desc.sockfd,
379 #if defined(LWS_WITH_UNIX_SOCK)
380 						wsi->unix_skt)) {
381 #else
382 						0)) {
383 #endif
384 			lws_snprintf(dcce, sizeof(dcce),
385 				     "conn fail: skt options: errno %d",
386 								LWS_ERRNO);
387 			cce = dcce;
388 			lwsl_wsi_warn(wsi, "%s", dcce);
389 			goto try_next_dns_result_closesock;
390 		}
391 
392 		/* apply requested socket options */
393 		if (lws_plat_set_socket_options_ip(wsi->desc.sockfd,
394 						   wsi->c_pri, wsi->flags))
395 			lwsl_wsi_warn(wsi, "unable to set ip options");
396 
397 		lwsl_wsi_debug(wsi, "WAITING_CONNECT");
398 		lwsi_set_state(wsi, LRS_WAITING_CONNECT);
399 
400 		if (wsi->a.context->event_loop_ops->sock_accept)
401 			if (wsi->a.context->event_loop_ops->sock_accept(wsi)) {
402 				lws_snprintf(dcce, sizeof(dcce),
403 					     "conn fail: sock accept");
404 				cce = dcce;
405 				lwsl_wsi_warn(wsi, "%s", dcce);
406 				goto try_next_dns_result_closesock;
407 			}
408 
409 		lws_pt_lock(pt, __func__);
410 		if (__insert_wsi_socket_into_fds(wsi->a.context, wsi)) {
411 			lws_snprintf(dcce, sizeof(dcce),
412 				     "conn fail: insert fd");
413 			cce = dcce;
414 			lws_pt_unlock(pt);
415 			goto try_next_dns_result_closesock;
416 		}
417 		lws_pt_unlock(pt);
418 
419 		/*
420 		 * The fd + wsi combination is entered into the wsi tables
421 		 * at this point, with a pollfd
422 		 *
423 		 * Past here, we can't simply free the structs as error
424 		 * handling as oom4 does.
425 		 *
426 		 * We can run the whole close flow, or unpick the fds inclusion
427 		 * and anything else we have done.
428 		 */
429 
430 		if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
431 			lws_snprintf(dcce, sizeof(dcce),
432 				     "conn fail: change pollfd");
433 			cce = dcce;
434 			goto try_next_dns_result_fds;
435 		}
436 
437 		if (!wsi->a.protocol)
438 			wsi->a.protocol = &wsi->a.vhost->protocols[0];
439 
440 		lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
441 				wsi->a.vhost->connect_timeout_secs);
442 
443 		iface = lws_wsi_client_stash_item(wsi, CIS_IFACE,
444 						  _WSI_TOKEN_CLIENT_IFACE);
445 
446 		if (iface && *iface) {
447 			m = lws_socket_bind(wsi->a.vhost, wsi, wsi->desc.sockfd,
448 					    0, iface, af);
449 			if (m < 0) {
450 				lws_snprintf(dcce, sizeof(dcce),
451 					     "conn fail: socket bind");
452 				cce = dcce;
453 				goto try_next_dns_result_fds;
454 			}
455 		}
456 	}
457 
458 #if defined(LWS_WITH_UNIX_SOCK)
459 	if (wsi->unix_skt) {
460 		psa = (const struct sockaddr *)&sau;
461 		if (sau.sun_path[0])
462 			n = (int)(sizeof(uint16_t) + strlen(sau.sun_path));
463 		else
464 			n = (int)(sizeof(uint16_t) +
465 					strlen(&sau.sun_path[1]) + 1);
466 	} else
467 #endif
468 
469 	if (!psa) /* coverity */
470 		goto try_next_dns_result_fds;
471 
472 	/*
473 	 * The actual connection attempt
474 	 */
475 
476 #if defined(LWS_ESP_PLATFORM)
477 	errno = 0;
478 #endif
479 
480 	/* grab a copy for peer tracking */
481 #if defined(LWS_WITH_UNIX_SOCK)
482 	if (!wsi->unix_skt)
483 #endif
484 		memmove(&wsi->sa46_peer, psa, (unsigned int)n);
485 
486 	/*
487 	 * Finally, make the actual connection attempt
488 	 */
489 
490 #if defined(LWS_WITH_SYS_METRICS)
491 	if (wsi->cal_conn.mt) {
492 		lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
493 	}
494 	lws_metrics_caliper_bind(wsi->cal_conn, wsi->a.context->mt_conn_tcp);
495 #endif
496 
497 	wsi->socket_is_permanently_unusable = 0;
498 
499 	if (lws_fi(&wsi->fic, "conn_cb_rej") ||
500 	    user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
501 			LWS_CALLBACK_CONNECTING, wsi->user_space,
502 			(void *)(intptr_t)wsi->desc.sockfd, 0)) {
503 		lwsl_wsi_info(wsi, "CONNECTION CB closed");
504 		goto failed1;
505 	}
506 
507 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
508 	cfail = lws_fi(&wsi->fic, "connfail");
509 	if (cfail)
510 		m = -1;
511 	else
512 #endif
513 		m = connect(wsi->desc.sockfd, (const struct sockaddr *)psa,
514 			    (socklen_t)n);
515 
516 #if defined(LWS_WITH_CONMON)
517 	wsi->conmon_datum = lws_now_usecs();
518 	wsi->conmon.ciu_sockconn = 0;
519 #endif
520 
521 	if (m == -1) {
522 		/*
523 		 * Since we're nonblocking, connect not having completed is not
524 		 * necessarily indicating any problem... we have to look at
525 		 * either errno or the socket to understand if we actually
526 		 * failed already...
527 		 */
528 
529 		int errno_copy = LWS_ERRNO;
530 
531 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
532 		if (cfail)
533 			/* fake an abnormal, fatal situation */
534 			errno_copy = 999;
535 #endif
536 
537 		lwsl_wsi_debug(wsi, "connect: fd %d errno: %d",
538 				wsi->desc.sockfd, errno_copy);
539 
540 		if (errno_copy &&
541 		    errno_copy != LWS_EALREADY &&
542 		    errno_copy != LWS_EINPROGRESS &&
543 		    errno_copy != LWS_EWOULDBLOCK
544 #ifdef _WIN32
545 		 && errno_copy != WSAEINVAL
546                  && errno_copy != WSAEISCONN
547 #endif
548 		) {
549 			/*
550 			 * The connect() failed immediately...
551 			 */
552 
553 #if defined(LWS_WITH_CONMON)
554 			wsi->conmon.ciu_sockconn = (lws_conmon_interval_us_t)
555 					(lws_now_usecs() - wsi->conmon_datum);
556 #endif
557 
558 			lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
559 
560 #if defined(_DEBUG)
561 #if defined(LWS_WITH_UNIX_SOCK)
562 			if (!wsi->unix_skt) {
563 #endif
564 
565 			char nads[48];
566 
567 			lws_sa46_write_numeric_address(&wsi->sa46_peer, nads,
568 						       sizeof(nads));
569 
570 			lws_snprintf(dcce, sizeof(dcce),
571 				     "conn fail: errno %d: %s:%d",
572 						errno_copy, nads, port);
573 			cce = dcce;
574 
575 			wsi->sa46_peer.sa4.sin_family = 0;
576 			lwsl_wsi_info(wsi, "%s", cce);
577 #if defined(LWS_WITH_UNIX_SOCK)
578 			} else {
579 				lws_snprintf(dcce, sizeof(dcce),
580 					     "conn fail: errno %d: UDS %s",
581 							       errno_copy, ads);
582 				cce = dcce;
583 			}
584 #endif
585 #endif
586 
587 			goto try_next_dns_result_fds;
588 		}
589 
590 #if defined(WIN32)
591 		if (lws_plat_check_connection_error(wsi))
592 			goto try_next_dns_result_fds;
593 
594 		if (errno_copy == WSAEISCONN)
595 			goto conn_good;
596 #endif
597 
598 		/*
599 		 * The connection attempt is ongoing asynchronously... let's set
600 		 * a specialized timeout for this connect attempt completion, it
601 		 * uses wsi->sul_connect_timeout just for this purpose
602 		 */
603 
604 		lws_sul_schedule(wsi->a.context, 0, &wsi->sul_connect_timeout,
605 				 lws_client_conn_wait_timeout,
606 				 wsi->a.context->timeout_secs *
607 						 LWS_USEC_PER_SEC);
608 
609 		/*
610 		 * must do specifically a POLLOUT poll to hear
611 		 * about the connect completion
612 		 */
613 		if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
614 			goto try_next_dns_result_fds;
615 
616 		return wsi;
617 	}
618 
619 conn_good:
620 
621 	/*
622 	 * The connection has happened
623 	 */
624 
625 #if defined(LWS_WITH_CONMON)
626 	wsi->conmon.ciu_sockconn = (lws_conmon_interval_us_t)
627 					(lws_now_usecs() - wsi->conmon_datum);
628 #endif
629 
630 #if !defined(LWS_PLAT_OPTEE)
631 	{
632 		socklen_t salen = sizeof(wsi->sa46_local);
633 #if defined(_DEBUG)
634 		char buf[64];
635 #endif
636 		if (getsockname((int)wsi->desc.sockfd,
637 				(struct sockaddr *)&wsi->sa46_local,
638 				&salen) == -1)
639 			lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
640 #if defined(_DEBUG)
641 #if defined(LWS_WITH_UNIX_SOCK)
642 		if (wsi->unix_skt)
643 			buf[0] = '\0';
644 		else
645 #endif
646 			lws_sa46_write_numeric_address(&wsi->sa46_local, buf, sizeof(buf));
647 
648 		lwsl_wsi_info(wsi, "source ads %s", buf);
649 #endif
650 	}
651 #endif
652 
653 	lws_sul_cancel(&wsi->sul_connect_timeout);
654 	lws_metrics_caliper_report(wsi->cal_conn, METRES_GO);
655 
656 	lws_addrinfo_clean(wsi);
657 
658 	if (wsi->a.protocol)
659 		wsi->a.protocol->callback(wsi, LWS_CALLBACK_WSI_CREATE,
660 					  wsi->user_space, NULL, 0);
661 
662 	lwsl_wsi_debug(wsi, "going into connect_4");
663 
664 	return lws_client_connect_4_established(wsi, NULL, plen);
665 
666 oom4:
667 	/*
668 	 * We get here if we're trying to clean up a connection attempt that
669 	 * didn't make it as far as getting inserted into the wsi / fd tables
670 	 */
671 
672 	if (lwsi_role_client(wsi) && wsi->a.protocol
673 				/* && lwsi_state_est(wsi) */)
674 		lws_inform_client_conn_fail(wsi,(void *)cce, strlen(cce));
675 
676 	/* take care that we might be inserted in fds already */
677 	if (wsi->position_in_fds_table != LWS_NO_FDS_POS)
678 		/* do the full wsi close flow */
679 		goto failed1;
680 
681 	lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
682 
683 	/*
684 	 * We can't be an active client connection any more, if we thought
685 	 * that was what we were going to be doing.  It should be if we are
686 	 * failing by oom4 path, we are still called by
687 	 * lws_client_connect_via_info() and will be returning NULL to that,
688 	 * so nobody else should have had a chance to queue on us.
689 	 */
690 	{
691 		struct lws_vhost *vhost = wsi->a.vhost;
692 		lws_sockfd_type sfd = wsi->desc.sockfd;
693 
694 		//lws_vhost_lock(vhost);
695 		__lws_free_wsi(wsi); /* acquires vhost lock in wsi reset */
696 		//lws_vhost_unlock(vhost);
697 
698 		sanity_assert_no_wsi_traces(vhost->context, wsi);
699 		sanity_assert_no_sockfd_traces(vhost->context, sfd);
700 	}
701 
702 	return NULL;
703 
704 connect_to:
705 	/*
706 	 * It looks like the sul_connect_timeout fired
707 	 */
708 	lwsl_wsi_info(wsi, "abandoning connect due to timeout");
709 
710 try_next_dns_result_fds:
711 	lws_pt_lock(pt, __func__);
712 	__remove_wsi_socket_from_fds(wsi);
713 	lws_pt_unlock(pt);
714 
715 try_next_dns_result_closesock:
716 	/*
717 	 * We are killing the socket but leaving
718 	 */
719 	compatible_close(wsi->desc.sockfd);
720 	wsi->desc.sockfd = LWS_SOCK_INVALID;
721 
722 try_next_dns_result:
723 	lws_sul_cancel(&wsi->sul_connect_timeout);
724 	if (lws_dll2_get_head(&wsi->dns_sorted_list))
725 		goto next_dns_result;
726 
727 	lws_addrinfo_clean(wsi);
728 	lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce));
729 
730 failed1:
731 	lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "client_connect3");
732 
733 	return NULL;
734 }
735