X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Ftls.c;h=eee3fc3d7de7ab8c3da278ba95092e7e560460a1;hb=5f2c8315b3c1b28da0386fddb118ad6a0ed77a0c;hp=b2ec147adb50e98ee415697c1f9656a3ef24afc5;hpb=6f1b7b39449c4cd58e37d831d5d97bfd25eb26f0;p=ffmpeg diff --git a/libavformat/tls.c b/libavformat/tls.c index b2ec147adb5..eee3fc3d7de 100644 --- a/libavformat/tls.c +++ b/libavformat/tls.c @@ -20,215 +20,61 @@ */ #include "avformat.h" -#include "url.h" -#include "libavutil/avstring.h" -#if CONFIG_GNUTLS -#include -#define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size) -#define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size) -#define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR) -#define TLS_free(c) do { \ - if (c->session) \ - gnutls_deinit(c->session); \ - if (c->cred) \ - gnutls_certificate_free_credentials(c->cred); \ - } while (0) -#elif CONFIG_OPENSSL -#include -#include -#include -#define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size) -#define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size) -#define TLS_shutdown(c) SSL_shutdown(c->ssl) -#define TLS_free(c) do { \ - if (c->ssl) \ - SSL_free(c->ssl); \ - if (c->ctx) \ - SSL_CTX_free(c->ctx); \ - } while (0) -#endif +#include "internal.h" #include "network.h" #include "os_support.h" -#include "internal.h" -#if HAVE_POLL_H -#include -#endif - -typedef struct { - const AVClass *class; - URLContext *tcp; -#if CONFIG_GNUTLS - gnutls_session_t session; - gnutls_certificate_credentials_t cred; -#elif CONFIG_OPENSSL - SSL_CTX *ctx; - SSL *ssl; -#endif - int fd; -} TLSContext; - -static int do_tls_poll(URLContext *h, int ret) -{ - TLSContext *c = h->priv_data; - struct pollfd p = { c->fd, 0, 0 }; -#if CONFIG_GNUTLS - if (ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED) { - av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret)); - return AVERROR(EIO); - } - if (gnutls_record_get_direction(c->session)) - p.events = POLLOUT; - else - p.events = POLLIN; -#elif CONFIG_OPENSSL - ret = SSL_get_error(c->ssl, ret); - if (ret == SSL_ERROR_WANT_READ) { - p.events = POLLIN; - } else if (ret == SSL_ERROR_WANT_WRITE) { - p.events = POLLOUT; - } else { - av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL)); - return AVERROR(EIO); - } -#endif - if (h->flags & AVIO_FLAG_NONBLOCK) - return AVERROR(EAGAIN); - while (1) { - int n = poll(&p, 1, 100); - if (n > 0) - break; - if (ff_check_interrupt(&h->interrupt_callback)) - return AVERROR(EINTR); - } - return 0; -} +#include "url.h" +#include "tls.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" -static int tls_open(URLContext *h, const char *uri, int flags) +int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options) { - TLSContext *c = h->priv_data; - int ret; int port; - char buf[200], host[200]; - int numerichost = 0; + const char *p; + char buf[200], opts[50] = ""; struct addrinfo hints = { 0 }, *ai = NULL; + const char *proxy_path; + int use_proxy; - ff_tls_init(); + if (c->listen) + snprintf(opts, sizeof(opts), "?listen=1"); - av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri); - ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL); + av_url_split(NULL, 0, NULL, 0, c->host, sizeof(c->host), &port, NULL, 0, uri); - hints.ai_flags = AI_NUMERICHOST; - if (!getaddrinfo(host, NULL, &hints, &ai)) { - numerichost = 1; - freeaddrinfo(ai); + p = strchr(uri, '?'); + + if (!p) { + p = opts; + } else { + if (av_find_info_tag(opts, sizeof(opts), "listen", p)) + c->listen = 1; } - ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE, &h->interrupt_callback); - if (ret) - goto fail; - c->fd = ffurl_get_file_handle(c->tcp); + ff_url_join(buf, sizeof(buf), "tcp", NULL, c->host, port, "%s", p); -#if CONFIG_GNUTLS - gnutls_init(&c->session, GNUTLS_CLIENT); - if (!numerichost) - gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host)); - gnutls_certificate_allocate_credentials(&c->cred); - gnutls_certificate_set_verify_flags(c->cred, 0); - gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred); - gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t) - (intptr_t) c->fd); - gnutls_priority_set_direct(c->session, "NORMAL", NULL); - while (1) { - ret = gnutls_handshake(c->session); - if (ret == 0) - break; - if ((ret = do_tls_poll(h, ret)) < 0) - goto fail; - } -#elif CONFIG_OPENSSL - c->ctx = SSL_CTX_new(SSLv3_client_method()); - if (!c->ctx) { - av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL)); - ret = AVERROR(EIO); - goto fail; - } - c->ssl = SSL_new(c->ctx); - if (!c->ssl) { - av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL)); - ret = AVERROR(EIO); - goto fail; - } - SSL_set_fd(c->ssl, c->fd); - if (!numerichost) - SSL_set_tlsext_host_name(c->ssl, host); - while (1) { - ret = SSL_connect(c->ssl); - if (ret > 0) - break; - if (ret == 0) { - av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n"); - ret = AVERROR(EIO); - goto fail; - } - if ((ret = do_tls_poll(h, ret)) < 0) - goto fail; + hints.ai_flags = AI_NUMERICHOST; + if (!getaddrinfo(c->host, NULL, &hints, &ai)) { + c->numerichost = 1; + freeaddrinfo(ai); } -#endif - return 0; -fail: - TLS_free(c); - if (c->tcp) - ffurl_close(c->tcp); - ff_tls_deinit(); - return ret; -} -static int tls_read(URLContext *h, uint8_t *buf, int size) -{ - TLSContext *c = h->priv_data; - while (1) { - int ret = TLS_read(c, buf, size); - if (ret > 0) - return ret; - if (ret == 0) - return AVERROR(EIO); - if ((ret = do_tls_poll(h, ret)) < 0) - return ret; - } - return 0; -} + proxy_path = getenv("http_proxy"); + use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->host) && + proxy_path && av_strstart(proxy_path, "http://", NULL); -static int tls_write(URLContext *h, const uint8_t *buf, int size) -{ - TLSContext *c = h->priv_data; - while (1) { - int ret = TLS_write(c, buf, size); - if (ret > 0) - return ret; - if (ret == 0) - return AVERROR(EIO); - if ((ret = do_tls_poll(h, ret)) < 0) - return ret; + if (use_proxy) { + char proxy_host[200], proxy_auth[200], dest[200]; + int proxy_port; + av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth), + proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0, + proxy_path); + ff_url_join(dest, sizeof(dest), NULL, NULL, c->host, port, NULL); + ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host, + proxy_port, "/%s", dest); } - return 0; -} -static int tls_close(URLContext *h) -{ - TLSContext *c = h->priv_data; - TLS_shutdown(c); - TLS_free(c); - ffurl_close(c->tcp); - ff_tls_deinit(); - return 0; + return ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE, + &parent->interrupt_callback, options); } - -URLProtocol ff_tls_protocol = { - .name = "tls", - .url_open = tls_open, - .url_read = tls_read, - .url_write = tls_write, - .url_seek = NULL, - .url_close = tls_close, - .priv_data_size = sizeof(TLSContext), -};