transport.c 3.99 KB
Newer Older
1 2 3 4 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*****************************************************************************
 * transport.c: HTTP/TLS TCP transport layer
 *****************************************************************************
 * Copyright © 2015 Rémi Denis-Courmont
 *
 * 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 <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef HAVE_POLL
#include <poll.h>
#endif
#include <fcntl.h>
#include <vlc_common.h>
#include <vlc_network.h>
#include <vlc_tls.h>

#include "transport.h"

static void cleanup_addrinfo(void *data)
{
    freeaddrinfo(data);
}

static void cleanup_fd(void *data)
{
    net_Close((intptr_t)data);
}

static int vlc_tcp_connect(vlc_object_t *obj, const char *name, unsigned port)
{
    struct addrinfo hints =
    {
        .ai_socktype = SOCK_STREAM,
        .ai_protocol = IPPROTO_TCP,
    }, *res;

    assert(name != NULL);
    msg_Dbg(obj, "resolving %s ...", name);

    int val = vlc_getaddrinfo(name, port, &hints, &res);
    if (val != 0)
    {   /* TODO: C locale for gai_strerror() */
        msg_Err(obj, "cannot resolve %s port %u: %s", name, port,
                gai_strerror(val));
        return -1;
    }

    int fd = -1;

    vlc_cleanup_push(cleanup_addrinfo, res);
    msg_Dbg(obj, "connecting to %s port %u ...", name, port);

    for (const struct addrinfo *p = res; p != NULL; p = p->ai_next)
    {
        fd = vlc_socket(p->ai_family, p->ai_socktype, p->ai_protocol, false);
        if (fd == -1)
        {
            msg_Warn(obj, "cannot create socket: %s", vlc_strerror_c(errno));
            continue;
        }

        vlc_cleanup_push(cleanup_fd, (void *)(intptr_t)fd);
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));

        val = connect(fd, p->ai_addr, p->ai_addrlen);
        vlc_cleanup_pop();

        if (val == 0)
            break; /* success! */

        msg_Err(obj, "cannot connect to %s port %u: %s", name, port,
                vlc_strerror_c(errno));
        net_Close(fd);
        fd = -1;
    }

    vlc_cleanup_pop();
    freeaddrinfo(res);
99

100
    if (fd != -1)
101
#ifndef _WIN32
102
        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
103
#else
104
        ioctlsocket(fd, FIONBIO, &(unsigned long){ 1 });
105
#endif
106 107 108
    return fd;
}

109 110 111 112 113 114 115 116 117
vlc_tls_t *vlc_http_connect(vlc_object_t *obj, const char *name, unsigned port)
{
    if (port == 0)
        port = 80;

    int fd = vlc_tcp_connect(obj, name, port);
    if (fd == -1)
        return NULL;

118
    vlc_tls_t *tls = vlc_tls_SocketOpen(obj, fd);
119 120 121 122 123
    if (tls == NULL)
        net_Close(fd);
    return tls;
}

124 125 126 127 128 129
vlc_tls_t *vlc_https_connect(vlc_tls_creds_t *creds, const char *name,
                             unsigned port, bool *restrict two)
{
    if (port == 0)
        port = 443;

130
    int fd = vlc_tcp_connect(creds->p_parent, name, port);
131 132 133 134 135
    if (fd == -1)
        return NULL;

    /* TLS with ALPN */
    const char *alpn[] = { "h2", "http/1.1", NULL };
136
    char *alp;
137 138 139 140 141 142 143 144 145 146 147 148 149

    vlc_tls_t *tls = vlc_tls_ClientSessionCreate(creds, fd, name, "https",
                                                 alpn, &alp);
    if (tls == NULL)
    {
        net_Close(fd);
        return NULL;
    }

    *two = (alp != NULL) && !strcmp(alp, "h2");
    free(alp);
    return tls;
}