Bug Summary

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