### Bug Summary

File: | builds/wireshark/wireshark/epan/addr_resolv.c
Warning: | line 798, column 9 Value of 'errno' was not checked and may be overwritten by function 'fgets'

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
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 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-06-05-100346-3529-1 -x c /builds/wireshark/wireshark/epan/addr_resolv.c

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

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