Bug Summary

File:epan/addr_resolv.c
Warning:line 3330, column 42
An undefined value may be read from 'errno'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name addr_resolv.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-20/lib/clang/20 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/libxml2 -isystem /usr/include/lua5.4 -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-20/lib/clang/20/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-truncation -Wno-format-nonliteral -Wno-pointer-sign -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2025-09-10-100354-3933-1 -x c /builds/wireshark/wireshark/epan/addr_resolv.c
1/* addr_resolv.c
2 * Routines for network object lookup
3 *
4 * Laurent Deniel <[email protected]>
5 *
6 * Add option to resolv VLAN ID to describing name
7 * Uli Heilmeier, March 2016
8 *
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <[email protected]>
11 * Copyright 1998 Gerald Combs
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 */
15
16#include "config.h"
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <errno(*__errno_location ()).h>
22
23#include <wsutil/strtoi.h>
24#include <wsutil/ws_assert.h>
25
26#include "enterprises.h"
27#include "manuf.h"
28
29/*
30 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
31 * are most likely to take a long time, given the way address-to-name
32 * lookups are done over NBNS).
33 *
34 * macOS does have SIGALRM, but if you longjmp() out of a name resolution
35 * call in a signal handler, you might crash, because the state of the
36 * resolution code that sends messages to lookupd might be inconsistent
37 * if you jump out of it in middle of a call.
38 *
39 * There's no guarantee that longjmp()ing out of name resolution calls
40 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
41 * code in tcpdump, to avoid those sorts of problems, and that was
42 * picked up by tcpdump.org tcpdump.
43 *
44 * So, for now, we do not use alarm() and SIGALRM to time out host name
45 * lookups. If we get a lot of complaints about lookups taking a long time,
46 * we can reconsider that decision. (Note that tcpdump originally added
47 * such a timeout mechanism that for the benefit of systems using NIS to
48 * look up host names; that might now be fixed in NIS implementations, for
49 * those sites still using NIS rather than DNS for that.... tcpdump no
50 * longer does that, for the same reasons that we don't.)
51 *
52 * If we're using an asynchronous DNS resolver, that shouldn't be an issue.
53 * If we're using a synchronous name lookup mechanism (which we'd do mainly
54 * to support resolving addresses and host names using more mechanisms than
55 * just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
56 * a separate thread, making it, in effect, asynchronous.
57 */
58
59#ifdef HAVE_NETINET_IN_H1
60# include <netinet/in.h>
61#endif
62
63#ifdef HAVE_NETDB_H1
64#include <netdb.h>
65#endif
66
67#ifdef HAVE_SYS_SOCKET_H1
68#include <sys/socket.h> /* needed to define AF_ values on UNIX */
69#endif
70
71#ifdef _WIN32
72#include <winsock2.h> /* needed to define AF_ values on Windows */
73#include <ws2tcpip.h>
74#endif
75
76#ifdef _WIN32
77# define socklen_t unsigned int
78#endif
79#include <ares.h>
80#include <ares_version.h>
81
82#include <glib.h>
83
84#include "packet.h"
85#include "addr_resolv.h"
86#include "wsutil/filesystem.h"
87
88#include <wsutil/report_message.h>
89#include <wsutil/file_util.h>
90#include <wsutil/pint.h>
91#include <wsutil/inet_cidr.h>
92
93#include <epan/strutil.h>
94#include <epan/to_str.h>
95#include <epan/maxmind_db.h>
96#include <epan/prefs.h>
97#include <epan/uat.h>
98#include "services.h"
99
100#define ENAME_HOSTS"hosts" "hosts"
101#define ENAME_SUBNETS"subnets" "subnets"
102#define ENAME_ETHERS"ethers" "ethers"
103#define ENAME_IPXNETS"ipxnets" "ipxnets"
104#define ENAME_MANUF"manuf" "manuf"
105#define ENAME_WKA"wka" "wka"
106#define ENAME_SERVICES"services" "services"
107#define ENAME_VLANS"vlans" "vlans"
108#define ENAME_SS7PCS"ss7pcs" "ss7pcs"
109#define ENAME_ENTERPRISES"enterprises" "enterprises"
110
111#define HASHETHSIZE2048 2048
112#define HASHHOSTSIZE2048 2048
113#define HASHIPXNETSIZE256 256
114#define SUBNETLENGTHSIZE32 32 /*1-32 inc.*/
115
116/* hash table used for IPv4 lookup */
117
118#define HASH_IPV4_ADDRESS(addr)((((((guint32) ( (((guint32) (addr) & (guint32) 0x000000ffU
) << 24) | (((guint32) (addr) & (guint32) 0x0000ff00U
) << 8) | (((guint32) (addr) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (addr) & (guint32) 0xff000000U
) >> 24)))))) & (2048 - 1))
(g_htonl(addr)(((((guint32) ( (((guint32) (addr) & (guint32) 0x000000ffU
) << 24) | (((guint32) (addr) & (guint32) 0x0000ff00U
) << 8) | (((guint32) (addr) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (addr) & (guint32) 0xff000000U
) >> 24))))))
& (HASHHOSTSIZE2048 - 1))
119
120
121typedef struct sub_net_hashipv4 {
122 unsigned addr;
123 /* XXX: No longer needed?*/
124 uint8_t flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
125 struct sub_net_hashipv4 *next;
126 char name[MAXNAMELEN64];
127} sub_net_hashipv4_t;
128
129/* Array of entries of subnets of different lengths */
130typedef struct {
131 size_t mask_length; /*1-32*/
132 uint32_t mask; /* e.g. 255.255.255.*/
133 sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
134} subnet_length_entry_t;
135
136
137/* hash table used for IPX network lookup */
138
139/* XXX - check goodness of hash function */
140
141#define HASH_IPX_NET(net)((net) & (256 - 1)) ((net) & (HASHIPXNETSIZE256 - 1))
142
143typedef struct hashipxnet {
144 unsigned addr;
145 struct hashipxnet *next;
146 char name[MAXNAMELEN64];
147} hashipxnet_t;
148
149typedef struct hashvlan {
150 unsigned id;
151/* struct hashvlan *next; */
152 char name[MAXVLANNAMELEN128];
153} hashvlan_t;
154
155typedef struct ss7pc {
156 uint32_t id; /* 1st byte NI, 3 following bytes: Point Code */
157 char pc_addr[MAXNAMELEN64];
158 char name[MAXNAMELEN64];
159} hashss7pc_t;
160
161/* hash tables used for ethernet and manufacturer lookup */
162struct hashether {
163 uint8_t flags; /* (See above) */
164 uint8_t addr[6];
165 char hexaddr[6*3];
166 char resolved_name[MAXNAMELEN64];
167};
168
169struct hasheui64 {
170 uint8_t flags; /* (See above) */
171 uint8_t addr[EUI64_ADDR_LEN8];
172 char hexaddr[EUI64_ADDR_LEN8*3];
173 char resolved_name[MAXNAMELEN64];
174};
175
176struct hashwka {
177 uint8_t flags; /* (See above) */
178 char* name;
179};
180
181struct hashmanuf {
182 uint8_t flags; /* (See above) */
183 uint8_t addr[3];
184 char hexaddr[3*3];
185 char resolved_name[MAXNAMELEN64];
186 char resolved_longname[MAXNAMELEN64];
187};
188
189/* internal type used when reading ethers file (or wka, manuf) */
190typedef struct _ether
191{
192 uint8_t addr[8];
193 char name[MAXNAMELEN64];
194 char longname[MAXNAMELEN64];
195} ether_t;
196
197/* internal ipxnet type */
198typedef struct _ipxnet
199{
200 unsigned addr;
201 char name[MAXNAMELEN64];
202} ipxnet_t;
203
204/* internal vlan type */
205typedef struct _vlan
206{
207 unsigned id;
208 char name[MAXVLANNAMELEN128];
209} vlan_t;
210
211static wmem_allocator_t *addr_resolv_scope;
212
213// Maps unsigned -> hashipxnet_t*
214static wmem_map_t *ipxnet_hash_table;
215static wmem_map_t *ipv4_hash_table;
216static wmem_map_t *ipv6_hash_table;
217// Maps unsigned -> hashvlan_t*
218static wmem_map_t *vlan_hash_table;
219static wmem_map_t *ss7pc_hash_table;
220
221// Maps IP address -> manually set hostname.
222static wmem_map_t *manually_resolved_ipv4_list;
223static wmem_map_t *manually_resolved_ipv6_list;
224
225static addrinfo_lists_t addrinfo_lists;
226
227struct cb_serv_data {
228 char *service;
229 port_type proto;
230};
231
232// Maps unsigned -> hashmanuf_t*
233// XXX: Note that hashmanuf_t* only accommodates 24-bit OUIs.
234// We might want to store vendor names from MA-M and MA-S to
235// present in the Resolved Addresses dialog.
236static wmem_map_t *manuf_hashtable;
237// Maps address -> hashwka_t*
238static wmem_map_t *wka_hashtable;
239// Maps address -> hashether_t*
240static wmem_map_t *eth_hashtable;
241// Maps address -> hasheui64_t*
242static wmem_map_t *eui64_hashtable;
243// Maps unsigned -> serv_port_t*
244static wmem_map_t *serv_port_hashtable;
245static wmem_map_t *serv_port_custom_hashtable;
246
247// Maps enterprise-id -> enterprise-desc (only used for user additions)
248static GHashTable *enterprises_hashtable;
249
250static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE32]; /* Ordered array of entries */
251static bool_Bool have_subnet_entry;
252
253static bool_Bool new_resolved_objects;
254
255static GPtrArray* extra_hosts_files;
256
257static hashether_t *add_eth_name(const uint8_t *addr, const char *name, bool_Bool static_entry);
258static hasheui64_t *add_eui64_name(const uint8_t *addr, const char *name, bool_Bool static_entry);
259static void add_serv_port_cb(const uint32_t port, void *ptr);
260
261/* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
262 * One-at-a-Time hash
263 */
264unsigned
265ipv6_oat_hash(const void *key)
266{
267 int len = 16;
268 const unsigned char *p = (const unsigned char *)key;
269 unsigned h = 0;
270 int i;
271
272 for ( i = 0; i < len; i++ ) {
273 h += p[i];
274 h += ( h << 10 );
275 h ^= ( h >> 6 );
276 }
277
278 h += ( h << 3 );
279 h ^= ( h >> 11 );
280 h += ( h << 15 );
281
282 return h;
283}
284
285unsigned
286ws_ipv6_hash(const void* key)
287{
288#ifdef HAVE_XXHASH1
289 return wmem_strong_hash(key, 16);
290#else
291 return ipv6_oat_hash(key);
292#endif
293}
294
295gboolean
296ipv6_equal(const void *v1, const void *v2)
297{
298
299 if (memcmp(v1, v2, sizeof (ws_in6_addr)) == 0) {
300 return true1;
301 }
302
303 return false0;
304}
305
306/*
307 * Flag controlling what names to resolve.
308 */
309e_addr_resolve gbl_resolv_flags = {
310 true1, /* mac_name */
311 false0, /* network_name */
312 false0, /* transport_name */
313 true1, /* dns_pkt_addr_resolution */
314 false0, /* handshake_sni_addr_resolution */
315 true1, /* use_external_net_name_resolver */
316 false0, /* vlan_name */
317 false0, /* ss7 point code names */
318 true1, /* maxmind_geoip */
319};
320
321/* XXX - ares_init_options(3) says:
322 * "The recommended concurrent query limit is about 32k queries"
323 */
324static unsigned name_resolve_concurrency = 500;
325static bool_Bool resolve_synchronously;
326
327/*
328 * Global variables (can be changed in GUI sections)
329 * XXX - they could be changed in GUI code, but there's currently no
330 * GUI code to change them.
331 */
332
333char *g_ethers_path; /* global ethers file */
334char *g_pethers_path; /* personal ethers file */
335char *g_wka_path; /* global well-known-addresses file */
336char *g_manuf_path; /* global manuf file */
337char *g_pmanuf_path; /* personal manuf file */
338char *g_ipxnets_path; /* global ipxnets file */
339char *g_pipxnets_path; /* personal ipxnets file */
340char *g_services_path; /* global services file */
341char *g_pservices_path; /* personal services file */
342char *g_pvlan_path; /* personal vlans file */
343char *g_ss7pcs_path; /* personal ss7pcs file */
344char *g_enterprises_path; /* global enterprises file */
345char *g_penterprises_path; /* personal enterprises file */
346 /* first resolving call */
347
348/*
349 * Submitted asynchronous queries trigger a callback (c_ares_ghba_cb()).
350 * Queries are added to c_ares_queue_head. During processing, queries are
351 * popped off the front of c_ares_queue_head and submitted using
352 * ares_gethostbyaddr().
353 * The callback processes the response, then frees the request.
354 */
355typedef struct _async_dns_queue_msg
356{
357 union {
358 uint32_t ip4;
359 ws_in6_addr ip6;
360 } addr;
361 int family;
362} async_dns_queue_msg_t;
363
364typedef struct _async_hostent {
365 int addr_size;
366 int copied;
367 void *addrp;
368} async_hostent_t;
369
370static void
371c_ares_ghba_cb(void *arg, int status, int timeouts _U___attribute__((unused)), struct hostent *he);
372
373/*
374 * Submitted synchronous queries trigger a callback (c_ares_ghba_sync_cb()).
375 * The callback processes the response, sets completed to true if
376 * completed is non-NULL, then frees the request.
377 */
378typedef struct _sync_dns_data
379{
380 union {
381 uint32_t ip4;
382 ws_in6_addr ip6;
383 } addr;
384 int family;
385 bool_Bool *completed;
386} sync_dns_data_t;
387
388static ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
389static ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
390
391static bool_Bool async_dns_initialized;
392static unsigned async_dns_in_flight;
393static wmem_list_t *async_dns_queue_head;
394static GMutex async_dns_queue_mtx;
395
396//UAT for providing a list of DNS servers to C-ARES for name resolution
397bool_Bool use_custom_dns_server_list;
398struct dns_server_data {
399 char *ipaddr;
400 uint32_t udp_port;
401 uint32_t tcp_port;
402};
403
404UAT_CSTRING_CB_DEF(dnsserverlist_uats, ipaddr, struct dns_server_data)static void dnsserverlist_uats_ipaddr_set_cb(void* rec, const
char* buf, unsigned len, const void* u1 __attribute__((unused
)), const void* u2 __attribute__((unused))) { char* new_buf =
g_strndup(buf,len); g_free((((struct dns_server_data*)rec)->
ipaddr)); (((struct dns_server_data*)rec)->ipaddr) = new_buf
; } static void dnsserverlist_uats_ipaddr_tostr_cb(void* rec,
char** out_ptr, unsigned* out_len, const void* u1 __attribute__
((unused)), const void* u2 __attribute__((unused))) { if (((struct
dns_server_data*)rec)->ipaddr ) { *out_ptr = g_strdup_inline
((((struct dns_server_data*)rec)->ipaddr)); *out_len = (unsigned
)strlen((((struct dns_server_data*)rec)->ipaddr)); } else {
*out_ptr = g_strdup_inline (""); *out_len = 0; } }
405UAT_DEC_CB_DEF(dnsserverlist_uats, tcp_port, struct dns_server_data)static void dnsserverlist_uats_tcp_port_set_cb(void* rec, const
char* buf, unsigned len, const void* u1 __attribute__((unused
)), const void* u2 __attribute__((unused))) { char* tmp_str =
g_strndup(buf,len); ws_strtou32(tmp_str, ((void*)0), &((
struct dns_server_data*)rec)->tcp_port); g_free(tmp_str); }
static void dnsserverlist_uats_tcp_port_tostr_cb(void* rec, char
** out_ptr, unsigned* out_len, const void* u1 __attribute__((
unused)), const void* u2 __attribute__((unused))) { *out_ptr =
wmem_strdup_printf(((void*)0), "%u",((struct dns_server_data
*)rec)->tcp_port); *out_len = (unsigned)strlen(*out_ptr); }
406UAT_DEC_CB_DEF(dnsserverlist_uats, udp_port, struct dns_server_data)static void dnsserverlist_uats_udp_port_set_cb(void* rec, const
char* buf, unsigned len, const void* u1 __attribute__((unused
)), const void* u2 __attribute__((unused))) { char* tmp_str =
g_strndup(buf,len); ws_strtou32(tmp_str, ((void*)0), &((
struct dns_server_data*)rec)->udp_port); g_free(tmp_str); }
static void dnsserverlist_uats_udp_port_tostr_cb(void* rec, char
** out_ptr, unsigned* out_len, const void* u1 __attribute__((
unused)), const void* u2 __attribute__((unused))) { *out_ptr =
wmem_strdup_printf(((void*)0), "%u",((struct dns_server_data
*)rec)->udp_port); *out_len = (unsigned)strlen(*out_ptr); }
407
408static uat_t *dnsserver_uat;
409static struct dns_server_data *dnsserverlist_uats;
410static unsigned ndnsservers;
411
412static void
413dns_server_free_cb(void *data)
414{
415 struct dns_server_data *h = (struct dns_server_data*)data;
416
417 g_free(h->ipaddr);
418}
419
420static void*
421dns_server_copy_cb(void *dst_, const void *src_, size_t len _U___attribute__((unused)))
422{
423 const struct dns_server_data *src = (const struct dns_server_data *)src_;
424 struct dns_server_data *dst = (struct dns_server_data *)dst_;
425
426 dst->ipaddr = g_strdup(src->ipaddr)g_strdup_inline (src->ipaddr);
427 dst->udp_port = src->udp_port;
428 dst->tcp_port = src->tcp_port;
429
430 return dst;
431}
432
433static bool_Bool
434dnsserver_uat_fld_ip_chk_cb(void* r _U___attribute__((unused)), const char* ipaddr, unsigned len _U___attribute__((unused)), const void* u1 _U___attribute__((unused)), const void* u2 _U___attribute__((unused)), char** err)
435{
436 //Check for a valid IPv4 or IPv6 address.
437 if (ipaddr && g_hostname_is_ip_address(ipaddr)) {
438 *err = NULL((void*)0);
439 return true1;
440 }
441
442 *err = ws_strdup_printf("No valid IP address given.")wmem_strdup_printf(((void*)0), "No valid IP address given.");
443 return false0;
444}
445
446static bool_Bool
447dnsserver_uat_fld_port_chk_cb(void* r _U___attribute__((unused)), const char* p, unsigned len _U___attribute__((unused)), const void* u1 _U___attribute__((unused)), const void* u2 _U___attribute__((unused)), char** err)
448{
449 if (!p || strlen(p) == 0u) {
450 // This should be removed in favor of Decode As. Make it optional.
451 *err = NULL((void*)0);
452 return true1;
453 }
454
455 if (strcmp(p, "53") != 0){
456 uint16_t port;
457 if (!ws_strtou16(p, NULL((void*)0), &port)) {
458 *err = g_strdup("Invalid port given.")g_strdup_inline ("Invalid port given.");
459 return false0;
460 }
461 }
462
463 *err = NULL((void*)0);
464 return true1;
465}
466
467static void
468c_ares_ghba_sync_cb(void *arg, int status, int timeouts _U___attribute__((unused)), struct hostent *he) {
469 sync_dns_data_t *sdd = (sync_dns_data_t *)arg;
470 char **p;
471
472 if (status == ARES_SUCCESS) {
473 for (p = he->h_addr_list; *p != NULL((void*)0); p++) {
474 switch(sdd->family) {
475 case AF_INET2:
476 add_ipv4_name(sdd->addr.ip4, he->h_name, false0);
477 break;
478 case AF_INET610:
479 add_ipv6_name(&sdd->addr.ip6, he->h_name, false0);
480 break;
481 default:
482 /* Throw an exception? */
483 break;
484 }
485 }
486
487 }
488
489 /*
490 * Let our caller know that this is complete.
491 */
492 *sdd->completed = true1;
493
494 /*
495 * Free the structure for this call.
496 */
497 g_free(sdd);
498}
499
500static void
501wait_for_sync_resolv(bool_Bool *completed) {
502 int nfds;
503 fd_set rfds, wfds;
504 struct timeval tv;
505
506 while (!*completed) {
507 /*
508 * Not yet resolved; wait for something to show up on the
509 * address-to-name C-ARES channel.
510 *
511 * To quote the source code for ares_timeout() as of C-ARES
512 * 1.12.0, "WARNING: Beware that this is linear in the number
513 * of outstanding requests! You are probably far better off
514 * just calling ares_process() once per second, rather than
515 * calling ares_timeout() to figure out when to next call
516 * ares_process().", although we should have only one request
517 * outstanding.
518 * As of C-ARES 1.20.0, the ares_timeout() function is now O(1),
519 * but we don't require that minimum version.
520 * https://github.com/c-ares/c-ares/commit/cf99c025cfb3e21295b59923876a31a68ea2cb4b
521 *
522 * And, yes, we have to reset it each time, as select(), in
523 * some OSes modifies the timeout to reflect the time remaining
524 * (e.g., Linux) and select() in other OSes doesn't (most if not
525 * all other UN*Xes, Windows?), so we can't rely on *either*
526 * behavior.
527 */
528 tv.tv_sec = 1;
529 tv.tv_usec = 0;
530
531 FD_ZERO(&rfds)do { unsigned int __i; fd_set *__arr = (&rfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
532 FD_ZERO(&wfds)do { unsigned int __i; fd_set *__arr = (&wfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
533 nfds = ares_fds(ghba_chan, &rfds, &wfds);
534 if (nfds > 0) {
535 if (select(nfds, &rfds, &wfds, NULL((void*)0), &tv) == -1) { /* call to select() failed */
536 /* If it's interrupted by a signal, no need to put out a message */
537 if (errno(*__errno_location ()) != EINTR4)
538 fprintf(stderrstderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno(*__errno_location ())));
539 return;
540 }
541 ares_process(ghba_chan, &rfds, &wfds);
542 }
543 }
544}
545
546static void
547process_async_dns_queue(void)
548{
549 wmem_list_frame_t* head;
550 async_dns_queue_msg_t *caqm;
551
552 if (async_dns_queue_head == NULL((void*)0))
553 return;
554
555 if (!g_mutex_trylock(&async_dns_queue_mtx))
556 return;
557
558 head = wmem_list_head(async_dns_queue_head);
559
560 while (head != NULL((void*)0) && async_dns_in_flight <= name_resolve_concurrency) {
561 caqm = (async_dns_queue_msg_t *)wmem_list_frame_data(head);
562 wmem_list_remove_frame(async_dns_queue_head, head);
563 if (caqm->family == AF_INET2) {
564 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(uint32_t), AF_INET2,
565 c_ares_ghba_cb, caqm);
566 async_dns_in_flight++;
567 } else if (caqm->family == AF_INET610) {
568 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(ws_in6_addr),
569 AF_INET610, c_ares_ghba_cb, caqm);
570 async_dns_in_flight++;
571 }
572
573 head = wmem_list_head(async_dns_queue_head);
574 }
575
576 g_mutex_unlock(&async_dns_queue_mtx);
577}
578
579static void
580wait_for_async_queue(void)
581{
582 struct timeval tv = { 0, 0 };
583 int nfds;
584 fd_set rfds, wfds;
585
586 new_resolved_objects = false0;
587
588 if (!async_dns_initialized) {
589 maxmind_db_lookup_process();
590 return;
591 }
592
593 while (1) {
594 /* We're switching to synchronous lookups, so process anything in
595 * the asynchronous queue. There might be more in the queue than
596 * name_resolve_concurrency allows, so check each cycle.
597 */
598 process_async_dns_queue();
599
600 FD_ZERO(&rfds)do { unsigned int __i; fd_set *__arr = (&rfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
601 FD_ZERO(&wfds)do { unsigned int __i; fd_set *__arr = (&wfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
602 nfds = ares_fds(ghba_chan, &rfds, &wfds);
603 if (nfds == 0) {
604 /* No more requests waiting for reply; we're done here. */
605 break;
606 }
607
608 /* See comment in wait_for_sync_resolv() about ares_timeout() being
609 * O(N) in the number of outstanding requests until c-ares 1.20, and
610 * why we might as well just set a 1 second to select().
611 */
612 tv.tv_sec = 1;
613 tv.tv_usec = 0;
614
615 if (select(nfds, &rfds, &wfds, NULL((void*)0), &tv) == -1) { /* call to select() failed */
616 /* If it's interrupted by a signal, no need to put out a message */
617 if (errno(*__errno_location ()) != EINTR4)
618 fprintf(stderrstderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno(*__errno_location ())));
619 return;
620 }
621 ares_process(ghba_chan, &rfds, &wfds);
622 }
623
624 maxmind_db_lookup_process();
625 return;
626}
627
628static void
629sync_lookup_ip4(const uint32_t addr)
630{
631 bool_Bool completed = false0;
632 sync_dns_data_t *sdd;
633
634 if (!async_dns_initialized) {
635 /*
636 * c-ares not initialized. Bail out.
637 */
638 return;
639 }
640
641 /*
642 * Start the request.
643 */
644 sdd = g_new(sync_dns_data_t, 1)((sync_dns_data_t *) g_malloc_n ((1), sizeof (sync_dns_data_t
)))
;
645 sdd->family = AF_INET2;
646 sdd->addr.ip4 = addr;
647 sdd->completed = &completed;
648 ares_gethostbyaddr(ghba_chan, &addr, sizeof(uint32_t), AF_INET2,
649 c_ares_ghba_sync_cb, sdd);
650
651 /*
652 * Now wait for it to finish.
653 */
654 wait_for_sync_resolv(&completed);
655}
656
657static void
658sync_lookup_ip6(const ws_in6_addr *addrp)
659{
660 bool_Bool completed = false0;
661 sync_dns_data_t *sdd;
662
663 if (!async_dns_initialized) {
664 /*
665 * c-ares not initialized. Bail out.
666 */
667 return;
668 }
669
670 /*
671 * Start the request.
672 */
673 sdd = g_new(sync_dns_data_t, 1)((sync_dns_data_t *) g_malloc_n ((1), sizeof (sync_dns_data_t
)))
;
674 sdd->family = AF_INET610;
675 memcpy(&sdd->addr.ip6, addrp, sizeof(sdd->addr.ip6));
676 sdd->completed = &completed;
677 ares_gethostbyaddr(ghba_chan, addrp, sizeof(ws_in6_addr), AF_INET610,
678 c_ares_ghba_sync_cb, sdd);
679
680 /*
681 * Now wait for it to finish.
682 */
683 wait_for_sync_resolv(&completed);
684}
685
686void
687set_resolution_synchrony(bool_Bool synchronous)
688{
689 resolve_synchronously = synchronous;
690 maxmind_db_set_synchrony(synchronous);
691
692 if (synchronous) {
693 wait_for_async_queue();
694 }
695}
696
697static void
698c_ares_set_dns_servers(void)
699{
700 if ((!async_dns_initialized) || (!use_custom_dns_server_list))
701 return;
702
703 if (ndnsservers == 0) {
704 //clear the list of servers. This may effectively disable name resolution
705 ares_set_servers_ports(ghba_chan, NULL((void*)0));
706 ares_set_servers_ports(ghbn_chan, NULL((void*)0));
707 } else {
708 struct ares_addr_port_node* servers = wmem_alloc_array(NULL, struct ares_addr_port_node, ndnsservers)((struct ares_addr_port_node*)wmem_alloc((((void*)0)), (((((ndnsservers
)) <= 0) || ((size_t)sizeof(struct ares_addr_port_node) >
(9223372036854775807L / (size_t)((ndnsservers))))) ? 0 : (sizeof
(struct ares_addr_port_node) * ((ndnsservers))))))
;
709 ws_in4_addr ipv4addr;
710 ws_in6_addr ipv6addr;
711 bool_Bool invalid_IP_found = false0;
712 struct ares_addr_port_node* server;
713 unsigned i;
714 for (i = 0, server = servers; i < ndnsservers-1; i++, server++) {
715 if (ws_inet_pton6(dnsserverlist_uats[i].ipaddr, &ipv6addr)) {
716 server->family = AF_INET610;
717 memcpy(&server->addr.addr6, &ipv6addr, 16);
718 } else if (ws_inet_pton4(dnsserverlist_uats[i].ipaddr, &ipv4addr)) {
719 server->family = AF_INET2;
720 memcpy(&server->addr.addr4, &ipv4addr, 4);
721 } else {
722 //This shouldn't happen, but just in case...
723 invalid_IP_found = true1;
724 server->family = 0;
725 memset(&server->addr.addr4, 0, 4);
726 break;
727 }
728
729 server->udp_port = (int)dnsserverlist_uats[i].udp_port;
730 server->tcp_port = (int)dnsserverlist_uats[i].tcp_port;
731
732 server->next = (server+1);
733 }
734 if (!invalid_IP_found) {
735 if (ws_inet_pton6(dnsserverlist_uats[i].ipaddr, &ipv6addr)) {
736 server->family = AF_INET610;
737 memcpy(&server->addr.addr6, &ipv6addr, 16);
738 }
739 else if (ws_inet_pton4(dnsserverlist_uats[i].ipaddr, &ipv4addr)) {
740 server->family = AF_INET2;
741 memcpy(&server->addr.addr4, &ipv4addr, 4);
742 } else {
743 //This shouldn't happen, but just in case...
744 server->family = 0;
745 memset(&server->addr.addr4, 0, 4);
746 }
747 }
748 server->udp_port = (int)dnsserverlist_uats[i].udp_port;
749 server->tcp_port = (int)dnsserverlist_uats[i].tcp_port;
750
751 server->next = NULL((void*)0);
752
753 ares_set_servers_ports(ghba_chan, servers);
754 ares_set_servers_ports(ghbn_chan, servers);
755 wmem_free(NULL((void*)0), servers);
756 }
757}
758
759typedef struct {
760 uint32_t mask;
761 size_t mask_length;
762 const char* name; /* Shallow copy */
763} subnet_entry_t;
764
765/* Maximum supported line length of hosts, services, manuf, etc. */
766#define MAX_LINELEN1024 1024
767
768/** Read a line without trailing (CR)LF. Returns -1 on failure. */
769static int
770fgetline(char *buf, int size, FILE *fp)
771{
772 if (fgets(buf, size, fp)) {
773 int len = (int)strcspn(buf, "\r\n");
774 buf[len] = '\0';
775 return len;
776 }
777 return -1;
778
779} /* fgetline */
780
781
782/*
783 * Local function definitions
784 */
785static subnet_entry_t subnet_lookup(const uint32_t addr);
786static void subnet_entry_set(uint32_t subnet_addr, const uint8_t mask_length, const char* name);
787
788static unsigned serv_port_custom_hash(const void *k)
789{
790 const serv_port_key_t *key = (const serv_port_key_t*)k;
791 return key->port + (key->type << 16);
792}
793
794static gboolean serv_port_custom_equal(const void *k1, const void *k2)
795{
796 const serv_port_key_t *key1 = (const serv_port_key_t*)k1;
797 const serv_port_key_t *key2 = (const serv_port_key_t*)k2;
798
799 return (key1->port == key2->port) && (key1->type == key2->type);
800}
801
802static void
803add_custom_service_name(port_type proto, const unsigned port, const char *service_name)
804{
805 char *name;
806 serv_port_key_t *key, *orig_key;
807
808 key = wmem_new(addr_resolv_scope, serv_port_key_t)((serv_port_key_t*)wmem_alloc((addr_resolv_scope), sizeof(serv_port_key_t
)))
;
809 key->port = (uint16_t)port;
810 key->type = proto;
811
812 if (wmem_map_lookup_extended(serv_port_custom_hashtable, key, (const void**)&orig_key, (void**)&name)) {
813 wmem_free(addr_resolv_scope, orig_key);
814 wmem_free(addr_resolv_scope, name);
815 }
816
817 name = wmem_strdup(addr_resolv_scope, service_name);
818 wmem_map_insert(serv_port_custom_hashtable, key, name);
819
820 // A new custom entry is not a new resolved object.
821 // new_resolved_objects = true;
822}
823
824static void
825add_service_name(port_type proto, const unsigned port, const char *service_name)
826{
827 serv_port_key_t *key = wmem_new(addr_resolv_scope, serv_port_key_t)((serv_port_key_t*)wmem_alloc((addr_resolv_scope), sizeof(serv_port_key_t
)))
;
828 key->port = (uint16_t)port;
829 key->type = proto;
830
831 wmem_map_insert(serv_port_hashtable, key, (void*)service_name);
832
833 new_resolved_objects = true1;
834}
835
836static void
837parse_service_line (char *line)
838{
839 char *cp;
840 char *service;
841 char *port;
842 port_type proto;
843 struct cb_serv_data cb_data;
844 range_t *port_rng = NULL((void*)0);
845
846 if ((cp = strchr(line, '#')))
847 *cp = '\0';
848
849 if ((cp = strtok(line, " \t")) == NULL((void*)0))
850 return;
851
852 service = cp;
853
854 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
855 return;
856
857 port = cp;
858
859 if (strtok(cp, "/") == NULL((void*)0))
860 return;
861
862 if (range_convert_str(NULL((void*)0), &port_rng, port, UINT16_MAX(65535)) != CVT_NO_ERROR) {
863 wmem_free (NULL((void*)0), port_rng);
864 return;
865 }
866
867 while ((cp = strtok(NULL((void*)0), "/")) != NULL((void*)0)) {
868 if (strcmp(cp, "tcp") == 0) {
869 proto = PT_TCP;
870 }
871 else if (strcmp(cp, "udp") == 0) {
872 proto = PT_UDP;
873 }
874 else if (strcmp(cp, "sctp") == 0) {
875 proto = PT_SCTP;
876 }
877 else if (strcmp(cp, "dccp") == 0) {
878 proto = PT_DCCP;
879 }
880 else {
881 break;
882 }
883 cb_data.service = service;
884 cb_data.proto = proto;
885 range_foreach(port_rng, add_serv_port_cb, &cb_data);
886 }
887
888 wmem_free (NULL((void*)0), port_rng);
889} /* parse_service_line */
890
891
892static void
893add_serv_port_cb(const uint32_t port, void *ptr)
894{
895 struct cb_serv_data *cb_data = (struct cb_serv_data *)ptr;
896
897 if ( port ) {
898 add_custom_service_name(cb_data->proto, port, cb_data->service);
899 }
900}
901
902
903static bool_Bool
904parse_services_file(const char * path)
905{
906 FILE *serv_p;
907 char buf[MAX_LINELEN1024];
908
909 /* services hash table initialization */
910 serv_p = ws_fopenfopen(path, "r");
911
912 if (serv_p == NULL((void*)0))
913 return false0;
914
915 while (fgetline(buf, sizeof(buf), serv_p) >= 0) {
916 parse_service_line(buf);
917 }
918
919 fclose(serv_p);
920 return true1;
921}
922
923/* -----------------
924 * unsigned integer to ascii
925 */
926static char *
927wmem_utoa(wmem_allocator_t *allocator, unsigned port)
928{
929 char *bp = (char *)wmem_alloc(allocator, MAXNAMELEN64);
930
931 /* XXX, uint32_to_str() ? */
932 uint32_to_str_buf(port, bp, MAXNAMELEN64);
933 return bp;
934}
935
936static const char *
937_serv_name_lookup(port_type proto, unsigned port)
938{
939 const char* name = NULL((void*)0);
940 ws_services_proto_t p;
941 ws_services_entry_t const *serv;
942
943 const serv_port_key_t custom_key = { (uint16_t)port, proto };
944 /* Look in the cache. Use an extended lookup so we can distinguish a port
945 * we already tried but had no name from one we haven't tried. */
946 if (!wmem_map_lookup_extended(serv_port_hashtable, &custom_key, NULL((void*)0), (void **)&name)) {
947 /* Try the user custom table */
948 name = wmem_map_lookup(serv_port_custom_hashtable, &custom_key);
949
950 if (name == NULL((void*)0)) {
951 /* now look in the global tables */
952 bool_Bool valid_proto = true1;
953 switch(proto) {
954 case PT_TCP: p = ws_tcp; break;
955 case PT_UDP: p = ws_udp; break;
956 case PT_SCTP: p = ws_sctp; break;
957 case PT_DCCP: p = ws_dccp; break;
958 default: valid_proto = false0;
959 }
960 if (valid_proto) {
961 serv = global_services_lookup(port, p);
962 if (serv) {
963 name = serv->name;
964 }
965 }
966 }
967
968 /* Cache result (even if NULL, so we can know we have no result.) */
969 add_service_name(proto, port, name);
970 }
971
972 return name;
973}
974
975const char *
976try_serv_name_lookup(port_type proto, unsigned port)
977{
978 return (proto == PT_NONE) ? NULL((void*)0) : _serv_name_lookup(proto, port);
979}
980
981const char *
982serv_name_lookup(port_type proto, unsigned port)
983{
984 const char *name;
985
986 /* first look for the name */
987 name = _serv_name_lookup(proto, port);
988 if (name != NULL((void*)0))
989 return name;
990
991 /* No resolved name. Do we have a cached numeric string? */
992 const serv_port_key_t key = { (uint16_t)port, PT_NONE };
993 name = (const char*)wmem_map_lookup(serv_port_hashtable, &key);
994 /* No name; create the numeric string. */
995 if (name == NULL((void*)0)) {
996 name = wmem_strdup_printf(addr_resolv_scope, "%u", port);
997 add_service_name(PT_NONE, port, name);
998 }
999
1000 return name;
1001}
1002
1003static void
1004initialize_services(void)
1005{
1006 ws_assert(serv_port_hashtable == NULL)do { if ((1) && !(serv_port_hashtable == ((void*)0)))
ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/addr_resolv.c",
1006, __func__, "assertion failed: %s", "serv_port_hashtable == ((void*)0)"
); } while (0)
;
1007 serv_port_hashtable = wmem_map_new(addr_resolv_scope, serv_port_custom_hash, serv_port_custom_equal);
1008 ws_assert(serv_port_custom_hashtable == NULL)do { if ((1) && !(serv_port_custom_hashtable == ((void
*)0))) ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/addr_resolv.c"
, 1008, __func__, "assertion failed: %s", "serv_port_custom_hashtable == ((void*)0)"
); } while (0)
;
1009 serv_port_custom_hashtable = wmem_map_new(addr_resolv_scope, serv_port_custom_hash, serv_port_custom_equal);
1010
1011 /* Compute the pathname of the global services file. */
1012 if (g_services_path == NULL((void*)0)) {
1013 g_services_path = get_datafile_path(ENAME_SERVICES"services");
1014 }
1015 parse_services_file(g_services_path);
1016
1017 /* Compute the pathname of the personal services file */
1018 if (g_pservices_path == NULL((void*)0)) {
1019 /* Check profile directory before personal configuration */
1020 g_pservices_path = get_persconffile_path(ENAME_SERVICES"services", true1);
1021 if (!parse_services_file(g_pservices_path)) {
1022 g_free(g_pservices_path);
1023 g_pservices_path = get_persconffile_path(ENAME_SERVICES"services", false0);
1024 parse_services_file(g_pservices_path);
1025 }
1026 }
1027}
1028
1029static void
1030service_name_lookup_cleanup(void)
1031{
1032 serv_port_hashtable = NULL((void*)0);
1033 serv_port_custom_hashtable = NULL((void*)0);
1034 g_free(g_services_path);
1035 g_services_path = NULL((void*)0);
1036 g_free(g_pservices_path);
1037 g_pservices_path = NULL((void*)0);
1038}
1039
1040static void
1041parse_enterprises_line (char *line)
1042{
1043 char *tok, *dec_str, *org_str;
1044 uint32_t dec;
1045 bool_Bool had_comment = false0;
1046
1047 /* Stop the line at any comment found */
1048 if ((tok = strchr(line, '#'))) {
1049 *tok = '\0';
1050 had_comment = true1;
1051 }
1052 /* Get enterprise number */
1053 dec_str = strtok(line, " \t");
1054 if (!dec_str)
1055 return;
1056 /* Get enterprise name */
1057 org_str = strtok(NULL((void*)0), ""); /* everything else */
1058 if (org_str && had_comment) {
1059 /* Only need to strip after (between name and where comment was) */
1060 org_str = g_strchomp(org_str);
1061 }
1062 if (!org_str)
1063 return;
1064
1065 /* Add entry using number as key */
1066 if (!ws_strtou32(dec_str, NULL((void*)0), &dec))
1067 return;
1068 g_hash_table_insert(enterprises_hashtable, GUINT_TO_POINTER(dec)((gpointer) (gulong) (dec)), g_strdup(org_str)g_strdup_inline (org_str));
1069}
1070
1071
1072static bool_Bool
1073parse_enterprises_file(const char * path)
1074{
1075 FILE *fp;
1076 char buf[MAX_LINELEN1024];
1077
1078 fp = ws_fopenfopen(path, "r");
1079 if (fp == NULL((void*)0))
1080 return false0;
1081
1082 while (fgetline(buf, sizeof(buf), fp) >= 0) {
1083 parse_enterprises_line(buf);
1084 }
1085
1086 fclose(fp);
1087 return true1;
1088}
1089
1090static void
1091initialize_enterprises(void)
1092{
1093 ws_assert(enterprises_hashtable == NULL)do { if ((1) && !(enterprises_hashtable == ((void*)0)
)) ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/addr_resolv.c"
, 1093, __func__, "assertion failed: %s", "enterprises_hashtable == ((void*)0)"
); } while (0)
;
1094 enterprises_hashtable = g_hash_table_new_full(NULL((void*)0), NULL((void*)0), NULL((void*)0), g_free);
1095
1096 if (g_enterprises_path == NULL((void*)0)) {
1097 g_enterprises_path = get_datafile_path(ENAME_ENTERPRISES"enterprises");
1098 }
1099 parse_enterprises_file(g_enterprises_path);
1100
1101 /* Populate entries from profile or personal */
1102 if (g_penterprises_path == NULL((void*)0)) {
1103 /* Check profile directory before personal configuration */
1104 g_penterprises_path = get_persconffile_path(ENAME_ENTERPRISES"enterprises", true1);
1105 if (!file_exists(g_penterprises_path)) {
1106 g_free(g_penterprises_path);
1107 g_penterprises_path = get_persconffile_path(ENAME_ENTERPRISES"enterprises", false0);
1108 }
1109 }
1110 /* Parse personal file (if present) */
1111 parse_enterprises_file(g_penterprises_path);
1112}
1113
1114const char *
1115try_enterprises_lookup(uint32_t value)
1116{
1117 /* Trying extra entries first. N.B. This does allow entries to be overwritten and found.. */
1118 const char *name = (const char *)g_hash_table_lookup(enterprises_hashtable, GUINT_TO_POINTER(value)((gpointer) (gulong) (value)));
1119 if (name) {
1120 return name;
1121 }
1122 else {
1123 return global_enterprises_lookup(value);
1124 }
1125}
1126
1127const char *
1128enterprises_lookup(uint32_t value, const char *unknown_str)
1129{
1130 const char *s;
1131
1132 s = try_enterprises_lookup(value);
1133 if (s != NULL((void*)0))
1134 return s;
1135 if (unknown_str != NULL((void*)0))
1136 return unknown_str;
1137 return "<Unknown>";
1138}
1139
1140void
1141enterprises_base_custom(char *buf, uint32_t value)
1142{
1143 const char *s;
1144
1145 if ((s = try_enterprises_lookup(value)) == NULL((void*)0))
1146 s = ITEM_LABEL_UNKNOWN_STR"Unknown";
1147 snprintf(buf, ITEM_LABEL_LENGTH240, "%s (%u)", s, value);
1148}
1149
1150static void
1151enterprises_cleanup(void)
1152{
1153 ws_assert(enterprises_hashtable)do { if ((1) && !(enterprises_hashtable)) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 1153, __func__, "assertion failed: %s"
, "enterprises_hashtable"); } while (0)
;
1154 g_hash_table_destroy(enterprises_hashtable);
1155 enterprises_hashtable = NULL((void*)0);
1156 g_free(g_enterprises_path);
1157 g_enterprises_path = NULL((void*)0);
1158 g_free(g_penterprises_path);
1159 g_penterprises_path = NULL((void*)0);
1160}
1161
1162/* Fill in an IP4 structure with info from subnets file or just with the
1163 * string form of the address.
1164 */
1165bool_Bool
1166fill_dummy_ip4(const unsigned addr, hashipv4_t* volatile tp)
1167{
1168 subnet_entry_t subnet_entry;
1169
1170 /* return value : true if addr matches any subnet */
1171 bool_Bool cidr_covered = false0;
1172
1173 /* Overwrite if we get async DNS reply */
1174
1175 /* Do we have a subnet for this address? */
1176 subnet_entry = subnet_lookup(addr);
1177 if (0 != subnet_entry.mask) {
1178 /* Print name, then '.' then IP address after subnet mask */
1179 uint32_t host_addr;
1180 char buffer[WS_INET_ADDRSTRLEN16];
1181 char* paddr;
1182 size_t i;
1183
1184 host_addr = addr & (~subnet_entry.mask);
1185 ip_addr_to_str_buf(&host_addr, buffer, WS_INET_ADDRSTRLEN16);
1186 paddr = buffer;
1187
1188 /* Skip to first octet that is not totally masked
1189 * If length of mask is 32, we chomp the whole address.
1190 * If the address string starts '.' (should not happen?),
1191 * we skip that '.'.
1192 */
1193 i = subnet_entry.mask_length / 8;
1194 while(*(paddr) != '\0' && i > 0) {
1195 if (*(++paddr) == '.') {
1196 --i;
1197 }
1198 }
1199
1200 /* There are more efficient ways to do this, but this is safe if we
1201 * trust snprintf and MAXDNSNAMELEN
1202 */
1203 snprintf(tp->name, MAXDNSNAMELEN256, "%s%s", subnet_entry.name, paddr);
1204
1205 /* Evaluate the subnet in CIDR notation
1206 * Reuse buffers built above
1207 */
1208 uint32_t subnet_addr;
1209 subnet_addr = addr & subnet_entry.mask;
1210
1211 char buffer_subnet[WS_INET_ADDRSTRLEN16];
1212 ip_addr_to_str_buf(&subnet_addr, buffer_subnet, WS_INET_ADDRSTRLEN16);
1213
1214 char buffer_cidr[WS_INET_CIDRADDRSTRLEN19];
1215 snprintf(buffer_cidr, WS_INET_CIDRADDRSTRLEN19, "%s%s%u", buffer_subnet, "/", (unsigned)subnet_entry.mask_length);
1216
1217 snprintf(tp->cidr_addr, WS_INET_CIDRADDRSTRLEN19, "%s%s%u", buffer_subnet, "/", (unsigned)subnet_entry.mask_length);
1218 cidr_covered = true1;
1219 } else {
1220 /* XXX: This means we end up printing "1.2.3.4 (1.2.3.4)" in many cases */
1221 ip_addr_to_str_buf(&addr, tp->name, MAXDNSNAMELEN256);
1222
1223 /* IP does not belong to any known subnet, just indicate this IP without "/.32" */
1224 ip_addr_to_str_buf(&addr, tp->cidr_addr, MAXDNSNAMELEN256);
1225 }
1226 return cidr_covered;
1227}
1228
1229
1230/* Fill in an IP6 structure with the string form of the address.
1231 */
1232static void
1233fill_dummy_ip6(hashipv6_t* volatile tp)
1234{
1235 /* Overwrite if we get async DNS reply */
1236 (void) g_strlcpy(tp->name, tp->ip6, MAXDNSNAMELEN256);
1237}
1238
1239static void
1240c_ares_ghba_cb(void *arg, int status, int timeouts _U___attribute__((unused)), struct hostent *he) {
1241 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
1242 char **p;
1243
1244 if (!caqm) return;
1245 /* XXX, what to do if async_dns_in_flight == 0? */
1246 async_dns_in_flight--;
1247
1248 if (status == ARES_SUCCESS) {
1249 for (p = he->h_addr_list; *p != NULL((void*)0); p++) {
1250 switch(caqm->family) {
1251 case AF_INET2:
1252 add_ipv4_name(caqm->addr.ip4, he->h_name, false0);
1253 break;
1254 case AF_INET610:
1255 add_ipv6_name(&caqm->addr.ip6, he->h_name, false0);
1256 break;
1257 default:
1258 /* Throw an exception? */
1259 break;
1260 }
1261 }
1262 }
1263 wmem_free(addr_resolv_scope, caqm);
1264}
1265
1266/* --------------- */
1267hashipv4_t *
1268new_ipv4(const unsigned addr)
1269{
1270 hashipv4_t *tp = wmem_new(addr_resolv_scope, hashipv4_t)((hashipv4_t*)wmem_alloc((addr_resolv_scope), sizeof(hashipv4_t
)))
;
1271 tp->addr = addr;
1272 tp->flags = 0;
1273 tp->name[0] = '\0';
1274 ip_addr_to_str_buf(&addr, tp->ip, sizeof(tp->ip));
1275 return tp;
1276}
1277
1278static hashipv4_t *
1279host_lookup(const unsigned addr)
1280{
1281 hashipv4_t * volatile tp;
1282
1283 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)));
1284 if (tp == NULL((void*)0)) {
1285 /*
1286 * We don't already have an entry for this host name; create one,
1287 * and then try to resolve it.
1288 */
1289 tp = new_ipv4(addr);
1290 fill_dummy_ip4(addr, tp);
1291 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)), tp);
1292 } else if (tp->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1))) {
1293 return tp;
1294 }
1295
1296 /*
1297 * This hasn't been resolved yet, and we haven't tried to
1298 * resolve it already.
1299 */
1300
1301 if (!gbl_resolv_flags.network_name)
1302 return tp;
1303
1304 if (gbl_resolv_flags.use_external_net_name_resolver) {
1305 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1306
1307 if (async_dns_initialized) {
1308 /* c-ares is initialized, so we can use it */
1309 if (resolve_synchronously || name_resolve_concurrency == 0) {
1310 /*
1311 * Either all names are to be resolved synchronously or
1312 * the concurrencly level is 0; do the resolution
1313 * synchronously.
1314 */
1315 sync_lookup_ip4(addr);
1316 } else {
1317 /*
1318 * Names are to be resolved asynchronously, and we
1319 * allow at least one asynchronous request in flight;
1320 * post an asynchronous request.
1321 */
1322 async_dns_queue_msg_t *caqm;
1323
1324 caqm = wmem_new(addr_resolv_scope, async_dns_queue_msg_t)((async_dns_queue_msg_t*)wmem_alloc((addr_resolv_scope), sizeof
(async_dns_queue_msg_t)))
;
1325 caqm->family = AF_INET2;
1326 caqm->addr.ip4 = addr;
1327 wmem_list_append(async_dns_queue_head, (void *) caqm);
1328 }
1329 }
1330 }
1331
1332 return tp;
1333
1334} /* host_lookup */
1335
1336/* --------------- */
1337static hashipv6_t *
1338new_ipv6(const ws_in6_addr *addr)
1339{
1340 hashipv6_t *tp = wmem_new(addr_resolv_scope, hashipv6_t)((hashipv6_t*)wmem_alloc((addr_resolv_scope), sizeof(hashipv6_t
)))
;
1341 memcpy(tp->addr, addr->bytes, sizeof tp->addr);
1342 tp->flags = 0;
1343 tp->name[0] = '\0';
1344 ip6_to_str_buf(addr, tp->ip6, sizeof(tp->ip6));
1345 return tp;
1346}
1347
1348/* ------------------------------------ */
1349static hashipv6_t *
1350host_lookup6(const ws_in6_addr *addr)
1351{
1352 hashipv6_t * volatile tp;
1353
1354 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addr);
1355 if (tp == NULL((void*)0)) {
1356 /*
1357 * We don't already have an entry for this host name; create one,
1358 * and then try to resolve it.
1359 */
1360 ws_in6_addr *addr_key;
1361
1362 addr_key = wmem_new(addr_resolv_scope, ws_in6_addr)((ws_in6_addr*)wmem_alloc((addr_resolv_scope), sizeof(ws_in6_addr
)))
;
1363 tp = new_ipv6(addr);
1364 memcpy(addr_key, addr, 16);
1365 fill_dummy_ip6(tp);
1366 wmem_map_insert(ipv6_hash_table, addr_key, tp);
1367 } else if (tp->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1))) {
1368 return tp;
1369 }
1370
1371 /*
1372 * This hasn't been resolved yet, and we haven't tried to
1373 * resolve it already.
1374 */
1375
1376 if (!gbl_resolv_flags.network_name)
1377 return tp;
1378
1379 if (gbl_resolv_flags.use_external_net_name_resolver) {
1380 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1381
1382 if (async_dns_initialized) {
1383 /* c-ares is initialized, so we can use it */
1384 if (resolve_synchronously || name_resolve_concurrency == 0) {
1385 /*
1386 * Either all names are to be resolved synchronously or
1387 * the concurrencly level is 0; do the resolution
1388 * synchronously.
1389 */
1390 sync_lookup_ip6(addr);
1391 } else {
1392 /*
1393 * Names are to be resolved asynchronously, and we
1394 * allow at least one asynchronous request in flight;
1395 * post an asynchronous request.
1396 */
1397 async_dns_queue_msg_t *caqm;
1398
1399 caqm = wmem_new(addr_resolv_scope, async_dns_queue_msg_t)((async_dns_queue_msg_t*)wmem_alloc((addr_resolv_scope), sizeof
(async_dns_queue_msg_t)))
;
1400 caqm->family = AF_INET610;
1401 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
1402 wmem_list_append(async_dns_queue_head, (void *) caqm);
1403 }
1404 }
1405 }
1406
1407 return tp;
1408
1409} /* host_lookup6 */
1410
1411/*
1412 * Ethernet / manufacturer resolution
1413 *
1414 * The following functions implement ethernet address resolution and
1415 * ethers files parsing (see ethers(4)).
1416 *
1417 * The manuf file has the same format as ethers(4) except that names are
1418 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1419 * only 3 bytes (instead of 6).
1420 *
1421 * Notes:
1422 *
1423 * I decide to not use the existing functions (see ethers(3) on some
1424 * operating systems) for the following reasons:
1425 * - performance gains (use of hash tables and some other enhancements),
1426 * - use of two ethers files (system-wide and per user),
1427 * - avoid the use of NIS maps,
1428 * - lack of these functions on some systems.
1429 *
1430 * So the following functions do _not_ behave as the standard ones.
1431 *
1432 * -- Laurent.
1433 */
1434
1435/*
1436 * Converts Ethernet addresses of the form aa:bb:cc or aa:bb:cc:dd:ee:ff/28.
1437 * '-' is also supported as a separator. The
1438 * octets must be exactly two hexadecimal characters and the mask must be either
1439 * 28 or 36. Pre-condition: cp MUST be at least 21 bytes.
1440 */
1441static bool_Bool
1442parse_ether_address_fast(const unsigned char *cp, ether_t *eth, unsigned int *mask,
1443 const bool_Bool accept_mask)
1444{
1445 /* XXX copied from strutil.c */
1446 /* a map from ASCII hex chars to their value */
1447 static const int8_t str_to_nibble[256] = {
1448 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1449 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1450 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1451 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
1452 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1453 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1454 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1455 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1456 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1457 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1458 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1459 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1460 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1461 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1462 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
1463 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
1464 };
1465 const uint8_t *str_to_nibble_usg = (const uint8_t *)str_to_nibble;
1466
1467 unsigned char sep = cp[2];
1468 if ((sep != ':' && sep != '-') || cp[5] != sep) {
1469 /* Unexpected separators. */
1470 return false0;
1471 }
1472
1473 /* N.B. store octet values in an int to detect invalid (-1) entries */
1474 int num0 = (str_to_nibble_usg[cp[0]] << 4) | (int8_t)str_to_nibble_usg[cp[1]];
1475 int num1 = (str_to_nibble_usg[cp[3]] << 4) | (int8_t)str_to_nibble_usg[cp[4]];
1476 int num2 = (str_to_nibble_usg[cp[6]] << 4) | (int8_t)str_to_nibble_usg[cp[7]];
1477
1478 if ((num0 | num1 | num2) & 0x100) {
1479 /* Not hexadecimal numbers. */
1480 return false0;
1481 }
1482
1483 eth->addr[0] = (uint8_t)num0;
1484 eth->addr[1] = (uint8_t)num1;
1485 eth->addr[2] = (uint8_t)num2;
1486
1487 if (cp[8] == '\0' && accept_mask) {
1488 /* Indicate that this is a manufacturer ID (0 is not allowed as a mask). */
1489 *mask = 0;
1490 return true1;
1491 } else if (cp[8] != sep || !accept_mask) {
1492 /* Format not handled by this fast path. */
1493 return false0;
1494 }
1495
1496 /* N.B. store octet values in an int to detect invalid (-1) entries */
1497 int num3 = (str_to_nibble_usg[cp[9]] << 4) | (int8_t)str_to_nibble_usg[cp[10]];
1498 int num4 = (str_to_nibble_usg[cp[12]] << 4) | (int8_t)str_to_nibble_usg[cp[13]];
1499 int num5 = (str_to_nibble_usg[cp[15]] << 4) | (int8_t)str_to_nibble_usg[cp[16]];
1500
1501 if (((num3 | num4 | num5) & 0x100) || cp[11] != sep || cp[14] != sep) {
1502 /* Not hexadecimal numbers or invalid separators. */
1503 return false0;
1504 }
1505
1506 eth->addr[3] = (uint8_t)num3;
1507 eth->addr[4] = (uint8_t)num4;
1508 eth->addr[5] = (uint8_t)num5;
1509 if (cp[17] == '\0') {
1510 /* We got 6 bytes, so this is a MAC address (48 is not allowed as a mask). */
1511 *mask = 48;
1512 return true1;
1513 } else if (cp[17] != '/' || cp[20] != '\0') {
1514 /* Format not handled by this fast path. */
1515 return false0;
1516 }
1517
1518 int m1 = cp[18];
1519 int m2 = cp[19];
1520 if (m1 == '3' && m2 == '6') { /* Mask /36 */
1521 eth->addr[4] &= 0xf0;
1522 eth->addr[5] = 0;
1523 *mask = 36;
1524 return true1;
1525 }
1526 if (m1 == '2' && m2 == '8') { /* Mask /28 */
1527 eth->addr[3] &= 0xf0;
1528 eth->addr[4] = 0;
1529 eth->addr[5] = 0;
1530 *mask = 28;
1531 return true1;
1532 }
1533 /* Unsupported mask */
1534 return false0;
1535}
1536
1537/*
1538 * If "accept_mask" is false, cp must point to an address that consists
1539 * of exactly 6 (EUI-48) or 8 (EUI-64) bytes.
1540 * If "accept_mask" is true, parse an up-to-6-byte sequence with an optional
1541 * mask.
1542 */
1543static bool_Bool
1544parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1545 const bool_Bool accept_mask)
1546{
1547 int i;
1548 unsigned long num;
1549 char *p;
1550 char sep = '\0';
1551
1552 for (i = 0; i < EUI64_ADDR_LEN8; i++) {
1553 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1554 if (!g_ascii_isxdigit(*cp)((g_ascii_table[(guchar) (*cp)] & G_ASCII_XDIGIT) != 0))
1555 return false0;
1556 num = strtoul(cp, &p, 16);
1557 if (p == cp)
1558 return false0; /* failed */
1559 if (num > 0xFF)
1560 return false0; /* not a valid octet */
1561 eth->addr[i] = (uint8_t) num;
1562 cp = p; /* skip past the number */
1563
1564 /* OK, what character terminated the octet? */
1565 if (*cp == '/') {
1566 /* "/" - this has a mask. */
1567 if (!accept_mask) {
1568 /* Entries with masks are not allowed in this file. */
1569 return false0;
1570 }
1571 cp++; /* skip past the '/' to get to the mask */
1572 if (!g_ascii_isdigit(*cp)((g_ascii_table[(guchar) (*cp)] & G_ASCII_DIGIT) != 0))
1573 return false0; /* no sign allowed */
1574 num = strtoul(cp, &p, 10);
1575 if (p == cp)
1576 return false0; /* failed */
1577 cp = p; /* skip past the number */
1578 if (*cp != '\0' && !g_ascii_isspace(*cp)((g_ascii_table[(guchar) (*cp)] & G_ASCII_SPACE) != 0))
1579 return false0; /* bogus terminator */
1580 if (num == 0 || num >= 48)
1581 return false0; /* bogus mask */
1582 /* Mask out the bits not covered by the mask */
1583 *mask = (int)num;
1584 for (i = 0; num >= 8; i++, num -= 8)
1585 ; /* skip octets entirely covered by the mask */
1586 /* Mask out the first masked octet */
1587 eth->addr[i] &= (0xFF << (8 - num));
1588 i++;
1589 /* Mask out completely-masked-out octets */
1590 for (; i < 6; i++)
1591 eth->addr[i] = 0;
1592 return true1;
1593 }
1594 if (*cp == '\0') {
1595 /* We're at the end of the address, and there's no mask. */
1596 if (i == 2) {
1597 /* We got 3 bytes, so this is a manufacturer ID. */
1598 if (!accept_mask) {
1599 /* Manufacturer IDs are not allowed in this file */
1600 return false0;
1601 }
1602 /* Indicate that this is a manufacturer ID (0 is not allowed
1603 as a mask). */
1604 *mask = 0;
1605 return true1;
1606 }
1607
1608 if (i == 5) {
1609 /* We got 6 bytes, so this is a MAC address (48 is not allowed as a mask). */
1610 if (mask) {
1611 *mask = 48;
1612 }
1613 return true1;
1614 }
1615
1616 if (i == 7) {
1617 /* We got 8 bytes, so this is a EUI-64 address (64 is not allowed as a mask). */
1618 if (mask) {
1619 *mask = 64;
1620 }
1621 return true1;
1622 }
1623
1624 /* We didn't get 3 or 6 or 8 bytes, and there's no mask; this is
1625 illegal. */
1626 return false0;
1627 } else {
1628 if (sep == '\0') {
1629 /* We don't know the separator used in this number; it can either
1630 be ':', '-', or '.'. */
1631 if (*cp != ':' && *cp != '-' && *cp != '.')
1632 return false0;
1633 sep = *cp; /* subsequent separators must be the same */
1634 } else {
1635 /* It has to be the same as the first separator */
1636 if (*cp != sep)
1637 return false0;
1638 }
1639 }
1640 cp++;
1641 }
1642
1643 return true1;
1644}
1645
1646static int
1647parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1648 const bool_Bool accept_mask)
1649{
1650 /*
1651 * See the ethers(4) or ethers(5) man page for ethers file format
1652 * (not available on all systems).
1653 * We allow both ethernet address separators (':' and '-'),
1654 * as well as Wireshark's '.' separator.
1655 */
1656
1657 char *cp;
1658
1659 line = g_strstrip(line)g_strchomp (g_strchug (line));
1660 if (line[0] == '\0' || line[0] == '#')
1661 return -1;
1662
1663 if ((cp = strchr(line, '#'))) {
1664 *cp = '\0';
1665 g_strchomp(line);
1666 }
1667
1668 if ((cp = strtok(line, " \t")) == NULL((void*)0))
1669 return -1;
1670
1671 /* First try to match the common format for the large ethers file. */
1672 if (!parse_ether_address_fast(cp, eth, mask, accept_mask)) {
1673 /* Fallback for the well-known addresses (wka) file. */
1674 if (!parse_ether_address(cp, eth, mask, accept_mask))
1675 return -1;
1676 }
1677
1678 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
1679 return -1;
1680
1681 (void) g_strlcpy(eth->name, cp, MAXNAMELEN64);
1682
1683 if ((cp = strtok(NULL((void*)0), "\t")) != NULL((void*)0))
1684 {
1685 (void) g_strlcpy(eth->longname, cp, MAXNAMELEN64);
1686 } else {
1687 /* Make the long name the short name */
1688 (void) g_strlcpy(eth->longname, eth->name, MAXNAMELEN64);
1689 }
1690
1691 return 0;
1692
1693} /* parse_ether_line */
1694
1695static FILE *eth_p;
1696
1697static void
1698set_ethent(char *path)
1699{
1700 if (eth_p)
1701 rewind(eth_p);
1702 else
1703 eth_p = ws_fopenfopen(path, "r");
1704}
1705
1706static void
1707end_ethent(void)
1708{
1709 if (eth_p) {
1710 fclose(eth_p);
1711 eth_p = NULL((void*)0);
1712 }
1713}
1714
1715static ether_t *
1716get_ethent(unsigned int *mask, const bool_Bool accept_mask)
1717{
1718
1719 static ether_t eth;
1720 char buf[MAX_LINELEN1024];
1721
1722 if (eth_p == NULL((void*)0))
1723 return NULL((void*)0);
1724
1725 while (fgetline(buf, sizeof(buf), eth_p) >= 0) {
1726 if (parse_ether_line(buf, &eth, mask, accept_mask) == 0) {
1727 return &eth;
1728 }
1729 }
1730
1731 return NULL((void*)0);
1732
1733} /* get_ethent */
1734
1735static hashmanuf_t *
1736manuf_hash_new_entry(const uint8_t *addr, const char* name, const char* longname)
1737{
1738 unsigned manuf_key;
1739 hashmanuf_t *manuf_value;
1740 char *endp;
1741
1742 /* manuf needs only the 3 most significant octets of the ethernet address */
1743 manuf_key = (addr[0] << 16) + (addr[1] << 8) + addr[2];
1744 manuf_value = wmem_new(addr_resolv_scope, hashmanuf_t)((hashmanuf_t*)wmem_alloc((addr_resolv_scope), sizeof(hashmanuf_t
)))
;
1745
1746 memcpy(manuf_value->addr, addr, 3);
1747 if (name != NULL((void*)0)) {
1748 (void) g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN64);
1749 manuf_value->flags = NAME_RESOLVED(1U<<1);
1750 if (longname != NULL((void*)0)) {
1751 (void) g_strlcpy(manuf_value->resolved_longname, longname, MAXNAMELEN64);
1752 }
1753 else {
1754 (void) g_strlcpy(manuf_value->resolved_longname, name, MAXNAMELEN64);
1755 }
1756 }
1757 else {
1758 manuf_value->flags = 0;
1759 manuf_value->resolved_name[0] = '\0';
1760 manuf_value->resolved_longname[0] = '\0';
1761 }
1762 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1763 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1764 *endp = '\0';
1765
1766 wmem_map_insert(manuf_hashtable, GUINT_TO_POINTER(manuf_key)((gpointer) (gulong) (manuf_key)), manuf_value);
1767 return manuf_value;
1768}
1769
1770static hashwka_t*
1771wka_hash_new_entry(const uint8_t *addr, char* name)
1772{
1773 uint8_t *wka_key;
1774 hashwka_t *wka_value;
1775
1776 wka_key = (uint8_t *)wmem_alloc(addr_resolv_scope, 6);
1777 memcpy(wka_key, addr, 6);
1778
1779 wka_value = (hashwka_t*)wmem_new(addr_resolv_scope, hashwka_t)((hashwka_t*)wmem_alloc((addr_resolv_scope), sizeof(hashwka_t
)))
;
1780 wka_value->flags = NAME_RESOLVED(1U<<1);
1781 wka_value->name = wmem_strdup(addr_resolv_scope, name);
1782
1783 wmem_map_insert(wka_hashtable, wka_key, wka_value);
1784 return wka_value;
1785}
1786
1787static void
1788add_manuf_name(const uint8_t *addr, unsigned int mask, char *name, char *longname)
1789{
1790 switch (mask)
1791 {
1792 case 0:
1793 {
1794 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1795 hashmanuf_t *entry = manuf_hash_new_entry(addr, name, longname);
1796 entry->flags |= STATIC_HOSTNAME(1U<<3);
1797 break;
1798 }
1799 case 48:
1800 {
1801 /* This is a well-known MAC address; add it to the Ethernet hash table */
1802 add_eth_name(addr, name, true1);
1803 break;
1804 }
1805 default:
1806 {
1807 /* This is a range of well-known addresses; add it to the well-known-address table */
1808 hashwka_t *entry = wka_hash_new_entry(addr, name);
1809 entry->flags |= STATIC_HOSTNAME(1U<<3);
1810 break;
1811 }
1812 }
1813} /* add_manuf_name */
1814
1815/* XXX: manuf_name_lookup returns a hashmanuf_t*, which cannot hold a 28 or
1816 * 36 bit MA-M or MA-S. So it returns those as unresolved. For EUI-48 and
1817 * EUI-64, MA-M and MA-S should be checked for separately in the global
1818 * tables.
1819 *
1820 * XXX - size_t is used only in a ws_return_val_if() that checks
1821 * whether the argument has at least 3 bytes; that's done only if
1822 * assertions are enabled, so it's used only if assertions are
1823 * enabled. This means that, if assertions aren't enabled, a
1824 * warning that the argument is unused will be issued by at least
1825 * some compilers, so we mark it as unused. Should we do that
1826 * check unconditionally, and just emit a warning if assertions
1827 * are enabled?
1828 */
1829static hashmanuf_t *
1830manuf_name_lookup(const uint8_t *addr, size_t size _U___attribute__((unused)))
1831{
1832 uint32_t manuf_key;
1833 uint8_t oct;
1834 hashmanuf_t *manuf_value;
1835
1836 ws_return_val_if(size < 3, NULL)do { if (1 && (size < 3)) { ws_log_full("InvalidArg"
, LOG_LEVEL_WARNING, "epan/addr_resolv.c", 1836, __func__, "invalid argument: %s"
, "size < 3"); return (((void*)0)); } } while (0)
;
1837
1838 /* manuf needs only the 3 most significant octets of the ethernet address */
1839 manuf_key = addr[0];
1840 manuf_key = manuf_key<<8;
1841 oct = addr[1];
1842 manuf_key = manuf_key | oct;
1843 manuf_key = manuf_key<<8;
1844 oct = addr[2];
1845 manuf_key = manuf_key | oct;
1846
1847
1848 /* first try to find a "perfect match" */
1849 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, GUINT_TO_POINTER(manuf_key)((gpointer) (gulong) (manuf_key)));
1850 if (manuf_value != NULL((void*)0)) {
1851 manuf_value->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1852 return manuf_value;
1853 }
1854
1855 /* Mask out the broadcast/multicast flag but not the locally
1856 * administered flag as locally administered means: not assigned
1857 * by the IEEE but the local administrator instead.
1858 * 0x01 multicast / broadcast bit
1859 * 0x02 locally administered bit */
1860 if ((manuf_key & 0x00010000) != 0) {
1861 manuf_key &= 0x00FEFFFF;
1862 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, GUINT_TO_POINTER(manuf_key)((gpointer) (gulong) (manuf_key)));
1863 if (manuf_value != NULL((void*)0)) {
1864 manuf_value->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1865 return manuf_value;
1866 }
1867 }
1868
1869 /* Try the global manuf tables. */
1870 const char *short_name, *long_name;
1871 /* We can't insert a 28 or 36 bit entry into the used hash table. */
1872 short_name = ws_manuf_lookup_oui24(addr, &long_name);
1873 if (short_name != NULL((void*)0)) {
1874 /* Found it */
1875 manuf_value = manuf_hash_new_entry(addr, short_name, long_name);
1876 } else {
1877 /* Add the address as a hex string */
1878 manuf_value = manuf_hash_new_entry(addr, NULL((void*)0), NULL((void*)0));
1879 }
1880
1881 manuf_value->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1882 return manuf_value;
1883
1884} /* manuf_name_lookup */
1885
1886static char *
1887wka_name_lookup(const uint8_t *addr, const unsigned int mask)
1888{
1889 uint8_t masked_addr[6];
1890 unsigned num;
1891 int i;
1892 hashwka_t *value;
1893
1894 if (wka_hashtable == NULL((void*)0)) {
1895 return NULL((void*)0);
1896 }
1897 /* Get the part of the address covered by the mask. */
1898 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1899 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1900 /* Mask out the first masked octet */
1901 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1902 i++;
1903 /* Zero out completely-masked-out octets */
1904 for (; i < 6; i++)
1905 masked_addr[i] = 0;
1906
1907 value = (hashwka_t*)wmem_map_lookup(wka_hashtable, masked_addr);
1908
1909 if (value) {
1910 value->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
1911 return value->name;
1912 }
1913
1914 return NULL((void*)0);
1915
1916} /* wka_name_lookup */
1917
1918unsigned get_hash_ether_status(hashether_t* ether)
1919{
1920 return ether->flags;
1921}
1922
1923bool_Bool get_hash_ether_used(hashether_t* ether)
1924{
1925 return ((ether->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1))) == TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1)));
1926}
1927
1928char* get_hash_ether_hexaddr(hashether_t* ether)
1929{
1930 return ether->hexaddr;
1931}
1932
1933char* get_hash_ether_resolved_name(hashether_t* ether)
1934{
1935 return ether->resolved_name;
1936}
1937
1938bool_Bool get_hash_wka_used(hashwka_t* wka)
1939{
1940 return ((wka->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1))) == TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1)));
1941}
1942
1943char* get_hash_wka_resolved_name(hashwka_t* wka)
1944{
1945 return wka->name;
1946}
1947
1948static unsigned
1949eth_addr_hash(const void *key)
1950{
1951 return wmem_strong_hash((const uint8_t *)key, 6);
1952}
1953
1954static gboolean
1955eth_addr_cmp(const void *a, const void *b)
1956{
1957 return (memcmp(a, b, 6) == 0);
1958}
1959
1960static unsigned
1961eui64_addr_hash(const void *key)
1962{
1963 return wmem_strong_hash((const uint8_t *)key, EUI64_ADDR_LEN8);
1964}
1965
1966static gboolean
1967eui64_addr_cmp(const void *a, const void *b)
1968{
1969 return (memcmp(a, b, EUI64_ADDR_LEN8) == 0);
1970}
1971
1972static void
1973initialize_ethers(void)
1974{
1975 ether_t *eth;
1976 unsigned mask = 0;
1977
1978 /* hash table initialization */
1979 ws_assert(wka_hashtable == NULL)do { if ((1) && !(wka_hashtable == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 1979, __func__, "assertion failed: %s"
, "wka_hashtable == ((void*)0)"); } while (0)
;
1980 wka_hashtable = wmem_map_new(addr_resolv_scope, eth_addr_hash, eth_addr_cmp);
1981 ws_assert(manuf_hashtable == NULL)do { if ((1) && !(manuf_hashtable == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 1981, __func__, "assertion failed: %s"
, "manuf_hashtable == ((void*)0)"); } while (0)
;
1982 manuf_hashtable = wmem_map_new(addr_resolv_scope, g_direct_hash, g_direct_equal);
1983 ws_assert(eth_hashtable == NULL)do { if ((1) && !(eth_hashtable == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 1983, __func__, "assertion failed: %s"
, "eth_hashtable == ((void*)0)"); } while (0)
;
1984 eth_hashtable = wmem_map_new(addr_resolv_scope, eth_addr_hash, eth_addr_cmp);
1985 ws_assert(eui64_hashtable == NULL)do { if ((1) && !(eui64_hashtable == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 1985, __func__, "assertion failed: %s"
, "eui64_hashtable == ((void*)0)"); } while (0)
;
1986 eui64_hashtable = wmem_map_new(addr_resolv_scope, eui64_addr_hash, eui64_addr_cmp);
1987
1988 /* Compute the pathname of the ethers file. */
1989 if (g_ethers_path == NULL((void*)0)) {
1990 g_ethers_path = g_build_filename(get_systemfile_dir(), ENAME_ETHERS"ethers", NULL((void*)0));
1991 }
1992
1993 /* Compute the pathname of the personal ethers file. */
1994 if (g_pethers_path == NULL((void*)0)) {
1995 /* Check profile directory before personal configuration */
1996 g_pethers_path = get_persconffile_path(ENAME_ETHERS"ethers", true1);
1997 if (!file_exists(g_pethers_path)) {
1998 g_free(g_pethers_path);
1999 g_pethers_path = get_persconffile_path(ENAME_ETHERS"ethers", false0);
2000 }
2001 }
2002
2003 /* Compute the pathname of the global manuf file */
2004 if (g_manuf_path == NULL((void*)0))
2005 g_manuf_path = get_datafile_path(ENAME_MANUF"manuf");
2006 /* Read it and initialize the hash table */
2007 if (file_exists(g_manuf_path)) {
2008 set_ethent(g_manuf_path);
2009 while ((eth = get_ethent(&mask, true1))) {
2010 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
2011 }
2012 end_ethent();
2013 }
2014
2015 /* Compute the pathname of the personal manuf file */
2016 if (g_pmanuf_path == NULL((void*)0)) {
2017 /* Check profile directory before personal configuration */
2018 g_pmanuf_path = get_persconffile_path(ENAME_MANUF"manuf", true1);
2019 if (!file_exists(g_pmanuf_path)) {
2020 g_free(g_pmanuf_path);
2021 g_pmanuf_path = get_persconffile_path(ENAME_MANUF"manuf", false0);
2022 }
2023 }
2024 /* Read it and initialize the hash table */
2025 if (file_exists(g_pmanuf_path)) {
2026 set_ethent(g_pmanuf_path);
2027 while ((eth = get_ethent(&mask, true1))) {
2028 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
2029 }
2030 end_ethent();
2031 }
2032
2033 /* Compute the pathname of the wka file */
2034 if (g_wka_path == NULL((void*)0))
2035 g_wka_path = get_datafile_path(ENAME_WKA"wka");
2036
2037 /* Read it and initialize the hash table */
2038 set_ethent(g_wka_path);
2039 while ((eth = get_ethent(&mask, true1))) {
2040 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
2041 }
2042 end_ethent();
2043
2044 /* Look at the ethers files last. These are set as static names,
2045 * so they override earlier entries, and the ones we read last
2046 * take precedence. Order of precedence is personal ethers file,
2047 * global ethers file, wka file, personal manuf file, global manuf
2048 * file, and then non-static sources like ARP Eth -> IP hostname
2049 * discovery (if enabled), NRB entries (if wiretap adds support for
2050 * EUI-48 in NRBs), etc.
2051 * XXX: What _is_ the proper order of precedence, and should it
2052 * be configurable? (cf. #18075) */
2053 set_ethent(g_ethers_path);
2054 while ((eth = get_ethent(&mask, false0))) {
2055 if (mask == 48) {
2056 add_eth_name(eth->addr, eth->name, true1);
2057 } else if (mask == 64) {
2058 add_eui64_name(eth->addr, eth->name, true1);
2059 }
2060 }
2061 end_ethent();
2062
2063 if (file_exists(g_pethers_path)) {
2064 set_ethent(g_pethers_path);
2065 while ((eth = get_ethent(&mask, false0))) {
2066 if (mask == 48) {
2067 add_eth_name(eth->addr, eth->name, true1);
2068 } else if (mask == 64) {
2069 add_eui64_name(eth->addr, eth->name, true1);
2070 }
2071 }
2072 end_ethent();
2073 }
2074
2075} /* initialize_ethers */
2076
2077static void
2078ethers_cleanup(void)
2079{
2080 wka_hashtable = NULL((void*)0);
2081 manuf_hashtable = NULL((void*)0);
2082 eth_hashtable = NULL((void*)0);
2083 eui64_hashtable = NULL((void*)0);
2084 g_free(g_ethers_path);
2085 g_ethers_path = NULL((void*)0);
2086 g_free(g_pethers_path);
2087 g_pethers_path = NULL((void*)0);
2088 g_free(g_manuf_path);
2089 g_manuf_path = NULL((void*)0);
2090 g_free(g_pmanuf_path);
2091 g_pmanuf_path = NULL((void*)0);
2092 g_free(g_wka_path);
2093 g_wka_path = NULL((void*)0);
2094}
2095
2096static void
2097eth_resolved_name_fill(hashether_t *tp, const char *name, unsigned mask, const uint8_t *addr)
2098{
2099 switch (mask) {
2100 case 24:
2101 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x",
2102 name, addr[3], addr[4], addr[5]);
2103 break;
2104 case 28:
2105 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%01x:%02x:%02x",
2106 name, addr[3] & 0x0F, addr[4], addr[5]);
2107 break;
2108 case 36:
2109 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%01x:%02x",
2110 name, addr[4] & 0x0F, addr[5]);
2111 break;
2112 default: // Future-proof generic algorithm
2113 {
2114 unsigned bytes = mask / 8;
2115 unsigned bitmask = mask % 8;
2116
2117 int pos = snprintf(tp->resolved_name, MAXNAMELEN64, "%s", name);
2118 if (pos >= MAXNAMELEN64) return;
2119
2120 if (bytes < 6) {
2121 pos += snprintf(tp->resolved_name + pos, MAXNAMELEN64 - pos,
2122 bitmask >= 4 ? "_%01x" : "_%02x",
2123 addr[bytes] & (0xFF >> bitmask));
2124 bytes++;
2125 }
2126
2127 while (bytes < 6) {
2128 if (pos >= MAXNAMELEN64) return;
2129 pos += snprintf(tp->resolved_name + pos, MAXNAMELEN64 - pos, ":%02x",
2130 addr[bytes]);
2131 bytes++;
2132 }
2133 }
2134 }
2135}
2136
2137/* Resolve ethernet address */
2138static hashether_t *
2139eth_addr_resolve(hashether_t *tp) {
2140 hashmanuf_t *manuf_value;
2141 const uint8_t *addr = tp->addr;
2142 size_t addr_size = sizeof(tp->addr);
2143
2144 if (!(tp->flags & NAME_RESOLVED(1U<<1))) {
2145 unsigned mask;
2146 char *name;
2147 address ether_addr;
2148
2149 /* Unknown name. Try looking for it in the well-known-address
2150 tables for well-known address ranges smaller than 2^24. */
2151 mask = 7;
2152 do {
2153 /* Only the topmost 5 bytes participate fully */
2154 if ((name = wka_name_lookup(addr, mask+40)) != NULL((void*)0)) {
2155 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x",
2156 name, addr[5] & (0xFF >> mask));
2157 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2158 return tp;
2159 }
2160 } while (mask--);
2161
2162 mask = 7;
2163 do {
2164 /* Only the topmost 4 bytes participate fully */
2165 if ((name = wka_name_lookup(addr, mask+32)) != NULL((void*)0)) {
2166 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x",
2167 name, addr[4] & (0xFF >> mask), addr[5]);
2168 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2169 return tp;
2170 }
2171 } while (mask--);
2172
2173 mask = 7;
2174 do {
2175 /* Only the topmost 3 bytes participate fully */
2176 if ((name = wka_name_lookup(addr, mask+24)) != NULL((void*)0)) {
2177 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x",
2178 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
2179 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2180 return tp;
2181 }
2182 } while (mask--);
2183
2184 /* Now try looking in the manufacturer table. */
2185 manuf_value = manuf_name_lookup(addr, addr_size);
2186 if ((manuf_value != NULL((void*)0)) && ((manuf_value->flags & NAME_RESOLVED(1U<<1)) == NAME_RESOLVED(1U<<1))) {
2187 snprintf(tp->resolved_name, MAXNAMELEN64, "%.*s_%02x:%02x:%02x",
2188 MAXNAMELEN64 - 10, manuf_value->resolved_name, addr[3], addr[4], addr[5]);
2189 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2190 return tp;
2191 }
2192
2193 /* Now try looking for it in the well-known-address
2194 tables for well-known address ranges larger than 2^24. */
2195 mask = 7;
2196 do {
2197 /* Only the topmost 2 bytes participate fully */
2198 if ((name = wka_name_lookup(addr, mask+16)) != NULL((void*)0)) {
2199 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x:%02x",
2200 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
2201 addr[5]);
2202 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2203 return tp;
2204 }
2205 } while (mask--);
2206
2207 mask = 7;
2208 do {
2209 /* Only the topmost byte participates fully */
2210 if ((name = wka_name_lookup(addr, mask+8)) != NULL((void*)0)) {
2211 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x:%02x:%02x",
2212 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
2213 addr[4], addr[5]);
2214 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2215 return tp;
2216 }
2217 } while (mask--);
2218
2219 mask = 7;
2220 do {
2221 /* Not even the topmost byte participates fully */
2222 if ((name = wka_name_lookup(addr, mask)) != NULL((void*)0)) {
2223 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
2224 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
2225 addr[3], addr[4], addr[5]);
2226 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2227 return tp;
2228 }
2229 } while (--mask); /* Work down to the last bit */
2230
2231 /* Now try looking in the global manuf data for a MA-M or MA-S
2232 * match. We do this last so that the other files override this
2233 * result.
2234 */
2235 const char *short_name, *long_name;
2236 short_name = ws_manuf_lookup(addr, &long_name, &mask);
2237 if (short_name != NULL((void*)0)) {
2238 if (mask == 24) {
2239 /* This shouldn't happen as it should be handled above,
2240 * but it doesn't hurt.
2241 */
2242 manuf_hash_new_entry(addr, short_name, long_name);
2243 }
2244 eth_resolved_name_fill(tp, short_name, mask, addr);
2245 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2246 return tp;
2247 }
2248 /* No match whatsoever. */
2249 set_address(&ether_addr, AT_ETHER, 6, addr);
2250 address_to_str_buf(&ether_addr, tp->resolved_name, MAXNAMELEN64);
2251 return tp;
2252 }
2253 return tp;
2254} /* eth_addr_resolve */
2255
2256static hashether_t *
2257eth_hash_new_entry(const uint8_t *addr, const bool_Bool resolve)
2258{
2259 hashether_t *tp;
2260 char *endp;
2261
2262 tp = wmem_new(addr_resolv_scope, hashether_t)((hashether_t*)wmem_alloc((addr_resolv_scope), sizeof(hashether_t
)))
;
2263 memcpy(tp->addr, addr, sizeof(tp->addr));
2264 tp->flags = 0;
2265 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
2266 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
2267 *endp = '\0';
2268 tp->resolved_name[0] = '\0';
2269
2270 if (resolve)
2271 eth_addr_resolve(tp);
2272
2273 wmem_map_insert(eth_hashtable, tp->addr, tp);
2274
2275 return tp;
2276} /* eth_hash_new_entry */
2277
2278static hashether_t *
2279add_eth_name(const uint8_t *addr, const char *name, bool_Bool static_entry)
2280{
2281 hashether_t *tp;
2282
2283 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
2284
2285 if (tp == NULL((void*)0)) {
2286 tp = eth_hash_new_entry(addr, false0);
2287 }
2288
2289 if (strcmp(tp->resolved_name, name) != 0 && (static_entry || !(tp->flags & STATIC_HOSTNAME(1U<<3)))) {
2290 (void) g_strlcpy(tp->resolved_name, name, MAXNAMELEN64);
2291 tp->flags |= NAME_RESOLVED(1U<<1);
2292 if (static_entry) {
2293 tp->flags |= STATIC_HOSTNAME(1U<<3);
2294 }
2295 new_resolved_objects = true1;
2296 }
2297
2298 return tp;
2299} /* add_eth_name */
2300
2301static hashether_t *
2302eth_name_lookup(const uint8_t *addr, const bool_Bool resolve)
2303{
2304 hashether_t *tp;
2305
2306 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
2307
2308 if (tp == NULL((void*)0)) {
2309 tp = eth_hash_new_entry(addr, resolve);
2310 } else {
2311 if (resolve && !(tp->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1)))) {
2312 eth_addr_resolve(tp); /* Found but needs to be resolved */
2313 }
2314 }
2315 if (resolve) {
2316 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
2317 }
2318
2319 return tp;
2320
2321} /* eth_name_lookup */
2322
2323static void
2324eui64_resolved_name_fill(hasheui64_t *tp, const char *name, unsigned mask, const uint8_t *addr)
2325{
2326 switch (mask) {
2327 case 24:
2328 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%02x:%02x:%02x:%02x:%02x",
2329 name, addr[3], addr[4], addr[5], addr[6], addr[7]);
2330 break;
2331 case 28:
2332 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%01x:%02x:%02x:%02x:%02x",
2333 name, addr[3] & 0x0F, addr[4], addr[5], addr[6], addr[7]);
2334 break;
2335 case 36:
2336 snprintf(tp->resolved_name, MAXNAMELEN64, "%s_%01x:%02x:%02x:%02x",
2337 name, addr[4] & 0x0F, addr[5], addr[6], addr[7]);
2338 break;
2339 default: // Future-proof generic algorithm
2340 {
2341 unsigned bytes = mask / 8;
2342 unsigned bitmask = mask % 8;
2343
2344 int pos = snprintf(tp->resolved_name, MAXNAMELEN64, "%s", name);
2345 if (pos >= MAXNAMELEN64) return;
2346
2347 if (bytes < EUI64_ADDR_LEN8) {
2348 pos += snprintf(tp->resolved_name + pos, MAXNAMELEN64 - pos,
2349 bitmask >= 4 ? "_%01x" : "_%02x",
2350 addr[bytes] & (0xFF >> bitmask));
2351 bytes++;
2352 }
2353
2354 while (bytes < EUI64_ADDR_LEN8) {
2355 if (pos >= MAXNAMELEN64) return;
2356 pos += snprintf(tp->resolved_name + pos, MAXNAMELEN64 - pos, ":%02x",
2357 addr[bytes]);
2358 bytes++;
2359 }
2360 }
2361 }
2362}
2363
2364/* Resolve EUI-64 address */
2365static hasheui64_t *
2366eui64_addr_resolve(hasheui64_t *tp)
2367{
2368 hashmanuf_t *manuf_value;
2369 const uint8_t *addr = tp->addr;
2370 size_t addr_size = sizeof(tp->addr);
2371
2372 if (!(tp->flags & NAME_RESOLVED(1U<<1))) {
2373 unsigned mask;
2374 address eui64_addr;
2375 /* manuf_name_lookup returns a hashmanuf_t* that covers an entire /24,
2376 * so we can't properly use it for MA-M and MA-S. We do want to check
2377 * it first so it also covers the user-defined tables.
2378 */
2379 manuf_value = manuf_name_lookup(addr, addr_size);
2380 if ((manuf_value != NULL((void*)0)) && ((manuf_value->flags & NAME_RESOLVED(1U<<1)) == NAME_RESOLVED(1U<<1))) {
2381 snprintf(tp->resolved_name, MAXNAMELEN64, "%.*s_%02x:%02x:%02x:%02x:%02x",
2382 MAXNAMELEN64 - 16, manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
2383 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2384 return tp;
2385 }
2386
2387 /* Now try looking in the global manuf data for a MA-M or MA-S
2388 * match. We do this last so that the other files override this
2389 * result.
2390 */
2391 const char *short_name, *long_name;
2392 short_name = ws_manuf_lookup(addr, &long_name, &mask);
2393 if (short_name != NULL((void*)0)) {
2394 if (mask == 24) {
2395 /* This shouldn't happen as it should be handled above,
2396 * but it doesn't hurt.
2397 */
2398 manuf_hash_new_entry(addr, short_name, long_name);
2399 }
2400 eui64_resolved_name_fill(tp, short_name, mask, addr);
2401 tp->flags |= NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4);
2402 return tp;
2403 }
2404 /* No match whatsoever. */
2405 set_address(&eui64_addr, AT_EUI64, 8, addr);
2406 address_to_str_buf(&eui64_addr, tp->resolved_name, MAXNAMELEN64);
2407 return tp;
2408 }
2409
2410 return tp;
2411} /* eui64_addr_resolve */
2412
2413static hasheui64_t *
2414eui64_hash_new_entry(const uint8_t *addr, const bool_Bool resolve)
2415{
2416 hasheui64_t *tp;
2417 char *endp;
2418
2419 tp = wmem_new(addr_resolv_scope, hasheui64_t)((hasheui64_t*)wmem_alloc((addr_resolv_scope), sizeof(hasheui64_t
)))
;
2420 memcpy(tp->addr, addr, sizeof(tp->addr));
2421 tp->flags = 0;
2422 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
2423 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
2424 *endp = '\0';
2425 tp->resolved_name[0] = '\0';
2426
2427 if (resolve)
2428 eui64_addr_resolve(tp);
2429
2430 wmem_map_insert(eui64_hashtable, tp->addr, tp);
2431
2432 return tp;
2433} /* eui64_hash_new_entry */
2434
2435static hasheui64_t *
2436add_eui64_name(const uint8_t *addr, const char *name, bool_Bool static_entry)
2437{
2438 hasheui64_t *tp;
2439
2440 tp = (hasheui64_t *)wmem_map_lookup(eui64_hashtable, addr);
2441
2442 if (tp == NULL((void*)0)) {
2443 tp = eui64_hash_new_entry(addr, false0);
2444 }
2445
2446 if (strcmp(tp->resolved_name, name) != 0 && (static_entry || !(tp->flags & STATIC_HOSTNAME(1U<<3)))) {
2447 (void) g_strlcpy(tp->resolved_name, name, MAXNAMELEN64);
2448 tp->flags |= NAME_RESOLVED(1U<<1);
2449 if (static_entry) {
2450 tp->flags |= STATIC_HOSTNAME(1U<<3);
2451 }
2452 new_resolved_objects = true1;
2453 }
2454
2455 return tp;
2456} /* add_eui64_name */
2457
2458static hasheui64_t *
2459eui64_name_lookup(const uint8_t *addr, const bool_Bool resolve)
2460{
2461 hasheui64_t *tp;
2462
2463 tp = (hasheui64_t *)wmem_map_lookup(eui64_hashtable, addr);
2464
2465 if (tp == NULL((void*)0)) {
2466 tp = eui64_hash_new_entry(addr, resolve);
2467 } else {
2468 if (resolve && !(tp->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1)))) {
2469 eui64_addr_resolve(tp); /* Found but needs to be resolved */
2470 }
2471 }
2472 if (resolve) {
2473 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0);
2474 }
2475
2476 return tp;
2477
2478} /* eui64_name_lookup */
2479
2480/* IPXNETS */
2481static int
2482parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
2483{
2484 /*
2485 * We allow three address separators (':', '-', and '.'),
2486 * as well as no separators
2487 */
2488
2489 char *cp;
2490 uint32_t a, a0, a1, a2, a3;
2491 bool_Bool found_single_number = false0;
2492
2493 if ((cp = strchr(line, '#')))
2494 *cp = '\0';
2495
2496 if ((cp = strtok(line, " \t\n")) == NULL((void*)0))
2497 return -1;
2498
2499 /* Either fill a0,a1,a2,a3 and found_single_number is false,
2500 * fill a and found_single_number is true,
2501 * or return -1
2502 */
2503 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
2504 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
2505 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
2506 if (sscanf(cp, "%x", &a) == 1) {
2507 found_single_number = true1;
2508 }
2509 else {
2510 return -1;
2511 }
2512 }
2513 }
2514 }
2515
2516 if ((cp = strtok(NULL((void*)0), " \t\n")) == NULL((void*)0))
2517 return -1;
2518
2519 if (found_single_number) {
2520 ipxnet->addr = a;
2521 }
2522 else {
2523 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
2524 }
2525
2526 (void) g_strlcpy(ipxnet->name, cp, MAXNAMELEN64);
2527
2528 return 0;
2529
2530} /* parse_ipxnets_line */
2531
2532static FILE *ipxnet_p;
2533
2534static void
2535set_ipxnetent(char *path)
2536{
2537 if (ipxnet_p)
2538 rewind(ipxnet_p);
2539 else
2540 ipxnet_p = ws_fopenfopen(path, "r");
2541}
2542
2543static void
2544end_ipxnetent(void)
2545{
2546 if (ipxnet_p) {
2547 fclose(ipxnet_p);
2548 ipxnet_p = NULL((void*)0);
2549 }
2550}
2551
2552static ipxnet_t *
2553get_ipxnetent(void)
2554{
2555
2556 static ipxnet_t ipxnet;
2557 char buf[MAX_LINELEN1024];
2558
2559 if (ipxnet_p == NULL((void*)0))
2560 return NULL((void*)0);
2561
2562 while (fgetline(buf, sizeof(buf), ipxnet_p) >= 0) {
2563 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
2564 return &ipxnet;
2565 }
2566 }
2567
2568 return NULL((void*)0);
2569
2570} /* get_ipxnetent */
2571
2572static ipxnet_t *
2573get_ipxnetbyaddr(uint32_t addr)
2574{
2575 ipxnet_t *ipxnet;
2576
2577 set_ipxnetent(g_ipxnets_path);
2578
2579 while (((ipxnet = get_ipxnetent()) != NULL((void*)0)) && (addr != ipxnet->addr) ) ;
2580
2581 if (ipxnet == NULL((void*)0)) {
2582 end_ipxnetent();
2583
2584 set_ipxnetent(g_pipxnets_path);
2585
2586 while (((ipxnet = get_ipxnetent()) != NULL((void*)0)) && (addr != ipxnet->addr) )
2587 ;
2588
2589 end_ipxnetent();
2590 }
2591
2592 return ipxnet;
2593
2594} /* get_ipxnetbyaddr */
2595
2596static void
2597initialize_ipxnets(void)
2598{
2599 /* Compute the pathname of the ipxnets file.
2600 *
2601 * XXX - is there a notion of an "ipxnets file" in any flavor of
2602 * UNIX, or with any add-on Netware package for UNIX? If not,
2603 * should the UNIX version of the ipxnets file be in the datafile
2604 * directory as well?
2605 */
2606 if (g_ipxnets_path == NULL((void*)0)) {
2607 g_ipxnets_path = wmem_strdup_printf(addr_resolv_scope, "%s" G_DIR_SEPARATOR_S"/" "%s",
2608 get_systemfile_dir(), ENAME_IPXNETS"ipxnets");
2609 }
2610
2611 /* Set g_pipxnets_path here, but don't actually do anything
2612 * with it. It's used in get_ipxnetbyaddr().
2613 */
2614 if (g_pipxnets_path == NULL((void*)0)) {
2615 /* Check profile directory before personal configuration */
2616 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS"ipxnets", true1);
2617 if (!file_exists(g_pipxnets_path)) {
2618 g_free(g_pipxnets_path);
2619 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS"ipxnets", false0);
2620 }
2621 }
2622
2623} /* initialize_ipxnets */
2624
2625static void
2626ipx_name_lookup_cleanup(void)
2627{
2628 g_ipxnets_path = NULL((void*)0);
2629 g_free(g_pipxnets_path);
2630 g_pipxnets_path = NULL((void*)0);
2631}
2632
2633static char *
2634ipxnet_name_lookup(wmem_allocator_t *allocator, const unsigned addr)
2635{
2636 hashipxnet_t *tp;
2637 ipxnet_t *ipxnet;
2638
2639 tp = (hashipxnet_t *)wmem_map_lookup(ipxnet_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)));
2640 if (tp == NULL((void*)0)) {
2641 tp = wmem_new(addr_resolv_scope, hashipxnet_t)((hashipxnet_t*)wmem_alloc((addr_resolv_scope), sizeof(hashipxnet_t
)))
;
2642 wmem_map_insert(ipxnet_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)), tp);
2643 } else {
2644 return wmem_strdup(allocator, tp->name);
2645 }
2646
2647 /* fill in a new entry */
2648
2649 tp->addr = addr;
2650
2651 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL((void*)0)) {
2652 /* unknown name */
2653 snprintf(tp->name, MAXNAMELEN64, "%X", addr);
2654
2655 } else {
2656 (void) g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN64);
2657 }
2658
2659 return wmem_strdup(allocator, tp->name);
2660
2661} /* ipxnet_name_lookup */
2662
2663/* VLANS */
2664static int
2665parse_vlan_line(char *line, vlan_t *vlan)
2666{
2667 char *cp;
2668 uint16_t id;
2669
2670 if ((cp = strchr(line, '#')))
2671 *cp = '\0';
2672
2673 if ((cp = strtok(line, " \t\n")) == NULL((void*)0))
2674 return -1;
2675
2676 if (sscanf(cp, "%" SCNu16"hu", &id) == 1) {
2677 vlan->id = id;
2678 }
2679 else {
2680 return -1;
2681 }
2682
2683 if ((cp = strtok(NULL((void*)0), "\t\n")) == NULL((void*)0))
2684 return -1;
2685
2686 (void) g_strlcpy(vlan->name, cp, MAXVLANNAMELEN128);
2687
2688 return 0;
2689
2690} /* parse_vlan_line */
2691
2692static FILE *vlan_p;
2693
2694static void
2695set_vlanent(char *path)
2696{
2697 if (vlan_p)
2698 rewind(vlan_p);
2699 else
2700 vlan_p = ws_fopenfopen(path, "r");
2701}
2702
2703static void
2704end_vlanent(void)
2705{
2706 if (vlan_p) {
2707 fclose(vlan_p);
2708 vlan_p = NULL((void*)0);
2709 }
2710}
2711
2712static vlan_t *
2713get_vlanent(void)
2714{
2715
2716 static vlan_t vlan;
2717 char buf[MAX_LINELEN1024];
2718
2719 if (vlan_p == NULL((void*)0))
2720 return NULL((void*)0);
2721
2722 while (fgetline(buf, sizeof(buf), vlan_p) >= 0) {
2723 if (parse_vlan_line(buf, &vlan) == 0) {
2724 return &vlan;
2725 }
2726 }
2727
2728 return NULL((void*)0);
2729
2730} /* get_vlanent */
2731
2732static vlan_t *
2733get_vlannamebyid(uint16_t id)
2734{
2735 vlan_t *vlan;
2736
2737 set_vlanent(g_pvlan_path);
2738
2739 while (((vlan = get_vlanent()) != NULL((void*)0)) && (id != vlan->id) ) ;
2740
2741 if (vlan == NULL((void*)0)) {
2742 end_vlanent();
2743
2744 }
2745
2746 return vlan;
2747
2748} /* get_vlannamebyid */
2749
2750static void
2751initialize_vlans(void)
2752{
2753 ws_assert(vlan_hash_table == NULL)do { if ((1) && !(vlan_hash_table == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 2753, __func__, "assertion failed: %s"
, "vlan_hash_table == ((void*)0)"); } while (0)
;
2754 vlan_hash_table = wmem_map_new(addr_resolv_scope, g_direct_hash, g_direct_equal);
2755
2756 /* Set g_pvlan_path here, but don't actually do anything
2757 * with it. It's used in get_vlannamebyid()
2758 */
2759 if (g_pvlan_path == NULL((void*)0)) {
2760 /* Check profile directory before personal configuration */
2761 g_pvlan_path = get_persconffile_path(ENAME_VLANS"vlans", true1);
2762 if (!file_exists(g_pvlan_path)) {
2763 g_free(g_pvlan_path);
2764 g_pvlan_path = get_persconffile_path(ENAME_VLANS"vlans", false0);
2765 }
2766 }
2767} /* initialize_vlans */
2768
2769static void
2770vlan_name_lookup_cleanup(void)
2771{
2772 end_vlanent();
2773 vlan_hash_table = NULL((void*)0);
2774 g_free(g_pvlan_path);
2775 g_pvlan_path = NULL((void*)0);
2776}
2777
2778static const char *
2779vlan_name_lookup(const unsigned id)
2780{
2781 hashvlan_t *tp;
2782 vlan_t *vlan;
2783
2784 tp = (hashvlan_t *)wmem_map_lookup(vlan_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)));
2785 if (tp == NULL((void*)0)) {
2786 tp = wmem_new(addr_resolv_scope, hashvlan_t)((hashvlan_t*)wmem_alloc((addr_resolv_scope), sizeof(hashvlan_t
)))
;
2787 wmem_map_insert(vlan_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)), tp);
2788 } else {
2789 return tp->name;
2790 }
2791
2792 /* fill in a new entry */
2793
2794 tp->id = id;
2795
2796 if ( (vlan = get_vlannamebyid(id)) == NULL((void*)0)) {
2797 /* unknown name */
2798 snprintf(tp->name, MAXVLANNAMELEN128, "<%u>", id);
2799
2800 } else {
2801 (void) g_strlcpy(tp->name, vlan->name, MAXVLANNAMELEN128);
2802 }
2803
2804 return tp->name;
2805
2806} /* vlan_name_lookup */
2807/* VLAN END */
2808
2809static bool_Bool
2810read_hosts_file (const char *hostspath, bool_Bool store_entries)
2811{
2812 FILE *hf;
2813 char line[MAX_LINELEN1024];
2814 char *cp;
2815 union {
2816 uint32_t ip4_addr;
2817 ws_in6_addr ip6_addr;
2818 } host_addr;
2819 bool_Bool is_ipv6, entry_found = false0;
2820
2821 /*
2822 * See the hosts(4) or hosts(5) man page for hosts file format
2823 * (not available on all systems).
2824 */
2825 if ((hf = ws_fopenfopen(hostspath, "r")) == NULL((void*)0))
2826 return false0;
2827
2828 while (fgetline(line, sizeof(line), hf) >= 0) {
2829 if ((cp = strchr(line, '#')))
2830 *cp = '\0';
2831
2832 if ((cp = strtok(line, " \t")) == NULL((void*)0))
2833 continue; /* no tokens in the line */
2834
2835 if (ws_inet_pton6(cp, &host_addr.ip6_addr)) {
2836 /* Valid IPv6 */
2837 is_ipv6 = true1;
2838 } else if (ws_inet_pton4(cp, &host_addr.ip4_addr)) {
2839 /* Valid IPv4 */
2840 is_ipv6 = false0;
2841 } else {
2842 continue;
2843 }
2844
2845 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
2846 continue; /* no host name */
2847
2848 entry_found = true1;
2849 if (store_entries) {
2850 if (is_ipv6) {
2851 add_ipv6_name(&host_addr.ip6_addr, cp, true1);
2852 } else {
2853 add_ipv4_name(host_addr.ip4_addr, cp, true1);
2854 }
2855 }
2856 }
2857
2858 fclose(hf);
2859 return entry_found ? true1 : false0;
2860} /* read_hosts_file */
2861
2862bool_Bool
2863add_hosts_file (const char *hosts_file)
2864{
2865 bool_Bool found = false0;
2866 unsigned i;
2867
2868 if (!hosts_file)
2869 return false0;
2870
2871 if (!extra_hosts_files)
2872 extra_hosts_files = g_ptr_array_new();
2873
2874 for (i = 0; i < extra_hosts_files->len; i++) {
2875 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)((extra_hosts_files)->pdata)[i]) == 0)
2876 found = true1;
2877 }
2878
2879 if (!found) {
2880 g_ptr_array_add(extra_hosts_files, wmem_strdup(wmem_epan_scope(), hosts_file));
2881 return read_hosts_file (hosts_file, false0);
2882 }
2883 return true1;
2884}
2885
2886bool_Bool
2887add_ip_name_from_string (const char *addr, const char *name)
2888{
2889 union {
2890 uint32_t ip4_addr;
2891 ws_in6_addr ip6_addr;
2892 } host_addr;
2893 bool_Bool is_ipv6;
2894 resolved_name_t *resolved_entry;
2895
2896 if (ws_inet_pton6(addr, &host_addr.ip6_addr)) {
2897 is_ipv6 = true1;
2898 } else if (ws_inet_pton4(addr, &host_addr.ip4_addr)) {
2899 is_ipv6 = false0;
2900 } else {
2901 return false0;
2902 }
2903
2904 if (is_ipv6) {
2905 resolved_entry = (resolved_name_t*)wmem_map_lookup(manually_resolved_ipv6_list, &host_addr.ip6_addr);
2906 if (resolved_entry)
2907 {
2908 // If we found a previous matching key (IP address), then just update the value (custom hostname);
2909 (void) g_strlcpy(resolved_entry->name, name, MAXDNSNAMELEN256);
2910 }
2911 else
2912 {
2913 // Add a new mapping entry, if this IP address isn't already in the list.
2914 ws_in6_addr* addr_key = wmem_new(wmem_epan_scope(), ws_in6_addr)((ws_in6_addr*)wmem_alloc((wmem_epan_scope()), sizeof(ws_in6_addr
)))
;
2915 memcpy(addr_key, &host_addr.ip6_addr, sizeof(ws_in6_addr));
2916
2917 resolved_entry = wmem_new(wmem_epan_scope(), resolved_name_t)((resolved_name_t*)wmem_alloc((wmem_epan_scope()), sizeof(resolved_name_t
)))
;
2918 (void) g_strlcpy(resolved_entry->name, name, MAXDNSNAMELEN256);
2919
2920 wmem_map_insert(manually_resolved_ipv6_list, addr_key, resolved_entry);
2921 }
2922 } else {
2923 resolved_entry = (resolved_name_t*)wmem_map_lookup(manually_resolved_ipv4_list, GUINT_TO_POINTER(host_addr.ip4_addr)((gpointer) (gulong) (host_addr.ip4_addr)));
2924 if (resolved_entry)
2925 {
2926 // If we found a previous matching key (IP address), then just update the value (custom hostname);
2927 (void) g_strlcpy(resolved_entry->name, name, MAXDNSNAMELEN256);
2928 }
2929 else
2930 {
2931 // Add a new mapping entry, if this IP address isn't already in the list.
2932 resolved_entry = wmem_new(wmem_epan_scope(), resolved_name_t)((resolved_name_t*)wmem_alloc((wmem_epan_scope()), sizeof(resolved_name_t
)))
;
2933 (void) g_strlcpy(resolved_entry->name, name, MAXDNSNAMELEN256);
2934
2935 wmem_map_insert(manually_resolved_ipv4_list, GUINT_TO_POINTER(host_addr.ip4_addr)((gpointer) (gulong) (host_addr.ip4_addr)), resolved_entry);
2936 }
2937 }
2938
2939 return true1;
2940} /* add_ip_name_from_string */
2941
2942extern resolved_name_t* get_edited_resolved_name(const char* addr)
2943{
2944 uint32_t ip4_addr;
2945 ws_in6_addr ip6_addr;
2946 resolved_name_t* resolved_entry = NULL((void*)0);
2947
2948 if (ws_inet_pton6(addr, &ip6_addr)) {
2949 resolved_entry = (resolved_name_t*)wmem_map_lookup(manually_resolved_ipv6_list, &ip6_addr);
2950 }
2951 else if (ws_inet_pton4(addr, &ip4_addr)) {
2952 resolved_entry = (resolved_name_t*)wmem_map_lookup(manually_resolved_ipv4_list, GUINT_TO_POINTER(ip4_addr)((gpointer) (gulong) (ip4_addr)));
2953 }
2954
2955 return resolved_entry;
2956}
2957
2958/*
2959 * Add the resolved addresses that are in use to the list used to create the pcapng NRB
2960 */
2961static void
2962ipv4_hash_table_resolved_to_list(void *key _U___attribute__((unused)), void *value, void *user_data)
2963{
2964 addrinfo_lists_t *lists = (addrinfo_lists_t *)user_data;
2965 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2966
2967 if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK((1U<<1) | (1U<<2))) == USED_AND_RESOLVED_MASK((1U<<1) | (1U<<2))) {
2968 lists->ipv4_addr_list = g_list_prepend(lists->ipv4_addr_list, ipv4_hash_table_entry);
2969 }
2970}
2971
2972/*
2973 * Add the resolved addresses that are in use to the list used to create the pcapng NRB
2974 */
2975static void
2976ipv6_hash_table_resolved_to_list(void *key _U___attribute__((unused)), void *value, void *user_data)
2977{
2978 addrinfo_lists_t *lists = (addrinfo_lists_t *)user_data;
2979 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2980
2981 if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK((1U<<1) | (1U<<2))) == USED_AND_RESOLVED_MASK((1U<<1) | (1U<<2))) {
2982 lists->ipv6_addr_list = g_list_prepend(lists->ipv6_addr_list, ipv6_hash_table_entry);
2983 }
2984}
2985
2986addrinfo_lists_t *
2987get_addrinfo_list(void)
2988{
2989 if (ipv4_hash_table) {
2990 wmem_map_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2991 }
2992
2993 if (ipv6_hash_table) {
2994 wmem_map_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2995 }
2996
2997 return &addrinfo_lists;
2998}
2999
3000/* Read in a list of subnet definition - name pairs.
3001 * <line> = <comment> | <entry> | <whitespace>
3002 * <comment> = <whitespace>#<any>
3003 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
3004 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
3005 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
3006 * <subnet_mask_length> is a decimal 1-31
3007 * <subnet_name> is a string containing no whitespace.
3008 * <whitespace> = (space | tab)+
3009 * Any malformed entries are ignored.
3010 * Any trailing data after the subnet_name is ignored.
3011 *
3012 * XXX Support IPv6
3013 */
3014static bool_Bool
3015read_subnets_file (const char *subnetspath)
3016{
3017 FILE *hf;
3018 char line[MAX_LINELEN1024];
3019 char *cp, *cp2;
3020 uint32_t host_addr; /* IPv4 ONLY */
3021 uint8_t mask_length;
3022
3023 if ((hf = ws_fopenfopen(subnetspath, "r")) == NULL((void*)0))
3024 return false0;
3025
3026 while (fgetline(line, sizeof(line), hf) >= 0) {
3027 if ((cp = strchr(line, '#')))
3028 *cp = '\0';
3029
3030 if ((cp = strtok(line, " \t")) == NULL((void*)0))
3031 continue; /* no tokens in the line */
3032
3033
3034 /* Expected format is <IP4 address>/<subnet length> */
3035 cp2 = strchr(cp, '/');
3036 if (NULL((void*)0) == cp2) {
3037 /* No length */
3038 continue;
3039 }
3040 *cp2 = '\0'; /* Cut token */
3041 ++cp2 ;
3042
3043 /* Check if this is a valid IPv4 address */
3044 if (!str_to_ip(cp, &host_addr)) {
3045 continue; /* no */
3046 }
3047
3048 if (!ws_strtou8(cp2, NULL((void*)0), &mask_length) || mask_length == 0 || mask_length > 32) {
3049 continue; /* invalid mask length */
3050 }
3051
3052 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
3053 continue; /* no subnet name */
3054
3055 subnet_entry_set(host_addr, mask_length, cp);
3056 }
3057
3058 fclose(hf);
3059 return true1;
3060} /* read_subnets_file */
3061
3062static subnet_entry_t
3063subnet_lookup(const uint32_t addr)
3064{
3065 subnet_entry_t subnet_entry;
3066 uint32_t i;
3067
3068 /* Search mask lengths linearly, longest first */
3069
3070 i = SUBNETLENGTHSIZE32;
3071 while(have_subnet_entry && i > 0) {
3072 uint32_t masked_addr;
3073 subnet_length_entry_t* length_entry;
3074
3075 /* Note that we run from 31 (length 32) to 0 (length 1) */
3076 --i;
3077 ws_assert(i < SUBNETLENGTHSIZE)do { if ((1) && !(i < 32)) ws_log_fatal_full("", LOG_LEVEL_ERROR
, "epan/addr_resolv.c", 3077, __func__, "assertion failed: %s"
, "i < 32"); } while (0)
;
3078
3079
3080 length_entry = &subnet_length_entries[i];
3081
3082 if (NULL((void*)0) != length_entry->subnet_addresses) {
3083 sub_net_hashipv4_t * tp;
3084 uint32_t hash_idx;
3085
3086 masked_addr = addr & length_entry->mask;
3087 hash_idx = HASH_IPV4_ADDRESS(masked_addr)((((((guint32) ( (((guint32) (masked_addr) & (guint32) 0x000000ffU
) << 24) | (((guint32) (masked_addr) & (guint32) 0x0000ff00U
) << 8) | (((guint32) (masked_addr) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (masked_addr) & (guint32) 0xff000000U
) >> 24)))))) & (2048 - 1))
;
3088
3089 tp = length_entry->subnet_addresses[hash_idx];
3090 while(tp != NULL((void*)0) && tp->addr != masked_addr) {
3091 tp = tp->next;
3092 }
3093
3094 if (NULL((void*)0) != tp) {
3095 subnet_entry.mask = length_entry->mask;
3096 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
3097 subnet_entry.name = tp->name;
3098 return subnet_entry;
3099 }
3100 }
3101 }
3102
3103 subnet_entry.mask = 0;
3104 subnet_entry.mask_length = 0;
3105 subnet_entry.name = NULL((void*)0);
3106
3107 return subnet_entry;
3108}
3109
3110/* Add a subnet-definition - name pair to the set.
3111 * The definition is taken by masking the address passed in with the mask of the
3112 * given length.
3113 */
3114static void
3115subnet_entry_set(uint32_t subnet_addr, const uint8_t mask_length, const char* name)
3116{
3117 subnet_length_entry_t* entry;
3118 sub_net_hashipv4_t * tp;
3119 size_t hash_idx;
3120
3121 ws_assert(mask_length > 0 && mask_length <= 32)do { if ((1) && !(mask_length > 0 && mask_length
<= 32)) ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/addr_resolv.c"
, 3121, __func__, "assertion failed: %s", "mask_length > 0 && mask_length <= 32"
); } while (0)
;
3122
3123 entry = &subnet_length_entries[mask_length - 1];
3124
3125 subnet_addr &= entry->mask;
3126
3127 hash_idx = HASH_IPV4_ADDRESS(subnet_addr)((((((guint32) ( (((guint32) (subnet_addr) & (guint32) 0x000000ffU
) << 24) | (((guint32) (subnet_addr) & (guint32) 0x0000ff00U
) << 8) | (((guint32) (subnet_addr) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (subnet_addr) & (guint32) 0xff000000U
) >> 24)))))) & (2048 - 1))
;
3128
3129 if (NULL((void*)0) == entry->subnet_addresses) {
3130 entry->subnet_addresses = (sub_net_hashipv4_t**)wmem_alloc0(addr_resolv_scope, sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE2048);
3131 }
3132
3133 if (NULL((void*)0) != (tp = entry->subnet_addresses[hash_idx])) {
3134 sub_net_hashipv4_t * new_tp;
3135
3136 while (tp->next) {
3137 if (tp->addr == subnet_addr) {
3138 return; /* XXX provide warning that an address was repeated? */
3139 } else {
3140 tp = tp->next;
3141 }
3142 }
3143
3144 new_tp = wmem_new(addr_resolv_scope, sub_net_hashipv4_t)((sub_net_hashipv4_t*)wmem_alloc((addr_resolv_scope), sizeof(
sub_net_hashipv4_t)))
;
3145 tp->next = new_tp;
3146 tp = new_tp;
3147 } else {
3148 tp = entry->subnet_addresses[hash_idx] = wmem_new(addr_resolv_scope, sub_net_hashipv4_t)((sub_net_hashipv4_t*)wmem_alloc((addr_resolv_scope), sizeof(
sub_net_hashipv4_t)))
;
3149 }
3150
3151 tp->next = NULL((void*)0);
3152 tp->addr = subnet_addr;
3153 (void) g_strlcpy(tp->name, name, MAXNAMELEN64); /* This is longer than subnet names can actually be */
3154 have_subnet_entry = true1;
3155}
3156
3157static void
3158subnet_name_lookup_init(void)
3159{
3160 char* subnetspath;
3161 uint32_t i;
3162
3163 for(i = 0; i < SUBNETLENGTHSIZE32; ++i) {
3164 uint32_t length = i + 1;
3165
3166 subnet_length_entries[i].subnet_addresses = NULL((void*)0);
3167 subnet_length_entries[i].mask_length = length;
3168 subnet_length_entries[i].mask = g_htonl(ws_ipv4_get_subnet_mask(length))(((((guint32) ( (((guint32) (ws_ipv4_get_subnet_mask(length))
& (guint32) 0x000000ffU) << 24) | (((guint32) (ws_ipv4_get_subnet_mask
(length)) & (guint32) 0x0000ff00U) << 8) | (((guint32
) (ws_ipv4_get_subnet_mask(length)) & (guint32) 0x00ff0000U
) >> 8) | (((guint32) (ws_ipv4_get_subnet_mask(length))
& (guint32) 0xff000000U) >> 24))))))
;
3169 }
3170
3171 /* Check profile directory before personal configuration */
3172 subnetspath = get_persconffile_path(ENAME_SUBNETS"subnets", true1);
3173 if (!read_subnets_file(subnetspath)) {
3174 if (errno(*__errno_location ()) != ENOENT2) {
3175 report_open_failure(subnetspath, errno(*__errno_location ()), false0);
3176 }
3177
3178 g_free(subnetspath);
3179 subnetspath = get_persconffile_path(ENAME_SUBNETS"subnets", false0);
3180 if (!read_subnets_file(subnetspath) && errno(*__errno_location ()) != ENOENT2) {
3181 report_open_failure(subnetspath, errno(*__errno_location ()), false0);
3182 }
3183 }
3184 g_free(subnetspath);
3185
3186 /*
3187 * Load the global subnets file, if we have one.
3188 */
3189 subnetspath = get_datafile_path(ENAME_SUBNETS"subnets");
3190 if (!read_subnets_file(subnetspath) && errno(*__errno_location ()) != ENOENT2) {
3191 report_open_failure(subnetspath, errno(*__errno_location ()), false0);
3192 }
3193 g_free(subnetspath);
3194}
3195
3196/* SS7 PC Name Resolution Portion */
3197static hashss7pc_t *
3198new_ss7pc(const uint8_t ni, const uint32_t pc)
3199{
3200 hashss7pc_t *tp = wmem_new(addr_resolv_scope, hashss7pc_t)((hashss7pc_t*)wmem_alloc((addr_resolv_scope), sizeof(hashss7pc_t
)))
;
3201 tp->id = (ni<<24) + (pc&0xffffff);
3202 tp->pc_addr[0] = '\0';
3203 tp->name[0] = '\0';
3204
3205 return tp;
3206}
3207
3208static hashss7pc_t *
3209host_lookup_ss7pc(const uint8_t ni, const uint32_t pc)
3210{
3211 hashss7pc_t * volatile tp;
3212 uint32_t id;
3213
3214 id = (ni<<24) + (pc&0xffffff);
3215
3216 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)));
3217 if (tp == NULL((void*)0)) {
3218 tp = new_ss7pc(ni, pc);
3219 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)), tp);
3220 }
3221
3222 return tp;
3223}
3224
3225void fill_unresolved_ss7pc(const char * pc_addr, const uint8_t ni, const uint32_t pc)
3226{
3227 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
3228
3229 (void) g_strlcpy(tp->pc_addr, pc_addr, MAXNAMELEN64);
3230}
3231
3232const char *
3233get_hostname_ss7pc(const uint8_t ni, const uint32_t pc)
3234{
3235 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
3236
3237 /* never resolved yet*/
3238 if (tp->pc_addr[0] == '\0')
3239 return tp->pc_addr;
3240
3241 /* Don't have name in file */
3242 if (tp->name[0] == '\0')
3243 return tp->pc_addr;
3244
3245 if (!gbl_resolv_flags.ss7pc_name)
3246 return tp->pc_addr;
3247
3248 return tp->name;
3249}
3250
3251static void
3252add_ss7pc_name(const uint8_t ni, uint32_t pc, const char *name)
3253{
3254 hashss7pc_t *tp;
3255 uint32_t id;
3256
3257 if (!name || name[0] == '\0')
3258 return;
3259
3260 id = (ni<<24) + (pc&0xffffff);
3261 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)));
3262 if (!tp) {
3263 tp = new_ss7pc(ni, pc);
3264 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id)((gpointer) (gulong) (id)), tp);
3265 }
3266
3267 if (g_ascii_strcasecmp(tp->name, name)) {
3268 (void) g_strlcpy(tp->name, name, MAXNAMELEN64);
3269 }
3270}
3271
3272static bool_Bool
3273read_ss7pcs_file(const char *ss7pcspath)
3274{
3275 FILE *hf;
3276 char line[MAX_LINELEN1024];
3277 char *cp;
3278 uint8_t ni;
3279 uint32_t pc;
3280 bool_Bool entry_found = false0;
3281
3282 /*
3283 * File format is Network Indicator (decimal)<dash>Point Code (Decimal)<tab/space>Hostname
3284 */
3285 if ((hf = ws_fopenfopen(ss7pcspath, "r")) == NULL((void*)0))
34
Taking false branch
3286 return false0;
3287
3288 while (fgetline(line, sizeof(line), hf) >= 0) {
35
Loop condition is false. Execution continues on line 3313
3289 if ((cp = strchr(line, '#')))
3290 *cp = '\0';
3291
3292 if ((cp = strtok(line, "-")) == NULL((void*)0))
3293 continue; /*no ni-pc separator*/
3294 if (!ws_strtou8(cp, NULL((void*)0), &ni))
3295 continue;
3296 if (ni > 3)
3297 continue;
3298
3299 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
3300 continue; /* no tokens for pc and name */
3301 if (!ws_strtou32(cp, NULL((void*)0), &pc))
3302 continue;
3303 if (pc >> 24 > 0)
3304 continue;
3305
3306 if ((cp = strtok(NULL((void*)0), " \t")) == NULL((void*)0))
3307 continue; /* no host name */
3308
3309 entry_found = true1;
3310 add_ss7pc_name(ni, pc, cp);
3311 }
3312
3313 fclose(hf);
36
Assuming that 'fclose' is successful; 'errno' becomes undefined after the call
3314 return entry_found
36.1
'entry_found' is false
? true1 : false0;
37
'?' condition is false
3315}
3316
3317static void
3318ss7pc_name_lookup_init(void)
3319{
3320 char *ss7pcspath;
3321
3322 ws_assert(ss7pc_hash_table == NULL)do { if ((1) && !(ss7pc_hash_table == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 3322, __func__, "assertion failed: %s"
, "ss7pc_hash_table == ((void*)0)"); } while (0)
;
30
Assuming 'ss7pc_hash_table' is equal to null
31
Taking false branch
32
Loop condition is false. Exiting loop
3323
3324 ss7pc_hash_table = wmem_map_new(addr_resolv_scope, g_direct_hash, g_direct_equal);
3325
3326 /*
3327 * Load the user's ss7pcs file
3328 */
3329 ss7pcspath = get_persconffile_path(ENAME_SS7PCS"ss7pcs", true1);
3330 if (!read_ss7pcs_file(ss7pcspath) && errno(*__errno_location ()) != ENOENT2) {
33
Calling 'read_ss7pcs_file'
38
Returning from 'read_ss7pcs_file'
39
An undefined value may be read from 'errno'
3331 report_open_failure(ss7pcspath, errno(*__errno_location ()), false0);
3332 }
3333 g_free(ss7pcspath);
3334}
3335
3336/* SS7PC Name Resolution End*/
3337
3338
3339/*
3340 * External Functions
3341 */
3342
3343void
3344addr_resolve_pref_init(module_t *nameres)
3345{
3346 prefs_register_bool_preference(nameres, "mac_name",
3347 "Resolve MAC addresses",
3348 "Resolve Ethernet MAC addresses to host names from the preferences"
3349 " or system's Ethers file, or to a manufacturer based name.",
3350 &gbl_resolv_flags.mac_name);
3351
3352 prefs_register_bool_preference(nameres, "transport_name",
3353 "Resolve transport names",
3354 "Resolve TCP/UDP ports into service names",
3355 &gbl_resolv_flags.transport_name);
3356
3357 prefs_register_bool_preference(nameres, "network_name",
3358 "Resolve network (IP) addresses",
3359 "Resolve IPv4, IPv6, and IPX addresses into host names."
3360 " The next set of check boxes determines how name resolution should be performed."
3361 " If no other options are checked name resolution is made from Wireshark's host file"
3362 " and capture file name resolution blocks.",
3363 &gbl_resolv_flags.network_name);
3364
3365 prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
3366 "Use captured DNS packet data for name resolution",
3367 "Use address/name pairs found in captured DNS packets for name resolution.",
3368 &gbl_resolv_flags.dns_pkt_addr_resolution);
3369
3370 prefs_register_bool_preference(nameres, "handshake_sni_addr_resolution",
3371 "Use SNI information from captured handshake packets",
3372 "Use the Server Name Indication found in TLS handshakes for name resolution.",
3373 &gbl_resolv_flags.handshake_sni_addr_resolution);
3374
3375 prefs_register_bool_preference(nameres, "use_external_name_resolver",
3376 "Use your system's DNS settings for name resolution",
3377 "Use your system's configured name resolver"
3378 " (usually DNS) to resolve network names."
3379 " Only applies when network name resolution"
3380 " is enabled.",
3381 &gbl_resolv_flags.use_external_net_name_resolver);
3382
3383 prefs_register_bool_preference(nameres, "use_custom_dns_servers",
3384 "Use a custom list of DNS servers for name resolution",
3385 "Use a DNS Servers list to resolve network names if true. If false, default information is used",
3386 &use_custom_dns_server_list);
3387
3388 static uat_field_t dns_server_uats_flds[] = {
3389 UAT_FLD_CSTRING_OTHER(dnsserverlist_uats, ipaddr, "IP address", dnsserver_uat_fld_ip_chk_cb, "IPv4 or IPv6 address"){"ipaddr", "IP address", PT_TXTMOD_STRING,{ dnsserver_uat_fld_ip_chk_cb
,dnsserverlist_uats_ipaddr_set_cb,dnsserverlist_uats_ipaddr_tostr_cb
},{0,0,0},0,"IPv4 or IPv6 address",((void*)0)}
,
3390 UAT_FLD_CSTRING_OTHER(dnsserverlist_uats, tcp_port, "TCP Port", dnsserver_uat_fld_port_chk_cb, "Port Number (TCP)"){"tcp_port", "TCP Port", PT_TXTMOD_STRING,{ dnsserver_uat_fld_port_chk_cb
,dnsserverlist_uats_tcp_port_set_cb,dnsserverlist_uats_tcp_port_tostr_cb
},{0,0,0},0,"Port Number (TCP)",((void*)0)}
,
3391 UAT_FLD_CSTRING_OTHER(dnsserverlist_uats, udp_port, "UDP Port", dnsserver_uat_fld_port_chk_cb, "Port Number (UDP)"){"udp_port", "UDP Port", PT_TXTMOD_STRING,{ dnsserver_uat_fld_port_chk_cb
,dnsserverlist_uats_udp_port_set_cb,dnsserverlist_uats_udp_port_tostr_cb
},{0,0,0},0,"Port Number (UDP)",((void*)0)}
,
3392 UAT_END_FIELDS{((void*)0),((void*)0),PT_TXTMOD_NONE,{0,0,0},{0,0,0},0,0,((void
*)0)}
3393 };
3394
3395 dnsserver_uat = uat_new("DNS Servers",
3396 sizeof(struct dns_server_data),
3397 "addr_resolve_dns_servers", /* filename */
3398 true1, /* from_profile */
3399 &dnsserverlist_uats, /* data_ptr */
3400 &ndnsservers, /* numitems_ptr */
3401 UAT_AFFECTS_DISSECTION0x00000001,
3402 NULL((void*)0),
3403 dns_server_copy_cb,
3404 NULL((void*)0),
3405 dns_server_free_cb,
3406 c_ares_set_dns_servers,
3407 NULL((void*)0),
3408 dns_server_uats_flds);
3409 static const char *dnsserver_uat_defaults[] = { NULL((void*)0), "53", "53" };
3410 uat_set_default_values(dnsserver_uat, dnsserver_uat_defaults);
3411 prefs_register_uat_preference(nameres, "dns_servers",
3412 "DNS Servers",
3413 "A table of IPv4 and IPv6 addresses of DNS servers to be used to resolve IP names and addresses",
3414 dnsserver_uat);
3415
3416 prefs_register_obsolete_preference(nameres, "concurrent_dns");
3417
3418 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
3419 "Maximum concurrent requests",
3420 "The maximum number of DNS requests that may"
3421 " be active at any time. A large value (many"
3422 " thousands) might overload the network or make"
3423 " your DNS server behave badly.",
3424 10,
3425 &name_resolve_concurrency);
3426
3427 prefs_register_obsolete_preference(nameres, "hosts_file_handling");
3428
3429 prefs_register_bool_preference(nameres, "vlan_name",
3430 "Resolve VLAN IDs",
3431 "Resolve VLAN IDs to network names from the preferences \"vlans\" file."
3432 " Format of the file is: \"ID<Tab>Name\"."
3433 " One line per VLAN, e.g.: 1 Management",
3434 &gbl_resolv_flags.vlan_name);
3435
3436 prefs_register_bool_preference(nameres, "ss7_pc_name",
3437 "Resolve SS7 PCs",
3438 "Resolve SS7 Point Codes to node names from the profiles \"ss7pcs\" file."
3439 " Format of the file is: \"Network_Indicator<Dash>PC_Decimal<Tab>Name\"."
3440 " One line per Point Code, e.g.: 2-1234 MyPointCode1",
3441 &gbl_resolv_flags.ss7pc_name);
3442
3443}
3444
3445void addr_resolve_pref_apply(void)
3446{
3447 c_ares_set_dns_servers();
3448 maxmind_db_pref_apply();
3449}
3450
3451void
3452disable_name_resolution(void) {
3453 gbl_resolv_flags.mac_name = false0;
3454 gbl_resolv_flags.network_name = false0;
3455 gbl_resolv_flags.transport_name = false0;
3456 gbl_resolv_flags.dns_pkt_addr_resolution = false0;
3457 gbl_resolv_flags.handshake_sni_addr_resolution = false0;
3458 gbl_resolv_flags.use_external_net_name_resolver = false0;
3459 gbl_resolv_flags.vlan_name = false0;
3460 gbl_resolv_flags.ss7pc_name = false0;
3461 gbl_resolv_flags.maxmind_geoip = false0;
3462}
3463
3464bool_Bool
3465host_name_lookup_process(void) {
3466 struct timeval tv = { 0, 0 };
3467 int nfds;
3468 fd_set rfds, wfds;
3469 bool_Bool nro = new_resolved_objects;
3470
3471 new_resolved_objects = false0;
3472 nro |= maxmind_db_lookup_process();
3473
3474 if (!async_dns_initialized)
3475 /* c-ares not initialized. Bail out and cancel timers. */
3476 return nro;
3477
3478 process_async_dns_queue();
3479
3480 FD_ZERO(&rfds)do { unsigned int __i; fd_set *__arr = (&rfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
3481 FD_ZERO(&wfds)do { unsigned int __i; fd_set *__arr = (&wfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
3482 nfds = ares_fds(ghba_chan, &rfds, &wfds);
3483 if (nfds > 0) {
3484 if (select(nfds, &rfds, &wfds, NULL((void*)0), &tv) == -1) { /* call to select() failed */
3485 /* If it's interrupted by a signal, no need to put out a message */
3486 if (errno(*__errno_location ()) != EINTR4)
3487 fprintf(stderrstderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno(*__errno_location ())));
3488 return nro;
3489 }
3490 ares_process(ghba_chan, &rfds, &wfds);
3491 }
3492
3493 /* Any new entries? */
3494 return nro;
3495}
3496
3497static void
3498_host_name_lookup_cleanup(void) {
3499 async_dns_queue_head = NULL((void*)0);
3500
3501 if (async_dns_initialized) {
3502 ares_destroy(ghba_chan);
3503 ares_destroy(ghbn_chan);
3504 }
3505#ifdef CARES_HAVE_ARES_LIBRARY_INIT1
3506 ares_library_cleanup();
3507#endif
3508 async_dns_initialized = false0;
3509}
3510
3511const char *
3512get_hostname(const unsigned addr)
3513{
3514 /* XXX why do we call this if we're not resolving? To create hash entries?
3515 * Why? So that we can return a const char*?
3516 *
3517 * Note the returned string is in addr_resolv_scope, which has a similar
3518 * life to the global file scope (slightly larger, in that the resolved
3519 * addresses need to be available during dissector registration, e.g.
3520 * for RADIUS and enterprises), so if not copied it is possible to use
3521 * it after freeing.
3522 *
3523 * Should this be deprecated in favor of get_hostname_wmem so that
3524 * host name lookups don't increase persistent memory usage even when
3525 * hostname lookups are disabled? (An alternative would be to return
3526 * NULL when lookups are disabled, but callers don't expect that.)
3527 */
3528 hashipv4_t *tp = host_lookup(addr);
3529
3530 if (!gbl_resolv_flags.network_name)
3531 return tp->ip;
3532
3533 tp->flags |= RESOLVED_ADDRESS_USED(1U<<2);
3534
3535 return tp->name;
3536}
3537
3538char *
3539get_hostname_wmem(wmem_allocator_t *allocator, const unsigned addr)
3540{
3541 if (!gbl_resolv_flags.network_name)
3542 return ip_addr_to_str(allocator, &addr);
3543
3544 hashipv4_t *tp = host_lookup(addr);
3545
3546 tp->flags |= RESOLVED_ADDRESS_USED(1U<<2);
3547
3548 return wmem_strdup(allocator, tp->name);
3549}
3550/* -------------------------- */
3551
3552const char *
3553get_hostname6(const ws_in6_addr *addr)
3554{
3555 /* XXX why do we call this if we're not resolving? To create hash entries?
3556 * Why? The same comments as get_hostname above apply.
3557 */
3558 hashipv6_t *tp = host_lookup6(addr);
3559
3560 if (!gbl_resolv_flags.network_name)
3561 return tp->ip6;
3562
3563 tp->flags |= RESOLVED_ADDRESS_USED(1U<<2);
3564
3565 return tp->name;
3566}
3567
3568char *
3569get_hostname6_wmem(wmem_allocator_t *allocator, const ws_in6_addr *addr)
3570{
3571 if (!gbl_resolv_flags.network_name)
3572 return ip6_to_str(allocator, addr);
3573
3574 hashipv6_t *tp = host_lookup6(addr);
3575
3576 tp->flags |= RESOLVED_ADDRESS_USED(1U<<2);
3577
3578 return wmem_strdup(allocator, tp->name);
3579}
3580/* -------------------------- */
3581void
3582add_ipv4_name(const unsigned addr, const char *name, bool_Bool static_entry)
3583{
3584 hashipv4_t *tp;
3585
3586 /*
3587 * Don't add zero-length names; apparently, some resolvers will return
3588 * them if they get them from DNS.
3589 */
3590 if (!name || name[0] == '\0')
3591 return;
3592
3593 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)));
3594 if (!tp) {
3595 tp = new_ipv4(addr);
3596 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr)((gpointer) (gulong) (addr)), tp);
3597 }
3598
3599 if (g_ascii_strcasecmp(tp->name, name) && (static_entry || !(tp->flags & STATIC_HOSTNAME(1U<<3)))) {
3600 (void) g_strlcpy(tp->name, name, MAXDNSNAMELEN256);
3601 new_resolved_objects = true1;
3602 if (static_entry)
3603 tp->flags |= STATIC_HOSTNAME(1U<<3);
3604 }
3605 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0)|NAME_RESOLVED(1U<<1);
3606} /* add_ipv4_name */
3607
3608/* -------------------------- */
3609void
3610add_ipv6_name(const ws_in6_addr *addrp, const char *name, const bool_Bool static_entry)
3611{
3612 hashipv6_t *tp;
3613
3614 /*
3615 * Don't add zero-length names; apparently, some resolvers will return
3616 * them if they get them from DNS.
3617 */
3618 if (!name || name[0] == '\0')
3619 return;
3620
3621 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addrp);
3622 if (!tp) {
3623 ws_in6_addr *addr_key;
3624
3625 addr_key = wmem_new(addr_resolv_scope, ws_in6_addr)((ws_in6_addr*)wmem_alloc((addr_resolv_scope), sizeof(ws_in6_addr
)))
;
3626 tp = new_ipv6(addrp);
3627 memcpy(addr_key, addrp, 16);
3628 wmem_map_insert(ipv6_hash_table, addr_key, tp);
3629 }
3630
3631 if (g_ascii_strcasecmp(tp->name, name) && (static_entry || !(tp->flags & STATIC_HOSTNAME(1U<<3)))) {
3632 (void) g_strlcpy(tp->name, name, MAXDNSNAMELEN256);
3633 new_resolved_objects = true1;
3634 if (static_entry)
3635 tp->flags |= STATIC_HOSTNAME(1U<<3);
3636 }
3637 tp->flags |= TRIED_RESOLVE_ADDRESS(1U<<0)|NAME_RESOLVED(1U<<1);
3638} /* add_ipv6_name */
3639
3640static void
3641add_manually_resolved_ipv4(void *key, void *value, void *user_data _U___attribute__((unused)))
3642{
3643 resolved_name_t *resolved_ipv4_entry = (resolved_name_t*)value;
3644 add_ipv4_name(GPOINTER_TO_UINT(key)((guint) (gulong) (key)), resolved_ipv4_entry->name, true1);
3645}
3646
3647static void
3648add_manually_resolved_ipv6(void *key, void *value, void *user_data _U___attribute__((unused)))
3649{
3650 resolved_name_t *resolved_ipv6_entry = (resolved_name_t*)value;
3651 add_ipv6_name((ws_in6_addr*)key, resolved_ipv6_entry->name, true1);
3652}
3653
3654static void
3655add_manually_resolved(void)
3656{
3657 if (manually_resolved_ipv4_list) {
3658 wmem_map_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL((void*)0));
3659 }
3660
3661 if (manually_resolved_ipv6_list) {
3662 wmem_map_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL((void*)0));
3663 }
3664}
3665
3666static void
3667host_name_lookup_init(void)
3668{
3669 char *hostspath;
3670 unsigned i;
3671
3672 ws_assert(ipxnet_hash_table == NULL)do { if ((1) && !(ipxnet_hash_table == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 3672, __func__, "assertion failed: %s"
, "ipxnet_hash_table == ((void*)0)"); } while (0)
;
5
Assuming 'ipxnet_hash_table' is equal to null
6
Taking false branch
7
Loop condition is false. Exiting loop
3673 ipxnet_hash_table = wmem_map_new(addr_resolv_scope, g_direct_hash, g_direct_equal);
3674
3675 ws_assert(ipv4_hash_table == NULL)do { if ((1) && !(ipv4_hash_table == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 3675, __func__, "assertion failed: %s"
, "ipv4_hash_table == ((void*)0)"); } while (0)
;
8
Assuming 'ipv4_hash_table' is equal to null
9
Taking false branch
10
Loop condition is false. Exiting loop
3676 ipv4_hash_table = wmem_map_new(addr_resolv_scope, g_direct_hash, g_direct_equal);
3677
3678 ws_assert(ipv6_hash_table == NULL)do { if ((1) && !(ipv6_hash_table == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 3678, __func__, "assertion failed: %s"
, "ipv6_hash_table == ((void*)0)"); } while (0)
;
11
Assuming 'ipv6_hash_table' is equal to null
12
Taking false branch
13
Loop condition is false. Exiting loop
3679 ipv6_hash_table = wmem_map_new(addr_resolv_scope, ipv6_oat_hash, ipv6_equal);
3680
3681 ws_assert(async_dns_queue_head == NULL)do { if ((1) && !(async_dns_queue_head == ((void*)0))
) ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/addr_resolv.c"
, 3681, __func__, "assertion failed: %s", "async_dns_queue_head == ((void*)0)"
); } while (0)
;
14
Assuming 'async_dns_queue_head' is equal to null
15
Taking false branch
16
Loop condition is false. Exiting loop
3682 async_dns_queue_head = wmem_list_new(addr_resolv_scope);
3683
3684 /*
3685 * The manually resolved lists are the only address resolution maps
3686 * that are not reset by addr_resolv_cleanup(), because they are
3687 * the only ones that do not have entries from personal configuration
3688 * files that can change when changing configurations. All their
3689 * entries must also be in epan scope.
3690 */
3691 if (manually_resolved_ipv4_list == NULL((void*)0))
17
Assuming 'manually_resolved_ipv4_list' is not equal to NULL
18
Taking false branch
3692 manually_resolved_ipv4_list = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
3693
3694 if (manually_resolved_ipv6_list == NULL((void*)0))
19
Assuming 'manually_resolved_ipv6_list' is not equal to NULL
20
Taking false branch
3695 manually_resolved_ipv6_list = wmem_map_new(wmem_epan_scope(), ws_ipv6_hash, ipv6_equal);
3696
3697 /*
3698 * Load the global hosts file, if we have one.
3699 */
3700 hostspath = get_datafile_path(ENAME_HOSTS"hosts");
3701 if (!read_hosts_file(hostspath, true1) && errno(*__errno_location ()) != ENOENT2) {
21
Assuming the condition is false
22
Taking false branch
3702 report_open_failure(hostspath, errno(*__errno_location ()), false0);
3703 }
3704 g_free(hostspath);
3705 /*
3706 * Load the user's hosts file no matter what, if they have one.
3707 */
3708 hostspath = get_persconffile_path(ENAME_HOSTS"hosts", true1);
3709 if (!read_hosts_file(hostspath, true1) && errno(*__errno_location ()) != ENOENT2) {
23
Assuming the condition is false
24
Taking false branch
3710 report_open_failure(hostspath, errno(*__errno_location ()), false0);
3711 }
3712 g_free(hostspath);
3713#ifdef CARES_HAVE_ARES_LIBRARY_INIT1
3714 if (ares_library_init(ARES_LIB_INIT_ALL((1 << 0))) == ARES_SUCCESS) {
25
Assuming the condition is false
26
Taking false branch
3715#endif
3716 /* XXX - Check which options we should set */
3717 if (ares_init_options(&ghba_chan, NULL((void*)0), 0) == ARES_SUCCESS && ares_init_options(&ghbn_chan, NULL((void*)0), 0) == ARES_SUCCESS) {
3718 async_dns_initialized = true1;
3719 c_ares_set_dns_servers();
3720 }
3721#ifdef CARES_HAVE_ARES_LIBRARY_INIT1
3722 }
3723#endif
3724
3725 if (extra_hosts_files) {
27
Assuming 'extra_hosts_files' is null
28
Taking false branch
3726 for (i = 0; i < extra_hosts_files->len; i++) {
3727 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i)((extra_hosts_files)->pdata)[i], true1);
3728 }
3729 }
3730
3731 subnet_name_lookup_init();
3732
3733 add_manually_resolved();
3734
3735 ss7pc_name_lookup_init();
29
Calling 'ss7pc_name_lookup_init'
3736}
3737
3738static void
3739host_name_lookup_cleanup(void)
3740{
3741 uint32_t i, j;
3742 sub_net_hashipv4_t *entry, *next_entry;
3743
3744 _host_name_lookup_cleanup();
3745
3746 ipxnet_hash_table = NULL((void*)0);
3747 ipv4_hash_table = NULL((void*)0);
3748 ipv6_hash_table = NULL((void*)0);
3749 ss7pc_hash_table = NULL((void*)0);
3750
3751 for(i = 0; i < SUBNETLENGTHSIZE32; ++i) {
3752 if (subnet_length_entries[i].subnet_addresses != NULL((void*)0)) {
3753 for (j = 0; j < HASHHOSTSIZE2048; j++) {
3754 for (entry = subnet_length_entries[i].subnet_addresses[j];
3755 entry != NULL((void*)0); entry = next_entry) {
3756 next_entry = entry->next;
3757 wmem_free(addr_resolv_scope, entry);
3758 }
3759 }
3760 wmem_free(addr_resolv_scope, subnet_length_entries[i].subnet_addresses);
3761 subnet_length_entries[i].subnet_addresses = NULL((void*)0);
3762 }
3763 }
3764
3765 have_subnet_entry = false0;
3766 new_resolved_objects = false0;
3767}
3768
3769
3770void host_name_lookup_reset(void)
3771{
3772 addr_resolv_cleanup();
3773 addr_resolv_init();
1
Calling 'addr_resolv_init'
3774}
3775
3776char *
3777udp_port_to_display(wmem_allocator_t *allocator, unsigned port)
3778{
3779
3780 if (!gbl_resolv_flags.transport_name) {
3781 return wmem_utoa(allocator, port);
3782 }
3783
3784 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
3785
3786} /* udp_port_to_display */
3787
3788char *
3789dccp_port_to_display(wmem_allocator_t *allocator, unsigned port)
3790{
3791
3792 if (!gbl_resolv_flags.transport_name) {
3793 return wmem_utoa(allocator, port);
3794 }
3795
3796 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
3797
3798} /* dccp_port_to_display */
3799
3800char *
3801tcp_port_to_display(wmem_allocator_t *allocator, unsigned port)
3802{
3803
3804 if (!gbl_resolv_flags.transport_name) {
3805 return wmem_utoa(allocator, port);
3806 }
3807
3808 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
3809
3810} /* tcp_port_to_display */
3811
3812char *
3813sctp_port_to_display(wmem_allocator_t *allocator, unsigned port)
3814{
3815
3816 if (!gbl_resolv_flags.transport_name) {
3817 return wmem_utoa(allocator, port);
3818 }
3819
3820 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
3821
3822} /* sctp_port_to_display */
3823
3824char *
3825port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, unsigned port)
3826{
3827 const char *port_str;
3828
3829 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3830 /* No name resolution support, just return port string */
3831 return wmem_strdup_printf(scope, "%u", port);
3832 }
3833 port_str = serv_name_lookup(proto, port);
3834 ws_assert(port_str)do { if ((1) && !(port_str)) ws_log_fatal_full("", LOG_LEVEL_ERROR
, "epan/addr_resolv.c", 3834, __func__, "assertion failed: %s"
, "port_str"); } while (0)
;
3835 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
3836}
3837
3838int
3839port_with_resolution_to_str_buf(char *buf, unsigned long buf_size, port_type proto, unsigned port)
3840{
3841 const char *port_str;
3842
3843 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3844 /* No name resolution support, just return port string */
3845 return snprintf(buf, buf_size, "%u", port);
3846 }
3847 port_str = serv_name_lookup(proto, port);
3848 ws_assert(port_str)do { if ((1) && !(port_str)) ws_log_fatal_full("", LOG_LEVEL_ERROR
, "epan/addr_resolv.c", 3848, __func__, "assertion failed: %s"
, "port_str"); } while (0)
;
3849 return snprintf(buf, buf_size, "%s (%u)", port_str, port);
3850}
3851
3852const char *
3853get_ether_name(const uint8_t *addr)
3854{
3855 hashether_t *tp;
3856 bool_Bool resolve = gbl_resolv_flags.mac_name;
3857
3858 tp = eth_name_lookup(addr, resolve);
3859
3860 return resolve ? tp->resolved_name : tp->hexaddr;
3861
3862} /* get_ether_name */
3863
3864const char *
3865tvb_get_ether_name(tvbuff_t *tvb, int offset)
3866{
3867 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
3868}
3869
3870/* Look for a (non-dummy) ether name in the hash, and return it if found.
3871 * If it's not found, simply return NULL.
3872 */
3873const char *
3874get_ether_name_if_known(const uint8_t *addr)
3875{
3876 hashether_t *tp;
3877
3878 /* Initialize ether structs if we're the first
3879 * ether-related function called */
3880 if (!gbl_resolv_flags.mac_name)
3881 return NULL((void*)0);
3882
3883 /* eth_name_lookup will create a (resolved) hash entry
3884 * if it doesn't exist, so it never returns NULL */
3885 tp = eth_name_lookup(addr, true1);
3886
3887 if ((tp->flags & (NAME_RESOLVED(1U<<1) | NAME_RESOLVED_PREFIX(1U<<4))) == NAME_RESOLVED(1U<<1)) {
3888 /* Name is from an exact match, not a prefix/OUI */
3889 return tp->resolved_name;
3890 }
3891 else {
3892 /* Name was created */
3893 return NULL((void*)0);
3894 }
3895}
3896
3897void
3898add_ether_byip(const unsigned ip, const uint8_t *eth)
3899{
3900 hashipv4_t *tp;
3901
3902 /* first check that IP address can be resolved */
3903 if (!gbl_resolv_flags.network_name)
3904 return;
3905
3906 tp = host_lookup(ip);
3907
3908 /*
3909 * Was this IP address resolved to a host name?
3910 */
3911 if (tp->flags & NAME_RESOLVED(1U<<1)) {
3912 /*
3913 * Yes, so add an entry in the ethers hashtable resolving
3914 * the MAC address to that name.
3915 */
3916 add_eth_name(eth, tp->name, false0);
3917 }
3918
3919} /* add_ether_byip */
3920
3921char *
3922get_ipxnet_name(wmem_allocator_t *allocator, const uint32_t addr)
3923{
3924
3925 if (!gbl_resolv_flags.network_name) {
3926 return ipxnet_to_str_punct(allocator, addr, '\0');
3927 }
3928
3929 return ipxnet_name_lookup(allocator, addr);
3930
3931} /* get_ipxnet_name */
3932
3933char *
3934get_vlan_name(wmem_allocator_t *allocator, const uint16_t id)
3935{
3936
3937 if (!gbl_resolv_flags.vlan_name) {
3938 return NULL((void*)0);
3939 }
3940
3941 return wmem_strdup(allocator, vlan_name_lookup(id));
3942
3943} /* get_vlan_name */
3944
3945const char *
3946get_manuf_name(const uint8_t *addr, size_t size)
3947{
3948 hashmanuf_t *manuf_value;
3949
3950 ws_return_val_if(size < 3, NULL)do { if (1 && (size < 3)) { ws_log_full("InvalidArg"
, LOG_LEVEL_WARNING, "epan/addr_resolv.c", 3950, __func__, "invalid argument: %s"
, "size < 3"); return (((void*)0)); } } while (0)
;
3951
3952 manuf_value = manuf_name_lookup(addr, size);
3953 if (gbl_resolv_flags.mac_name && ((manuf_value->flags & NAME_RESOLVED(1U<<1)) == NAME_RESOLVED(1U<<1)))
3954 return manuf_value->resolved_name;
3955
3956 return manuf_value->hexaddr;
3957
3958} /* get_manuf_name */
3959
3960const char *
3961tvb_get_manuf_name(tvbuff_t *tvb, int offset)
3962{
3963 uint8_t buf[3] = { 0 };
3964 tvb_memcpy(tvb, buf, offset, 3);
3965 return get_manuf_name(buf, sizeof(buf));
3966}
3967
3968const char *
3969get_manuf_name_if_known(const uint8_t *addr, size_t size)
3970{
3971 hashmanuf_t *manuf_value;
3972
3973 ws_return_val_if(size < 3, NULL)do { if (1 && (size < 3)) { ws_log_full("InvalidArg"
, LOG_LEVEL_WARNING, "epan/addr_resolv.c", 3973, __func__, "invalid argument: %s"
, "size < 3"); return (((void*)0)); } } while (0)
;
3974
3975 manuf_value = manuf_name_lookup(addr, size);
3976 if (manuf_value != NULL((void*)0) && ((manuf_value->flags & NAME_RESOLVED(1U<<1)) == NAME_RESOLVED(1U<<1))) {
3977 return manuf_value->resolved_longname;
3978 }
3979
3980 if (size >= 6) {
3981 /* Try the global manuf tables. */
3982 const char *short_name, *long_name;
3983 short_name = ws_manuf_lookup_str(addr, &long_name);
3984 if (short_name != NULL((void*)0)) {
3985 /* Found it */
3986 return long_name;
3987 }
3988 }
3989
3990 return NULL((void*)0);
3991
3992} /* get_manuf_name_if_known */
3993
3994const char *
3995uint_get_manuf_name_if_known(const uint32_t manuf_key)
3996{
3997 uint8_t addr[6] = { 0 };
3998 addr[0] = (manuf_key >> 16) & 0xFF;
3999 addr[1] = (manuf_key >> 8) & 0xFF;
4000 addr[2] = manuf_key & 0xFF;
4001
4002 return get_manuf_name_if_known(addr, sizeof(addr));
4003}
4004
4005const char *
4006tvb_get_manuf_name_if_known(tvbuff_t *tvb, int offset)
4007{
4008 uint8_t buf[3] = { 0 };
4009 tvb_memcpy(tvb, buf, offset, 3);
4010 return get_manuf_name_if_known(buf, sizeof(buf));
4011}
4012
4013bool_Bool get_hash_manuf_used(hashmanuf_t* manuf)
4014{
4015 return ((manuf->flags & TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1))) == TRIED_OR_RESOLVED_MASK((1U<<0) | (1U<<1)));
4016}
4017
4018char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
4019{
4020 return manuf->resolved_longname;
4021}
4022
4023const char *
4024get_eui64_name(const uint8_t *addr)
4025{
4026 hasheui64_t *tp;
4027 bool_Bool resolve = gbl_resolv_flags.mac_name;
4028
4029 tp = eui64_name_lookup(addr, resolve);
4030
4031 return resolve ? tp->resolved_name : tp->hexaddr;
4032
4033} /* get_eui64_name */
4034
4035char *
4036eui64_to_display(wmem_allocator_t *allocator, const uint64_t addr_eui64)
4037{
4038 uint8_t addr[EUI64_ADDR_LEN8];
4039
4040 phtonu64(addr, addr_eui64);
4041
4042 const char *result = get_eui64_name(addr);
4043
4044 return wmem_strdup(allocator, result);
4045} /* eui64_to_display */
4046
4047#define GHI_TIMEOUT(250 * 1000) (250 * 1000)
4048static void
4049c_ares_ghi_cb(void *arg, int status, int timeouts _U___attribute__((unused)), struct hostent *hp) {
4050 /*
4051 * XXX - If we wanted to be really fancy we could cache results here and
4052 * look them up in get_host_ipaddr* below.
4053 *
4054 * XXX - This only gets the first host address if there's more than one.
4055 */
4056 async_hostent_t *ahp = (async_hostent_t *)arg;
4057 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
4058 memcpy(ahp->addrp, hp->h_addrh_addr_list[0], hp->h_length);
4059 ahp->copied = hp->h_length;
4060 }
4061}
4062
4063/* Translate a string, assumed either to be a dotted-quad IPv4 address or
4064 * a host name, to a numeric IPv4 address. Return true if we succeed and
4065 * set "*addrp" to that numeric IPv4 address; return false if we fail. */
4066bool_Bool
4067get_host_ipaddr(const char *host, uint32_t *addrp)
4068{
4069 struct timeval tv = { 0, GHI_TIMEOUT(250 * 1000) }, *tvp;
4070 int nfds;
4071 fd_set rfds, wfds;
4072 async_hostent_t ahe;
4073
4074 /*
4075 * XXX - are there places where this is used to translate something
4076 * that's *only* supposed to be an IPv4 address, and where it
4077 * *shouldn't* translate host names?
4078 */
4079 if (!ws_inet_pton4(host, addrp)) {
4080
4081 /* It's not a valid dotted-quad IP address; is it a valid
4082 * host name?
4083 */
4084
4085 /* If we're not allowed to do name resolution, don't do name
4086 * resolution...
4087 * XXX - What if we're allowed to do name resolution, and the name
4088 * is in a DNS packet we've dissected or in a Name Resolution Block,
4089 * or a user-entered manual name resolution?
4090 */
4091 if (!gbl_resolv_flags.network_name ||
4092 !gbl_resolv_flags.use_external_net_name_resolver) {
4093 return false0;
4094 }
4095
4096 if (!async_dns_initialized || name_resolve_concurrency < 1) {
4097 return false0;
4098 }
4099 ahe.addr_size = (int) sizeof (struct in_addr);
4100 ahe.copied = 0;
4101 ahe.addrp = addrp;
4102 ares_gethostbyname(ghbn_chan, host, AF_INET2, c_ares_ghi_cb, &ahe);
4103 FD_ZERO(&rfds)do { unsigned int __i; fd_set *__arr = (&rfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
4104 FD_ZERO(&wfds)do { unsigned int __i; fd_set *__arr = (&wfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
4105 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
4106 if (nfds > 0) {
4107 tvp = ares_timeout(ghbn_chan, &tv, &tv);
4108 if (select(nfds, &rfds, &wfds, NULL((void*)0), tvp) == -1) { /* call to select() failed */
4109 /* If it's interrupted by a signal, no need to put out a message */
4110 if (errno(*__errno_location ()) != EINTR4)
4111 fprintf(stderrstderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno(*__errno_location ())));
4112 return false0;
4113 }
4114 ares_process(ghbn_chan, &rfds, &wfds);
4115 }
4116 ares_cancel(ghbn_chan);
4117 if (ahe.addr_size == ahe.copied) {
4118 return true1;
4119 }
4120 return false0;
4121 }
4122
4123 return true1;
4124}
4125
4126/*
4127 * Translate IPv6 numeric address or FQDN hostname into binary IPv6 address.
4128 * Return true if we succeed and set "*addrp" to that numeric IPv6 address;
4129 * return false if we fail.
4130 */
4131bool_Bool
4132get_host_ipaddr6(const char *host, ws_in6_addr *addrp)
4133{
4134 struct timeval tv = { 0, GHI_TIMEOUT(250 * 1000) }, *tvp;
4135 int nfds;
4136 fd_set rfds, wfds;
4137 async_hostent_t ahe;
4138
4139 if (str_to_ip6(host, addrp))
4140 return true1;
4141
4142 /* It's not a valid dotted-quad IP address; is it a valid
4143 * host name?
4144 *
4145 * XXX - are there places where this is used to translate something
4146 * that's *only* supposed to be an IPv6 address, and where it
4147 * *shouldn't* translate host names?
4148 */
4149
4150 /* If we're not allowed to do name resolution, don't do name
4151 * resolution...
4152 * XXX - What if we're allowed to do name resolution, and the name
4153 * is in a DNS packet we've dissected or in a Name Resolution Block,
4154 * or a user-entered manual name resolution?
4155 */
4156 if (!gbl_resolv_flags.network_name ||
4157 !gbl_resolv_flags.use_external_net_name_resolver) {
4158 return false0;
4159 }
4160
4161 /* try FQDN */
4162 if (!async_dns_initialized || name_resolve_concurrency < 1) {
4163 return false0;
4164 }
4165 ahe.addr_size = (int) sizeof (ws_in6_addr);
4166 ahe.copied = 0;
4167 ahe.addrp = addrp;
4168 ares_gethostbyname(ghbn_chan, host, AF_INET610, c_ares_ghi_cb, &ahe);
4169 FD_ZERO(&rfds)do { unsigned int __i; fd_set *__arr = (&rfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
4170 FD_ZERO(&wfds)do { unsigned int __i; fd_set *__arr = (&wfds); for (__i =
0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ((__arr
)->__fds_bits)[__i] = 0; } while (0)
;
4171 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
4172 if (nfds > 0) {
4173 tvp = ares_timeout(ghbn_chan, &tv, &tv);
4174 if (select(nfds, &rfds, &wfds, NULL((void*)0), tvp) == -1) { /* call to select() failed */
4175 /* If it's interrupted by a signal, no need to put out a message */
4176 if (errno(*__errno_location ()) != EINTR4)
4177 fprintf(stderrstderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno(*__errno_location ())));
4178 return false0;
4179 }
4180 ares_process(ghbn_chan, &rfds, &wfds);
4181 }
4182 ares_cancel(ghbn_chan);
4183 if (ahe.addr_size == ahe.copied) {
4184 return true1;
4185 }
4186
4187 return false0;
4188}
4189
4190wmem_map_t *
4191get_manuf_hashtable(void)
4192{
4193 return manuf_hashtable;
4194}
4195
4196wmem_map_t *
4197get_wka_hashtable(void)
4198{
4199 return wka_hashtable;
4200}
4201
4202wmem_map_t *
4203get_eth_hashtable(void)
4204{
4205 return eth_hashtable;
4206}
4207
4208wmem_map_t *
4209get_serv_port_hashtable(void)
4210{
4211 return serv_port_hashtable;
4212}
4213
4214wmem_map_t *
4215get_ipxnet_hash_table(void)
4216{
4217 return ipxnet_hash_table;
4218}
4219
4220wmem_map_t *
4221get_vlan_hash_table(void)
4222{
4223 return vlan_hash_table;
4224}
4225
4226wmem_map_t *
4227get_ipv4_hash_table(void)
4228{
4229 return ipv4_hash_table;
4230}
4231
4232wmem_map_t *
4233get_ipv6_hash_table(void)
4234{
4235 return ipv6_hash_table;
4236}
4237/* Initialize all the address resolution subsystems in this file */
4238void
4239addr_resolv_init(void)
4240{
4241 ws_assert(addr_resolv_scope == NULL)do { if ((1) && !(addr_resolv_scope == ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/addr_resolv.c", 4241, __func__, "assertion failed: %s"
, "addr_resolv_scope == ((void*)0)"); } while (0)
;
2
Taking false branch
3
Loop condition is false. Exiting loop
4242 addr_resolv_scope = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
4243 initialize_services();
4244 initialize_ethers();
4245 initialize_ipxnets();
4246 initialize_vlans();
4247 initialize_enterprises();
4248 host_name_lookup_init();
4
Calling 'host_name_lookup_init'
4249}
4250
4251/* Clean up all the address resolution subsystems in this file */
4252void
4253addr_resolv_cleanup(void)
4254{
4255 vlan_name_lookup_cleanup();
4256 service_name_lookup_cleanup();
4257 ethers_cleanup();
4258 ipx_name_lookup_cleanup();
4259 enterprises_cleanup();
4260 host_name_lookup_cleanup();
4261
4262 wmem_destroy_allocator(addr_resolv_scope);
4263 addr_resolv_scope = NULL((void*)0);
4264}
4265
4266bool_Bool
4267str_to_ip(const char *str, void *dst)
4268{
4269 return ws_inet_pton4(str, (uint32_t *)dst);
4270}
4271
4272bool_Bool
4273str_to_ip6(const char *str, void *dst)
4274{
4275 return ws_inet_pton6(str, (ws_in6_addr *)dst);
4276}
4277
4278/*
4279 * convert a 0-terminated string that contains an ethernet address into
4280 * the corresponding sequence of 6 bytes
4281 * eth_bytes is a buffer >= 6 bytes that was allocated by the caller
4282 */
4283bool_Bool
4284str_to_eth(const char *str, char *eth_bytes)
4285{
4286 ether_t eth;
4287 unsigned mask;
4288
4289 if (!parse_ether_address(str, &eth, &mask, false0))
4290 return false0;
4291
4292 if (mask == 48) {
4293 memcpy(eth_bytes, eth.addr, 6);
4294 }
4295 return true1;
4296}
4297
4298/*
4299 * Editor modelines - https://www.wireshark.org/tools/modelines.html
4300 *
4301 * Local variables:
4302 * c-basic-offset: 4
4303 * tab-width: 8
4304 * indent-tabs-mode: nil
4305 * End:
4306 *
4307 * vi: set shiftwidth=4 tabstop=8 expandtab:
4308 * :indentSize=4:tabSize=8:noTabs=true:
4309 */