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 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
146 struct hashipv4 *next;
147 gchar name[MAXNAMELEN];
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)
1068 hash_idx = HASH_ETH_ADDRESS(addr);
1070 tp = eth_table[hash_idx];
1073 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1076 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1077 /* address already known */
1078 if (!tp->is_dummy_entry) {
1081 /* replace this dummy (manuf) entry with a real name */
1086 if (tp->next == NULL) {
1087 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1095 strncpy(tp->name, name, MAXNAMELEN);
1096 tp->name[MAXNAMELEN-1] = '\0';
1098 memcpy(tp->addr, addr, sizeof(tp->addr));
1101 tp->is_dummy_entry = FALSE;
1105 } /* add_eth_name */
1107 static gchar *eth_name_lookup(const guint8 *addr)
1110 hashmanuf_t *manufp;
1116 hash_idx = HASH_ETH_ADDRESS(addr);
1118 tp = eth_table[hash_idx];
1121 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1124 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1127 if (tp->next == NULL) {
1128 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1136 /* fill in a new entry */
1138 memcpy(tp->addr, addr, sizeof(tp->addr));
1141 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1142 /* Unknown name. Try looking for it in the well-known-address
1143 tables for well-known address ranges smaller than 2^24. */
1146 /* Only the topmost 5 bytes participate fully */
1147 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1148 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1149 etp->name, addr[5] & (0xFF >> mask));
1150 tp->is_dummy_entry = TRUE;
1160 /* Only the topmost 4 bytes participate fully */
1161 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1162 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1163 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1164 tp->is_dummy_entry = TRUE;
1174 /* Only the topmost 3 bytes participate fully */
1175 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1176 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1177 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1178 tp->is_dummy_entry = TRUE;
1186 /* Now try looking in the manufacturer table. */
1187 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1188 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1189 manufp->name, addr[3], addr[4], addr[5]);
1190 tp->is_dummy_entry = TRUE;
1194 /* Now try looking for it in the well-known-address
1195 tables for well-known address ranges larger than 2^24. */
1198 /* Only the topmost 2 bytes participate fully */
1199 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1200 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1201 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1203 tp->is_dummy_entry = TRUE;
1213 /* Only the topmost byte participates fully */
1214 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1215 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1216 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1218 tp->is_dummy_entry = TRUE;
1226 for (mask = 7; mask > 0; mask--) {
1227 /* Not even the topmost byte participates fully */
1228 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1229 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1230 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1231 addr[3], addr[4], addr[5]);
1232 tp->is_dummy_entry = TRUE;
1237 /* No match whatsoever. */
1238 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1239 tp->is_dummy_entry = TRUE;
1242 strncpy(tp->name, eth->name, MAXNAMELEN);
1243 tp->name[MAXNAMELEN-1] = '\0';
1244 tp->is_dummy_entry = FALSE;
1249 } /* eth_name_lookup */
1251 static guint8 *eth_addr_lookup(const gchar *name)
1255 hashether_t **table = eth_table;
1258 /* to be optimized (hash table from name to addr) */
1259 for (i = 0; i < HASHETHSIZE; i++) {
1262 if (strcmp(tp->name, name) == 0)
1268 /* not in hash table : performs a file lookup */
1270 if ((eth = get_ethbyname(name)) == NULL)
1273 /* add new entry in hash table */
1275 tp = add_eth_name(eth->addr, name);
1279 } /* eth_addr_lookup */
1283 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1286 * We allow three address separators (':', '-', and '.'),
1287 * as well as no separators
1291 guint32 a, a0, a1, a2, a3;
1292 gboolean found_single_number = FALSE;
1294 if ((cp = strchr(line, '#')))
1297 if ((cp = strtok(line, " \t\n")) == NULL)
1300 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1301 * fill a and found_single_number is TRUE,
1304 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1305 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1306 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1307 if (sscanf(cp, "%x", &a) == 1) {
1308 found_single_number = TRUE;
1317 if ((cp = strtok(NULL, " \t\n")) == NULL)
1320 if (found_single_number) {
1324 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1327 strncpy(ipxnet->name, cp, MAXNAMELEN);
1328 ipxnet->name[MAXNAMELEN-1] = '\0';
1332 } /* parse_ipxnets_line */
1334 static FILE *ipxnet_p = NULL;
1336 static void set_ipxnetent(char *path)
1341 ipxnet_p = eth_fopen(path, "r");
1344 static void end_ipxnetent(void)
1352 static ipxnet_t *get_ipxnetent(void)
1355 static ipxnet_t ipxnet;
1356 static int size = 0;
1357 static char *buf = NULL;
1359 if (ipxnet_p == NULL)
1362 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1363 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1370 } /* get_ipxnetent */
1372 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1376 set_ipxnetent(g_ipxnets_path);
1378 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1381 if (ipxnet == NULL) {
1384 set_ipxnetent(g_pipxnets_path);
1386 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1394 } /* get_ipxnetbyname */
1396 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1401 set_ipxnetent(g_ipxnets_path);
1403 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1405 if (ipxnet == NULL) {
1408 set_ipxnetent(g_pipxnets_path);
1410 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1418 } /* get_ipxnetbyaddr */
1420 static void initialize_ipxnets(void)
1422 /* Compute the pathname of the ipxnets file.
1424 * XXX - is there a notion of an "ipxnets file" in any flavor of
1425 * UNIX, or with any add-on Netware package for UNIX? If not,
1426 * should the UNIX version of the ipxnets file be in the datafile
1427 * directory as well?
1429 if (g_ipxnets_path == NULL) {
1430 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1431 get_systemfile_dir(), ENAME_IPXNETS);
1434 /* Set g_pipxnets_path here, but don't actually do anything
1435 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1437 if (g_pipxnets_path == NULL)
1438 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1440 } /* initialize_ipxnets */
1442 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1447 hash_idx = HASH_IPX_NET(addr);
1449 tp = ipxnet_table[hash_idx];
1452 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1455 if (tp->next == NULL) {
1456 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1465 strncpy(tp->name, name, MAXNAMELEN);
1466 tp->name[MAXNAMELEN-1] = '\0';
1471 } /* add_ipxnet_name */
1473 static gchar *ipxnet_name_lookup(const guint addr)
1479 hash_idx = HASH_IPX_NET(addr);
1481 tp = ipxnet_table[hash_idx];
1484 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1487 if (tp->addr == addr) {
1490 if (tp->next == NULL) {
1491 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1499 /* fill in a new entry */
1504 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1506 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1509 strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1510 tp->name[MAXNAMELEN-1] = '\0';
1515 } /* ipxnet_name_lookup */
1517 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1521 hashipxnet_t **table = ipxnet_table;
1524 /* to be optimized (hash table from name to addr) */
1525 for (i = 0; i < HASHIPXNETSIZE; i++) {
1528 if (strcmp(tp->name, name) == 0) {
1536 /* not in hash table : performs a file lookup */
1538 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1543 /* add new entry in hash table */
1545 tp = add_ipxnet_name(ipxnet->addr, name);
1550 } /* ipxnet_addr_lookup */
1553 read_hosts_file (const char *hostspath)
1559 guint32 host_addr[4]; /* IPv4 or IPv6 */
1560 struct e_in6_addr ipv6_addr;
1565 * See the hosts(4) or hosts(5) man page for hosts file format
1566 * (not available on all systems).
1568 if ((hf = eth_fopen(hostspath, "r")) == NULL)
1571 while (fgetline(&line, &size, hf) >= 0) {
1572 if ((cp = strchr(line, '#')))
1575 if ((cp = strtok(line, " \t")) == NULL)
1576 continue; /* no tokens in the line */
1578 ret = inet_pton(AF_INET6, cp, &host_addr);
1580 continue; /* error parsing */
1585 /* Not valid IPv6 - valid IPv4? */
1586 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1591 if ((cp = strtok(NULL, " \t")) == NULL)
1592 continue; /* no host name */
1595 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1596 add_ipv6_name(&ipv6_addr, cp);
1598 add_ipv4_name(host_addr[0], cp);
1601 * Add the aliases, too, if there are any.
1603 while ((cp = strtok(NULL, " \t")) != NULL) {
1605 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1606 add_ipv6_name(&ipv6_addr, cp);
1608 add_ipv4_name(host_addr[0], cp);
1616 } /* read_hosts_file */
1619 * External Functions
1623 host_name_lookup_init(void) {
1626 #ifdef HAVE_GNU_ADNS
1629 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
1630 static char rootpath_ot[] = "\\hosts";
1632 #endif /*GNU_ADNS */
1635 * Load the user's hosts file, if they have one.
1637 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE);
1638 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1639 report_open_failure(hostspath, errno, FALSE);
1644 * Load the global hosts file, if we have one.
1646 hostspath = get_datafile_path(ENAME_HOSTS);
1647 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1648 report_open_failure(hostspath, errno, FALSE);
1652 #ifdef HAVE_GNU_ADNS
1654 * We're using GNU ADNS, which doesn't check the system hosts file;
1655 * we load that file ourselves.
1659 sysroot = getenv_utf8("WINDIR");
1660 if (sysroot != NULL) {
1662 * The file should be under WINDIR.
1663 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
1664 * %WINDIR%\system32\drivers\etc\hosts.
1665 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
1667 * XXX - should we base it on the dwPlatformId value from
1670 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
1671 if (!read_hosts_file(hostspath)) {
1673 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
1674 read_hosts_file(hostspath);
1679 read_hosts_file("/etc/hosts");
1682 /* XXX - Any flags we should be using? */
1683 /* XXX - We could provide config settings for DNS servers, and
1684 pass them to ADNS with adns_init_strcfg */
1685 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1687 * XXX - should we report the error? I'm assuming that some crashes
1688 * reported on a Windows machine with TCP/IP not configured are due
1689 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1690 * ADNS in a state where it crashes due to that. We'll still try
1691 * doing name resolution anyway.
1695 gnu_adns_initialized = TRUE;
1696 adns_currently_queued = 0;
1697 #endif /* HAVE_GNU_ADNS */
1700 #ifdef HAVE_GNU_ADNS
1702 /* XXX - The ADNS "documentation" isn't very clear:
1703 * - Do we need to keep our query structures around?
1706 host_name_lookup_process(gpointer data _U_) {
1707 adns_queue_msg_t *almsg;
1709 char addr_str[] = "111.222.333.444.in-addr.arpa.";
1715 adns_queue_head = g_list_first(adns_queue_head);
1717 cur = adns_queue_head;
1718 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1719 almsg = (adns_queue_msg_t *) cur->data;
1720 if (! almsg->submitted && almsg->type == AF_INET) {
1721 addr_bytes = (guint8 *) &almsg->ip4_addr;
1722 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1723 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1724 /* XXX - what if it fails? */
1725 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1726 almsg->submitted = TRUE;
1727 adns_currently_queued++;
1732 cur = adns_queue_head;
1735 almsg = (adns_queue_msg_t *) cur->data;
1736 if (almsg->submitted) {
1737 ret = adns_check(ads, &almsg->query, &ans, NULL);
1739 if (ans->status == adns_s_ok) {
1740 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1747 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1749 adns_currently_queued--;
1753 /* Keep the timeout in place */
1758 host_name_lookup_cleanup(void) {
1761 adns_queue_head = g_list_first(adns_queue_head);
1762 while (adns_queue_head) {
1763 qdata = adns_queue_head->data;
1764 adns_queue_head = g_list_remove(adns_queue_head, qdata);
1768 if (gnu_adns_initialized)
1775 host_name_lookup_process(gpointer data _U_) {
1776 /* Kill the timeout, as there's nothing for it to do */
1781 host_name_lookup_cleanup(void) {
1784 #endif /* HAVE_GNU_ADNS */
1786 extern gchar *get_hostname(guint addr)
1790 if (!(g_resolv_flags & RESOLV_NETWORK))
1791 return ip_to_str((guint8 *)&addr);
1793 return host_name_lookup(addr, &found);
1796 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1800 if (!(g_resolv_flags & RESOLV_NETWORK))
1801 return ip6_to_str(addr);
1802 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
1803 return ip6_to_str(addr);
1804 return host_name_lookup6(addr, &found);
1807 extern void add_ipv4_name(guint addr, const gchar *name)
1813 hash_idx = HASH_IPV4_ADDRESS(addr);
1815 tp = ipv4_table[hash_idx];
1818 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1821 if (tp->addr == addr) {
1822 /* address already known */
1823 if (!tp->is_dummy_entry) {
1826 /* replace this dummy entry with the new one */
1831 if (tp->next == NULL) {
1832 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1840 strncpy(tp->name, name, MAXNAMELEN);
1841 tp->name[MAXNAMELEN-1] = '\0';
1846 tp->is_dummy_entry = FALSE;
1848 } /* add_ipv4_name */
1850 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
1856 hash_idx = HASH_IPV6_ADDRESS(*addrp);
1858 tp = ipv6_table[hash_idx];
1861 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1864 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
1865 /* address already known */
1866 if (!tp->is_dummy_entry) {
1869 /* replace this dummy entry with the new one */
1874 if (tp->next == NULL) {
1875 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1883 strncpy(tp->name, name, MAXNAMELEN);
1884 tp->name[MAXNAMELEN-1] = '\0';
1889 tp->is_dummy_entry = FALSE;
1891 } /* add_ipv6_name */
1893 extern gchar *get_udp_port(guint port)
1897 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1898 cur=ep_alloc(MAXNAMELEN);
1899 g_snprintf(cur, MAXNAMELEN, "%u", port);
1903 return serv_name_lookup(port, PT_UDP);
1905 } /* get_udp_port */
1907 extern gchar *get_dccp_port(guint port)
1911 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1912 cur=ep_alloc(MAXNAMELEN);
1913 g_snprintf(cur, MAXNAMELEN, "%u", port);
1917 return serv_name_lookup(port, PT_DCCP);
1919 } /* get_dccp_port */
1922 extern gchar *get_tcp_port(guint port)
1926 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1927 cur=ep_alloc(MAXNAMELEN);
1928 g_snprintf(cur, MAXNAMELEN, "%u", port);
1932 return serv_name_lookup(port, PT_TCP);
1934 } /* get_tcp_port */
1936 extern gchar *get_sctp_port(guint port)
1940 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1941 cur=ep_alloc(MAXNAMELEN);
1942 g_snprintf(cur, MAXNAMELEN, "%u", port);
1946 return serv_name_lookup(port, PT_SCTP);
1948 } /* get_sctp_port */
1951 const gchar *get_addr_name(address *addr)
1953 const gchar *result;
1955 result = solve_address_to_name(addr);
1961 /* if it gets here, either it is of type AT_NONE, */
1962 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
1964 if (addr->type == AT_NONE){
1968 return(address_to_str(addr));
1969 } /* get_addr_name */
1972 void get_addr_name_buf(address *addr, gchar *buf, guint size)
1974 const gchar *result;
1976 result = get_addr_name(addr);
1978 strncpy(buf,result,size);
1982 } /* get_addr_name_buf */
1985 extern gchar *get_ether_name(const guint8 *addr)
1987 if (!(g_resolv_flags & RESOLV_MAC))
1988 return ether_to_str(addr);
1990 if (!eth_resolution_initialized) {
1991 initialize_ethers();
1992 eth_resolution_initialized = 1;
1995 return eth_name_lookup(addr);
1997 } /* get_ether_name */
1999 /* Look for an ether name in the hash, and return it if found.
2000 * If it's not found, simply return NULL. We DO NOT make a new
2001 * hash entry for it with the hex digits turned into a string.
2003 gchar *get_ether_name_if_known(const guint8 *addr)
2008 /* Initialize ether structs if we're the first
2009 * ether-related function called */
2010 if (!(g_resolv_flags & RESOLV_MAC))
2013 if (!eth_resolution_initialized) {
2014 initialize_ethers();
2015 eth_resolution_initialized = 1;
2018 hash_idx = HASH_ETH_ADDRESS(addr);
2020 tp = eth_table[hash_idx];
2023 /* Hash key not found in table.
2024 * Force a lookup (and a hash entry) for addr, then call
2025 * myself. I plan on not getting into an infinite loop because
2026 * eth_name_lookup() is guaranteed to make a hashtable entry,
2027 * so when I call myself again, I can never get into this
2028 * block of code again. Knock on wood...
2030 (void) eth_name_lookup(addr);
2031 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2035 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2036 if (!tp->is_dummy_entry) {
2037 /* A name was found, and its origin is an ethers file */
2041 /* A name was found, but it was created, not found in a file */
2045 if (tp->next == NULL) {
2046 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2047 (void) eth_name_lookup(addr);
2048 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2053 g_assert_not_reached();
2058 extern guint8 *get_ether_addr(const gchar *name)
2061 /* force resolution (do not check g_resolv_flags) */
2063 if (!eth_resolution_initialized) {
2064 initialize_ethers();
2065 eth_resolution_initialized = 1;
2068 return eth_addr_lookup(name);
2070 } /* get_ether_addr */
2072 extern void add_ether_byip(guint ip, const guint8 *eth)
2078 /* first check that IP address can be resolved */
2080 if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2083 /* ok, we can add this entry in the ethers hashtable */
2086 add_eth_name(eth, host);
2088 } /* add_ether_byip */
2090 extern const gchar *get_ipxnet_name(const guint32 addr)
2093 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2094 return ipxnet_to_str_punct(addr, '\0');
2097 if (!ipxnet_resolution_initialized) {
2098 initialize_ipxnets();
2099 ipxnet_resolution_initialized = 1;
2102 return ipxnet_name_lookup(addr);
2104 } /* get_ipxnet_name */
2106 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2111 /* force resolution (do not check g_resolv_flags) */
2113 if (!ipxnet_resolution_initialized) {
2114 initialize_ipxnets();
2115 ipxnet_resolution_initialized = 1;
2118 addr = ipxnet_addr_lookup(name, &success);
2123 } /* get_ipxnet_addr */
2125 extern const gchar *get_manuf_name(const guint8 *addr)
2128 hashmanuf_t *manufp;
2130 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2131 initialize_ethers();
2132 eth_resolution_initialized = 1;
2135 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2136 cur=ep_alloc(MAXMANUFLEN);
2137 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2141 return manufp->name;
2143 } /* get_manuf_name */
2146 const gchar *get_manuf_name_if_known(const guint8 *addr)
2148 hashmanuf_t *manufp;
2150 if (!eth_resolution_initialized) {
2151 initialize_ethers();
2152 eth_resolution_initialized = 1;
2155 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2159 return manufp->name;
2161 } /* get_manuf_name_if_known */
2164 /* Translate a string, assumed either to be a dotted-quad IP address or
2165 * a host name, to a numeric IP address. Return TRUE if we succeed and
2166 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2167 * Used more in the dfilter parser rather than in packet dissectors */
2168 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2170 struct in_addr ipaddr;
2174 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2175 * inet_pton(AF_INET) does not support hexadecimal notation nor
2176 * less-than-4 octet notation.
2178 if (!inet_aton(host, &ipaddr)) {
2179 /* It's not a valid dotted-quad IP address; is it a valid
2181 hp = gethostbyname(host);
2185 /* Apparently, some versions of gethostbyaddr can
2186 * return IPv6 addresses. */
2187 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2188 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2193 /* Does the string really contain dotted-quad IP?
2194 * Check against inet_atons that accept strings such as
2195 * "130.230" as valid addresses and try to convert them
2196 * to some form of a classful (host.net) notation.
2198 unsigned int a0, a1, a2, a3;
2199 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2203 *addrp = g_ntohl(ipaddr.s_addr);
2208 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2209 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2210 * return FALSE if we fail.
2212 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2216 if (inet_pton(AF_INET6, host, addrp) == 1)
2220 #ifdef HAVE_GETHOSTBYNAME2
2221 hp = gethostbyname2(host, AF_INET6);
2225 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2226 memcpy(addrp, hp->h_addr, hp->h_length);
2234 * Find out whether a hostname resolves to an ip or ipv6 address
2235 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2236 * that we don't know)
2238 const char* host_ip_af(const char *host
2239 #ifndef HAVE_GETHOSTBYNAME2
2244 #ifdef HAVE_GETHOSTBYNAME2
2246 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";