Bug Summary

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