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_SUBNETS "subnets"
131 #define ENAME_ETHERS "ethers"
132 #define ENAME_IPXNETS "ipxnets"
133 #define ENAME_MANUF "manuf"
134 #define ENAME_SERVICES "services"
136 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
137 #define HASHETHSIZE 1024
138 #define HASHHOSTSIZE 1024
139 #define HASHIPXNETSIZE 256
140 #define HASHMANUFSIZE 256
141 #define HASHPORTSIZE 256
142 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
144 /* hash table used for IPv4 lookup */
146 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
148 typedef struct hashipv4 {
150 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
151 struct hashipv4 *next;
152 gchar name[MAXNAMELEN];
155 /* hash table used for IPv6 lookup */
157 #define HASH_IPV6_ADDRESS(addr) \
158 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
160 typedef struct hashipv6 {
161 struct e_in6_addr addr;
162 gchar name[MAXNAMELEN];
163 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
164 struct hashipv6 *next;
167 /* Array of entries of subnets of different lengths */
169 gsize mask_length; /*1-32*/
170 guint32 mask; /* e.g. 255.255.255.*/
171 hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
172 } subnet_length_entry_t;
174 /* hash table used for TCP/UDP/SCTP port lookup */
176 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
178 typedef struct hashport {
180 gchar name[MAXNAMELEN];
181 struct hashport *next;
184 /* hash table used for IPX network lookup */
186 /* XXX - check goodness of hash function */
188 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
190 typedef struct hashipxnet {
192 gchar name[MAXNAMELEN];
193 struct hashipxnet *next;
196 /* hash tables used for ethernet and manufacturer lookup */
198 #define HASH_ETH_ADDRESS(addr) \
199 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
202 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
204 typedef struct hashmanuf {
206 char name[MAXMANUFLEN];
207 struct hashmanuf *next;
210 typedef struct hashether {
212 char name[MAXNAMELEN];
213 gboolean is_dummy_entry; /* not a complete entry */
214 struct hashether *next;
217 /* internal ethernet type */
219 typedef struct _ether
222 char name[MAXNAMELEN];
225 /* internal ipxnet type */
227 typedef struct _ipxnet
230 char name[MAXNAMELEN];
233 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
234 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
235 static hashport_t *udp_port_table[HASHPORTSIZE];
236 static hashport_t *tcp_port_table[HASHPORTSIZE];
237 static hashport_t *sctp_port_table[HASHPORTSIZE];
238 static hashport_t *dccp_port_table[HASHPORTSIZE];
239 static hashether_t *eth_table[HASHETHSIZE];
240 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
241 static hashether_t *(*wka_table[48])[HASHETHSIZE];
242 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
244 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
245 static gboolean have_subnet_entry = FALSE;
247 static int eth_resolution_initialized = 0;
248 static int ipxnet_resolution_initialized = 0;
249 static int service_resolution_initialized = 0;
251 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
254 * Flag controlling what names to resolve.
256 guint32 g_resolv_flags;
259 * Global variables (can be changed in GUI sections)
260 * XXX - they could be changed in GUI code, but there's currently no
261 * GUI code to change them.
264 gchar *g_ethers_path = NULL; /* global ethers file */
265 gchar *g_pethers_path = NULL; /* personal ethers file */
266 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
267 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
268 gchar *g_services_path = NULL; /* global services file */
269 gchar *g_pservices_path = NULL; /* personal services file */
270 /* first resolving call */
276 static gboolean gnu_adns_initialized = FALSE;
280 int adns_currently_queued = 0;
282 typedef struct _adns_queue_msg
286 struct e_in6_addr ip6_addr;
291 GList *adns_queue_head = NULL;
293 #endif /* HAVE_GNU_ADNS */
298 const gchar* name; /* Shallow copy */
302 * Miscellaneous functions
305 static int fgetline(char **buf, int *size, FILE *fp)
317 if ((*buf = g_malloc(*size)) == NULL)
325 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
326 if (len+1 >= *size) {
327 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
333 if (len == 0 && c == EOF)
344 * Local function definitions
346 static subnet_entry_t subnet_lookup(const guint32 addr);
347 static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name);
350 static void add_service_name(hashport_t **proto_table, guint port, const char *service_name)
356 hash_idx = HASH_PORT(port);
357 tp = proto_table[hash_idx];
360 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
363 if( tp->port == port ) {
366 if (tp->next == NULL) {
367 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
375 /* fill in a new entry */
379 g_strlcpy(tp->name, service_name, MAXNAMELEN);
383 static void parse_service_line (char *line)
386 * See the services(4) or services(5) man page for services file format
387 * (not available on all systems).
395 if ((cp = strchr(line, '#')))
398 if ((cp = strtok(line, " \t")) == NULL)
403 if ((cp = strtok(NULL, " \t")) == NULL)
408 if ((cp = strtok(cp, "/")) == NULL)
411 if ((cp = strtok(NULL, "/")) == NULL)
414 /* seems we got all interesting things from the file */
415 if(strcmp(cp, "tcp") == 0) {
416 add_service_name(tcp_port_table, atoi(port), service);
420 if(strcmp(cp, "udp") == 0) {
421 add_service_name(udp_port_table, atoi(port), service);
425 if(strcmp(cp, "sctp") == 0) {
426 add_service_name(sctp_port_table, atoi(port), service);
430 if(strcmp(cp, "dcp") == 0) {
431 add_service_name(dccp_port_table, atoi(port), service);
435 } /* parse_service_line */
439 static void parse_services_file(const char * path)
443 static char *buf = NULL;
445 /* services hash table initialization */
446 serv_p = eth_fopen(path, "r");
451 while (fgetline(&buf, &size, serv_p) >= 0) {
452 parse_service_line (buf);
459 static void initialize_services(void)
462 /* the hash table won't ignore duplicates, so use the personal path first */
464 /* set personal services path */
465 if (g_pservices_path == NULL)
466 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
468 parse_services_file(g_pservices_path);
470 /* Compute the pathname of the services file. */
471 if (g_services_path == NULL) {
472 g_services_path = get_datafile_path(ENAME_SERVICES);
475 parse_services_file(g_services_path);
477 } /* initialize_services */
481 static gchar *serv_name_lookup(guint port, port_type proto)
486 const char *serv_proto = NULL;
487 struct servent *servp;
490 if (!service_resolution_initialized) {
491 initialize_services();
492 service_resolution_initialized = 1;
497 table = udp_port_table;
501 table = tcp_port_table;
505 table = sctp_port_table;
509 table = dccp_port_table;
513 /* not yet implemented */
518 hash_idx = HASH_PORT(port);
519 tp = table[hash_idx];
522 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
525 if( tp->port == port ) {
528 if (tp->next == NULL) {
529 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
537 /* fill in a new entry */
541 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
542 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
544 g_snprintf(tp->name, MAXNAMELEN, "%d", port);
546 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
551 } /* serv_name_lookup */
554 #ifdef AVOID_DNS_TIMEOUT
556 #define DNS_TIMEOUT 2 /* max sec per call */
558 jmp_buf hostname_env;
560 static void abort_network_query(int sig _U_)
562 longjmp(hostname_env, 1);
564 #endif /* AVOID_DNS_TIMEOUT */
566 /* Fill in an IP4 structure with info from subnets file or just with
567 * string form of address.
569 static void fill_dummy_ip4(guint addr, hashipv4_t* volatile tp)
571 subnet_entry_t subnet_entry;
572 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
574 /* Do we have a subnet for this address? */
575 subnet_entry = subnet_lookup(addr);
576 if(0 != subnet_entry.mask) {
577 /* Print name, then '.' then IP address after subnet mask */
579 gchar buffer[MAX_IP_STR_LEN];
583 host_addr = addr & (~(guint32)subnet_entry.mask);
584 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
587 /* Skip to first octet that is not totally masked
588 * If length of mask is 32, we chomp the whole address.
589 * If the address string starts '.' (should not happen?),
592 i = subnet_entry.mask_length / 8;
593 while(*(paddr) != '\0' && i > 0) {
594 if(*(++paddr) == '.') {
599 /* There are more efficient ways to do this, but this is safe if we
600 * trust g_snprintf and MAXNAMELEN
602 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
604 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
608 static gchar *host_name_lookup(guint addr, gboolean *found)
611 hashipv4_t * volatile tp;
612 struct hostent *hostp;
614 adns_queue_msg_t *qmsg;
619 hash_idx = HASH_IPV4_ADDRESS(addr);
621 tp = ipv4_table[hash_idx];
624 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
627 if( tp->addr == addr ) {
628 if (tp->is_dummy_entry)
632 if (tp->next == NULL) {
633 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
641 /* fill in a new entry */
646 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
647 prefs.name_resolve_concurrency > 0 &&
648 gnu_adns_initialized) {
649 qmsg = g_malloc(sizeof(adns_queue_msg_t));
650 qmsg->type = AF_INET;
651 qmsg->ip4_addr = addr;
652 qmsg->submitted = FALSE;
653 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
655 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
656 * going to risk changing the semantics.
658 fill_dummy_ip4(addr, tp);
661 #endif /* HAVE_GNU_ADNS */
664 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
665 * the name of the host on which it's running; to work around that
666 * botch, we don't try to translate an all-zero IP address to a host
669 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
670 /* Use async DNS if possible, else fall back to timeouts,
671 * else call gethostbyaddr and hope for the best
674 # ifdef AVOID_DNS_TIMEOUT
676 /* Quick hack to avoid DNS/YP timeout */
678 if (!setjmp(hostname_env)) {
679 signal(SIGALRM, abort_network_query);
681 # endif /* AVOID_DNS_TIMEOUT */
683 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
685 # ifdef AVOID_DNS_TIMEOUT
687 # endif /* AVOID_DNS_TIMEOUT */
690 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
691 tp->is_dummy_entry = FALSE;
695 # ifdef AVOID_DNS_TIMEOUT
697 # endif /* AVOID_DNS_TIMEOUT */
701 /* unknown host or DNS timeout */
704 fill_dummy_ip4(addr, tp);
707 } /* host_name_lookup */
709 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
712 hashipv6_t * volatile tp;
714 struct hostent *hostp;
719 hash_idx = HASH_IPV6_ADDRESS(*addr);
721 tp = ipv6_table[hash_idx];
724 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
727 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
728 if (tp->is_dummy_entry)
732 if (tp->next == NULL) {
733 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
741 /* fill in a new entry */
746 if (g_resolv_flags & RESOLV_NETWORK) {
747 #ifdef AVOID_DNS_TIMEOUT
749 /* Quick hack to avoid DNS/YP timeout */
751 if (!setjmp(hostname_env)) {
752 signal(SIGALRM, abort_network_query);
754 #endif /* AVOID_DNS_TIMEOUT */
755 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
756 #ifdef AVOID_DNS_TIMEOUT
758 # endif /* AVOID_DNS_TIMEOUT */
761 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
762 tp->is_dummy_entry = FALSE;
766 #ifdef AVOID_DNS_TIMEOUT
768 # endif /* AVOID_DNS_TIMEOUT */
772 /* unknown host or DNS timeout */
774 ip6_to_str_buf(addr, tp->name);
775 tp->is_dummy_entry = TRUE;
779 } /* host_name_lookup6 */
781 static const gchar *solve_address_to_name(address *addr)
784 struct e_in6_addr ipv6_addr;
786 switch (addr->type) {
789 return get_ether_name(addr->data);
792 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
793 return get_hostname(ipv4_addr);
796 memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
797 return get_hostname6(&ipv6_addr);
805 } /* solve_address_to_name */
809 * Ethernet / manufacturer resolution
811 * The following functions implement ethernet address resolution and
812 * ethers files parsing (see ethers(4)).
814 * The manuf file has the same format as ethers(4) except that names are
815 * truncated to MAXMANUFLEN-1 characters and that an address contains
816 * only 3 bytes (instead of 6).
820 * I decide to not use the existing functions (see ethers(3) on some
821 * operating systems) for the following reasons:
822 * - performance gains (use of hash tables and some other enhancements),
823 * - use of two ethers files (system-wide and per user),
824 * - avoid the use of NIS maps,
825 * - lack of these functions on some systems.
827 * So the following functions do _not_ behave as the standard ones.
834 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
835 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
839 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
847 for (i = 0; i < 6; i++) {
848 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
849 if (!isxdigit((unsigned char)*cp))
851 num = strtoul(cp, &p, 16);
853 return FALSE; /* failed */
855 return FALSE; /* not a valid octet */
856 eth->addr[i] = (guint8) num;
857 cp = p; /* skip past the number */
859 /* OK, what character terminated the octet? */
861 /* "/" - this has a mask. */
863 /* Entries with masks are allowed only in the "manuf" files. */
866 cp++; /* skip past the '/' to get to the mask */
867 if (!isdigit((unsigned char)*cp))
868 return FALSE; /* no sign allowed */
869 num = strtoul(cp, &p, 10);
871 return FALSE; /* failed */
872 cp = p; /* skip past the number */
873 if (*cp != '\0' && !isspace((unsigned char)*cp))
874 return FALSE; /* bogus terminator */
875 if (num == 0 || num >= 48)
876 return FALSE; /* bogus mask */
877 /* Mask out the bits not covered by the mask */
879 for (i = 0; num >= 8; i++, num -= 8)
880 ; /* skip octets entirely covered by the mask */
881 /* Mask out the first masked octet */
882 eth->addr[i] &= (0xFF << (8 - num));
884 /* Mask out completely-masked-out octets */
890 /* We're at the end of the address, and there's no mask. */
892 /* We got 3 bytes, so this is a manufacturer ID. */
894 /* Manufacturer IDs are only allowed in the "manuf"
898 /* Indicate that this is a manufacturer ID (0 is not allowed
905 /* We got 6 bytes, so this is a MAC address.
906 If we're reading one of the "manuf" files, indicate that
907 this is a MAC address (48 is not allowed as a mask). */
913 /* We didn't get 3 or 6 bytes, and there's no mask; this is
918 /* We don't know the separator used in this number; it can either
919 be ':', '-', or '.'. */
920 if (*cp != ':' && *cp != '-' && *cp != '.')
922 sep = *cp; /* subsequent separators must be the same */
924 /* It has to be the same as the first separator */
935 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
939 * See the ethers(4) or ethers(5) man page for ethers file format
940 * (not available on all systems).
941 * We allow both ethernet address separators (':' and '-'),
942 * as well as Wireshark's '.' separator.
947 if ((cp = strchr(line, '#')))
950 if ((cp = strtok(line, " \t")) == NULL)
953 if (!parse_ether_address(cp, eth, mask, manuf_file))
956 if ((cp = strtok(NULL, " \t")) == NULL)
959 g_strlcpy(eth->name, cp, MAXNAMELEN);
963 } /* parse_ether_line */
965 static FILE *eth_p = NULL;
967 static void set_ethent(char *path)
972 eth_p = eth_fopen(path, "r");
975 static void end_ethent(void)
983 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
988 static char *buf = NULL;
993 while (fgetline(&buf, &size, eth_p) >= 0) {
994 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1003 static ether_t *get_ethbyname(const gchar *name)
1007 set_ethent(g_pethers_path);
1009 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1015 set_ethent(g_ethers_path);
1017 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1025 } /* get_ethbyname */
1027 static ether_t *get_ethbyaddr(const guint8 *addr)
1032 set_ethent(g_pethers_path);
1034 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1040 set_ethent(g_ethers_path);
1042 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1050 } /* get_ethbyaddr */
1052 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
1055 /* All but the topmost byte is masked out */
1056 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
1060 /* All but the topmost 2 bytes are masked out */
1061 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1066 /* All but the topmost 3 bytes are masked out */
1067 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1068 & (HASHETHSIZE - 1);
1072 /* All but the topmost 4 bytes are masked out */
1073 return ((((addr[0] << 8) | addr[1]) ^
1074 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1079 /* All but the topmost 5 bytes are masked out */
1080 return ((((addr[1] << 8) | addr[2]) ^
1081 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1085 /* No bytes are fully masked out */
1086 return ((((addr[1] << 8) | addr[2]) ^
1087 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1091 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
1095 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
1098 /* This is a well-known MAC address; just add this to the Ethernet
1100 add_eth_name(addr, name);
1105 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1107 hash_idx = HASH_ETH_MANUF(addr);
1109 tp = manuf_table[hash_idx];
1112 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1115 if (tp->next == NULL) {
1116 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1124 memcpy(tp->addr, addr, sizeof(tp->addr));
1125 g_strlcpy(tp->name, name, MAXMANUFLEN);
1130 /* This is a range of well-known addresses; add it to the appropriate
1131 well-known-address table, creating that table if necessary. */
1132 wka_tp = wka_table[mask];
1134 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1136 hash_idx = hash_eth_wka(addr, mask);
1138 etp = (*wka_tp)[hash_idx];
1141 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1144 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
1145 /* address already known */
1148 if (etp->next == NULL) {
1149 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1157 memcpy(etp->addr, addr, sizeof(etp->addr));
1158 g_strlcpy(etp->name, name, MAXNAMELEN);
1160 etp->is_dummy_entry = FALSE;
1162 } /* add_manuf_name */
1164 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
1168 guint8 stripped_addr[3];
1170 hash_idx = HASH_ETH_MANUF(addr);
1172 /* first try to find a "perfect match" */
1173 tp = manuf_table[hash_idx];
1175 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1181 /* Mask out the broadcast/multicast flag but not the locally
1182 * administered flag as localy administered means: not assigend
1183 * by the IEEE but the local administrator instead.
1184 * 0x01 multicast / broadcast bit
1185 * 0x02 locally administered bit */
1186 memcpy(stripped_addr, addr, 3);
1187 stripped_addr[0] &= 0xFE;
1189 tp = manuf_table[hash_idx];
1191 if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
1199 } /* manuf_name_lookup */
1201 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
1204 hashether_t *(*wka_tp)[HASHETHSIZE];
1206 guint8 masked_addr[6];
1210 wka_tp = wka_table[mask];
1211 if (wka_tp == NULL) {
1212 /* There are no entries in the table for that mask value, as there is
1213 no table for that mask value. */
1217 /* Get the part of the address covered by the mask. */
1218 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1219 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1220 /* Mask out the first masked octet */
1221 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1223 /* Zero out completely-masked-out octets */
1227 hash_idx = hash_eth_wka(masked_addr, mask);
1229 tp = (*wka_tp)[hash_idx];
1232 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1240 } /* wka_name_lookup */
1242 static void initialize_ethers(void)
1248 /* Compute the pathname of the ethers file. */
1249 if (g_ethers_path == NULL) {
1250 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1251 get_systemfile_dir(), ENAME_ETHERS);
1254 /* Set g_pethers_path here, but don't actually do anything
1255 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1257 if (g_pethers_path == NULL)
1258 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1260 /* manuf hash table initialization */
1262 /* Compute the pathname of the manuf file */
1263 manuf_path = get_datafile_path(ENAME_MANUF);
1265 /* Read it and initialize the hash table */
1266 set_ethent(manuf_path);
1268 while ((eth = get_ethent(&mask, TRUE))) {
1269 add_manuf_name(eth->addr, mask, eth->name);
1276 } /* initialize_ethers */
1278 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1284 hash_idx = HASH_ETH_ADDRESS(addr);
1286 tp = eth_table[hash_idx];
1289 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1292 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1293 /* address already known */
1294 if (!tp->is_dummy_entry) {
1297 /* replace this dummy (manuf) entry with a real name */
1302 if (tp->next == NULL) {
1303 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1311 g_strlcpy(tp->name, name, MAXNAMELEN);
1313 memcpy(tp->addr, addr, sizeof(tp->addr));
1316 tp->is_dummy_entry = FALSE;
1320 } /* add_eth_name */
1322 static gchar *eth_name_lookup(const guint8 *addr)
1325 hashmanuf_t *manufp;
1331 hash_idx = HASH_ETH_ADDRESS(addr);
1333 tp = eth_table[hash_idx];
1336 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1339 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1342 if (tp->next == NULL) {
1343 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1351 /* fill in a new entry */
1353 memcpy(tp->addr, addr, sizeof(tp->addr));
1356 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1357 /* Unknown name. Try looking for it in the well-known-address
1358 tables for well-known address ranges smaller than 2^24. */
1361 /* Only the topmost 5 bytes participate fully */
1362 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1363 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1364 etp->name, addr[5] & (0xFF >> mask));
1365 tp->is_dummy_entry = TRUE;
1375 /* Only the topmost 4 bytes participate fully */
1376 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1377 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1378 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1379 tp->is_dummy_entry = TRUE;
1389 /* Only the topmost 3 bytes participate fully */
1390 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1391 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1392 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1393 tp->is_dummy_entry = TRUE;
1401 /* Now try looking in the manufacturer table. */
1402 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1403 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1404 manufp->name, addr[3], addr[4], addr[5]);
1405 tp->is_dummy_entry = TRUE;
1409 /* Now try looking for it in the well-known-address
1410 tables for well-known address ranges larger than 2^24. */
1413 /* Only the topmost 2 bytes participate fully */
1414 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1415 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1416 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1418 tp->is_dummy_entry = TRUE;
1428 /* Only the topmost byte participates fully */
1429 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1430 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1431 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1433 tp->is_dummy_entry = TRUE;
1441 for (mask = 7; mask > 0; mask--) {
1442 /* Not even the topmost byte participates fully */
1443 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1444 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1445 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1446 addr[3], addr[4], addr[5]);
1447 tp->is_dummy_entry = TRUE;
1452 /* No match whatsoever. */
1453 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1454 tp->is_dummy_entry = TRUE;
1457 g_strlcpy(tp->name, eth->name, MAXNAMELEN);
1458 tp->is_dummy_entry = FALSE;
1463 } /* eth_name_lookup */
1465 static guint8 *eth_addr_lookup(const gchar *name)
1469 hashether_t **table = eth_table;
1472 /* to be optimized (hash table from name to addr) */
1473 for (i = 0; i < HASHETHSIZE; i++) {
1476 if (strcmp(tp->name, name) == 0)
1482 /* not in hash table : performs a file lookup */
1484 if ((eth = get_ethbyname(name)) == NULL)
1487 /* add new entry in hash table */
1489 tp = add_eth_name(eth->addr, name);
1493 } /* eth_addr_lookup */
1497 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1500 * We allow three address separators (':', '-', and '.'),
1501 * as well as no separators
1505 guint32 a, a0, a1, a2, a3;
1506 gboolean found_single_number = FALSE;
1508 if ((cp = strchr(line, '#')))
1511 if ((cp = strtok(line, " \t\n")) == NULL)
1514 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1515 * fill a and found_single_number is TRUE,
1518 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1519 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1520 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1521 if (sscanf(cp, "%x", &a) == 1) {
1522 found_single_number = TRUE;
1531 if ((cp = strtok(NULL, " \t\n")) == NULL)
1534 if (found_single_number) {
1538 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1541 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1545 } /* parse_ipxnets_line */
1547 static FILE *ipxnet_p = NULL;
1549 static void set_ipxnetent(char *path)
1554 ipxnet_p = eth_fopen(path, "r");
1557 static void end_ipxnetent(void)
1565 static ipxnet_t *get_ipxnetent(void)
1568 static ipxnet_t ipxnet;
1569 static int size = 0;
1570 static char *buf = NULL;
1572 if (ipxnet_p == NULL)
1575 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1576 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1583 } /* get_ipxnetent */
1585 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1589 set_ipxnetent(g_ipxnets_path);
1591 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1594 if (ipxnet == NULL) {
1597 set_ipxnetent(g_pipxnets_path);
1599 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1607 } /* get_ipxnetbyname */
1609 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1614 set_ipxnetent(g_ipxnets_path);
1616 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1618 if (ipxnet == NULL) {
1621 set_ipxnetent(g_pipxnets_path);
1623 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1631 } /* get_ipxnetbyaddr */
1633 static void initialize_ipxnets(void)
1635 /* Compute the pathname of the ipxnets file.
1637 * XXX - is there a notion of an "ipxnets file" in any flavor of
1638 * UNIX, or with any add-on Netware package for UNIX? If not,
1639 * should the UNIX version of the ipxnets file be in the datafile
1640 * directory as well?
1642 if (g_ipxnets_path == NULL) {
1643 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1644 get_systemfile_dir(), ENAME_IPXNETS);
1647 /* Set g_pipxnets_path here, but don't actually do anything
1648 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1650 if (g_pipxnets_path == NULL)
1651 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1653 } /* initialize_ipxnets */
1655 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1660 hash_idx = HASH_IPX_NET(addr);
1662 tp = ipxnet_table[hash_idx];
1665 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1668 if (tp->next == NULL) {
1669 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1678 g_strlcpy(tp->name, name, MAXNAMELEN);
1683 } /* add_ipxnet_name */
1685 static gchar *ipxnet_name_lookup(const guint addr)
1691 hash_idx = HASH_IPX_NET(addr);
1693 tp = ipxnet_table[hash_idx];
1696 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1699 if (tp->addr == addr) {
1702 if (tp->next == NULL) {
1703 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1711 /* fill in a new entry */
1716 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1718 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1721 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1726 } /* ipxnet_name_lookup */
1728 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1732 hashipxnet_t **table = ipxnet_table;
1735 /* to be optimized (hash table from name to addr) */
1736 for (i = 0; i < HASHIPXNETSIZE; i++) {
1739 if (strcmp(tp->name, name) == 0) {
1747 /* not in hash table : performs a file lookup */
1749 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1754 /* add new entry in hash table */
1756 tp = add_ipxnet_name(ipxnet->addr, name);
1761 } /* ipxnet_addr_lookup */
1764 read_hosts_file (const char *hostspath)
1770 guint32 host_addr[4]; /* IPv4 or IPv6 */
1771 struct e_in6_addr ipv6_addr;
1776 * See the hosts(4) or hosts(5) man page for hosts file format
1777 * (not available on all systems).
1779 if ((hf = eth_fopen(hostspath, "r")) == NULL)
1782 while (fgetline(&line, &size, hf) >= 0) {
1783 if ((cp = strchr(line, '#')))
1786 if ((cp = strtok(line, " \t")) == NULL)
1787 continue; /* no tokens in the line */
1789 ret = inet_pton(AF_INET6, cp, &host_addr);
1791 continue; /* error parsing */
1796 /* Not valid IPv6 - valid IPv4? */
1797 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1802 if ((cp = strtok(NULL, " \t")) == NULL)
1803 continue; /* no host name */
1806 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1807 add_ipv6_name(&ipv6_addr, cp);
1809 add_ipv4_name(host_addr[0], cp);
1812 * Add the aliases, too, if there are any.
1814 while ((cp = strtok(NULL, " \t")) != NULL) {
1816 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1817 add_ipv6_name(&ipv6_addr, cp);
1819 add_ipv4_name(host_addr[0], cp);
1827 } /* read_hosts_file */
1830 /* Read in a list of subnet definition - name pairs.
1831 * <line> = <comment> | <entry> | <whitespace>
1832 * <comment> = <whitespace>#<any>
1833 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
1834 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
1835 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
1836 * <subnet_mask_length> is a decimal 1-31
1837 * <subnet_name> is a string containing no whitespace.
1838 * <whitespace> = (space | tab)+
1839 * Any malformed entries are ignored.
1840 * Any trailing data after the subnet_name is ignored.
1845 read_subnets_file (const char *subnetspath)
1851 guint32 host_addr; /* IPv4 ONLY */
1854 if ((hf = eth_fopen(subnetspath, "r")) == NULL)
1857 while (fgetline(&line, &size, hf) >= 0) {
1858 if ((cp = strchr(line, '#')))
1861 if ((cp = strtok(line, " \t")) == NULL)
1862 continue; /* no tokens in the line */
1865 /* Expected format is <IP4 address>/<subnet length> */
1866 cp2 = strchr(cp, '/');
1871 *cp2 = '\0'; /* Cut token */
1874 /* Check if this is a valid IPv4 address */
1875 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
1879 mask_length = atoi(cp2);
1880 if(0 >= mask_length || mask_length > 31) {
1881 continue; /* invalid mask length */
1884 if ((cp = strtok(NULL, " \t")) == NULL)
1885 continue; /* no subnet name */
1887 subnet_entry_set(host_addr, (guint32)mask_length, cp);
1894 } /* read_subnets_file */
1896 static subnet_entry_t subnet_lookup(const guint32 addr)
1898 subnet_entry_t subnet_entry;
1901 /* Search mask lengths linearly, longest first */
1903 i = SUBNETLENGTHSIZE;
1904 while(have_subnet_entry && i > 0) {
1905 guint32 masked_addr;
1906 subnet_length_entry_t* length_entry;
1908 /* Note that we run from 31 (length 32) to 0 (length 1) */
1910 g_assert(i < SUBNETLENGTHSIZE);
1913 length_entry = &subnet_length_entries[i];
1915 if(NULL != length_entry->subnet_addresses) {
1919 masked_addr = addr & length_entry->mask;
1920 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
1922 tp = length_entry->subnet_addresses[hash_idx];
1923 while(tp != NULL && tp->addr != masked_addr) {
1928 subnet_entry.mask = length_entry->mask;
1929 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
1930 subnet_entry.name = tp->name;
1931 return subnet_entry;
1936 subnet_entry.mask = 0;
1937 subnet_entry.mask_length = 0;
1938 subnet_entry.name = NULL;
1940 return subnet_entry;
1943 /* Add a subnet-definition - name pair to the set.
1944 * The definition is taken by masking the address passed in with the mask of the
1947 static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name)
1949 subnet_length_entry_t* entry;
1953 g_assert(mask_length > 0 && mask_length <= 32);
1955 entry = &subnet_length_entries[mask_length - 1];
1957 subnet_addr &= entry->mask;
1959 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
1961 if(NULL == entry->subnet_addresses) {
1962 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
1965 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
1966 if(tp->addr == subnet_addr) {
1967 return; /* XXX provide warning that an address was repeated? */
1969 hashipv4_t * new_tp = g_new(hashipv4_t,1);
1974 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
1978 tp->addr = subnet_addr;
1979 tp->is_dummy_entry = FALSE; /*Never used again...*/
1980 strncpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
1981 have_subnet_entry = TRUE;
1984 static guint32 get_subnet_mask(guint32 mask_length) {
1986 static guint32 masks[SUBNETLENGTHSIZE];
1987 static gboolean initialised = FALSE;
1990 memset(masks, 0, sizeof(masks));
1994 /* XXX There must be a better way to do this than
1995 * hand-coding the values, but I can't seem to
1999 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2000 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2001 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2002 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2003 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2004 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2005 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2006 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2008 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2009 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2010 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2011 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2012 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2013 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2014 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2015 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2017 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2018 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2019 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2020 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2021 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2022 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2023 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2024 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2026 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2027 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2028 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2029 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2030 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2031 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2032 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2033 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2036 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2037 g_assert_not_reached();
2040 return masks[mask_length - 1];
2044 static void subnet_name_lookup_init()
2049 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2050 guint32 length = i + 1;
2052 subnet_length_entries[i].subnet_addresses = NULL;
2053 subnet_length_entries[i].mask_length = length;
2054 subnet_length_entries[i].mask = get_subnet_mask(length);
2057 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2058 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2059 report_open_failure(subnetspath, errno, FALSE);
2061 g_free(subnetspath);
2064 * Load the global subnets file, if we have one.
2066 subnetspath = get_datafile_path(ENAME_SUBNETS);
2067 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2068 report_open_failure(subnetspath, errno, FALSE);
2070 g_free(subnetspath);
2074 * External Functions
2078 host_name_lookup_init(void) {
2081 #ifdef HAVE_GNU_ADNS
2084 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2085 static char rootpath_ot[] = "\\hosts";
2087 #endif /*GNU_ADNS */
2090 * Load the user's hosts file, if they have one.
2092 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2093 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2094 report_open_failure(hostspath, errno, FALSE);
2099 * Load the global hosts file, if we have one.
2101 hostspath = get_datafile_path(ENAME_HOSTS);
2102 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2103 report_open_failure(hostspath, errno, FALSE);
2107 #ifdef HAVE_GNU_ADNS
2109 * We're using GNU ADNS, which doesn't check the system hosts file;
2110 * we load that file ourselves.
2114 sysroot = getenv_utf8("WINDIR");
2115 if (sysroot != NULL) {
2117 * The file should be under WINDIR.
2118 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2119 * %WINDIR%\system32\drivers\etc\hosts.
2120 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2122 * XXX - should we base it on the dwPlatformId value from
2125 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2126 if (!read_hosts_file(hostspath)) {
2128 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2129 read_hosts_file(hostspath);
2134 read_hosts_file("/etc/hosts");
2137 /* XXX - Any flags we should be using? */
2138 /* XXX - We could provide config settings for DNS servers, and
2139 pass them to ADNS with adns_init_strcfg */
2140 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2142 * XXX - should we report the error? I'm assuming that some crashes
2143 * reported on a Windows machine with TCP/IP not configured are due
2144 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2145 * ADNS in a state where it crashes due to that. We'll still try
2146 * doing name resolution anyway.
2150 gnu_adns_initialized = TRUE;
2151 adns_currently_queued = 0;
2152 #endif /* HAVE_GNU_ADNS */
2154 subnet_name_lookup_init();
2157 #ifdef HAVE_GNU_ADNS
2159 /* XXX - The ADNS "documentation" isn't very clear:
2160 * - Do we need to keep our query structures around?
2163 host_name_lookup_process(gpointer data _U_) {
2164 adns_queue_msg_t *almsg;
2166 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2172 adns_queue_head = g_list_first(adns_queue_head);
2174 cur = adns_queue_head;
2175 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
2176 almsg = (adns_queue_msg_t *) cur->data;
2177 if (! almsg->submitted && almsg->type == AF_INET) {
2178 addr_bytes = (guint8 *) &almsg->ip4_addr;
2179 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2180 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2181 /* XXX - what if it fails? */
2182 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2183 almsg->submitted = TRUE;
2184 adns_currently_queued++;
2189 cur = adns_queue_head;
2192 almsg = (adns_queue_msg_t *) cur->data;
2193 if (almsg->submitted) {
2194 ret = adns_check(ads, &almsg->query, &ans, NULL);
2196 if (ans->status == adns_s_ok) {
2197 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2204 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
2206 adns_currently_queued--;
2210 /* Keep the timeout in place */
2215 host_name_lookup_cleanup(void) {
2218 adns_queue_head = g_list_first(adns_queue_head);
2219 while (adns_queue_head) {
2220 qdata = adns_queue_head->data;
2221 adns_queue_head = g_list_remove(adns_queue_head, qdata);
2225 if (gnu_adns_initialized)
2232 host_name_lookup_process(gpointer data _U_) {
2233 /* Kill the timeout, as there's nothing for it to do */
2238 host_name_lookup_cleanup(void) {
2241 #endif /* HAVE_GNU_ADNS */
2243 extern gchar *get_hostname(guint addr)
2247 if (!(g_resolv_flags & RESOLV_NETWORK))
2248 return ip_to_str((guint8 *)&addr);
2250 return host_name_lookup(addr, &found);
2253 extern const gchar *get_hostname6(struct e_in6_addr *addr)
2257 if (!(g_resolv_flags & RESOLV_NETWORK))
2258 return ip6_to_str(addr);
2259 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
2260 return ip6_to_str(addr);
2261 return host_name_lookup6(addr, &found);
2264 extern void add_ipv4_name(guint addr, const gchar *name)
2270 hash_idx = HASH_IPV4_ADDRESS(addr);
2272 tp = ipv4_table[hash_idx];
2275 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
2278 if (tp->addr == addr) {
2279 /* address already known */
2280 if (!tp->is_dummy_entry) {
2283 /* replace this dummy entry with the new one */
2288 if (tp->next == NULL) {
2289 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
2297 g_strlcpy(tp->name, name, MAXNAMELEN);
2302 tp->is_dummy_entry = FALSE;
2304 } /* add_ipv4_name */
2306 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
2312 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2314 tp = ipv6_table[hash_idx];
2317 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2320 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2321 /* address already known */
2322 if (!tp->is_dummy_entry) {
2325 /* replace this dummy entry with the new one */
2330 if (tp->next == NULL) {
2331 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2339 g_strlcpy(tp->name, name, MAXNAMELEN);
2344 tp->is_dummy_entry = FALSE;
2346 } /* add_ipv6_name */
2348 /* -----------------
2349 * unsigned integer to ascii
2351 static gchar *ep_utoa(guint port)
2353 gchar *bp = ep_alloc(MAXNAMELEN);
2355 bp = &bp[MAXNAMELEN -1];
2359 *--bp = (port % 10) +'0';
2360 } while ((port /= 10) != 0);
2365 extern gchar *get_udp_port(guint port)
2368 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2369 return ep_utoa(port);
2372 return serv_name_lookup(port, PT_UDP);
2374 } /* get_udp_port */
2376 extern gchar *get_dccp_port(guint port)
2379 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2380 return ep_utoa(port);
2383 return serv_name_lookup(port, PT_DCCP);
2385 } /* get_dccp_port */
2388 extern gchar *get_tcp_port(guint port)
2391 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2392 return ep_utoa(port);
2395 return serv_name_lookup(port, PT_TCP);
2397 } /* get_tcp_port */
2399 extern gchar *get_sctp_port(guint port)
2402 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2403 return ep_utoa(port);
2406 return serv_name_lookup(port, PT_SCTP);
2408 } /* get_sctp_port */
2411 const gchar *get_addr_name(address *addr)
2413 const gchar *result;
2415 result = solve_address_to_name(addr);
2421 /* if it gets here, either it is of type AT_NONE, */
2422 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
2424 if (addr->type == AT_NONE){
2428 return(address_to_str(addr));
2429 } /* get_addr_name */
2432 void get_addr_name_buf(address *addr, gchar *buf, guint size)
2434 const gchar *result = get_addr_name(addr);
2436 g_snprintf(buf, size, "%s", result);
2437 } /* get_addr_name_buf */
2440 extern gchar *get_ether_name(const guint8 *addr)
2442 if (!(g_resolv_flags & RESOLV_MAC))
2443 return ether_to_str(addr);
2445 if (!eth_resolution_initialized) {
2446 initialize_ethers();
2447 eth_resolution_initialized = 1;
2450 return eth_name_lookup(addr);
2452 } /* get_ether_name */
2454 /* Look for an ether name in the hash, and return it if found.
2455 * If it's not found, simply return NULL. We DO NOT make a new
2456 * hash entry for it with the hex digits turned into a string.
2458 gchar *get_ether_name_if_known(const guint8 *addr)
2463 /* Initialize ether structs if we're the first
2464 * ether-related function called */
2465 if (!(g_resolv_flags & RESOLV_MAC))
2468 if (!eth_resolution_initialized) {
2469 initialize_ethers();
2470 eth_resolution_initialized = 1;
2473 hash_idx = HASH_ETH_ADDRESS(addr);
2475 tp = eth_table[hash_idx];
2478 /* Hash key not found in table.
2479 * Force a lookup (and a hash entry) for addr, then call
2480 * myself. I plan on not getting into an infinite loop because
2481 * eth_name_lookup() is guaranteed to make a hashtable entry,
2482 * so when I call myself again, I can never get into this
2483 * block of code again. Knock on wood...
2485 (void) eth_name_lookup(addr);
2486 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2490 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2491 if (!tp->is_dummy_entry) {
2492 /* A name was found, and its origin is an ethers file */
2496 /* A name was found, but it was created, not found in a file */
2500 if (tp->next == NULL) {
2501 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2502 (void) eth_name_lookup(addr);
2503 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2508 g_assert_not_reached();
2513 extern guint8 *get_ether_addr(const gchar *name)
2516 /* force resolution (do not check g_resolv_flags) */
2518 if (!eth_resolution_initialized) {
2519 initialize_ethers();
2520 eth_resolution_initialized = 1;
2523 return eth_addr_lookup(name);
2525 } /* get_ether_addr */
2527 extern void add_ether_byip(guint ip, const guint8 *eth)
2533 /* first check that IP address can be resolved */
2535 if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2538 /* ok, we can add this entry in the ethers hashtable */
2541 add_eth_name(eth, host);
2543 } /* add_ether_byip */
2545 extern const gchar *get_ipxnet_name(const guint32 addr)
2548 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2549 return ipxnet_to_str_punct(addr, '\0');
2552 if (!ipxnet_resolution_initialized) {
2553 initialize_ipxnets();
2554 ipxnet_resolution_initialized = 1;
2557 return ipxnet_name_lookup(addr);
2559 } /* get_ipxnet_name */
2561 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2566 /* force resolution (do not check g_resolv_flags) */
2568 if (!ipxnet_resolution_initialized) {
2569 initialize_ipxnets();
2570 ipxnet_resolution_initialized = 1;
2573 addr = ipxnet_addr_lookup(name, &success);
2578 } /* get_ipxnet_addr */
2580 extern const gchar *get_manuf_name(const guint8 *addr)
2583 hashmanuf_t *manufp;
2585 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2586 initialize_ethers();
2587 eth_resolution_initialized = 1;
2590 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2591 cur=ep_alloc(MAXMANUFLEN);
2592 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2596 return manufp->name;
2598 } /* get_manuf_name */
2601 const gchar *get_manuf_name_if_known(const guint8 *addr)
2603 hashmanuf_t *manufp;
2605 if (!eth_resolution_initialized) {
2606 initialize_ethers();
2607 eth_resolution_initialized = 1;
2610 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2614 return manufp->name;
2616 } /* get_manuf_name_if_known */
2619 /* Translate a string, assumed either to be a dotted-quad IP address or
2620 * a host name, to a numeric IP address. Return TRUE if we succeed and
2621 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2622 * Used more in the dfilter parser rather than in packet dissectors */
2623 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2625 struct in_addr ipaddr;
2629 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2630 * inet_pton(AF_INET) does not support hexadecimal notation nor
2631 * less-than-4 octet notation.
2633 if (!inet_aton(host, &ipaddr)) {
2634 /* It's not a valid dotted-quad IP address; is it a valid
2636 hp = gethostbyname(host);
2640 /* Apparently, some versions of gethostbyaddr can
2641 * return IPv6 addresses. */
2642 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2643 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2648 /* Does the string really contain dotted-quad IP?
2649 * Check against inet_atons that accept strings such as
2650 * "130.230" as valid addresses and try to convert them
2651 * to some form of a classful (host.net) notation.
2653 unsigned int a0, a1, a2, a3;
2654 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2658 *addrp = g_ntohl(ipaddr.s_addr);
2663 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2664 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2665 * return FALSE if we fail.
2667 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2671 if (inet_pton(AF_INET6, host, addrp) == 1)
2675 #ifdef HAVE_GETHOSTBYNAME2
2676 hp = gethostbyname2(host, AF_INET6);
2680 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2681 memcpy(addrp, hp->h_addr, hp->h_length);
2689 * Find out whether a hostname resolves to an ip or ipv6 address
2690 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2691 * that we don't know)
2693 const char* host_ip_af(const char *host
2694 #ifndef HAVE_GETHOSTBYNAME2
2699 #ifdef HAVE_GETHOSTBYNAME2
2701 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";