tls.c 3.54 KB
Newer Older
1 2 3 4
/*
 * TLS/SSL Protocol
 * Copyright (c) 2011 Martin Storsjo
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9 10 11
 * 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.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
wm4's avatar
wm4 committed
23 24 25
#include "internal.h"
#include "network.h"
#include "os_support.h"
26
#include "url.h"
wm4's avatar
wm4 committed
27
#include "tls.h"
28
#include "libavutil/avstring.h"
29
#include "libavutil/opt.h"
30
#include "libavutil/parseutils.h"
31

wm4's avatar
wm4 committed
32
static void set_options(TLSShared *c, const char *uri)
33
{
34
    char buf[1024];
35 36 37 38
    const char *p = strchr(uri, '?');
    if (!p)
        return;

39 40
    if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p))
        c->ca_file = av_strdup(buf);
41

42
    if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
Peter Ross's avatar
Peter Ross committed
43
        char *endptr = NULL;
44
        c->verify = strtol(buf, &endptr, 10);
Peter Ross's avatar
Peter Ross committed
45
        if (buf == endptr)
46
            c->verify = 1;
Peter Ross's avatar
Peter Ross committed
47 48
    }

49 50 51 52 53
    if (!c->cert_file && av_find_info_tag(buf, sizeof(buf), "cert", p))
        c->cert_file = av_strdup(buf);

    if (!c->key_file && av_find_info_tag(buf, sizeof(buf), "key", p))
        c->key_file = av_strdup(buf);
54 55
}

wm4's avatar
wm4 committed
56
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
57 58
{
    int port;
59
    const char *p;
wm4's avatar
wm4 committed
60
    char buf[200], opts[50] = "";
61
    struct addrinfo hints = { 0 }, *ai = NULL;
62 63
    const char *proxy_path;
    int use_proxy;
64

wm4's avatar
wm4 committed
65
    set_options(c, uri);
66

67 68 69
    if (c->listen)
        snprintf(opts, sizeof(opts), "?listen=1");

70
    av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
71 72 73 74 75 76 77 78 79 80

    p = strchr(uri, '?');

    if (!p) {
        p = opts;
    } else {
        if (av_find_info_tag(opts, sizeof(opts), "listen", p))
            c->listen = 1;
    }

81
    ff_url_join(buf, sizeof(buf), "tcp", NULL, c->underlying_host, port, "%s", p);
82 83

    hints.ai_flags = AI_NUMERICHOST;
84
    if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
wm4's avatar
wm4 committed
85
        c->numerichost = 1;
86 87 88
        freeaddrinfo(ai);
    }

89 90 91
    if (!c->host && !(c->host = av_strdup(c->underlying_host)))
        return AVERROR(ENOMEM);

92
    proxy_path = getenv("http_proxy");
93
    use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->underlying_host) &&
94
                proxy_path && av_strstart(proxy_path, "http://", NULL);
95

96 97 98 99 100 101
    if (use_proxy) {
        char proxy_host[200], proxy_auth[200], dest[200];
        int proxy_port;
        av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
                     proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
                     proxy_path);
102
        ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
103 104 105 106
        ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
                    proxy_port, "/%s", dest);
    }

107 108
    return ffurl_open_whitelist(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
                                &parent->interrupt_callback, options,
109
                                parent->protocol_whitelist, parent->protocol_blacklist, parent);
110
}