Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
33 {
34  int port;
35  const char *p;
36  char buf[200], opts[50] = "";
37  struct addrinfo hints = { 0 }, *ai = NULL;
38  const char *proxy_path;
39  int use_proxy;
40 
41  if (c->listen)
42  snprintf(opts, sizeof(opts), "?listen=1");
43 
44  av_url_split(NULL, 0, NULL, 0, c->host, sizeof(c->host), &port, NULL, 0, uri);
45 
46  p = strchr(uri, '?');
47 
48  if (!p) {
49  p = opts;
50  } else {
51  if (av_find_info_tag(opts, sizeof(opts), "listen", p))
52  c->listen = 1;
53  }
54 
55  ff_url_join(buf, sizeof(buf), "tcp", NULL, c->host, port, "%s", p);
56 
57  hints.ai_flags = AI_NUMERICHOST;
58  if (!getaddrinfo(c->host, NULL, &hints, &ai)) {
59  c->numerichost = 1;
60  freeaddrinfo(ai);
61  }
62 
63  proxy_path = getenv("http_proxy");
64  use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->host) &&
65  proxy_path && av_strstart(proxy_path, "http://", NULL);
66 
67  if (use_proxy) {
68  char proxy_host[200], proxy_auth[200], dest[200];
69  int proxy_port;
70  av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
71  proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
72  proxy_path);
73  ff_url_join(dest, sizeof(dest), NULL, NULL, c->host, port, NULL);
74  ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
75  proxy_port, "/%s", dest);
76  }
77 
78  return ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
79  &parent->interrupt_callback, options, parent->protocols, parent);
80 }
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:2870
AVIOInterruptCB interrupt_callback
Definition: url.h:51
int listen
Definition: tls.h:36
#define freeaddrinfo
Definition: network.h:187
#define AI_NUMERICHOST
Definition: network.h:156
AVOptions.
miscellaneous OS support macros and functions.
char host[200]
Definition: tls.h:38
Definition: tls.h:31
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:619
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const URLProtocol **protocols, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:175
const OptionDef options[]
Definition: avconv_opt.c:2447
AVDictionary * opts
Definition: movenc.c:50
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:260
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:36
NULL
Definition: eval.c:55
Definition: url.h:38
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:370
misc parsing utilities
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:32
#define getaddrinfo
Definition: network.h:186
const struct URLProtocol ** protocols
A NULL-terminated list of protocols usable by the child contexts.
Definition: url.h:44
Main libavformat public API header.
int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
Definition: tls.c:32
URLContext * tcp
Definition: tls.h:41
int numerichost
Definition: tls.h:39
int ai_flags
Definition: network.h:107
unbuffered private I/O API