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 MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
142 #define HASHETHSIZE 2048
143 #define HASHHOSTSIZE 2048
144 #define HASHIPXNETSIZE 256
145 #define HASHMANUFSIZE 256
146 #define HASHPORTSIZE 256
147 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
149 /* hash table used for IPv4 lookup */
151 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
153 typedef struct hashipv4 {
155 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
156 gboolean resolve; /* already tried to resolve it */
157 struct hashipv4 *next;
159 gchar name[MAXNAMELEN];
162 /* hash table used for IPv6 lookup */
164 #define HASH_IPV6_ADDRESS(addr) \
165 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
167 typedef struct hashipv6 {
168 struct e_in6_addr addr;
169 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
170 gboolean resolve; /* */
171 struct hashipv6 *next;
172 gchar ip6[47]; /* XX */
173 gchar name[MAXNAMELEN];
176 /* Array of entries of subnets of different lengths */
178 gsize mask_length; /*1-32*/
179 guint32 mask; /* e.g. 255.255.255.*/
180 hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
181 } subnet_length_entry_t;
183 /* hash table used for TCP/UDP/SCTP port lookup */
185 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
187 typedef struct hashport {
189 struct hashport *next;
190 gchar name[MAXNAMELEN];
193 /* hash table used for IPX network lookup */
195 /* XXX - check goodness of hash function */
197 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
199 typedef struct hashipxnet {
201 struct hashipxnet *next;
202 gchar name[MAXNAMELEN];
205 /* hash tables used for ethernet and manufacturer lookup */
207 #define HASH_ETH_ADDRESS(addr) \
208 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
211 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
213 typedef struct hashmanuf {
215 struct hashmanuf *next;
216 char name[MAXMANUFLEN];
219 typedef struct hashether {
221 gboolean is_dummy_entry; /* not a complete entry */
222 gboolean resolve; /* */
223 struct hashether *next;
225 char name[MAXNAMELEN];
228 /* internal ethernet type */
230 typedef struct _ether
233 char name[MAXNAMELEN];
236 /* internal ipxnet type */
238 typedef struct _ipxnet
241 char name[MAXNAMELEN];
244 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
245 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
247 static hashport_t **cb_port_table;
248 static gchar *cb_service;
250 static hashport_t *udp_port_table[HASHPORTSIZE];
251 static hashport_t *tcp_port_table[HASHPORTSIZE];
252 static hashport_t *sctp_port_table[HASHPORTSIZE];
253 static hashport_t *dccp_port_table[HASHPORTSIZE];
254 static hashether_t *eth_table[HASHETHSIZE];
255 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
256 static hashether_t *(*wka_table[48])[HASHETHSIZE];
257 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
259 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
260 static gboolean have_subnet_entry = FALSE;
262 static int eth_resolution_initialized = 0;
263 static int ipxnet_resolution_initialized = 0;
264 static int service_resolution_initialized = 0;
265 static gboolean new_resolved_objects = FALSE;
267 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
268 static void add_serv_port_cb(const guint32 port);
271 * Flag controlling what names to resolve.
273 guint32 g_resolv_flags;
276 * Global variables (can be changed in GUI sections)
277 * XXX - they could be changed in GUI code, but there's currently no
278 * GUI code to change them.
281 gchar *g_ethers_path = NULL; /* global ethers file */
282 gchar *g_pethers_path = NULL; /* personal ethers file */
283 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
284 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
285 gchar *g_services_path = NULL; /* global services file */
286 gchar *g_pservices_path = NULL; /* personal services file */
287 /* first resolving call */
292 * Submitted queries trigger a callback (c_ares_ghba_cb()).
293 * Queries are added to c_ares_queue_head. During processing, queries are
294 * popped off the front of c_ares_queue_head and submitted using
295 * ares_gethostbyaddr().
296 * The callback processes the response, then frees the request.
299 typedef struct _async_dns_queue_msg
303 struct e_in6_addr ip6;
306 } async_dns_queue_msg_t;
308 typedef struct _async_hostent {
314 #if ( ( ARES_VERSION_MAJOR < 1 ) \
315 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
316 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
318 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
321 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
322 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
329 * Submitted queries have to be checked individually using adns_check().
330 * Queries are added to adns_queue_head. During processing, the list is
331 * iterated twice: once to request queries up to the concurrency limit,
332 * and once to check the status of each query.
337 typedef struct _async_dns_queue_msg
343 } async_dns_queue_msg_t;
345 #endif /* HAVE_GNU_ADNS */
346 #endif /* HAVE_C_ARES */
348 static gboolean async_dns_initialized = FALSE;
349 static int async_dns_in_flight = 0;
350 static GList *async_dns_queue_head = NULL;
352 /* push a dns request */
354 add_async_dns_ipv4(int type, guint32 addr)
356 async_dns_queue_msg_t *msg;
358 msg = g_malloc(sizeof(async_dns_queue_msg_t));
361 msg->addr.ip4 = addr;
364 msg->ip4_addr = addr;
365 msg->submitted = FALSE;
367 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
375 const gchar* name; /* Shallow copy */
379 * Miscellaneous functions
383 fgetline(char **buf, int *size, FILE *fp)
388 if (fp == NULL || buf == NULL)
395 *buf = g_malloc(*size);
405 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
406 if (len+1 >= *size) {
407 *buf = g_realloc(*buf, *size += BUFSIZ);
412 if (len == 0 && c == EOF)
423 * Local function definitions
425 static subnet_entry_t subnet_lookup(const guint32 addr);
426 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
430 add_service_name(hashport_t **proto_table, const guint port, const char *service_name)
436 hash_idx = HASH_PORT(port);
437 tp = proto_table[hash_idx];
440 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
443 if( tp->port == port ) {
446 if (tp->next == NULL) {
447 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
455 /* fill in a new entry */
459 g_strlcpy(tp->name, service_name, MAXNAMELEN);
461 new_resolved_objects = TRUE;
466 parse_service_line (char *line)
469 * See the services(4) or services(5) man page for services file format
470 * (not available on all systems).
477 range_t *port_rng = NULL;
478 guint32 max_port = MAX_UDP_PORT;
480 if ((cp = strchr(line, '#')))
483 if ((cp = strtok(line, " \t")) == NULL)
488 if ((cp = strtok(NULL, " \t")) == NULL)
493 if ((cp = strtok(cp, "/")) == NULL)
496 if ((cp = strtok(NULL, "/")) == NULL)
499 /* seems we got all interesting things from the file */
500 if(strcmp(cp, "tcp") == 0) {
501 max_port = MAX_TCP_PORT;
502 cb_port_table = tcp_port_table;
504 else if(strcmp(cp, "udp") == 0) {
505 max_port = MAX_UDP_PORT;
506 cb_port_table = udp_port_table;
508 else if(strcmp(cp, "sctp") == 0) {
509 max_port = MAX_SCTP_PORT;
510 cb_port_table = sctp_port_table;
512 else if(strcmp(cp, "dccp") == 0) {
513 max_port = MAX_DCCP_PORT;
514 cb_port_table = dccp_port_table;
519 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
520 /* some assertion here? */
524 cb_service = service;
525 range_foreach(port_rng, add_serv_port_cb);
527 } /* parse_service_line */
531 add_serv_port_cb(const guint32 port)
534 add_service_name(cb_port_table, port, cb_service);
540 parse_services_file(const char * path)
544 static char *buf = NULL;
546 /* services hash table initialization */
547 serv_p = ws_fopen(path, "r");
552 while (fgetline(&buf, &size, serv_p) >= 0) {
553 parse_service_line (buf);
560 initialize_services(void)
563 /* the hash table won't ignore duplicates, so use the personal path first */
565 /* set personal services path */
566 if (g_pservices_path == NULL)
567 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
569 parse_services_file(g_pservices_path);
571 /* Compute the pathname of the services file. */
572 if (g_services_path == NULL) {
573 g_services_path = get_datafile_path(ENAME_SERVICES);
576 parse_services_file(g_services_path);
578 } /* initialize_services */
583 *serv_name_lookup(const guint port, const port_type proto)
588 const char *serv_proto = NULL;
589 struct servent *servp;
592 if (!service_resolution_initialized) {
593 initialize_services();
594 service_resolution_initialized = 1;
599 table = udp_port_table;
603 table = tcp_port_table;
607 table = sctp_port_table;
611 table = dccp_port_table;
615 /* not yet implemented */
620 hash_idx = HASH_PORT(port);
621 tp = table[hash_idx];
624 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
627 if( tp->port == port ) {
630 if (tp->next == NULL) {
631 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
639 /* fill in a new entry */
643 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
644 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
646 guint32_to_str_buf(port, tp->name, MAXNAMELEN);
648 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
653 } /* serv_name_lookup */
656 /* Fill in an IP4 structure with info from subnets file or just with the
657 * string form of the address.
660 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
662 subnet_entry_t subnet_entry;
664 if (tp->is_dummy_entry)
665 return; /* already done */
667 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
669 /* Do we have a subnet for this address? */
670 subnet_entry = subnet_lookup(addr);
671 if(0 != subnet_entry.mask) {
672 /* Print name, then '.' then IP address after subnet mask */
674 gchar buffer[MAX_IP_STR_LEN];
678 host_addr = addr & (~(guint32)subnet_entry.mask);
679 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
682 /* Skip to first octet that is not totally masked
683 * If length of mask is 32, we chomp the whole address.
684 * If the address string starts '.' (should not happen?),
687 i = subnet_entry.mask_length / 8;
688 while(*(paddr) != '\0' && i > 0) {
689 if(*(++paddr) == '.') {
694 /* There are more efficient ways to do this, but this is safe if we
695 * trust g_snprintf and MAXNAMELEN
697 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
699 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
706 #if ( ( ARES_VERSION_MAJOR < 1 ) \
707 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
708 c_ares_ghba_cb(void *arg, int status, struct hostent *he) {
710 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
712 async_dns_queue_msg_t *caqm = arg;
716 async_dns_in_flight--;
718 if (status == ARES_SUCCESS) {
719 for (p = he->h_addr_list; *p != NULL; p++) {
720 switch(caqm->family) {
722 add_ipv4_name(caqm->addr.ip4, he->h_name);
725 add_ipv6_name(&caqm->addr.ip6, he->h_name);
728 /* Throw an exception? */
735 #endif /* HAVE_C_ARES */
737 /* --------------- */
739 new_ipv4(const guint addr)
741 hashipv4_t *tp = g_malloc(sizeof(hashipv4_t));
745 tp->is_dummy_entry = FALSE;
746 ip_to_str_buf((guint8 *)&addr, tp->ip, sizeof(tp->ip));
751 host_lookup(const guint addr, const gboolean resolve, gboolean *found)
754 hashipv4_t * volatile tp;
755 struct hostent *hostp;
759 hash_idx = HASH_IPV4_ADDRESS(addr);
761 tp = ipv4_table[hash_idx];
764 tp = ipv4_table[hash_idx] = new_ipv4(addr);
767 if( tp->addr == addr ) {
768 if (tp->is_dummy_entry && !tp->resolve)
770 if (tp->is_dummy_entry)
774 if (tp->next == NULL) {
775 tp->next = new_ipv4(addr);
786 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
787 prefs.name_resolve_concurrency > 0 &&
788 async_dns_initialized) {
789 add_async_dns_ipv4(AF_INET, addr);
790 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
791 * going to risk changing the semantics.
793 fill_dummy_ip4(addr, tp);
796 #endif /* ASYNC_DNS */
799 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
800 * the name of the host on which it's running; to work around that
801 * botch, we don't try to translate an all-zero IP address to a host
804 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
805 /* Use async DNS if possible, else fall back to timeouts,
806 * else call gethostbyaddr and hope for the best
809 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
812 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
813 tp->is_dummy_entry = FALSE;
818 /* unknown host or DNS timeout */
824 fill_dummy_ip4(addr, tp);
827 } /* host_name_lookup */
830 host_name_lookup(const guint addr, gboolean *found)
833 tp = host_lookup(addr, TRUE, found);
838 /* --------------- */
840 new_ipv6(const struct e_in6_addr *addr)
842 hashipv6_t *tp = g_malloc(sizeof(hashipv6_t));
846 tp->is_dummy_entry = FALSE;
847 ip6_to_str_buf(addr, tp->ip6);
851 /* ------------------------------------ */
853 host_lookup6(const struct e_in6_addr *addr, const gboolean resolve, gboolean *found)
856 hashipv6_t * volatile tp;
859 async_dns_queue_msg_t *caqm;
860 #endif /* HAVE_C_ARES */
861 struct hostent *hostp;
866 hash_idx = HASH_IPV6_ADDRESS(*addr);
868 tp = ipv6_table[hash_idx];
871 tp = ipv6_table[hash_idx] = new_ipv6(addr);
874 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
875 if (tp->is_dummy_entry && !tp->resolve)
877 if (tp->is_dummy_entry)
881 if (tp->next == NULL) {
882 tp->next = new_ipv6(addr);
895 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
896 prefs.name_resolve_concurrency > 0 &&
897 async_dns_initialized) {
898 caqm = g_malloc(sizeof(async_dns_queue_msg_t));
899 caqm->family = AF_INET6;
900 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
901 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
903 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
904 * going to risk changing the semantics.
906 if (!tp->is_dummy_entry) {
907 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
908 ip6_to_str_buf(addr, tp->name);
909 tp->is_dummy_entry = TRUE;
913 #endif /* HAVE_C_ARES */
915 /* Quick hack to avoid DNS/YP timeout */
916 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
919 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
920 tp->is_dummy_entry = FALSE;
926 /* unknown host or DNS timeout */
927 if (!tp->is_dummy_entry) {
928 tp->is_dummy_entry = TRUE;
929 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
938 host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
941 tp = host_lookup6(addr, TRUE, found);
947 solve_address_to_name(const address *addr)
949 switch (addr->type) {
952 return get_ether_name(addr->data);
956 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
957 return get_hostname(ip4_addr);
961 struct e_in6_addr ip6_addr;
962 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
963 return get_hostname6(&ip6_addr);
975 se_solve_address_to_name(const address *addr)
977 switch (addr->type) {
980 return get_ether_name(addr->data);
984 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
985 return get_hostname(ip4_addr);
989 struct e_in6_addr ip6_addr;
990 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
991 return get_hostname6(&ip6_addr);
995 return se_strdup(addr->data);
1003 * Ethernet / manufacturer resolution
1005 * The following functions implement ethernet address resolution and
1006 * ethers files parsing (see ethers(4)).
1008 * The manuf file has the same format as ethers(4) except that names are
1009 * truncated to MAXMANUFLEN-1 characters and that an address contains
1010 * only 3 bytes (instead of 6).
1014 * I decide to not use the existing functions (see ethers(3) on some
1015 * operating systems) for the following reasons:
1016 * - performance gains (use of hash tables and some other enhancements),
1017 * - use of two ethers files (system-wide and per user),
1018 * - avoid the use of NIS maps,
1019 * - lack of these functions on some systems.
1021 * So the following functions do _not_ behave as the standard ones.
1028 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1029 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1033 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1034 const gboolean manuf_file)
1041 for (i = 0; i < 6; i++) {
1042 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1043 if (!isxdigit((unsigned char)*cp))
1045 num = strtoul(cp, &p, 16);
1047 return FALSE; /* failed */
1049 return FALSE; /* not a valid octet */
1050 eth->addr[i] = (guint8) num;
1051 cp = p; /* skip past the number */
1053 /* OK, what character terminated the octet? */
1055 /* "/" - this has a mask. */
1057 /* Entries with masks are allowed only in the "manuf" files. */
1060 cp++; /* skip past the '/' to get to the mask */
1061 if (!isdigit((unsigned char)*cp))
1062 return FALSE; /* no sign allowed */
1063 num = strtoul(cp, &p, 10);
1065 return FALSE; /* failed */
1066 cp = p; /* skip past the number */
1067 if (*cp != '\0' && !isspace((unsigned char)*cp))
1068 return FALSE; /* bogus terminator */
1069 if (num == 0 || num >= 48)
1070 return FALSE; /* bogus mask */
1071 /* Mask out the bits not covered by the mask */
1073 for (i = 0; num >= 8; i++, num -= 8)
1074 ; /* skip octets entirely covered by the mask */
1075 /* Mask out the first masked octet */
1076 eth->addr[i] &= (0xFF << (8 - num));
1078 /* Mask out completely-masked-out octets */
1084 /* We're at the end of the address, and there's no mask. */
1086 /* We got 3 bytes, so this is a manufacturer ID. */
1088 /* Manufacturer IDs are only allowed in the "manuf"
1092 /* Indicate that this is a manufacturer ID (0 is not allowed
1099 /* We got 6 bytes, so this is a MAC address.
1100 If we're reading one of the "manuf" files, indicate that
1101 this is a MAC address (48 is not allowed as a mask). */
1107 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1112 /* We don't know the separator used in this number; it can either
1113 be ':', '-', or '.'. */
1114 if (*cp != ':' && *cp != '-' && *cp != '.')
1116 sep = *cp; /* subsequent separators must be the same */
1118 /* It has to be the same as the first separator */
1130 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1131 const gboolean manuf_file)
1134 * See the ethers(4) or ethers(5) man page for ethers file format
1135 * (not available on all systems).
1136 * We allow both ethernet address separators (':' and '-'),
1137 * as well as Wireshark's '.' separator.
1142 if ((cp = strchr(line, '#')))
1145 if ((cp = strtok(line, " \t")) == NULL)
1148 if (!parse_ether_address(cp, eth, mask, manuf_file))
1151 if ((cp = strtok(NULL, " \t")) == NULL)
1154 g_strlcpy(eth->name, cp, MAXNAMELEN);
1158 } /* parse_ether_line */
1160 static FILE *eth_p = NULL;
1163 set_ethent(char *path)
1168 eth_p = ws_fopen(path, "r");
1181 get_ethent(unsigned int *mask, const gboolean manuf_file)
1185 static int size = 0;
1186 static char *buf = NULL;
1191 while (fgetline(&buf, &size, eth_p) >= 0) {
1192 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1202 get_ethbyname(const gchar *name)
1206 set_ethent(g_pethers_path);
1208 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1214 set_ethent(g_ethers_path);
1216 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1224 } /* get_ethbyname */
1227 get_ethbyaddr(const guint8 *addr)
1232 set_ethent(g_pethers_path);
1234 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1240 set_ethent(g_ethers_path);
1242 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
1250 } /* get_ethbyaddr */
1253 hash_eth_wka(const guint8 *addr, unsigned int mask)
1256 /* All but the topmost byte is masked out */
1257 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
1261 /* All but the topmost 2 bytes are masked out */
1262 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1267 /* All but the topmost 3 bytes are masked out */
1268 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1269 & (HASHETHSIZE - 1);
1273 /* All but the topmost 4 bytes are masked out */
1274 return ((((addr[0] << 8) | addr[1]) ^
1275 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1280 /* All but the topmost 5 bytes are masked out */
1281 return ((((addr[1] << 8) | addr[2]) ^
1282 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1286 /* No bytes are fully masked out */
1287 return ((((addr[1] << 8) | addr[2]) ^
1288 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1293 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1297 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
1300 /* This is a well-known MAC address; just add this to the Ethernet
1302 add_eth_name(addr, name);
1307 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1309 hash_idx = HASH_ETH_MANUF(addr);
1311 tp = manuf_table[hash_idx];
1314 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1317 if (tp->next == NULL) {
1318 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1326 memcpy(tp->addr, addr, sizeof(tp->addr));
1327 g_strlcpy(tp->name, name, MAXMANUFLEN);
1332 /* This is a range of well-known addresses; add it to the appropriate
1333 well-known-address table, creating that table if necessary. */
1334 wka_tp = wka_table[mask];
1336 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1338 hash_idx = hash_eth_wka(addr, mask);
1340 etp = (*wka_tp)[hash_idx];
1343 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1346 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
1347 /* address already known */
1350 if (etp->next == NULL) {
1351 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1359 memcpy(etp->addr, addr, sizeof(etp->addr));
1360 g_strlcpy(etp->name, name, MAXNAMELEN);
1362 etp->is_dummy_entry = FALSE;
1364 } /* add_manuf_name */
1366 static hashmanuf_t *
1367 manuf_name_lookup(const guint8 *addr)
1371 guint8 stripped_addr[3];
1373 hash_idx = HASH_ETH_MANUF(addr);
1375 /* first try to find a "perfect match" */
1376 tp = manuf_table[hash_idx];
1378 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1384 /* Mask out the broadcast/multicast flag but not the locally
1385 * administered flag as localy administered means: not assigend
1386 * by the IEEE but the local administrator instead.
1387 * 0x01 multicast / broadcast bit
1388 * 0x02 locally administered bit */
1389 memcpy(stripped_addr, addr, 3);
1390 stripped_addr[0] &= 0xFE;
1392 tp = manuf_table[hash_idx];
1394 if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
1402 } /* manuf_name_lookup */
1404 static hashether_t *
1405 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1408 hashether_t *(*wka_tp)[HASHETHSIZE];
1410 guint8 masked_addr[6];
1414 wka_tp = wka_table[mask];
1415 if (wka_tp == NULL) {
1416 /* There are no entries in the table for that mask value, as there is
1417 no table for that mask value. */
1421 /* Get the part of the address covered by the mask. */
1422 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1423 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1424 /* Mask out the first masked octet */
1425 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1427 /* Zero out completely-masked-out octets */
1431 hash_idx = hash_eth_wka(masked_addr, mask);
1433 tp = (*wka_tp)[hash_idx];
1436 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1444 } /* wka_name_lookup */
1447 initialize_ethers(void)
1453 /* Compute the pathname of the ethers file. */
1454 if (g_ethers_path == NULL) {
1455 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1456 get_systemfile_dir(), ENAME_ETHERS);
1459 /* Set g_pethers_path here, but don't actually do anything
1460 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1462 if (g_pethers_path == NULL)
1463 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1465 /* manuf hash table initialization */
1467 /* Compute the pathname of the manuf file */
1468 manuf_path = get_datafile_path(ENAME_MANUF);
1470 /* Read it and initialize the hash table */
1471 set_ethent(manuf_path);
1473 while ((eth = get_ethent(&mask, TRUE))) {
1474 add_manuf_name(eth->addr, mask, eth->name);
1481 } /* initialize_ethers */
1483 static hashether_t *
1484 add_eth_name(const guint8 *addr, const gchar *name)
1490 hash_idx = HASH_ETH_ADDRESS(addr);
1492 tp = eth_table[hash_idx];
1495 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1498 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1499 /* address already known */
1500 if (!tp->is_dummy_entry) {
1503 /* replace this dummy (manuf) entry with a real name */
1508 if (tp->next == NULL) {
1509 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1517 g_strlcpy(tp->name, name, MAXNAMELEN);
1519 memcpy(tp->addr, addr, sizeof(tp->addr));
1520 g_strlcpy(tp->hexa, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexa));
1523 tp->is_dummy_entry = FALSE;
1524 new_resolved_objects = TRUE;
1528 } /* add_eth_name */
1531 static hashether_t *
1532 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1535 hashmanuf_t *manufp;
1541 hash_idx = HASH_ETH_ADDRESS(addr);
1543 tp = eth_table[hash_idx];
1546 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1549 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1552 if (tp->next == NULL) {
1553 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1561 /* fill in a new entry */
1562 memcpy(tp->addr, addr, sizeof(tp->addr));
1564 g_strlcpy(tp->hexa, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexa));
1566 g_strlcpy(tp->name, tp->hexa, MAXNAMELEN);
1570 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1571 /* Unknown name. Try looking for it in the well-known-address
1572 tables for well-known address ranges smaller than 2^24. */
1575 /* Only the topmost 5 bytes participate fully */
1576 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1577 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1578 etp->name, addr[5] & (0xFF >> mask));
1579 tp->is_dummy_entry = TRUE;
1589 /* Only the topmost 4 bytes participate fully */
1590 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1591 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1592 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1593 tp->is_dummy_entry = TRUE;
1603 /* Only the topmost 3 bytes participate fully */
1604 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1605 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1606 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1607 tp->is_dummy_entry = TRUE;
1615 /* Now try looking in the manufacturer table. */
1616 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1617 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1618 manufp->name, addr[3], addr[4], addr[5]);
1619 tp->is_dummy_entry = TRUE;
1623 /* Now try looking for it in the well-known-address
1624 tables for well-known address ranges larger than 2^24. */
1627 /* Only the topmost 2 bytes participate fully */
1628 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1629 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1630 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1632 tp->is_dummy_entry = TRUE;
1642 /* Only the topmost byte participates fully */
1643 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1644 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1645 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1647 tp->is_dummy_entry = TRUE;
1655 for (mask = 7; mask > 0; mask--) {
1656 /* Not even the topmost byte participates fully */
1657 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1658 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1659 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1660 addr[3], addr[4], addr[5]);
1661 tp->is_dummy_entry = TRUE;
1666 /* No match whatsoever. */
1667 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1668 tp->is_dummy_entry = TRUE;
1671 g_strlcpy(tp->name, eth->name, MAXNAMELEN);
1672 tp->is_dummy_entry = FALSE;
1677 } /* eth_name_lookup */
1680 eth_addr_lookup(const gchar *name)
1684 hashether_t **table = eth_table;
1687 /* to be optimized (hash table from name to addr) */
1688 for (i = 0; i < HASHETHSIZE; i++) {
1691 if (strcmp(tp->name, name) == 0)
1697 /* not in hash table : performs a file lookup */
1699 if ((eth = get_ethbyname(name)) == NULL)
1702 /* add new entry in hash table */
1704 tp = add_eth_name(eth->addr, name);
1708 } /* eth_addr_lookup */
1713 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1716 * We allow three address separators (':', '-', and '.'),
1717 * as well as no separators
1721 guint32 a, a0, a1, a2, a3;
1722 gboolean found_single_number = FALSE;
1724 if ((cp = strchr(line, '#')))
1727 if ((cp = strtok(line, " \t\n")) == NULL)
1730 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1731 * fill a and found_single_number is TRUE,
1734 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1735 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1736 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1737 if (sscanf(cp, "%x", &a) == 1) {
1738 found_single_number = TRUE;
1747 if ((cp = strtok(NULL, " \t\n")) == NULL)
1750 if (found_single_number) {
1754 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1757 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1761 } /* parse_ipxnets_line */
1763 static FILE *ipxnet_p = NULL;
1766 set_ipxnetent(char *path)
1771 ipxnet_p = ws_fopen(path, "r");
1787 static ipxnet_t ipxnet;
1788 static int size = 0;
1789 static char *buf = NULL;
1791 if (ipxnet_p == NULL)
1794 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1795 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1802 } /* get_ipxnetent */
1805 get_ipxnetbyname(const gchar *name)
1809 set_ipxnetent(g_ipxnets_path);
1811 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1814 if (ipxnet == NULL) {
1817 set_ipxnetent(g_pipxnets_path);
1819 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1827 } /* get_ipxnetbyname */
1830 get_ipxnetbyaddr(guint32 addr)
1834 set_ipxnetent(g_ipxnets_path);
1836 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1838 if (ipxnet == NULL) {
1841 set_ipxnetent(g_pipxnets_path);
1843 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1851 } /* get_ipxnetbyaddr */
1854 initialize_ipxnets(void)
1856 /* Compute the pathname of the ipxnets file.
1858 * XXX - is there a notion of an "ipxnets file" in any flavor of
1859 * UNIX, or with any add-on Netware package for UNIX? If not,
1860 * should the UNIX version of the ipxnets file be in the datafile
1861 * directory as well?
1863 if (g_ipxnets_path == NULL) {
1864 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1865 get_systemfile_dir(), ENAME_IPXNETS);
1868 /* Set g_pipxnets_path here, but don't actually do anything
1869 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1871 if (g_pipxnets_path == NULL)
1872 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1874 } /* initialize_ipxnets */
1876 static hashipxnet_t *
1877 add_ipxnet_name(guint addr, const gchar *name)
1882 hash_idx = HASH_IPX_NET(addr);
1884 tp = ipxnet_table[hash_idx];
1887 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1890 if (tp->next == NULL) {
1891 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1900 g_strlcpy(tp->name, name, MAXNAMELEN);
1902 new_resolved_objects = TRUE;
1906 } /* add_ipxnet_name */
1909 ipxnet_name_lookup(const guint addr)
1915 hash_idx = HASH_IPX_NET(addr);
1917 tp = ipxnet_table[hash_idx];
1920 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1923 if (tp->addr == addr) {
1926 if (tp->next == NULL) {
1927 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1935 /* fill in a new entry */
1940 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1942 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1945 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1950 } /* ipxnet_name_lookup */
1953 ipxnet_addr_lookup(const gchar *name, gboolean *success)
1957 hashipxnet_t **table = ipxnet_table;
1960 /* to be optimized (hash table from name to addr) */
1961 for (i = 0; i < HASHIPXNETSIZE; i++) {
1964 if (strcmp(tp->name, name) == 0) {
1972 /* not in hash table : performs a file lookup */
1974 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1979 /* add new entry in hash table */
1981 tp = add_ipxnet_name(ipxnet->addr, name);
1986 } /* ipxnet_addr_lookup */
1989 read_hosts_file (const char *hostspath)
1995 guint32 host_addr[4]; /* IPv4 or IPv6 */
1996 struct e_in6_addr ip6_addr;
2001 * See the hosts(4) or hosts(5) man page for hosts file format
2002 * (not available on all systems).
2004 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2007 while (fgetline(&line, &size, hf) >= 0) {
2008 if ((cp = strchr(line, '#')))
2011 if ((cp = strtok(line, " \t")) == NULL)
2012 continue; /* no tokens in the line */
2014 ret = inet_pton(AF_INET6, cp, &host_addr);
2016 continue; /* error parsing */
2021 /* Not valid IPv6 - valid IPv4? */
2022 if (inet_pton(AF_INET, cp, &host_addr) != 1)
2027 if ((cp = strtok(NULL, " \t")) == NULL)
2028 continue; /* no host name */
2031 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2032 add_ipv6_name(&ip6_addr, cp);
2034 add_ipv4_name(host_addr[0], cp);
2037 * Add the aliases, too, if there are any.
2039 while ((cp = strtok(NULL, " \t")) != NULL) {
2041 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2042 add_ipv6_name(&ip6_addr, cp);
2044 add_ipv4_name(host_addr[0], cp);
2051 } /* read_hosts_file */
2054 add_ip_name_from_string (const char *addr, const char *name)
2056 guint32 host_addr[4]; /* IPv4 */
2057 struct e_in6_addr ip6_addr; /* IPv6 */
2061 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2063 /* Error parsing address */
2070 /* Not valid IPv6 - valid IPv4? */
2071 if (inet_pton(AF_INET, addr, &host_addr) != 1)
2072 return FALSE; /* no */
2077 add_ipv6_name(&ip6_addr, name);
2079 add_ipv4_name(host_addr[0], name);
2083 } /* add_ip_name_from_string */
2086 /* Read in a list of subnet definition - name pairs.
2087 * <line> = <comment> | <entry> | <whitespace>
2088 * <comment> = <whitespace>#<any>
2089 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2090 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2091 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2092 * <subnet_mask_length> is a decimal 1-31
2093 * <subnet_name> is a string containing no whitespace.
2094 * <whitespace> = (space | tab)+
2095 * Any malformed entries are ignored.
2096 * Any trailing data after the subnet_name is ignored.
2101 read_subnets_file (const char *subnetspath)
2107 guint32 host_addr; /* IPv4 ONLY */
2110 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2113 while (fgetline(&line, &size, hf) >= 0) {
2114 if ((cp = strchr(line, '#')))
2117 if ((cp = strtok(line, " \t")) == NULL)
2118 continue; /* no tokens in the line */
2121 /* Expected format is <IP4 address>/<subnet length> */
2122 cp2 = strchr(cp, '/');
2127 *cp2 = '\0'; /* Cut token */
2130 /* Check if this is a valid IPv4 address */
2131 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
2135 mask_length = atoi(cp2);
2136 if(0 >= mask_length || mask_length > 31) {
2137 continue; /* invalid mask length */
2140 if ((cp = strtok(NULL, " \t")) == NULL)
2141 continue; /* no subnet name */
2143 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2149 } /* read_subnets_file */
2151 static subnet_entry_t
2152 subnet_lookup(const guint32 addr)
2154 subnet_entry_t subnet_entry;
2157 /* Search mask lengths linearly, longest first */
2159 i = SUBNETLENGTHSIZE;
2160 while(have_subnet_entry && i > 0) {
2161 guint32 masked_addr;
2162 subnet_length_entry_t* length_entry;
2164 /* Note that we run from 31 (length 32) to 0 (length 1) */
2166 g_assert(i < SUBNETLENGTHSIZE);
2169 length_entry = &subnet_length_entries[i];
2171 if(NULL != length_entry->subnet_addresses) {
2175 masked_addr = addr & length_entry->mask;
2176 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2178 tp = length_entry->subnet_addresses[hash_idx];
2179 while(tp != NULL && tp->addr != masked_addr) {
2184 subnet_entry.mask = length_entry->mask;
2185 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2186 subnet_entry.name = tp->name;
2187 return subnet_entry;
2192 subnet_entry.mask = 0;
2193 subnet_entry.mask_length = 0;
2194 subnet_entry.name = NULL;
2196 return subnet_entry;
2199 /* Add a subnet-definition - name pair to the set.
2200 * The definition is taken by masking the address passed in with the mask of the
2204 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2206 subnet_length_entry_t* entry;
2210 g_assert(mask_length > 0 && mask_length <= 32);
2212 entry = &subnet_length_entries[mask_length - 1];
2214 subnet_addr &= entry->mask;
2216 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2218 if(NULL == entry->subnet_addresses) {
2219 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
2222 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2223 if(tp->addr == subnet_addr) {
2224 return; /* XXX provide warning that an address was repeated? */
2226 hashipv4_t * new_tp = g_new(hashipv4_t,1);
2231 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
2235 tp->addr = subnet_addr;
2236 tp->is_dummy_entry = FALSE; /*Never used again...*/
2237 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2238 have_subnet_entry = TRUE;
2242 get_subnet_mask(const guint32 mask_length) {
2244 static guint32 masks[SUBNETLENGTHSIZE];
2245 static gboolean initialised = FALSE;
2248 memset(masks, 0, sizeof(masks));
2252 /* XXX There must be a better way to do this than
2253 * hand-coding the values, but I can't seem to
2257 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2258 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2259 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2260 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2261 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2262 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2263 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2264 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2266 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2267 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2268 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2269 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2270 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2271 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2272 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2273 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2275 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2276 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2277 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2278 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2279 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2280 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2281 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2282 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2284 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2285 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2286 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2287 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2288 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2289 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2290 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2291 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2294 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2295 g_assert_not_reached();
2298 return masks[mask_length - 1];
2303 subnet_name_lookup_init(void)
2308 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2309 guint32 length = i + 1;
2311 subnet_length_entries[i].subnet_addresses = NULL;
2312 subnet_length_entries[i].mask_length = length;
2313 subnet_length_entries[i].mask = get_subnet_mask(length);
2316 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2317 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2318 report_open_failure(subnetspath, errno, FALSE);
2320 g_free(subnetspath);
2323 * Load the global subnets file, if we have one.
2325 subnetspath = get_datafile_path(ENAME_SUBNETS);
2326 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2327 report_open_failure(subnetspath, errno, FALSE);
2329 g_free(subnetspath);
2333 * External Functions
2337 host_name_lookup_init(void) {
2340 #ifdef HAVE_GNU_ADNS
2343 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2344 static char rootpath_ot[] = "\\hosts";
2346 #endif /*GNU_ADNS */
2349 * Load the user's hosts file, if they have one.
2351 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2352 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2353 report_open_failure(hostspath, errno, FALSE);
2358 * Load the global hosts file, if we have one.
2360 hostspath = get_datafile_path(ENAME_HOSTS);
2361 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2362 report_open_failure(hostspath, errno, FALSE);
2367 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2368 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2370 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2371 async_dns_initialized = TRUE;
2373 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2377 #ifdef HAVE_GNU_ADNS
2379 * We're using GNU ADNS, which doesn't check the system hosts file;
2380 * we load that file ourselves.
2384 sysroot = getenv_utf8("WINDIR");
2385 if (sysroot != NULL) {
2387 * The file should be under WINDIR.
2388 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2389 * %WINDIR%\system32\drivers\etc\hosts.
2390 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2392 * XXX - should we base it on the dwPlatformId value from
2395 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2396 if (!read_hosts_file(hostspath)) {
2398 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2399 read_hosts_file(hostspath);
2404 read_hosts_file("/etc/hosts");
2407 /* XXX - Any flags we should be using? */
2408 /* XXX - We could provide config settings for DNS servers, and
2409 pass them to ADNS with adns_init_strcfg */
2410 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2412 * XXX - should we report the error? I'm assuming that some crashes
2413 * reported on a Windows machine with TCP/IP not configured are due
2414 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2415 * ADNS in a state where it crashes due to that. We'll still try
2416 * doing name resolution anyway.
2420 async_dns_initialized = TRUE;
2421 async_dns_in_flight = 0;
2422 #endif /* HAVE_GNU_ADNS */
2423 #endif /* HAVE_C_ARES */
2425 subnet_name_lookup_init();
2430 host_name_lookup_process(gpointer data _U_) {
2431 async_dns_queue_msg_t *caqm;
2432 struct timeval tv = { 0, 0 };
2435 gboolean nro = new_resolved_objects;
2437 new_resolved_objects = FALSE;
2439 if (!async_dns_initialized)
2440 /* c-ares not initialized. Bail out and cancel timers. */
2443 async_dns_queue_head = g_list_first(async_dns_queue_head);
2445 while (async_dns_queue_head && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2446 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2447 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2448 if (caqm->family == AF_INET) {
2449 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2450 c_ares_ghba_cb, caqm);
2451 async_dns_in_flight++;
2452 } else if (caqm->family == AF_INET6) {
2453 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2454 AF_INET6, c_ares_ghba_cb, caqm);
2455 async_dns_in_flight++;
2461 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2463 select(nfds, &rfds, &wfds, NULL, &tv);
2464 ares_process(ghba_chan, &rfds, &wfds);
2467 /* Any new entries? */
2472 host_name_lookup_cleanup(void) {
2475 cur = g_list_first(async_dns_queue_head);
2478 cur = g_list_next (cur);
2481 g_list_free(async_dns_queue_head);
2483 if (async_dns_initialized) {
2484 ares_destroy(ghba_chan);
2485 ares_destroy(ghbn_chan);
2487 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2488 ares_library_cleanup();
2490 async_dns_initialized = FALSE;
2493 #elif defined(HAVE_GNU_ADNS)
2495 /* XXX - The ADNS "documentation" isn't very clear:
2496 * - Do we need to keep our query structures around?
2499 host_name_lookup_process(gpointer data _U_) {
2500 async_dns_queue_msg_t *almsg;
2502 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2507 gboolean nro = new_resolved_objects;
2509 new_resolved_objects = FALSE;
2510 async_dns_queue_head = g_list_first(async_dns_queue_head);
2512 cur = async_dns_queue_head;
2513 while (cur && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2514 almsg = (async_dns_queue_msg_t *) cur->data;
2515 if (! almsg->submitted && almsg->type == AF_INET) {
2516 addr_bytes = (guint8 *) &almsg->ip4_addr;
2517 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2518 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2519 /* XXX - what if it fails? */
2520 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2521 almsg->submitted = TRUE;
2522 async_dns_in_flight++;
2527 cur = async_dns_queue_head;
2530 almsg = (async_dns_queue_msg_t *) cur->data;
2531 if (almsg->submitted) {
2532 ret = adns_check(ads, &almsg->query, &ans, NULL);
2534 if (ans->status == adns_s_ok) {
2535 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2542 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2544 async_dns_in_flight--;
2548 /* Keep the timeout in place */
2553 host_name_lookup_cleanup(void) {
2556 async_dns_queue_head = g_list_first(async_dns_queue_head);
2557 while (async_dns_queue_head) {
2558 qdata = async_dns_queue_head->data;
2559 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2563 if (async_dns_initialized)
2565 async_dns_initialized = FALSE;
2568 #else /* HAVE_GNU_ADNS */
2571 host_name_lookup_process(gpointer data _U_) {
2572 gboolean nro = new_resolved_objects;
2574 new_resolved_objects = FALSE;
2580 host_name_lookup_cleanup(void) {
2583 #endif /* HAVE_C_ARES */
2585 extern const gchar *
2586 get_hostname(const guint addr)
2589 gboolean resolve = g_resolv_flags & RESOLV_NETWORK;
2590 hashipv4_t *tp = host_lookup(addr, resolve, &found);
2598 /* -------------------------- */
2600 extern const gchar *
2601 get_hostname6(const struct e_in6_addr *addr)
2604 gboolean resolve = g_resolv_flags & RESOLV_NETWORK;
2605 hashipv6_t *tp = host_lookup6(addr, resolve, &found);
2607 if (!resolve || E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
2612 /* -------------------------- */
2614 add_ipv4_name(const guint addr, const gchar *name)
2619 hash_idx = HASH_IPV4_ADDRESS(addr);
2621 tp = ipv4_table[hash_idx];
2624 tp = ipv4_table[hash_idx] = new_ipv4(addr);
2627 if (tp->addr == addr) {
2628 /* address already known */
2629 if (!tp->is_dummy_entry) {
2632 /* replace this dummy entry with the new one */
2636 if (tp->next == NULL) {
2637 tp->next = new_ipv4(addr);
2644 g_strlcpy(tp->name, name, MAXNAMELEN);
2646 new_resolved_objects = TRUE;
2647 } /* add_ipv4_name */
2649 /* -------------------------- */
2651 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2656 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2658 tp = ipv6_table[hash_idx];
2661 tp = ipv6_table[hash_idx] = new_ipv6(addrp);
2664 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2665 /* address already known */
2666 if (!tp->is_dummy_entry) {
2669 /* replace this dummy entry with the new one */
2673 if (tp->next == NULL) {
2674 tp->next = new_ipv6(addrp);
2682 g_strlcpy(tp->name, name, MAXNAMELEN);
2684 new_resolved_objects = TRUE;
2686 } /* add_ipv6_name */
2688 /* -----------------
2689 * unsigned integer to ascii
2694 gchar *bp = ep_alloc(MAXNAMELEN);
2696 /* XXX, guint32_to_str() ? */
2697 guint32_to_str_buf(port, bp, MAXNAMELEN);
2703 get_udp_port(guint port)
2706 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2707 return ep_utoa(port);
2710 return serv_name_lookup(port, PT_UDP);
2712 } /* get_udp_port */
2715 get_dccp_port(guint port)
2718 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2719 return ep_utoa(port);
2722 return serv_name_lookup(port, PT_DCCP);
2724 } /* get_dccp_port */
2728 get_tcp_port(guint port)
2731 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2732 return ep_utoa(port);
2735 return serv_name_lookup(port, PT_TCP);
2737 } /* get_tcp_port */
2740 get_sctp_port(guint port)
2743 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2744 return ep_utoa(port);
2747 return serv_name_lookup(port, PT_SCTP);
2749 } /* get_sctp_port */
2752 get_addr_name(const address *addr)
2754 const gchar *result;
2756 result = solve_address_to_name(addr);
2761 /* if it gets here, either it is of type AT_NONE, */
2762 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
2764 if (addr->type == AT_NONE){
2768 /* We need an ephemeral allocated string */
2769 return ep_address_to_str(addr);
2773 se_get_addr_name(const address *addr)
2775 const gchar *result;
2777 result = se_solve_address_to_name(addr);
2782 /* if it gets here, either it is of type AT_NONE, */
2783 /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
2785 if (addr->type == AT_NONE){
2789 /* We need a "permanently" allocated string */
2790 return se_address_to_str(addr);
2794 get_addr_name_buf(const address *addr, gchar *buf, gsize size)
2796 const gchar *result = get_addr_name(addr);
2798 g_strlcpy(buf, result, size);
2799 } /* get_addr_name_buf */
2803 get_ether_name(const guint8 *addr)
2806 gboolean resolve = g_resolv_flags & RESOLV_MAC;
2808 if (!(g_resolv_flags & RESOLV_MAC))
2809 return ether_to_str(addr);
2811 if (resolve && !eth_resolution_initialized) {
2812 initialize_ethers();
2813 eth_resolution_initialized = 1;
2816 tp = eth_name_lookup(addr, resolve);
2818 } /* get_ether_name */
2820 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2821 * If it's not found, simply return NULL.
2824 get_ether_name_if_known(const guint8 *addr)
2828 /* Initialize ether structs if we're the first
2829 * ether-related function called */
2830 if (!(g_resolv_flags & RESOLV_MAC))
2833 if (!eth_resolution_initialized) {
2834 initialize_ethers();
2835 eth_resolution_initialized = 1;
2838 /* eth_name_lookup will create a hash entry if it doesn't exist */
2839 tp = eth_name_lookup(addr, TRUE);
2840 g_assert(tp != NULL);
2842 if (! tp->is_dummy_entry) {
2843 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
2847 /* Name was created */
2853 get_ether_addr(const gchar *name)
2856 /* force resolution (do not check g_resolv_flags) */
2858 if (!eth_resolution_initialized) {
2859 initialize_ethers();
2860 eth_resolution_initialized = 1;
2863 return eth_addr_lookup(name);
2865 } /* get_ether_addr */
2868 add_ether_byip(const guint ip, const guint8 *eth)
2874 /* first check that IP address can be resolved */
2875 if (!(g_resolv_flags & RESOLV_NETWORK))
2878 if ((host = host_name_lookup(ip, &found)) == NULL)
2881 /* ok, we can add this entry in the ethers hashtable */
2884 add_eth_name(eth, host);
2886 } /* add_ether_byip */
2888 extern const gchar *
2889 get_ipxnet_name(const guint32 addr)
2892 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2893 return ipxnet_to_str_punct(addr, '\0');
2896 if (!ipxnet_resolution_initialized) {
2897 initialize_ipxnets();
2898 ipxnet_resolution_initialized = 1;
2901 return ipxnet_name_lookup(addr);
2903 } /* get_ipxnet_name */
2906 get_ipxnet_addr(const gchar *name, gboolean *known)
2911 /* force resolution (do not check g_resolv_flags) */
2913 if (!ipxnet_resolution_initialized) {
2914 initialize_ipxnets();
2915 ipxnet_resolution_initialized = 1;
2918 addr = ipxnet_addr_lookup(name, &success);
2923 } /* get_ipxnet_addr */
2925 extern const gchar *
2926 get_manuf_name(const guint8 *addr)
2929 hashmanuf_t *manufp;
2931 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2932 initialize_ethers();
2933 eth_resolution_initialized = 1;
2936 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2937 cur=ep_alloc(MAXMANUFLEN);
2938 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2942 return manufp->name;
2944 } /* get_manuf_name */
2948 get_manuf_name_if_known(const guint8 *addr)
2950 hashmanuf_t *manufp;
2952 if (!eth_resolution_initialized) {
2953 initialize_ethers();
2954 eth_resolution_initialized = 1;
2957 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2961 return manufp->name;
2963 } /* get_manuf_name_if_known */
2967 #define GHI_TIMEOUT (250 * 1000)
2969 #if ( ( ARES_VERSION_MAJOR < 1 ) \
2970 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
2971 c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
2973 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
2976 * XXX - If we wanted to be really fancy we could cache results here and
2977 * look them up in get_host_ipaddr* below.
2979 async_hostent_t *ahp = arg;
2980 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
2981 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
2982 ahp->copied = hp->h_length;
2985 #endif /* HAVE_C_ARES */
2987 /* Translate a string, assumed either to be a dotted-quad IP address or
2988 * a host name, to a numeric IP address. Return TRUE if we succeed and
2989 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2990 * Used more in the dfilter parser rather than in packet dissectors */
2992 get_host_ipaddr(const char *host, guint32 *addrp)
2994 struct in_addr ipaddr;
2996 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
2999 async_hostent_t ahe;
3000 #else /* HAVE_C_ARES */
3002 #endif /* HAVE_C_ARES */
3005 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3006 * inet_pton(AF_INET) does not support hexadecimal notation nor
3007 * less-than-4 octet notation.
3009 if (!inet_aton(host, &ipaddr)) {
3010 if (! (g_resolv_flags & RESOLV_NETWORK)) {
3013 /* It's not a valid dotted-quad IP address; is it a valid
3016 if (! (g_resolv_flags & RESOLV_CONCURRENT) ||
3017 prefs.name_resolve_concurrency < 1 ||
3018 ! async_dns_initialized) {
3021 ahe.addr_size = (int) sizeof (struct in_addr);
3024 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3027 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3029 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3030 select(nfds, &rfds, &wfds, NULL, tvp);
3031 ares_process(ghbn_chan, &rfds, &wfds);
3033 ares_cancel(ghbn_chan);
3034 if (ahe.addr_size == ahe.copied) {
3038 #else /* ! HAVE_C_ARES */
3039 hp = gethostbyname(host);
3043 /* Apparently, some versions of gethostbyaddr can
3044 * return IPv6 addresses. */
3045 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3046 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3050 #endif /* HAVE_C_ARES */
3052 /* Does the string really contain dotted-quad IP?
3053 * Check against inet_atons that accept strings such as
3054 * "130.230" as valid addresses and try to convert them
3055 * to some form of a classful (host.net) notation.
3057 unsigned int a0, a1, a2, a3;
3058 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3062 *addrp = g_ntohl(ipaddr.s_addr);
3067 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3068 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3069 * return FALSE if we fail.
3072 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3075 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3078 async_hostent_t ahe;
3079 #else /* HAVE_C_ARES */
3081 #endif /* HAVE_C_ARES */
3083 if (inet_pton(AF_INET6, host, addrp) == 1)
3086 if (! (g_resolv_flags & RESOLV_NETWORK)) {
3092 if (! (g_resolv_flags & RESOLV_CONCURRENT) ||
3093 prefs.name_resolve_concurrency < 1 ||
3094 ! async_dns_initialized) {
3097 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3100 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3103 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3105 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3106 select(nfds, &rfds, &wfds, NULL, tvp);
3107 ares_process(ghbn_chan, &rfds, &wfds);
3109 ares_cancel(ghbn_chan);
3110 if (ahe.addr_size == ahe.copied) {
3113 #elif defined(HAVE_GETHOSTBYNAME2)
3114 hp = gethostbyname2(host, AF_INET6);
3115 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3116 memcpy(addrp, hp->h_addr, hp->h_length);
3125 * Find out whether a hostname resolves to an ip or ipv6 address
3126 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3127 * that we don't know)
3129 const char* host_ip_af(const char *host
3130 #ifndef HAVE_GETHOSTBYNAME2
3135 #ifdef HAVE_GETHOSTBYNAME2
3137 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";