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