static void
tcp_alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf);
-static bool
-inactive(isc_nmsocket_t *sock) {
- return (!isc__nmsocket_active(sock) || atomic_load(&sock->closing) ||
- atomic_load(&sock->mgr->closing) ||
- (sock->server != NULL && !isc__nmsocket_active(sock->server)));
-}
-
static void
failed_accept_cb(isc_nmsocket_t *sock, isc_result_t eresult) {
REQUIRE(sock->accepting);
r = uv_timer_init(&worker->loop, &sock->timer);
RUNTIME_CHECK(r == 0);
+ uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
+
+ if (isc__nm_closing(sock)) {
+ result = ISC_R_CANCELED;
+ goto error;
+ }
r = uv_tcp_open(&sock->uv_handle.tcp, sock->fd);
if (r != 0) {
done:
result = isc__nm_uverr2result(r);
-
+error:
LOCK(&sock->lock);
sock->result = result;
SIGNAL(&sock->cond);
*/
isc__nm_uvreq_put(&req, sock);
return;
- } else if (!isc__nmsocket_active(sock)) {
+ } else if (isc__nmsocket_closing(sock)) {
/* Socket was closed midflight by isc__nm_tcp_shutdown() */
result = ISC_R_CANCELED;
goto error;
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
result = ISC_R_CANCELED;
goto done;
}
REQUIRE(sock->tid == isc_nm_tid());
UNUSED(worker);
- if (inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
sock->reading = true;
failed_read_cb(sock, ISC_R_CANCELED);
return;
REQUIRE(sock->reading);
REQUIRE(buf != NULL);
- if (inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
failed_read_cb(sock, ISC_R_CANCELED);
goto free;
}
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
if (quota != NULL) {
isc_quota_detach("a);
}
int r;
- if (inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
return (ISC_R_CANCELED);
}
return;
}
- if (sock->statichandle) {
+ if (sock->statichandle != NULL) {
failed_read_cb(sock, ISC_R_CANCELED);
return;
}
RUNTIME_CHECK(r == 0);
uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
+ if (isc__nm_closing(sock)) {
+ result = ISC_R_CANCELED;
+ goto error;
+ }
+
r = uv_tcp_open(&sock->uv_handle.tcp, sock->fd);
if (r != 0) {
isc__nm_closesocket(sock->fd);
done:
result = isc__nm_uverr2result(r);
-
+error:
LOCK(&sock->lock);
sock->result = result;
SIGNAL(&sock->cond);
*/
isc__nm_uvreq_put(&req, sock);
return;
- } else if (!isc__nmsocket_active(sock)) {
+ } else if (isc__nmsocket_closing(sock)) {
/* Socket was closed midflight by isc__nm_tcpdns_shutdown() */
result = ISC_R_CANCELED;
goto error;
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (isc__nm_inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
result = ISC_R_CANCELED;
goto done;
}
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_nm_tid());
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
sock->reading = true;
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
return;
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_nm_tid());
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
return (ISC_R_CANCELED);
}
REQUIRE(sock->reading);
REQUIRE(buf != NULL);
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
goto free;
}
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (isc__nm_inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
if (quota != NULL) {
isc_quota_detach("a);
}
UNUSED(worker);
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
result = ISC_R_CANCELED;
goto fail;
}
isc__nmsocket_timer_stop(sock);
isc__nm_stop_reading(sock);
+ uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
uv_close((uv_handle_t *)&sock->timer, timer_close_cb);
}
return;
}
- if (sock->statichandle) {
+ if (sock->statichandle != NULL) {
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
return;
}
atomic_store(&sock->connecting, true);
+ /* 2 minute timeout */
+ result = isc__nm_socket_connectiontimeout(sock->fd, 120 * 1000);
+ RUNTIME_CHECK(result == ISC_R_SUCCESS);
+
r = uv_tcp_init(&worker->loop, &sock->uv_handle.tcp);
RUNTIME_CHECK(r == 0);
uv_handle_set_data(&sock->uv_handle.handle, sock);
RUNTIME_CHECK(r == 0);
uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
+ if (isc__nm_closing(sock)) {
+ result = ISC_R_CANCELED;
+ goto error;
+ }
+
r = uv_tcp_open(&sock->uv_handle.tcp, sock->fd);
if (r != 0) {
isc__nm_closesocket(sock->fd);
done:
result = isc__nm_uverr2result(r);
-
+error:
LOCK(&sock->lock);
sock->result = result;
SIGNAL(&sock->cond);
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_nm_tid());
- isc__nmsocket_timer_stop(sock);
- uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
-
req = uv_handle_get_data((uv_handle_t *)uvreq);
REQUIRE(VALID_UVREQ(req));
*/
isc__nm_uvreq_put(&req, sock);
return;
- } else if (!isc__nmsocket_active(sock)) {
- /* Socket was closed midflight by isc__nm_tcpdns_shutdown() */
+ } else if (isc__nmsocket_closing(sock)) {
+ /* Socket was closed midflight by isc__nm_tlsdns_shutdown() */
result = ISC_R_CANCELED;
goto error;
} else if (status == UV_ETIMEDOUT) {
result = isc_sockaddr_fromsockaddr(&sock->peer, (struct sockaddr *)&ss);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
+ /* Setting pending req */
sock->tls.pending_req = req;
isc__nm_process_sock_buffer(sock);
result = tls_cycle(sock);
if (result != ISC_R_SUCCESS) {
+ sock->tls.pending_req = NULL;
goto error;
}
return;
error:
- sock->tls.pending_req = NULL;
isc__nm_failed_connect_cb(sock, req, result);
}
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (isc__nm_inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
result = ISC_R_CANCELED;
goto done;
}
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_nm_tid());
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
sock->reading = true;
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
return;
REQUIRE(VALID_NMSOCK(sock));
REQUIRE(sock->tid == isc_nm_tid());
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
return (ISC_R_CANCELED);
}
sock->buf + sock->buf_len,
sock->buf_size - sock->buf_len, &len);
if (rv != 1) {
- /* Process what's in the buffer so far
+ /*
+ * Process what's in the buffer so far
*/
isc__nm_process_sock_buffer(sock);
- /* FIXME: Should we call
- * failed_read_cb()? */
+ /*
+ * FIXME: Should we call
+ * isc__nm_failed_read_cb()?
+ */
break;
}
- REQUIRE((size_t)pending == len);
+ INSIST((size_t)pending == len);
sock->buf_len += len;
isc__nm_uvreq_t *req = sock->tls.pending_req;
sock->tls.pending_req = NULL;
+ isc__nmsocket_timer_stop(sock);
+ uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
+
atomic_store(&sock->connecting, false);
isc__nm_connectcb(sock, req, ISC_R_SUCCESS, true);
switch (sock->tls.state) {
case TLS_STATE_HANDSHAKE:
case TLS_STATE_IO:
- isc__nm_tlsdns_failed_read_cb(sock, result);
+ if (atomic_load(&sock->connecting)) {
+ isc__nm_uvreq_t *req = sock->tls.pending_req;
+ sock->tls.pending_req = NULL;
+ isc__nm_failed_connect_cb(sock, req, result);
+ } else {
+ isc__nm_tlsdns_failed_read_cb(sock, result);
+ }
break;
case TLS_STATE_ERROR:
return;
sock->tls.state = TLS_STATE_ERROR;
sock->tls.pending_error = result;
- /* tlsdns_close_direct(sock); */
+ isc__nmsocket_shutdown(sock);
}
static void
tls_cycle(isc_nmsocket_t *sock) {
isc_result_t result;
+ if (isc__nmsocket_closing(sock)) {
+ return (ISC_R_CANCELED);
+ }
+
result = tls_pop_error(sock);
if (result != ISC_R_SUCCESS) {
goto done;
REQUIRE(VALID_NMSOCK(sock));
/* Socket was closed midflight by isc__nm_tlsdns_shutdown() */
- if (!isc__nmsocket_active(sock)) {
+ if (isc__nmsocket_closing(sock)) {
return;
}
REQUIRE(sock->reading);
REQUIRE(buf != NULL);
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
goto free;
}
REQUIRE(VALID_NMSOCK(ssock));
REQUIRE(ssock->tid == isc_nm_tid());
- if (isc__nm_inactive(ssock)) {
+ if (isc__nmsocket_closing(ssock)) {
if (quota != NULL) {
isc_quota_detach("a);
}
return (result);
}
- if (isc__nm_inactive(sock)) {
+ if (isc__nmsocket_closing(sock)) {
return (ISC_R_CANCELED);
}
isc__nmsocket_timer_stop(sock);
isc__nm_stop_reading(sock);
+
+ uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
uv_close((uv_handle_t *)&sock->timer, timer_close_cb);
}
return;
}
- if (sock->accepting) {
- return;
+ if (sock->tls.tls) {
+ /* Shutdown any active TLS connections */
+ (void)SSL_shutdown(sock->tls.tls);
}
- if (sock->tls.pending_req != NULL) {
- isc__nm_uvreq_t *req = sock->tls.pending_req;
- sock->tls.pending_req = NULL;
-
- isc__nm_failed_connect_cb(sock, req, ISC_R_CANCELED);
-
+ if (sock->accepting) {
return;
}
+ /* TLS handshake hasn't been completed yet */
if (atomic_load(&sock->connecting)) {
+ /*
+ * TCP connection has been established, now waiting on
+ * TLS handshake to complete
+ */
+ if (sock->tls.pending_req != NULL) {
+ isc__nm_uvreq_t *req = sock->tls.pending_req;
+ sock->tls.pending_req = NULL;
+
+ isc__nm_failed_connect_cb(sock, req, ISC_R_CANCELED);
+ return;
+ }
+
+ /* The TCP connection hasn't been established yet */
isc_nmsocket_t *tsock = NULL;
isc__nmsocket_attach(sock, &tsock);
uv_close(&sock->uv_handle.handle, tlsdns_close_connect_cb);
return;
}
- if (sock->statichandle) {
+ if (sock->statichandle != NULL) {
isc__nm_failed_read_cb(sock, ISC_R_CANCELED);
return;
}