]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Fix dnsmgr entries to ask for the same address family each time.
authorRichard Mudgett <rmudgett@digium.com>
Tue, 22 Nov 2011 22:55:28 +0000 (22:55 +0000)
committerRichard Mudgett <rmudgett@digium.com>
Tue, 22 Nov 2011 22:55:28 +0000 (22:55 +0000)
The dnsmgr refresh would always get the first address found regardless of
the original address family requested.  So if you asked for only IPv4
addresses originally, you might get an IPv6 address on refresh.

* Saved the original address family requested by ast_dnsmgr_lookup() to be
used when the address is refreshed.

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@345976 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/dnsmgr.h
main/dnsmgr.c

index da83030649a008ed70b280585611e95e24b1d80d..b62c2a14b26bb533ce3f0993b9ad5c46801326d1 100644 (file)
@@ -44,6 +44,7 @@ struct ast_dnsmgr_entry;
  * \param result where the DNS manager should store the IP address as it refreshes it.
  * \param service
  *
+ * \details
  * This function allocates a new DNS manager entry object, and fills it with the
  * provided hostname and IP address.  This function does not force an initial lookup
  * of the IP address.  So, generally, this should be used when the initial address
@@ -55,6 +56,24 @@ struct ast_dnsmgr_entry;
  */
 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service);
 
+/*!
+ * \brief Allocate a new DNS manager entry
+ *
+ * \param name the hostname
+ * \param result where the DNS manager should store the IP address as it refreshes it.
+ * \param service
+ * \param family Address family to filter DNS addresses.
+ *
+ * \details
+ * This function allocates a new DNS manager entry object, and fills it with the
+ * provided hostname and IP address.  This function does not force an initial lookup
+ * of the IP address.  So, generally, this should be used when the initial address
+ * is already known.
+ *
+ * \return a DNS manager entry
+ */
+struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family);
+
 /*!
  * \brief Free a DNS manager entry
  *
index 8076b6f8054fa365d6327292393ea03ed9734a48..722cf36f8f5ac2317f27486d7f8333c9d3986d3a 100644 (file)
@@ -54,6 +54,8 @@ struct ast_dnsmgr_entry {
        struct ast_sockaddr *result;
        /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
        char *service;
+       /*! Address family to filter DNS responses. */
+       unsigned int family;
        /*! Set to 1 if the entry changes */
        unsigned int changed:1;
        ast_mutex_t lock;
@@ -83,7 +85,7 @@ static struct refresh_info master_refresh_info = {
        .verbose = 0,
 };
 
-struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
+struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
 {
        struct ast_dnsmgr_entry *entry;
        int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
@@ -99,6 +101,7 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *r
                entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
                strcpy(entry->service, service);
        }
+       entry->family = family;
 
        AST_RWLIST_WRLOCK(&entry_list);
        AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
@@ -107,6 +110,11 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *r
        return entry;
 }
 
+struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
+{
+       return ast_dnsmgr_get_family(name, result, service, 0);
+}
+
 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
 {
        if (!entry)
@@ -123,6 +131,8 @@ void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
 
 int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
 {
+       unsigned int family;
+
        if (ast_strlen_zero(name) || !result || !dnsmgr) {
                return -1;
        }
@@ -131,6 +141,9 @@ int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_
                return 0;
        }
 
+       /* Lookup address family filter. */
+       family = result->ss.ss_family;
+
        /*
         * If it's actually an IP address and not a name, there's no
         * need for a managed lookup.
@@ -150,7 +163,7 @@ int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_
        }
        
        ast_verb(3, "adding dns manager for '%s'\n", name);
-       *dnsmgr = ast_dnsmgr_get(name, result, service);
+       *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
        return !*dnsmgr;
 }
 
@@ -168,6 +181,7 @@ static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
                ast_verb(3, "refreshing '%s'\n", entry->name);
        }
 
+       tmp.ss.ss_family = entry->family;
        if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
                if (!ast_sockaddr_port(&tmp)) {
                        ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));