[FFmpeg-devel] [PATCH] dns cache map key now hostname+port

Slavka hvostik.leva at gmail.com
Mon Oct 8 16:48:41 EEST 2018


From: root <root at DESKTOP-1UVTC9H.localdomain>

---
 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 */
-- 
2.7.4



More information about the ffmpeg-devel mailing list