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....)
70 #ifdef HAVE_NETINET_IN_H
71 # include <netinet/in.h>
78 #ifdef HAVE_ARPA_INET_H
79 #include <arpa/inet.h>
84 #ifdef HAVE_SYS_SOCKET_H
85 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
88 #ifdef HAVE_WINSOCK2_H
89 #include <winsock2.h> /* needed to define AF_ values on Windows */
92 #ifdef NEED_INET_ATON_H
93 # include "wsutil/inet_aton.h"
96 #ifdef NEED_INET_V6DEFS_H
97 # include "wsutil/inet_v6defs.h"
100 #if defined(_WIN32) && defined(INET6)
101 # include <ws2tcpip.h>
105 # if defined(_WIN32) && !defined(INET6)
106 # define socklen_t unsigned int
109 # include <ares_version.h>
111 # ifdef HAVE_GNU_ADNS
114 # if defined(inet_aton) && defined(_WIN32)
117 # endif /* HAVE_GNU_ADNS */
118 #endif /* HAVE_C_ARES */
123 #include "report_err.h"
125 #include "ipv6-utils.h"
126 #include "addr_resolv.h"
127 #include "filesystem.h"
129 #include <epan/strutil.h>
130 #include <wsutil/file_util.h>
131 #include <epan/prefs.h>
132 #include <epan/emem.h>
134 #define ENAME_HOSTS "hosts"
135 #define ENAME_SUBNETS "subnets"
136 #define ENAME_ETHERS "ethers"
137 #define ENAME_IPXNETS "ipxnets"
138 #define ENAME_MANUF "manuf"
139 #define ENAME_SERVICES "services"
141 #define HASHETHSIZE 2048
142 #define HASHHOSTSIZE 2048
143 #define HASHIPXNETSIZE 256
144 #define HASHMANUFSIZE 256
145 #define HASHPORTSIZE 256
146 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
148 /* hash table used for IPv4 lookup */
150 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
153 * XXX Some of this is duplicated in addrinfo_list. We may want to replace the
154 * addr and name parts with a struct addrinfo or create our own addrinfo-like
155 * struct that simply points to the data below.
157 typedef struct hashipv4 {
159 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
160 gboolean resolve; /* already tried to resolve it */
161 struct hashipv4 *next;
163 gchar name[MAXNAMELEN];
166 /* hash table used for IPv6 lookup */
168 #define HASH_IPV6_ADDRESS(addr) \
169 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
171 typedef struct hashipv6 {
172 struct e_in6_addr addr;
173 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
174 gboolean resolve; /* */
175 struct hashipv6 *next;
176 gchar ip6[MAX_IP6_STR_LEN]; /* XX */
177 gchar name[MAXNAMELEN];
180 /* Array of entries of subnets of different lengths */
182 gsize mask_length; /*1-32*/
183 guint32 mask; /* e.g. 255.255.255.*/
184 hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
185 } subnet_length_entry_t;
187 /* hash table used for TCP/UDP/SCTP port lookup */
189 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
191 typedef struct hashport {
193 struct hashport *next;
194 gchar name[MAXNAMELEN];
197 /* hash table used for IPX network lookup */
199 /* XXX - check goodness of hash function */
201 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
203 typedef struct hashipxnet {
205 struct hashipxnet *next;
206 gchar name[MAXNAMELEN];
209 /* hash tables used for ethernet and manufacturer lookup */
211 #define HASH_ETH_ADDRESS(addr) \
212 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
215 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
217 typedef struct hashmanuf {
218 struct hashmanuf *next;
223 #define HASHETHER_STATUS_UNRESOLVED 1
224 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
225 #define HASHETHER_STATUS_RESOLVED_NAME 3
227 typedef struct hashether {
228 struct hashether *next;
229 guint status; /* (See above) */
232 char resolved_name[MAXNAMELEN];
235 typedef struct hashwka {
236 struct hashwka *next;
238 char name[MAXNAMELEN];
241 /* internal ethernet type */
243 typedef struct _ether
246 char name[MAXNAMELEN];
249 /* internal ipxnet type */
251 typedef struct _ipxnet
254 char name[MAXNAMELEN];
257 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
258 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
260 static hashport_t **cb_port_table;
261 static gchar *cb_service;
263 static hashport_t *udp_port_table[HASHPORTSIZE];
264 static hashport_t *tcp_port_table[HASHPORTSIZE];
265 static hashport_t *sctp_port_table[HASHPORTSIZE];
266 static hashport_t *dccp_port_table[HASHPORTSIZE];
267 static hashether_t *eth_table[HASHETHSIZE];
268 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
269 static hashwka_t *(*wka_table[48])[HASHETHSIZE];
270 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
272 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
273 static gboolean have_subnet_entry = FALSE;
275 static gboolean eth_resolution_initialized = FALSE;
276 static int ipxnet_resolution_initialized = 0;
277 static int service_resolution_initialized = 0;
278 static gboolean new_resolved_objects = FALSE;
280 static struct addrinfo *addrinfo_list = NULL; /* IPv4 and IPv6 */
281 static struct addrinfo *addrinfo_list_last = NULL;
283 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
284 static void add_serv_port_cb(const guint32 port);
287 * Flag controlling what names to resolve.
289 guint32 gbl_resolv_flags;
292 * Global variables (can be changed in GUI sections)
293 * XXX - they could be changed in GUI code, but there's currently no
294 * GUI code to change them.
297 gchar *g_ethers_path = NULL; /* global ethers file */
298 gchar *g_pethers_path = NULL; /* personal ethers file */
299 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
300 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
301 gchar *g_services_path = NULL; /* global services file */
302 gchar *g_pservices_path = NULL; /* personal services file */
303 /* first resolving call */
308 * Submitted queries trigger a callback (c_ares_ghba_cb()).
309 * Queries are added to c_ares_queue_head. During processing, queries are
310 * popped off the front of c_ares_queue_head and submitted using
311 * ares_gethostbyaddr().
312 * The callback processes the response, then frees the request.
315 typedef struct _async_dns_queue_msg
319 struct e_in6_addr ip6;
322 } async_dns_queue_msg_t;
324 typedef struct _async_hostent {
330 #if ( ( ARES_VERSION_MAJOR < 1 ) \
331 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
332 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
334 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
337 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
338 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
345 * Submitted queries have to be checked individually using adns_check().
346 * Queries are added to adns_queue_head. During processing, the list is
347 * iterated twice: once to request queries up to the concurrency limit,
348 * and once to check the status of each query.
353 typedef struct _async_dns_queue_msg
359 } async_dns_queue_msg_t;
361 #endif /* HAVE_GNU_ADNS */
362 #endif /* HAVE_C_ARES */
364 static gboolean async_dns_initialized = FALSE;
365 static int async_dns_in_flight = 0;
366 static GList *async_dns_queue_head = NULL;
368 /* push a dns request */
370 add_async_dns_ipv4(int type, guint32 addr)
372 async_dns_queue_msg_t *msg;
374 msg = g_malloc(sizeof(async_dns_queue_msg_t));
377 msg->addr.ip4 = addr;
380 msg->ip4_addr = addr;
381 msg->submitted = FALSE;
383 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
391 const gchar* name; /* Shallow copy */
395 * Miscellaneous functions
399 fgetline(char **buf, int *size, FILE *fp)
404 if (fp == NULL || buf == NULL)
411 *buf = g_malloc(*size);
421 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
422 if (len+1 >= *size) {
423 *buf = g_realloc(*buf, *size += BUFSIZ);
428 if (len == 0 && c == EOF)
439 * Local function definitions
441 static subnet_entry_t subnet_lookup(const guint32 addr);
442 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
446 add_service_name(hashport_t **proto_table, const guint port, const char *service_name)
452 hash_idx = HASH_PORT(port);
453 tp = proto_table[hash_idx];
456 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
459 if( tp->port == port ) {
462 if (tp->next == NULL) {
463 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
471 /* fill in a new entry */
475 g_strlcpy(tp->name, service_name, MAXNAMELEN);
477 new_resolved_objects = TRUE;
482 parse_service_line (char *line)
485 * See the services(4) or services(5) man page for services file format
486 * (not available on all systems).
493 range_t *port_rng = NULL;
494 guint32 max_port = MAX_UDP_PORT;
496 if ((cp = strchr(line, '#')))
499 if ((cp = strtok(line, " \t")) == NULL)
504 if ((cp = strtok(NULL, " \t")) == NULL)
509 if (strtok(cp, "/") == NULL)
512 if ((cp = strtok(NULL, "/")) == NULL)
515 /* seems we got all interesting things from the file */
516 if(strcmp(cp, "tcp") == 0) {
517 max_port = MAX_TCP_PORT;
518 cb_port_table = tcp_port_table;
520 else if(strcmp(cp, "udp") == 0) {
521 max_port = MAX_UDP_PORT;
522 cb_port_table = udp_port_table;
524 else if(strcmp(cp, "sctp") == 0) {
525 max_port = MAX_SCTP_PORT;
526 cb_port_table = sctp_port_table;
528 else if(strcmp(cp, "dccp") == 0) {
529 max_port = MAX_DCCP_PORT;
530 cb_port_table = dccp_port_table;
535 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
536 /* some assertion here? */
540 cb_service = service;
541 range_foreach(port_rng, add_serv_port_cb);
543 } /* parse_service_line */
547 add_serv_port_cb(const guint32 port)
550 add_service_name(cb_port_table, port, cb_service);
556 parse_services_file(const char * path)
560 static char *buf = NULL;
562 /* services hash table initialization */
563 serv_p = ws_fopen(path, "r");
568 while (fgetline(&buf, &size, serv_p) >= 0) {
569 parse_service_line (buf);
576 initialize_services(void)
579 /* the hash table won't ignore duplicates, so use the personal path first */
581 /* set personal services path */
582 if (g_pservices_path == NULL)
583 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
585 parse_services_file(g_pservices_path);
587 /* Compute the pathname of the services file. */
588 if (g_services_path == NULL) {
589 g_services_path = get_datafile_path(ENAME_SERVICES);
592 parse_services_file(g_services_path);
594 } /* initialize_services */
599 *serv_name_lookup(const guint port, const port_type proto)
604 const char *serv_proto = NULL;
605 struct servent *servp;
608 if (!service_resolution_initialized) {
609 initialize_services();
610 service_resolution_initialized = 1;
615 table = udp_port_table;
619 table = tcp_port_table;
623 table = sctp_port_table;
627 table = dccp_port_table;
631 /* not yet implemented */
636 hash_idx = HASH_PORT(port);
637 tp = table[hash_idx];
640 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
643 if( tp->port == port ) {
646 if (tp->next == NULL) {
647 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
655 /* fill in a new entry */
659 if (!(gbl_resolv_flags & RESOLV_TRANSPORT) ||
660 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
662 guint32_to_str_buf(port, tp->name, MAXNAMELEN);
664 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
669 } /* serv_name_lookup */
672 /* Fill in an IP4 structure with info from subnets file or just with the
673 * string form of the address.
676 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
678 subnet_entry_t subnet_entry;
680 if (tp->is_dummy_entry)
681 return; /* already done */
683 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
685 /* Do we have a subnet for this address? */
686 subnet_entry = subnet_lookup(addr);
687 if(0 != subnet_entry.mask) {
688 /* Print name, then '.' then IP address after subnet mask */
690 gchar buffer[MAX_IP_STR_LEN];
694 host_addr = addr & (~(guint32)subnet_entry.mask);
695 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
698 /* Skip to first octet that is not totally masked
699 * If length of mask is 32, we chomp the whole address.
700 * If the address string starts '.' (should not happen?),
703 i = subnet_entry.mask_length / 8;
704 while(*(paddr) != '\0' && i > 0) {
705 if(*(++paddr) == '.') {
710 /* There are more efficient ways to do this, but this is safe if we
711 * trust g_snprintf and MAXNAMELEN
713 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
715 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
722 #if ( ( ARES_VERSION_MAJOR < 1 ) \
723 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
724 c_ares_ghba_cb(void *arg, int status, struct hostent *he) {
726 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
728 async_dns_queue_msg_t *caqm = arg;
732 async_dns_in_flight--;
734 if (status == ARES_SUCCESS) {
735 for (p = he->h_addr_list; *p != NULL; p++) {
736 switch(caqm->family) {
738 add_ipv4_name(caqm->addr.ip4, he->h_name);
741 add_ipv6_name(&caqm->addr.ip6, he->h_name);
744 /* Throw an exception? */
751 #endif /* HAVE_C_ARES */
753 /* --------------- */
755 new_ipv4(const guint addr)
757 hashipv4_t *tp = g_malloc(sizeof(hashipv4_t));
761 tp->is_dummy_entry = FALSE;
762 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
767 host_lookup(const guint addr, const gboolean resolve, gboolean *found)
770 hashipv4_t * volatile tp;
771 struct hostent *hostp;
775 hash_idx = HASH_IPV4_ADDRESS(addr);
777 tp = ipv4_table[hash_idx];
780 tp = ipv4_table[hash_idx] = new_ipv4(addr);
783 if( tp->addr == addr ) {
784 if (tp->is_dummy_entry && !tp->resolve)
786 if (tp->is_dummy_entry)
790 if (tp->next == NULL) {
791 tp->next = new_ipv4(addr);
802 if ((gbl_resolv_flags & RESOLV_CONCURRENT) &&
803 prefs.name_resolve_concurrency > 0 &&
804 async_dns_initialized) {
805 add_async_dns_ipv4(AF_INET, addr);
806 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
807 * going to risk changing the semantics.
809 fill_dummy_ip4(addr, tp);
812 #endif /* ASYNC_DNS */
815 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
816 * the name of the host on which it's running; to work around that
817 * botch, we don't try to translate an all-zero IP address to a host
820 if (addr != 0 && (gbl_resolv_flags & RESOLV_NETWORK)) {
821 /* Use async DNS if possible, else fall back to timeouts,
822 * else call gethostbyaddr and hope for the best
825 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
828 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
829 tp->is_dummy_entry = FALSE;
834 /* unknown host or DNS timeout */
840 fill_dummy_ip4(addr, tp);
843 } /* host_name_lookup */
846 host_name_lookup(const guint addr, gboolean *found)
849 tp = host_lookup(addr, TRUE, found);
854 /* --------------- */
856 new_ipv6(const struct e_in6_addr *addr)
858 hashipv6_t *tp = g_malloc(sizeof(hashipv6_t));
862 tp->is_dummy_entry = FALSE;
863 ip6_to_str_buf(addr, tp->ip6);
867 /* ------------------------------------ */
869 host_lookup6(const struct e_in6_addr *addr, const gboolean resolve, gboolean *found)
872 hashipv6_t * volatile tp;
875 async_dns_queue_msg_t *caqm;
876 #endif /* HAVE_C_ARES */
877 struct hostent *hostp;
882 hash_idx = HASH_IPV6_ADDRESS(*addr);
884 tp = ipv6_table[hash_idx];
887 tp = ipv6_table[hash_idx] = new_ipv6(addr);
890 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
891 if (tp->is_dummy_entry && !tp->resolve)
893 if (tp->is_dummy_entry)
897 if (tp->next == NULL) {
898 tp->next = new_ipv6(addr);
911 if ((gbl_resolv_flags & RESOLV_CONCURRENT) &&
912 prefs.name_resolve_concurrency > 0 &&
913 async_dns_initialized) {
914 caqm = g_malloc(sizeof(async_dns_queue_msg_t));
915 caqm->family = AF_INET6;
916 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
917 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
919 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
920 * going to risk changing the semantics.
922 if (!tp->is_dummy_entry) {
923 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
924 ip6_to_str_buf(addr, tp->name);
925 tp->is_dummy_entry = TRUE;
929 #endif /* HAVE_C_ARES */
931 /* Quick hack to avoid DNS/YP timeout */
932 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
935 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
936 tp->is_dummy_entry = FALSE;
942 /* unknown host or DNS timeout */
943 if (!tp->is_dummy_entry) {
944 tp->is_dummy_entry = TRUE;
945 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
954 host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
957 tp = host_lookup6(addr, TRUE, found);
963 solve_address_to_name(const address *addr)
965 switch (addr->type) {
968 return get_ether_name(addr->data);
972 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
973 return get_hostname(ip4_addr);
977 struct e_in6_addr ip6_addr;
978 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
979 return get_hostname6(&ip6_addr);
991 se_solve_address_to_name(const address *addr)
993 switch (addr->type) {
996 return get_ether_name(addr->data);
1000 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
1001 return get_hostname(ip4_addr);
1005 struct e_in6_addr ip6_addr;
1006 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
1007 return get_hostname6(&ip6_addr);
1011 return se_strdup(addr->data);
1019 * Ethernet / manufacturer resolution
1021 * The following functions implement ethernet address resolution and
1022 * ethers files parsing (see ethers(4)).
1024 * The manuf file has the same format as ethers(4) except that names are
1025 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1026 * only 3 bytes (instead of 6).
1030 * I decide to not use the existing functions (see ethers(3) on some
1031 * operating systems) for the following reasons:
1032 * - performance gains (use of hash tables and some other enhancements),
1033 * - use of two ethers files (system-wide and per user),
1034 * - avoid the use of NIS maps,
1035 * - lack of these functions on some systems.
1037 * So the following functions do _not_ behave as the standard ones.
1044 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1045 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1049 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1050 const gboolean manuf_file)
1057 for (i = 0; i < 6; i++) {
1058 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1059 if (!isxdigit((unsigned char)*cp))
1061 num = strtoul(cp, &p, 16);
1063 return FALSE; /* failed */
1065 return FALSE; /* not a valid octet */
1066 eth->addr[i] = (guint8) num;
1067 cp = p; /* skip past the number */
1069 /* OK, what character terminated the octet? */
1071 /* "/" - this has a mask. */
1073 /* Entries with masks are allowed only in the "manuf" files. */
1076 cp++; /* skip past the '/' to get to the mask */
1077 if (!isdigit((unsigned char)*cp))
1078 return FALSE; /* no sign allowed */
1079 num = strtoul(cp, &p, 10);
1081 return FALSE; /* failed */
1082 cp = p; /* skip past the number */
1083 if (*cp != '\0' && !isspace((unsigned char)*cp))
1084 return FALSE; /* bogus terminator */
1085 if (num == 0 || num >= 48)
1086 return FALSE; /* bogus mask */
1087 /* Mask out the bits not covered by the mask */
1089 for (i = 0; num >= 8; i++, num -= 8)
1090 ; /* skip octets entirely covered by the mask */
1091 /* Mask out the first masked octet */
1092 eth->addr[i] &= (0xFF << (8 - num));
1094 /* Mask out completely-masked-out octets */
1100 /* We're at the end of the address, and there's no mask. */
1102 /* We got 3 bytes, so this is a manufacturer ID. */
1104 /* Manufacturer IDs are only allowed in the "manuf"
1108 /* Indicate that this is a manufacturer ID (0 is not allowed
1115 /* We got 6 bytes, so this is a MAC address.
1116 If we're reading one of the "manuf" files, indicate that
1117 this is a MAC address (48 is not allowed as a mask). */
1123 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1128 /* We don't know the separator used in this number; it can either
1129 be ':', '-', or '.'. */
1130 if (*cp != ':' && *cp != '-' && *cp != '.')
1132 sep = *cp; /* subsequent separators must be the same */
1134 /* It has to be the same as the first separator */
1146 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1147 const gboolean manuf_file)
1150 * See the ethers(4) or ethers(5) man page for ethers file format
1151 * (not available on all systems).
1152 * We allow both ethernet address separators (':' and '-'),
1153 * as well as Wireshark's '.' separator.
1158 if ((cp = strchr(line, '#')))
1161 if ((cp = strtok(line, " \t")) == NULL)
1164 if (!parse_ether_address(cp, eth, mask, manuf_file))
1167 if ((cp = strtok(NULL, " \t")) == NULL)
1170 g_strlcpy(eth->name, cp, MAXNAMELEN);
1174 } /* parse_ether_line */
1176 static FILE *eth_p = NULL;
1179 set_ethent(char *path)
1184 eth_p = ws_fopen(path, "r");
1197 get_ethent(unsigned int *mask, const gboolean manuf_file)
1201 static int size = 0;
1202 static char *buf = NULL;
1207 while (fgetline(&buf, &size, eth_p) >= 0) {
1208 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1218 get_ethbyname(const gchar *name)
1222 set_ethent(g_pethers_path);
1224 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1230 set_ethent(g_ethers_path);
1232 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1240 } /* get_ethbyname */
1243 get_ethbyaddr(const guint8 *addr)
1248 set_ethent(g_pethers_path);
1250 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1256 set_ethent(g_ethers_path);
1258 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1266 } /* get_ethbyaddr */
1269 hash_eth_wka(const guint8 *addr, unsigned int mask)
1272 /* All but the topmost byte is masked out */
1273 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
1277 /* All but the topmost 2 bytes are masked out */
1278 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1283 /* All but the topmost 3 bytes are masked out */
1284 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1285 & (HASHETHSIZE - 1);
1289 /* All but the topmost 4 bytes are masked out */
1290 return ((((addr[0] << 8) | addr[1]) ^
1291 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1296 /* All but the topmost 5 bytes are masked out */
1297 return ((((addr[1] << 8) | addr[2]) ^
1298 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1302 /* No bytes are fully masked out */
1303 return ((((addr[1] << 8) | addr[2]) ^
1304 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1308 static hashmanuf_t *
1309 manuf_hash_new_entry(const guint8 *addr, gchar *name)
1313 mtp = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1314 memcpy(mtp->addr, addr, sizeof(mtp->addr));
1315 /* The length of this name is limited (in the number of UTF-8 characters,
1316 * not bytes) in make-manuf. That doesn't mean a user can't put a longer
1317 * name in their personal manuf file, though...
1319 mtp->name = g_strdup(name);
1322 } /* manuf_hash_new_entry */
1325 wka_hash_new_entry(const guint8 *addr, gchar *name)
1329 wtp = (hashwka_t *)g_malloc(sizeof(hashwka_t));
1330 memcpy(wtp->addr, addr, sizeof(wtp->addr));
1331 g_strlcpy(wtp->name, name, MAXNAMELEN);
1334 } /* wka_hash_new_entry */
1337 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1341 hashwka_t *(*wka_tp)[HASHETHSIZE], *wtp;
1344 * XXX - can we use Standard Annotation Language annotations to
1345 * note that mask, as returned by parse_ether_address() (and thus
1346 * by the routines that call it, and thus passed to us) cannot be > 48,
1347 * or is SAL too weak to express that?
1350 /* This is a well-known MAC address; just add this to the Ethernet
1352 add_eth_name(addr, name);
1357 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1359 hash_idx = HASH_ETH_MANUF(addr);
1360 mtp = manuf_table[hash_idx];
1363 manuf_table[hash_idx] = manuf_hash_new_entry(addr, name);
1367 if (mtp->next == NULL) {
1368 mtp->next = manuf_hash_new_entry(addr, name);
1376 /* This is a range of well-known addresses; add it to the appropriate
1377 well-known-address table, creating that table if necessary. */
1378 wka_tp = wka_table[mask];
1380 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1382 hash_idx = hash_eth_wka(addr, mask);
1384 wtp = (*wka_tp)[hash_idx];
1387 (*wka_tp)[hash_idx] = wka_hash_new_entry(addr, name);
1391 if (memcmp(wtp->addr, addr, sizeof(wtp->addr)) == 0) {
1392 /* address already known */
1395 if (wtp->next == NULL) {
1396 wtp->next = wka_hash_new_entry(addr, name);
1402 } /* add_manuf_name */
1404 static hashmanuf_t *
1405 manuf_name_lookup(const guint8 *addr)
1409 guint8 stripped_addr[3];
1411 hash_idx = HASH_ETH_MANUF(addr);
1413 /* first try to find a "perfect match" */
1414 mtp = manuf_table[hash_idx];
1415 while(mtp != NULL) {
1416 if (memcmp(mtp->addr, addr, sizeof(mtp->addr)) == 0) {
1422 /* Mask out the broadcast/multicast flag but not the locally
1423 * administered flag as localy administered means: not assigend
1424 * by the IEEE but the local administrator instead.
1425 * 0x01 multicast / broadcast bit
1426 * 0x02 locally administered bit */
1427 memcpy(stripped_addr, addr, 3);
1428 stripped_addr[0] &= 0xFE;
1430 mtp = manuf_table[hash_idx];
1431 while(mtp != NULL) {
1432 if (memcmp(mtp->addr, stripped_addr, sizeof(mtp->addr)) == 0) {
1440 } /* manuf_name_lookup */
1443 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1446 hashwka_t *(*wka_tp)[HASHETHSIZE];
1448 guint8 masked_addr[6];
1452 wka_tp = wka_table[mask];
1453 if (wka_tp == NULL) {
1454 /* There are no entries in the table for that mask value, as there is
1455 no table for that mask value. */
1459 /* Get the part of the address covered by the mask. */
1460 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1461 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1462 /* Mask out the first masked octet */
1463 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1465 /* Zero out completely-masked-out octets */
1469 hash_idx = hash_eth_wka(masked_addr, mask);
1471 wtp = (*wka_tp)[hash_idx];
1473 while(wtp != NULL) {
1474 if (memcmp(wtp->addr, masked_addr, sizeof(wtp->addr)) == 0) {
1482 } /* wka_name_lookup */
1485 initialize_ethers(void)
1491 /* Compute the pathname of the ethers file. */
1492 if (g_ethers_path == NULL) {
1493 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1494 get_systemfile_dir(), ENAME_ETHERS);
1497 /* Set g_pethers_path here, but don't actually do anything
1498 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1500 if (g_pethers_path == NULL)
1501 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1503 /* manuf hash table initialization */
1505 /* Compute the pathname of the manuf file */
1506 manuf_path = get_datafile_path(ENAME_MANUF);
1508 /* Read it and initialize the hash table */
1509 set_ethent(manuf_path);
1511 while ((eth = get_ethent(&mask, TRUE))) {
1512 add_manuf_name(eth->addr, mask, eth->name);
1519 } /* initialize_ethers */
1521 /* Resolve ethernet address */
1522 static hashether_t *
1523 eth_addr_resolve(hashether_t *tp) {
1525 const guint8 *addr = tp->addr;
1527 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1528 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1529 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1536 /* Unknown name. Try looking for it in the well-known-address
1537 tables for well-known address ranges smaller than 2^24. */
1540 /* Only the topmost 5 bytes participate fully */
1541 if ((wtp = wka_name_lookup(addr, mask+40)) != NULL) {
1542 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1543 wtp->name, addr[5] & (0xFF >> mask));
1544 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1554 /* Only the topmost 4 bytes participate fully */
1555 if ((wtp = wka_name_lookup(addr, mask+32)) != NULL) {
1556 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1557 wtp->name, addr[4] & (0xFF >> mask), addr[5]);
1558 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1568 /* Only the topmost 3 bytes participate fully */
1569 if ((wtp = wka_name_lookup(addr, mask+24)) != NULL) {
1570 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1571 wtp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1572 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1580 /* Now try looking in the manufacturer table. */
1581 if ((mtp = manuf_name_lookup(addr)) != NULL) {
1582 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1583 mtp->name, addr[3], addr[4], addr[5]);
1584 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1588 /* Now try looking for it in the well-known-address
1589 tables for well-known address ranges larger than 2^24. */
1592 /* Only the topmost 2 bytes participate fully */
1593 if ((wtp = wka_name_lookup(addr, mask+16)) != NULL) {
1594 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1595 wtp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1597 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1607 /* Only the topmost byte participates fully */
1608 if ((wtp = wka_name_lookup(addr, mask+8)) != NULL) {
1609 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1610 wtp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1612 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1620 for (mask = 7; mask > 0; mask--) {
1621 /* Not even the topmost byte participates fully */
1622 if ((wtp = wka_name_lookup(addr, mask)) != NULL) {
1623 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1624 wtp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1625 addr[3], addr[4], addr[5]);
1626 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1631 /* No match whatsoever. */
1632 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s", ether_to_str(addr));
1633 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1636 g_assert_not_reached();
1637 } /* eth_addr_resolve */
1639 static hashether_t *
1640 eth_hash_new_entry(const guint8 *addr, const gboolean resolve) {
1643 tp = (hashether_t *)g_malloc(sizeof(hashether_t));
1644 memcpy(tp->addr, addr, sizeof(tp->addr));
1645 tp->status = HASHETHER_STATUS_UNRESOLVED;
1646 g_strlcpy(tp->hexaddr, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexaddr));
1647 tp->resolved_name[0] = '\0';
1651 eth_addr_resolve(tp);
1654 } /* eth_hash_new_entry */
1656 static hashether_t *
1657 add_eth_name(const guint8 *addr, const gchar *name)
1662 hash_idx = HASH_ETH_ADDRESS(addr);
1664 tp = eth_table[hash_idx];
1666 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, FALSE);
1669 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1670 /* address already known */
1671 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME)
1672 return tp; /* Entry with a name already in table; ignore attempted replacement */
1673 break; /* Update name of existing entry */
1675 if (tp->next == NULL) {
1676 tp = tp->next = eth_hash_new_entry(addr, FALSE);
1683 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1684 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1685 new_resolved_objects = TRUE;
1688 } /* add_eth_name */
1690 static hashether_t *
1691 eth_name_lookup(const guint8 *addr, const gboolean resolve) {
1695 hash_idx = HASH_ETH_ADDRESS(addr);
1697 tp = eth_table[hash_idx];
1699 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, resolve);
1703 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1704 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED))
1705 eth_addr_resolve(tp); /* Found but needs to be resolved */
1708 if (tp->next == NULL) {
1709 tp->next = eth_hash_new_entry(addr, resolve);
1715 } /* eth_name_lookup */
1718 eth_addr_lookup(const gchar *name)
1722 hashether_t **table = eth_table;
1725 /* to be optimized (hash table from name to addr) */
1726 for (i = 0; i < HASHETHSIZE; i++) {
1729 if (strcmp(tp->resolved_name, name) == 0)
1735 /* not in hash table : performs a file lookup */
1737 if ((eth = get_ethbyname(name)) == NULL)
1740 /* add new entry in hash table */
1742 tp = add_eth_name(eth->addr, name);
1746 } /* eth_addr_lookup */
1751 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1754 * We allow three address separators (':', '-', and '.'),
1755 * as well as no separators
1759 guint32 a, a0, a1, a2, a3;
1760 gboolean found_single_number = FALSE;
1762 if ((cp = strchr(line, '#')))
1765 if ((cp = strtok(line, " \t\n")) == NULL)
1768 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1769 * fill a and found_single_number is TRUE,
1772 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1773 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1774 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1775 if (sscanf(cp, "%x", &a) == 1) {
1776 found_single_number = TRUE;
1785 if ((cp = strtok(NULL, " \t\n")) == NULL)
1788 if (found_single_number) {
1792 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1795 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1799 } /* parse_ipxnets_line */
1801 static FILE *ipxnet_p = NULL;
1804 set_ipxnetent(char *path)
1809 ipxnet_p = ws_fopen(path, "r");
1825 static ipxnet_t ipxnet;
1826 static int size = 0;
1827 static char *buf = NULL;
1829 if (ipxnet_p == NULL)
1832 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1833 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1840 } /* get_ipxnetent */
1843 get_ipxnetbyname(const gchar *name)
1847 set_ipxnetent(g_ipxnets_path);
1849 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1852 if (ipxnet == NULL) {
1855 set_ipxnetent(g_pipxnets_path);
1857 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1865 } /* get_ipxnetbyname */
1868 get_ipxnetbyaddr(guint32 addr)
1872 set_ipxnetent(g_ipxnets_path);
1874 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1876 if (ipxnet == NULL) {
1879 set_ipxnetent(g_pipxnets_path);
1881 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1889 } /* get_ipxnetbyaddr */
1892 initialize_ipxnets(void)
1894 /* Compute the pathname of the ipxnets file.
1896 * XXX - is there a notion of an "ipxnets file" in any flavor of
1897 * UNIX, or with any add-on Netware package for UNIX? If not,
1898 * should the UNIX version of the ipxnets file be in the datafile
1899 * directory as well?
1901 if (g_ipxnets_path == NULL) {
1902 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1903 get_systemfile_dir(), ENAME_IPXNETS);
1906 /* Set g_pipxnets_path here, but don't actually do anything
1907 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1909 if (g_pipxnets_path == NULL)
1910 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1912 } /* initialize_ipxnets */
1914 static hashipxnet_t *
1915 add_ipxnet_name(guint addr, const gchar *name)
1920 hash_idx = HASH_IPX_NET(addr);
1922 tp = ipxnet_table[hash_idx];
1925 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1928 if (tp->next == NULL) {
1929 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1938 g_strlcpy(tp->name, name, MAXNAMELEN);
1940 new_resolved_objects = TRUE;
1944 } /* add_ipxnet_name */
1947 ipxnet_name_lookup(const guint addr)
1953 hash_idx = HASH_IPX_NET(addr);
1955 tp = ipxnet_table[hash_idx];
1958 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1961 if (tp->addr == addr) {
1964 if (tp->next == NULL) {
1965 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1973 /* fill in a new entry */
1978 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1980 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1983 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1988 } /* ipxnet_name_lookup */
1991 ipxnet_addr_lookup(const gchar *name, gboolean *success)
1995 hashipxnet_t **table = ipxnet_table;
1998 /* to be optimized (hash table from name to addr) */
1999 for (i = 0; i < HASHIPXNETSIZE; i++) {
2002 if (strcmp(tp->name, name) == 0) {
2010 /* not in hash table : performs a file lookup */
2012 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
2017 /* add new entry in hash table */
2019 tp = add_ipxnet_name(ipxnet->addr, name);
2024 } /* ipxnet_addr_lookup */
2027 read_hosts_file (const char *hostspath)
2033 guint32 host_addr[4]; /* IPv4 or IPv6 */
2034 struct e_in6_addr ip6_addr;
2039 * See the hosts(4) or hosts(5) man page for hosts file format
2040 * (not available on all systems).
2042 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2045 while (fgetline(&line, &size, hf) >= 0) {
2046 if ((cp = strchr(line, '#')))
2049 if ((cp = strtok(line, " \t")) == NULL)
2050 continue; /* no tokens in the line */
2052 ret = inet_pton(AF_INET6, cp, &host_addr);
2054 continue; /* error parsing */
2059 /* Not valid IPv6 - valid IPv4? */
2060 if (inet_pton(AF_INET, cp, &host_addr) != 1)
2065 if ((cp = strtok(NULL, " \t")) == NULL)
2066 continue; /* no host name */
2069 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2070 add_ipv6_name(&ip6_addr, cp);
2072 add_ipv4_name(host_addr[0], cp);
2075 * Add the aliases, too, if there are any.
2076 * XXX - host_lookup() only returns the first entry.
2078 while ((cp = strtok(NULL, " \t")) != NULL) {
2080 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2081 add_ipv6_name(&ip6_addr, cp);
2083 add_ipv4_name(host_addr[0], cp);
2090 } /* read_hosts_file */
2093 add_ip_name_from_string (const char *addr, const char *name)
2095 guint32 host_addr[4]; /* IPv4 */
2096 struct e_in6_addr ip6_addr; /* IPv6 */
2100 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2102 /* Error parsing address */
2109 /* Not valid IPv6 - valid IPv4? */
2110 if (inet_pton(AF_INET, addr, &host_addr) != 1)
2111 return FALSE; /* no */
2116 add_ipv6_name(&ip6_addr, name);
2118 add_ipv4_name(host_addr[0], name);
2122 } /* add_ip_name_from_string */
2125 get_addrinfo_list(void) {
2126 return addrinfo_list;
2129 /* Read in a list of subnet definition - name pairs.
2130 * <line> = <comment> | <entry> | <whitespace>
2131 * <comment> = <whitespace>#<any>
2132 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2133 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2134 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2135 * <subnet_mask_length> is a decimal 1-31
2136 * <subnet_name> is a string containing no whitespace.
2137 * <whitespace> = (space | tab)+
2138 * Any malformed entries are ignored.
2139 * Any trailing data after the subnet_name is ignored.
2144 read_subnets_file (const char *subnetspath)
2150 guint32 host_addr; /* IPv4 ONLY */
2153 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2156 while (fgetline(&line, &size, hf) >= 0) {
2157 if ((cp = strchr(line, '#')))
2160 if ((cp = strtok(line, " \t")) == NULL)
2161 continue; /* no tokens in the line */
2164 /* Expected format is <IP4 address>/<subnet length> */
2165 cp2 = strchr(cp, '/');
2170 *cp2 = '\0'; /* Cut token */
2173 /* Check if this is a valid IPv4 address */
2174 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
2178 mask_length = atoi(cp2);
2179 if(0 >= mask_length || mask_length > 31) {
2180 continue; /* invalid mask length */
2183 if ((cp = strtok(NULL, " \t")) == NULL)
2184 continue; /* no subnet name */
2186 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2192 } /* read_subnets_file */
2194 static subnet_entry_t
2195 subnet_lookup(const guint32 addr)
2197 subnet_entry_t subnet_entry;
2200 /* Search mask lengths linearly, longest first */
2202 i = SUBNETLENGTHSIZE;
2203 while(have_subnet_entry && i > 0) {
2204 guint32 masked_addr;
2205 subnet_length_entry_t* length_entry;
2207 /* Note that we run from 31 (length 32) to 0 (length 1) */
2209 g_assert(i < SUBNETLENGTHSIZE);
2212 length_entry = &subnet_length_entries[i];
2214 if(NULL != length_entry->subnet_addresses) {
2218 masked_addr = addr & length_entry->mask;
2219 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2221 tp = length_entry->subnet_addresses[hash_idx];
2222 while(tp != NULL && tp->addr != masked_addr) {
2227 subnet_entry.mask = length_entry->mask;
2228 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2229 subnet_entry.name = tp->name;
2230 return subnet_entry;
2235 subnet_entry.mask = 0;
2236 subnet_entry.mask_length = 0;
2237 subnet_entry.name = NULL;
2239 return subnet_entry;
2242 /* Add a subnet-definition - name pair to the set.
2243 * The definition is taken by masking the address passed in with the mask of the
2247 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2249 subnet_length_entry_t* entry;
2253 g_assert(mask_length > 0 && mask_length <= 32);
2255 entry = &subnet_length_entries[mask_length - 1];
2257 subnet_addr &= entry->mask;
2259 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2261 if(NULL == entry->subnet_addresses) {
2262 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
2265 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2266 if(tp->addr == subnet_addr) {
2267 return; /* XXX provide warning that an address was repeated? */
2269 hashipv4_t * new_tp = g_new(hashipv4_t,1);
2274 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
2278 tp->addr = subnet_addr;
2279 tp->is_dummy_entry = FALSE; /*Never used again...*/
2280 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2281 have_subnet_entry = TRUE;
2285 get_subnet_mask(const guint32 mask_length) {
2287 static guint32 masks[SUBNETLENGTHSIZE];
2288 static gboolean initialised = FALSE;
2291 memset(masks, 0, sizeof(masks));
2295 /* XXX There must be a better way to do this than
2296 * hand-coding the values, but I can't seem to
2300 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2301 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2302 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2303 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2304 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2305 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2306 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2307 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2309 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2310 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2311 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2312 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2313 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2314 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2315 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2316 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2318 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2319 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2320 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2321 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2322 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2323 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2324 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2325 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2327 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2328 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2329 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2330 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2331 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2332 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2333 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2334 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2337 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2338 g_assert_not_reached();
2341 return masks[mask_length - 1];
2346 subnet_name_lookup_init(void)
2351 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2352 guint32 length = i + 1;
2354 subnet_length_entries[i].subnet_addresses = NULL;
2355 subnet_length_entries[i].mask_length = length;
2356 subnet_length_entries[i].mask = get_subnet_mask(length);
2359 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2360 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2361 report_open_failure(subnetspath, errno, FALSE);
2363 g_free(subnetspath);
2366 * Load the global subnets file, if we have one.
2368 subnetspath = get_datafile_path(ENAME_SUBNETS);
2369 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2370 report_open_failure(subnetspath, errno, FALSE);
2372 g_free(subnetspath);
2377 * External Functions
2381 host_name_lookup_init(void) {
2383 struct addrinfo *ai;
2385 #ifdef HAVE_GNU_ADNS
2388 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2389 static char rootpath_ot[] = "\\hosts";
2391 #endif /*GNU_ADNS */
2393 if (!addrinfo_list) {
2394 ai = g_malloc0(sizeof(struct addrinfo));
2395 addrinfo_list = addrinfo_list_last = ai;
2399 * Load the user's hosts file, if they have one.
2401 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2402 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2403 report_open_failure(hostspath, errno, FALSE);
2408 * Load the global hosts file, if we have one.
2410 hostspath = get_datafile_path(ENAME_HOSTS);
2411 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2412 report_open_failure(hostspath, errno, FALSE);
2417 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2418 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2420 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2421 async_dns_initialized = TRUE;
2423 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2427 #ifdef HAVE_GNU_ADNS
2429 * We're using GNU ADNS, which doesn't check the system hosts file;
2430 * we load that file ourselves.
2434 sysroot = getenv_utf8("WINDIR");
2435 if (sysroot != NULL) {
2437 * The file should be under WINDIR.
2438 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2439 * %WINDIR%\system32\drivers\etc\hosts.
2440 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2442 * XXX - should we base it on the dwPlatformId value from
2445 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2446 if (!read_hosts_file(hostspath)) {
2448 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2449 read_hosts_file(hostspath);
2454 read_hosts_file("/etc/hosts");
2457 /* XXX - Any flags we should be using? */
2458 /* XXX - We could provide config settings for DNS servers, and
2459 pass them to ADNS with adns_init_strcfg */
2460 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2462 * XXX - should we report the error? I'm assuming that some crashes
2463 * reported on a Windows machine with TCP/IP not configured are due
2464 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2465 * ADNS in a state where it crashes due to that. We'll still try
2466 * doing name resolution anyway.
2470 async_dns_initialized = TRUE;
2471 async_dns_in_flight = 0;
2472 #endif /* HAVE_GNU_ADNS */
2473 #endif /* HAVE_C_ARES */
2475 subnet_name_lookup_init();
2480 host_name_lookup_process(gpointer data _U_) {
2481 async_dns_queue_msg_t *caqm;
2482 struct timeval tv = { 0, 0 };
2485 gboolean nro = new_resolved_objects;
2487 new_resolved_objects = FALSE;
2489 if (!async_dns_initialized)
2490 /* c-ares not initialized. Bail out and cancel timers. */
2493 async_dns_queue_head = g_list_first(async_dns_queue_head);
2495 while (async_dns_queue_head != NULL && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2496 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2497 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2498 if (caqm->family == AF_INET) {
2499 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2500 c_ares_ghba_cb, caqm);
2501 async_dns_in_flight++;
2502 } else if (caqm->family == AF_INET6) {
2503 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2504 AF_INET6, c_ares_ghba_cb, caqm);
2505 async_dns_in_flight++;
2511 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2513 select(nfds, &rfds, &wfds, NULL, &tv);
2514 ares_process(ghba_chan, &rfds, &wfds);
2517 /* Any new entries? */
2522 host_name_lookup_cleanup(void) {
2525 cur = g_list_first(async_dns_queue_head);
2528 cur = g_list_next (cur);
2531 g_list_free(async_dns_queue_head);
2533 if (async_dns_initialized) {
2534 ares_destroy(ghba_chan);
2535 ares_destroy(ghbn_chan);
2537 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2538 ares_library_cleanup();
2540 async_dns_initialized = FALSE;
2543 #elif defined(HAVE_GNU_ADNS)
2545 /* XXX - The ADNS "documentation" isn't very clear:
2546 * - Do we need to keep our query structures around?
2549 host_name_lookup_process(gpointer data _U_) {
2550 async_dns_queue_msg_t *almsg;
2552 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2557 gboolean nro = new_resolved_objects;
2559 new_resolved_objects = FALSE;
2560 async_dns_queue_head = g_list_first(async_dns_queue_head);
2562 cur = async_dns_queue_head;
2563 while (cur && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2564 almsg = (async_dns_queue_msg_t *) cur->data;
2565 if (! almsg->submitted && almsg->type == AF_INET) {
2566 addr_bytes = (guint8 *) &almsg->ip4_addr;
2567 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2568 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2569 /* XXX - what if it fails? */
2570 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2571 almsg->submitted = TRUE;
2572 async_dns_in_flight++;
2577 cur = async_dns_queue_head;
2580 almsg = (async_dns_queue_msg_t *) cur->data;
2581 if (almsg->submitted) {
2582 ret = adns_check(ads, &almsg->query, &ans, NULL);
2584 if (ans->status == adns_s_ok) {
2585 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2592 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2594 async_dns_in_flight--;
2598 /* Keep the timeout in place */
2603 host_name_lookup_cleanup(void) {
2606 async_dns_queue_head = g_list_first(async_dns_queue_head);
2607 while (async_dns_queue_head) {
2608 qdata = async_dns_queue_head->data;
2609 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2613 if (async_dns_initialized)
2615 async_dns_initialized = FALSE;
2618 #else /* HAVE_GNU_ADNS */
2621 host_name_lookup_process(gpointer data _U_) {
2622 gboolean nro = new_resolved_objects;
2624 new_resolved_objects = FALSE;
2630 host_name_lookup_cleanup(void) {
2633 #endif /* HAVE_C_ARES */
2635 extern const gchar *
2636 get_hostname(const guint addr)
2639 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2640 hashipv4_t *tp = host_lookup(addr, resolve, &found);
2648 /* -------------------------- */
2650 extern const gchar *
2651 get_hostname6(const struct e_in6_addr *addr)
2654 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2655 hashipv6_t *tp = host_lookup6(addr, resolve, &found);
2663 /* -------------------------- */
2665 add_ipv4_name(const guint addr, const gchar *name)
2669 struct addrinfo *ai;
2670 struct sockaddr_in *sa4;
2672 hash_idx = HASH_IPV4_ADDRESS(addr);
2674 tp = ipv4_table[hash_idx];
2677 tp = ipv4_table[hash_idx] = new_ipv4(addr);
2680 if (tp->addr == addr) {
2681 /* address already known */
2682 if (!tp->is_dummy_entry) {
2685 /* replace this dummy entry with the new one */
2689 if (tp->next == NULL) {
2690 tp->next = new_ipv4(addr);
2697 g_strlcpy(tp->name, name, MAXNAMELEN);
2699 new_resolved_objects = TRUE;
2701 if (!addrinfo_list) {
2702 ai = g_malloc0(sizeof(struct addrinfo));
2703 addrinfo_list = addrinfo_list_last = ai;
2706 sa4 = g_malloc0(sizeof(struct sockaddr_in));
2707 sa4->sin_family = AF_INET;
2708 sa4->sin_addr.s_addr = addr;
2710 ai = g_malloc0(sizeof(struct addrinfo));
2711 ai->ai_family = AF_INET;
2712 ai->ai_addrlen = sizeof(struct sockaddr_in);
2713 ai->ai_canonname = (char *) tp->name;
2714 ai->ai_addr = (struct sockaddr*) sa4;
2716 addrinfo_list_last->ai_next = ai;
2717 addrinfo_list_last = ai;
2719 } /* add_ipv4_name */
2721 /* -------------------------- */
2723 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2727 struct addrinfo *ai;
2728 struct sockaddr_in6 *sa6;
2730 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2732 tp = ipv6_table[hash_idx];
2735 tp = ipv6_table[hash_idx] = new_ipv6(addrp);
2738 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2739 /* address already known */
2740 if (!tp->is_dummy_entry) {
2743 /* replace this dummy entry with the new one */
2747 if (tp->next == NULL) {
2748 tp->next = new_ipv6(addrp);
2755 g_strlcpy(tp->name, name, MAXNAMELEN);
2757 new_resolved_objects = TRUE;
2759 if (!addrinfo_list) {
2760 ai = g_malloc0(sizeof(struct addrinfo));
2761 addrinfo_list = addrinfo_list_last = ai;
2764 sa6 = g_malloc0(sizeof(struct sockaddr_in6));
2765 sa6->sin6_family = AF_INET;
2766 memcpy(sa6->sin6_addr.s6_addr, addrp, 16);
2768 ai = g_malloc0(sizeof(struct addrinfo));
2769 ai->ai_family = AF_INET6;
2770 ai->ai_addrlen = sizeof(struct sockaddr_in);
2771 ai->ai_canonname = (char *) tp->name;
2772 ai->ai_addr = (struct sockaddr *) sa6;
2774 addrinfo_list_last->ai_next = ai;
2775 addrinfo_list_last = ai;
2777 } /* add_ipv6_name */
2779 /* -----------------
2780 * unsigned integer to ascii
2785 gchar *bp = ep_alloc(MAXNAMELEN);
2787 /* XXX, guint32_to_str() ? */
2788 guint32_to_str_buf(port, bp, MAXNAMELEN);
2794 get_udp_port(guint port)
2797 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2798 return ep_utoa(port);
2801 return serv_name_lookup(port, PT_UDP);
2803 } /* get_udp_port */
2806 get_dccp_port(guint port)
2809 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2810 return ep_utoa(port);
2813 return serv_name_lookup(port, PT_DCCP);
2815 } /* get_dccp_port */
2819 get_tcp_port(guint port)
2822 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2823 return ep_utoa(port);
2826 return serv_name_lookup(port, PT_TCP);
2828 } /* get_tcp_port */
2831 get_sctp_port(guint port)
2834 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2835 return ep_utoa(port);
2838 return serv_name_lookup(port, PT_SCTP);
2840 } /* get_sctp_port */
2843 get_addr_name(const address *addr)
2845 const gchar *result;
2847 result = solve_address_to_name(addr);
2852 /* if it gets here, either it is of type AT_NONE, */
2853 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
2855 if (addr->type == AT_NONE){
2859 /* We need an ephemeral allocated string */
2860 return ep_address_to_str(addr);
2864 se_get_addr_name(const address *addr)
2866 const gchar *result;
2868 result = se_solve_address_to_name(addr);
2873 /* if it gets here, either it is of type AT_NONE, */
2874 /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
2876 if (addr->type == AT_NONE){
2880 /* We need a "permanently" allocated string */
2881 return se_address_to_str(addr);
2885 get_addr_name_buf(const address *addr, gchar *buf, gsize size)
2887 const gchar *result = get_addr_name(addr);
2889 g_strlcpy(buf, result, size);
2890 } /* get_addr_name_buf */
2894 get_ether_name(const guint8 *addr)
2897 gboolean resolve = (gbl_resolv_flags & RESOLV_MAC) != 0;
2899 if (resolve && !eth_resolution_initialized) {
2900 initialize_ethers();
2901 eth_resolution_initialized = TRUE;
2904 tp = eth_name_lookup(addr, resolve);
2906 return resolve ? tp->resolved_name : tp->hexaddr;
2908 } /* get_ether_name */
2910 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2911 * If it's not found, simply return NULL.
2914 get_ether_name_if_known(const guint8 *addr)
2918 /* Initialize ether structs if we're the first
2919 * ether-related function called */
2920 if (!(gbl_resolv_flags & RESOLV_MAC))
2923 if (!eth_resolution_initialized) {
2924 initialize_ethers();
2925 eth_resolution_initialized = TRUE;
2928 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
2929 tp = eth_name_lookup(addr, TRUE);
2930 g_assert(tp != NULL);
2932 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
2933 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
2934 return tp->resolved_name;
2937 /* Name was created */
2943 get_ether_addr(const gchar *name)
2946 /* force resolution (do not check gbl_resolv_flags) */
2948 if (!eth_resolution_initialized) {
2949 initialize_ethers();
2950 eth_resolution_initialized = TRUE;
2953 return eth_addr_lookup(name);
2955 } /* get_ether_addr */
2958 add_ether_byip(const guint ip, const guint8 *eth)
2964 /* first check that IP address can be resolved */
2965 if (!(gbl_resolv_flags & RESOLV_NETWORK))
2968 if ((host = host_name_lookup(ip, &found)) == NULL)
2971 /* ok, we can add this entry in the ethers hashtable */
2974 add_eth_name(eth, host);
2976 } /* add_ether_byip */
2978 extern const gchar *
2979 get_ipxnet_name(const guint32 addr)
2982 if (!(gbl_resolv_flags & RESOLV_NETWORK)) {
2983 return ipxnet_to_str_punct(addr, '\0');
2986 if (!ipxnet_resolution_initialized) {
2987 initialize_ipxnets();
2988 ipxnet_resolution_initialized = 1;
2991 return ipxnet_name_lookup(addr);
2993 } /* get_ipxnet_name */
2996 get_ipxnet_addr(const gchar *name, gboolean *known)
3001 /* force resolution (do not check gbl_resolv_flags) */
3003 if (!ipxnet_resolution_initialized) {
3004 initialize_ipxnets();
3005 ipxnet_resolution_initialized = 1;
3008 addr = ipxnet_addr_lookup(name, &success);
3013 } /* get_ipxnet_addr */
3015 extern const gchar *
3016 get_manuf_name(const guint8 *addr)
3021 if ((gbl_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
3022 initialize_ethers();
3023 eth_resolution_initialized = TRUE;
3026 if (!(gbl_resolv_flags & RESOLV_MAC) || ((mtp = manuf_name_lookup(addr)) == NULL)) {
3027 cur=ep_strdup_printf("%02x:%02x:%02x", addr[0], addr[1], addr[2]);
3033 } /* get_manuf_name */
3035 extern const gchar *
3036 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3038 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3042 get_manuf_name_if_known(const guint8 *addr)
3046 if (!eth_resolution_initialized) {
3047 initialize_ethers();
3048 eth_resolution_initialized = TRUE;
3051 if ((mtp = manuf_name_lookup(addr)) == NULL) {
3057 } /* get_manuf_name_if_known */
3059 extern const gchar *
3060 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3062 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3065 extern const gchar *
3066 get_eui64_name(const guint64 addr_eui64)
3070 guint8 *addr = ep_alloc(8);
3072 /* Copy and convert the address to network byte order. */
3073 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3075 if ((gbl_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
3076 initialize_ethers();
3077 eth_resolution_initialized = TRUE;
3080 if (!(gbl_resolv_flags & RESOLV_MAC) || ((mtp = manuf_name_lookup(addr)) == NULL)) {
3081 cur=ep_strdup_printf("%02x:%02x:%02x%02x:%02x:%02x%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
3084 cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", mtp->name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3087 } /* get_eui64_name */
3091 get_eui64_name_if_known(const guint64 addr_eui64)
3095 guint8 *addr = ep_alloc(8);
3097 /* Copy and convert the address to network byte order. */
3098 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3100 if (!eth_resolution_initialized) {
3101 initialize_ethers();
3102 eth_resolution_initialized = TRUE;
3105 if ((mtp = manuf_name_lookup(addr)) == NULL) {
3109 cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", mtp->name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3112 } /* get_eui64_name_if_known */
3115 #define GHI_TIMEOUT (250 * 1000)
3117 #if ( ( ARES_VERSION_MAJOR < 1 ) \
3118 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3119 c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
3121 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3124 * XXX - If we wanted to be really fancy we could cache results here and
3125 * look them up in get_host_ipaddr* below.
3127 async_hostent_t *ahp = arg;
3128 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3129 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3130 ahp->copied = hp->h_length;
3133 #endif /* HAVE_C_ARES */
3135 /* Translate a string, assumed either to be a dotted-quad IP address or
3136 * a host name, to a numeric IP address. Return TRUE if we succeed and
3137 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3138 * Used more in the dfilter parser rather than in packet dissectors */
3140 get_host_ipaddr(const char *host, guint32 *addrp)
3142 struct in_addr ipaddr;
3144 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3147 async_hostent_t ahe;
3148 #else /* HAVE_C_ARES */
3150 #endif /* HAVE_C_ARES */
3153 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3154 * inet_pton(AF_INET) does not support hexadecimal notation nor
3155 * less-than-4 octet notation.
3157 if (!inet_aton(host, &ipaddr)) {
3158 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3161 /* It's not a valid dotted-quad IP address; is it a valid
3164 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3165 prefs.name_resolve_concurrency < 1 ||
3166 ! async_dns_initialized) {
3169 ahe.addr_size = (int) sizeof (struct in_addr);
3172 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3175 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3177 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3178 select(nfds, &rfds, &wfds, NULL, tvp);
3179 ares_process(ghbn_chan, &rfds, &wfds);
3181 ares_cancel(ghbn_chan);
3182 if (ahe.addr_size == ahe.copied) {
3186 #else /* ! HAVE_C_ARES */
3187 hp = gethostbyname(host);
3191 /* Apparently, some versions of gethostbyaddr can
3192 * return IPv6 addresses. */
3193 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3194 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3198 #endif /* HAVE_C_ARES */
3200 /* Does the string really contain dotted-quad IP?
3201 * Check against inet_atons that accept strings such as
3202 * "130.230" as valid addresses and try to convert them
3203 * to some form of a classful (host.net) notation.
3205 unsigned int a0, a1, a2, a3;
3206 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3210 *addrp = ipaddr.s_addr;
3215 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3216 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3217 * return FALSE if we fail.
3220 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3223 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3226 async_hostent_t ahe;
3227 #elif defined(HAVE_GETHOSTBYNAME2)
3229 #endif /* HAVE_C_ARES */
3231 if (inet_pton(AF_INET6, host, addrp) == 1)
3234 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3240 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3241 prefs.name_resolve_concurrency < 1 ||
3242 ! async_dns_initialized) {
3245 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3248 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3251 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3253 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3254 select(nfds, &rfds, &wfds, NULL, tvp);
3255 ares_process(ghbn_chan, &rfds, &wfds);
3257 ares_cancel(ghbn_chan);
3258 if (ahe.addr_size == ahe.copied) {
3261 #elif defined(HAVE_GETHOSTBYNAME2)
3262 hp = gethostbyname2(host, AF_INET6);
3263 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3264 memcpy(addrp, hp->h_addr, hp->h_length);
3273 * Find out whether a hostname resolves to an ip or ipv6 address
3274 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3275 * that we don't know)
3277 const char* host_ip_af(const char *host
3278 #ifndef HAVE_GETHOSTBYNAME2
3283 #ifdef HAVE_GETHOSTBYNAME2
3285 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";