libavformat/tls.c
558d192d
 /*
  * TLS/SSL Protocol
  * Copyright (c) 2011 Martin Storsjo
  *
d814a839
  * This file is part of FFmpeg.
558d192d
  *
d814a839
  * FFmpeg is free software; you can redistribute it and/or
558d192d
  * 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.
  *
d814a839
  * FFmpeg is distributed in the hope that it will be useful,
558d192d
  * 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
d814a839
  * License along with FFmpeg; if not, write to the Free Software
558d192d
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "avformat.h"
4a006b9e
 #include "internal.h"
 #include "network.h"
 #include "os_support.h"
558d192d
 #include "url.h"
4a006b9e
 #include "tls.h"
558d192d
 #include "libavutil/avstring.h"
8b09d917
 #include "libavutil/opt.h"
973a758f
 #include "libavutil/parseutils.h"
d15eec4d
 
4a006b9e
 static void set_options(TLSShared *c, const char *uri)
d15eec4d
 {
f31c36e5
     char buf[1024];
973a758f
     const char *p = strchr(uri, '?');
     if (!p)
         return;
 
e6e71fd7
     if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p))
         c->ca_file = av_strdup(buf);
973a758f
 
e6e71fd7
     if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
b2460858
         char *endptr = NULL;
e6e71fd7
         c->verify = strtol(buf, &endptr, 10);
b2460858
         if (buf == endptr)
e6e71fd7
             c->verify = 1;
b2460858
     }
 
f31c36e5
     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);
973a758f
 }
 
4a006b9e
 int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
558d192d
 {
     int port;
cd9d6399
     const char *p;
4a006b9e
     char buf[200], opts[50] = "";
558d192d
     struct addrinfo hints = { 0 }, *ai = NULL;
2565dbeb
     const char *proxy_path;
     int use_proxy;
558d192d
 
4a006b9e
     set_options(c, uri);
558d192d
 
705b748e
     if (c->listen)
         snprintf(opts, sizeof(opts), "?listen=1");
 
6dd5371e
     av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
cd9d6399
 
     p = strchr(uri, '?');
 
     if (!p) {
         p = opts;
     } else {
         if (av_find_info_tag(opts, sizeof(opts), "listen", p))
             c->listen = 1;
     }
 
6dd5371e
     ff_url_join(buf, sizeof(buf), "tcp", NULL, c->underlying_host, port, "%s", p);
558d192d
 
     hints.ai_flags = AI_NUMERICHOST;
6dd5371e
     if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
4a006b9e
         c->numerichost = 1;
558d192d
         freeaddrinfo(ai);
     }
 
6dd5371e
     if (!c->host && !(c->host = av_strdup(c->underlying_host)))
         return AVERROR(ENOMEM);
 
de9cd1b1
     proxy_path = getenv("http_proxy");
6dd5371e
     use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->underlying_host) &&
4b1f5e50
                 proxy_path && av_strstart(proxy_path, "http://", NULL);
de9cd1b1
 
2565dbeb
     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);
6dd5371e
         ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
2565dbeb
         ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
                     proxy_port, "/%s", dest);
     }
 
fe3fed0b
     return ffurl_open_whitelist(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
                                 &parent->interrupt_callback, options,
                                 parent->protocol_whitelist);
558d192d
 }