2 * Routines for network object lookup
6 * Laurent Deniel <laurent.deniel@free.fr>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
39 * are most likely to take a long time, given the way address-to-name
40 * lookups are done over NBNS).
42 * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
43 * call in a signal handler, you might crash, because the state of the
44 * resolution code that sends messages to lookupd might be inconsistent
45 * if you jump out of it in middle of a call.
47 * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
48 * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
49 * call (and possibly in other gethostbyaddr() calls), because there's
50 * a mutex lock held in gethostbyaddr() and it doesn't get released
51 * if we longjmp out of it.
53 * There's no guarantee that longjmp()ing out of name resolution calls
54 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
55 * code in tcpdump, to avoid those sorts of problems, and that was
56 * picked up by tcpdump.org tcpdump.
58 * So, for now, we do not define AVOID_DNS_TIMEOUT. If we get a
59 * significantly more complaints about lookups taking a long time,
60 * we can reconsider that decision. (Note that tcpdump originally
61 * added that for the benefit of systems using NIS to look up host
62 * names; that might now be fixed in NIS implementations, for those
63 * sites still using NIS rather than DNS for that....)
70 #ifdef HAVE_NETINET_IN_H
71 # include <netinet/in.h>
78 #ifdef HAVE_ARPA_INET_H
79 #include <arpa/inet.h>
84 #ifdef HAVE_SYS_SOCKET_H
85 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
88 #ifdef HAVE_WINSOCK2_H
89 #include <winsock2.h> /* needed to define AF_ values on Windows */
92 #ifdef AVOID_DNS_TIMEOUT
96 #ifdef NEED_INET_ATON_H
97 # include "inet_aton.h"
100 #ifdef NEED_INET_V6DEFS_H
101 # include "inet_v6defs.h"
112 #if defined(_WIN32) && defined(INET6)
113 # include <ws2tcpip.h>
118 #include "report_err.h"
120 #include "ipv6-utils.h"
121 #include "addr_resolv.h"
122 #include "filesystem.h"
123 #include <wiretap/file_util.h>
124 #include <epan/prefs.h>
125 #include <epan/emem.h>
127 #define ENAME_HOSTS "hosts"
128 #define ENAME_ETHERS "ethers"
129 #define ENAME_IPXNETS "ipxnets"
130 #define ENAME_MANUF "manuf"
132 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
133 #define HASHETHSIZE 1024
134 #define HASHHOSTSIZE 1024
135 #define HASHIPXNETSIZE 256
136 #define HASHMANUFSIZE 256
137 #define HASHPORTSIZE 256
139 /* hash table used for IPv4 lookup */
141 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
143 typedef struct hashipv4 {
145 gchar name[MAXNAMELEN];
146 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
147 struct hashipv4 *next;
150 /* hash table used for IPv6 lookup */
152 #define HASH_IPV6_ADDRESS(addr) \
153 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
155 typedef struct hashipv6 {
156 struct e_in6_addr addr;
157 gchar name[MAXNAMELEN];
158 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
159 struct hashipv6 *next;
162 /* hash table used for TCP/UDP/SCTP port lookup */
164 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
166 typedef struct hashport {
168 gchar name[MAXNAMELEN];
169 struct hashport *next;
172 /* hash table used for IPX network lookup */
174 /* XXX - check goodness of hash function */
176 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
178 typedef struct hashipxnet {
180 gchar name[MAXNAMELEN];
181 struct hashipxnet *next;
184 /* hash tables used for ethernet and manufacturer lookup */
186 #define HASH_ETH_ADDRESS(addr) \
187 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
190 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
192 typedef struct hashmanuf {
194 char name[MAXMANUFLEN];
195 struct hashmanuf *next;
198 typedef struct hashether {
200 char name[MAXNAMELEN];
201 gboolean is_dummy_entry; /* not a complete entry */
202 struct hashether *next;
205 /* internal ethernet type */
207 typedef struct _ether
210 char name[MAXNAMELEN];
213 /* internal ipxnet type */
215 typedef struct _ipxnet
218 char name[MAXNAMELEN];
221 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
222 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
223 static hashport_t *udp_port_table[HASHPORTSIZE];
224 static hashport_t *tcp_port_table[HASHPORTSIZE];
225 static hashport_t *sctp_port_table[HASHPORTSIZE];
226 static hashport_t *dccp_port_table[HASHPORTSIZE];
227 static hashether_t *eth_table[HASHETHSIZE];
228 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
229 static hashether_t *(*wka_table[48])[HASHETHSIZE];
230 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
232 static int eth_resolution_initialized = 0;
233 static int ipxnet_resolution_initialized = 0;
235 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
238 * Flag controlling what names to resolve.
240 guint32 g_resolv_flags;
243 * Global variables (can be changed in GUI sections)
244 * XXX - they could be changed in GUI code, but there's currently no
245 * GUI code to change them.
248 gchar *g_ethers_path = NULL; /* global ethers file */
249 gchar *g_pethers_path = NULL; /* personal ethers file */
250 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
251 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
252 /* first resolving call */
258 static gboolean gnu_adns_initialized = FALSE;
262 int adns_currently_queued = 0;
264 typedef struct _adns_queue_msg
268 struct e_in6_addr ip6_addr;
273 GList *adns_queue_head = NULL;
275 #endif /* HAVE_GNU_ADNS */
278 * Local function definitions
281 static gchar *serv_name_lookup(guint port, port_type proto)
286 const char *serv_proto = NULL;
287 struct servent *servp;
291 table = udp_port_table;
295 table = tcp_port_table;
299 table = sctp_port_table;
303 table = dccp_port_table;
307 /* not yet implemented */
313 hash_idx = HASH_PORT(port);
314 tp = table[hash_idx];
317 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
320 if( tp->port == port ) {
323 if (tp->next == NULL) {
324 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
332 /* fill in a new entry */
336 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
337 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
339 g_snprintf(tp->name, MAXNAMELEN, "%d", port);
341 strncpy(tp->name, servp->s_name, MAXNAMELEN);
342 tp->name[MAXNAMELEN-1] = '\0';
347 } /* serv_name_lookup */
350 #ifdef AVOID_DNS_TIMEOUT
352 #define DNS_TIMEOUT 2 /* max sec per call */
354 jmp_buf hostname_env;
356 static void abort_network_query(int sig _U_)
358 longjmp(hostname_env, 1);
360 #endif /* AVOID_DNS_TIMEOUT */
362 static gchar *host_name_lookup(guint addr, gboolean *found)
365 hashipv4_t * volatile tp;
366 struct hostent *hostp;
368 adns_queue_msg_t *qmsg;
373 hash_idx = HASH_IPV4_ADDRESS(addr);
375 tp = ipv4_table[hash_idx];
378 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
381 if( tp->addr == addr ) {
382 if (tp->is_dummy_entry)
386 if (tp->next == NULL) {
387 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
395 /* fill in a new entry */
400 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
401 prefs.name_resolve_concurrency > 0 &&
402 gnu_adns_initialized) {
403 qmsg = g_malloc(sizeof(adns_queue_msg_t));
404 qmsg->type = AF_INET;
405 qmsg->ip4_addr = addr;
406 qmsg->submitted = FALSE;
407 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
409 tp->is_dummy_entry = TRUE;
410 ip_to_str_buf((guint8 *)&addr, tp->name);
413 #endif /* HAVE_GNU_ADNS */
416 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
417 * the name of the host on which it's running; to work around that
418 * botch, we don't try to translate an all-zero IP address to a host
421 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
422 /* Use async DNS if possible, else fall back to timeouts,
423 * else call gethostbyaddr and hope for the best
426 # ifdef AVOID_DNS_TIMEOUT
428 /* Quick hack to avoid DNS/YP timeout */
430 if (!setjmp(hostname_env)) {
431 signal(SIGALRM, abort_network_query);
433 # endif /* AVOID_DNS_TIMEOUT */
435 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
437 # ifdef AVOID_DNS_TIMEOUT
439 # endif /* AVOID_DNS_TIMEOUT */
442 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
443 tp->name[MAXNAMELEN-1] = '\0';
444 tp->is_dummy_entry = FALSE;
448 # ifdef AVOID_DNS_TIMEOUT
450 # endif /* AVOID_DNS_TIMEOUT */
454 /* unknown host or DNS timeout */
456 ip_to_str_buf((guint8 *)&addr, tp->name);
457 tp->is_dummy_entry = TRUE;
462 } /* host_name_lookup */
464 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
467 hashipv6_t * volatile tp;
469 struct hostent *hostp;
474 hash_idx = HASH_IPV6_ADDRESS(*addr);
476 tp = ipv6_table[hash_idx];
479 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
482 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
483 if (tp->is_dummy_entry)
487 if (tp->next == NULL) {
488 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
496 /* fill in a new entry */
501 if (g_resolv_flags & RESOLV_NETWORK) {
502 #ifdef AVOID_DNS_TIMEOUT
504 /* Quick hack to avoid DNS/YP timeout */
506 if (!setjmp(hostname_env)) {
507 signal(SIGALRM, abort_network_query);
509 #endif /* AVOID_DNS_TIMEOUT */
510 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
511 #ifdef AVOID_DNS_TIMEOUT
513 # endif /* AVOID_DNS_TIMEOUT */
516 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
517 tp->name[MAXNAMELEN-1] = '\0';
518 tp->is_dummy_entry = FALSE;
522 #ifdef AVOID_DNS_TIMEOUT
524 # endif /* AVOID_DNS_TIMEOUT */
528 /* unknown host or DNS timeout */
530 ip6_to_str_buf(addr, tp->name);
531 tp->is_dummy_entry = TRUE;
535 } /* host_name_lookup6 */
537 static const gchar *solve_address_to_name(address *addr)
540 struct e_in6_addr ipv6_addr;
542 switch (addr->type) {
545 return get_ether_name(addr->data);
548 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
549 return get_hostname(ipv4_addr);
552 memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
553 return get_hostname6(&ipv6_addr);
561 } /* solve_address_to_name */
565 * Miscellaneous functions
568 static int fgetline(char **buf, int *size, FILE *fp)
580 if ((*buf = g_malloc(*size)) == NULL)
588 while ((c = getc(fp)) != EOF && c != '\n') {
589 if (len+1 >= *size) {
590 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
596 if (len == 0 && c == EOF)
607 * Ethernet / manufacturer resolution
609 * The following functions implement ethernet address resolution and
610 * ethers files parsing (see ethers(4)).
612 * The manuf file has the same format as ethers(4) except that names are
613 * truncated to MAXMANUFLEN-1 characters and that an address contains
614 * only 3 bytes (instead of 6).
618 * I decide to not use the existing functions (see ethers(3) on some
619 * operating systems) for the following reasons:
620 * - performance gains (use of hash tables and some other enhancements),
621 * - use of two ethers files (system-wide and per user),
622 * - avoid the use of NIS maps,
623 * - lack of these functions on some systems.
625 * So the following functions do _not_ behave as the standard ones.
632 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
633 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
637 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
645 for (i = 0; i < 6; i++) {
646 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
647 if (!isxdigit((unsigned char)*cp))
649 num = strtoul(cp, &p, 16);
651 return FALSE; /* failed */
653 return FALSE; /* not a valid octet */
654 eth->addr[i] = (guint8) num;
655 cp = p; /* skip past the number */
657 /* OK, what character terminated the octet? */
659 /* "/" - this has a mask. */
661 /* Entries with masks are allowed only in the "manuf" files. */
664 cp++; /* skip past the '/' to get to the mask */
665 if (!isdigit((unsigned char)*cp))
666 return FALSE; /* no sign allowed */
667 num = strtoul(cp, &p, 10);
669 return FALSE; /* failed */
670 cp = p; /* skip past the number */
671 if (*cp != '\0' && !isspace((unsigned char)*cp))
672 return FALSE; /* bogus terminator */
673 if (num == 0 || num >= 48)
674 return FALSE; /* bogus mask */
675 /* Mask out the bits not covered by the mask */
677 for (i = 0; num >= 8; i++, num -= 8)
678 ; /* skip octets entirely covered by the mask */
679 /* Mask out the first masked octet */
680 eth->addr[i] &= (0xFF << (8 - num));
682 /* Mask out completely-masked-out octets */
688 /* We're at the end of the address, and there's no mask. */
690 /* We got 3 bytes, so this is a manufacturer ID. */
692 /* Manufacturer IDs are only allowed in the "manuf"
696 /* Indicate that this is a manufacturer ID (0 is not allowed
703 /* We got 6 bytes, so this is a MAC address.
704 If we're reading one of the "manuf" files, indicate that
705 this is a MAC address (48 is not allowed as a mask). */
711 /* We didn't get 3 or 6 bytes, and there's no mask; this is
716 /* We don't know the separator used in this number; it can either
717 be ':', '-', or '.'. */
718 if (*cp != ':' && *cp != '-' && *cp != '.')
720 sep = *cp; /* subsequent separators must be the same */
722 /* It has to be the same as the first separator */
733 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
737 * See the ethers(4) or ethers(5) man page for ethers file format
738 * (not available on all systems).
739 * We allow both ethernet address separators (':' and '-'),
740 * as well as Ethereal's '.' separator.
745 if ((cp = strchr(line, '#')))
748 if ((cp = strtok(line, " \t")) == NULL)
751 if (!parse_ether_address(cp, eth, mask, manuf_file))
754 if ((cp = strtok(NULL, " \t")) == NULL)
757 strncpy(eth->name, cp, MAXNAMELEN);
758 eth->name[MAXNAMELEN-1] = '\0';
762 } /* parse_ether_line */
764 static FILE *eth_p = NULL;
766 static void set_ethent(char *path)
771 eth_p = eth_fopen(path, "r");
774 static void end_ethent(void)
782 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
787 static char *buf = NULL;
792 while (fgetline(&buf, &size, eth_p) >= 0) {
793 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
802 static ether_t *get_ethbyname(const gchar *name)
806 set_ethent(g_pethers_path);
808 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
814 set_ethent(g_ethers_path);
816 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
824 } /* get_ethbyname */
826 static ether_t *get_ethbyaddr(const guint8 *addr)
831 set_ethent(g_pethers_path);
833 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
839 set_ethent(g_ethers_path);
841 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
849 } /* get_ethbyaddr */
851 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
854 /* All but the topmost byte is masked out */
855 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
859 /* All but the topmost 2 bytes are masked out */
860 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
865 /* All but the topmost 3 bytes are masked out */
866 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
871 /* All but the topmost 4 bytes are masked out */
872 return ((((addr[0] << 8) | addr[1]) ^
873 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
878 /* All but the topmost 5 bytes are masked out */
879 return ((((addr[1] << 8) | addr[2]) ^
880 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
884 /* No bytes are fully masked out */
885 return ((((addr[1] << 8) | addr[2]) ^
886 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
890 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
894 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
897 /* This is a well-known MAC address; just add this to the Ethernet
899 add_eth_name(addr, name);
904 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
906 hash_idx = HASH_ETH_MANUF(addr);
908 tp = manuf_table[hash_idx];
911 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
914 if (tp->next == NULL) {
915 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
923 memcpy(tp->addr, addr, sizeof(tp->addr));
924 strncpy(tp->name, name, MAXMANUFLEN);
925 tp->name[MAXMANUFLEN-1] = '\0';
930 /* This is a range of well-known addresses; add it to the appropriate
931 well-known-address table, creating that table if necessary. */
932 wka_tp = wka_table[mask];
934 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
936 hash_idx = hash_eth_wka(addr, mask);
938 etp = (*wka_tp)[hash_idx];
941 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
944 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
945 /* address already known */
948 if (etp->next == NULL) {
949 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
957 memcpy(etp->addr, addr, sizeof(etp->addr));
958 strncpy(etp->name, name, MAXNAMELEN);
959 etp->name[MAXNAMELEN-1] = '\0';
961 etp->is_dummy_entry = FALSE;
963 } /* add_manuf_name */
965 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
970 hash_idx = HASH_ETH_MANUF(addr);
972 tp = manuf_table[hash_idx];
975 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
983 } /* manuf_name_lookup */
985 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
988 hashether_t *(*wka_tp)[HASHETHSIZE];
990 guint8 masked_addr[6];
994 wka_tp = wka_table[mask];
995 if (wka_tp == NULL) {
996 /* There are no entries in the table for that mask value, as there is
997 no table for that mask value. */
1001 /* Get the part of the address covered by the mask. */
1002 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1003 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1004 /* Mask out the first masked octet */
1005 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1007 /* Zero out completely-masked-out octets */
1011 hash_idx = hash_eth_wka(masked_addr, mask);
1013 tp = (*wka_tp)[hash_idx];
1016 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1024 } /* wka_name_lookup */
1026 static void initialize_ethers(void)
1032 /* Compute the pathname of the ethers file. */
1033 if (g_ethers_path == NULL) {
1034 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1035 get_systemfile_dir(), ENAME_ETHERS);
1038 /* Set g_pethers_path here, but don't actually do anything
1039 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1041 if (g_pethers_path == NULL)
1042 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1044 /* manuf hash table initialization */
1046 /* Compute the pathname of the manuf file */
1047 manuf_path = get_datafile_path(ENAME_MANUF);
1049 /* Read it and initialize the hash table */
1050 set_ethent(manuf_path);
1052 while ((eth = get_ethent(&mask, TRUE))) {
1053 add_manuf_name(eth->addr, mask, eth->name);
1060 } /* initialize_ethers */
1062 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1067 hash_idx = HASH_ETH_ADDRESS(addr);
1069 tp = eth_table[hash_idx];
1072 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1075 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1076 /* address already known */
1077 if (!tp->is_dummy_entry) {
1080 /* replace this dummy (manuf) entry with a real name */
1084 if (tp->next == NULL) {
1085 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1093 memcpy(tp->addr, addr, sizeof(tp->addr));
1094 strncpy(tp->name, name, MAXNAMELEN);
1095 tp->name[MAXNAMELEN-1] = '\0';
1097 tp->is_dummy_entry = FALSE;
1101 } /* add_eth_name */
1103 static gchar *eth_name_lookup(const guint8 *addr)
1106 hashmanuf_t *manufp;
1112 hash_idx = HASH_ETH_ADDRESS(addr);
1114 tp = eth_table[hash_idx];
1117 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1120 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1123 if (tp->next == NULL) {
1124 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1132 /* fill in a new entry */
1134 memcpy(tp->addr, addr, sizeof(tp->addr));
1137 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1138 /* Unknown name. Try looking for it in the well-known-address
1139 tables for well-known address ranges smaller than 2^24. */
1142 /* Only the topmost 5 bytes participate fully */
1143 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1144 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1145 etp->name, addr[5] & (0xFF >> mask));
1146 tp->is_dummy_entry = TRUE;
1156 /* Only the topmost 4 bytes participate fully */
1157 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1158 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1159 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1160 tp->is_dummy_entry = TRUE;
1170 /* Only the topmost 3 bytes participate fully */
1171 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1172 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1173 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1174 tp->is_dummy_entry = TRUE;
1182 /* Now try looking in the manufacturer table. */
1183 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1184 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1185 manufp->name, addr[3], addr[4], addr[5]);
1186 tp->is_dummy_entry = TRUE;
1190 /* Now try looking for it in the well-known-address
1191 tables for well-known address ranges larger than 2^24. */
1194 /* Only the topmost 2 bytes participate fully */
1195 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1196 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1197 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1199 tp->is_dummy_entry = TRUE;
1209 /* Only the topmost byte participates fully */
1210 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1211 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1212 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1214 tp->is_dummy_entry = TRUE;
1222 for (mask = 7; mask > 0; mask--) {
1223 /* Not even the topmost byte participates fully */
1224 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1225 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1226 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1227 addr[3], addr[4], addr[5]);
1228 tp->is_dummy_entry = TRUE;
1233 /* No match whatsoever. */
1234 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1235 tp->is_dummy_entry = TRUE;
1238 strncpy(tp->name, eth->name, MAXNAMELEN);
1239 tp->name[MAXNAMELEN-1] = '\0';
1240 tp->is_dummy_entry = FALSE;
1245 } /* eth_name_lookup */
1247 static guint8 *eth_addr_lookup(const gchar *name)
1251 hashether_t **table = eth_table;
1254 /* to be optimized (hash table from name to addr) */
1255 for (i = 0; i < HASHETHSIZE; i++) {
1258 if (strcmp(tp->name, name) == 0)
1264 /* not in hash table : performs a file lookup */
1266 if ((eth = get_ethbyname(name)) == NULL)
1269 /* add new entry in hash table */
1271 tp = add_eth_name(eth->addr, name);
1275 } /* eth_addr_lookup */
1279 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1282 * We allow three address separators (':', '-', and '.'),
1283 * as well as no separators
1287 guint32 a, a0, a1, a2, a3;
1288 gboolean found_single_number = FALSE;
1290 if ((cp = strchr(line, '#')))
1293 if ((cp = strtok(line, " \t\n")) == NULL)
1296 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1297 * fill a and found_single_number is TRUE,
1300 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1301 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1302 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1303 if (sscanf(cp, "%x", &a) == 1) {
1304 found_single_number = TRUE;
1313 if ((cp = strtok(NULL, " \t\n")) == NULL)
1316 if (found_single_number) {
1320 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1323 strncpy(ipxnet->name, cp, MAXNAMELEN);
1324 ipxnet->name[MAXNAMELEN-1] = '\0';
1328 } /* parse_ipxnets_line */
1330 static FILE *ipxnet_p = NULL;
1332 static void set_ipxnetent(char *path)
1337 ipxnet_p = eth_fopen(path, "r");
1340 static void end_ipxnetent(void)
1348 static ipxnet_t *get_ipxnetent(void)
1351 static ipxnet_t ipxnet;
1352 static int size = 0;
1353 static char *buf = NULL;
1355 if (ipxnet_p == NULL)
1358 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1359 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1366 } /* get_ipxnetent */
1368 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1372 set_ipxnetent(g_ipxnets_path);
1374 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1377 if (ipxnet == NULL) {
1380 set_ipxnetent(g_pipxnets_path);
1382 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1390 } /* get_ipxnetbyname */
1392 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1397 set_ipxnetent(g_ipxnets_path);
1399 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1401 if (ipxnet == NULL) {
1404 set_ipxnetent(g_pipxnets_path);
1406 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1414 } /* get_ipxnetbyaddr */
1416 static void initialize_ipxnets(void)
1418 /* Compute the pathname of the ipxnets file.
1420 * XXX - is there a notion of an "ipxnets file" in any flavor of
1421 * UNIX, or with any add-on Netware package for UNIX? If not,
1422 * should the UNIX version of the ipxnets file be in the datafile
1423 * directory as well?
1425 if (g_ipxnets_path == NULL) {
1426 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1427 get_systemfile_dir(), ENAME_IPXNETS);
1430 /* Set g_pipxnets_path here, but don't actually do anything
1431 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1433 if (g_pipxnets_path == NULL)
1434 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1436 } /* initialize_ipxnets */
1438 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1443 hash_idx = HASH_IPX_NET(addr);
1445 tp = ipxnet_table[hash_idx];
1448 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1451 if (tp->next == NULL) {
1452 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1461 strncpy(tp->name, name, MAXNAMELEN);
1462 tp->name[MAXNAMELEN-1] = '\0';
1467 } /* add_ipxnet_name */
1469 static gchar *ipxnet_name_lookup(const guint addr)
1475 hash_idx = HASH_IPX_NET(addr);
1477 tp = ipxnet_table[hash_idx];
1480 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1483 if (tp->addr == addr) {
1486 if (tp->next == NULL) {
1487 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1495 /* fill in a new entry */
1500 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1502 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1505 strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1506 tp->name[MAXNAMELEN-1] = '\0';
1511 } /* ipxnet_name_lookup */
1513 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1517 hashipxnet_t **table = ipxnet_table;
1520 /* to be optimized (hash table from name to addr) */
1521 for (i = 0; i < HASHIPXNETSIZE; i++) {
1524 if (strcmp(tp->name, name) == 0) {
1532 /* not in hash table : performs a file lookup */
1534 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1539 /* add new entry in hash table */
1541 tp = add_ipxnet_name(ipxnet->addr, name);
1546 } /* ipxnet_addr_lookup */
1549 read_hosts_file (const char *hostspath)
1555 guint32 host_addr[4]; /* IPv4 or IPv6 */
1556 struct e_in6_addr ipv6_addr;
1561 * See the hosts(4) or hosts(5) man page for hosts file format
1562 * (not available on all systems).
1564 if ((hf = eth_fopen(hostspath, "r")) == NULL)
1567 while (fgetline(&line, &size, hf) >= 0) {
1568 if ((cp = strchr(line, '#')))
1571 if ((cp = strtok(line, " \t")) == NULL)
1572 continue; /* no tokens in the line */
1574 ret = inet_pton(AF_INET6, cp, &host_addr);
1576 continue; /* error parsing */
1581 /* Not valid IPv6 - valid IPv4? */
1582 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1587 if ((cp = strtok(NULL, " \t")) == NULL)
1588 continue; /* no host name */
1591 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1592 add_ipv6_name(&ipv6_addr, cp);
1594 add_ipv4_name(host_addr[0], cp);
1597 * Add the aliases, too, if there are any.
1599 while ((cp = strtok(NULL, " \t")) != NULL) {
1601 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1602 add_ipv6_name(&ipv6_addr, cp);
1604 add_ipv4_name(host_addr[0], cp);
1612 } /* read_hosts_file */
1615 * External Functions
1619 host_name_lookup_init(void) {
1622 #ifdef HAVE_GNU_ADNS
1625 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
1626 static char rootpath_ot[] = "\\hosts";
1628 #endif /*GNU_ADNS */
1631 * Load the user's hosts file, if they have one.
1633 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE);
1634 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1635 report_open_failure(hostspath, errno, FALSE);
1639 #ifdef HAVE_GNU_ADNS
1641 * We're using GNU ADNS, which doesn't check the system hosts file;
1642 * we load that file ourselves.
1646 sysroot = getenv_utf8("WINDIR");
1647 if (sysroot != NULL) {
1649 * The file should be under WINDIR.
1650 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
1651 * %WINDIR%\system32\drivers\etc\hosts.
1652 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
1654 * XXX - should we base it on the dwPlatformId value from
1657 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
1658 if (!read_hosts_file(hostspath)) {
1660 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
1661 read_hosts_file(hostspath);
1666 read_hosts_file("/etc/hosts");
1669 /* XXX - Any flags we should be using? */
1670 /* XXX - We could provide config settings for DNS servers, and
1671 pass them to ADNS with adns_init_strcfg */
1672 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1674 * XXX - should we report the error? I'm assuming that some crashes
1675 * reported on a Windows machine with TCP/IP not configured are due
1676 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1677 * ADNS in a state where it crashes due to that. We'll still try
1678 * doing name resolution anyway.
1682 gnu_adns_initialized = TRUE;
1683 adns_currently_queued = 0;
1684 #endif /* HAVE_GNU_ADNS */
1687 #ifdef HAVE_GNU_ADNS
1689 /* XXX - The ADNS "documentation" isn't very clear:
1690 * - Do we need to keep our query structures around?
1693 host_name_lookup_process(gpointer data _U_) {
1694 adns_queue_msg_t *almsg;
1696 char addr_str[] = "111.222.333.444.in-addr.arpa.";
1702 adns_queue_head = g_list_first(adns_queue_head);
1704 cur = adns_queue_head;
1705 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1706 almsg = (adns_queue_msg_t *) cur->data;
1707 if (! almsg->submitted && almsg->type == AF_INET) {
1708 addr_bytes = (guint8 *) &almsg->ip4_addr;
1709 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1710 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1711 /* XXX - what if it fails? */
1712 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1713 almsg->submitted = TRUE;
1714 adns_currently_queued++;
1719 cur = adns_queue_head;
1722 almsg = (adns_queue_msg_t *) cur->data;
1723 if (almsg->submitted) {
1724 ret = adns_check(ads, &almsg->query, &ans, NULL);
1726 if (ans->status == adns_s_ok) {
1727 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1734 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1736 adns_currently_queued--;
1740 /* Keep the timeout in place */
1745 host_name_lookup_cleanup(void) {
1748 adns_queue_head = g_list_first(adns_queue_head);
1749 while (adns_queue_head) {
1750 qdata = adns_queue_head->data;
1751 adns_queue_head = g_list_remove(adns_queue_head, qdata);
1755 if (gnu_adns_initialized)
1762 host_name_lookup_process(gpointer data _U_) {
1763 /* Kill the timeout, as there's nothing for it to do */
1768 host_name_lookup_cleanup(void) {
1771 #endif /* HAVE_GNU_ADNS */
1773 extern gchar *get_hostname(guint addr)
1777 if (!(g_resolv_flags & RESOLV_NETWORK))
1778 return ip_to_str((guint8 *)&addr);
1780 return host_name_lookup(addr, &found);
1783 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1787 if (!(g_resolv_flags & RESOLV_NETWORK))
1788 return ip6_to_str(addr);
1789 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
1790 return ip6_to_str(addr);
1791 return host_name_lookup6(addr, &found);
1794 extern void add_ipv4_name(guint addr, const gchar *name)
1799 hash_idx = HASH_IPV4_ADDRESS(addr);
1801 tp = ipv4_table[hash_idx];
1804 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1807 if (tp->addr == addr) {
1808 /* address already known */
1809 if (!tp->is_dummy_entry) {
1812 /* replace this dummy entry with the new one */
1816 if (tp->next == NULL) {
1817 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1825 strncpy(tp->name, name, MAXNAMELEN);
1826 tp->name[MAXNAMELEN-1] = '\0';
1829 tp->is_dummy_entry = FALSE;
1831 } /* add_ipv4_name */
1833 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
1838 hash_idx = HASH_IPV6_ADDRESS(*addrp);
1840 tp = ipv6_table[hash_idx];
1843 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1846 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
1847 /* address already known */
1848 if (!tp->is_dummy_entry) {
1851 /* replace this dummy entry with the new one */
1855 if (tp->next == NULL) {
1856 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1864 strncpy(tp->name, name, MAXNAMELEN);
1865 tp->name[MAXNAMELEN-1] = '\0';
1868 tp->is_dummy_entry = FALSE;
1870 } /* add_ipv6_name */
1872 extern gchar *get_udp_port(guint port)
1876 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1877 cur=ep_alloc(MAXNAMELEN);
1878 g_snprintf(cur, MAXNAMELEN, "%u", port);
1882 return serv_name_lookup(port, PT_UDP);
1884 } /* get_udp_port */
1886 extern gchar *get_dccp_port(guint port)
1890 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1891 cur=ep_alloc(MAXNAMELEN);
1892 g_snprintf(cur, MAXNAMELEN, "%u", port);
1896 return serv_name_lookup(port, PT_DCCP);
1898 } /* get_dccp_port */
1901 extern gchar *get_tcp_port(guint port)
1905 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1906 cur=ep_alloc(MAXNAMELEN);
1907 g_snprintf(cur, MAXNAMELEN, "%u", port);
1911 return serv_name_lookup(port, PT_TCP);
1913 } /* get_tcp_port */
1915 extern gchar *get_sctp_port(guint port)
1919 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1920 cur=ep_alloc(MAXNAMELEN);
1921 g_snprintf(cur, MAXNAMELEN, "%u", port);
1925 return serv_name_lookup(port, PT_SCTP);
1927 } /* get_sctp_port */
1930 const gchar *get_addr_name(address *addr)
1932 const gchar *result;
1934 result = solve_address_to_name(addr);
1940 /* if it gets here, either it is of type AT_NONE, */
1941 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
1943 if (addr->type == AT_NONE){
1947 return(address_to_str(addr));
1948 } /* get_addr_name */
1951 void get_addr_name_buf(address *addr, gchar *buf, guint size)
1953 const gchar *result;
1955 result = get_addr_name(addr);
1957 strncpy(buf,result,size);
1961 } /* get_addr_name_buf */
1964 extern gchar *get_ether_name(const guint8 *addr)
1966 if (!(g_resolv_flags & RESOLV_MAC))
1967 return ether_to_str(addr);
1969 if (!eth_resolution_initialized) {
1970 initialize_ethers();
1971 eth_resolution_initialized = 1;
1974 return eth_name_lookup(addr);
1976 } /* get_ether_name */
1978 /* Look for an ether name in the hash, and return it if found.
1979 * If it's not found, simply return NULL. We DO NOT make a new
1980 * hash entry for it with the hex digits turned into a string.
1982 gchar *get_ether_name_if_known(const guint8 *addr)
1987 /* Initialize ether structs if we're the first
1988 * ether-related function called */
1989 if (!(g_resolv_flags & RESOLV_MAC))
1992 if (!eth_resolution_initialized) {
1993 initialize_ethers();
1994 eth_resolution_initialized = 1;
1997 hash_idx = HASH_ETH_ADDRESS(addr);
1999 tp = eth_table[hash_idx];
2002 /* Hash key not found in table.
2003 * Force a lookup (and a hash entry) for addr, then call
2004 * myself. I plan on not getting into an infinite loop because
2005 * eth_name_lookup() is guaranteed to make a hashtable entry,
2006 * so when I call myself again, I can never get into this
2007 * block of code again. Knock on wood...
2009 (void) eth_name_lookup(addr);
2010 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2014 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2015 if (!tp->is_dummy_entry) {
2016 /* A name was found, and its origin is an ethers file */
2020 /* A name was found, but it was created, not found in a file */
2024 if (tp->next == NULL) {
2025 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2026 (void) eth_name_lookup(addr);
2027 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2032 g_assert_not_reached();
2037 extern guint8 *get_ether_addr(const gchar *name)
2040 /* force resolution (do not check g_resolv_flags) */
2042 if (!eth_resolution_initialized) {
2043 initialize_ethers();
2044 eth_resolution_initialized = 1;
2047 return eth_addr_lookup(name);
2049 } /* get_ether_addr */
2051 extern void add_ether_byip(guint ip, const guint8 *eth)
2057 /* first check that IP address can be resolved */
2059 if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2062 /* ok, we can add this entry in the ethers hashtable */
2065 add_eth_name(eth, host);
2067 } /* add_ether_byip */
2069 extern const gchar *get_ipxnet_name(const guint32 addr)
2072 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2073 return ipxnet_to_str_punct(addr, '\0');
2076 if (!ipxnet_resolution_initialized) {
2077 initialize_ipxnets();
2078 ipxnet_resolution_initialized = 1;
2081 return ipxnet_name_lookup(addr);
2083 } /* get_ipxnet_name */
2085 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2090 /* force resolution (do not check g_resolv_flags) */
2092 if (!ipxnet_resolution_initialized) {
2093 initialize_ipxnets();
2094 ipxnet_resolution_initialized = 1;
2097 addr = ipxnet_addr_lookup(name, &success);
2102 } /* get_ipxnet_addr */
2104 extern const gchar *get_manuf_name(const guint8 *addr)
2107 hashmanuf_t *manufp;
2109 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2110 initialize_ethers();
2111 eth_resolution_initialized = 1;
2114 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2115 cur=ep_alloc(MAXMANUFLEN);
2116 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2120 return manufp->name;
2122 } /* get_manuf_name */
2125 const gchar *get_manuf_name_if_known(const guint8 *addr)
2127 hashmanuf_t *manufp;
2129 if (!eth_resolution_initialized) {
2130 initialize_ethers();
2131 eth_resolution_initialized = 1;
2134 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2138 return manufp->name;
2140 } /* get_manuf_name_if_known */
2143 /* Translate a string, assumed either to be a dotted-quad IP address or
2144 * a host name, to a numeric IP address. Return TRUE if we succeed and
2145 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2146 * Used more in the dfilter parser rather than in packet dissectors */
2147 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2149 struct in_addr ipaddr;
2153 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2154 * inet_pton(AF_INET) does not support hexadecimal notation nor
2155 * less-than-4 octet notation.
2157 if (!inet_aton(host, &ipaddr)) {
2158 /* It's not a valid dotted-quad IP address; is it a valid
2160 hp = gethostbyname(host);
2164 /* Apparently, some versions of gethostbyaddr can
2165 * return IPv6 addresses. */
2166 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2167 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2172 /* Does the string really contain dotted-quad IP?
2173 * Check against inet_atons that accept strings such as
2174 * "130.230" as valid addresses and try to convert them
2175 * to some form of a classful (host.net) notation.
2177 unsigned int a0, a1, a2, a3;
2178 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2182 *addrp = g_ntohl(ipaddr.s_addr);
2187 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2188 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2189 * return FALSE if we fail.
2191 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2195 if (inet_pton(AF_INET6, host, addrp) == 1)
2199 #ifdef HAVE_GETHOSTBYNAME2
2200 hp = gethostbyname2(host, AF_INET6);
2204 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2205 memcpy(addrp, hp->h_addr, hp->h_length);
2213 * Find out whether a hostname resolves to an ip or ipv6 address
2214 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2215 * that we don't know)
2217 const char* host_ip_af(const char *host
2218 #ifndef HAVE_GETHOSTBYNAME2
2223 #ifdef HAVE_GETHOSTBYNAME2
2225 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";