1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avformat/tls: make passing an external socket universal

This commit is contained in:
Timo Rothenpieler
2025-07-01 18:07:24 +02:00
parent 2604c86c1f
commit 6af099522e
3 changed files with 16 additions and 11 deletions

View File

@ -57,15 +57,14 @@ typedef struct TLSShared {
char underlying_host[200]; char underlying_host[200];
int numerichost; int numerichost;
int external_sock;
URLContext *udp;
URLContext *tcp; URLContext *tcp;
int is_dtls; int is_dtls;
enum DTLSState state; enum DTLSState state;
int use_external_udp;
URLContext *udp;
/* The certificate and private key content used for DTLS handshake */ /* The certificate and private key content used for DTLS handshake */
char* cert_buf; char* cert_buf;
char* key_buf; char* key_buf;
@ -90,7 +89,7 @@ typedef struct TLSShared {
#define TLS_COMMON_OPTIONS(pstruct, options_field) \ #define TLS_COMMON_OPTIONS(pstruct, options_field) \
{"listen", "Listen for incoming connections", offsetof(pstruct, options_field . listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \ {"listen", "Listen for incoming connections", offsetof(pstruct, options_field . listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
{"http_proxy", "Set proxy to tunnel through", offsetof(pstruct, options_field . http_proxy), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \ {"http_proxy", "Set proxy to tunnel through", offsetof(pstruct, options_field . http_proxy), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
{"use_external_udp", "Use external UDP from muxer or demuxer", offsetof(pstruct, options_field . use_external_udp), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 1, .flags = TLS_OPTFL }, \ {"external_sock", "Use external socket", offsetof(pstruct, options_field . external_sock), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = TLS_OPTFL }, \
{"mtu", "Maximum Transmission Unit", offsetof(pstruct, options_field . mtu), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = TLS_OPTFL}, \ {"mtu", "Maximum Transmission Unit", offsetof(pstruct, options_field . mtu), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = TLS_OPTFL}, \
{"cert_pem", "Certificate PEM string", offsetof(pstruct, options_field . cert_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \ {"cert_pem", "Certificate PEM string", offsetof(pstruct, options_field . cert_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
{"key_pem", "Private key PEM string", offsetof(pstruct, options_field . key_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \ {"key_pem", "Private key PEM string", offsetof(pstruct, options_field . key_buf), AV_OPT_TYPE_STRING, .flags = TLS_OPTFL }, \
@ -100,7 +99,7 @@ int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AV
int ff_url_read_all(const char *url, AVBPrint *bp); int ff_url_read_all(const char *url, AVBPrint *bp);
int ff_dtls_set_udp(URLContext *h, URLContext *udp); int ff_tls_set_external_socket(URLContext *h, URLContext *sock);
int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz); int ff_dtls_export_materials(URLContext *h, char *dtls_srtp_materials, size_t materials_sz);

View File

@ -486,10 +486,16 @@ static const char* openssl_get_error(TLSContext *ctx)
return ctx->error_message; return ctx->error_message;
} }
int ff_dtls_set_udp(URLContext *h, URLContext *udp) int ff_tls_set_external_socket(URLContext *h, URLContext *sock)
{ {
TLSContext *c = h->priv_data; TLSContext *c = h->priv_data;
c->tls_shared.udp = udp; TLSShared *s = &c->tls_shared;
if (s->is_dtls)
c->tls_shared.udp = sock;
else
c->tls_shared.tcp = sock;
return 0; return 0;
} }
@ -829,7 +835,7 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
if (ret < 0) if (ret < 0)
goto fail; goto fail;
if (p->tls_shared.use_external_udp != 1) { if (p->tls_shared.external_sock != 1) {
if ((ret = ff_tls_open_underlying(&p->tls_shared, h, url, options)) < 0) { if ((ret = ff_tls_open_underlying(&p->tls_shared, h, url, options)) < 0) {
av_log(p, AV_LOG_ERROR, "Failed to connect %s\n", url); av_log(p, AV_LOG_ERROR, "Failed to connect %s\n", url);
return ret; return ret;
@ -850,7 +856,7 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
* *
* The SSL_do_handshake can't be called if DTLS hasn't prepare for udp. * The SSL_do_handshake can't be called if DTLS hasn't prepare for udp.
*/ */
if (p->tls_shared.use_external_udp != 1) { if (p->tls_shared.external_sock != 1) {
ret = dtls_handshake(h); ret = dtls_handshake(h);
// Fatal SSL error, for example, no available suite when peer is DTLS 1.0 while we are DTLS 1.2. // Fatal SSL error, for example, no available suite when peer is DTLS 1.0 while we are DTLS 1.2.
if (ret < 0) { if (ret < 0) {

View File

@ -387,7 +387,7 @@ static av_cold int dtls_initialize(AVFormatContext *s)
{ {
WHIPContext *whip = s->priv_data; WHIPContext *whip = s->priv_data;
/* reuse the udp created by whip */ /* reuse the udp created by whip */
ff_dtls_set_udp(whip->dtls_uc, whip->udp); ff_tls_set_external_socket(whip->dtls_uc, whip->udp);
/* Make the socket non-blocking */ /* Make the socket non-blocking */
ff_socket_nonblock(ffurl_get_file_handle(whip->dtls_uc), 1); ff_socket_nonblock(ffurl_get_file_handle(whip->dtls_uc), 1);
@ -1302,7 +1302,7 @@ next_packet:
av_dict_set(&opts, "key_file", whip->key_file, 0); av_dict_set(&opts, "key_file", whip->key_file, 0);
} else } else
av_dict_set(&opts, "key_pem", whip->key_buf, 0); av_dict_set(&opts, "key_pem", whip->key_buf, 0);
av_dict_set_int(&opts, "use_external_udp", 1, 0); av_dict_set_int(&opts, "external_sock", 1, 0);
av_dict_set_int(&opts, "listen", 1, 0); av_dict_set_int(&opts, "listen", 1, 0);
/* If got the first binding response, start DTLS handshake. */ /* If got the first binding response, start DTLS handshake. */
ret = ffurl_open_whitelist(&whip->dtls_uc, buf, AVIO_FLAG_READ_WRITE, &s->interrupt_callback, ret = ffurl_open_whitelist(&whip->dtls_uc, buf, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,