From patchwork Mon Oct 8 13:48:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Slavka X-Patchwork-Id: 10543 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:ab0:73d2:0:0:0:0:0 with SMTP id m18csp4315165uaq; Mon, 8 Oct 2018 18:50:06 -0700 (PDT) X-Google-Smtp-Source: ACcGV61bWzMB+NeL9cG7GyEpTZIjecuezKV+R5Jlz4ayx+2r34m1hR/xpPKTz2B+7sgEnzALPxn0 X-Received: by 2002:adf:81e1:: with SMTP id 88-v6mr20521287wra.19.1539049806874; Mon, 08 Oct 2018 18:50:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539049806; cv=none; d=google.com; s=arc-20160816; b=YyBj3dXsj8DyGNqpyOB2jIL2ZIEo/T8KkhnLUj+a6rZeFxOmSASlJYueJq16W4yrTR uqlKf02vixwpPmZtmm9oui2MIsjnQhaWlDIVZ3PkI9OrziDlGCRgw6JzhzqMtFUaukj4 Cjun888ORGzxcCsfm7e+opiexTCfGfrmZHFJXjSzTtb+H84DqLSArch7G0zjTlqyjWO5 kbw7Zg0D+4Pf5mkhZgn55mmryHstuezrSfiGOKVMOKds5uSfaGs6C6LB6xcTWYB62+Fx 4RFtJ5bD3mcb0d8ZM8A1SWq3w3/isz/6vyjORVDz4Y/ApZueLtEWeysyYhIs0BPhEZJt uY8Q== 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:message-id:date:to:from:dkim-signature :delivered-to; bh=yp0jg4mNE5SMh4lBrmbUdXIuPqGkv800XRc8APjTvZQ=; b=RfAJvTfT1CyT+cUrr8KIf3DefSzWREuzF8ACnQcX4IU5PQ8aX9Zvr/iVOi0qYQG2z+ y49s9+9ODl7dnWBwWAfJLrrmQI+GFIWod3b0JjhgOJ+hKNLpB9bS9SOt+ulE8Mv3AWep 3l1fcAaM6Yk4cpd7p9iPO1goE7L3XH6UwGAQyWwI5KbS7jBW/fXdDsW8Uewi0m6x7sdx PoTw1ub08iB6aCr0nH/HAVHEroEHTYzWfOMuuetpKz+kAVCbSINW7XbxKbjb3a2spH8N X7yQmRpXYGP+DpmO+P78k5NHlTx+08Y1cRk8w9E3wjq7tRU+fwce3DaoNYNYioWUTKRr wKOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=G38x9mek; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r13-v6si14577133wrw.212.2018.10.08.18.50.05; Mon, 08 Oct 2018 18:50:06 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=G38x9mek; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2021368A0F6; Tue, 9 Oct 2018 04:49:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com [209.85.208.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 916A6689FA0 for ; Mon, 8 Oct 2018 16:55:45 +0300 (EEST) Received: by mail-lj1-f193.google.com with SMTP id p1-v6so14096383ljg.6 for ; Mon, 08 Oct 2018 06:56:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=qT1taFGOJmskRqS5DTUoQ5zpRpor//59qkVr3yikhiw=; b=G38x9mekeLdBy11++e5Iq+IYcP7arfF1kBlcSetvNVGLH8AzqAicVN3HWX/XJcmszu CDAONU14Jraq0cqCTX6YlvOXbPvp4W6AIhlvu0SDbQymnEAwo8U7EiqP/cOn2nWFfkBe TR9/lmSt+D+U5iTt5RkvsIR5aJTVOhvaAUbw17UHPi7eWjTQjZteDHEZfGiozPZUYthc UnI+cXBQWyRgdW/LtmG9/KWVYZMsuW5sSB17iVDB59XbjUpTSCaf5EqWDJ01Ihe7oMKp PpWTkn2hKV95hGFJEBGNGRjAmNCYh2klM5+ZCDN2WSnFx+6E8Ptw6Dtm35/buN+QtWzd 5+vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=qT1taFGOJmskRqS5DTUoQ5zpRpor//59qkVr3yikhiw=; b=e7McvU9vBFyuwx+wXWSsENL0KhRUPwAlB2dTt6Nvp8J3SSg/wXeuiKQDIZ5/XeljJx dYyRMTVY9aXCidRWL8u/0/6wsJ3Oh5Fh0s5NhxodnboK9J9t9WDhpzOcpq7pdHJ3qb9b IIJZtlTcwP+z/cipAai7wLw73lvf5VrDOi04zwFfQ1iECF8W0pfq+4Wd6kFLPSioQRoQ 6RJcT8IW2gL00zFBNNZQpEt0LBmdozSDCiA7V0a1qwZv9JrKRSFCydk2fG11/UmV16n3 tuL3ZaaFFH7D9qjYwlBoW3JdLyVhlZGr5qXF3xrw3VqJswJE8QYdQBeeavhDaYiV56WB 186w== X-Gm-Message-State: ABuFfoi/heJUii7ghH1cZ8iQ9rDbwvs8jnH7nwgomT+UYU8jkviMLtjR LUxoA2X+YGRn1xX0CKoVAffYpUaq X-Received: by 2002:a2e:9584:: with SMTP id w4-v6mr11598158ljh.118.1539006540832; Mon, 08 Oct 2018 06:49:00 -0700 (PDT) Received: from localhost.localdomain ([195.138.82.255]) by smtp.gmail.com with ESMTPSA id l124-v6sm3643266lfb.58.2018.10.08.06.48.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Oct 2018 06:49:00 -0700 (PDT) From: Slavka To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Oct 2018 16:48:41 +0300 Message-Id: <20181008134841.10300-1-hvostik.leva@gmail.com> X-Mailer: git-send-email 2.10.0.windows.1 X-Mailman-Approved-At: Tue, 09 Oct 2018 04:49:40 +0300 Subject: [FFmpeg-devel] [PATCH] dns cache map key now hostname+port 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: root MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: root --- libavformat/tcp.c | 16 ++++++++-------- libavutil/dns_cache.c | 28 +++++++++++++++++++--------- libavutil/dns_cache.h | 6 +++--- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index d2de743..6515309 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -405,9 +405,9 @@ static int tcp_open(URLContext *h, const char *uri, int flags) memcpy(hostname_bak, hostname, 1024); if (s->dns_cache_clear) { av_log(NULL, AV_LOG_INFO, "will delete cache entry, hostname = %s\n", hostname); - remove_dns_cache_entry(hostname); + remove_dns_cache_entry(hostname, portstr); } else { - dns_entry = get_dns_cache_reference(hostname); + dns_entry = get_dns_cache_reference(hostname, portstr); } } @@ -496,7 +496,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags) av_log(NULL, AV_LOG_WARNING, "terminated by application in AVAPP_CTRL_DID_TCP_OPEN"); goto fail1; } else if (!dns_entry && strcmp(control.ip, hostname_bak)) { - add_dns_cache_entry(hostname_bak, cur_ai, s->dns_cache_timeout); + add_dns_cache_entry(hostname_bak, portstr, cur_ai, s->dns_cache_timeout); av_log(NULL, AV_LOG_INFO, "Add dns cache hostname = %s, ip = %s\n", hostname_bak , control.ip); } } @@ -528,7 +528,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags) if (dns_entry) { av_log(NULL, AV_LOG_ERROR, "Hit dns cache but connect fail hostname = %s, ip = %s\n", hostname , control.ip); release_dns_cache_reference(hostname_bak, &dns_entry); - remove_dns_cache_entry(hostname_bak); + remove_dns_cache_entry(hostname_bak, portstr); } else { freeaddrinfo(ai); } @@ -592,9 +592,9 @@ static int tcp_fast_open(URLContext *h, const char *http_request, const char *ur memcpy(hostname_bak, hostname, 1024); if (s->dns_cache_clear) { av_log(NULL, AV_LOG_INFO, "will delete cache entry, hostname = %s\n", hostname); - remove_dns_cache_entry(hostname); + remove_dns_cache_entry(hostname, portstr); } else { - dns_entry = get_dns_cache_reference(hostname); + dns_entry = get_dns_cache_reference(hostname, portstr); } } @@ -686,7 +686,7 @@ static int tcp_fast_open(URLContext *h, const char *http_request, const char *ur av_log(NULL, AV_LOG_WARNING, "terminated by application in AVAPP_CTRL_DID_TCP_OPEN"); goto fail1; } else if (!dns_entry && strcmp(control.ip, hostname_bak)) { - add_dns_cache_entry(hostname_bak, cur_ai, s->dns_cache_timeout); + add_dns_cache_entry(hostname_bak, portstr, cur_ai, s->dns_cache_timeout); av_log(NULL, AV_LOG_INFO, "Add dns cache hostname = %s, ip = %s\n", hostname_bak , control.ip); } } @@ -718,7 +718,7 @@ static int tcp_fast_open(URLContext *h, const char *http_request, const char *ur if (dns_entry) { av_log(NULL, AV_LOG_ERROR, "Hit dns cache but connect fail hostname = %s, ip = %s\n", hostname , control.ip); release_dns_cache_reference(hostname_bak, &dns_entry); - remove_dns_cache_entry(hostname_bak); + remove_dns_cache_entry(hostname_bak, portstr); } else { freeaddrinfo(ai); } diff --git a/libavutil/dns_cache.c b/libavutil/dns_cache.c index c705401..70d71a4 100644 --- a/libavutil/dns_cache.c +++ b/libavutil/dns_cache.c @@ -115,10 +115,13 @@ fail: return NULL; } -DnsCacheEntry *get_dns_cache_reference(char *hostname) { +DnsCacheEntry *get_dns_cache_reference(char *hostname, char *portstr) { AVDictionaryEntry *elem = NULL; DnsCacheEntry *dns_cache_entry = NULL; int64_t cur_time = av_gettime_relative(); + char hostnameWithPort[1024]; + strcat(hostnameWithPort, hostname); + strcat(hostnameWithPort, portstr); if (cur_time < 0 || !hostname || strlen(hostname) == 0) { return NULL; @@ -130,14 +133,15 @@ DnsCacheEntry *get_dns_cache_reference(char *hostname) { #endif } + if (context && context->initialized) { pthread_mutex_lock(&context->dns_dictionary_mutex); - elem = av_dict_get(context->dns_dictionary, hostname, NULL, AV_DICT_MATCH_CASE); + elem = av_dict_get(context->dns_dictionary, hostnameWithPort, NULL, AV_DICT_MATCH_CASE); if (elem) { dns_cache_entry = (DnsCacheEntry *) (intptr_t) strtoll(elem->value, NULL, 10); if (dns_cache_entry) { if (dns_cache_entry->expired_time < cur_time) { - inner_remove_dns_cache(hostname, dns_cache_entry); + inner_remove_dns_cache(hostnameWithPort, dns_cache_entry); dns_cache_entry = NULL; } else { dns_cache_entry->ref_count++; @@ -169,9 +173,12 @@ int release_dns_cache_reference(char *hostname, DnsCacheEntry **p_entry) { return 0; } -int remove_dns_cache_entry(char *hostname) { +int remove_dns_cache_entry(char *hostname, char *portstr) { AVDictionaryEntry *elem = NULL; DnsCacheEntry *dns_cache_entry = NULL; + char hostnameWithPort[1024]; + strcat(hostnameWithPort, hostname); + strcat(hostnameWithPort, portstr); if (!hostname || strlen(hostname) == 0) { return -1; @@ -179,11 +186,11 @@ int remove_dns_cache_entry(char *hostname) { if (context && context->initialized) { pthread_mutex_lock(&context->dns_dictionary_mutex); - elem = av_dict_get(context->dns_dictionary, hostname, NULL, AV_DICT_MATCH_CASE); + elem = av_dict_get(context->dns_dictionary, hostnameWithPort, NULL, AV_DICT_MATCH_CASE); if (elem) { dns_cache_entry = (DnsCacheEntry *) (intptr_t) strtoll(elem->value, NULL, 10); if (dns_cache_entry) { - inner_remove_dns_cache(hostname, dns_cache_entry); + inner_remove_dns_cache(hostnameWithPort, dns_cache_entry); } } pthread_mutex_unlock(&context->dns_dictionary_mutex); @@ -192,10 +199,13 @@ int remove_dns_cache_entry(char *hostname) { return 0; } -int add_dns_cache_entry(char *hostname, struct addrinfo *cur_ai, int64_t timeout) { +int add_dns_cache_entry(char *hostname, char* portstr, struct addrinfo *cur_ai, int64_t timeout) { DnsCacheEntry *new_entry = NULL; DnsCacheEntry *old_entry = NULL; AVDictionaryEntry *elem = NULL; + char hostnameWithPort[1024]; + strcat(hostnameWithPort, hostname); + strcat(hostnameWithPort, portstr); if (!hostname || strlen(hostname) == 0 || timeout <= 0) { goto fail; @@ -207,7 +217,7 @@ int add_dns_cache_entry(char *hostname, struct addrinfo *cur_ai, int64_t timeout if (context && context->initialized) { pthread_mutex_lock(&context->dns_dictionary_mutex); - elem = av_dict_get(context->dns_dictionary, hostname, NULL, AV_DICT_MATCH_CASE); + elem = av_dict_get(context->dns_dictionary, hostnameWithPort, NULL, AV_DICT_MATCH_CASE); if (elem) { old_entry = (DnsCacheEntry *) (intptr_t) strtoll(elem->value, NULL, 10); if (old_entry) { @@ -217,7 +227,7 @@ int add_dns_cache_entry(char *hostname, struct addrinfo *cur_ai, int64_t timeout } new_entry = new_dns_cache_entry(hostname, cur_ai, timeout); if (new_entry) { - av_dict_set_int(&context->dns_dictionary, hostname, (int64_t) (intptr_t) new_entry, 0); + av_dict_set_int(&context->dns_dictionary, hostnameWithPort, (int64_t) (intptr_t) new_entry, 0); } pthread_mutex_unlock(&context->dns_dictionary_mutex); diff --git a/libavutil/dns_cache.h b/libavutil/dns_cache.h index a2ed92e..0ea7e2d 100644 --- a/libavutil/dns_cache.h +++ b/libavutil/dns_cache.h @@ -30,9 +30,9 @@ typedef struct DnsCacheEntry { struct addrinfo *res; // construct by private function, not support ai_next and ai_canonname, can only be released using free_private_addrinfo } DnsCacheEntry; -DnsCacheEntry *get_dns_cache_reference(char *hostname); +DnsCacheEntry *get_dns_cache_reference(char *hostname, char* portstr); int release_dns_cache_reference(char *hostname, DnsCacheEntry **p_entry); -int remove_dns_cache_entry(char *hostname); -int add_dns_cache_entry(char *hostname, struct addrinfo *cur_ai, int64_t timeout); +int remove_dns_cache_entry(char *hostname, char* portstr); +int add_dns_cache_entry(char *hostname, char* portstr, struct addrinfo *cur_ai, int64_t timeout); #endif /* AVUTIL_DNS_CACHE_H */