Merge commit 'd15eec4d6bdfa3bd4c4b5b7dd2dbd699ba253d02'
authorMichael Niedermayer <michaelni@gmx.at>
Fri, 22 May 2015 00:28:22 +0000 (02:28 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Fri, 22 May 2015 00:28:27 +0000 (02:28 +0200)
* commit 'd15eec4d6bdfa3bd4c4b5b7dd2dbd699ba253d02':
  tls: Use custom IO to read from the URLContext

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavformat/tls.c

  #if CONFIG_GNUTLS
  #include <gnutls/gnutls.h>
  #include <gnutls/x509.h>
 -#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_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); \
 +        if ((c)->session) \
 +            gnutls_deinit((c)->session); \
 +        if ((c)->cred) \
 +            gnutls_certificate_free_credentials((c)->cred); \
      } while (0)
+ static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
+                                void *buf, size_t len)
+ {
+     URLContext *h = (URLContext*) transport;
+     int ret = ffurl_read(h, buf, len);
+     if (ret >= 0)
+         return ret;
+     if (ret == AVERROR(EAGAIN))
+         errno = EAGAIN;
+     else
+         errno = EIO;
+     return -1;
+ }
+ static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
+                                const void *buf, size_t len)
+ {
+     URLContext *h = (URLContext*) transport;
+     int ret = ffurl_write(h, buf, len);
+     if (ret >= 0)
+         return ret;
+     if (ret == AVERROR(EAGAIN))
+         errno = EAGAIN;
+     else
+         errno = EIO;
+     return -1;
+ }
  #elif CONFIG_OPENSSL
  #include <openssl/bio.h>
  #include <openssl/ssl.h>
  #include <openssl/err.h>
 -#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_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); \
 +        if ((c)->ssl) \
 +            SSL_free((c)->ssl); \
 +        if ((c)->ctx) \
 +            SSL_CTX_free((c)->ctx); \
      } while (0)
+ static int url_bio_create(BIO *b)
+ {
+     b->init = 1;
+     b->ptr = NULL;
+     b->flags = 0;
+     return 1;
+ }
+ static int url_bio_destroy(BIO *b)
+ {
+     return 1;
+ }
+ static int url_bio_bread(BIO *b, char *buf, int len)
+ {
+     URLContext *h = b->ptr;
+     int ret = ffurl_read(h, buf, len);
+     if (ret >= 0)
+         return ret;
+     BIO_clear_retry_flags(b);
+     if (ret == AVERROR(EAGAIN))
+         BIO_set_retry_read(b);
+     return -1;
+ }
+ static int url_bio_bwrite(BIO *b, const char *buf, int len)
+ {
+     URLContext *h = b->ptr;
+     int ret = ffurl_write(h, buf, len);
+     if (ret >= 0)
+         return ret;
+     BIO_clear_retry_flags(b);
+     if (ret == AVERROR(EAGAIN))
+         BIO_set_retry_write(b);
+     return -1;
+ }
+ static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
+ {
+     if (cmd == BIO_CTRL_FLUSH) {
+         BIO_clear_retry_flags(b);
+         return 1;
+     }
+     return 0;
+ }
+ static int url_bio_bputs(BIO *b, const char *str)
+ {
+     return url_bio_bwrite(b, str, strlen(str));
+ }
+ static BIO_METHOD url_bio_method = {
+     .type = BIO_TYPE_SOURCE_SINK,
+     .name = "urlprotocol bio",
+     .bwrite = url_bio_bwrite,
+     .bread = url_bio_bread,
+     .bputs = url_bio_bputs,
+     .bgets = NULL,
+     .ctrl = url_bio_ctrl,
+     .create = url_bio_create,
+     .destroy = url_bio_destroy,
+ };
  #endif
 -#include "network.h"
 -#include "os_support.h"
 -#include "internal.h"
  #if HAVE_POLL_H
  #include <poll.h>
  #endif
@@@ -174,9 -239,11 +265,12 @@@ static int tls_open(URLContext *h, cons
      struct addrinfo hints = { 0 }, *ai = NULL;
      const char *proxy_path;
      int use_proxy;
+ #if CONFIG_OPENSSL
+     BIO *bio;
+ #endif
  
 -    ff_tls_init();
 +    if ((ret = ff_tls_init()) < 0)
 +        return ret;
  
      if (c->listen)
          snprintf(opts, sizeof(opts), "?listen=1");
              ret = AVERROR(EIO);
              goto fail;
          }
 -    }
 +    } else if (c->cert_file || c->key_file)
 +        av_log(h, AV_LOG_ERROR, "cert and key required\n");
      gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
-     gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
-                                          (intptr_t) c->fd);
+     c->tcp->flags |= AVIO_FLAG_NONBLOCK;
+     gnutls_transport_set_pull_function(c->session, gnutls_url_pull);
+     gnutls_transport_set_push_function(c->session, gnutls_url_push);
+     gnutls_transport_set_ptr(c->session, c->tcp);
      gnutls_priority_set_direct(c->session, "NORMAL", NULL);
      while (1) {
          ret = gnutls_handshake(c->session);