Add BASE_PT_ field display types
[metze/wireshark/wip.git] / epan / addr_resolv.c
index 6776001e3121ea1a6e94c1e6da7d3ad6f11a52ec..8aab2bdc9c330785b2bc63ce0577e406e46b7216 100644 (file)
@@ -289,7 +289,7 @@ static gboolean
 ipv6_equal(gconstpointer v1, gconstpointer v2)
 {
 
-    if( memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0 ) {
+    if (memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0) {
         return TRUE;
     }
 
@@ -299,7 +299,15 @@ ipv6_equal(gconstpointer v1, gconstpointer v2)
 /*
  * Flag controlling what names to resolve.
  */
-e_addr_resolve gbl_resolv_flags = {TRUE, FALSE, FALSE, TRUE, TRUE, FALSE};
+e_addr_resolve gbl_resolv_flags = {
+    TRUE,   /* mac_name */
+    FALSE,  /* network_name */
+    FALSE,  /* transport_name */
+    TRUE,   /* concurrent_dns */
+    TRUE,   /* dns_pkt_addr_resolution */
+    TRUE,   /* use_external_net_name_resolver */
+    FALSE   /* load_hosts_file_from_profile_only */
+};
 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
 static guint name_resolve_concurrency = 500;
 #endif
@@ -476,7 +484,7 @@ add_service_name(port_type proto, const guint port, const char *service_name)
         g_free(key);
     }
 
-    switch(proto){
+    switch(proto) {
         case PT_TCP:
             g_free(serv_port_table->tcp_name);
             serv_port_table->tcp_name = g_strdup(service_name);
@@ -538,26 +546,26 @@ parse_service_line (char *line)
         return;
 
     /* seems we got all interesting things from the file */
-    if(strcmp(cp, "tcp") == 0) {
+    if (strcmp(cp, "tcp") == 0) {
         max_port = MAX_TCP_PORT;
         proto = PT_TCP;
     }
-    else if(strcmp(cp, "udp") == 0) {
+    else if (strcmp(cp, "udp") == 0) {
         max_port = MAX_UDP_PORT;
         proto = PT_UDP;
     }
-    else if(strcmp(cp, "sctp") == 0) {
+    else if (strcmp(cp, "sctp") == 0) {
         max_port = MAX_SCTP_PORT;
         proto = PT_SCTP;
     }
-    else if(strcmp(cp, "dccp") == 0) {
+    else if (strcmp(cp, "dccp") == 0) {
         max_port = MAX_DCCP_PORT;
         proto = PT_DCCP;
     } else {
         return;
     }
 
-    if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
+    if (CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port)) {
         /* some assertion here? */
         return;
     }
@@ -613,34 +621,34 @@ wmem_utoa(wmem_allocator_t *allocator, guint port)
 }
 
 
-static gchar
-*serv_name_lookup(const guint port, const port_type proto)
+static const gchar *
+serv_name_lookup(const guint port, const port_type proto)
 {
     serv_port_t *serv_port_table;
     gchar *name;
 
     serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
 
-    if(serv_port_table){
+    if (serv_port_table) {
         /* Set which table we should look up port in */
         switch(proto) {
             case PT_UDP:
-                if(serv_port_table->udp_name){
+                if (serv_port_table->udp_name) {
                     return serv_port_table->udp_name;
                 }
                 break;
             case PT_TCP:
-                if(serv_port_table->tcp_name){
+                if (serv_port_table->tcp_name) {
                     return serv_port_table->tcp_name;
                 }
                 break;
             case PT_SCTP:
-                if(serv_port_table->sctp_name){
+                if (serv_port_table->sctp_name) {
                     return serv_port_table->sctp_name;
                 }
                 break;
             case PT_DCCP:
-                if(serv_port_table->dccp_name){
+                if (serv_port_table->dccp_name) {
                     return serv_port_table->dccp_name;
                 }
                 break;
@@ -651,14 +659,11 @@ static gchar
         } /* proto */
     }
 
-    /* getservbyport() was used here but it was to expensive, if the functionality is desired
-     * it would be better to pre parse etc/services or C:\Windows\System32\drivers\etc at
-     * startup
-     */
+    /* Use numerical port string */
     name = (gchar*)g_malloc(16);
     guint32_to_str_buf(port, name, 16);
 
-    if(serv_port_table == NULL){
+    if (serv_port_table == NULL) {
         int *key;
 
         key = (int *)g_new(int, 1);
@@ -748,7 +753,7 @@ initialize_services(void)
 static void
 service_name_lookup_cleanup(void)
 {
-    if(serv_port_hashtable){
+    if (serv_port_hashtable) {
         g_hash_table_destroy(serv_port_hashtable);
         serv_port_hashtable = NULL;
     }
@@ -762,14 +767,14 @@ fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
 {
     subnet_entry_t subnet_entry;
 
-    if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY)
+    if (tp->flags & DUMMY_ADDRESS_ENTRY)
         return; /* already done */
 
-    tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
+    tp->flags |= DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
 
     /* Do we have a subnet for this address? */
     subnet_entry = subnet_lookup(addr);
-    if(0 != subnet_entry.mask) {
+    if (0 != subnet_entry.mask) {
         /* Print name, then '.' then IP address after subnet mask */
         guint32 host_addr;
         gchar buffer[MAX_IP_STR_LEN];
@@ -787,7 +792,7 @@ fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
          */
         i = subnet_entry.mask_length / 8;
         while(*(paddr) != '\0' && i > 0) {
-            if(*(++paddr) == '.') {
+            if (*(++paddr) == '.') {
                 --i;
             }
         }
@@ -849,6 +854,7 @@ new_ipv4(const guint addr)
     hashipv4_t *tp = g_new(hashipv4_t, 1);
     tp->addr = addr;
     tp->flags = 0;
+    tp->name[0] = '\0';
     ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
     return tp;
 }
@@ -857,19 +863,18 @@ static hashipv4_t *
 host_lookup(const guint addr, gboolean *found)
 {
     hashipv4_t * volatile tp;
-    struct hostent *hostp;
 
     *found = TRUE;
 
     tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
-    if(tp == NULL){
+    if (tp == NULL) {
         tp = new_ipv4(addr);
         g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
-    }else{
-        if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY){
+    } else {
+        if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY) {
             goto try_resolv;
         }
-        if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
+        if (tp->flags & DUMMY_ADDRESS_ENTRY) {
             *found = FALSE;
         }
         return tp;
@@ -877,7 +882,7 @@ host_lookup(const guint addr, gboolean *found)
 
 try_resolv:
     if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
-        tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
+        tp->flags |= TRIED_RESOLVE_ADDRESS;
 
 #ifdef ASYNC_DNS
         if (gbl_resolv_flags.concurrent_dns &&
@@ -890,18 +895,32 @@ try_resolv:
             fill_dummy_ip4(addr, tp);
             return tp;
         }
-#endif /* ASYNC_DNS */
 
         /*
          * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
          * the name of the host on which it's running; to work around that
          * botch, we don't try to translate an all-zero IP address to a host
          * name.
+         *
+         * Presumably getaddrinfo() behaves the same way.  Anyway, we should
+         * never get to this code on Windows since those builds include c-ares.
          */
+#elif defined(HAVE_GETADDRINFO)
         if (addr != 0) {
-            /* Use async DNS if possible, else fall back to timeouts,
-             * else call gethostbyaddr and hope for the best
-             */
+            struct sockaddr_in sin;
+
+            memset(&sin, 0, sizeof(sin));
+            sin.sin_family      = AF_INET;
+            sin.sin_addr.s_addr = addr;
+            if (getnameinfo((struct sockaddr *)&sin, sizeof(sin),
+                            tp->name, sizeof(tp->name),
+                            NULL, 0, NI_NAMEREQD) == 0) {
+                return tp;
+            }
+        }
+#elif defined(HAVE_GETHOSTBYNAME)
+        if (addr != 0) {
+            struct hostent *hostp;
 
             hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
 
@@ -910,13 +929,12 @@ try_resolv:
                 return tp;
             }
         }
+#endif
 
         /* unknown host or DNS timeout */
-
     }
 
     *found = FALSE;
-
     fill_dummy_ip4(addr, tp);
     return tp;
 
@@ -929,6 +947,7 @@ new_ipv6(const struct e_in6_addr *addr)
     hashipv6_t *tp = g_new(hashipv6_t,1);
     tp->addr = *addr;
     tp->flags = 0;
+    tp->name[0] = '\0';
     ip6_to_str_buf(addr, tp->ip6);
     return tp;
 }
@@ -941,25 +960,28 @@ host_lookup6(const struct e_in6_addr *addr, gboolean *found)
 #ifdef INET6
 #ifdef HAVE_C_ARES
     async_dns_queue_msg_t *caqm;
-#endif /* HAVE_C_ARES */
+#elif defined(HAVE_GETADDRINFO)
+    struct sockaddr_in6 sin6;
+#elif defined(HAVE_GETHOSTBYNAME)
     struct hostent *hostp;
+#endif
 #endif /* INET6 */
 
     *found = TRUE;
 
     tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
-    if(tp == NULL){
+    if (tp == NULL) {
         struct e_in6_addr *addr_key;
 
         addr_key = g_new(struct e_in6_addr,1);
         tp = new_ipv6(addr);
         memcpy(addr_key, addr, 16);
         g_hash_table_insert(ipv6_hash_table, addr_key, tp);
-    }else{
-        if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY){
+    } else {
+        if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY) {
             goto try_resolv;
         }
-        if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
+        if (tp->flags & DUMMY_ADDRESS_ENTRY) {
             *found = FALSE;
         }
         return tp;
@@ -968,9 +990,8 @@ host_lookup6(const struct e_in6_addr *addr, gboolean *found)
 try_resolv:
     if (gbl_resolv_flags.network_name &&
             gbl_resolv_flags.use_external_net_name_resolver) {
-        tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
+        tp->flags |= TRIED_RESOLVE_ADDRESS;
 #ifdef INET6
-
 #ifdef HAVE_C_ARES
         if ((gbl_resolv_flags.concurrent_dns) &&
                 name_resolve_concurrency > 0 &&
@@ -983,15 +1004,23 @@ try_resolv:
             /* XXX found is set to TRUE, which seems a bit odd, but I'm not
              * going to risk changing the semantics.
              */
-            if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0){
+            if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
                 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
                 ip6_to_str_buf(addr, tp->name);
-                tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
+                tp->flags |= DUMMY_ADDRESS_ENTRY;
             }
             return tp;
         }
-#endif /* HAVE_C_ARES */
-
+#elif defined(HAVE_GETADDRINFO)
+        memset(&sin6, 0, sizeof(sin6));
+        sin6.sin6_family      = AF_INET6;
+        memcpy(sin6.sin6_addr.s6_addr, addr, sizeof(*addr));
+        if (getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
+                        tp->name, sizeof(tp->name),
+                        NULL, 0, NI_NAMEREQD) == 0) {
+            return tp;
+        }
+#elif defined(HAVE_GETHOSTBYNAME)
         /* Quick hack to avoid DNS/YP timeout */
         hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
 
@@ -999,12 +1028,13 @@ try_resolv:
             g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
             return tp;
         }
+#endif
 #endif /* INET6 */
     }
 
     /* unknown host or DNS timeout */
     if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
-        tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
+        tp->flags |= DUMMY_ADDRESS_ENTRY;
         g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
     }
     *found = FALSE;
@@ -1012,34 +1042,6 @@ try_resolv:
 
 } /* host_lookup6 */
 
-static const gchar *
-solve_address_to_name(const address *addr)
-{
-    switch (addr->type) {
-
-        case AT_ETHER:
-            return get_ether_name((const guint8 *)addr->data);
-
-        case AT_IPv4: {
-                          guint32 ip4_addr;
-                          memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
-                          return get_hostname(ip4_addr);
-                      }
-
-        case AT_IPv6: {
-                          struct e_in6_addr ip6_addr;
-                          memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
-                          return get_hostname6(&ip6_addr);
-                      }
-
-        case AT_STRINGZ:
-                      return (const gchar *)addr->data;
-
-        default:
-                      return NULL;
-    }
-}
-
 /*
  * Ethernet / manufacturer resolution
  *
@@ -1383,7 +1385,7 @@ manuf_name_lookup(const guint8 *addr)
      * by the IEEE but the local administrator instead.
      * 0x01 multicast / broadcast bit
      * 0x02 locally administered bit */
-    if((manuf_key & 0x00010000) != 0){
+    if ((manuf_key & 0x00010000) != 0) {
         manuf_key &= 0x00FEFFFF;
         manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
         if (manuf_value != NULL) {
@@ -1404,7 +1406,7 @@ wka_name_lookup(const guint8 *addr, const unsigned int mask)
     gint       i;
     gchar     *name;
 
-    if(wka_hashtable == NULL){
+    if (wka_hashtable == NULL) {
         return NULL;
     }
     /* Get the part of the address covered by the mask. */
@@ -1456,7 +1458,7 @@ initialize_ethers(void)
 {
     ether_t *eth;
     char    *manuf_path;
-    guint    mask;
+    guint    mask = 0;
 
     /* hash table initialization */
     wka_hashtable   = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, g_free, g_free);
@@ -1496,16 +1498,16 @@ static void
 eth_name_lookup_cleanup(void)
 {
 
-    if(manuf_hashtable) {
+    if (manuf_hashtable) {
         g_hash_table_destroy(manuf_hashtable);
         manuf_hashtable = NULL;
     }
-    if(wka_hashtable) {
+    if (wka_hashtable) {
         g_hash_table_destroy(wka_hashtable);
         wka_hashtable = NULL;
     }
 
-    if(eth_hashtable) {
+    if (eth_hashtable) {
         g_hash_table_destroy(eth_hashtable);
         eth_hashtable = NULL;
     }
@@ -1662,7 +1664,7 @@ add_eth_name(const guint8 *addr, const gchar *name)
 
     tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
 
-    if( tp == NULL ){
+    if (tp == NULL) {
         tp = eth_hash_new_entry(addr, FALSE);
     }
 
@@ -1679,10 +1681,10 @@ eth_name_lookup(const guint8 *addr, const gboolean resolve)
     hashether_t  *tp;
 
     tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
-    if( tp == NULL ) {
+    if (tp == NULL) {
         tp = eth_hash_new_entry(addr, resolve);
     } else {
-        if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)){
+        if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
             eth_addr_resolve(tp); /* Found but needs to be resolved */
         }
     }
@@ -1898,7 +1900,7 @@ initialize_ipxnets(void)
 static void
 ipx_name_lookup_cleanup(void)
 {
-    if(ipxnet_hash_table){
+    if (ipxnet_hash_table) {
         g_hash_table_destroy(ipxnet_hash_table);
         ipxnet_hash_table = NULL;
     }
@@ -1912,9 +1914,9 @@ add_ipxnet_name(guint addr, const gchar *name)
     hashipxnet_t *tp;
 
     tp = (hashipxnet_t   *)g_hash_table_lookup(ipxnet_hash_table, &addr);
-    if(tp){
+    if (tp) {
         g_strlcpy(tp->name, name, MAXNAMELEN);
-    }else{
+    } else {
         int *key;
 
         key = (int *)g_new(int, 1);
@@ -1941,14 +1943,14 @@ ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
     ipxnet_t *ipxnet;
 
     tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
-    if(tp == NULL){
+    if (tp == NULL) {
         int *key;
 
         key = (int *)g_new(int, 1);
         *key = addr;
         tp = g_new(hashipxnet_t, 1);
         g_hash_table_insert(ipxnet_hash_table, key, tp);
-    }else{
+    } else {
         return wmem_strdup(allocator, tp->name);
     }
 
@@ -2058,9 +2060,12 @@ read_hosts_file (const char *hostspath, gboolean store_entries)
             } else
                 add_ipv4_name(host_addr[0], cp);
 
+#if 0
             /*
              * Add the aliases, too, if there are any.
-             * XXX - host_lookup() only returns the first entry.
+             * XXX - except we only store the last one added.  The name
+             * resolver returns the first name in the hosts file, we should
+             * too.
              */
             while ((cp = strtok(NULL, " \t")) != NULL) {
                 if (is_ipv6) {
@@ -2069,6 +2074,7 @@ read_hosts_file (const char *hostspath, gboolean store_entries)
                 } else
                     add_ipv4_name(host_addr[0], cp);
             }
+#endif
         }
     }
     g_free(line);
@@ -2150,7 +2156,7 @@ ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user
     addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
     hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
 
-    if((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
+    if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
         lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
     }
 
@@ -2166,7 +2172,7 @@ ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user
     addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
     hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
 
-    if((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
+    if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
         lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
     }
 
@@ -2175,11 +2181,11 @@ ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user
 addrinfo_lists_t *
 get_addrinfo_list(void) {
 
-    if(ipv4_hash_table){
+    if (ipv4_hash_table) {
         g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
     }
 
-    if(ipv6_hash_table){
+    if (ipv6_hash_table) {
         g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
     }
 
@@ -2223,7 +2229,7 @@ read_subnets_file (const char *subnetspath)
 
         /* Expected format is <IP4 address>/<subnet length> */
         cp2 = strchr(cp, '/');
-        if(NULL == cp2) {
+        if (NULL == cp2) {
             /* No length */
             continue;
         }
@@ -2236,7 +2242,7 @@ read_subnets_file (const char *subnetspath)
         }
 
         mask_length = atoi(cp2);
-        if(0 >= mask_length || mask_length > 31) {
+        if (0 >= mask_length || mask_length > 32) {
             continue; /* invalid mask length */
         }
 
@@ -2271,7 +2277,7 @@ subnet_lookup(const guint32 addr)
 
         length_entry = &subnet_length_entries[i];
 
-        if(NULL != length_entry->subnet_addresses) {
+        if (NULL != length_entry->subnet_addresses) {
             sub_net_hashipv4_t * tp;
             guint32 hash_idx;
 
@@ -2283,7 +2289,7 @@ subnet_lookup(const guint32 addr)
                 tp = tp->next;
             }
 
-            if(NULL != tp) {
+            if (NULL != tp) {
                 subnet_entry.mask = length_entry->mask;
                 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
                 subnet_entry.name = tp->name;
@@ -2318,18 +2324,24 @@ subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* na
 
     hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
 
-    if(NULL == entry->subnet_addresses) {
+    if (NULL == entry->subnet_addresses) {
         entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
     }
 
-    if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
-        if(tp->addr == subnet_addr) {
-            return;    /* XXX provide warning that an address was repeated? */
-        } else {
-            sub_net_hashipv4_t * new_tp = g_new(sub_net_hashipv4_t, 1);
-            tp->next = new_tp;
-            tp = new_tp;
+    if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
+        sub_net_hashipv4_t * new_tp;
+
+        while (tp->next) {
+            if (tp->addr == subnet_addr) {
+                return; /* XXX provide warning that an address was repeated? */
+            } else {
+                tp = tp->next;
+            }
         }
+
+        new_tp = g_new(sub_net_hashipv4_t, 1);
+        tp->next = new_tp;
+        tp = new_tp;
     } else {
         tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
     }
@@ -2337,7 +2349,7 @@ subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* na
     tp->next = NULL;
     tp->addr = subnet_addr;
     /* Clear DUMMY_ADDRESS_ENTRY */
-    tp->flags = tp->flags & 0xfe; /*Never used again...*/
+    tp->flags &= ~DUMMY_ADDRESS_ENTRY; /*Never used again...*/
     g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
     have_subnet_entry = TRUE;
 }
@@ -2407,6 +2419,11 @@ addr_resolve_pref_init(module_t *nameres)
             " capture file name resolution blocks and DNS packets in the capture.",
             &gbl_resolv_flags.network_name);
 
+    prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
+            "Use captured DNS packet data for address resolution",
+            "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
+            &gbl_resolv_flags.dns_pkt_addr_resolution);
+
     prefs_register_bool_preference(nameres, "use_external_name_resolver",
             "Use an external network name resolver",
             "Use your system's configured name resolver"
@@ -2446,6 +2463,16 @@ addr_resolve_pref_init(module_t *nameres)
 
 }
 
+void
+disable_name_resolution(void) {
+    gbl_resolv_flags.mac_name                           = FALSE;
+    gbl_resolv_flags.network_name                       = FALSE;
+    gbl_resolv_flags.transport_name                     = FALSE;
+    gbl_resolv_flags.concurrent_dns                     = FALSE;
+    gbl_resolv_flags.dns_pkt_addr_resolution            = FALSE;
+    gbl_resolv_flags.use_external_net_name_resolver     = FALSE;
+}
+
 #ifdef HAVE_C_ARES
 gboolean
 host_name_lookup_process(void) {
@@ -2621,7 +2648,7 @@ get_hostname(const guint addr)
     if (!gbl_resolv_flags.network_name)
         return tp->ip;
 
-    tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
+    tp->flags |= RESOLVED_ADDRESS_USED;
 
     return tp->name;
 }
@@ -2641,7 +2668,7 @@ get_hostname6(const struct e_in6_addr *addr)
     if (!gbl_resolv_flags.network_name)
         return tp->ip6;
 
-    tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
+    tp->flags |= RESOLVED_ADDRESS_USED;
 
     return tp->name;
 }
@@ -2656,22 +2683,21 @@ add_ipv4_name(const guint addr, const gchar *name)
      * Don't add zero-length names; apparently, some resolvers will return
      * them if they get them from DNS.
      */
-    if (name[0] == '\0')
+    if (!name || name[0] == '\0')
         return;
 
 
     tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
-    if(tp){
-        g_strlcpy(tp->name, name, MAXNAMELEN);
-    }else{
+    if (!tp) {
         tp = new_ipv4(addr);
-        g_strlcpy(tp->name, name, MAXNAMELEN);
         g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
     }
 
-    g_strlcpy(tp->name, name, MAXNAMELEN);
-    tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
-    new_resolved_objects = TRUE;
+    if (g_ascii_strcasecmp(tp->name, name)) {
+        g_strlcpy(tp->name, name, MAXNAMELEN);
+        new_resolved_objects = TRUE;
+    }
+    tp->flags |= TRIED_RESOLVE_ADDRESS;
 
 } /* add_ipv4_name */
 
@@ -2685,25 +2711,24 @@ add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
      * Don't add zero-length names; apparently, some resolvers will return
      * them if they get them from DNS.
      */
-    if (name[0] == '\0')
+    if (!name || name[0] == '\0')
         return;
 
     tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
-    if(tp){
-        g_strlcpy(tp->name, name, MAXNAMELEN);
-    }else{
+    if (!tp) {
         struct e_in6_addr *addr_key;
 
         addr_key = g_new(struct e_in6_addr,1);
         tp = new_ipv6(addrp);
         memcpy(addr_key, addrp, 16);
-        g_strlcpy(tp->name, name, MAXNAMELEN);
         g_hash_table_insert(ipv6_hash_table, addr_key, tp);
     }
 
-    g_strlcpy(tp->name, name, MAXNAMELEN);
-    tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
-    new_resolved_objects = TRUE;
+    if (g_ascii_strcasecmp(tp->name, name)) {
+        g_strlcpy(tp->name, name, MAXNAMELEN);
+        new_resolved_objects = TRUE;
+    }
+    tp->flags |= TRIED_RESOLVE_ADDRESS;
 
 } /* add_ipv6_name */
 
@@ -2726,11 +2751,11 @@ add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
 static void
 add_manually_resolved(void)
 {
-    if(manually_resolved_ipv4_list){
+    if (manually_resolved_ipv4_list) {
         g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
     }
 
-    if(manually_resolved_ipv6_list){
+    if (manually_resolved_ipv6_list) {
         g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
     }
 }
@@ -2761,7 +2786,7 @@ host_name_lookup_init(void)
     /*
      * Load the global hosts file, if we have one.
      */
-    if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
+    if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
         hostspath = get_datafile_path(ENAME_HOSTS);
         if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
             report_open_failure(hostspath, errno, FALSE);
@@ -2807,7 +2832,7 @@ host_name_lookup_init(void)
          * XXX - should we base it on the dwPlatformId value from
          * GetVersionEx()?
          */
-        if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
+        if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
             hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
             if (!read_hosts_file(hostspath, TRUE)) {
                 g_free(hostspath);
@@ -2818,7 +2843,7 @@ host_name_lookup_init(void)
         }
     }
 #else /* _WIN32 */
-    if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
+    if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
         read_hosts_file("/etc/hosts", TRUE);
     }
 #endif /* _WIN32 */
@@ -2843,7 +2868,7 @@ host_name_lookup_init(void)
 #endif /* HAVE_GNU_ADNS */
 #endif /* HAVE_C_ARES */
 
-    if(extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only){
+    if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
         for (i = 0; i < extra_hosts_files->len; i++) {
             read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
         }
@@ -2860,17 +2885,17 @@ host_name_lookup_cleanup(void)
     guint32 i, j;
     _host_name_lookup_cleanup();
 
-    if(ipxnet_hash_table){
+    if (ipxnet_hash_table) {
         g_hash_table_destroy(ipxnet_hash_table);
         ipxnet_hash_table = NULL;
     }
 
-    if(ipv4_hash_table){
+    if (ipv4_hash_table) {
         g_hash_table_destroy(ipv4_hash_table);
         ipv4_hash_table = NULL;
     }
 
-    if(ipv6_hash_table){
+    if (ipv6_hash_table) {
         g_hash_table_destroy(ipv6_hash_table);
         ipv6_hash_table = NULL;
     }
@@ -2911,13 +2936,13 @@ free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
 void
 manually_resolve_cleanup(void)
 {
-    if(manually_resolved_ipv4_list){
+    if (manually_resolved_ipv4_list) {
         g_slist_foreach(manually_resolved_ipv4_list, free_manually_resolved_ipv4, NULL);
         g_slist_free(manually_resolved_ipv4_list);
         manually_resolved_ipv4_list = NULL;
     }
 
-    if(manually_resolved_ipv6_list){
+    if (manually_resolved_ipv6_list) {
         g_slist_foreach(manually_resolved_ipv6_list, free_manually_resolved_ipv6, NULL);
         g_slist_free(manually_resolved_ipv6_list);
         manually_resolved_ipv6_list = NULL;
@@ -2973,30 +2998,18 @@ sctp_port_to_display(wmem_allocator_t *allocator, guint port)
 
 } /* sctp_port_to_display */
 
-const gchar *
-address_to_display(wmem_allocator_t *allocator, const address *addr)
+int
+port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type port_typ, guint16 port_num)
 {
-    gchar *str = NULL;
-    const gchar *result = solve_address_to_name(addr);
+    const gchar *port_res_str;
 
-    if (result != NULL) {
-        str = wmem_strdup(allocator, result);
-    }
-    else if (addr->type == AT_NONE) {
-        str = wmem_strdup(allocator, "NONE");
+    if (!gbl_resolv_flags.transport_name ||
+            (port_typ == PT_NONE) ||
+            ((port_res_str = serv_name_lookup(port_num, port_typ)) == NULL)) {
+        /* No name resolution support, just return port string */
+        return g_snprintf(buf, buf_size, "%u", port_num);
     }
-    else {
-        str = (gchar *) wmem_alloc(allocator, MAX_ADDR_STR_LEN);
-        address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
-    }
-
-    return str;
-}
-
-const gchar *
-get_addr_name(const address *addr)
-{
-    return solve_address_to_name(addr);
+    return g_snprintf(buf, buf_size, "%s (%u)", port_res_str, port_num);
 }
 
 gchar *
@@ -3011,6 +3024,12 @@ get_ether_name(const guint8 *addr)
 
 } /* get_ether_name */
 
+gchar *
+tvb_get_ether_name(tvbuff_t *tvb, gint offset)
+{
+    return get_ether_name(tvb_get_ptr(tvb, offset, 6));
+}
+
 /* Look for a (non-dummy) ether name in the hash, and return it if found.
  * If it's not found, simply return NULL.
  */
@@ -3182,18 +3201,22 @@ char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
 const gchar *
 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
 {
-    guint8 *addr = (guint8 *)wmem_alloc(allocator, 8);
+    guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
     hashmanuf_t *manuf_value;
+    const gchar *ret;
 
     /* Copy and convert the address to network byte order. */
     *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
 
     manuf_value = manuf_name_lookup(addr);
     if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
-        return wmem_strdup_printf(allocator, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
+        ret = wmem_strdup_printf(allocator, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
+    } else {
+        ret = wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
     }
-    return wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
 
+    wmem_free(NULL, addr);
+    return ret;
 } /* eui64_to_display */
 
 #ifdef HAVE_C_ARES
@@ -3238,6 +3261,8 @@ get_host_ipaddr(const char *host, guint32 *addrp)
     async_hostent_t ahe;
 #elif defined(HAVE_GETADDRINFO)
     struct addrinfo hint, *result = NULL;
+#elif defined(HAVE_GETHOSTBYNAME)
+    struct hostent *hp;
 #endif
 
     /*
@@ -3304,6 +3329,18 @@ get_host_ipaddr(const char *host, guint32 *addrp)
                 return ret_val;
             }
         }
+#elif defined(HAVE_GETHOSTBYNAME)
+        hp = gethostbyname(host);
+        if (hp == NULL) {
+            /* No. */
+            return FALSE;
+            /* Apparently, some versions of gethostbyaddr can
+             * return IPv6 addresses. */
+         } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
+             memcpy(&ipaddr, hp->h_addr, hp->h_length);
+         } else {
+             return FALSE;
+         }
 #endif
     } else {
         /* Does the string really contain dotted-quad IP?
@@ -3335,6 +3372,8 @@ get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
     async_hostent_t ahe;
 #elif defined(HAVE_GETADDRINFO)
     struct addrinfo hint, *result = NULL;
+#elif defined(HAVE_GETHOSTBYNAME2)
+    struct hostent *hp;
 #endif /* HAVE_C_ARES */
 
     if (str_to_ip6(host, addrp))
@@ -3397,6 +3436,12 @@ get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
             return ret_val;
         }
     }
+#elif defined(HAVE_GETHOSTBYNAME2)
+    hp = gethostbyname2(host, AF_INET6);
+    if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
+        memcpy(addrp, hp->h_addr, hp->h_length);
+        return TRUE;
+    }
 #endif
 
     return FALSE;
@@ -3407,8 +3452,9 @@ get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
  * Return "ip6" if it is IPv6, "ip" otherwise (including the case
  * that we don't know)
  */
-const char* host_ip_af(const char *host
-#ifndef HAVE_GETADDRINFO
+const char *
+host_ip_af(const char *host
+#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETHOSTBYNAME2)
         _U_
 #endif
         )
@@ -3424,6 +3470,9 @@ const char* host_ip_af(const char *host
         }
         freeaddrinfo(result);
     }
+#elif defined(HAVE_GETHOSTBYNAME2)
+    struct hostent *h;
+    return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
 #endif
     return af;
 }