Sockets.cpp 3.53 KB
Newer Older
1 2 3
/*
 * Sockets.cpp
 *****************************************************************************
4
 * Copyright (C) 2015 - VideoLAN and VLC authors
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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.
 *****************************************************************************/
#include "Sockets.hpp"

#include <vlc_network.h>
#include <cerrno>

using namespace adaptative::http;

Socket::Socket()
{
    netfd = -1;
30 31 32 33 34 35 36
    type = REGULAR;
}

Socket::Socket( int type_ )
{
    netfd = -1;
    type = type_;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
}

Socket::~Socket()
{
    disconnect();
}

bool Socket::connect(vlc_object_t *stream, const std::string &hostname, int port)
{
    netfd = net_ConnectTCP(stream, hostname.c_str(), port);

    if(netfd == -1)
        return false;

    return true;
}

54 55 56 57 58
int Socket::getType() const
{
    return type;
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72
bool Socket::connected() const
{
    return (netfd != -1);
}

void Socket::disconnect()
{
    if (netfd >= 0)
    {
        net_Close(netfd);
        netfd = -1;
    }
}

73
ssize_t Socket::read(vlc_object_t *stream, void *p_buffer, size_t len)
74
{
75
    return net_Read(stream, netfd, p_buffer, len);
76 77 78 79
}

std::string Socket::readline(vlc_object_t *stream)
{
80
    char *line = ::net_Gets(stream, netfd);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    if(line == NULL)
        return "";
    std::string ret(line);
    ::free(line);
    return ret;
}

bool Socket::send(vlc_object_t *stream, const void *buf, size_t size)
{
    if (netfd == -1)
        return false;

    if (size == 0)
        return true;

96
    return net_Write(stream, netfd, buf, size) == (ssize_t)size;
97 98
}

99
TLSSocket::TLSSocket() : Socket( TLS )
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
{
    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;
}

138
ssize_t TLSSocket::read(vlc_object_t *, void *p_buffer, size_t len)
139
{
140
    return vlc_tls_Read(tls, p_buffer, len, true);
141
}
142

143 144 145 146 147
std::string TLSSocket::readline(vlc_object_t *)
{
    char *line = ::vlc_tls_GetLine(tls);
    if(line == NULL)
        return "";
148

149 150
    std::string ret(line);
    ::free(line);
151 152 153
    return ret;
}

154
bool TLSSocket::send(vlc_object_t *, const void *buf, size_t size)
155 156 157 158 159 160 161
{
    if (!connected())
        return false;

    if (size == 0)
        return true;

162
    return vlc_tls_Write(tls, buf, size) == (ssize_t)size;
163 164 165 166 167 168 169 170 171 172 173 174
}

void TLSSocket::disconnect()
{
    if(tls)
        vlc_tls_SessionDelete(tls);
    if(creds)
        vlc_tls_Delete(creds);
    tls = NULL;
    creds = NULL;
    Socket::disconnect();
}