From patchwork Sat Sep 21 17:59:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodger Combs X-Patchwork-Id: 15217 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 33DE944A621 for ; Sat, 21 Sep 2019 20:59:27 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 15E9568A9ED; Sat, 21 Sep 2019 20:59:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E6D2168A92E for ; Sat, 21 Sep 2019 20:59:19 +0300 (EEST) Received: by mail-io1-f51.google.com with SMTP id q10so23680452iop.2 for ; Sat, 21 Sep 2019 10:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=llsbFNurHVIaYDnk7vJIBSkdr/Ve6/TgjygcMQ40GIY=; b=DLiSxdzLOHRu71VFrQezME4lzU3rhOLCkYU6yQkucXzfdYXXYYEydODX97KAO/AXje OkL5CKbv0fg/NVHLCPZ0YsijXKJmcMC6C8+UvO0BfRKXjyy0oFttbEzZz2O3zlBPCzXo 8iQNvHMPKSjbpli7Z9t4UACqw46VnfJh+fuLVA0MY+VlwSRFLeZ/lQsQfzycF0zn2ZXJ IWW6cq4slB7cNAbUJ9qcgTJFYxOiYaxxQSH5CTnuRw/WUcWthh6352pTlGnHMbz45oy+ ey7JXk0fXb+VQJw8Um9ljCpWXe3ESZ/bO8jE1A0RmucSCMWcRsF9HDf5opKusUuGAZxL PzcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=llsbFNurHVIaYDnk7vJIBSkdr/Ve6/TgjygcMQ40GIY=; b=VZjOmQ4da7O06JlwZi4l4mOP8Fh4+QqRPEGxF0jCUA+5qRc0AmIyfSVybkJSZZ2hqK RsZKMUhBZ8Agzs4IJQ4J25zSnorbZWTIsHHCwkhhTHY1JJjsMRvr1neoaHxgUxgrZTAs CU7SYXAWpi+IgxZDbS6J1OhEAsyoyxTlC1ZXmNDbtWU1INrhIvr5YZ2/7kqAi8ROCSmS YabpPiPX2EehAu94ugm/ErSiy3Gll5+eggBSGoUoAbcB4n2hFC/lBCEGV5RJRhhEFcyS zL5g41Y94QDwzS4I99ipVcJbFcuVl1RQ9kbs14SrK6XKg4zhKgxhy7cdOi/1FytWGo/3 5jZA== X-Gm-Message-State: APjAAAWfyxcWjEBgFUj5NCMKG24bWS+n2/3/A8kR6YYuA8Cfl9I2azrm vFMiAdy9hZcHcnhWclKOTHuMFLn1yMg= X-Google-Smtp-Source: APXvYqxP7oQ8hEXBj9BL7nYt7XuUL8LQhE7XzlnMwbUFGSZMEsdNN8EnXaF30RJvC32aC2rb1kMLwg== X-Received: by 2002:a6b:7d0c:: with SMTP id c12mr22462370ioq.238.1569088758312; Sat, 21 Sep 2019 10:59:18 -0700 (PDT) Received: from MacBook-Pro.localdomain ([24.14.135.13]) by smtp.gmail.com with ESMTPSA id c8sm7135622iol.57.2019.09.21.10.59.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 21 Sep 2019 10:59:17 -0700 (PDT) From: Rodger Combs To: ffmpeg-devel@ffmpeg.org Date: Sat, 21 Sep 2019 12:59:13 -0500 Message-Id: <20190921175913.55990-1-rodger.combs@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavf/tcp: add resolve_hosts option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This can be used to resolve particular hosts offline without affecting HTTP headers, TLS SNI, or cross-domain redirects. It works similarly to curl's --resolve option, but without port-specific handling. --- libavformat/tcp.c | 27 +++++++++++++++++++++++++++ libavformat/version.h | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 2198e0f00e..e4c439ee56 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -45,6 +45,7 @@ typedef struct TCPContext { #if !HAVE_WINSOCK2_H int tcp_mss; #endif /* !HAVE_WINSOCK2_H */ + char *resolve_hosts; } TCPContext; #define OFFSET(x) offsetof(TCPContext, x) @@ -60,6 +61,7 @@ static const AVOption options[] = { #if !HAVE_WINSOCK2_H { "tcp_mss", "Maximum segment size for outgoing TCP packets", OFFSET(tcp_mss), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, #endif /* !HAVE_WINSOCK2_H */ + { "resolve_hosts", "Comma-separated host resolutions, in the form host:ip", OFFSET(resolve_hosts), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, { NULL } }; @@ -99,6 +101,27 @@ static void customize_fd(void *ctx, int fd) #endif /* !HAVE_WINSOCK2_H */ } +static int lookup_host(URLContext *h, char *hostname, size_t hostname_size) +{ + TCPContext *s = h->priv_data; + if (hostname[0]) { + size_t hostlen = strlen(hostname); + for (const char *addr = s->resolve_hosts; addr; addr = strchr(addr, ','), addr && addr++) { + if (!strncmp(addr, hostname, hostlen) && addr[hostlen] == ':') { + addr += hostlen + 1; + const char *end = strchr(addr, ','); + size_t len = end ? end - addr : strlen(addr); + if (len >= hostname_size - 1) + return AVERROR(ENOMEM); + memcpy(hostname, addr, len); + hostname[len] = 0; + return 0; + } + } + } + return 0; +} + /* return non zero if error */ static int tcp_open(URLContext *h, const char *uri, int flags) { @@ -120,6 +143,10 @@ static int tcp_open(URLContext *h, const char *uri, int flags) av_log(h, AV_LOG_ERROR, "Port missing in uri\n"); return AVERROR(EINVAL); } + + if ((ret = lookup_host(h, hostname, sizeof(hostname))) < 0) + return ret; + p = strchr(uri, '?'); if (p) { if (av_find_info_tag(buf, sizeof(buf), "listen", p)) { diff --git a/libavformat/version.h b/libavformat/version.h index 2eb14659d0..d1dbb1e2d1 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -33,7 +33,7 @@ // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 #define LIBAVFORMAT_VERSION_MINOR 32 -#define LIBAVFORMAT_VERSION_MICRO 105 +#define LIBAVFORMAT_VERSION_MICRO 106 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \