2 * Routines for network object lookup
6 * Laurent Deniel <laurent.deniel@free.fr>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
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....)
78 #ifdef HAVE_NETINET_IN_H
79 # include <netinet/in.h>
86 #ifdef HAVE_ARPA_INET_H
87 #include <arpa/inet.h>
92 #ifdef HAVE_SYS_SOCKET_H
93 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
96 #ifdef HAVE_WINSOCK2_H
97 #include <winsock2.h> /* needed to define AF_ values on Windows */
100 #ifdef AVOID_DNS_TIMEOUT
104 #ifdef NEED_INET_ATON_H
105 # include "inet_aton.h"
108 #ifdef NEED_INET_V6DEFS_H
109 # 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"
124 #include <epan/strutil.h>
125 #include <wiretap/file_util.h>
126 #include <epan/prefs.h>
127 #include <epan/emem.h>
129 #define ENAME_HOSTS "hosts"
130 #define ENAME_ETHERS "ethers"
131 #define ENAME_IPXNETS "ipxnets"
132 #define ENAME_MANUF "manuf"
133 #define ENAME_SERVICES "services"
135 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
136 #define HASHETHSIZE 1024
137 #define HASHHOSTSIZE 1024
138 #define HASHIPXNETSIZE 256
139 #define HASHMANUFSIZE 256
140 #define HASHPORTSIZE 256
142 /* hash table used for IPv4 lookup */
144 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
146 typedef struct hashipv4 {
148 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
149 struct hashipv4 *next;
150 gchar name[MAXNAMELEN];
153 /* hash table used for IPv6 lookup */
155 #define HASH_IPV6_ADDRESS(addr) \
156 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
158 typedef struct hashipv6 {
159 struct e_in6_addr addr;
160 gchar name[MAXNAMELEN];
161 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
162 struct hashipv6 *next;
165 /* hash table used for TCP/UDP/SCTP port lookup */
167 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
169 typedef struct hashport {
171 gchar name[MAXNAMELEN];
172 struct hashport *next;
175 /* hash table used for IPX network lookup */
177 /* XXX - check goodness of hash function */
179 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
181 typedef struct hashipxnet {
183 gchar name[MAXNAMELEN];
184 struct hashipxnet *next;
187 /* hash tables used for ethernet and manufacturer lookup */
189 #define HASH_ETH_ADDRESS(addr) \
190 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
193 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
195 typedef struct hashmanuf {
197 char name[MAXMANUFLEN];
198 struct hashmanuf *next;
201 typedef struct hashether {
203 char name[MAXNAMELEN];
204 gboolean is_dummy_entry; /* not a complete entry */
205 struct hashether *next;
208 /* internal ethernet type */
210 typedef struct _ether
213 char name[MAXNAMELEN];
216 /* internal ipxnet type */
218 typedef struct _ipxnet
221 char name[MAXNAMELEN];
224 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
225 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
226 static hashport_t *udp_port_table[HASHPORTSIZE];
227 static hashport_t *tcp_port_table[HASHPORTSIZE];
228 static hashport_t *sctp_port_table[HASHPORTSIZE];
229 static hashport_t *dccp_port_table[HASHPORTSIZE];
230 static hashether_t *eth_table[HASHETHSIZE];
231 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
232 static hashether_t *(*wka_table[48])[HASHETHSIZE];
233 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
235 static int eth_resolution_initialized = 0;
236 static int ipxnet_resolution_initialized = 0;
237 static int service_resolution_initialized = 0;
239 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
242 * Flag controlling what names to resolve.
244 guint32 g_resolv_flags;
247 * Global variables (can be changed in GUI sections)
248 * XXX - they could be changed in GUI code, but there's currently no
249 * GUI code to change them.
252 gchar *g_ethers_path = NULL; /* global ethers file */
253 gchar *g_pethers_path = NULL; /* personal ethers file */
254 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
255 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
256 gchar *g_services_path = NULL; /* global services file */
257 gchar *g_pservices_path = NULL; /* personal services file */
258 /* first resolving call */
264 static gboolean gnu_adns_initialized = FALSE;
268 int adns_currently_queued = 0;
270 typedef struct _adns_queue_msg
274 struct e_in6_addr ip6_addr;
279 GList *adns_queue_head = NULL;
281 #endif /* HAVE_GNU_ADNS */
285 * Miscellaneous functions
288 static int fgetline(char **buf, int *size, FILE *fp)
300 if ((*buf = g_malloc(*size)) == NULL)
308 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
309 if (len+1 >= *size) {
310 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
316 if (len == 0 && c == EOF)
327 * Local function definitions
331 static void add_service_name(hashport_t **proto_table, guint port, const char *service_name)
337 hash_idx = HASH_PORT(port);
338 tp = proto_table[hash_idx];
341 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
344 if( tp->port == port ) {
347 if (tp->next == NULL) {
348 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
356 /* fill in a new entry */
360 g_strlcpy(tp->name, service_name, MAXNAMELEN);
364 static void parse_service_line (char *line)
367 * See the services(4) or services(5) man page for services file format
368 * (not available on all systems).
376 if ((cp = strchr(line, '#')))
379 if ((cp = strtok(line, " \t")) == NULL)
384 if ((cp = strtok(NULL, " \t")) == NULL)
389 if ((cp = strtok(cp, "/")) == NULL)
392 if ((cp = strtok(NULL, "/")) == NULL)
395 /* seems we got all interesting things from the file */
396 if(strcmp(cp, "tcp") == 0) {
397 add_service_name(tcp_port_table, atoi(port), service);
401 if(strcmp(cp, "udp") == 0) {
402 add_service_name(udp_port_table, atoi(port), service);
406 if(strcmp(cp, "sctp") == 0) {
407 add_service_name(sctp_port_table, atoi(port), service);
411 if(strcmp(cp, "dcp") == 0) {
412 add_service_name(dccp_port_table, atoi(port), service);
416 } /* parse_service_line */
420 static void parse_services_file(const char * path)
424 static char *buf = NULL;
426 /* services hash table initialization */
427 serv_p = eth_fopen(path, "r");
432 while (fgetline(&buf, &size, serv_p) >= 0) {
433 parse_service_line (buf);
440 static void initialize_services(void)
443 /* the hash table won't ignore duplicates, so use the personal path first */
445 /* set personal services path */
446 if (g_pservices_path == NULL)
447 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
449 parse_services_file(g_pservices_path);
451 /* Compute the pathname of the services file. */
452 if (g_services_path == NULL) {
453 g_services_path = get_datafile_path(ENAME_SERVICES);
456 parse_services_file(g_services_path);
458 } /* initialize_services */
462 static gchar *serv_name_lookup(guint port, port_type proto)
467 const char *serv_proto = NULL;
468 struct servent *servp;
471 if (!service_resolution_initialized) {
472 initialize_services();
473 service_resolution_initialized = 1;
478 table = udp_port_table;
482 table = tcp_port_table;
486 table = sctp_port_table;
490 table = dccp_port_table;
494 /* not yet implemented */
499 hash_idx = HASH_PORT(port);
500 tp = table[hash_idx];
503 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
506 if( tp->port == port ) {
509 if (tp->next == NULL) {
510 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
518 /* fill in a new entry */
522 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
523 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
525 g_snprintf(tp->name, MAXNAMELEN, "%d", port);
527 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
532 } /* serv_name_lookup */
535 #ifdef AVOID_DNS_TIMEOUT
537 #define DNS_TIMEOUT 2 /* max sec per call */
539 jmp_buf hostname_env;
541 static void abort_network_query(int sig _U_)
543 longjmp(hostname_env, 1);
545 #endif /* AVOID_DNS_TIMEOUT */
547 static gchar *host_name_lookup(guint addr, gboolean *found)
550 hashipv4_t * volatile tp;
551 struct hostent *hostp;
553 adns_queue_msg_t *qmsg;
558 hash_idx = HASH_IPV4_ADDRESS(addr);
560 tp = ipv4_table[hash_idx];
563 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
566 if( tp->addr == addr ) {
567 if (tp->is_dummy_entry)
571 if (tp->next == NULL) {
572 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
580 /* fill in a new entry */
585 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
586 prefs.name_resolve_concurrency > 0 &&
587 gnu_adns_initialized) {
588 qmsg = g_malloc(sizeof(adns_queue_msg_t));
589 qmsg->type = AF_INET;
590 qmsg->ip4_addr = addr;
591 qmsg->submitted = FALSE;
592 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
594 tp->is_dummy_entry = TRUE;
595 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
598 #endif /* HAVE_GNU_ADNS */
601 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
602 * the name of the host on which it's running; to work around that
603 * botch, we don't try to translate an all-zero IP address to a host
606 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
607 /* Use async DNS if possible, else fall back to timeouts,
608 * else call gethostbyaddr and hope for the best
611 # ifdef AVOID_DNS_TIMEOUT
613 /* Quick hack to avoid DNS/YP timeout */
615 if (!setjmp(hostname_env)) {
616 signal(SIGALRM, abort_network_query);
618 # endif /* AVOID_DNS_TIMEOUT */
620 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
622 # ifdef AVOID_DNS_TIMEOUT
624 # endif /* AVOID_DNS_TIMEOUT */
627 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
628 tp->is_dummy_entry = FALSE;
632 # ifdef AVOID_DNS_TIMEOUT
634 # endif /* AVOID_DNS_TIMEOUT */
638 /* unknown host or DNS timeout */
640 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
641 tp->is_dummy_entry = TRUE;
646 } /* host_name_lookup */
648 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
651 hashipv6_t * volatile tp;
653 struct hostent *hostp;
658 hash_idx = HASH_IPV6_ADDRESS(*addr);
660 tp = ipv6_table[hash_idx];
663 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
666 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
667 if (tp->is_dummy_entry)
671 if (tp->next == NULL) {
672 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
680 /* fill in a new entry */
685 if (g_resolv_flags & RESOLV_NETWORK) {
686 #ifdef AVOID_DNS_TIMEOUT
688 /* Quick hack to avoid DNS/YP timeout */
690 if (!setjmp(hostname_env)) {
691 signal(SIGALRM, abort_network_query);
693 #endif /* AVOID_DNS_TIMEOUT */
694 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
695 #ifdef AVOID_DNS_TIMEOUT
697 # endif /* AVOID_DNS_TIMEOUT */
700 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
701 tp->is_dummy_entry = FALSE;
705 #ifdef AVOID_DNS_TIMEOUT
707 # endif /* AVOID_DNS_TIMEOUT */
711 /* unknown host or DNS timeout */
713 ip6_to_str_buf(addr, tp->name);
714 tp->is_dummy_entry = TRUE;
718 } /* host_name_lookup6 */
720 static const gchar *solve_address_to_name(address *addr)
723 struct e_in6_addr ipv6_addr;
725 switch (addr->type) {
728 return get_ether_name(addr->data);
731 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
732 return get_hostname(ipv4_addr);
735 memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
736 return get_hostname6(&ipv6_addr);
744 } /* solve_address_to_name */
748 * Ethernet / manufacturer resolution
750 * The following functions implement ethernet address resolution and
751 * ethers files parsing (see ethers(4)).
753 * The manuf file has the same format as ethers(4) except that names are
754 * truncated to MAXMANUFLEN-1 characters and that an address contains
755 * only 3 bytes (instead of 6).
759 * I decide to not use the existing functions (see ethers(3) on some
760 * operating systems) for the following reasons:
761 * - performance gains (use of hash tables and some other enhancements),
762 * - use of two ethers files (system-wide and per user),
763 * - avoid the use of NIS maps,
764 * - lack of these functions on some systems.
766 * So the following functions do _not_ behave as the standard ones.
773 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
774 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
778 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
786 for (i = 0; i < 6; i++) {
787 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
788 if (!isxdigit((unsigned char)*cp))
790 num = strtoul(cp, &p, 16);
792 return FALSE; /* failed */
794 return FALSE; /* not a valid octet */
795 eth->addr[i] = (guint8) num;
796 cp = p; /* skip past the number */
798 /* OK, what character terminated the octet? */
800 /* "/" - this has a mask. */
802 /* Entries with masks are allowed only in the "manuf" files. */
805 cp++; /* skip past the '/' to get to the mask */
806 if (!isdigit((unsigned char)*cp))
807 return FALSE; /* no sign allowed */
808 num = strtoul(cp, &p, 10);
810 return FALSE; /* failed */
811 cp = p; /* skip past the number */
812 if (*cp != '\0' && !isspace((unsigned char)*cp))
813 return FALSE; /* bogus terminator */
814 if (num == 0 || num >= 48)
815 return FALSE; /* bogus mask */
816 /* Mask out the bits not covered by the mask */
818 for (i = 0; num >= 8; i++, num -= 8)
819 ; /* skip octets entirely covered by the mask */
820 /* Mask out the first masked octet */
821 eth->addr[i] &= (0xFF << (8 - num));
823 /* Mask out completely-masked-out octets */
829 /* We're at the end of the address, and there's no mask. */
831 /* We got 3 bytes, so this is a manufacturer ID. */
833 /* Manufacturer IDs are only allowed in the "manuf"
837 /* Indicate that this is a manufacturer ID (0 is not allowed
844 /* We got 6 bytes, so this is a MAC address.
845 If we're reading one of the "manuf" files, indicate that
846 this is a MAC address (48 is not allowed as a mask). */
852 /* We didn't get 3 or 6 bytes, and there's no mask; this is
857 /* We don't know the separator used in this number; it can either
858 be ':', '-', or '.'. */
859 if (*cp != ':' && *cp != '-' && *cp != '.')
861 sep = *cp; /* subsequent separators must be the same */
863 /* It has to be the same as the first separator */
874 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
878 * See the ethers(4) or ethers(5) man page for ethers file format
879 * (not available on all systems).
880 * We allow both ethernet address separators (':' and '-'),
881 * as well as Wireshark's '.' separator.
886 if ((cp = strchr(line, '#')))
889 if ((cp = strtok(line, " \t")) == NULL)
892 if (!parse_ether_address(cp, eth, mask, manuf_file))
895 if ((cp = strtok(NULL, " \t")) == NULL)
898 g_strlcpy(eth->name, cp, MAXNAMELEN);
902 } /* parse_ether_line */
904 static FILE *eth_p = NULL;
906 static void set_ethent(char *path)
911 eth_p = eth_fopen(path, "r");
914 static void end_ethent(void)
922 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
927 static char *buf = NULL;
932 while (fgetline(&buf, &size, eth_p) >= 0) {
933 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
942 static ether_t *get_ethbyname(const gchar *name)
946 set_ethent(g_pethers_path);
948 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
954 set_ethent(g_ethers_path);
956 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
964 } /* get_ethbyname */
966 static ether_t *get_ethbyaddr(const guint8 *addr)
971 set_ethent(g_pethers_path);
973 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
979 set_ethent(g_ethers_path);
981 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
989 } /* get_ethbyaddr */
991 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
994 /* All but the topmost byte is masked out */
995 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
999 /* All but the topmost 2 bytes are masked out */
1000 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1005 /* All but the topmost 3 bytes are masked out */
1006 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1007 & (HASHETHSIZE - 1);
1011 /* All but the topmost 4 bytes are masked out */
1012 return ((((addr[0] << 8) | addr[1]) ^
1013 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1018 /* All but the topmost 5 bytes are masked out */
1019 return ((((addr[1] << 8) | addr[2]) ^
1020 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1024 /* No bytes are fully masked out */
1025 return ((((addr[1] << 8) | addr[2]) ^
1026 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1030 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
1034 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
1037 /* This is a well-known MAC address; just add this to the Ethernet
1039 add_eth_name(addr, name);
1044 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1046 hash_idx = HASH_ETH_MANUF(addr);
1048 tp = manuf_table[hash_idx];
1051 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1054 if (tp->next == NULL) {
1055 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1063 memcpy(tp->addr, addr, sizeof(tp->addr));
1064 g_strlcpy(tp->name, name, MAXMANUFLEN);
1069 /* This is a range of well-known addresses; add it to the appropriate
1070 well-known-address table, creating that table if necessary. */
1071 wka_tp = wka_table[mask];
1073 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1075 hash_idx = hash_eth_wka(addr, mask);
1077 etp = (*wka_tp)[hash_idx];
1080 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1083 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
1084 /* address already known */
1087 if (etp->next == NULL) {
1088 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1096 memcpy(etp->addr, addr, sizeof(etp->addr));
1097 g_strlcpy(etp->name, name, MAXNAMELEN);
1099 etp->is_dummy_entry = FALSE;
1101 } /* add_manuf_name */
1103 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
1107 guint8 stripped_addr[3];
1109 hash_idx = HASH_ETH_MANUF(addr);
1111 /* first try to find a "perfect match" */
1112 tp = manuf_table[hash_idx];
1114 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1120 /* Mask out the broadcast/multicast flag but not the locally
1121 * administered flag as localy administered means: not assigend
1122 * by the IEEE but the local administrator instead.
1123 * 0x01 multicast / broadcast bit
1124 * 0x02 locally administered bit */
1125 memcpy(stripped_addr, addr, 3);
1126 stripped_addr[0] &= 0xFE;
1128 tp = manuf_table[hash_idx];
1130 if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
1138 } /* manuf_name_lookup */
1140 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
1143 hashether_t *(*wka_tp)[HASHETHSIZE];
1145 guint8 masked_addr[6];
1149 wka_tp = wka_table[mask];
1150 if (wka_tp == NULL) {
1151 /* There are no entries in the table for that mask value, as there is
1152 no table for that mask value. */
1156 /* Get the part of the address covered by the mask. */
1157 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1158 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1159 /* Mask out the first masked octet */
1160 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1162 /* Zero out completely-masked-out octets */
1166 hash_idx = hash_eth_wka(masked_addr, mask);
1168 tp = (*wka_tp)[hash_idx];
1171 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1179 } /* wka_name_lookup */
1181 static void initialize_ethers(void)
1187 /* Compute the pathname of the ethers file. */
1188 if (g_ethers_path == NULL) {
1189 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1190 get_systemfile_dir(), ENAME_ETHERS);
1193 /* Set g_pethers_path here, but don't actually do anything
1194 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1196 if (g_pethers_path == NULL)
1197 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1199 /* manuf hash table initialization */
1201 /* Compute the pathname of the manuf file */
1202 manuf_path = get_datafile_path(ENAME_MANUF);
1204 /* Read it and initialize the hash table */
1205 set_ethent(manuf_path);
1207 while ((eth = get_ethent(&mask, TRUE))) {
1208 add_manuf_name(eth->addr, mask, eth->name);
1215 } /* initialize_ethers */
1217 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1223 hash_idx = HASH_ETH_ADDRESS(addr);
1225 tp = eth_table[hash_idx];
1228 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1231 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1232 /* address already known */
1233 if (!tp->is_dummy_entry) {
1236 /* replace this dummy (manuf) entry with a real name */
1241 if (tp->next == NULL) {
1242 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1250 g_strlcpy(tp->name, name, MAXNAMELEN);
1252 memcpy(tp->addr, addr, sizeof(tp->addr));
1255 tp->is_dummy_entry = FALSE;
1259 } /* add_eth_name */
1261 static gchar *eth_name_lookup(const guint8 *addr)
1264 hashmanuf_t *manufp;
1270 hash_idx = HASH_ETH_ADDRESS(addr);
1272 tp = eth_table[hash_idx];
1275 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1278 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1281 if (tp->next == NULL) {
1282 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1290 /* fill in a new entry */
1292 memcpy(tp->addr, addr, sizeof(tp->addr));
1295 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1296 /* Unknown name. Try looking for it in the well-known-address
1297 tables for well-known address ranges smaller than 2^24. */
1300 /* Only the topmost 5 bytes participate fully */
1301 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1302 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1303 etp->name, addr[5] & (0xFF >> mask));
1304 tp->is_dummy_entry = TRUE;
1314 /* Only the topmost 4 bytes participate fully */
1315 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1316 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1317 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1318 tp->is_dummy_entry = TRUE;
1328 /* Only the topmost 3 bytes participate fully */
1329 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1330 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1331 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1332 tp->is_dummy_entry = TRUE;
1340 /* Now try looking in the manufacturer table. */
1341 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1342 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1343 manufp->name, addr[3], addr[4], addr[5]);
1344 tp->is_dummy_entry = TRUE;
1348 /* Now try looking for it in the well-known-address
1349 tables for well-known address ranges larger than 2^24. */
1352 /* Only the topmost 2 bytes participate fully */
1353 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1354 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1355 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1357 tp->is_dummy_entry = TRUE;
1367 /* Only the topmost byte participates fully */
1368 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1369 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1370 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1372 tp->is_dummy_entry = TRUE;
1380 for (mask = 7; mask > 0; mask--) {
1381 /* Not even the topmost byte participates fully */
1382 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1383 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1384 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1385 addr[3], addr[4], addr[5]);
1386 tp->is_dummy_entry = TRUE;
1391 /* No match whatsoever. */
1392 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1393 tp->is_dummy_entry = TRUE;
1396 g_strlcpy(tp->name, eth->name, MAXNAMELEN);
1397 tp->is_dummy_entry = FALSE;
1402 } /* eth_name_lookup */
1404 static guint8 *eth_addr_lookup(const gchar *name)
1408 hashether_t **table = eth_table;
1411 /* to be optimized (hash table from name to addr) */
1412 for (i = 0; i < HASHETHSIZE; i++) {
1415 if (strcmp(tp->name, name) == 0)
1421 /* not in hash table : performs a file lookup */
1423 if ((eth = get_ethbyname(name)) == NULL)
1426 /* add new entry in hash table */
1428 tp = add_eth_name(eth->addr, name);
1432 } /* eth_addr_lookup */
1436 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1439 * We allow three address separators (':', '-', and '.'),
1440 * as well as no separators
1444 guint32 a, a0, a1, a2, a3;
1445 gboolean found_single_number = FALSE;
1447 if ((cp = strchr(line, '#')))
1450 if ((cp = strtok(line, " \t\n")) == NULL)
1453 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1454 * fill a and found_single_number is TRUE,
1457 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1458 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1459 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1460 if (sscanf(cp, "%x", &a) == 1) {
1461 found_single_number = TRUE;
1470 if ((cp = strtok(NULL, " \t\n")) == NULL)
1473 if (found_single_number) {
1477 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1480 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1484 } /* parse_ipxnets_line */
1486 static FILE *ipxnet_p = NULL;
1488 static void set_ipxnetent(char *path)
1493 ipxnet_p = eth_fopen(path, "r");
1496 static void end_ipxnetent(void)
1504 static ipxnet_t *get_ipxnetent(void)
1507 static ipxnet_t ipxnet;
1508 static int size = 0;
1509 static char *buf = NULL;
1511 if (ipxnet_p == NULL)
1514 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1515 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1522 } /* get_ipxnetent */
1524 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1528 set_ipxnetent(g_ipxnets_path);
1530 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1533 if (ipxnet == NULL) {
1536 set_ipxnetent(g_pipxnets_path);
1538 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1546 } /* get_ipxnetbyname */
1548 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1553 set_ipxnetent(g_ipxnets_path);
1555 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1557 if (ipxnet == NULL) {
1560 set_ipxnetent(g_pipxnets_path);
1562 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1570 } /* get_ipxnetbyaddr */
1572 static void initialize_ipxnets(void)
1574 /* Compute the pathname of the ipxnets file.
1576 * XXX - is there a notion of an "ipxnets file" in any flavor of
1577 * UNIX, or with any add-on Netware package for UNIX? If not,
1578 * should the UNIX version of the ipxnets file be in the datafile
1579 * directory as well?
1581 if (g_ipxnets_path == NULL) {
1582 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1583 get_systemfile_dir(), ENAME_IPXNETS);
1586 /* Set g_pipxnets_path here, but don't actually do anything
1587 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1589 if (g_pipxnets_path == NULL)
1590 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1592 } /* initialize_ipxnets */
1594 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1599 hash_idx = HASH_IPX_NET(addr);
1601 tp = ipxnet_table[hash_idx];
1604 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1607 if (tp->next == NULL) {
1608 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1617 g_strlcpy(tp->name, name, MAXNAMELEN);
1622 } /* add_ipxnet_name */
1624 static gchar *ipxnet_name_lookup(const guint addr)
1630 hash_idx = HASH_IPX_NET(addr);
1632 tp = ipxnet_table[hash_idx];
1635 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1638 if (tp->addr == addr) {
1641 if (tp->next == NULL) {
1642 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1650 /* fill in a new entry */
1655 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1657 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1660 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1665 } /* ipxnet_name_lookup */
1667 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1671 hashipxnet_t **table = ipxnet_table;
1674 /* to be optimized (hash table from name to addr) */
1675 for (i = 0; i < HASHIPXNETSIZE; i++) {
1678 if (strcmp(tp->name, name) == 0) {
1686 /* not in hash table : performs a file lookup */
1688 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1693 /* add new entry in hash table */
1695 tp = add_ipxnet_name(ipxnet->addr, name);
1700 } /* ipxnet_addr_lookup */
1703 read_hosts_file (const char *hostspath)
1709 guint32 host_addr[4]; /* IPv4 or IPv6 */
1710 struct e_in6_addr ipv6_addr;
1715 * See the hosts(4) or hosts(5) man page for hosts file format
1716 * (not available on all systems).
1718 if ((hf = eth_fopen(hostspath, "r")) == NULL)
1721 while (fgetline(&line, &size, hf) >= 0) {
1722 if ((cp = strchr(line, '#')))
1725 if ((cp = strtok(line, " \t")) == NULL)
1726 continue; /* no tokens in the line */
1728 ret = inet_pton(AF_INET6, cp, &host_addr);
1730 continue; /* error parsing */
1735 /* Not valid IPv6 - valid IPv4? */
1736 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1741 if ((cp = strtok(NULL, " \t")) == NULL)
1742 continue; /* no host name */
1745 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1746 add_ipv6_name(&ipv6_addr, cp);
1748 add_ipv4_name(host_addr[0], cp);
1751 * Add the aliases, too, if there are any.
1753 while ((cp = strtok(NULL, " \t")) != NULL) {
1755 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1756 add_ipv6_name(&ipv6_addr, cp);
1758 add_ipv4_name(host_addr[0], cp);
1766 } /* read_hosts_file */
1769 * External Functions
1773 host_name_lookup_init(void) {
1776 #ifdef HAVE_GNU_ADNS
1779 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
1780 static char rootpath_ot[] = "\\hosts";
1782 #endif /*GNU_ADNS */
1785 * Load the user's hosts file, if they have one.
1787 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
1788 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1789 report_open_failure(hostspath, errno, FALSE);
1794 * Load the global hosts file, if we have one.
1796 hostspath = get_datafile_path(ENAME_HOSTS);
1797 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1798 report_open_failure(hostspath, errno, FALSE);
1802 #ifdef HAVE_GNU_ADNS
1804 * We're using GNU ADNS, which doesn't check the system hosts file;
1805 * we load that file ourselves.
1809 sysroot = getenv_utf8("WINDIR");
1810 if (sysroot != NULL) {
1812 * The file should be under WINDIR.
1813 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
1814 * %WINDIR%\system32\drivers\etc\hosts.
1815 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
1817 * XXX - should we base it on the dwPlatformId value from
1820 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
1821 if (!read_hosts_file(hostspath)) {
1823 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
1824 read_hosts_file(hostspath);
1829 read_hosts_file("/etc/hosts");
1832 /* XXX - Any flags we should be using? */
1833 /* XXX - We could provide config settings for DNS servers, and
1834 pass them to ADNS with adns_init_strcfg */
1835 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1837 * XXX - should we report the error? I'm assuming that some crashes
1838 * reported on a Windows machine with TCP/IP not configured are due
1839 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1840 * ADNS in a state where it crashes due to that. We'll still try
1841 * doing name resolution anyway.
1845 gnu_adns_initialized = TRUE;
1846 adns_currently_queued = 0;
1847 #endif /* HAVE_GNU_ADNS */
1850 #ifdef HAVE_GNU_ADNS
1852 /* XXX - The ADNS "documentation" isn't very clear:
1853 * - Do we need to keep our query structures around?
1856 host_name_lookup_process(gpointer data _U_) {
1857 adns_queue_msg_t *almsg;
1859 char addr_str[] = "111.222.333.444.in-addr.arpa.";
1865 adns_queue_head = g_list_first(adns_queue_head);
1867 cur = adns_queue_head;
1868 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1869 almsg = (adns_queue_msg_t *) cur->data;
1870 if (! almsg->submitted && almsg->type == AF_INET) {
1871 addr_bytes = (guint8 *) &almsg->ip4_addr;
1872 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1873 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1874 /* XXX - what if it fails? */
1875 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1876 almsg->submitted = TRUE;
1877 adns_currently_queued++;
1882 cur = adns_queue_head;
1885 almsg = (adns_queue_msg_t *) cur->data;
1886 if (almsg->submitted) {
1887 ret = adns_check(ads, &almsg->query, &ans, NULL);
1889 if (ans->status == adns_s_ok) {
1890 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1897 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1899 adns_currently_queued--;
1903 /* Keep the timeout in place */
1908 host_name_lookup_cleanup(void) {
1911 adns_queue_head = g_list_first(adns_queue_head);
1912 while (adns_queue_head) {
1913 qdata = adns_queue_head->data;
1914 adns_queue_head = g_list_remove(adns_queue_head, qdata);
1918 if (gnu_adns_initialized)
1925 host_name_lookup_process(gpointer data _U_) {
1926 /* Kill the timeout, as there's nothing for it to do */
1931 host_name_lookup_cleanup(void) {
1934 #endif /* HAVE_GNU_ADNS */
1936 extern gchar *get_hostname(guint addr)
1940 if (!(g_resolv_flags & RESOLV_NETWORK))
1941 return ip_to_str((guint8 *)&addr);
1943 return host_name_lookup(addr, &found);
1946 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1950 if (!(g_resolv_flags & RESOLV_NETWORK))
1951 return ip6_to_str(addr);
1952 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
1953 return ip6_to_str(addr);
1954 return host_name_lookup6(addr, &found);
1957 extern void add_ipv4_name(guint addr, const gchar *name)
1963 hash_idx = HASH_IPV4_ADDRESS(addr);
1965 tp = ipv4_table[hash_idx];
1968 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1971 if (tp->addr == addr) {
1972 /* address already known */
1973 if (!tp->is_dummy_entry) {
1976 /* replace this dummy entry with the new one */
1981 if (tp->next == NULL) {
1982 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1990 g_strlcpy(tp->name, name, MAXNAMELEN);
1995 tp->is_dummy_entry = FALSE;
1997 } /* add_ipv4_name */
1999 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
2005 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2007 tp = ipv6_table[hash_idx];
2010 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2013 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2014 /* address already known */
2015 if (!tp->is_dummy_entry) {
2018 /* replace this dummy entry with the new one */
2023 if (tp->next == NULL) {
2024 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2032 g_strlcpy(tp->name, name, MAXNAMELEN);
2037 tp->is_dummy_entry = FALSE;
2039 } /* add_ipv6_name */
2041 /* -----------------
2042 * unsigned integer to ascii
2044 static gchar *ep_utoa(guint port)
2046 gchar *bp = ep_alloc(MAXNAMELEN);
2048 bp = &bp[MAXNAMELEN -1];
2052 *--bp = (port % 10) +'0';
2053 } while ((port /= 10) != 0);
2058 extern gchar *get_udp_port(guint port)
2061 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2062 return ep_utoa(port);
2065 return serv_name_lookup(port, PT_UDP);
2067 } /* get_udp_port */
2069 extern gchar *get_dccp_port(guint port)
2072 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2073 return ep_utoa(port);
2076 return serv_name_lookup(port, PT_DCCP);
2078 } /* get_dccp_port */
2081 extern gchar *get_tcp_port(guint port)
2084 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2085 return ep_utoa(port);
2088 return serv_name_lookup(port, PT_TCP);
2090 } /* get_tcp_port */
2092 extern gchar *get_sctp_port(guint port)
2095 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2096 return ep_utoa(port);
2099 return serv_name_lookup(port, PT_SCTP);
2101 } /* get_sctp_port */
2104 const gchar *get_addr_name(address *addr)
2106 const gchar *result;
2108 result = solve_address_to_name(addr);
2114 /* if it gets here, either it is of type AT_NONE, */
2115 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
2117 if (addr->type == AT_NONE){
2121 return(address_to_str(addr));
2122 } /* get_addr_name */
2125 void get_addr_name_buf(address *addr, gchar *buf, guint size)
2127 const gchar *result = get_addr_name(addr);
2129 g_snprintf(buf, size, "%s", result);
2130 } /* get_addr_name_buf */
2133 extern gchar *get_ether_name(const guint8 *addr)
2135 if (!(g_resolv_flags & RESOLV_MAC))
2136 return ether_to_str(addr);
2138 if (!eth_resolution_initialized) {
2139 initialize_ethers();
2140 eth_resolution_initialized = 1;
2143 return eth_name_lookup(addr);
2145 } /* get_ether_name */
2147 /* Look for an ether name in the hash, and return it if found.
2148 * If it's not found, simply return NULL. We DO NOT make a new
2149 * hash entry for it with the hex digits turned into a string.
2151 gchar *get_ether_name_if_known(const guint8 *addr)
2156 /* Initialize ether structs if we're the first
2157 * ether-related function called */
2158 if (!(g_resolv_flags & RESOLV_MAC))
2161 if (!eth_resolution_initialized) {
2162 initialize_ethers();
2163 eth_resolution_initialized = 1;
2166 hash_idx = HASH_ETH_ADDRESS(addr);
2168 tp = eth_table[hash_idx];
2171 /* Hash key not found in table.
2172 * Force a lookup (and a hash entry) for addr, then call
2173 * myself. I plan on not getting into an infinite loop because
2174 * eth_name_lookup() is guaranteed to make a hashtable entry,
2175 * so when I call myself again, I can never get into this
2176 * block of code again. Knock on wood...
2178 (void) eth_name_lookup(addr);
2179 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2183 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2184 if (!tp->is_dummy_entry) {
2185 /* A name was found, and its origin is an ethers file */
2189 /* A name was found, but it was created, not found in a file */
2193 if (tp->next == NULL) {
2194 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2195 (void) eth_name_lookup(addr);
2196 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2201 g_assert_not_reached();
2206 extern guint8 *get_ether_addr(const gchar *name)
2209 /* force resolution (do not check g_resolv_flags) */
2211 if (!eth_resolution_initialized) {
2212 initialize_ethers();
2213 eth_resolution_initialized = 1;
2216 return eth_addr_lookup(name);
2218 } /* get_ether_addr */
2220 extern void add_ether_byip(guint ip, const guint8 *eth)
2226 /* first check that IP address can be resolved */
2228 if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2231 /* ok, we can add this entry in the ethers hashtable */
2234 add_eth_name(eth, host);
2236 } /* add_ether_byip */
2238 extern const gchar *get_ipxnet_name(const guint32 addr)
2241 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2242 return ipxnet_to_str_punct(addr, '\0');
2245 if (!ipxnet_resolution_initialized) {
2246 initialize_ipxnets();
2247 ipxnet_resolution_initialized = 1;
2250 return ipxnet_name_lookup(addr);
2252 } /* get_ipxnet_name */
2254 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2259 /* force resolution (do not check g_resolv_flags) */
2261 if (!ipxnet_resolution_initialized) {
2262 initialize_ipxnets();
2263 ipxnet_resolution_initialized = 1;
2266 addr = ipxnet_addr_lookup(name, &success);
2271 } /* get_ipxnet_addr */
2273 extern const gchar *get_manuf_name(const guint8 *addr)
2276 hashmanuf_t *manufp;
2278 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2279 initialize_ethers();
2280 eth_resolution_initialized = 1;
2283 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2284 cur=ep_alloc(MAXMANUFLEN);
2285 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2289 return manufp->name;
2291 } /* get_manuf_name */
2294 const gchar *get_manuf_name_if_known(const guint8 *addr)
2296 hashmanuf_t *manufp;
2298 if (!eth_resolution_initialized) {
2299 initialize_ethers();
2300 eth_resolution_initialized = 1;
2303 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2307 return manufp->name;
2309 } /* get_manuf_name_if_known */
2312 /* Translate a string, assumed either to be a dotted-quad IP address or
2313 * a host name, to a numeric IP address. Return TRUE if we succeed and
2314 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2315 * Used more in the dfilter parser rather than in packet dissectors */
2316 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2318 struct in_addr ipaddr;
2322 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2323 * inet_pton(AF_INET) does not support hexadecimal notation nor
2324 * less-than-4 octet notation.
2326 if (!inet_aton(host, &ipaddr)) {
2327 /* It's not a valid dotted-quad IP address; is it a valid
2329 hp = gethostbyname(host);
2333 /* Apparently, some versions of gethostbyaddr can
2334 * return IPv6 addresses. */
2335 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2336 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2341 /* Does the string really contain dotted-quad IP?
2342 * Check against inet_atons that accept strings such as
2343 * "130.230" as valid addresses and try to convert them
2344 * to some form of a classful (host.net) notation.
2346 unsigned int a0, a1, a2, a3;
2347 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2351 *addrp = g_ntohl(ipaddr.s_addr);
2356 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2357 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2358 * return FALSE if we fail.
2360 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2364 if (inet_pton(AF_INET6, host, addrp) == 1)
2368 #ifdef HAVE_GETHOSTBYNAME2
2369 hp = gethostbyname2(host, AF_INET6);
2373 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2374 memcpy(addrp, hp->h_addr, hp->h_length);
2382 * Find out whether a hostname resolves to an ip or ipv6 address
2383 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2384 * that we don't know)
2386 const char* host_ip_af(const char *host
2387 #ifndef HAVE_GETHOSTBYNAME2
2392 #ifdef HAVE_GETHOSTBYNAME2
2394 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";