On Fri, 26 May 2017 13:56:13 +0200 Diego Biurrun <di...@biurrun.de> wrote:
> --- > TLS is not handled like other protocols. Instead the implementation details > of which crypto library is used get exposed to the user. Hiding those > details allows simplifying and refactoring some code and avoiding the > special-casing of TLS. > > This should get a slightly more elaborate log message before pushing. > > configure | 8 ++---- > libavformat/Makefile | 3 +- > libavformat/network.c | 20 -------------- > libavformat/protocols.c | 3 +- > libavformat/tls.c | 70 > +++++++++++++++++++++++++++++++++++++++++------ > libavformat/tls.h | 8 ------ > libavformat/tls_gnutls.c | 53 +++-------------------------------- > libavformat/tls_openssl.c | 53 +++-------------------------------- > libavformat/utils.c | 4 +++ > 9 files changed, 77 insertions(+), 145 deletions(-) > > diff --git a/configure b/configure > index d6c44cf..d17e9f7 100755 > --- a/configure > +++ b/configure > @@ -2468,12 +2468,8 @@ sctp_protocol_deps="struct_sctp_event_subscribe" > sctp_protocol_select="network" > srtp_protocol_select="rtp_protocol srtp" > tcp_protocol_select="network" > -tls_gnutls_protocol_deps="gnutls" > -tls_gnutls_protocol_select="tcp_protocol" > -tls_openssl_protocol_conflict="tls_gnutls_protocol" > -tls_openssl_protocol_deps="openssl" > -tls_openssl_protocol_select="tcp_protocol" > -tls_protocol_deps_any="tls_gnutls_protocol tls_openssl_protocol" > +tls_protocol_deps_any="gnutls openssl" > +tls_protocol_select="tcp_protocol" > udp_protocol_select="network" > unix_protocol_deps="sys_un_h" > unix_protocol_select="network" > diff --git a/libavformat/Makefile b/libavformat/Makefile > index 7b1df93..7e306ec 100644 > --- a/libavformat/Makefile > +++ b/libavformat/Makefile > @@ -408,8 +408,7 @@ OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o > OBJS-$(CONFIG_SCTP_PROTOCOL) += sctp.o > OBJS-$(CONFIG_SRTP_PROTOCOL) += srtpproto.o srtp.o > OBJS-$(CONFIG_TCP_PROTOCOL) += tcp.o > -OBJS-$(CONFIG_TLS_GNUTLS_PROTOCOL) += tls_gnutls.o tls.o > -OBJS-$(CONFIG_TLS_OPENSSL_PROTOCOL) += tls_openssl.o tls.o > +OBJS-$(CONFIG_TLS_PROTOCOL) += tls.o > OBJS-$(CONFIG_UDP_PROTOCOL) += udp.o > OBJS-$(CONFIG_UNIX_PROTOCOL) += unix.o > > diff --git a/libavformat/network.c b/libavformat/network.c > index 2c34b4a..978ff73 100644 > --- a/libavformat/network.c > +++ b/libavformat/network.c > @@ -25,26 +25,6 @@ > #include "libavcodec/internal.h" > #include "libavutil/mem.h" > > -void ff_tls_init(void) > -{ > -#if CONFIG_TLS_OPENSSL_PROTOCOL > - ff_openssl_init(); > -#endif > -#if CONFIG_TLS_GNUTLS_PROTOCOL > - ff_gnutls_init(); > -#endif > -} > - > -void ff_tls_deinit(void) > -{ > -#if CONFIG_TLS_OPENSSL_PROTOCOL > - ff_openssl_deinit(); > -#endif > -#if CONFIG_TLS_GNUTLS_PROTOCOL > - ff_gnutls_deinit(); > -#endif > -} > - > int ff_network_inited_globally; > > int ff_network_init(void) > diff --git a/libavformat/protocols.c b/libavformat/protocols.c > index d254540..8ea5c0e 100644 > --- a/libavformat/protocols.c > +++ b/libavformat/protocols.c > @@ -48,8 +48,7 @@ extern const URLProtocol ff_rtp_protocol; > extern const URLProtocol ff_sctp_protocol; > extern const URLProtocol ff_srtp_protocol; > extern const URLProtocol ff_tcp_protocol; > -extern const URLProtocol ff_tls_gnutls_protocol; > -extern const URLProtocol ff_tls_openssl_protocol; > +extern const URLProtocol ff_tls_protocol; > extern const URLProtocol ff_udp_protocol; > extern const URLProtocol ff_unix_protocol; > extern const URLProtocol ff_librtmp_protocol; > diff --git a/libavformat/tls.c b/libavformat/tls.c > index fab243e..afb174b 100644 > --- a/libavformat/tls.c > +++ b/libavformat/tls.c > @@ -19,15 +19,13 @@ > * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 > USA > */ > > -#include "avformat.h" > -#include "internal.h" > -#include "network.h" > -#include "os_support.h" > -#include "url.h" > -#include "tls.h" > -#include "libavutil/avstring.h" > -#include "libavutil/opt.h" > -#include "libavutil/parseutils.h" > +#include "config.h" > + > +#if CONFIG_GNUTLS > +#include "tls_gnutls.c" > +#else > +#include "tls_openssl.c" > +#endif > > int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char > *uri, AVDictionary **options) > { > @@ -78,3 +76,57 @@ int ff_tls_open_underlying(TLSShared *c, URLContext > *parent, const char *uri, AV > return ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE, > &parent->interrupt_callback, options, > parent->protocols, parent); > } > + > + > +static int tls_read(URLContext *h, uint8_t *buf, int size) > +{ > + TLSContext *c = h->priv_data; > +#if CONFIG_GNUTLS > + int ret = gnutls_record_recv(c->session, buf, size); > +#else > + int ret = SSL_read(c->ssl, buf, size); > +#endif > + if (ret > 0) > + return ret; > + if (ret == 0) > + return AVERROR_EOF; > + return print_tls_error(h, ret); > +} > + > +static int tls_write(URLContext *h, const uint8_t *buf, int size) > +{ > + TLSContext *c = h->priv_data; > +#if CONFIG_GNUTLS > + int ret = gnutls_record_send(c->session, buf, size); > +#else > + int ret = SSL_write(c->ssl, buf, size); > +#endif > + if (ret > 0) > + return ret; > + if (ret == 0) > + return AVERROR_EOF; > + return print_tls_error(h, ret); > +} > + > +static const AVOption options[] = { > + TLS_COMMON_OPTIONS(TLSContext, tls_shared), > + { NULL } > +}; > + > +static const AVClass tls_class = { > + .class_name = "tls", > + .item_name = av_default_item_name, > + .option = options, > + .version = LIBAVUTIL_VERSION_INT, > +}; > + > +const URLProtocol ff_tls_protocol = { > + .name = "tls", > + .url_open2 = tls_open, > + .url_read = tls_read, > + .url_write = tls_write, > + .url_close = tls_close, > + .priv_data_size = sizeof(TLSContext), > + .flags = URL_PROTOCOL_FLAG_NETWORK, > + .priv_data_class = &tls_class, > +}; > diff --git a/libavformat/tls.h b/libavformat/tls.h > index 22cb625..57adff9 100644 > --- a/libavformat/tls.h > +++ b/libavformat/tls.h > @@ -26,8 +26,6 @@ > #include "url.h" > #include "libavutil/opt.h" > > -#define CONFIG_TLS_PROTOCOL (CONFIG_TLS_GNUTLS_PROTOCOL | > CONFIG_TLS_OPENSSL_PROTOCOL) > - > typedef struct TLSShared { > char *ca_file; > int verify; > @@ -51,10 +49,4 @@ typedef struct TLSShared { > > int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char > *uri, AVDictionary **options); > > -void ff_gnutls_init(void); > -void ff_gnutls_deinit(void); > - > -void ff_openssl_init(void); > -void ff_openssl_deinit(void); > - > #endif /* AVFORMAT_TLS_H */ > diff --git a/libavformat/tls_gnutls.c b/libavformat/tls_gnutls.c > index f8a612a..82bc6d7 100644 > --- a/libavformat/tls_gnutls.c > +++ b/libavformat/tls_gnutls.c > @@ -43,14 +43,14 @@ typedef struct TLSContext { > int need_shutdown; > } TLSContext; > > -void ff_gnutls_init(void) > +void ff_tls_init(void) > { > avpriv_lock_avformat(); > gnutls_global_init(); > avpriv_unlock_avformat(); > } > > -void ff_gnutls_deinit(void) > +void ff_tls_deinit(void) > { > avpriv_lock_avformat(); > gnutls_global_deinit(); > @@ -84,7 +84,7 @@ static int tls_close(URLContext *h) > gnutls_certificate_free_credentials(c->cred); > if (c->tls_shared.tcp) > ffurl_close(c->tls_shared.tcp); > - ff_gnutls_deinit(); > + ff_tls_deinit(); > return 0; > } > > @@ -120,7 +120,7 @@ static int tls_open(URLContext *h, const char *uri, int > flags, AVDictionary **op > TLSShared *c = &p->tls_shared; > int ret; > > - ff_gnutls_init(); > + ff_tls_init(); > > if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0) > goto fail; > @@ -198,48 +198,3 @@ fail: > tls_close(h); > return ret; > } > - > -static int tls_read(URLContext *h, uint8_t *buf, int size) > -{ > - TLSContext *c = h->priv_data; > - int ret = gnutls_record_recv(c->session, buf, size); > - if (ret > 0) > - return ret; > - if (ret == 0) > - return AVERROR_EOF; > - return print_tls_error(h, ret); > -} > - > -static int tls_write(URLContext *h, const uint8_t *buf, int size) > -{ > - TLSContext *c = h->priv_data; > - int ret = gnutls_record_send(c->session, buf, size); > - if (ret > 0) > - return ret; > - if (ret == 0) > - return AVERROR_EOF; > - return print_tls_error(h, ret); > -} > - > -static const AVOption options[] = { > - TLS_COMMON_OPTIONS(TLSContext, tls_shared), > - { NULL } > -}; > - > -static const AVClass tls_class = { > - .class_name = "tls", > - .item_name = av_default_item_name, > - .option = options, > - .version = LIBAVUTIL_VERSION_INT, > -}; > - > -const URLProtocol ff_tls_gnutls_protocol = { > - .name = "tls", > - .url_open2 = tls_open, > - .url_read = tls_read, > - .url_write = tls_write, > - .url_close = tls_close, > - .priv_data_size = sizeof(TLSContext), > - .flags = URL_PROTOCOL_FLAG_NETWORK, > - .priv_data_class = &tls_class, > -}; > diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c > index 0abccf0..066c1f4 100644 > --- a/libavformat/tls_openssl.c > +++ b/libavformat/tls_openssl.c > @@ -66,7 +66,7 @@ static unsigned long openssl_thread_id(void) > #endif > #endif > > -void ff_openssl_init(void) > +void ff_tls_init(void) > { > avpriv_lock_avformat(); > if (!openssl_init) { > @@ -89,7 +89,7 @@ void ff_openssl_init(void) > avpriv_unlock_avformat(); > } > > -void ff_openssl_deinit(void) > +void ff_tls_deinit(void) > { > avpriv_lock_avformat(); > openssl_init--; > @@ -128,7 +128,7 @@ static int tls_close(URLContext *h) > if (c->url_bio_method) > BIO_meth_free(c->url_bio_method); > #endif > - ff_openssl_deinit(); > + ff_tls_deinit(); > return 0; > } > > @@ -216,7 +216,7 @@ static int tls_open(URLContext *h, const char *uri, int > flags, AVDictionary **op > BIO *bio; > int ret; > > - ff_openssl_init(); > + ff_tls_init(); > > if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0) > goto fail; > @@ -288,48 +288,3 @@ fail: > tls_close(h); > return ret; > } > - > -static int tls_read(URLContext *h, uint8_t *buf, int size) > -{ > - TLSContext *c = h->priv_data; > - int ret = SSL_read(c->ssl, buf, size); > - if (ret > 0) > - return ret; > - if (ret == 0) > - return AVERROR_EOF; > - return print_tls_error(h, ret); > -} > - > -static int tls_write(URLContext *h, const uint8_t *buf, int size) > -{ > - TLSContext *c = h->priv_data; > - int ret = SSL_write(c->ssl, buf, size); > - if (ret > 0) > - return ret; > - if (ret == 0) > - return AVERROR_EOF; > - return print_tls_error(h, ret); > -} > - > -static const AVOption options[] = { > - TLS_COMMON_OPTIONS(TLSContext, tls_shared), > - { NULL } > -}; > - > -static const AVClass tls_class = { > - .class_name = "tls", > - .item_name = av_default_item_name, > - .option = options, > - .version = LIBAVUTIL_VERSION_INT, > -}; > - > -const URLProtocol ff_tls_openssl_protocol = { > - .name = "tls", > - .url_open2 = tls_open, > - .url_read = tls_read, > - .url_write = tls_write, > - .url_close = tls_close, > - .priv_data_size = sizeof(TLSContext), > - .flags = URL_PROTOCOL_FLAG_NETWORK, > - .priv_data_class = &tls_class, > -}; > diff --git a/libavformat/utils.c b/libavformat/utils.c > index eaba473..fd85a02 100644 > --- a/libavformat/utils.c > +++ b/libavformat/utils.c > @@ -3211,8 +3211,10 @@ int avformat_network_init(void) > ff_network_inited_globally = 1; > if ((ret = ff_network_init()) < 0) > return ret; > +#if CONFIG_TLS_PROTOCOL > ff_tls_init(); > #endif > +#endif > return 0; > } > > @@ -3220,8 +3222,10 @@ int avformat_network_deinit(void) > { > #if CONFIG_NETWORK > ff_network_close(); > +#if CONFIG_TLS_PROTOCOL > ff_tls_deinit(); > #endif > +#endif > return 0; > } > Are you serious? This reverts the change wbs and me have done to _disentangle_ those protocols. _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel