addr_resolv: add support for static hostname entries

Ping #18075
This commit is contained in:
Chuck Craft 2022-05-05 14:27:41 -05:00 committed by AndersBroman
parent 2578e2a1fb
commit 8de2b2e860
7 changed files with 29 additions and 24 deletions

View File

@ -1135,13 +1135,13 @@ cleanup_capture_info(capture_info *cf_info)
} }
static void static void
count_ipv4_address(const guint addr _U_, const gchar *name _U_) count_ipv4_address(const guint addr _U_, const gchar *name _U_, const gboolean static_entry _U_)
{ {
num_ipv4_addresses++; num_ipv4_addresses++;
} }
static void static void
count_ipv6_address(const void *addrp _U_, const gchar *name _U_) count_ipv6_address(const void *addrp _U_, const gchar *name _U_, const gboolean static_entry _U_)
{ {
num_ipv6_addresses++; num_ipv6_addresses++;
} }

View File

@ -430,10 +430,10 @@ c_ares_ghba_sync_cb(void *arg, int status, int timeouts _U_, struct hostent *he)
for (p = he->h_addr_list; *p != NULL; p++) { for (p = he->h_addr_list; *p != NULL; p++) {
switch(sdd->family) { switch(sdd->family) {
case AF_INET: case AF_INET:
add_ipv4_name(sdd->addr.ip4, he->h_name); add_ipv4_name(sdd->addr.ip4, he->h_name, FALSE);
break; break;
case AF_INET6: case AF_INET6:
add_ipv6_name(&sdd->addr.ip6, he->h_name); add_ipv6_name(&sdd->addr.ip6, he->h_name, FALSE);
break; break;
default: default:
/* Throw an exception? */ /* Throw an exception? */
@ -1069,10 +1069,10 @@ c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
for (p = he->h_addr_list; *p != NULL; p++) { for (p = he->h_addr_list; *p != NULL; p++) {
switch(caqm->family) { switch(caqm->family) {
case AF_INET: case AF_INET:
add_ipv4_name(caqm->addr.ip4, he->h_name); add_ipv4_name(caqm->addr.ip4, he->h_name, FALSE);
break; break;
case AF_INET6: case AF_INET6:
add_ipv6_name(&caqm->addr.ip6, he->h_name); add_ipv6_name(&caqm->addr.ip6, he->h_name, FALSE);
break; break;
default: default:
/* Throw an exception? */ /* Throw an exception? */
@ -2337,9 +2337,9 @@ read_hosts_file (const char *hostspath, gboolean store_entries)
entry_found = TRUE; entry_found = TRUE;
if (store_entries) { if (store_entries) {
if (is_ipv6) { if (is_ipv6) {
add_ipv6_name(&host_addr.ip6_addr, cp); add_ipv6_name(&host_addr.ip6_addr, cp, TRUE);
} else { } else {
add_ipv4_name(host_addr.ip4_addr, cp); add_ipv4_name(host_addr.ip4_addr, cp, TRUE);
} }
} }
} }
@ -3048,7 +3048,7 @@ get_hostname6(const ws_in6_addr *addr)
/* -------------------------- */ /* -------------------------- */
void void
add_ipv4_name(const guint addr, const gchar *name) add_ipv4_name(const guint addr, const gchar *name, gboolean static_entry)
{ {
hashipv4_t *tp; hashipv4_t *tp;
@ -3065,16 +3065,18 @@ add_ipv4_name(const guint addr, const gchar *name)
wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp); wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
} }
if (g_ascii_strcasecmp(tp->name, name)) { if (g_ascii_strcasecmp(tp->name, name) && (static_entry || !(tp->flags & STATIC_HOSTNAME))) {
(void) g_strlcpy(tp->name, name, MAXNAMELEN); (void) g_strlcpy(tp->name, name, MAXNAMELEN);
new_resolved_objects = TRUE; new_resolved_objects = TRUE;
if (static_entry)
tp->flags |= STATIC_HOSTNAME;
} }
tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED; tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
} /* add_ipv4_name */ } /* add_ipv4_name */
/* -------------------------- */ /* -------------------------- */
void void
add_ipv6_name(const ws_in6_addr *addrp, const gchar *name) add_ipv6_name(const ws_in6_addr *addrp, const gchar *name, const gboolean static_entry)
{ {
hashipv6_t *tp; hashipv6_t *tp;
@ -3095,9 +3097,11 @@ add_ipv6_name(const ws_in6_addr *addrp, const gchar *name)
wmem_map_insert(ipv6_hash_table, addr_key, tp); wmem_map_insert(ipv6_hash_table, addr_key, tp);
} }
if (g_ascii_strcasecmp(tp->name, name)) { if (g_ascii_strcasecmp(tp->name, name) && (static_entry || !(tp->flags & STATIC_HOSTNAME))) {
(void) g_strlcpy(tp->name, name, MAXNAMELEN); (void) g_strlcpy(tp->name, name, MAXNAMELEN);
new_resolved_objects = TRUE; new_resolved_objects = TRUE;
if (static_entry)
tp->flags |= STATIC_HOSTNAME;
} }
tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED; tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
} /* add_ipv6_name */ } /* add_ipv6_name */
@ -3106,14 +3110,14 @@ static void
add_manually_resolved_ipv4(gpointer key, gpointer value, gpointer user_data _U_) add_manually_resolved_ipv4(gpointer key, gpointer value, gpointer user_data _U_)
{ {
resolved_name_t *resolved_ipv4_entry = (resolved_name_t*)value; resolved_name_t *resolved_ipv4_entry = (resolved_name_t*)value;
add_ipv4_name(GPOINTER_TO_UINT(key), resolved_ipv4_entry->name); add_ipv4_name(GPOINTER_TO_UINT(key), resolved_ipv4_entry->name, TRUE);
} }
static void static void
add_manually_resolved_ipv6(gpointer key, gpointer value, gpointer user_data _U_) add_manually_resolved_ipv6(gpointer key, gpointer value, gpointer user_data _U_)
{ {
resolved_name_t *resolved_ipv6_entry = (resolved_name_t*)value; resolved_name_t *resolved_ipv6_entry = (resolved_name_t*)value;
add_ipv6_name((ws_in6_addr*)key, resolved_ipv6_entry->name); add_ipv6_name((ws_in6_addr*)key, resolved_ipv6_entry->name, TRUE);
} }
static void static void

View File

@ -86,6 +86,7 @@ typedef struct _resolved_name {
#define TRIED_RESOLVE_ADDRESS (1U<<0) /* XXX - what does this bit *really* mean? */ #define TRIED_RESOLVE_ADDRESS (1U<<0) /* XXX - what does this bit *really* mean? */
#define NAME_RESOLVED (1U<<1) /* the name field contains a host name, not a printable address */ #define NAME_RESOLVED (1U<<1) /* the name field contains a host name, not a printable address */
#define RESOLVED_ADDRESS_USED (1U<<2) /* a get_hostname* call returned the host name */ #define RESOLVED_ADDRESS_USED (1U<<2) /* a get_hostname* call returned the host name */
#define STATIC_HOSTNAME (1U<<3) /* do not update entries from hosts file with DNS responses */
#define TRIED_OR_RESOLVED_MASK (TRIED_RESOLVE_ADDRESS | NAME_RESOLVED) #define TRIED_OR_RESOLVED_MASK (TRIED_RESOLVE_ADDRESS | NAME_RESOLVED)
#define USED_AND_RESOLVED_MASK (NAME_RESOLVED | RESOLVED_ADDRESS_USED) #define USED_AND_RESOLVED_MASK (NAME_RESOLVED | RESOLVED_ADDRESS_USED)
@ -271,10 +272,10 @@ WS_DLL_PUBLIC char* get_hash_manuf_resolved_name(hashmanuf_t* manuf);
/* adds a hostname/IPv4 in the hash table */ /* adds a hostname/IPv4 in the hash table */
WS_DLL_PUBLIC void add_ipv4_name(const guint addr, const gchar *name); WS_DLL_PUBLIC void add_ipv4_name(const guint addr, const gchar *name, const gboolean static_entry);
/* adds a hostname/IPv6 in the hash table */ /* adds a hostname/IPv6 in the hash table */
WS_DLL_PUBLIC void add_ipv6_name(const ws_in6_addr *addr, const gchar *name); WS_DLL_PUBLIC void add_ipv6_name(const ws_in6_addr *addr, const gchar *name, const gboolean static_entry);
/** Add an additional "hosts" file for IPv4 and IPv6 name resolution. /** Add an additional "hosts" file for IPv4 and IPv6 name resolution.
* *

View File

@ -2101,7 +2101,7 @@ dissect_dns_answer(tvbuff_t *tvb, int offsetx, int dns_data_offset,
!PINFO_FD_VISITED(pinfo)) { !PINFO_FD_VISITED(pinfo)) {
guint32 addr_int; guint32 addr_int;
tvb_memcpy(tvb, &addr_int, cur_offset, sizeof(addr_int)); tvb_memcpy(tvb, &addr_int, cur_offset, sizeof(addr_int));
add_ipv4_name(addr_int, name); add_ipv4_name(addr_int, name, FALSE);
} }
} }
break; break;
@ -2636,7 +2636,7 @@ dissect_dns_answer(tvbuff_t *tvb, int offsetx, int dns_data_offset,
!PINFO_FD_VISITED(pinfo)) { !PINFO_FD_VISITED(pinfo)) {
ws_in6_addr addr_in6; ws_in6_addr addr_in6;
tvb_memcpy(tvb, &addr_in6, cur_offset, sizeof(addr_in6)); tvb_memcpy(tvb, &addr_in6, cur_offset, sizeof(addr_in6));
add_ipv6_name(&addr_in6, name); add_ipv6_name(&addr_in6, name, FALSE);
} }
} }
break; break;

View File

@ -190,7 +190,7 @@ static int CaptureInfo_set_hosts(lua_State* L) {
} }
name = luaL_checklstring(L,-1,&name_len); name = luaL_checklstring(L,-1,&name_len);
wth->add_new_ipv4(v4_addr, name); wth->add_new_ipv4(v4_addr, name, FALSE);
/* removes 'value'; keeps 'key' for next iteration */ /* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1); lua_pop(L, 1);
@ -233,7 +233,7 @@ static int CaptureInfo_set_hosts(lua_State* L) {
} }
name = luaL_checklstring(L,-1,&name_len); name = luaL_checklstring(L,-1,&name_len);
wth->add_new_ipv6((const void *)(&v6_addr), name); wth->add_new_ipv6((const void *)(&v6_addr), name, FALSE);
/* removes 'value'; keeps 'key' for next iteration */ /* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1); lua_pop(L, 1);

View File

@ -2427,7 +2427,7 @@ pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh,
ws_buffer_free(&nrb_rec); ws_buffer_free(&nrb_rec);
return FALSE; /* fail */ return FALSE; /* fail */
} }
pn->add_new_ipv4(v4_addr, namep); pn->add_new_ipv4(v4_addr, namep, FALSE);
} }
} }
@ -2488,7 +2488,7 @@ pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh,
return FALSE; /* fail */ return FALSE; /* fail */
} }
pn->add_new_ipv6(ws_buffer_start_ptr(&nrb_rec), pn->add_new_ipv6(ws_buffer_start_ptr(&nrb_rec),
namep); namep, FALSE);
} }
} }

View File

@ -1750,11 +1750,11 @@ void wtap_cleareof(wtap *wth);
* Set callback functions to add new hostnames. Currently pcapng-only. * Set callback functions to add new hostnames. Currently pcapng-only.
* MUST match add_ipv4_name and add_ipv6_name in addr_resolv.c. * MUST match add_ipv4_name and add_ipv6_name in addr_resolv.c.
*/ */
typedef void (*wtap_new_ipv4_callback_t) (const guint addr, const gchar *name); typedef void (*wtap_new_ipv4_callback_t) (const guint addr, const gchar *name, const gboolean static_entry);
WS_DLL_PUBLIC WS_DLL_PUBLIC
void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4); void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4);
typedef void (*wtap_new_ipv6_callback_t) (const void *addrp, const gchar *name); typedef void (*wtap_new_ipv6_callback_t) (const void *addrp, const gchar *name, const gboolean static_entry);
WS_DLL_PUBLIC WS_DLL_PUBLIC
void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6); void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6);