tunnel_test.c 4.57 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
/*****************************************************************************
 * tunnel_test.c: HTTP CONNECT
 *****************************************************************************
 * Copyright (C) 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

#undef NDEBUG

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
# define accept4(a,b,c,d) accept(a,b,c)
#endif
#include <netinet/in.h>
#include <arpa/inet.h>

#include <vlc_common.h>
#include <vlc_tls.h>
#include "transport.h"

static void proxy_client_process(int fd)
{
    char buf[1024];
    size_t buflen = 0;
    ssize_t val;

    /* Read request (and possibly more) */
    while (strnstr(buf, "\r\n\r\n", buflen) == NULL)
    {
        val = recv(fd, buf + buflen, sizeof (buf) - buflen - 1, 0);
        if (val <= 0)
            assert(!"Incomplete request");
        buflen += val;
    }

    buf[buflen] = '\0';

    char host[64];
    unsigned port, ver;
    int offset;

    assert(sscanf(buf, "CONNECT %63[^:]:%u HTTP/1.%1u%n", host, &port, &ver,
                  &offset) == 3);
    assert(!strcmp(host, "www.example.com"));
    assert(port == 443);
    assert(ver == 1);

    assert(sscanf(buf + offset + 2, "Host: %63[^:]:%u", host, &port) == 2);
    assert(!strcmp(host, "www.example.com"));
    assert(port == 443);

77 78 79
    assert(strstr(buf, "\r\nProxy-Authorization: Basic "
                  "QWxhZGRpbjpvcGVuIHNlc2FtZQ==\r\n") != NULL);

80 81 82 83 84 85 86
    const char resp[] = "HTTP/1.1 500 Failure\r\n\r\n";

    val = write(fd, resp, strlen(resp));
    assert((size_t)val == strlen(resp));
    shutdown(fd, SHUT_WR);
}

87 88
static unsigned connection_count = 0;

89 90 91 92 93 94 95 96 97 98 99 100
static void *proxy_thread(void *data)
{
    int lfd = (intptr_t)data;

    for (;;)
    {
        int cfd = accept4(lfd, NULL, NULL, SOCK_CLOEXEC);
        if (cfd == -1)
            continue;

        int canc = vlc_savecancel();
        proxy_client_process(cfd);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
101
        vlc_close(cfd);
102
        connection_count++;
103 104 105 106 107
        vlc_restorecancel(canc);
    }
    vlc_assert_unreachable();
}

108
static int server_socket(unsigned *port)
109
{
110 111 112
    int fd = socket(PF_INET6, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP);
    if (fd == -1)
        return -1;
113 114 115 116 117 118 119 120 121 122

    struct sockaddr_in6 addr = {
        .sin6_family = AF_INET6,
#ifdef HAVE_SA_LEN
        .sin6_len = sizeof (addr),
#endif
        .sin6_addr = in6addr_loopback,
    };
    socklen_t addrlen = sizeof (addr);

123
    if (bind(fd, (struct sockaddr *)&addr, addrlen)
124
     || getsockname(fd, (struct sockaddr *)&addr, &addrlen))
125
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126
        vlc_close(fd);
127
        return -1;
128 129
    }

130 131 132 133 134 135
    *port = ntohs(addr.sin6_port);
    return fd;
}

int main(void)
{
136
    char *url;
137
    unsigned port;
138
    bool two = false;
139

140
    /* Test bad URLs */
141
    vlc_https_connect_proxy(NULL, NULL, "www.example.com", 0, &two,
142
                            "/test");
143
    vlc_https_connect_proxy(NULL, NULL, "www.example.com", 0, &two,
144 145 146 147 148 149
                            "ftp://proxy.example.com/");

    int lfd = server_socket(&port);
    if (lfd == -1)
        return 77;

150
    if (asprintf(&url, "http://Aladdin:open%%20sesame@[::1]:%u", port) < 0)
151
        url = NULL;
152

153 154
    assert(url != NULL);

155
    /* Test connection failure */
156
    vlc_https_connect_proxy(NULL, NULL, "www.example.com", 0, &two, url);
157

158 159
    if (listen(lfd, 255))
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
160
        vlc_close(lfd);
161 162 163 164
        return 77;
    }

    vlc_thread_t th;
165 166 167 168
    if (vlc_clone(&th, proxy_thread, (void*)(intptr_t)lfd,
                  VLC_THREAD_PRIORITY_LOW))
        assert(!"Thread error");

169
    /* Test proxy error */
170
    vlc_https_connect_proxy(NULL, NULL, "www.example.com", 0, &two, url);
171 172 173

    vlc_cancel(th);
    vlc_join(th, NULL);
174
    assert(connection_count > 0);
175
    free(url);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
176
    vlc_close(lfd);
177
}