FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tls.c
Go to the documentation of this file.
1 /*
2  * TLS/DTLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
4  * Copyright (c) 2025 Jack Lau
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avformat.h"
24 #include "internal.h"
25 #include "network.h"
26 #include "os_support.h"
27 #include "url.h"
28 #include "tls.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/getenv_utf8.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/parseutils.h"
33 
34 static int set_options(TLSShared *c, const char *uri)
35 {
36  char buf[1024];
37  const char *p = strchr(uri, '?');
38  if (!p)
39  return 0;
40 
41  if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p)) {
42  c->ca_file = av_strdup(buf);
43  if (!c->ca_file)
44  return AVERROR(ENOMEM);
45  }
46 
47  if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
48  char *endptr = NULL;
49  c->verify = strtol(buf, &endptr, 10);
50  if (buf == endptr)
51  c->verify = 1;
52  }
53 
54  if (!c->cert_file && av_find_info_tag(buf, sizeof(buf), "cert", p)) {
55  c->cert_file = av_strdup(buf);
56  if (!c->cert_file)
57  return AVERROR(ENOMEM);
58  }
59 
60  if (!c->key_file && av_find_info_tag(buf, sizeof(buf), "key", p)) {
61  c->key_file = av_strdup(buf);
62  if (!c->key_file)
63  return AVERROR(ENOMEM);
64  }
65 
66  return 0;
67 }
68 
70 {
71  int port;
72  const char *p;
73  char buf[200], opts[50] = "";
74  struct addrinfo hints = { 0 }, *ai = NULL;
75  const char *proxy_path;
76  char *env_http_proxy, *env_no_proxy;
77  int use_proxy;
78  int ret;
79 
80  ret = set_options(c, uri);
81  if (ret < 0)
82  return ret;
83 
84  if (c->listen)
85  snprintf(opts, sizeof(opts), "?listen=1");
86 
87  av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
88 
89  p = strchr(uri, '?');
90 
91  if (!p) {
92  p = opts;
93  } else {
94  if (av_find_info_tag(opts, sizeof(opts), "listen", p))
95  c->listen = 1;
96  }
97 
98  ff_url_join(buf, sizeof(buf), c->is_dtls ? "udp" : "tcp", NULL, c->underlying_host, port, "%s", p);
99 
100  hints.ai_flags = AI_NUMERICHOST;
101  if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
102  c->numerichost = 1;
103  freeaddrinfo(ai);
104  }
105 
106  if (!c->host && !(c->host = av_strdup(c->underlying_host)))
107  return AVERROR(ENOMEM);
108 
109  env_http_proxy = getenv_utf8("http_proxy");
110  proxy_path = c->http_proxy ? c->http_proxy : env_http_proxy;
111 
112  env_no_proxy = getenv_utf8("no_proxy");
113  use_proxy = !ff_http_match_no_proxy(env_no_proxy, c->underlying_host) &&
114  proxy_path && av_strstart(proxy_path, "http://", NULL);
115  freeenv_utf8(env_no_proxy);
116 
117  if (use_proxy) {
118  char proxy_host[200], proxy_auth[200], dest[200];
119  int proxy_port;
120  av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
121  proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
122  proxy_path);
123  ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
124  ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
125  proxy_port, "/%s", dest);
126  }
127 
128  freeenv_utf8(env_http_proxy);
129  if (c->is_dtls) {
130  av_dict_set_int(options, "connect", 1, 0);
131  av_dict_set_int(options, "fifo_size", 0, 0);
132  /* Set the max packet size to the buffer size. */
133  av_dict_set_int(options, "pkt_size", c->mtu, 0);
134  }
135  ret = ffurl_open_whitelist(c->is_dtls ? &c->udp : &c->tcp, buf, AVIO_FLAG_READ_WRITE,
136  &parent->interrupt_callback, options,
137  parent->protocol_whitelist, parent->protocol_blacklist, parent);
138  if (c->is_dtls) {
139  if (ret < 0) {
140  av_log(c, AV_LOG_ERROR, "WHIP: Failed to connect udp://%s:%d\n", c->underlying_host, port);
141  return ret;
142  }
143  /* Make the socket non-blocking, set to READ and WRITE mode after connected */
145  c->udp->flags |= AVIO_FLAG_READ | AVIO_FLAG_NONBLOCK;
146  }
147  return ret;
148 }
149 
150 /**
151  * Read all data from the given URL url and store it in the given buffer bp.
152  */
153 int ff_url_read_all(const char *url, AVBPrint *bp)
154 {
155  int ret = 0;
157  URLContext *uc = NULL;
158  char buf[MAX_URL_SIZE];
159 
161  if (ret < 0) {
162  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to open url %s\n", url);
163  goto end;
164  }
165 
166  while (1) {
167  ret = ffurl_read(uc, buf, sizeof(buf));
168  if (ret == AVERROR_EOF) {
169  /* Reset the error because we read all response as answer util EOF. */
170  ret = 0;
171  break;
172  }
173  if (ret <= 0) {
174  av_log(NULL, AV_LOG_ERROR, "TLS: Failed to read from url=%s, key is %s\n", url, bp->str);
175  goto end;
176  }
177 
178  av_bprintf(bp, "%.*s", ret, buf);
179  if (!av_bprint_is_complete(bp)) {
180  av_log(NULL, AV_LOG_ERROR, "TLS: Exceed max size %.*s, %s\n", ret, buf, bp->str);
181  ret = AVERROR(EIO);
182  goto end;
183  }
184  }
185 
186 end:
187  ffurl_closep(&uc);
188  av_dict_free(&opts);
189  return ret;
190 }
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_find_info_tag
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:756
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVIO_FLAG_READ_WRITE
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:619
freeenv_utf8
static void freeenv_utf8(char *var)
Definition: getenv_utf8.h:72
AVDictionary
Definition: dict.c:32
os_support.h
freeaddrinfo
#define freeaddrinfo
Definition: network.h:218
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ffurl_open_whitelist
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:363
AI_NUMERICHOST
#define AI_NUMERICHOST
Definition: network.h:187
ff_url_join
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:40
ff_http_match_no_proxy
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:557
internal.h
opts
AVDictionary * opts
Definition: movenc.c:51
URLContext::protocol_whitelist
const char * protocol_whitelist
Definition: url.h:46
NULL
#define NULL
Definition: coverity.c:32
URLContext::protocol_blacklist
const char * protocol_blacklist
Definition: url.h:47
parseutils.h
getenv_utf8
static char * getenv_utf8(const char *varname)
Definition: getenv_utf8.h:67
options
Definition: swscale.c:43
set_options
static int set_options(TLSShared *c, const char *uri)
Definition: tls.c:34
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_url_read_all
int ff_url_read_all(const char *url, AVBPrint *bp)
Read all data from the given URL url and store it in the given buffer bp.
Definition: tls.c:153
ff_socket_nonblock
int ff_socket_nonblock(int socket, int enable)
getenv_utf8.h
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:233
av_strstart
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:36
getaddrinfo
#define getaddrinfo
Definition: network.h:217
URLContext
Definition: url.h:35
av_url_split
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:354
url.h
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:589
ret
ret
Definition: filter_design.txt:187
ff_tls_open_underlying
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:69
URLContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Definition: url.h:44
avformat.h
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
network.h
tls.h
MAX_URL_SIZE
#define MAX_URL_SIZE
Definition: internal.h:30
addrinfo::ai_flags
int ai_flags
Definition: network.h:138
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:177
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
mem.h
TLSShared
Definition: tls.h:47
AVIO_FLAG_NONBLOCK
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:636
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
addrinfo
Definition: network.h:137
snprintf
#define snprintf
Definition: snprintf.h:34
ffurl_get_file_handle
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:815
ffurl_read
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: url.h:181