2 * Definitions for network object lookup
4 * Laurent Deniel <laurent.deniel@free.fr>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* The buffers returned by these functions are all allocated with a
25 * packet lifetime and does not have have to be freed.
26 * However, take into account that when the packet dissection
27 * completes, these buffers will be automatically reclaimed/freed.
28 * If you need the buffer to remain for a longer scope than packet lifetime
29 * you must copy the content to an wmem_file_scope() buffer.
35 #include <epan/address.h>
36 #include <epan/tvbuff.h>
37 #include <epan/ipv6.h>
38 #include <epan/to_str.h>
39 #include <wiretap/wtap.h>
40 #include "ws_symbol_export.h"
44 #endif /* __cplusplus */
47 #define MAXNAMELEN 64 /* max name length (hostname and port name) */
50 #ifndef MAXVLANNAMELEN
51 #define MAXVLANNAMELEN 128 /* max vlan name length */
54 #define BASE_ENTERPRISES BASE_CUSTOM
55 #define STRINGS_ENTERPRISES CF_FUNC(enterprises_base_custom)
58 * @brief Flags to control name resolution.
60 typedef struct _e_addr_resolve {
61 gboolean mac_name; /**< Whether to resolve Ethernet MAC to manufacturer names */
62 gboolean network_name; /**< Whether to resolve IPv4, IPv6, and IPX addresses into host names */
63 gboolean transport_name; /**< Whether to resolve TCP/UDP/DCCP/SCTP ports into service names */
64 gboolean dns_pkt_addr_resolution; /**< Whether to resolve addresses using captured DNS packets */
65 gboolean use_external_net_name_resolver; /**< Whether to system's configured DNS server to resolve names */
66 gboolean load_hosts_file_from_profile_only; /**< Whether to only load the hosts in the current profile, not hosts files */
67 gboolean vlan_name; /**< Whether to resolve VLAN IDs to names */
68 gboolean ss7pc_name; /**< Whether to resolve SS7 Point Codes to names */
71 #define ADDR_RESOLV_MACADDR(at) \
72 (((at)->type == AT_ETHER))
74 #define ADDR_RESOLV_NETADDR(at) \
75 (((at)->type == AT_IPv4) || ((at)->type == AT_IPv6) || ((at)->type == AT_IPX))
78 typedef struct hashether hashether_t;
81 typedef struct hashmanuf hashmanuf_t;
83 typedef struct serv_port {
92 * Flags for various IPv4/IPv6 hash table entries.
94 #define TRIED_RESOLVE_ADDRESS (1U<<0) /* XXX - what does this bit *really* mean? */
95 #define NAME_RESOLVED (1U<<1) /* the name field contains a host name, not a printable address */
96 #define RESOLVED_ADDRESS_USED (1U<<2) /* a get_hostname* call returned the host name */
98 #define TRIED_OR_RESOLVED_MASK (TRIED_RESOLVE_ADDRESS | NAME_RESOLVED)
99 #define USED_AND_RESOLVED_MASK (NAME_RESOLVED | RESOLVED_ADDRESS_USED)
102 * Flag controlling what names to resolve.
104 WS_DLL_PUBLIC e_addr_resolve gbl_resolv_flags;
106 /* global variables */
108 extern gchar *g_ethers_path;
109 extern gchar *g_ipxnets_path;
110 extern gchar *g_pethers_path;
111 extern gchar *g_pipxnets_path;
113 /* Functions in addr_resolv.c */
116 * udp_port_to_display() returns the port name corresponding to that UDP port,
117 * or the port number as a string if not found.
119 WS_DLL_PUBLIC gchar *udp_port_to_display(wmem_allocator_t *allocator, guint port);
122 * tcp_port_to_display() returns the port name corresponding to that TCP port,
123 * or the port number as a string if not found.
125 WS_DLL_PUBLIC gchar *tcp_port_to_display(wmem_allocator_t *allocator, guint port);
128 * dccp_port_to_display() returns the port name corresponding to that DCCP port,
129 * or the port number as a string if not found.
131 extern gchar *dccp_port_to_display(wmem_allocator_t *allocator, guint port);
134 * sctp_port_to_display() returns the port name corresponding to that SCTP port,
135 * or the port number as a string if not found.
137 WS_DLL_PUBLIC gchar *sctp_port_to_display(wmem_allocator_t *allocator, guint port);
140 * serv_name_lookup() returns the well known service name string, or numeric
141 * representation if one doesn't exist.
143 WS_DLL_PUBLIC const gchar *serv_name_lookup(port_type proto, guint port);
146 * enterprises_lookup() returns the private enterprise code string, or 'unknown_str'
147 * if one doesn't exist, or "<Unknown>" if that is NULL.
149 WS_DLL_PUBLIC const gchar *enterprises_lookup(guint32 value, const char *unknown_str);
152 * try_enterprises_lookup() returns the private enterprise code string, or NULL if not found.
154 WS_DLL_PUBLIC const gchar *try_enterprises_lookup(guint32 value);
157 * enterprises_base_custom() prints the "name (decimal)" string to 'buf'.
158 * (Used with BASE_CUSTOM field display).
160 WS_DLL_PUBLIC void enterprises_base_custom(char *buf, guint32 value);
163 * try_serv_name_lookup() returns the well known service name string, or NULL if
166 WS_DLL_PUBLIC const gchar *try_serv_name_lookup(port_type proto, guint port);
169 * port_with_resolution_to_str() prints the "<resolved> (<numerical>)" port
172 WS_DLL_PUBLIC gchar *port_with_resolution_to_str(wmem_allocator_t *scope,
173 port_type proto, guint port);
176 * port_with_resolution_to_str_buf() prints the "<resolved> (<numerical>)" port
177 * string to 'buf'. Return value is the same as g_snprintf().
179 WS_DLL_PUBLIC int port_with_resolution_to_str_buf(gchar *buf, gulong buf_size,
180 port_type proto, guint port);
183 * Asynchronous host name lookup initialization, processing, and cleanup
186 /* Setup name resolution preferences */
188 extern void addr_resolve_pref_init(struct pref_module *nameres);
191 * disable_name_resolution() sets all relevant gbl_resolv_flags to FALSE.
193 WS_DLL_PUBLIC void disable_name_resolution(void);
195 /** If we're using c-ares process outstanding host name lookups.
196 * This is called from a GLIB timeout in Wireshark and before processing
197 * each packet in TShark.
199 * @return True if any new objects have been resolved since the previous
200 * call. This can be used to trigger a display update, e.g. in Wireshark.
202 WS_DLL_PUBLIC gboolean host_name_lookup_process(void);
204 /* get_hostname returns the host name or "%d.%d.%d.%d" if not found */
205 WS_DLL_PUBLIC const gchar *get_hostname(const guint addr);
207 /* get_hostname6 returns the host name, or numeric addr if not found */
208 WS_DLL_PUBLIC const gchar *get_hostname6(const ws_in6_addr *ad);
210 /* get_ether_name returns the logical name if found in ethers files else
211 "<vendor>_%02x:%02x:%02x" if the vendor code is known else
212 "%02x:%02x:%02x:%02x:%02x:%02x" */
213 WS_DLL_PUBLIC const gchar *get_ether_name(const guint8 *addr);
215 /* get_hostname_ss7pc returns the logical name if found in ss7pcs file else
216 '\0' on the first call or the unresolved Point Code in the subsequent calls */
217 const gchar *get_hostname_ss7pc(const guint8 ni, const guint32 pc);
219 /* fill_unresolved_ss7pc initializes the unresolved Point Code Address string in the hashtable */
220 void fill_unresolved_ss7pc(const gchar * pc_addr, const guint8 ni, const guint32 pc);
223 /* Same as get_ether_name with tvb support */
224 WS_DLL_PUBLIC const gchar *tvb_get_ether_name(tvbuff_t *tvb, gint offset);
226 /* get_ether_name_if_known returns the logical name if found in ethers files else NULL */
227 const gchar *get_ether_name_if_known(const guint8 *addr);
230 * Given a sequence of 3 octets containing an OID, get_manuf_name()
231 * returns the vendor name, or "%02x:%02x:%02x" if not known.
233 extern const gchar *get_manuf_name(const guint8 *addr);
236 * Given a sequence of 3 octets containing an OID, get_manuf_name_if_known()
237 * returns the vendor name, or NULL if not known.
239 WS_DLL_PUBLIC const gchar *get_manuf_name_if_known(const guint8 *addr);
242 * Given an integer containing a 24-bit OID, uint_get_manuf_name_if_known()
243 * returns the vendor name, or NULL if not known.
245 extern const gchar *uint_get_manuf_name_if_known(const guint oid);
248 * Given a tvbuff and an offset in that tvbuff for a 3-octet OID,
249 * tvb_get_manuf_name() returns the vendor name, or "%02x:%02x:%02x"
252 WS_DLL_PUBLIC const gchar *tvb_get_manuf_name(tvbuff_t *tvb, gint offset);
255 * Given a tvbuff and an offset in that tvbuff for a 3-octet OID,
256 * tvb_get_manuf_name_if_known() returns the vendor name, or NULL
259 WS_DLL_PUBLIC const gchar *tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset);
261 /* eui64_to_display returns "<vendor>_%02x:%02x:%02x:%02x:%02x:%02x" if the vendor code is known
262 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x" */
263 extern gchar *eui64_to_display(wmem_allocator_t *allocator, const guint64 addr);
265 /* get_ipxnet_name returns the logical name if found in an ipxnets file,
266 * or a string formatted with "%X" if not */
267 extern gchar *get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr);
269 /* get_vlan_name returns the logical name if found in a vlans file,
270 * or the VLAN ID itself as a string if not found*/
271 extern gchar *get_vlan_name(wmem_allocator_t *allocator, const guint16 id);
273 WS_DLL_PUBLIC guint get_hash_ether_status(hashether_t* ether);
274 WS_DLL_PUBLIC char* get_hash_ether_hexaddr(hashether_t* ether);
275 WS_DLL_PUBLIC char* get_hash_ether_resolved_name(hashether_t* ether);
277 WS_DLL_PUBLIC char* get_hash_manuf_resolved_name(hashmanuf_t* manuf);
280 /* adds a hostname/IPv4 in the hash table */
281 WS_DLL_PUBLIC void add_ipv4_name(const guint addr, const gchar *name);
283 /* adds a hostname/IPv6 in the hash table */
284 WS_DLL_PUBLIC void add_ipv6_name(const ws_in6_addr *addr, const gchar *name);
286 /** Add an additional "hosts" file for IPv4 and IPv6 name resolution.
288 * The file can be added before host_name_lookup_init() is called and
289 * will be re-read each time host_name_lookup_init() is called.
291 * @param hosts_file Absolute path to the hosts file.
293 * @return TRUE if the hosts file can be read.
295 WS_DLL_PUBLIC gboolean add_hosts_file (const char *hosts_file);
297 /* adds a hostname in the hash table */
298 WS_DLL_PUBLIC gboolean add_ip_name_from_string (const char *addr, const char *name);
301 /** Get lists of host name to address mappings we know about.
303 * The struct contains two g_lists one with hashipv4_t entries and one with hashipv6_t entries.
305 * @return a struct with lists of known addresses(IPv4 and IPv6). May be NULL.
307 WS_DLL_PUBLIC addrinfo_lists_t *get_addrinfo_list(void);
309 /* add ethernet address / name corresponding to IP address */
310 extern void add_ether_byip(const guint ip, const guint8 *eth);
312 /** Translates a string representing a hostname or dotted-decimal IPv4 address
313 * into a numeric IPv4 address value in network byte order. If compiled with
314 * c-ares, the request will wait a maximum of 250ms for the request to finish.
315 * Otherwise the wait time will be system-dependent, ususally much longer.
316 * Immediately returns FALSE for hostnames if network name resolution is
319 * @param[in] host The hostname.
320 * @param[out] addrp The numeric IPv4 address in network byte order.
321 * @return TRUE on success, FALSE on failure, timeout.
324 gboolean get_host_ipaddr(const char *host, guint32 *addrp);
326 /** Translates a string representing a hostname or colon-hex IPv6 address
327 * into a numeric IPv6 address value in network byte order. If compiled with
328 * c-ares, the request will wait a maximum of 250ms for the request to finish.
329 * Otherwise the wait time will be system-dependent, usually much longer.
330 * Immediately returns FALSE for hostnames if network name resolution is
333 * @param[in] host The hostname.
334 * @param[out] addrp The numeric IPv6 address in network byte order.
335 * @return TRUE on success, FALSE on failure or timeout.
338 gboolean get_host_ipaddr6(const char *host, ws_in6_addr *addrp);
341 wmem_map_t *get_manuf_hashtable(void);
344 wmem_map_t *get_wka_hashtable(void);
347 wmem_map_t *get_eth_hashtable(void);
350 wmem_map_t *get_serv_port_hashtable(void);
353 wmem_map_t *get_ipxnet_hash_table(void);
356 wmem_map_t *get_vlan_hash_table(void);
359 wmem_map_t *get_ipv4_hash_table(void);
362 wmem_map_t *get_ipv6_hash_table(void);
365 * private functions (should only be called by epan directly)
369 void name_resolver_init(void);
371 /* (Re)Initialize hostname resolution subsystem */
373 void host_name_lookup_init(void);
375 /* Clean up only hostname resolutions (so they don't "leak" from one
379 void host_name_lookup_cleanup(void);
382 void addr_resolv_init(void);
385 void addr_resolv_cleanup(void);
388 void manually_resolve_cleanup(void);
391 gboolean str_to_ip(const char *str, void *dst);
394 gboolean str_to_ip6(const char *str, void *dst);
398 #endif /* __cplusplus */
400 #endif /* __RESOLV_H__ */