From patchwork Thu Sep 20 22:12:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 10424 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp54540jap; Thu, 20 Sep 2018 15:12:32 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYr+lgzXZD3YrAVGf1GcXmquSjG43bdpAHvBKHTssqYDVXiU7KS+eM8jXA4tqR3gmsvGrqm X-Received: by 2002:a1c:98cc:: with SMTP id a195-v6mr4947452wme.64.1537481552801; Thu, 20 Sep 2018 15:12:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537481552; cv=none; d=google.com; s=arc-20160816; b=vznZMewJ1QMAvpy3E7avtd7CLV/wp4VIUdI/ckywyJd2wtrFCOeV7ChrXVTfymDP0A UvY4gdZs9RNdyN50gEEaxsO8v+N2FfB5aGJFUfmRb6eaSgyIcEi9EfaHBEV3rUig/Cy3 /dV4cqjBz4xqzHHqRQZJv3DOiYAqB+rwmk2kUQyv5C6ugf/B7LG7CUnNodseLrQ1BNnJ ULdMKFeY2BuXTSCxa/tr4GK36ZLH0BrD6kWj/IiNkjW2HEsCl9JfExrnTRQi9ull2KSw WIeVZpjMGs4sH+h5NF3CmRMGV5ROeylLQC1vSPRPw3FwW12fTmkZpxpgHLXQmkK0Pk06 ESkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=RbBKsq4ETtSJkuE7ktLVE/jNTZLo0V3ycsCwuPyiyj4=; b=xCOo8jkVnQ/Dg+LMz38UaWrGmJnqrq+JcNHbaegzxNrbilFMCpSgM7eoQ2S7YgN3H3 DaovDvcIp4Mr/NS4O/L5DZQHIIfHCzqIwXB9FjwEDUvnCRBpA6MBi3NqfU/7VvkBXplh UeyKlUg56lvNoJpkkM/3Va5ZKnLkGgDRjFXuC/JemXDQiJmo/IIqyrg2DlHUicuv5cVG dWD+n7Ooxp1PodKH0hRgVGgRXs+7hv6UuHH5M4+8EdOQ/04ys2yO7e4s/aQwAj1v/XXq QJnVsBbA26XHgrqu7vC0dn0WLzffmazIdrocxbxndbk9DcjqrtVvja9mdz+tHyBiIWAf ByuA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s7-v6si23726455wrn.62.2018.09.20.15.12.32; Thu, 20 Sep 2018 15:12:32 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6E67068A6AD; Fri, 21 Sep 2018 01:12:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8224E68A57A for ; Fri, 21 Sep 2018 01:12:02 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 99F76E0FC8; Fri, 21 Sep 2018 00:12:16 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zmlcRvpiJk9X; Fri, 21 Sep 2018 00:12:15 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 73FA7E08BE; Fri, 21 Sep 2018 00:12:15 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 Sep 2018 00:12:01 +0200 Message-Id: <20180920221202.1876-3-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180920221202.1876-1-cus@passwd.hu> References: <20180920221202.1876-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH 3/4] avformat/rtpproto: use factorized ip functions 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 Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavformat/Makefile | 2 +- libavformat/rtpproto.c | 126 ++++--------------------------------------------- 2 files changed, 11 insertions(+), 117 deletions(-) diff --git a/libavformat/Makefile b/libavformat/Makefile index 467ae2d87e..e0222535c1 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -596,7 +596,7 @@ OBJS-$(CONFIG_RTMPS_PROTOCOL) += rtmpproto.o rtmpdigest.o rtmppkt.o OBJS-$(CONFIG_RTMPT_PROTOCOL) += rtmpproto.o rtmpdigest.o rtmppkt.o OBJS-$(CONFIG_RTMPTE_PROTOCOL) += rtmpproto.o rtmpdigest.o rtmppkt.o OBJS-$(CONFIG_RTMPTS_PROTOCOL) += rtmpproto.o rtmpdigest.o rtmppkt.o -OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o +OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o ip.o OBJS-$(CONFIG_SCTP_PROTOCOL) += sctp.o OBJS-$(CONFIG_SRTP_PROTOCOL) += srtpproto.o srtp.o OBJS-$(CONFIG_SUBFILE_PROTOCOL) += subfile.o diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c index c01d9cea18..9ffd89ce49 100644 --- a/libavformat/rtpproto.c +++ b/libavformat/rtpproto.c @@ -32,6 +32,7 @@ #include "rtp.h" #include "rtpproto.h" #include "url.h" +#include "ip.h" #include #include "internal.h" @@ -45,8 +46,8 @@ typedef struct RTPContext { const AVClass *class; URLContext *rtp_hd, *rtcp_hd, *fec_hd; - int rtp_fd, rtcp_fd, nb_ssm_include_addrs, nb_ssm_exclude_addrs; - struct sockaddr_storage **ssm_include_addrs, **ssm_exclude_addrs; + int rtp_fd, rtcp_fd; + IPSourceFilters filters; int write_to_source; struct sockaddr_storage last_rtp_source, last_rtcp_source; socklen_t last_rtp_source_len, last_rtcp_source_len; @@ -126,45 +127,6 @@ int ff_rtp_set_remote_url(URLContext *h, const char *uri) return 0; } -static struct addrinfo* rtp_resolve_host(const char *hostname, int port, - int type, int family, int flags) -{ - struct addrinfo hints = { 0 }, *res = 0; - int error; - char service[16]; - - snprintf(service, sizeof(service), "%d", port); - hints.ai_socktype = type; - hints.ai_family = family; - hints.ai_flags = flags; - if ((error = getaddrinfo(hostname, service, &hints, &res))) { - res = NULL; - av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error)); - } - - return res; -} - -static int compare_addr(const struct sockaddr_storage *a, - const struct sockaddr_storage *b) -{ - if (a->ss_family != b->ss_family) - return 1; - if (a->ss_family == AF_INET) { - return (((const struct sockaddr_in *)a)->sin_addr.s_addr != - ((const struct sockaddr_in *)b)->sin_addr.s_addr); - } - -#if HAVE_STRUCT_SOCKADDR_IN6 - if (a->ss_family == AF_INET6) { - const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr; - const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr; - return memcmp(s6_addr_a, s6_addr_b, 16); - } -#endif - return 1; -} - static int get_port(const struct sockaddr_storage *ss) { if (ss->ss_family == AF_INET) @@ -186,25 +148,6 @@ static void set_port(struct sockaddr_storage *ss, int port) #endif } -static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr) -{ - int i; - if (s->nb_ssm_exclude_addrs) { - for (i = 0; i < s->nb_ssm_exclude_addrs; i++) { - if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i])) - return 1; - } - } - if (s->nb_ssm_include_addrs) { - for (i = 0; i < s->nb_ssm_include_addrs; i++) { - if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i])) - return 0; - } - return 1; - } - return 0; -} - /** * add option to url of the form: * "http://host:port/path?option1=val1&option2=val2... @@ -252,48 +195,6 @@ static void build_udp_url(RTPContext *s, url_add_option(buf, buf_size, "block=%s", exclude_sources); } -static void rtp_parse_addr_list(URLContext *h, char *buf, - struct sockaddr_storage ***address_list_ptr, - int *address_list_size_ptr) -{ - struct addrinfo *ai = NULL; - struct sockaddr_storage *source_addr; - char tmp = '\0', *p = buf, *next; - - /* Resolve all of the IPs */ - - while (p && p[0]) { - next = strchr(p, ','); - - if (next) { - tmp = *next; - *next = '\0'; - } - - ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0); - if (ai) { - source_addr = av_mallocz(sizeof(struct sockaddr_storage)); - if (!source_addr) { - freeaddrinfo(ai); - break; - } - - memcpy(source_addr, ai->ai_addr, ai->ai_addrlen); - freeaddrinfo(ai); - dynarray_add(address_list_ptr, address_list_size_ptr, source_addr); - } else { - av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p); - } - - if (next) { - *next = tmp; - p = next + 1; - } else { - p = NULL; - } - } -} - /** * url syntax: rtp://host:port[?option=val...] * option: 'ttl=n' : set the ttl value (for multicast only) @@ -366,17 +267,16 @@ static int rtp_open(URLContext *h, const char *uri, int flags) } if (av_find_info_tag(buf, sizeof(buf), "sources", p)) { av_strlcpy(include_sources, buf, sizeof(include_sources)); - - rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs); + ff_ip_parse_sources(h, buf, &s->filters); } else { - rtp_parse_addr_list(h, s->sources, &s->ssm_include_addrs, &s->nb_ssm_include_addrs); + ff_ip_parse_sources(h, s->sources, &s->filters); sources = s->sources; } if (av_find_info_tag(buf, sizeof(buf), "block", p)) { av_strlcpy(exclude_sources, buf, sizeof(exclude_sources)); - rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs); + ff_ip_parse_blocks(h, buf, &s->filters); } else { - rtp_parse_addr_list(h, s->block, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs); + ff_ip_parse_blocks(h, s->block, &s->filters); block = s->block; } } @@ -500,7 +400,7 @@ static int rtp_read(URLContext *h, uint8_t *buf, int size) continue; return AVERROR(EIO); } - if (rtp_check_source_lists(s, addrs[i])) + if (ff_ip_check_source_lists(addrs[i], &s->filters)) continue; return len; } @@ -603,14 +503,8 @@ static int rtp_write(URLContext *h, const uint8_t *buf, int size) static int rtp_close(URLContext *h) { RTPContext *s = h->priv_data; - int i; - - for (i = 0; i < s->nb_ssm_include_addrs; i++) - av_freep(&s->ssm_include_addrs[i]); - av_freep(&s->ssm_include_addrs); - for (i = 0; i < s->nb_ssm_exclude_addrs; i++) - av_freep(&s->ssm_exclude_addrs[i]); - av_freep(&s->ssm_exclude_addrs); + + ff_ip_free_filters(&s->filters); ffurl_close(s->rtp_hd); ffurl_close(s->rtcp_hd);