vlc | branch: master | Francois Cartegnie <[email protected]> | Wed Feb 21 23:18:05 2018 +0100| [55fa460795d4f066c35a4331c40fbe9f12315cbc] | committer: Francois Cartegnie
demux: adaptive: remove old TLS api and network primitives fixes old api leak > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=55fa460795d4f066c35a4331c40fbe9f12315cbc --- modules/demux/Makefile.am | 4 +- modules/demux/adaptive/http/ConnectionParams.hpp | 2 +- modules/demux/adaptive/http/HTTPConnection.cpp | 41 +++-- modules/demux/adaptive/http/HTTPConnection.hpp | 6 +- .../demux/adaptive/http/HTTPConnectionManager.cpp | 2 +- modules/demux/adaptive/http/Sockets.cpp | 178 --------------------- modules/demux/adaptive/http/Sockets.hpp | 72 --------- modules/demux/adaptive/http/Transport.cpp | 113 +++++++++++++ modules/demux/adaptive/http/Transport.hpp | 51 ++++++ 9 files changed, 191 insertions(+), 278 deletions(-) diff --git a/modules/demux/Makefile.am b/modules/demux/Makefile.am index e6006ae0a4..dba3c4c6b6 100644 --- a/modules/demux/Makefile.am +++ b/modules/demux/Makefile.am @@ -338,8 +338,8 @@ libadaptive_plugin_la_SOURCES = \ demux/adaptive/http/HTTPConnection.hpp \ demux/adaptive/http/HTTPConnectionManager.cpp \ demux/adaptive/http/HTTPConnectionManager.h \ - demux/adaptive/http/Sockets.hpp \ - demux/adaptive/http/Sockets.cpp \ + demux/adaptive/http/Transport.hpp \ + demux/adaptive/http/Transport.cpp \ demux/adaptive/plumbing/CommandsQueue.cpp \ demux/adaptive/plumbing/CommandsQueue.hpp \ demux/adaptive/plumbing/Demuxer.cpp \ diff --git a/modules/demux/adaptive/http/ConnectionParams.hpp b/modules/demux/adaptive/http/ConnectionParams.hpp index 040d7f6e6b..9381e396b7 100644 --- a/modules/demux/adaptive/http/ConnectionParams.hpp +++ b/modules/demux/adaptive/http/ConnectionParams.hpp @@ -27,7 +27,7 @@ namespace adaptive { namespace http { - class Socket; + class Transport; class ConnectionParams { diff --git a/modules/demux/adaptive/http/HTTPConnection.cpp b/modules/demux/adaptive/http/HTTPConnection.cpp index b24eb87212..0ed1f98e57 100644 --- a/modules/demux/adaptive/http/HTTPConnection.cpp +++ b/modules/demux/adaptive/http/HTTPConnection.cpp @@ -24,7 +24,7 @@ #include "HTTPConnection.hpp" #include "ConnectionParams.hpp" #include "AuthStorage.hpp" -#include "Sockets.hpp" +#include "Transport.hpp" #include <cstdio> #include <sstream> @@ -65,10 +65,10 @@ const std::string & AbstractConnection::getContentType() const } HTTPConnection::HTTPConnection(vlc_object_t *p_object_, AuthStorage *auth, - Socket *socket_, const ConnectionParams &proxy, bool persistent) + Transport *socket_, const ConnectionParams &proxy, bool persistent) : AbstractConnection( p_object_ ) { - socket = socket_; + transport = socket_; psz_useragent = var_InheritString(p_object_, "http-user-agent"); queryOk = false; retries = 0; @@ -83,7 +83,7 @@ HTTPConnection::HTTPConnection(vlc_object_t *p_object_, AuthStorage *auth, HTTPConnection::~HTTPConnection() { free(psz_useragent); - delete socket; + delete transport; } bool HTTPConnection::canReuse(const ConnectionParams ¶ms_) const @@ -108,16 +108,16 @@ bool HTTPConnection::canReuse(const ConnectionParams ¶ms_) const bool HTTPConnection::connect() { if(proxyparams.getHostname().empty()) - return socket->connect(p_object, params.getHostname().c_str(), + return transport->connect(p_object, params.getHostname().c_str(), params.getPort()); else - return socket->connect(p_object, proxyparams.getHostname().c_str(), + return transport->connect(p_object, proxyparams.getHostname().c_str(), proxyparams.getPort()); } bool HTTPConnection::connected() const { - return socket->connected(); + return transport->connected(); } void HTTPConnection::disconnect() @@ -129,7 +129,7 @@ void HTTPConnection::disconnect() chunkLength = 0; bytesRange = BytesRange(); contentType = std::string(); - socket->disconnect(); + transport->disconnect(); } int HTTPConnection::request(const std::string &path, const BytesRange &range) @@ -168,7 +168,7 @@ int HTTPConnection::request(const std::string &path, const BytesRange &range) if(!send( header )) { - socket->disconnect(); + transport->disconnect(); if(!connectionClose) { /* server closed connection pipeline after last req. need new */ @@ -185,11 +185,11 @@ int HTTPConnection::request(const std::string &path, const BytesRange &range) } else if(i_ret == VLC_ETIMEOUT) /* redir */ { - socket->disconnect(); + transport->disconnect(); } else if(i_ret == VLC_EGENERIC) { - socket->disconnect(); + transport->disconnect(); if(!connectionClose) { connectionClose = true; @@ -219,14 +219,14 @@ ssize_t HTTPConnection::read(void *p_buffer, size_t len) len = toRead; ssize_t ret = ( chunked ) ? readChunk(p_buffer, len) - : socket->read(p_object, p_buffer, len); + : transport->read(p_buffer, len); if(ret >= 0) bytesRead += ret; if(ret < 0 || (size_t)ret < len || /* set EOF */ (contentLength == bytesRead && connectionClose)) { - socket->disconnect(); + transport->disconnect(); return ret; } @@ -240,7 +240,7 @@ bool HTTPConnection::send(const std::string &data) bool HTTPConnection::send(const void *buf, size_t size) { - return socket->send(p_object, buf, size); + return transport->send(buf, size); } int HTTPConnection::parseReply() @@ -319,7 +319,7 @@ ssize_t HTTPConnection::readChunk(void *p_buffer, size_t len) if(toread > chunkLength) toread = chunkLength; - ssize_t in = socket->read(p_object, &((uint8_t*)p_buffer)[copied], toread); + ssize_t in = transport->read(&((uint8_t*)p_buffer)[copied], toread); if(in < 0) { return (copied == 0) ? in : copied; @@ -336,7 +336,7 @@ ssize_t HTTPConnection::readChunk(void *p_buffer, size_t len) if(chunkLength == 0) { char crlf[2]; - ssize_t in = socket->read(p_object, &crlf, 2); + ssize_t in = transport->read(&crlf, 2); if(in < 2 || memcmp(crlf, "\r\n", 2)) return (copied == 0) ? -1 : copied; } @@ -347,7 +347,7 @@ ssize_t HTTPConnection::readChunk(void *p_buffer, size_t len) std::string HTTPConnection::readLine() { - return socket->readline(p_object); + return transport->readline(); } void HTTPConnection::setUsed( bool b ) @@ -591,15 +591,14 @@ AbstractConnection * ConnectionFactory::createConnection(vlc_object_t *p_object, } else scheme = params.getScheme(); - const int sockettype = (params.getScheme() == "https") ? TLSSocket::TLS : Socket::REGULAR; - Socket *socket = (sockettype == TLSSocket::TLS) ? new (std::nothrow) TLSSocket() - : new (std::nothrow) Socket(); + const bool b_secure = (params.getScheme() == "https"); + Transport *socket = new (std::nothrow) Transport(b_secure); if(!socket) return NULL; /* disable pipelined tls until we have ticket/resume session support */ HTTPConnection *conn = new (std::nothrow) - HTTPConnection(p_object, authStorage, socket, proxy, sockettype != TLSSocket::TLS); + HTTPConnection(p_object, authStorage, socket, proxy, !b_secure); if(!conn) { delete socket; diff --git a/modules/demux/adaptive/http/HTTPConnection.hpp b/modules/demux/adaptive/http/HTTPConnection.hpp index c006a06808..dd8e41a163 100644 --- a/modules/demux/adaptive/http/HTTPConnection.hpp +++ b/modules/demux/adaptive/http/HTTPConnection.hpp @@ -34,7 +34,7 @@ namespace adaptive { namespace http { - class Socket; + class Transport; class AuthStorage; class AbstractConnection @@ -66,7 +66,7 @@ namespace adaptive class HTTPConnection : public AbstractConnection { public: - HTTPConnection(vlc_object_t *, AuthStorage *, Socket *, + HTTPConnection(vlc_object_t *, AuthStorage *, Transport *, const ConnectionParams &, bool = false); virtual ~HTTPConnection(); @@ -107,7 +107,7 @@ namespace adaptive static const int retryCount = 5; private: - Socket *socket; + Transport *transport; }; class StreamUrlConnection : public AbstractConnection diff --git a/modules/demux/adaptive/http/HTTPConnectionManager.cpp b/modules/demux/adaptive/http/HTTPConnectionManager.cpp index 2f10bde146..a6f1baf347 100644 --- a/modules/demux/adaptive/http/HTTPConnectionManager.cpp +++ b/modules/demux/adaptive/http/HTTPConnectionManager.cpp @@ -28,7 +28,7 @@ #include "HTTPConnectionManager.h" #include "HTTPConnection.hpp" #include "ConnectionParams.hpp" -#include "Sockets.hpp" +#include "Transport.hpp" #include "Downloader.hpp" #include <vlc_url.h> #include <vlc_http.h> diff --git a/modules/demux/adaptive/http/Sockets.cpp b/modules/demux/adaptive/http/Sockets.cpp deleted file mode 100644 index d6916ee569..0000000000 --- a/modules/demux/adaptive/http/Sockets.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Sockets.cpp - ***************************************************************************** - * Copyright (C) 2015 - VideoLAN and VLC authors - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. - *****************************************************************************/ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "Sockets.hpp" - -#include <vlc_network.h> -#include <cerrno> - -using namespace adaptive::http; - -Socket::Socket() -{ - netfd = -1; - type = REGULAR; -} - -Socket::Socket( int type_ ) -{ - netfd = -1; - type = type_; -} - -Socket::~Socket() -{ - disconnect(); -} - -bool Socket::connect(vlc_object_t *p_object, const std::string &hostname, int port) -{ - netfd = net_ConnectTCP(p_object, hostname.c_str(), port); - - if(netfd == -1) - return false; - - return true; -} - -int Socket::getType() const -{ - return type; -} - -bool Socket::connected() const -{ - return (netfd != -1); -} - -void Socket::disconnect() -{ - if (netfd >= 0) - { - net_Close(netfd); - netfd = -1; - } -} - -ssize_t Socket::read(vlc_object_t *p_object, void *p_buffer, size_t len) -{ - return net_Read(p_object, netfd, p_buffer, len); -} - -std::string Socket::readline(vlc_object_t *p_object) -{ - char *line = ::net_Gets(p_object, netfd); - if(line == NULL) - return ""; - std::string ret(line); - ::free(line); - return ret; -} - -bool Socket::send(vlc_object_t *p_object, const void *buf, size_t size) -{ - if (netfd == -1) - return false; - - if (size == 0) - return true; - - return net_Write(p_object, netfd, buf, size) == (ssize_t)size; -} - -TLSSocket::TLSSocket() : Socket( TLS ) -{ - creds = NULL; - tls = NULL; -} - -TLSSocket::~TLSSocket() -{ - disconnect(); -} - -bool TLSSocket::connect(vlc_object_t *p_object, const std::string &hostname, int port) -{ - disconnect(); - if(!Socket::connect(p_object, hostname, port)) - return false; - - creds = vlc_tls_ClientCreate(p_object); - if(!creds) - { - disconnect(); - return false; - } - - tls = vlc_tls_ClientSessionCreateFD(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) -{ - return vlc_tls_Read(tls, p_buffer, len, true); -} - -std::string TLSSocket::readline(vlc_object_t *) -{ - char *line = ::vlc_tls_GetLine(tls); - if(line == NULL) - return ""; - - std::string ret(line); - ::free(line); - return ret; -} - -bool TLSSocket::send(vlc_object_t *, const void *buf, size_t size) -{ - if (!connected()) - return false; - - if (size == 0) - return true; - - return vlc_tls_Write(tls, buf, size) == (ssize_t)size; -} - -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/adaptive/http/Sockets.hpp b/modules/demux/adaptive/http/Sockets.hpp deleted file mode 100644 index d1df1d2df0..0000000000 --- a/modules/demux/adaptive/http/Sockets.hpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Sockets.hpp - ***************************************************************************** - * Copyright (C) 2015 - VideoLAN and VLC authors - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. - *****************************************************************************/ -#ifndef SOCKETS_HPP -#define SOCKETS_HPP - -#include <vlc_common.h> -#include <vlc_tls.h> -#include <string> - -namespace adaptive -{ - namespace http - { - class Socket - { - public: - Socket(); - virtual ~Socket(); - virtual bool connect (vlc_object_t *, const std::string&, int port = 80); - 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); - virtual std::string readline(vlc_object_t *); - virtual void disconnect (); - int getType() const; - static const int REGULAR = 0; - - protected: - Socket( int ); - int netfd; - int type; - }; - - 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); - virtual std::string readline(vlc_object_t *); - virtual void disconnect (); - static const int TLS = REGULAR + 1; - - private: - vlc_tls_creds_t *creds; - vlc_tls_t *tls; - }; - } -} - - -#endif // SOCKETS_HPP diff --git a/modules/demux/adaptive/http/Transport.cpp b/modules/demux/adaptive/http/Transport.cpp new file mode 100644 index 0000000000..b827a69a31 --- /dev/null +++ b/modules/demux/adaptive/http/Transport.cpp @@ -0,0 +1,113 @@ +/* + * Transport.cpp + ***************************************************************************** + * Copyright (C) 2015-2018 VideoLabs, VideoLAN and VLC authors + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "Transport.hpp" + +using namespace adaptive::http; + +Transport::Transport(bool b_secure_) +{ + creds = NULL; + tls = NULL; + b_secure = b_secure_; +} + +Transport::~Transport() +{ + if(connected()) + disconnect(); +} + +bool Transport::connect(vlc_object_t *p_object, const std::string &hostname, int port) +{ + if(connected()) + disconnect(); + + if(b_secure) + { + creds = vlc_tls_ClientCreate(p_object); + if(!creds) + return false; + tls = vlc_tls_SocketOpenTLS(creds, hostname.c_str(), port, "https", + NULL, NULL ); + if(!tls) + { + vlc_tls_Delete(creds); + creds = NULL; + } + } + else + { + tls = vlc_tls_SocketOpenTCP(p_object, hostname.c_str(), port); + } + + return tls != NULL; +} + +bool Transport::connected() const +{ + return tls != NULL; +} + +void Transport::disconnect() +{ + if(tls) + { + vlc_tls_Close(tls); + tls = NULL; + } + + if(creds) + { + vlc_tls_Delete(creds); + creds = NULL; + } +} + +ssize_t Transport::read(void *p_buffer, size_t len) +{ + return vlc_tls_Read(tls, p_buffer, len, true); +} + +std::string Transport::readline() +{ + char *line = ::vlc_tls_GetLine(tls); + if(line == NULL) + return ""; + + std::string ret(line); + ::free(line); + return ret; +} + +bool Transport::send(const void *buf, size_t size) +{ + if (!connected()) + return false; + + if (size == 0) + return true; + + return vlc_tls_Write(tls, buf, size) == (ssize_t)size; +} + diff --git a/modules/demux/adaptive/http/Transport.hpp b/modules/demux/adaptive/http/Transport.hpp new file mode 100644 index 0000000000..c06db0e23f --- /dev/null +++ b/modules/demux/adaptive/http/Transport.hpp @@ -0,0 +1,51 @@ +/* + * Transport.hpp + ***************************************************************************** + * Copyright (C) 2015-2018 - VideoLabs, VideoLAN and VLC authors + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ +#ifndef TRANSPORT_HPP +#define TRANSPORT_HPP + +#include <vlc_common.h> +#include <vlc_tls.h> +#include <string> + +namespace adaptive +{ + namespace http + { + class Transport + { + public: + Transport(bool b_secure = false); + ~Transport(); + bool connect (vlc_object_t *, const std::string&, int port = 80); + bool connected () const; + bool send (const void *buf, size_t size); + ssize_t read (void *p_buffer, size_t len); + std::string readline(); + void disconnect (); + + protected: + vlc_tls_creds_t *creds; + vlc_tls_t *tls; + bool b_secure; + }; + } +} + +#endif // TRANSPORT_HPP _______________________________________________ vlc-commits mailing list [email protected] https://mailman.videolan.org/listinfo/vlc-commits
