Skip to content

Commit cb68e9b

Browse files
committed
tls_openssl: Merge two info callback functions into one
Signed-off-by: Jack Lau <[email protected]>
1 parent b4fd042 commit cb68e9b

File tree

1 file changed

+7
-73
lines changed

1 file changed

+7
-73
lines changed

libavformat/tls_openssl.c

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -754,87 +754,21 @@ static av_cold void init_bio_method(URLContext *h)
754754
SSL_set_bio(p->ssl, bio, bio);
755755
}
756756

757-
/**
758-
* Callback function to print the OpenSSL SSL status.
759-
*/
760-
static void openssl_dtls_on_info(const SSL *dtls, int where, int r0)
761-
{
762-
int w, r1, is_fatal, is_warning, is_close_notify;
763-
const char *method = "undefined", *alert_type, *alert_desc;
764-
TLSContext *ctx = (TLSContext*)SSL_get_ex_data(dtls, 0);
765-
766-
w = where & ~SSL_ST_MASK;
767-
if (w & SSL_ST_CONNECT) {
768-
method = "SSL_connect";
769-
} else if (w & SSL_ST_ACCEPT)
770-
method = "SSL_accept";
771-
772-
r1 = SSL_get_error(ctx->ssl, r0);
773-
if (where & SSL_CB_LOOP) {
774-
av_log(ctx, AV_LOG_VERBOSE, "DTLS: Info method=%s state=%s(%s), where=%d, ret=%d, r1=%d\n",
775-
method, SSL_state_string(dtls), SSL_state_string_long(dtls), where, r0, r1);
776-
} else if (where & SSL_CB_ALERT) {
777-
method = (where & SSL_CB_READ) ? "read":"write";
778-
779-
alert_type = SSL_alert_type_string_long(r0);
780-
alert_desc = SSL_alert_desc_string(r0);
781-
782-
if (!av_strcasecmp(alert_type, "warning") && !av_strcasecmp(alert_desc, "CN")) {
783-
av_log(ctx, AV_LOG_WARNING, "DTLS: SSL3 alert method=%s type=%s, desc=%s(%s), where=%d, ret=%d, r1=%d\n",
784-
method, alert_type, alert_desc, SSL_alert_desc_string_long(r0), where, r0, r1);
785-
} else
786-
av_log(ctx, AV_LOG_ERROR, "DTLS: SSL3 alert method=%s type=%s, desc=%s(%s), where=%d, ret=%d, r1=%d %s\n",
787-
method, alert_type, alert_desc, SSL_alert_desc_string_long(r0), where, r0, r1, ctx->error_message);
788-
789-
/**
790-
* Notify the DTLS to handle the ALERT message, which maybe means media connection disconnect.
791-
* CN(Close Notify) is sent when peer close the PeerConnection. fatal, IP(Illegal Parameter)
792-
* is sent when DTLS failed.
793-
*/
794-
is_fatal = !av_strncasecmp(alert_type, "fatal", 5);
795-
is_warning = !av_strncasecmp(alert_type, "warning", 7);
796-
is_close_notify = !av_strncasecmp(alert_desc, "CN", 2);
797-
ctx->tls_shared.state = is_fatal ? DTLS_STATE_FAILED : (is_warning && is_close_notify ? DTLS_STATE_CLOSED : DTLS_STATE_NONE);
798-
if (ctx->tls_shared.state != DTLS_STATE_NONE) {
799-
av_log(ctx, AV_LOG_INFO, "DTLS: Notify ctx=%p, state=%d, fatal=%d, warning=%d, cn=%d\n",
800-
ctx, ctx->tls_shared.state, is_fatal, is_warning, is_close_notify);
801-
}
802-
} else if (where & SSL_CB_EXIT) {
803-
if (!r0) {
804-
av_log(ctx, AV_LOG_WARNING, "DTLS: Fail method=%s state=%s(%s), where=%d, ret=%d, r1=%d\n",
805-
method, SSL_state_string(dtls), SSL_state_string_long(dtls), where, r0, r1);
806-
} else if (r0 < 0) {
807-
if (r1 != SSL_ERROR_NONE && r1 != SSL_ERROR_WANT_READ && r1 != SSL_ERROR_WANT_WRITE) {
808-
av_log(ctx, AV_LOG_ERROR, "DTLS: Error method=%s state=%s(%s), where=%d, ret=%d, r1=%d %s\n",
809-
method, SSL_state_string(dtls), SSL_state_string_long(dtls), where, r0, r1, ctx->error_message);
810-
} else
811-
av_log(ctx, AV_LOG_VERBOSE, "DTLS: Info method=%s state=%s(%s), where=%d, ret=%d, r1=%d\n",
812-
method, SSL_state_string(dtls), SSL_state_string_long(dtls), where, r0, r1);
813-
}
814-
815-
}
816-
}
817-
818-
static void tls_info_callback(const SSL *ssl, int where, int ret) {
819-
const char *direction = "";
757+
static void openssl_info_callback(const SSL *ssl, int where, int ret) {
820758
const char *method = "undefined";
821-
if (where & SSL_CB_READ) {
822-
direction = "Received";
823-
} else if (where & SSL_CB_WRITE) {
824-
direction = "Sent";
825-
}
759+
TLSContext *ctx = (TLSContext*)SSL_get_ex_data(ssl, 0);
826760

827761
if (where & SSL_ST_CONNECT) {
828762
method = "SSL_connect";
829763
} else if (where & SSL_ST_ACCEPT)
830764
method = "SSL_accept";
831765

832766
if (where & SSL_CB_LOOP) {
833-
av_log(NULL, AV_LOG_DEBUG, "TLS: Info method=%s state=%s(%s), where=%d, ret=%d\n",
767+
av_log(ctx, AV_LOG_DEBUG, "Info method=%s state=%s(%s), where=%d, ret=%d\n",
834768
method, SSL_state_string(ssl), SSL_state_string_long(ssl), where, ret);
835769
} else if (where & SSL_CB_ALERT) {
836770
method = (where & SSL_CB_READ) ? "read":"write";
837-
av_log(NULL, AV_LOG_DEBUG, "TLS: Alert method=%s state=%s(%s), where=%d, ret=%d\n",
771+
av_log(ctx, AV_LOG_DEBUG, "Alert method=%s state=%s(%s), where=%d, ret=%d\n",
838772
method, SSL_state_string(ssl), SSL_state_string_long(ssl), where, ret);
839773
}
840774
}
@@ -1023,8 +957,7 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
1023957

1024958
/* Setup the callback for logging. */
1025959
SSL_set_ex_data(p->ssl, 0, p);
1026-
SSL_set_info_callback(p->ssl, openssl_dtls_on_info);
1027-
960+
SSL_set_info_callback(p->ssl, openssl_info_callback);
1028961
/**
1029962
* We have set the MTU to fragment the DTLS packet. It is important to note that the
1030963
* packet is split to ensure that each handshake packet is smaller than the MTU.
@@ -1115,7 +1048,6 @@ static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **op
11151048
ret = AVERROR(EIO);
11161049
goto fail;
11171050
}
1118-
SSL_CTX_set_info_callback(p->ctx, tls_info_callback);
11191051
SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
11201052
ret = openssl_init_ca_key_cert(h);
11211053
if (ret < 0) goto fail;
@@ -1129,6 +1061,8 @@ static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **op
11291061
ret = AVERROR(EIO);
11301062
goto fail;
11311063
}
1064+
SSL_set_ex_data(p->ssl, 0, p);
1065+
SSL_CTX_set_info_callback(p->ctx, openssl_info_callback);
11321066
init_bio_method(h);
11331067
if (!c->listen && !c->numerichost)
11341068
SSL_set_tlsext_host_name(p->ssl, c->host);

0 commit comments

Comments
 (0)