Commit f4a73140 authored by Francois Cartegnie's avatar Francois Cartegnie

demux: adaptative: add tls

parent 0e86ff50
...@@ -79,10 +79,12 @@ bool HTTPConnectionManager::connectChunk(Chunk *chunk) ...@@ -79,10 +79,12 @@ bool HTTPConnectionManager::connectChunk(Chunk *chunk)
HTTPConnection *conn = getConnectionForHost(chunk->getHostname()); HTTPConnection *conn = getConnectionForHost(chunk->getHostname());
if(!conn) 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) if(!socket)
return false; 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) if(!conn)
{ {
delete socket; delete socket;
......
...@@ -96,3 +96,108 @@ bool Socket::send(vlc_object_t *stream, const void *buf, size_t size) ...@@ -96,3 +96,108 @@ bool Socket::send(vlc_object_t *stream, const void *buf, size_t size)
return true; 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();
}
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#endif #endif
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_tls.h>
#include <string> #include <string>
namespace adaptative namespace adaptative
...@@ -47,6 +48,22 @@ namespace adaptative ...@@ -47,6 +48,22 @@ namespace adaptative
int netfd; 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;
};
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment