upstreams_cleanup from upstreams_dereference

This commit is contained in:
Willem Toorop 2015-04-18 22:17:28 +02:00
parent 84c5b67ee0
commit 0ba6af3523
3 changed files with 36 additions and 46 deletions

View File

@ -470,29 +470,22 @@ upstreams_resize(getdns_upstreams *upstreams, size_t size)
return r; return r;
} }
static void void
upstreams_cleanup(getdns_upstreams *upstreams) priv_getdns_upstreams_dereference(getdns_upstreams *upstreams)
{ {
if (!upstreams) size_t i;
return;
for (int i = 0; i < (int)upstreams->count; i++) {
if (upstreams->upstreams[i].tls_obj != NULL) {
SSL_shutdown(upstreams->upstreams[i].tls_obj);
SSL_free(upstreams->upstreams[i].tls_obj);
upstreams->upstreams[i].tls_obj = NULL;
}
if (upstreams->upstreams[i].fd != -1) {
close(upstreams->upstreams[i].fd);
upstreams->upstreams[i].fd = -1;
}
}
}
static void if (upstreams && --upstreams->referenced == 0) {
upstreams_dereference(getdns_upstreams *upstreams) for (i = 0; i < upstreams->count; i++) {
{ if (upstreams->upstreams[i].tls_obj != NULL) {
if (upstreams && --upstreams->referenced == 0) SSL_shutdown(upstreams->upstreams[i].tls_obj);
SSL_free(upstreams->upstreams[i].tls_obj);
}
if (upstreams->upstreams[i].fd != -1)
close(upstreams->upstreams[i].fd);
}
GETDNS_FREE(upstreams->mf, upstreams); GETDNS_FREE(upstreams->mf, upstreams);
}
} }
static uint8_t* static uint8_t*
@ -537,11 +530,9 @@ upstream_ntop_buf(getdns_upstream *upstream, getdns_transport_t transport,
if (transport == GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN) if (transport == GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN)
(void) snprintf(buf + strlen(buf), len - strlen(buf), (void) snprintf(buf + strlen(buf), len - strlen(buf),
"@%d", GETDNS_TLS_PORT); "@%d", GETDNS_TLS_PORT);
else { else if (upstream_port(upstream) != 53 && upstream_port(upstream) != 0)
if (upstream_port(upstream) != 53 && upstream_port(upstream) != 0) (void) snprintf(buf + strlen(buf), len - strlen(buf),
(void) snprintf(buf + strlen(buf), len - strlen(buf), "@%d", (int)upstream_port(upstream));
"@%d", (int)upstream_port(upstream));
}
} }
static int static int
@ -919,8 +910,7 @@ getdns_context_destroy(struct getdns_context *context)
getdns_traverse_postorder(&context->local_hosts, getdns_traverse_postorder(&context->local_hosts,
destroy_local_host, context); destroy_local_host, context);
upstreams_cleanup(context->upstreams); priv_getdns_upstreams_dereference(context->upstreams);
upstreams_dereference(context->upstreams);
GETDNS_FREE(context->my_mf, context); GETDNS_FREE(context->my_mf, context);
} /* getdns_context_destroy */ } /* getdns_context_destroy */
@ -1524,8 +1514,7 @@ getdns_context_set_upstream_recursive_servers(struct getdns_context *context,
upstreams->count++; upstreams->count++;
freeaddrinfo(ai); freeaddrinfo(ai);
} }
upstreams_dereference(context->upstreams); priv_getdns_upstreams_dereference(context->upstreams);
/*Don't the existing upstreams need to be handled before overwritting here?*/
context->upstreams = upstreams; context->upstreams = upstreams;
dispatch_updated(context, dispatch_updated(context,
GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS); GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS);
@ -1535,7 +1524,7 @@ getdns_context_set_upstream_recursive_servers(struct getdns_context *context,
invalid_parameter: invalid_parameter:
r = GETDNS_RETURN_INVALID_PARAMETER; r = GETDNS_RETURN_INVALID_PARAMETER;
error: error:
upstreams_dereference(upstreams); priv_getdns_upstreams_dereference(upstreams);
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
} /* getdns_context_set_upstream_recursive_servers */ } /* getdns_context_set_upstream_recursive_servers */
@ -1731,14 +1720,14 @@ getdns_cancel_callback(getdns_context *context,
} /* getdns_cancel_callback */ } /* getdns_cancel_callback */
static getdns_return_t static getdns_return_t
ub_setup_stub(struct ub_ctx *ctx, struct getdns_context *context) ub_setup_stub(struct ub_ctx *ctx, getdns_context *context)
{ {
getdns_return_t r = GETDNS_RETURN_GOOD; getdns_return_t r = GETDNS_RETURN_GOOD;
size_t i; size_t i;
getdns_upstream *upstream; getdns_upstream *upstream;
char addr[1024]; char addr[1024];
getdns_upstreams *upstreams = context->upstreams; getdns_upstreams *upstreams = context->upstreams;
(void) ub_ctx_set_fwd(ctx, NULL); (void) ub_ctx_set_fwd(ctx, NULL);
for (i = 0; i < upstreams->count; i++) { for (i = 0; i < upstreams->count; i++) {
upstream = &upstreams->upstreams[i]; upstream = &upstreams->upstreams[i];
@ -1840,21 +1829,21 @@ getdns_context_prepare_for_resolution(struct getdns_context *context,
/* TODO: move this transport logic to a separate functions*/ /* TODO: move this transport logic to a separate functions*/
if (context->resolution_type == GETDNS_RESOLUTION_STUB) { if (context->resolution_type == GETDNS_RESOLUTION_STUB) {
switch (context->dns_transport) { switch (context->dns_transport) {
case GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN: case GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN:
case GETDNS_TRANSPORT_TLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN: case GETDNS_TRANSPORT_TLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN:
if (context->tls_ctx == NULL) { if (context->tls_ctx == NULL) {
#ifdef HAVE_LIBTLS1_2 #ifdef HAVE_LIBTLS1_2
/* Create client context, use TLS v1.2 only for now */ /* Create client context, use TLS v1.2 only for now */
context->tls_ctx = SSL_CTX_new(TLSv1_2_client_method()); context->tls_ctx = SSL_CTX_new(TLSv1_2_client_method());
#endif #endif
if(!context->tls_ctx && context->dns_transport == if(!context->tls_ctx && context->dns_transport ==
GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN) { GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN) {
return GETDNS_RETURN_BAD_CONTEXT; return GETDNS_RETURN_BAD_CONTEXT;
}
} }
break; }
default: break;
break; default:
break;
} }
} }
/* Block use of TLS ONLY in recursive mode as it won't work */ /* Block use of TLS ONLY in recursive mode as it won't work */

View File

@ -233,4 +233,6 @@ void priv_getdns_context_ub_read_cb(void *userarg);
getdns_base_transport_t priv_get_base_transport(getdns_transport_t transport, int level); getdns_base_transport_t priv_get_base_transport(getdns_transport_t transport, int level);
void priv_getdns_upstreams_dereference(getdns_upstreams *upstreams);
#endif /* _GETDNS_CONTEXT_H_ */ #endif /* _GETDNS_CONTEXT_H_ */

View File

@ -177,8 +177,7 @@ dns_req_free(getdns_dns_req * req)
return; return;
} }
if (req->upstreams && --req->upstreams->referenced == 0) priv_getdns_upstreams_dereference(req->upstreams);
GETDNS_FREE(req->upstreams->mf, req->upstreams);
/* cleanup network requests */ /* cleanup network requests */
for (net_req = req->netreqs; *net_req; net_req++) for (net_req = req->netreqs; *net_req; net_req++)