[FFmpeg-devel] [PATCH v2 1/4] avformat/tls_openssl: add record trace function

Steven Liu lingjiujianke at gmail.com
Fri Jul 11 18:03:44 EEST 2025


Jack Lau <jacklau1222gm-at-gmail.com at ffmpeg.org> 于2025年7月11日周五 21:21写道:
>
> Signed-off-by: Jack Lau <jacklau1222 at qq.com>
> ---
>  libavformat/tls_openssl.c | 51 +++++++++++++++++++++++++++++++++++++--
>  1 file changed, 49 insertions(+), 2 deletions(-)
>
> diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c
> index 2a01fb387d..8639ac9758 100644
> --- a/libavformat/tls_openssl.c
> +++ b/libavformat/tls_openssl.c
> @@ -20,6 +20,7 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>
> +#include "libavutil/intreadwrite.h"
>  #include "libavutil/mem.h"
>  #include "network.h"
>  #include "os_support.h"
> @@ -559,6 +560,48 @@ static int tls_close(URLContext *h)
>      return 0;
>  }
>
> +/*
> + * Trace a single TLS/DTLS record.
> + *
> + * See RFC 5246 Section 6.2.1, RFC 6347 Section 4.1
> + *
> + * @param data     Raw record (network byte‑order).
> + * @param length   Size of @data in bytes.
> + * @param incoming Non‑zero when the packet was received, zero when sent.
> + */
> +static void openssl_state_trace(uint8_t *data, int length, int incoming)
> +{
> +    uint8_t  content_type   = 0;  /* TLS/DTLS ContentType       */
> +    uint16_t record_length  = 0;  /* Length field from header   */
> +    uint8_t  handshake_type = 0;  /* First byte of Handshake msg */
> +    int is_dtls = 0;
> +
> +    /* ContentType is always the very first byte */
> +    if (length >= 1)
> +        content_type = AV_RB8(&data[0]);
> +    if (length >= 3 && data[1] == DTLS1_VERSION_MAJOR)
> +        is_dtls = 1;
> +    /* TLS header is 5 bytes, DTLS header is 13 bytes */
> +    if (length >= 13 && is_dtls)
> +        record_length = AV_RB16(&data[11]);
> +    else if (length >= 5 && !is_dtls)
> +        record_length = AV_RB16(&data[3]);
> +    /*
> +     * HandshakeType values (TLS 1.0–1.2, DTLS 1.0/1.2)
> +     * See RFC 5246 Section 7.4, RFC 6347 Section 4.2
> +     *
> +     * Only present when ContentType == handshake(22)
> +     */
> +    if (content_type == 22) {
use #define or enum should understand easyer than only number 22.

#define CONTENT_TYPE_CHANGE_CIPHER_SPEC 20
#define CONTENT_TYPE_CHANGE_ALERT 21
#define CONTENT_TYPE_HANDSHAKE 22
#define CONTENT_TYPE_APPLICATION_DATA 23

or


enum {
          content_type_change_cipher_spec = 20,
          contentt_ype_alert = 21,
          content_type_handshake = 22,
          content_type_application_data = 23,
          content_type_others = 255
      } ContentType;

perhapes enum better than define?

> +        int hs_off = is_dtls ? 13 : 5;
> +        if (length > hs_off)
> +            handshake_type = AV_RB8(&data[hs_off]);
> +    }
> +
> +    av_log(NULL, AV_LOG_TRACE ,"TLS: Trace %s, len=%u, cnt=%u, size=%u, hs=%u\n",
> +        (incoming? "RECV":"SEND"), length, content_type, record_length, handshake_type);
> +}
> +
>  static int url_bio_create(BIO *b)
>  {
>      BIO_set_init(b, 1);
> @@ -576,8 +619,10 @@ static int url_bio_bread(BIO *b, char *buf, int len)
>  {
>      TLSContext *c = BIO_get_data(b);
>      int ret = ffurl_read(c->tls_shared.is_dtls ? c->tls_shared.udp : c->tls_shared.tcp, buf, len);
> -    if (ret >= 0)
> +    if (ret >= 0) {
> +        openssl_state_trace((uint8_t*)buf, ret, 1);
>          return ret;
> +    }
>      BIO_clear_retry_flags(b);
>      if (ret == AVERROR_EXIT)
>          return 0;
> @@ -592,8 +637,10 @@ static int url_bio_bwrite(BIO *b, const char *buf, int len)
>  {
>      TLSContext *c = BIO_get_data(b);
>      int ret = ffurl_write(c->tls_shared.is_dtls ? c->tls_shared.udp : c->tls_shared.tcp, buf, len);
> -    if (ret >= 0)
> +    if (ret >= 0) {
> +        openssl_state_trace((uint8_t*)buf, ret, 0);
>          return ret;
> +    }
>      BIO_clear_retry_flags(b);
>      if (ret == AVERROR_EXIT)
>          return 0;
> --
> 2.49.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list