Snort: speed up parsing of options by avoiding g_snprintf()
[metze/wireshark/wip.git] / epan / addr_resolv.h
1 /* addr_resolv.h
2  * Definitions for network object lookup
3  *
4  * Laurent Deniel <laurent.deniel@free.fr>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
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.
30  */
31
32 #ifndef __RESOLV_H__
33 #define __RESOLV_H__
34
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"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif /* __cplusplus */
45
46 #ifndef MAXNAMELEN
47 #define MAXNAMELEN      64      /* max name length (hostname and port name) */
48 #endif
49
50 #ifndef MAXVLANNAMELEN
51 #define MAXVLANNAMELEN          128     /* max vlan name length */
52 #endif
53
54 #define BASE_ENTERPRISES     BASE_CUSTOM
55 #define STRINGS_ENTERPRISES  CF_FUNC(enterprises_base_custom)
56
57 /**
58  * @brief Flags to control name resolution.
59  */
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 */
69 } e_addr_resolve;
70
71 #define ADDR_RESOLV_MACADDR(at) \
72     (((at)->type == AT_ETHER))
73
74 #define ADDR_RESOLV_NETADDR(at) \
75     (((at)->type == AT_IPv4) || ((at)->type == AT_IPv6) || ((at)->type == AT_IPX))
76
77 struct hashether;
78 typedef struct hashether hashether_t;
79
80 struct hashmanuf;
81 typedef struct hashmanuf hashmanuf_t;
82
83 typedef struct serv_port {
84   gchar            *udp_name;
85   gchar            *tcp_name;
86   gchar            *sctp_name;
87   gchar            *dccp_name;
88   gchar            *numeric;
89 } serv_port_t;
90
91 /*
92  * Flags for various IPv4/IPv6 hash table entries.
93  */
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 */
97
98 #define TRIED_OR_RESOLVED_MASK   (TRIED_RESOLVE_ADDRESS | NAME_RESOLVED)
99 #define USED_AND_RESOLVED_MASK   (NAME_RESOLVED | RESOLVED_ADDRESS_USED)
100
101 /*
102  * Flag controlling what names to resolve.
103  */
104 WS_DLL_PUBLIC e_addr_resolve gbl_resolv_flags;
105
106 /* global variables */
107
108 extern gchar *g_ethers_path;
109 extern gchar *g_ipxnets_path;
110 extern gchar *g_pethers_path;
111 extern gchar *g_pipxnets_path;
112
113 /* Functions in addr_resolv.c */
114
115 /*
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.
118  */
119 WS_DLL_PUBLIC gchar *udp_port_to_display(wmem_allocator_t *allocator, guint port);
120
121 /*
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.
124  */
125 WS_DLL_PUBLIC gchar *tcp_port_to_display(wmem_allocator_t *allocator, guint port);
126
127 /*
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.
130  */
131 extern gchar *dccp_port_to_display(wmem_allocator_t *allocator, guint port);
132
133 /*
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.
136  */
137 WS_DLL_PUBLIC gchar *sctp_port_to_display(wmem_allocator_t *allocator, guint port);
138
139 /*
140  * serv_name_lookup() returns the well known service name string, or numeric
141  * representation if one doesn't exist.
142  */
143 WS_DLL_PUBLIC const gchar *serv_name_lookup(port_type proto, guint port);
144
145 /*
146  * enterprises_lookup() returns the private enterprise code string, or 'unknown_str'
147  * if one doesn't exist, or "<Unknown>" if that is NULL.
148  */
149 WS_DLL_PUBLIC const gchar *enterprises_lookup(guint32 value, const char *unknown_str);
150
151 /*
152  * try_enterprises_lookup() returns the private enterprise code string, or NULL if not found.
153  */
154 WS_DLL_PUBLIC const gchar *try_enterprises_lookup(guint32 value);
155
156 /*
157  * enterprises_base_custom() prints the "name (decimal)" string to 'buf'.
158  * (Used with BASE_CUSTOM field display).
159  */
160 WS_DLL_PUBLIC void enterprises_base_custom(char *buf, guint32 value);
161
162 /*
163  * try_serv_name_lookup() returns the well known service name string, or NULL if
164  * one doesn't exist.
165  */
166 WS_DLL_PUBLIC const gchar *try_serv_name_lookup(port_type proto, guint port);
167
168 /*
169  * port_with_resolution_to_str() prints the "<resolved> (<numerical>)" port
170  * string.
171  */
172 WS_DLL_PUBLIC gchar *port_with_resolution_to_str(wmem_allocator_t *scope,
173                                         port_type proto, guint port);
174
175 /*
176  * port_with_resolution_to_str_buf() prints the "<resolved> (<numerical>)" port
177  * string to 'buf'. Return value is the same as g_snprintf().
178  */
179 WS_DLL_PUBLIC int port_with_resolution_to_str_buf(gchar *buf, gulong buf_size,
180                                         port_type proto, guint port);
181
182 /*
183  * Asynchronous host name lookup initialization, processing, and cleanup
184  */
185
186 /* Setup name resolution preferences */
187 struct pref_module;
188 extern void addr_resolve_pref_init(struct pref_module *nameres);
189
190 /*
191  * disable_name_resolution() sets all relevant gbl_resolv_flags to FALSE.
192  */
193 WS_DLL_PUBLIC void disable_name_resolution(void);
194
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.
198  *
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.
201  */
202 WS_DLL_PUBLIC gboolean host_name_lookup_process(void);
203
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);
206
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);
209
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);
214
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);
218
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);
221
222
223 /* Same as get_ether_name with tvb support */
224 WS_DLL_PUBLIC const gchar *tvb_get_ether_name(tvbuff_t *tvb, gint offset);
225
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);
228
229 /*
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.
232  */
233 extern const gchar *get_manuf_name(const guint8 *addr);
234
235 /*
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.
238  */
239 WS_DLL_PUBLIC const gchar *get_manuf_name_if_known(const guint8 *addr);
240
241 /*
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.
244  */
245 extern const gchar *uint_get_manuf_name_if_known(const guint oid);
246
247 /*
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"
250  * if not known.
251  */
252 WS_DLL_PUBLIC const gchar *tvb_get_manuf_name(tvbuff_t *tvb, gint offset);
253
254 /*
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
257  * if not known.
258  */
259 WS_DLL_PUBLIC const gchar *tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset);
260
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);
264
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);
268
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);
272
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);
276
277 WS_DLL_PUBLIC char* get_hash_manuf_resolved_name(hashmanuf_t* manuf);
278
279
280 /* adds a hostname/IPv4 in the hash table */
281 WS_DLL_PUBLIC void add_ipv4_name(const guint addr, const gchar *name);
282
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);
285
286 /** Add an additional "hosts" file for IPv4 and IPv6 name resolution.
287  *
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.
290  *
291  * @param hosts_file Absolute path to the hosts file.
292  *
293  * @return TRUE if the hosts file can be read.
294  */
295 WS_DLL_PUBLIC gboolean add_hosts_file (const char *hosts_file);
296
297 /* adds a hostname in the hash table */
298 WS_DLL_PUBLIC gboolean add_ip_name_from_string (const char *addr, const char *name);
299
300
301 /** Get lists of host name to address mappings we know about.
302  *
303  * The struct contains two g_lists one with hashipv4_t entries and one with hashipv6_t entries.
304  *
305  * @return a struct with lists of known addresses(IPv4 and IPv6). May be NULL.
306  */
307 WS_DLL_PUBLIC addrinfo_lists_t *get_addrinfo_list(void);
308
309 /* add ethernet address / name corresponding to IP address  */
310 extern void add_ether_byip(const guint ip, const guint8 *eth);
311
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
317  *  disabled.
318  *
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.
322  */
323 WS_DLL_PUBLIC
324 gboolean get_host_ipaddr(const char *host, guint32 *addrp);
325
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
331  *  disabled.
332  *
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.
336  */
337 WS_DLL_PUBLIC
338 gboolean get_host_ipaddr6(const char *host, ws_in6_addr *addrp);
339
340 WS_DLL_PUBLIC
341 wmem_map_t *get_manuf_hashtable(void);
342
343 WS_DLL_PUBLIC
344 wmem_map_t *get_wka_hashtable(void);
345
346 WS_DLL_PUBLIC
347 wmem_map_t *get_eth_hashtable(void);
348
349 WS_DLL_PUBLIC
350 wmem_map_t *get_serv_port_hashtable(void);
351
352 WS_DLL_PUBLIC
353 wmem_map_t *get_ipxnet_hash_table(void);
354
355 WS_DLL_PUBLIC
356 wmem_map_t *get_vlan_hash_table(void);
357
358 WS_DLL_PUBLIC
359 wmem_map_t *get_ipv4_hash_table(void);
360
361 WS_DLL_PUBLIC
362 wmem_map_t *get_ipv6_hash_table(void);
363
364 /*
365  * private functions (should only be called by epan directly)
366  */
367
368 WS_DLL_LOCAL
369 void name_resolver_init(void);
370
371 /* (Re)Initialize hostname resolution subsystem */
372 WS_DLL_LOCAL
373 void host_name_lookup_init(void);
374
375 /* Clean up only hostname resolutions (so they don't "leak" from one
376  * file to the next).
377  */
378 WS_DLL_LOCAL
379 void host_name_lookup_cleanup(void);
380
381 WS_DLL_LOCAL
382 void addr_resolv_init(void);
383
384 WS_DLL_LOCAL
385 void addr_resolv_cleanup(void);
386
387 WS_DLL_PUBLIC
388 void manually_resolve_cleanup(void);
389
390 WS_DLL_PUBLIC
391 gboolean str_to_ip(const char *str, void *dst);
392
393 WS_DLL_PUBLIC
394 gboolean str_to_ip6(const char *str, void *dst);
395
396 #ifdef __cplusplus
397 }
398 #endif /* __cplusplus */
399
400 #endif /* __RESOLV_H__ */