You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6KB

  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. #include "avformat.h"
  22. #include "internal.h"
  23. #include "network.h"
  24. #include "os_support.h"
  25. #include "url.h"
  26. #include "tls.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. static void set_options(TLSShared *c, const char *uri)
  31. {
  32. char buf[1024];
  33. const char *p = strchr(uri, '?');
  34. if (!p)
  35. return;
  36. if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p))
  37. c->ca_file = av_strdup(buf);
  38. if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) {
  39. char *endptr = NULL;
  40. c->verify = strtol(buf, &endptr, 10);
  41. if (buf == endptr)
  42. c->verify = 1;
  43. }
  44. if (!c->cert_file && av_find_info_tag(buf, sizeof(buf), "cert", p))
  45. c->cert_file = av_strdup(buf);
  46. if (!c->key_file && av_find_info_tag(buf, sizeof(buf), "key", p))
  47. c->key_file = av_strdup(buf);
  48. }
  49. int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options)
  50. {
  51. int port;
  52. const char *p;
  53. char buf[200], opts[50] = "";
  54. struct addrinfo hints = { 0 }, *ai = NULL;
  55. const char *proxy_path;
  56. int use_proxy;
  57. set_options(c, uri);
  58. if (c->listen)
  59. snprintf(opts, sizeof(opts), "?listen=1");
  60. av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri);
  61. p = strchr(uri, '?');
  62. if (!p) {
  63. p = opts;
  64. } else {
  65. if (av_find_info_tag(opts, sizeof(opts), "listen", p))
  66. c->listen = 1;
  67. }
  68. ff_url_join(buf, sizeof(buf), "tcp", NULL, c->underlying_host, port, "%s", p);
  69. hints.ai_flags = AI_NUMERICHOST;
  70. if (!getaddrinfo(c->underlying_host, NULL, &hints, &ai)) {
  71. c->numerichost = 1;
  72. freeaddrinfo(ai);
  73. }
  74. if (!c->host && !(c->host = av_strdup(c->underlying_host)))
  75. return AVERROR(ENOMEM);
  76. proxy_path = c->http_proxy ? c->http_proxy : getenv("http_proxy");
  77. use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), c->underlying_host) &&
  78. proxy_path && av_strstart(proxy_path, "http://", NULL);
  79. if (use_proxy) {
  80. char proxy_host[200], proxy_auth[200], dest[200];
  81. int proxy_port;
  82. av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
  83. proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
  84. proxy_path);
  85. ff_url_join(dest, sizeof(dest), NULL, NULL, c->underlying_host, port, NULL);
  86. ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
  87. proxy_port, "/%s", dest);
  88. }
  89. return ffurl_open_whitelist(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
  90. &parent->interrupt_callback, options,
  91. parent->protocol_whitelist, parent->protocol_blacklist, parent);
  92. }