FFmpeg  4.4.9
tls.c
Go to the documentation of this file.
1 /*
2  * TLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * 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  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avformat.h"
23 #include "internal.h"
24 #include "network.h"
25 #include "os_support.h"
26 #include "url.h"
27 #include "tls.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
31 
32 static int set_options(TLSShared *c, const char *uri)
33 {
34  char buf[1024];
35  const char *p = strchr(uri, '?');
36  if (!p)
37  return 0;
38 
39  if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p)) {
40  c->ca_file = av_strdup(buf);
41  if (!c->ca_file)
42  return AVERROR(ENOMEM);
43  }
44 
45  if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
46  char *endptr = NULL;
47  c->verify = strtol(buf, &endptr, 10);
48  if (buf == endptr)
49  c->verify = 1;
50  }
51 
52  if (!c->cert_file && av_find_info_tag(buf, sizeof(buf), "cert", p)) {
53  c->cert_file = av_strdup(buf);
54  if (!c->cert_file)
55  return AVERROR(ENOMEM);
56  }
57 
58  if (!c->key_file && av_find_info_tag(buf, sizeof(buf), "key", p)) {
59  c->key_file = av_strdup(buf);
60  if (!c->key_file)
61  return AVERROR(ENOMEM);
62  }
63 
64  return 0;
65 }
66 
68 {
69  int port;
70  const char *p;
71  char buf[200], opts[50] = "";
72  struct addrinfo hints = { 0 }, *ai = NULL;
73  const char *proxy_path;
74  int use_proxy;
75  int ret;
76 
77  ret = set_options(c, uri);
78  if (ret < 0)
79  return ret;
80 
81  if (c->listen)
82  snprintf(opts, sizeof(opts), "?listen=1");
83 
84  av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
85 
86  p = strchr(uri, '?');
87 
88  if (!p) {
89  p = opts;
90  } else {
91  if (av_find_info_tag(opts, sizeof(opts), "listen", p))
92  c->listen = 1;
93  }
94 
95  ff_url_join(buf, sizeof(buf), "tcp", NULL, c->underlying_host, port, "%s", p);
96 
97  hints.ai_flags = AI_NUMERICHOST;
98  if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
99  c->numerichost = 1;
100  freeaddrinfo(ai);
101  }
102 
103  if (!c->host && !(c->host = av_strdup(c->underlying_host)))
104  return AVERROR(ENOMEM);
105 
106  proxy_path = c->http_proxy ? c->http_proxy : getenv("http_proxy");
107  use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->underlying_host) &&
108  proxy_path && av_strstart(proxy_path, "http://", NULL);
109 
110  if (use_proxy) {
111  char proxy_host[200], proxy_auth[200], dest[200];
112  int proxy_port;
113  av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
114  proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
115  proxy_path);
116  ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
117  ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
118  proxy_port, "/%s", dest);
119  }
120 
121  return ffurl_open_whitelist(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
122  &parent->interrupt_callback, options,
123  parent->protocol_whitelist, parent->protocol_blacklist, parent);
124 }
Main libavformat public API header.
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:309
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:676
#define NULL
Definition: coverity.c:32
const OptionDef options[]
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:4798
#define AVERROR(e)
Definition: error.h:43
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
common internal API header
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:551
#define AI_NUMERICHOST
Definition: network.h:187
#define getaddrinfo
Definition: network.h:217
#define freeaddrinfo
Definition: network.h:218
AVOptions.
miscellaneous OS support macros and functions.
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
Definition: parseutils.c:751
misc parsing utilities
#define snprintf
Definition: snprintf.h:34
Definition: tls.h:29
Definition: url.h:38
const char * protocol_whitelist
Definition: url.h:49
AVIOInterruptCB interrupt_callback
Definition: url.h:47
const char * protocol_blacklist
Definition: url.h:50
int ai_flags
Definition: network.h:138
AVDictionary * opts
Definition: movenc.c:50
static int set_options(TLSShared *c, const char *uri)
Definition: tls.c:32
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:67
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:38
unbuffered private I/O API
static double c[64]