[vlc-commits] demux: adaptative: add tls

Francois Cartegnie git at videolan.org
Tue May 12 18:28:58 CEST 2015


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Tue May 12 00:20:31 2015 +0200| [f4a731405440c0ef1ccc662747299ac15df8ba42] | committer: Francois Cartegnie

demux: adaptative: add tls

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f4a731405440c0ef1ccc662747299ac15df8ba42
---

 .../adaptative/http/HTTPConnectionManager.cpp      |    6 +-
 modules/demux/adaptative/http/Sockets.cpp          |  105 ++++++++++++++++++++
 modules/demux/adaptative/http/Sockets.hpp          |   17 ++++
 3 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/modules/demux/adaptative/http/HTTPConnectionManager.cpp b/modules/demux/adaptative/http/HTTPConnectionManager.cpp
index 945311f..aeabaac 100644
--- a/modules/demux/adaptative/http/HTTPConnectionManager.cpp
+++ b/modules/demux/adaptative/http/HTTPConnectionManager.cpp
@@ -79,10 +79,12 @@ bool HTTPConnectionManager::connectChunk(Chunk *chunk)
     HTTPConnection *conn = getConnectionForHost(chunk->getHostname());
     if(!conn)
     {
-        Socket *socket = new (std::nothrow) Socket();
+        const bool tls = (chunk->getScheme() == "https");
+        Socket *socket = tls ? new (std::nothrow) TLSSocket(): new (std::nothrow) Socket();
         if(!socket)
             return false;
-        conn = new (std::nothrow) HTTPConnection(stream, socket, chunk, true);
+        /* disable pipelined tls until we have ticket/resume session support */
+        conn = new (std::nothrow) HTTPConnection(stream, socket, chunk, !tls);
         if(!conn)
         {
             delete socket;
diff --git a/modules/demux/adaptative/http/Sockets.cpp b/modules/demux/adaptative/http/Sockets.cpp
index 65de5db..9d049a0 100644
--- a/modules/demux/adaptative/http/Sockets.cpp
+++ b/modules/demux/adaptative/http/Sockets.cpp
@@ -96,3 +96,108 @@ bool Socket::send(vlc_object_t *stream, const void *buf, size_t size)
     return true;
 }
 
+TLSSocket::TLSSocket() : Socket()
+{
+    creds = NULL;
+    tls = NULL;
+}
+
+TLSSocket::~TLSSocket()
+{
+    disconnect();
+}
+
+bool TLSSocket::connect(vlc_object_t *stream, const std::string &hostname, int port)
+{
+    disconnect();
+    if(!Socket::connect(stream, hostname, port))
+        return false;
+
+    creds = vlc_tls_ClientCreate(stream);
+    if(!creds)
+    {
+        disconnect();
+        return false;
+    }
+
+    tls = vlc_tls_ClientSessionCreate(creds, netfd, hostname.c_str(), "https", NULL, NULL);
+    if(!tls)
+    {
+        disconnect();
+        return false;
+    }
+
+    return true;
+}
+
+bool TLSSocket::connected() const
+{
+    return Socket::connected() && tls;
+}
+
+ssize_t TLSSocket::read(vlc_object_t *, void *p_buffer, size_t len, bool)
+{
+    ssize_t size;
+    size_t totalread = 0;
+    do
+    {
+        size = tls_Recv(tls, (uint8_t*)p_buffer + totalread, len - totalread); /* only returns partial chunks */
+        if(size >= 0)
+        {
+            totalread += (size_t) size;
+        }
+        else if(errno != EINTR && errno!=EAGAIN)
+        {
+            break;
+        }
+    } while ( totalread < len );
+    return totalread;
+}
+
+std::string TLSSocket::readline(vlc_object_t *stream)
+{
+    std::string ret;
+    ret.reserve(256);
+    char c[2] = {0,0};
+    ssize_t size = TLSSocket::read(stream, c, 1, true);
+
+    while(size > 0)
+    {
+        ret.append( &c[0] );
+        if(c[0] == '\n')
+            break;
+
+        size = TLSSocket::read(stream, c, 1, true);
+    }
+
+    return ret;
+}
+
+bool TLSSocket::send(vlc_object_t *stream, const void *buf, size_t size)
+{
+    if (!connected())
+        return false;
+
+    if (size == 0)
+        return true;
+
+    ssize_t ret = tls_Send(tls, buf, size);
+    if (ret <= 0)
+        return false;
+
+    if ( (size_t)ret < size )
+        send( stream, ((uint8_t*)buf) + ret, size - ret );
+
+    return true;
+}
+
+void TLSSocket::disconnect()
+{
+    if(tls)
+        vlc_tls_SessionDelete(tls);
+    if(creds)
+        vlc_tls_Delete(creds);
+    tls = NULL;
+    creds = NULL;
+    Socket::disconnect();
+}
diff --git a/modules/demux/adaptative/http/Sockets.hpp b/modules/demux/adaptative/http/Sockets.hpp
index 686f5b3..e8161ae 100644
--- a/modules/demux/adaptative/http/Sockets.hpp
+++ b/modules/demux/adaptative/http/Sockets.hpp
@@ -25,6 +25,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_tls.h>
 #include <string>
 
 namespace adaptative
@@ -47,6 +48,22 @@ namespace adaptative
                 int netfd;
         };
 
+        class TLSSocket : public Socket
+        {
+            public:
+                TLSSocket();
+                virtual ~TLSSocket();
+                virtual bool    connect     (vlc_object_t *, const std::string&, int port = 443);
+                virtual bool    connected   () const;
+                virtual bool    send        (vlc_object_t *, const void *buf, size_t size);
+                virtual ssize_t read        (vlc_object_t *, void *p_buffer, size_t len, bool);
+                virtual std::string readline(vlc_object_t *);
+                virtual void    disconnect  ();
+
+            private:
+                vlc_tls_creds_t *creds;
+                vlc_tls_t *tls;
+        };
     }
 }
 



More information about the vlc-commits mailing list