2011-02-06 00:19:49 +02:00
|
|
|
/*
|
|
|
|
* TLS/SSL Protocol
|
|
|
|
* Copyright (c) 2011 Martin Storsjo
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-02-06 00:19:49 +02:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-02-06 00:19:49 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-02-06 00:19:49 +02:00
|
|
|
* 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
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-02-06 00:19:49 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2015-05-27 12:57:50 +02:00
|
|
|
#include "internal.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "os_support.h"
|
2011-02-06 00:19:49 +02:00
|
|
|
#include "url.h"
|
2015-05-27 12:57:50 +02:00
|
|
|
#include "tls.h"
|
2011-02-06 00:19:49 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2011-02-22 12:02:01 +02:00
|
|
|
#include "libavutil/opt.h"
|
2012-07-22 07:24:43 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2011-11-10 17:58:58 +03:00
|
|
|
|
2015-05-27 12:57:50 +02:00
|
|
|
static void set_options(TLSShared *c, const char *uri)
|
2011-11-10 17:58:58 +03:00
|
|
|
{
|
2013-09-27 12:21:42 +03:00
|
|
|
char buf[1024];
|
2012-07-22 07:24:43 +03:00
|
|
|
const char *p = strchr(uri, '?');
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
2013-09-27 10:22:42 +03:00
|
|
|
if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p))
|
|
|
|
c->ca_file = av_strdup(buf);
|
2012-07-22 07:24:43 +03:00
|
|
|
|
2013-09-27 10:22:42 +03:00
|
|
|
if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
|
2012-07-22 07:24:53 +03:00
|
|
|
char *endptr = NULL;
|
2013-09-27 10:22:42 +03:00
|
|
|
c->verify = strtol(buf, &endptr, 10);
|
2012-07-22 07:24:53 +03:00
|
|
|
if (buf == endptr)
|
2013-09-27 10:22:42 +03:00
|
|
|
c->verify = 1;
|
2012-07-22 07:24:53 +03:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:21:42 +03:00
|
|
|
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);
|
2012-07-22 07:24:43 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:57:50 +02:00
|
|
|
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
|
2011-02-06 00:19:49 +02:00
|
|
|
{
|
|
|
|
int port;
|
2014-10-09 22:34:54 +03:00
|
|
|
const char *p;
|
2015-05-27 12:57:50 +02:00
|
|
|
char buf[200], opts[50] = "";
|
2011-02-06 00:19:49 +02:00
|
|
|
struct addrinfo hints = { 0 }, *ai = NULL;
|
2011-11-10 16:26:23 +03:00
|
|
|
const char *proxy_path;
|
|
|
|
int use_proxy;
|
2011-02-06 00:19:49 +02:00
|
|
|
|
2015-05-27 12:57:50 +02:00
|
|
|
set_options(c, uri);
|
2011-02-06 00:19:49 +02:00
|
|
|
|
2013-09-19 12:30:52 +03:00
|
|
|
if (c->listen)
|
|
|
|
snprintf(opts, sizeof(opts), "?listen=1");
|
|
|
|
|
2015-06-08 11:50:48 +02:00
|
|
|
av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
|
2014-10-09 22:34:54 +03:00
|
|
|
|
|
|
|
p = strchr(uri, '?');
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
p = opts;
|
|
|
|
} else {
|
|
|
|
if (av_find_info_tag(opts, sizeof(opts), "listen", p))
|
|
|
|
c->listen = 1;
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:50:48 +02:00
|
|
|
ff_url_join(buf, sizeof(buf), "tcp", NULL, c->underlying_host, port, "%s", p);
|
2011-02-06 00:19:49 +02:00
|
|
|
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
2015-06-08 11:50:48 +02:00
|
|
|
if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
|
2015-05-27 12:57:50 +02:00
|
|
|
c->numerichost = 1;
|
2011-02-06 00:19:49 +02:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:50:48 +02:00
|
|
|
if (!c->host && !(c->host = av_strdup(c->underlying_host)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-02-27 12:13:47 +03:00
|
|
|
proxy_path = getenv("http_proxy");
|
2015-06-08 11:50:48 +02:00
|
|
|
use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->underlying_host) &&
|
2014-08-14 23:31:25 +03:00
|
|
|
proxy_path && av_strstart(proxy_path, "http://", NULL);
|
2013-02-27 12:13:47 +03:00
|
|
|
|
2011-11-10 16:26:23 +03:00
|
|
|
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);
|
2015-06-08 11:50:48 +02:00
|
|
|
ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
|
2011-11-10 16:26:23 +03:00
|
|
|
ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
|
|
|
|
proxy_port, "/%s", dest);
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:57:50 +02:00
|
|
|
return ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
|
|
|
|
&parent->interrupt_callback, options);
|
2011-02-06 00:19:49 +02:00
|
|
|
}
|