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 <wsutil/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];
236 static hashport_t **cb_port_table;
237 static gchar *cb_service;
239 static hashport_t *udp_port_table[HASHPORTSIZE];
240 static hashport_t *tcp_port_table[HASHPORTSIZE];
241 static hashport_t *sctp_port_table[HASHPORTSIZE];
242 static hashport_t *dccp_port_table[HASHPORTSIZE];
243 static hashether_t *eth_table[HASHETHSIZE];
244 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
245 static hashether_t *(*wka_table[48])[HASHETHSIZE];
246 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
248 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
249 static gboolean have_subnet_entry = FALSE;
251 static int eth_resolution_initialized = 0;
252 static int ipxnet_resolution_initialized = 0;
253 static int service_resolution_initialized = 0;
255 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
256 static void add_serv_port_cb(guint32 port);
259 * Flag controlling what names to resolve.
261 guint32 g_resolv_flags;
264 * Global variables (can be changed in GUI sections)
265 * XXX - they could be changed in GUI code, but there's currently no
266 * GUI code to change them.
269 gchar *g_ethers_path = NULL; /* global ethers file */
270 gchar *g_pethers_path = NULL; /* personal ethers file */
271 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
272 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
273 gchar *g_services_path = NULL; /* global services file */
274 gchar *g_pservices_path = NULL; /* personal services file */
275 /* first resolving call */
281 static gboolean gnu_adns_initialized = FALSE;
285 int adns_currently_queued = 0;
287 typedef struct _adns_queue_msg
291 struct e_in6_addr ip6_addr;
296 GList *adns_queue_head = NULL;
298 #endif /* HAVE_GNU_ADNS */
303 const gchar* name; /* Shallow copy */
307 * Miscellaneous functions
310 static int fgetline(char **buf, int *size, FILE *fp)
322 if ((*buf = g_malloc(*size)) == NULL)
330 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
331 if (len+1 >= *size) {
332 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
338 if (len == 0 && c == EOF)
349 * Local function definitions
351 static subnet_entry_t subnet_lookup(const guint32 addr);
352 static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name);
355 static void add_service_name(hashport_t **proto_table, guint port, const char *service_name)
361 hash_idx = HASH_PORT(port);
362 tp = proto_table[hash_idx];
365 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
368 if( tp->port == port ) {
371 if (tp->next == NULL) {
372 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
380 /* fill in a new entry */
384 g_strlcpy(tp->name, service_name, MAXNAMELEN);
388 static void parse_service_line (char *line)
391 * See the services(4) or services(5) man page for services file format
392 * (not available on all systems).
399 range_t *port_rng = NULL;
400 guint32 max_port = MAX_UDP_PORT;
402 if ((cp = strchr(line, '#')))
405 if ((cp = strtok(line, " \t")) == NULL)
410 if ((cp = strtok(NULL, " \t")) == NULL)
415 if ((cp = strtok(cp, "/")) == NULL)
418 if ((cp = strtok(NULL, "/")) == NULL)
421 /* seems we got all interesting things from the file */
422 if(strcmp(cp, "tcp") == 0) {
423 max_port = MAX_TCP_PORT;
424 cb_port_table = tcp_port_table;
426 else if(strcmp(cp, "udp") == 0) {
427 max_port = MAX_UDP_PORT;
428 cb_port_table = udp_port_table;
430 else if(strcmp(cp, "sctp") == 0) {
431 max_port = MAX_SCTP_PORT;
432 cb_port_table = sctp_port_table;
434 else if(strcmp(cp, "dccp") == 0) {
435 max_port = MAX_DCCP_PORT;
436 cb_port_table = dccp_port_table;
441 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
442 /* some assertion here? */
446 cb_service = service;
447 range_foreach(port_rng, add_serv_port_cb);
449 } /* parse_service_line */
453 add_serv_port_cb(guint32 port)
456 add_service_name(cb_port_table, port, cb_service);
461 static void parse_services_file(const char * path)
465 static char *buf = NULL;
467 /* services hash table initialization */
468 serv_p = ws_fopen(path, "r");
473 while (fgetline(&buf, &size, serv_p) >= 0) {
474 parse_service_line (buf);
481 static void initialize_services(void)
484 /* the hash table won't ignore duplicates, so use the personal path first */
486 /* set personal services path */
487 if (g_pservices_path == NULL)
488 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
490 parse_services_file(g_pservices_path);
492 /* Compute the pathname of the services file. */
493 if (g_services_path == NULL) {
494 g_services_path = get_datafile_path(ENAME_SERVICES);
497 parse_services_file(g_services_path);
499 } /* initialize_services */
503 static gchar *serv_name_lookup(guint port, port_type proto)
508 const char *serv_proto = NULL;
509 struct servent *servp;
512 if (!service_resolution_initialized) {
513 initialize_services();
514 service_resolution_initialized = 1;
519 table = udp_port_table;
523 table = tcp_port_table;
527 table = sctp_port_table;
531 table = dccp_port_table;
535 /* not yet implemented */
540 hash_idx = HASH_PORT(port);
541 tp = table[hash_idx];
544 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
547 if( tp->port == port ) {
550 if (tp->next == NULL) {
551 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
559 /* fill in a new entry */
563 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
564 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
566 g_snprintf(tp->name, MAXNAMELEN, "%d", port);
568 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
573 } /* serv_name_lookup */
576 #ifdef AVOID_DNS_TIMEOUT
578 #define DNS_TIMEOUT 2 /* max sec per call */
580 jmp_buf hostname_env;
582 static void abort_network_query(int sig _U_)
584 longjmp(hostname_env, 1);
586 #endif /* AVOID_DNS_TIMEOUT */
588 /* Fill in an IP4 structure with info from subnets file or just with
589 * string form of address.
591 static void fill_dummy_ip4(guint addr, hashipv4_t* volatile tp)
593 subnet_entry_t subnet_entry;
594 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
596 /* Do we have a subnet for this address? */
597 subnet_entry = subnet_lookup(addr);
598 if(0 != subnet_entry.mask) {
599 /* Print name, then '.' then IP address after subnet mask */
601 gchar buffer[MAX_IP_STR_LEN];
605 host_addr = addr & (~(guint32)subnet_entry.mask);
606 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
609 /* Skip to first octet that is not totally masked
610 * If length of mask is 32, we chomp the whole address.
611 * If the address string starts '.' (should not happen?),
614 i = subnet_entry.mask_length / 8;
615 while(*(paddr) != '\0' && i > 0) {
616 if(*(++paddr) == '.') {
621 /* There are more efficient ways to do this, but this is safe if we
622 * trust g_snprintf and MAXNAMELEN
624 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
626 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
630 static gchar *host_name_lookup(guint addr, gboolean *found)
633 hashipv4_t * volatile tp;
634 struct hostent *hostp;
636 adns_queue_msg_t *qmsg;
641 hash_idx = HASH_IPV4_ADDRESS(addr);
643 tp = ipv4_table[hash_idx];
646 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
649 if( tp->addr == addr ) {
650 if (tp->is_dummy_entry)
654 if (tp->next == NULL) {
655 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
663 /* fill in a new entry */
668 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
669 prefs.name_resolve_concurrency > 0 &&
670 gnu_adns_initialized) {
671 qmsg = g_malloc(sizeof(adns_queue_msg_t));
672 qmsg->type = AF_INET;
673 qmsg->ip4_addr = addr;
674 qmsg->submitted = FALSE;
675 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
677 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
678 * going to risk changing the semantics.
680 fill_dummy_ip4(addr, tp);
683 #endif /* HAVE_GNU_ADNS */
686 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
687 * the name of the host on which it's running; to work around that
688 * botch, we don't try to translate an all-zero IP address to a host
691 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
692 /* Use async DNS if possible, else fall back to timeouts,
693 * else call gethostbyaddr and hope for the best
696 # ifdef AVOID_DNS_TIMEOUT
698 /* Quick hack to avoid DNS/YP timeout */
700 if (!setjmp(hostname_env)) {
701 signal(SIGALRM, abort_network_query);
703 # endif /* AVOID_DNS_TIMEOUT */
705 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
707 # ifdef AVOID_DNS_TIMEOUT
709 # endif /* AVOID_DNS_TIMEOUT */
712 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
713 tp->is_dummy_entry = FALSE;
717 # ifdef AVOID_DNS_TIMEOUT
719 # endif /* AVOID_DNS_TIMEOUT */
723 /* unknown host or DNS timeout */
726 fill_dummy_ip4(addr, tp);
729 } /* host_name_lookup */
731 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
734 hashipv6_t * volatile tp;
736 struct hostent *hostp;
741 hash_idx = HASH_IPV6_ADDRESS(*addr);
743 tp = ipv6_table[hash_idx];
746 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
749 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
750 if (tp->is_dummy_entry)
754 if (tp->next == NULL) {
755 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
763 /* fill in a new entry */
768 if (g_resolv_flags & RESOLV_NETWORK) {
769 #ifdef AVOID_DNS_TIMEOUT
771 /* Quick hack to avoid DNS/YP timeout */
773 if (!setjmp(hostname_env)) {
774 signal(SIGALRM, abort_network_query);
776 #endif /* AVOID_DNS_TIMEOUT */
777 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
778 #ifdef AVOID_DNS_TIMEOUT
780 # endif /* AVOID_DNS_TIMEOUT */
783 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
784 tp->is_dummy_entry = FALSE;
788 #ifdef AVOID_DNS_TIMEOUT
790 # endif /* AVOID_DNS_TIMEOUT */
794 /* unknown host or DNS timeout */
796 ip6_to_str_buf(addr, tp->name);
797 tp->is_dummy_entry = TRUE;
801 } /* host_name_lookup6 */
803 static const gchar *solve_address_to_name(address *addr)
806 struct e_in6_addr ipv6_addr;
808 switch (addr->type) {
811 return get_ether_name(addr->data);
814 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
815 return get_hostname(ipv4_addr);
818 memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
819 return get_hostname6(&ipv6_addr);
827 } /* solve_address_to_name */
831 * Ethernet / manufacturer resolution
833 * The following functions implement ethernet address resolution and
834 * ethers files parsing (see ethers(4)).
836 * The manuf file has the same format as ethers(4) except that names are
837 * truncated to MAXMANUFLEN-1 characters and that an address contains
838 * only 3 bytes (instead of 6).
842 * I decide to not use the existing functions (see ethers(3) on some
843 * operating systems) for the following reasons:
844 * - performance gains (use of hash tables and some other enhancements),
845 * - use of two ethers files (system-wide and per user),
846 * - avoid the use of NIS maps,
847 * - lack of these functions on some systems.
849 * So the following functions do _not_ behave as the standard ones.
856 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
857 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
861 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
869 for (i = 0; i < 6; i++) {
870 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
871 if (!isxdigit((unsigned char)*cp))
873 num = strtoul(cp, &p, 16);
875 return FALSE; /* failed */
877 return FALSE; /* not a valid octet */
878 eth->addr[i] = (guint8) num;
879 cp = p; /* skip past the number */
881 /* OK, what character terminated the octet? */
883 /* "/" - this has a mask. */
885 /* Entries with masks are allowed only in the "manuf" files. */
888 cp++; /* skip past the '/' to get to the mask */
889 if (!isdigit((unsigned char)*cp))
890 return FALSE; /* no sign allowed */
891 num = strtoul(cp, &p, 10);
893 return FALSE; /* failed */
894 cp = p; /* skip past the number */
895 if (*cp != '\0' && !isspace((unsigned char)*cp))
896 return FALSE; /* bogus terminator */
897 if (num == 0 || num >= 48)
898 return FALSE; /* bogus mask */
899 /* Mask out the bits not covered by the mask */
901 for (i = 0; num >= 8; i++, num -= 8)
902 ; /* skip octets entirely covered by the mask */
903 /* Mask out the first masked octet */
904 eth->addr[i] &= (0xFF << (8 - num));
906 /* Mask out completely-masked-out octets */
912 /* We're at the end of the address, and there's no mask. */
914 /* We got 3 bytes, so this is a manufacturer ID. */
916 /* Manufacturer IDs are only allowed in the "manuf"
920 /* Indicate that this is a manufacturer ID (0 is not allowed
927 /* We got 6 bytes, so this is a MAC address.
928 If we're reading one of the "manuf" files, indicate that
929 this is a MAC address (48 is not allowed as a mask). */
935 /* We didn't get 3 or 6 bytes, and there's no mask; this is
940 /* We don't know the separator used in this number; it can either
941 be ':', '-', or '.'. */
942 if (*cp != ':' && *cp != '-' && *cp != '.')
944 sep = *cp; /* subsequent separators must be the same */
946 /* It has to be the same as the first separator */
957 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
961 * See the ethers(4) or ethers(5) man page for ethers file format
962 * (not available on all systems).
963 * We allow both ethernet address separators (':' and '-'),
964 * as well as Wireshark's '.' separator.
969 if ((cp = strchr(line, '#')))
972 if ((cp = strtok(line, " \t")) == NULL)
975 if (!parse_ether_address(cp, eth, mask, manuf_file))
978 if ((cp = strtok(NULL, " \t")) == NULL)
981 g_strlcpy(eth->name, cp, MAXNAMELEN);
985 } /* parse_ether_line */
987 static FILE *eth_p = NULL;
989 static void set_ethent(char *path)
994 eth_p = ws_fopen(path, "r");
997 static void end_ethent(void)
1005 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
1009 static int size = 0;
1010 static char *buf = NULL;
1015 while (fgetline(&buf, &size, eth_p) >= 0) {
1016 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1025 static ether_t *get_ethbyname(const gchar *name)
1029 set_ethent(g_pethers_path);
1031 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1037 set_ethent(g_ethers_path);
1039 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1047 } /* get_ethbyname */
1049 static ether_t *get_ethbyaddr(const guint8 *addr)
1054 set_ethent(g_pethers_path);
1056 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1062 set_ethent(g_ethers_path);
1064 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1072 } /* get_ethbyaddr */
1074 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
1077 /* All but the topmost byte is masked out */
1078 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
1082 /* All but the topmost 2 bytes are masked out */
1083 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1088 /* All but the topmost 3 bytes are masked out */
1089 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1090 & (HASHETHSIZE - 1);
1094 /* All but the topmost 4 bytes are masked out */
1095 return ((((addr[0] << 8) | addr[1]) ^
1096 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1101 /* All but the topmost 5 bytes are masked out */
1102 return ((((addr[1] << 8) | addr[2]) ^
1103 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1107 /* No bytes are fully masked out */
1108 return ((((addr[1] << 8) | addr[2]) ^
1109 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1113 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
1117 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
1120 /* This is a well-known MAC address; just add this to the Ethernet
1122 add_eth_name(addr, name);
1127 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1129 hash_idx = HASH_ETH_MANUF(addr);
1131 tp = manuf_table[hash_idx];
1134 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1137 if (tp->next == NULL) {
1138 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1146 memcpy(tp->addr, addr, sizeof(tp->addr));
1147 g_strlcpy(tp->name, name, MAXMANUFLEN);
1152 /* This is a range of well-known addresses; add it to the appropriate
1153 well-known-address table, creating that table if necessary. */
1154 wka_tp = wka_table[mask];
1156 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1158 hash_idx = hash_eth_wka(addr, mask);
1160 etp = (*wka_tp)[hash_idx];
1163 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1166 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
1167 /* address already known */
1170 if (etp->next == NULL) {
1171 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1179 memcpy(etp->addr, addr, sizeof(etp->addr));
1180 g_strlcpy(etp->name, name, MAXNAMELEN);
1182 etp->is_dummy_entry = FALSE;
1184 } /* add_manuf_name */
1186 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
1190 guint8 stripped_addr[3];
1192 hash_idx = HASH_ETH_MANUF(addr);
1194 /* first try to find a "perfect match" */
1195 tp = manuf_table[hash_idx];
1197 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1203 /* Mask out the broadcast/multicast flag but not the locally
1204 * administered flag as localy administered means: not assigend
1205 * by the IEEE but the local administrator instead.
1206 * 0x01 multicast / broadcast bit
1207 * 0x02 locally administered bit */
1208 memcpy(stripped_addr, addr, 3);
1209 stripped_addr[0] &= 0xFE;
1211 tp = manuf_table[hash_idx];
1213 if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
1221 } /* manuf_name_lookup */
1223 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
1226 hashether_t *(*wka_tp)[HASHETHSIZE];
1228 guint8 masked_addr[6];
1232 wka_tp = wka_table[mask];
1233 if (wka_tp == NULL) {
1234 /* There are no entries in the table for that mask value, as there is
1235 no table for that mask value. */
1239 /* Get the part of the address covered by the mask. */
1240 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1241 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1242 /* Mask out the first masked octet */
1243 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1245 /* Zero out completely-masked-out octets */
1249 hash_idx = hash_eth_wka(masked_addr, mask);
1251 tp = (*wka_tp)[hash_idx];
1254 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1262 } /* wka_name_lookup */
1264 static void initialize_ethers(void)
1270 /* Compute the pathname of the ethers file. */
1271 if (g_ethers_path == NULL) {
1272 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1273 get_systemfile_dir(), ENAME_ETHERS);
1276 /* Set g_pethers_path here, but don't actually do anything
1277 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1279 if (g_pethers_path == NULL)
1280 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1282 /* manuf hash table initialization */
1284 /* Compute the pathname of the manuf file */
1285 manuf_path = get_datafile_path(ENAME_MANUF);
1287 /* Read it and initialize the hash table */
1288 set_ethent(manuf_path);
1290 while ((eth = get_ethent(&mask, TRUE))) {
1291 add_manuf_name(eth->addr, mask, eth->name);
1298 } /* initialize_ethers */
1300 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1306 hash_idx = HASH_ETH_ADDRESS(addr);
1308 tp = eth_table[hash_idx];
1311 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1314 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1315 /* address already known */
1316 if (!tp->is_dummy_entry) {
1319 /* replace this dummy (manuf) entry with a real name */
1324 if (tp->next == NULL) {
1325 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1333 g_strlcpy(tp->name, name, MAXNAMELEN);
1335 memcpy(tp->addr, addr, sizeof(tp->addr));
1338 tp->is_dummy_entry = FALSE;
1342 } /* add_eth_name */
1344 static gchar *eth_name_lookup(const guint8 *addr)
1347 hashmanuf_t *manufp;
1353 hash_idx = HASH_ETH_ADDRESS(addr);
1355 tp = eth_table[hash_idx];
1358 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1361 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1364 if (tp->next == NULL) {
1365 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1373 /* fill in a new entry */
1375 memcpy(tp->addr, addr, sizeof(tp->addr));
1378 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1379 /* Unknown name. Try looking for it in the well-known-address
1380 tables for well-known address ranges smaller than 2^24. */
1383 /* Only the topmost 5 bytes participate fully */
1384 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1385 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1386 etp->name, addr[5] & (0xFF >> mask));
1387 tp->is_dummy_entry = TRUE;
1397 /* Only the topmost 4 bytes participate fully */
1398 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1399 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1400 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1401 tp->is_dummy_entry = TRUE;
1411 /* Only the topmost 3 bytes participate fully */
1412 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1413 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1414 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1415 tp->is_dummy_entry = TRUE;
1423 /* Now try looking in the manufacturer table. */
1424 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1425 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1426 manufp->name, addr[3], addr[4], addr[5]);
1427 tp->is_dummy_entry = TRUE;
1431 /* Now try looking for it in the well-known-address
1432 tables for well-known address ranges larger than 2^24. */
1435 /* Only the topmost 2 bytes participate fully */
1436 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1437 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1438 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1440 tp->is_dummy_entry = TRUE;
1450 /* Only the topmost byte participates fully */
1451 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1452 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1453 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1455 tp->is_dummy_entry = TRUE;
1463 for (mask = 7; mask > 0; mask--) {
1464 /* Not even the topmost byte participates fully */
1465 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1466 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1467 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1468 addr[3], addr[4], addr[5]);
1469 tp->is_dummy_entry = TRUE;
1474 /* No match whatsoever. */
1475 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1476 tp->is_dummy_entry = TRUE;
1479 g_strlcpy(tp->name, eth->name, MAXNAMELEN);
1480 tp->is_dummy_entry = FALSE;
1485 } /* eth_name_lookup */
1487 static guint8 *eth_addr_lookup(const gchar *name)
1491 hashether_t **table = eth_table;
1494 /* to be optimized (hash table from name to addr) */
1495 for (i = 0; i < HASHETHSIZE; i++) {
1498 if (strcmp(tp->name, name) == 0)
1504 /* not in hash table : performs a file lookup */
1506 if ((eth = get_ethbyname(name)) == NULL)
1509 /* add new entry in hash table */
1511 tp = add_eth_name(eth->addr, name);
1515 } /* eth_addr_lookup */
1519 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1522 * We allow three address separators (':', '-', and '.'),
1523 * as well as no separators
1527 guint32 a, a0, a1, a2, a3;
1528 gboolean found_single_number = FALSE;
1530 if ((cp = strchr(line, '#')))
1533 if ((cp = strtok(line, " \t\n")) == NULL)
1536 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1537 * fill a and found_single_number is TRUE,
1540 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1541 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1542 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1543 if (sscanf(cp, "%x", &a) == 1) {
1544 found_single_number = TRUE;
1553 if ((cp = strtok(NULL, " \t\n")) == NULL)
1556 if (found_single_number) {
1560 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1563 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1567 } /* parse_ipxnets_line */
1569 static FILE *ipxnet_p = NULL;
1571 static void set_ipxnetent(char *path)
1576 ipxnet_p = ws_fopen(path, "r");
1579 static void end_ipxnetent(void)
1587 static ipxnet_t *get_ipxnetent(void)
1590 static ipxnet_t ipxnet;
1591 static int size = 0;
1592 static char *buf = NULL;
1594 if (ipxnet_p == NULL)
1597 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1598 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1605 } /* get_ipxnetent */
1607 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1611 set_ipxnetent(g_ipxnets_path);
1613 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1616 if (ipxnet == NULL) {
1619 set_ipxnetent(g_pipxnets_path);
1621 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1629 } /* get_ipxnetbyname */
1631 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1636 set_ipxnetent(g_ipxnets_path);
1638 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1640 if (ipxnet == NULL) {
1643 set_ipxnetent(g_pipxnets_path);
1645 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1653 } /* get_ipxnetbyaddr */
1655 static void initialize_ipxnets(void)
1657 /* Compute the pathname of the ipxnets file.
1659 * XXX - is there a notion of an "ipxnets file" in any flavor of
1660 * UNIX, or with any add-on Netware package for UNIX? If not,
1661 * should the UNIX version of the ipxnets file be in the datafile
1662 * directory as well?
1664 if (g_ipxnets_path == NULL) {
1665 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1666 get_systemfile_dir(), ENAME_IPXNETS);
1669 /* Set g_pipxnets_path here, but don't actually do anything
1670 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1672 if (g_pipxnets_path == NULL)
1673 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1675 } /* initialize_ipxnets */
1677 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1682 hash_idx = HASH_IPX_NET(addr);
1684 tp = ipxnet_table[hash_idx];
1687 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1690 if (tp->next == NULL) {
1691 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1700 g_strlcpy(tp->name, name, MAXNAMELEN);
1705 } /* add_ipxnet_name */
1707 static gchar *ipxnet_name_lookup(const guint addr)
1713 hash_idx = HASH_IPX_NET(addr);
1715 tp = ipxnet_table[hash_idx];
1718 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1721 if (tp->addr == addr) {
1724 if (tp->next == NULL) {
1725 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1733 /* fill in a new entry */
1738 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1740 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1743 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1748 } /* ipxnet_name_lookup */
1750 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1754 hashipxnet_t **table = ipxnet_table;
1757 /* to be optimized (hash table from name to addr) */
1758 for (i = 0; i < HASHIPXNETSIZE; i++) {
1761 if (strcmp(tp->name, name) == 0) {
1769 /* not in hash table : performs a file lookup */
1771 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1776 /* add new entry in hash table */
1778 tp = add_ipxnet_name(ipxnet->addr, name);
1783 } /* ipxnet_addr_lookup */
1786 read_hosts_file (const char *hostspath)
1792 guint32 host_addr[4]; /* IPv4 or IPv6 */
1793 struct e_in6_addr ipv6_addr;
1798 * See the hosts(4) or hosts(5) man page for hosts file format
1799 * (not available on all systems).
1801 if ((hf = ws_fopen(hostspath, "r")) == NULL)
1804 while (fgetline(&line, &size, hf) >= 0) {
1805 if ((cp = strchr(line, '#')))
1808 if ((cp = strtok(line, " \t")) == NULL)
1809 continue; /* no tokens in the line */
1811 ret = inet_pton(AF_INET6, cp, &host_addr);
1813 continue; /* error parsing */
1818 /* Not valid IPv6 - valid IPv4? */
1819 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1824 if ((cp = strtok(NULL, " \t")) == NULL)
1825 continue; /* no host name */
1828 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1829 add_ipv6_name(&ipv6_addr, cp);
1831 add_ipv4_name(host_addr[0], cp);
1834 * Add the aliases, too, if there are any.
1836 while ((cp = strtok(NULL, " \t")) != NULL) {
1838 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1839 add_ipv6_name(&ipv6_addr, cp);
1841 add_ipv4_name(host_addr[0], cp);
1849 } /* read_hosts_file */
1852 /* Read in a list of subnet definition - name pairs.
1853 * <line> = <comment> | <entry> | <whitespace>
1854 * <comment> = <whitespace>#<any>
1855 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
1856 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
1857 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
1858 * <subnet_mask_length> is a decimal 1-31
1859 * <subnet_name> is a string containing no whitespace.
1860 * <whitespace> = (space | tab)+
1861 * Any malformed entries are ignored.
1862 * Any trailing data after the subnet_name is ignored.
1867 read_subnets_file (const char *subnetspath)
1873 guint32 host_addr; /* IPv4 ONLY */
1876 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
1879 while (fgetline(&line, &size, hf) >= 0) {
1880 if ((cp = strchr(line, '#')))
1883 if ((cp = strtok(line, " \t")) == NULL)
1884 continue; /* no tokens in the line */
1887 /* Expected format is <IP4 address>/<subnet length> */
1888 cp2 = strchr(cp, '/');
1893 *cp2 = '\0'; /* Cut token */
1896 /* Check if this is a valid IPv4 address */
1897 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
1901 mask_length = atoi(cp2);
1902 if(0 >= mask_length || mask_length > 31) {
1903 continue; /* invalid mask length */
1906 if ((cp = strtok(NULL, " \t")) == NULL)
1907 continue; /* no subnet name */
1909 subnet_entry_set(host_addr, (guint32)mask_length, cp);
1916 } /* read_subnets_file */
1918 static subnet_entry_t subnet_lookup(const guint32 addr)
1920 subnet_entry_t subnet_entry;
1923 /* Search mask lengths linearly, longest first */
1925 i = SUBNETLENGTHSIZE;
1926 while(have_subnet_entry && i > 0) {
1927 guint32 masked_addr;
1928 subnet_length_entry_t* length_entry;
1930 /* Note that we run from 31 (length 32) to 0 (length 1) */
1932 g_assert(i < SUBNETLENGTHSIZE);
1935 length_entry = &subnet_length_entries[i];
1937 if(NULL != length_entry->subnet_addresses) {
1941 masked_addr = addr & length_entry->mask;
1942 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
1944 tp = length_entry->subnet_addresses[hash_idx];
1945 while(tp != NULL && tp->addr != masked_addr) {
1950 subnet_entry.mask = length_entry->mask;
1951 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
1952 subnet_entry.name = tp->name;
1953 return subnet_entry;
1958 subnet_entry.mask = 0;
1959 subnet_entry.mask_length = 0;
1960 subnet_entry.name = NULL;
1962 return subnet_entry;
1965 /* Add a subnet-definition - name pair to the set.
1966 * The definition is taken by masking the address passed in with the mask of the
1969 static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name)
1971 subnet_length_entry_t* entry;
1975 g_assert(mask_length > 0 && mask_length <= 32);
1977 entry = &subnet_length_entries[mask_length - 1];
1979 subnet_addr &= entry->mask;
1981 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
1983 if(NULL == entry->subnet_addresses) {
1984 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
1987 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
1988 if(tp->addr == subnet_addr) {
1989 return; /* XXX provide warning that an address was repeated? */
1991 hashipv4_t * new_tp = g_new(hashipv4_t,1);
1996 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
2000 tp->addr = subnet_addr;
2001 tp->is_dummy_entry = FALSE; /*Never used again...*/
2002 strncpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2003 have_subnet_entry = TRUE;
2006 static guint32 get_subnet_mask(guint32 mask_length) {
2008 static guint32 masks[SUBNETLENGTHSIZE];
2009 static gboolean initialised = FALSE;
2012 memset(masks, 0, sizeof(masks));
2016 /* XXX There must be a better way to do this than
2017 * hand-coding the values, but I can't seem to
2021 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2022 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2023 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2024 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2025 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2026 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2027 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2028 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2030 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2031 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2032 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2033 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2034 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2035 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2036 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2037 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2039 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2040 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2041 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2042 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2043 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2044 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2045 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2046 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2048 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2049 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2050 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2051 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2052 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2053 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2054 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2055 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2058 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2059 g_assert_not_reached();
2062 return masks[mask_length - 1];
2066 static void subnet_name_lookup_init()
2071 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2072 guint32 length = i + 1;
2074 subnet_length_entries[i].subnet_addresses = NULL;
2075 subnet_length_entries[i].mask_length = length;
2076 subnet_length_entries[i].mask = get_subnet_mask(length);
2079 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2080 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2081 report_open_failure(subnetspath, errno, FALSE);
2083 g_free(subnetspath);
2086 * Load the global subnets file, if we have one.
2088 subnetspath = get_datafile_path(ENAME_SUBNETS);
2089 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2090 report_open_failure(subnetspath, errno, FALSE);
2092 g_free(subnetspath);
2096 * External Functions
2100 host_name_lookup_init(void) {
2103 #ifdef HAVE_GNU_ADNS
2106 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2107 static char rootpath_ot[] = "\\hosts";
2109 #endif /*GNU_ADNS */
2112 * Load the user's hosts file, if they have one.
2114 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2115 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2116 report_open_failure(hostspath, errno, FALSE);
2121 * Load the global hosts file, if we have one.
2123 hostspath = get_datafile_path(ENAME_HOSTS);
2124 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2125 report_open_failure(hostspath, errno, FALSE);
2129 #ifdef HAVE_GNU_ADNS
2131 * We're using GNU ADNS, which doesn't check the system hosts file;
2132 * we load that file ourselves.
2136 sysroot = getenv_utf8("WINDIR");
2137 if (sysroot != NULL) {
2139 * The file should be under WINDIR.
2140 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2141 * %WINDIR%\system32\drivers\etc\hosts.
2142 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2144 * XXX - should we base it on the dwPlatformId value from
2147 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2148 if (!read_hosts_file(hostspath)) {
2150 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2151 read_hosts_file(hostspath);
2156 read_hosts_file("/etc/hosts");
2159 /* XXX - Any flags we should be using? */
2160 /* XXX - We could provide config settings for DNS servers, and
2161 pass them to ADNS with adns_init_strcfg */
2162 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2164 * XXX - should we report the error? I'm assuming that some crashes
2165 * reported on a Windows machine with TCP/IP not configured are due
2166 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2167 * ADNS in a state where it crashes due to that. We'll still try
2168 * doing name resolution anyway.
2172 gnu_adns_initialized = TRUE;
2173 adns_currently_queued = 0;
2174 #endif /* HAVE_GNU_ADNS */
2176 subnet_name_lookup_init();
2179 #ifdef HAVE_GNU_ADNS
2181 /* XXX - The ADNS "documentation" isn't very clear:
2182 * - Do we need to keep our query structures around?
2185 host_name_lookup_process(gpointer data _U_) {
2186 adns_queue_msg_t *almsg;
2188 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2194 adns_queue_head = g_list_first(adns_queue_head);
2196 cur = adns_queue_head;
2197 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
2198 almsg = (adns_queue_msg_t *) cur->data;
2199 if (! almsg->submitted && almsg->type == AF_INET) {
2200 addr_bytes = (guint8 *) &almsg->ip4_addr;
2201 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2202 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2203 /* XXX - what if it fails? */
2204 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2205 almsg->submitted = TRUE;
2206 adns_currently_queued++;
2211 cur = adns_queue_head;
2214 almsg = (adns_queue_msg_t *) cur->data;
2215 if (almsg->submitted) {
2216 ret = adns_check(ads, &almsg->query, &ans, NULL);
2218 if (ans->status == adns_s_ok) {
2219 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2226 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
2228 adns_currently_queued--;
2232 /* Keep the timeout in place */
2237 host_name_lookup_cleanup(void) {
2240 adns_queue_head = g_list_first(adns_queue_head);
2241 while (adns_queue_head) {
2242 qdata = adns_queue_head->data;
2243 adns_queue_head = g_list_remove(adns_queue_head, qdata);
2247 if (gnu_adns_initialized)
2254 host_name_lookup_process(gpointer data _U_) {
2255 /* Kill the timeout, as there's nothing for it to do */
2260 host_name_lookup_cleanup(void) {
2263 #endif /* HAVE_GNU_ADNS */
2265 extern gchar *get_hostname(guint addr)
2269 if (!(g_resolv_flags & RESOLV_NETWORK))
2270 return ip_to_str((guint8 *)&addr);
2272 return host_name_lookup(addr, &found);
2275 extern const gchar *get_hostname6(struct e_in6_addr *addr)
2279 if (!(g_resolv_flags & RESOLV_NETWORK))
2280 return ip6_to_str(addr);
2281 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
2282 return ip6_to_str(addr);
2283 return host_name_lookup6(addr, &found);
2286 extern void add_ipv4_name(guint addr, const gchar *name)
2292 hash_idx = HASH_IPV4_ADDRESS(addr);
2294 tp = ipv4_table[hash_idx];
2297 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
2300 if (tp->addr == addr) {
2301 /* address already known */
2302 if (!tp->is_dummy_entry) {
2305 /* replace this dummy entry with the new one */
2310 if (tp->next == NULL) {
2311 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
2319 g_strlcpy(tp->name, name, MAXNAMELEN);
2324 tp->is_dummy_entry = FALSE;
2326 } /* add_ipv4_name */
2328 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
2334 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2336 tp = ipv6_table[hash_idx];
2339 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2342 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2343 /* address already known */
2344 if (!tp->is_dummy_entry) {
2347 /* replace this dummy entry with the new one */
2352 if (tp->next == NULL) {
2353 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2361 g_strlcpy(tp->name, name, MAXNAMELEN);
2366 tp->is_dummy_entry = FALSE;
2368 } /* add_ipv6_name */
2370 /* -----------------
2371 * unsigned integer to ascii
2373 static gchar *ep_utoa(guint port)
2375 gchar *bp = ep_alloc(MAXNAMELEN);
2377 bp = &bp[MAXNAMELEN -1];
2381 *--bp = (port % 10) +'0';
2382 } while ((port /= 10) != 0);
2387 extern gchar *get_udp_port(guint port)
2390 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2391 return ep_utoa(port);
2394 return serv_name_lookup(port, PT_UDP);
2396 } /* get_udp_port */
2398 extern gchar *get_dccp_port(guint port)
2401 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2402 return ep_utoa(port);
2405 return serv_name_lookup(port, PT_DCCP);
2407 } /* get_dccp_port */
2410 extern gchar *get_tcp_port(guint port)
2413 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2414 return ep_utoa(port);
2417 return serv_name_lookup(port, PT_TCP);
2419 } /* get_tcp_port */
2421 extern gchar *get_sctp_port(guint port)
2424 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2425 return ep_utoa(port);
2428 return serv_name_lookup(port, PT_SCTP);
2430 } /* get_sctp_port */
2433 const gchar *get_addr_name(address *addr)
2435 const gchar *result;
2437 result = solve_address_to_name(addr);
2443 /* if it gets here, either it is of type AT_NONE, */
2444 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
2446 if (addr->type == AT_NONE){
2450 return(address_to_str(addr));
2451 } /* get_addr_name */
2454 void get_addr_name_buf(address *addr, gchar *buf, guint size)
2456 const gchar *result = get_addr_name(addr);
2458 g_snprintf(buf, size, "%s", result);
2459 } /* get_addr_name_buf */
2462 extern gchar *get_ether_name(const guint8 *addr)
2464 if (!(g_resolv_flags & RESOLV_MAC))
2465 return ether_to_str(addr);
2467 if (!eth_resolution_initialized) {
2468 initialize_ethers();
2469 eth_resolution_initialized = 1;
2472 return eth_name_lookup(addr);
2474 } /* get_ether_name */
2476 /* Look for an ether name in the hash, and return it if found.
2477 * If it's not found, simply return NULL. We DO NOT make a new
2478 * hash entry for it with the hex digits turned into a string.
2480 gchar *get_ether_name_if_known(const guint8 *addr)
2485 /* Initialize ether structs if we're the first
2486 * ether-related function called */
2487 if (!(g_resolv_flags & RESOLV_MAC))
2490 if (!eth_resolution_initialized) {
2491 initialize_ethers();
2492 eth_resolution_initialized = 1;
2495 hash_idx = HASH_ETH_ADDRESS(addr);
2497 tp = eth_table[hash_idx];
2500 /* Hash key not found in table.
2501 * Force a lookup (and a hash entry) for addr, then call
2502 * myself. I plan on not getting into an infinite loop because
2503 * eth_name_lookup() is guaranteed to make a hashtable entry,
2504 * so when I call myself again, I can never get into this
2505 * block of code again. Knock on wood...
2507 (void) eth_name_lookup(addr);
2508 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2512 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2513 if (!tp->is_dummy_entry) {
2514 /* A name was found, and its origin is an ethers file */
2518 /* A name was found, but it was created, not found in a file */
2522 if (tp->next == NULL) {
2523 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2524 (void) eth_name_lookup(addr);
2525 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2530 g_assert_not_reached();
2535 extern guint8 *get_ether_addr(const gchar *name)
2538 /* force resolution (do not check g_resolv_flags) */
2540 if (!eth_resolution_initialized) {
2541 initialize_ethers();
2542 eth_resolution_initialized = 1;
2545 return eth_addr_lookup(name);
2547 } /* get_ether_addr */
2549 extern void add_ether_byip(guint ip, const guint8 *eth)
2555 /* first check that IP address can be resolved */
2557 if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2560 /* ok, we can add this entry in the ethers hashtable */
2563 add_eth_name(eth, host);
2565 } /* add_ether_byip */
2567 extern const gchar *get_ipxnet_name(const guint32 addr)
2570 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2571 return ipxnet_to_str_punct(addr, '\0');
2574 if (!ipxnet_resolution_initialized) {
2575 initialize_ipxnets();
2576 ipxnet_resolution_initialized = 1;
2579 return ipxnet_name_lookup(addr);
2581 } /* get_ipxnet_name */
2583 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2588 /* force resolution (do not check g_resolv_flags) */
2590 if (!ipxnet_resolution_initialized) {
2591 initialize_ipxnets();
2592 ipxnet_resolution_initialized = 1;
2595 addr = ipxnet_addr_lookup(name, &success);
2600 } /* get_ipxnet_addr */
2602 extern const gchar *get_manuf_name(const guint8 *addr)
2605 hashmanuf_t *manufp;
2607 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2608 initialize_ethers();
2609 eth_resolution_initialized = 1;
2612 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2613 cur=ep_alloc(MAXMANUFLEN);
2614 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2618 return manufp->name;
2620 } /* get_manuf_name */
2623 const gchar *get_manuf_name_if_known(const guint8 *addr)
2625 hashmanuf_t *manufp;
2627 if (!eth_resolution_initialized) {
2628 initialize_ethers();
2629 eth_resolution_initialized = 1;
2632 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2636 return manufp->name;
2638 } /* get_manuf_name_if_known */
2641 /* Translate a string, assumed either to be a dotted-quad IP address or
2642 * a host name, to a numeric IP address. Return TRUE if we succeed and
2643 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2644 * Used more in the dfilter parser rather than in packet dissectors */
2645 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2647 struct in_addr ipaddr;
2651 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2652 * inet_pton(AF_INET) does not support hexadecimal notation nor
2653 * less-than-4 octet notation.
2655 if (!inet_aton(host, &ipaddr)) {
2656 /* It's not a valid dotted-quad IP address; is it a valid
2658 hp = gethostbyname(host);
2662 /* Apparently, some versions of gethostbyaddr can
2663 * return IPv6 addresses. */
2664 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2665 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2670 /* Does the string really contain dotted-quad IP?
2671 * Check against inet_atons that accept strings such as
2672 * "130.230" as valid addresses and try to convert them
2673 * to some form of a classful (host.net) notation.
2675 unsigned int a0, a1, a2, a3;
2676 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2680 *addrp = g_ntohl(ipaddr.s_addr);
2685 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2686 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2687 * return FALSE if we fail.
2689 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2693 if (inet_pton(AF_INET6, host, addrp) == 1)
2697 #ifdef HAVE_GETHOSTBYNAME2
2698 hp = gethostbyname2(host, AF_INET6);
2702 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2703 memcpy(addrp, hp->h_addr, hp->h_length);
2711 * Find out whether a hostname resolves to an ip or ipv6 address
2712 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2713 * that we don't know)
2715 const char* host_ip_af(const char *host
2716 #ifndef HAVE_GETHOSTBYNAME2
2721 #ifdef HAVE_GETHOSTBYNAME2
2723 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";