|
|
|
@ -78,23 +78,9 @@ static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
|
|
|
|
|
|
|
|
|
|
static int ssl_close(void *cookie)
|
|
|
|
|
{
|
|
|
|
|
int cookie_fd = SSL_get_fd(cookie);
|
|
|
|
|
int ret;
|
|
|
|
|
if (cookie_fd > -1) {
|
|
|
|
|
/*
|
|
|
|
|
* According to the TLS standard, it is acceptable for an application to only send its shutdown
|
|
|
|
|
* alert and then close the underlying connection without waiting for the peer's response (this
|
|
|
|
|
* way resources can be saved, as the process can already terminate or serve another connection).
|
|
|
|
|
*/
|
|
|
|
|
if ((ret = SSL_shutdown(cookie)) < 0) {
|
|
|
|
|
ast_log(LOG_ERROR, "SSL_shutdown() failed: %d\n", SSL_get_error(cookie, ret));
|
|
|
|
|
}
|
|
|
|
|
SSL_free(cookie);
|
|
|
|
|
/* adding shutdown(2) here has no added benefit */
|
|
|
|
|
if (close(cookie_fd)) {
|
|
|
|
|
ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(SSL_get_fd(cookie));
|
|
|
|
|
SSL_shutdown(cookie);
|
|
|
|
|
SSL_free(cookie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif /* DO_SSL */
|
|
|
|
@ -159,7 +145,8 @@ static void *handle_tcptls_connection(void *data)
|
|
|
|
|
if (!tcptls_session->parent->tls_cfg) {
|
|
|
|
|
if ((tcptls_session->f = fdopen(tcptls_session->fd, "w+"))) {
|
|
|
|
|
if(setvbuf(tcptls_session->f, NULL, _IONBF, 0)) {
|
|
|
|
|
ast_tcptls_close_session_file(tcptls_session);
|
|
|
|
|
fclose(tcptls_session->f);
|
|
|
|
|
tcptls_session->f = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -225,7 +212,8 @@ static void *handle_tcptls_connection(void *data)
|
|
|
|
|
if (peer) {
|
|
|
|
|
X509_free(peer);
|
|
|
|
|
}
|
|
|
|
|
ast_tcptls_close_session_file(tcptls_session);
|
|
|
|
|
close(tcptls_session->fd);
|
|
|
|
|
fclose(tcptls_session->f);
|
|
|
|
|
ao2_ref(tcptls_session, -1);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
@ -242,7 +230,7 @@ static void *handle_tcptls_connection(void *data)
|
|
|
|
|
#endif /* DO_SSL */
|
|
|
|
|
|
|
|
|
|
if (!tcptls_session->f) {
|
|
|
|
|
ast_tcptls_close_session_file(tcptls_session);
|
|
|
|
|
close(tcptls_session->fd);
|
|
|
|
|
ast_log(LOG_WARNING, "FILE * open failed!\n");
|
|
|
|
|
#ifndef DO_SSL
|
|
|
|
|
if (tcptls_session->parent->tls_cfg) {
|
|
|
|
@ -288,9 +276,7 @@ void *ast_tcptls_server_root(void *data)
|
|
|
|
|
tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
|
|
|
|
|
if (!tcptls_session) {
|
|
|
|
|
ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
|
|
|
|
|
if (close(fd)) {
|
|
|
|
|
ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -307,7 +293,7 @@ void *ast_tcptls_server_root(void *data)
|
|
|
|
|
/* This thread is now the only place that controls the single ref to tcptls_session */
|
|
|
|
|
if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
|
|
|
|
|
ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
|
|
|
|
|
ast_tcptls_close_session_file(tcptls_session);
|
|
|
|
|
close(tcptls_session->fd);
|
|
|
|
|
ao2_ref(tcptls_session, -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -572,24 +558,6 @@ error:
|
|
|
|
|
desc->accept_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
|
|
|
|
|
{
|
|
|
|
|
if (tcptls_session->f) {
|
|
|
|
|
if (fclose(tcptls_session->f)) {
|
|
|
|
|
ast_log(LOG_ERROR, "fclose() failed: %s\n", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
tcptls_session->f = NULL;
|
|
|
|
|
tcptls_session->fd = -1;
|
|
|
|
|
} else if (tcptls_session->fd != -1) {
|
|
|
|
|
if (close(tcptls_session->fd)) {
|
|
|
|
|
ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
tcptls_session->fd = -1;
|
|
|
|
|
} else {
|
|
|
|
|
ast_log(LOG_ERROR, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
|
|
|
|
|
{
|
|
|
|
|
if (desc->master != AST_PTHREADT_NULL) {
|
|
|
|
|