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))
152 typedef struct hashipv4 {
154 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
155 gboolean resolve; /* already tried to resolve it */
156 struct hashipv4 *next;
158 gchar name[MAXNAMELEN];
161 /* hash table used for IPv6 lookup */
163 #define HASH_IPV6_ADDRESS(addr) \
164 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
166 typedef struct hashipv6 {
167 struct e_in6_addr addr;
168 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
169 gboolean resolve; /* */
170 struct hashipv6 *next;
171 gchar ip6[47]; /* XX */
172 gchar name[MAXNAMELEN];
175 /* Array of entries of subnets of different lengths */
177 gsize mask_length; /*1-32*/
178 guint32 mask; /* e.g. 255.255.255.*/
179 hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
180 } subnet_length_entry_t;
182 /* hash table used for TCP/UDP/SCTP port lookup */
184 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
186 typedef struct hashport {
188 struct hashport *next;
189 gchar name[MAXNAMELEN];
192 /* hash table used for IPX network lookup */
194 /* XXX - check goodness of hash function */
196 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
198 typedef struct hashipxnet {
200 struct hashipxnet *next;
201 gchar name[MAXNAMELEN];
204 /* hash tables used for ethernet and manufacturer lookup */
206 #define HASH_ETH_ADDRESS(addr) \
207 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
210 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
212 typedef struct hashmanuf {
213 struct hashmanuf *next;
218 #define HASHETHER_STATUS_UNRESOLVED 1
219 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
220 #define HASHETHER_STATUS_RESOLVED_NAME 3
222 typedef struct hashether {
223 struct hashether *next;
224 guint status; /* (See above) */
227 char resolved_name[MAXNAMELEN];
230 typedef struct hashwka {
231 struct hashwka *next;
233 char name[MAXNAMELEN];
236 /* internal ethernet type */
238 typedef struct _ether
241 char name[MAXNAMELEN];
244 /* internal ipxnet type */
246 typedef struct _ipxnet
249 char name[MAXNAMELEN];
252 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
253 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
255 static hashport_t **cb_port_table;
256 static gchar *cb_service;
258 static hashport_t *udp_port_table[HASHPORTSIZE];
259 static hashport_t *tcp_port_table[HASHPORTSIZE];
260 static hashport_t *sctp_port_table[HASHPORTSIZE];
261 static hashport_t *dccp_port_table[HASHPORTSIZE];
262 static hashether_t *eth_table[HASHETHSIZE];
263 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
264 static hashwka_t *(*wka_table[48])[HASHETHSIZE];
265 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
267 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
268 static gboolean have_subnet_entry = FALSE;
270 static gboolean eth_resolution_initialized = FALSE;
271 static int ipxnet_resolution_initialized = 0;
272 static int service_resolution_initialized = 0;
273 static gboolean new_resolved_objects = FALSE;
275 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
276 static void add_serv_port_cb(const guint32 port);
279 * Flag controlling what names to resolve.
281 guint32 gbl_resolv_flags;
284 * Global variables (can be changed in GUI sections)
285 * XXX - they could be changed in GUI code, but there's currently no
286 * GUI code to change them.
289 gchar *g_ethers_path = NULL; /* global ethers file */
290 gchar *g_pethers_path = NULL; /* personal ethers file */
291 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
292 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
293 gchar *g_services_path = NULL; /* global services file */
294 gchar *g_pservices_path = NULL; /* personal services file */
295 /* first resolving call */
300 * Submitted queries trigger a callback (c_ares_ghba_cb()).
301 * Queries are added to c_ares_queue_head. During processing, queries are
302 * popped off the front of c_ares_queue_head and submitted using
303 * ares_gethostbyaddr().
304 * The callback processes the response, then frees the request.
307 typedef struct _async_dns_queue_msg
311 struct e_in6_addr ip6;
314 } async_dns_queue_msg_t;
316 typedef struct _async_hostent {
322 #if ( ( ARES_VERSION_MAJOR < 1 ) \
323 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
324 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
326 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
329 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
330 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
337 * Submitted queries have to be checked individually using adns_check().
338 * Queries are added to adns_queue_head. During processing, the list is
339 * iterated twice: once to request queries up to the concurrency limit,
340 * and once to check the status of each query.
345 typedef struct _async_dns_queue_msg
351 } async_dns_queue_msg_t;
353 #endif /* HAVE_GNU_ADNS */
354 #endif /* HAVE_C_ARES */
356 static gboolean async_dns_initialized = FALSE;
357 static int async_dns_in_flight = 0;
358 static GList *async_dns_queue_head = NULL;
360 /* push a dns request */
362 add_async_dns_ipv4(int type, guint32 addr)
364 async_dns_queue_msg_t *msg;
366 msg = g_malloc(sizeof(async_dns_queue_msg_t));
369 msg->addr.ip4 = addr;
372 msg->ip4_addr = addr;
373 msg->submitted = FALSE;
375 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
383 const gchar* name; /* Shallow copy */
387 * Miscellaneous functions
391 fgetline(char **buf, int *size, FILE *fp)
396 if (fp == NULL || buf == NULL)
403 *buf = g_malloc(*size);
413 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
414 if (len+1 >= *size) {
415 *buf = g_realloc(*buf, *size += BUFSIZ);
420 if (len == 0 && c == EOF)
431 * Local function definitions
433 static subnet_entry_t subnet_lookup(const guint32 addr);
434 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
438 add_service_name(hashport_t **proto_table, const guint port, const char *service_name)
444 hash_idx = HASH_PORT(port);
445 tp = proto_table[hash_idx];
448 tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
451 if( tp->port == port ) {
454 if (tp->next == NULL) {
455 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
463 /* fill in a new entry */
467 g_strlcpy(tp->name, service_name, MAXNAMELEN);
469 new_resolved_objects = TRUE;
474 parse_service_line (char *line)
477 * See the services(4) or services(5) man page for services file format
478 * (not available on all systems).
485 range_t *port_rng = NULL;
486 guint32 max_port = MAX_UDP_PORT;
488 if ((cp = strchr(line, '#')))
491 if ((cp = strtok(line, " \t")) == NULL)
496 if ((cp = strtok(NULL, " \t")) == NULL)
501 if ((cp = strtok(cp, "/")) == NULL)
504 if ((cp = strtok(NULL, "/")) == NULL)
507 /* seems we got all interesting things from the file */
508 if(strcmp(cp, "tcp") == 0) {
509 max_port = MAX_TCP_PORT;
510 cb_port_table = tcp_port_table;
512 else if(strcmp(cp, "udp") == 0) {
513 max_port = MAX_UDP_PORT;
514 cb_port_table = udp_port_table;
516 else if(strcmp(cp, "sctp") == 0) {
517 max_port = MAX_SCTP_PORT;
518 cb_port_table = sctp_port_table;
520 else if(strcmp(cp, "dccp") == 0) {
521 max_port = MAX_DCCP_PORT;
522 cb_port_table = dccp_port_table;
527 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
528 /* some assertion here? */
532 cb_service = service;
533 range_foreach(port_rng, add_serv_port_cb);
535 } /* parse_service_line */
539 add_serv_port_cb(const guint32 port)
542 add_service_name(cb_port_table, port, cb_service);
548 parse_services_file(const char * path)
552 static char *buf = NULL;
554 /* services hash table initialization */
555 serv_p = ws_fopen(path, "r");
560 while (fgetline(&buf, &size, serv_p) >= 0) {
561 parse_service_line (buf);
568 initialize_services(void)
571 /* the hash table won't ignore duplicates, so use the personal path first */
573 /* set personal services path */
574 if (g_pservices_path == NULL)
575 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
577 parse_services_file(g_pservices_path);
579 /* Compute the pathname of the services file. */
580 if (g_services_path == NULL) {
581 g_services_path = get_datafile_path(ENAME_SERVICES);
584 parse_services_file(g_services_path);
586 } /* initialize_services */
591 *serv_name_lookup(const guint port, const port_type proto)
596 const char *serv_proto = NULL;
597 struct servent *servp;
600 if (!service_resolution_initialized) {
601 initialize_services();
602 service_resolution_initialized = 1;
607 table = udp_port_table;
611 table = tcp_port_table;
615 table = sctp_port_table;
619 table = dccp_port_table;
623 /* not yet implemented */
628 hash_idx = HASH_PORT(port);
629 tp = table[hash_idx];
632 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
635 if( tp->port == port ) {
638 if (tp->next == NULL) {
639 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
647 /* fill in a new entry */
651 if (!(gbl_resolv_flags & RESOLV_TRANSPORT) ||
652 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
654 guint32_to_str_buf(port, tp->name, MAXNAMELEN);
656 g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
661 } /* serv_name_lookup */
664 /* Fill in an IP4 structure with info from subnets file or just with the
665 * string form of the address.
668 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
670 subnet_entry_t subnet_entry;
672 if (tp->is_dummy_entry)
673 return; /* already done */
675 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
677 /* Do we have a subnet for this address? */
678 subnet_entry = subnet_lookup(addr);
679 if(0 != subnet_entry.mask) {
680 /* Print name, then '.' then IP address after subnet mask */
682 gchar buffer[MAX_IP_STR_LEN];
686 host_addr = addr & (~(guint32)subnet_entry.mask);
687 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
690 /* Skip to first octet that is not totally masked
691 * If length of mask is 32, we chomp the whole address.
692 * If the address string starts '.' (should not happen?),
695 i = subnet_entry.mask_length / 8;
696 while(*(paddr) != '\0' && i > 0) {
697 if(*(++paddr) == '.') {
702 /* There are more efficient ways to do this, but this is safe if we
703 * trust g_snprintf and MAXNAMELEN
705 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
707 ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
714 #if ( ( ARES_VERSION_MAJOR < 1 ) \
715 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
716 c_ares_ghba_cb(void *arg, int status, struct hostent *he) {
718 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
720 async_dns_queue_msg_t *caqm = arg;
724 async_dns_in_flight--;
726 if (status == ARES_SUCCESS) {
727 for (p = he->h_addr_list; *p != NULL; p++) {
728 switch(caqm->family) {
730 add_ipv4_name(caqm->addr.ip4, he->h_name);
733 add_ipv6_name(&caqm->addr.ip6, he->h_name);
736 /* Throw an exception? */
743 #endif /* HAVE_C_ARES */
745 /* --------------- */
747 new_ipv4(const guint addr)
749 hashipv4_t *tp = g_malloc(sizeof(hashipv4_t));
753 tp->is_dummy_entry = FALSE;
754 ip_to_str_buf((guint8 *)&addr, tp->ip, sizeof(tp->ip));
759 host_lookup(const guint addr, const gboolean resolve, gboolean *found)
762 hashipv4_t * volatile tp;
763 struct hostent *hostp;
767 hash_idx = HASH_IPV4_ADDRESS(addr);
769 tp = ipv4_table[hash_idx];
772 tp = ipv4_table[hash_idx] = new_ipv4(addr);
775 if( tp->addr == addr ) {
776 if (tp->is_dummy_entry && !tp->resolve)
778 if (tp->is_dummy_entry)
782 if (tp->next == NULL) {
783 tp->next = new_ipv4(addr);
794 if ((gbl_resolv_flags & RESOLV_CONCURRENT) &&
795 prefs.name_resolve_concurrency > 0 &&
796 async_dns_initialized) {
797 add_async_dns_ipv4(AF_INET, addr);
798 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
799 * going to risk changing the semantics.
801 fill_dummy_ip4(addr, tp);
804 #endif /* ASYNC_DNS */
807 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
808 * the name of the host on which it's running; to work around that
809 * botch, we don't try to translate an all-zero IP address to a host
812 if (addr != 0 && (gbl_resolv_flags & RESOLV_NETWORK)) {
813 /* Use async DNS if possible, else fall back to timeouts,
814 * else call gethostbyaddr and hope for the best
817 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
820 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
821 tp->is_dummy_entry = FALSE;
826 /* unknown host or DNS timeout */
832 fill_dummy_ip4(addr, tp);
835 } /* host_name_lookup */
838 host_name_lookup(const guint addr, gboolean *found)
841 tp = host_lookup(addr, TRUE, found);
846 /* --------------- */
848 new_ipv6(const struct e_in6_addr *addr)
850 hashipv6_t *tp = g_malloc(sizeof(hashipv6_t));
854 tp->is_dummy_entry = FALSE;
855 ip6_to_str_buf(addr, tp->ip6);
859 /* ------------------------------------ */
861 host_lookup6(const struct e_in6_addr *addr, const gboolean resolve, gboolean *found)
864 hashipv6_t * volatile tp;
867 async_dns_queue_msg_t *caqm;
868 #endif /* HAVE_C_ARES */
869 struct hostent *hostp;
874 hash_idx = HASH_IPV6_ADDRESS(*addr);
876 tp = ipv6_table[hash_idx];
879 tp = ipv6_table[hash_idx] = new_ipv6(addr);
882 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
883 if (tp->is_dummy_entry && !tp->resolve)
885 if (tp->is_dummy_entry)
889 if (tp->next == NULL) {
890 tp->next = new_ipv6(addr);
903 if ((gbl_resolv_flags & RESOLV_CONCURRENT) &&
904 prefs.name_resolve_concurrency > 0 &&
905 async_dns_initialized) {
906 caqm = g_malloc(sizeof(async_dns_queue_msg_t));
907 caqm->family = AF_INET6;
908 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
909 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
911 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
912 * going to risk changing the semantics.
914 if (!tp->is_dummy_entry) {
915 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
916 ip6_to_str_buf(addr, tp->name);
917 tp->is_dummy_entry = TRUE;
921 #endif /* HAVE_C_ARES */
923 /* Quick hack to avoid DNS/YP timeout */
924 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
927 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
928 tp->is_dummy_entry = FALSE;
934 /* unknown host or DNS timeout */
935 if (!tp->is_dummy_entry) {
936 tp->is_dummy_entry = TRUE;
937 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
946 host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
949 tp = host_lookup6(addr, TRUE, found);
955 solve_address_to_name(const address *addr)
957 switch (addr->type) {
960 return get_ether_name(addr->data);
964 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
965 return get_hostname(ip4_addr);
969 struct e_in6_addr ip6_addr;
970 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
971 return get_hostname6(&ip6_addr);
983 se_solve_address_to_name(const address *addr)
985 switch (addr->type) {
988 return get_ether_name(addr->data);
992 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
993 return get_hostname(ip4_addr);
997 struct e_in6_addr ip6_addr;
998 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
999 return get_hostname6(&ip6_addr);
1003 return se_strdup(addr->data);
1011 * Ethernet / manufacturer resolution
1013 * The following functions implement ethernet address resolution and
1014 * ethers files parsing (see ethers(4)).
1016 * The manuf file has the same format as ethers(4) except that names are
1017 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1018 * only 3 bytes (instead of 6).
1022 * I decide to not use the existing functions (see ethers(3) on some
1023 * operating systems) for the following reasons:
1024 * - performance gains (use of hash tables and some other enhancements),
1025 * - use of two ethers files (system-wide and per user),
1026 * - avoid the use of NIS maps,
1027 * - lack of these functions on some systems.
1029 * So the following functions do _not_ behave as the standard ones.
1036 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1037 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1041 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1042 const gboolean manuf_file)
1049 for (i = 0; i < 6; i++) {
1050 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1051 if (!isxdigit((unsigned char)*cp))
1053 num = strtoul(cp, &p, 16);
1055 return FALSE; /* failed */
1057 return FALSE; /* not a valid octet */
1058 eth->addr[i] = (guint8) num;
1059 cp = p; /* skip past the number */
1061 /* OK, what character terminated the octet? */
1063 /* "/" - this has a mask. */
1065 /* Entries with masks are allowed only in the "manuf" files. */
1068 cp++; /* skip past the '/' to get to the mask */
1069 if (!isdigit((unsigned char)*cp))
1070 return FALSE; /* no sign allowed */
1071 num = strtoul(cp, &p, 10);
1073 return FALSE; /* failed */
1074 cp = p; /* skip past the number */
1075 if (*cp != '\0' && !isspace((unsigned char)*cp))
1076 return FALSE; /* bogus terminator */
1077 if (num == 0 || num >= 48)
1078 return FALSE; /* bogus mask */
1079 /* Mask out the bits not covered by the mask */
1081 for (i = 0; num >= 8; i++, num -= 8)
1082 ; /* skip octets entirely covered by the mask */
1083 /* Mask out the first masked octet */
1084 eth->addr[i] &= (0xFF << (8 - num));
1086 /* Mask out completely-masked-out octets */
1092 /* We're at the end of the address, and there's no mask. */
1094 /* We got 3 bytes, so this is a manufacturer ID. */
1096 /* Manufacturer IDs are only allowed in the "manuf"
1100 /* Indicate that this is a manufacturer ID (0 is not allowed
1107 /* We got 6 bytes, so this is a MAC address.
1108 If we're reading one of the "manuf" files, indicate that
1109 this is a MAC address (48 is not allowed as a mask). */
1115 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1120 /* We don't know the separator used in this number; it can either
1121 be ':', '-', or '.'. */
1122 if (*cp != ':' && *cp != '-' && *cp != '.')
1124 sep = *cp; /* subsequent separators must be the same */
1126 /* It has to be the same as the first separator */
1138 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1139 const gboolean manuf_file)
1142 * See the ethers(4) or ethers(5) man page for ethers file format
1143 * (not available on all systems).
1144 * We allow both ethernet address separators (':' and '-'),
1145 * as well as Wireshark's '.' separator.
1150 if ((cp = strchr(line, '#')))
1153 if ((cp = strtok(line, " \t")) == NULL)
1156 if (!parse_ether_address(cp, eth, mask, manuf_file))
1159 if ((cp = strtok(NULL, " \t")) == NULL)
1162 g_strlcpy(eth->name, cp, MAXNAMELEN);
1166 } /* parse_ether_line */
1168 static FILE *eth_p = NULL;
1171 set_ethent(char *path)
1176 eth_p = ws_fopen(path, "r");
1189 get_ethent(unsigned int *mask, const gboolean manuf_file)
1193 static int size = 0;
1194 static char *buf = NULL;
1199 while (fgetline(&buf, &size, eth_p) >= 0) {
1200 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1210 get_ethbyname(const gchar *name)
1214 set_ethent(g_pethers_path);
1216 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1222 set_ethent(g_ethers_path);
1224 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1232 } /* get_ethbyname */
1235 get_ethbyaddr(const guint8 *addr)
1240 set_ethent(g_pethers_path);
1242 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1248 set_ethent(g_ethers_path);
1250 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1258 } /* get_ethbyaddr */
1261 hash_eth_wka(const guint8 *addr, unsigned int mask)
1264 /* All but the topmost byte is masked out */
1265 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
1269 /* All but the topmost 2 bytes are masked out */
1270 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1275 /* All but the topmost 3 bytes are masked out */
1276 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1277 & (HASHETHSIZE - 1);
1281 /* All but the topmost 4 bytes are masked out */
1282 return ((((addr[0] << 8) | addr[1]) ^
1283 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1288 /* All but the topmost 5 bytes are masked out */
1289 return ((((addr[1] << 8) | addr[2]) ^
1290 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1294 /* No bytes are fully masked out */
1295 return ((((addr[1] << 8) | addr[2]) ^
1296 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1300 static hashmanuf_t *
1301 manuf_hash_new_entry(const guint8 *addr, gchar *name)
1305 mtp = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1306 memcpy(mtp->addr, addr, sizeof(mtp->addr));
1307 /* The length of this name is limited (in the number of UTF-8 characters,
1308 * not bytes) in make-manuf. That doesn't mean a user can't put a longer
1309 * name in their personal manuf file, though...
1311 mtp->name = g_strdup(name);
1314 } /* manuf_hash_new_entry */
1317 wka_hash_new_entry(const guint8 *addr, gchar *name)
1321 wtp = (hashwka_t *)g_malloc(sizeof(hashwka_t));
1322 memcpy(wtp->addr, addr, sizeof(wtp->addr));
1323 g_strlcpy(wtp->name, name, MAXNAMELEN);
1326 } /* wka_hash_new_entry */
1329 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1333 hashwka_t *(*wka_tp)[HASHETHSIZE], *wtp;
1336 * XXX - can we use Standard Annotation Language annotations to
1337 * note that mask, as returned by parse_ether_address() (and thus
1338 * by the routines that call it, and thus passed to us) cannot be > 48,
1339 * or is SAL too weak to express that?
1342 /* This is a well-known MAC address; just add this to the Ethernet
1344 add_eth_name(addr, name);
1349 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1351 hash_idx = HASH_ETH_MANUF(addr);
1352 mtp = manuf_table[hash_idx];
1355 manuf_table[hash_idx] = manuf_hash_new_entry(addr, name);
1359 if (mtp->next == NULL) {
1360 mtp->next = manuf_hash_new_entry(addr, name);
1368 /* This is a range of well-known addresses; add it to the appropriate
1369 well-known-address table, creating that table if necessary. */
1370 wka_tp = wka_table[mask];
1372 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1374 hash_idx = hash_eth_wka(addr, mask);
1376 wtp = (*wka_tp)[hash_idx];
1379 (*wka_tp)[hash_idx] = wka_hash_new_entry(addr, name);
1383 if (memcmp(wtp->addr, addr, sizeof(wtp->addr)) == 0) {
1384 /* address already known */
1387 if (wtp->next == NULL) {
1388 wtp->next = wka_hash_new_entry(addr, name);
1394 } /* add_manuf_name */
1396 static hashmanuf_t *
1397 manuf_name_lookup(const guint8 *addr)
1401 guint8 stripped_addr[3];
1403 hash_idx = HASH_ETH_MANUF(addr);
1405 /* first try to find a "perfect match" */
1406 mtp = manuf_table[hash_idx];
1407 while(mtp != NULL) {
1408 if (memcmp(mtp->addr, addr, sizeof(mtp->addr)) == 0) {
1414 /* Mask out the broadcast/multicast flag but not the locally
1415 * administered flag as localy administered means: not assigend
1416 * by the IEEE but the local administrator instead.
1417 * 0x01 multicast / broadcast bit
1418 * 0x02 locally administered bit */
1419 memcpy(stripped_addr, addr, 3);
1420 stripped_addr[0] &= 0xFE;
1422 mtp = manuf_table[hash_idx];
1423 while(mtp != NULL) {
1424 if (memcmp(mtp->addr, stripped_addr, sizeof(mtp->addr)) == 0) {
1432 } /* manuf_name_lookup */
1435 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1438 hashwka_t *(*wka_tp)[HASHETHSIZE];
1440 guint8 masked_addr[6];
1444 wka_tp = wka_table[mask];
1445 if (wka_tp == NULL) {
1446 /* There are no entries in the table for that mask value, as there is
1447 no table for that mask value. */
1451 /* Get the part of the address covered by the mask. */
1452 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1453 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1454 /* Mask out the first masked octet */
1455 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1457 /* Zero out completely-masked-out octets */
1461 hash_idx = hash_eth_wka(masked_addr, mask);
1463 wtp = (*wka_tp)[hash_idx];
1465 while(wtp != NULL) {
1466 if (memcmp(wtp->addr, masked_addr, sizeof(wtp->addr)) == 0) {
1474 } /* wka_name_lookup */
1477 initialize_ethers(void)
1483 /* Compute the pathname of the ethers file. */
1484 if (g_ethers_path == NULL) {
1485 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1486 get_systemfile_dir(), ENAME_ETHERS);
1489 /* Set g_pethers_path here, but don't actually do anything
1490 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1492 if (g_pethers_path == NULL)
1493 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1495 /* manuf hash table initialization */
1497 /* Compute the pathname of the manuf file */
1498 manuf_path = get_datafile_path(ENAME_MANUF);
1500 /* Read it and initialize the hash table */
1501 set_ethent(manuf_path);
1503 while ((eth = get_ethent(&mask, TRUE))) {
1504 add_manuf_name(eth->addr, mask, eth->name);
1511 } /* initialize_ethers */
1513 /* Resolve ethernet address */
1514 static hashether_t *
1515 eth_addr_resolve(hashether_t *tp) {
1517 const guint8 *addr = tp->addr;
1519 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1520 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1521 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1528 /* Unknown name. Try looking for it in the well-known-address
1529 tables for well-known address ranges smaller than 2^24. */
1532 /* Only the topmost 5 bytes participate fully */
1533 if ((wtp = wka_name_lookup(addr, mask+40)) != NULL) {
1534 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1535 wtp->name, addr[5] & (0xFF >> mask));
1536 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1546 /* Only the topmost 4 bytes participate fully */
1547 if ((wtp = wka_name_lookup(addr, mask+32)) != NULL) {
1548 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1549 wtp->name, addr[4] & (0xFF >> mask), addr[5]);
1550 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1560 /* Only the topmost 3 bytes participate fully */
1561 if ((wtp = wka_name_lookup(addr, mask+24)) != NULL) {
1562 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1563 wtp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1564 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1572 /* Now try looking in the manufacturer table. */
1573 if ((mtp = manuf_name_lookup(addr)) != NULL) {
1574 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1575 mtp->name, addr[3], addr[4], addr[5]);
1576 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1580 /* Now try looking for it in the well-known-address
1581 tables for well-known address ranges larger than 2^24. */
1584 /* Only the topmost 2 bytes participate fully */
1585 if ((wtp = wka_name_lookup(addr, mask+16)) != NULL) {
1586 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1587 wtp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1589 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1599 /* Only the topmost byte participates fully */
1600 if ((wtp = wka_name_lookup(addr, mask+8)) != NULL) {
1601 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1602 wtp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1604 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1612 for (mask = 7; mask > 0; mask--) {
1613 /* Not even the topmost byte participates fully */
1614 if ((wtp = wka_name_lookup(addr, mask)) != NULL) {
1615 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1616 wtp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1617 addr[3], addr[4], addr[5]);
1618 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1623 /* No match whatsoever. */
1624 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s", ether_to_str(addr));
1625 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1628 g_assert_not_reached();
1629 } /* eth_addr_resolve */
1631 static hashether_t *
1632 eth_hash_new_entry(const guint8 *addr, const gboolean resolve) {
1635 tp = (hashether_t *)g_malloc(sizeof(hashether_t));
1636 memcpy(tp->addr, addr, sizeof(tp->addr));
1637 tp->status = HASHETHER_STATUS_UNRESOLVED;
1638 g_strlcpy(tp->hexaddr, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexaddr));
1639 tp->resolved_name[0] = '\0';
1643 eth_addr_resolve(tp);
1646 } /* eth_hash_new_entry */
1648 static hashether_t *
1649 add_eth_name(const guint8 *addr, const gchar *name)
1654 hash_idx = HASH_ETH_ADDRESS(addr);
1656 tp = eth_table[hash_idx];
1658 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, FALSE);
1661 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1662 /* address already known */
1663 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME)
1664 return tp; /* Entry with a name already in table; ignore attempted replacement */
1665 break; /* Update name of existing entry */
1667 if (tp->next == NULL) {
1668 tp = tp->next = eth_hash_new_entry(addr, FALSE);
1675 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1676 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1677 new_resolved_objects = TRUE;
1680 } /* add_eth_name */
1682 static hashether_t *
1683 eth_name_lookup(const guint8 *addr, const gboolean resolve) {
1687 hash_idx = HASH_ETH_ADDRESS(addr);
1689 tp = eth_table[hash_idx];
1691 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, resolve);
1695 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1696 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED))
1697 eth_addr_resolve(tp); /* Found but needs to be resolved */
1700 if (tp->next == NULL) {
1701 tp->next = eth_hash_new_entry(addr, resolve);
1707 } /* eth_name_lookup */
1710 eth_addr_lookup(const gchar *name)
1714 hashether_t **table = eth_table;
1717 /* to be optimized (hash table from name to addr) */
1718 for (i = 0; i < HASHETHSIZE; i++) {
1721 if (strcmp(tp->resolved_name, name) == 0)
1727 /* not in hash table : performs a file lookup */
1729 if ((eth = get_ethbyname(name)) == NULL)
1732 /* add new entry in hash table */
1734 tp = add_eth_name(eth->addr, name);
1738 } /* eth_addr_lookup */
1743 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1746 * We allow three address separators (':', '-', and '.'),
1747 * as well as no separators
1751 guint32 a, a0, a1, a2, a3;
1752 gboolean found_single_number = FALSE;
1754 if ((cp = strchr(line, '#')))
1757 if ((cp = strtok(line, " \t\n")) == NULL)
1760 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1761 * fill a and found_single_number is TRUE,
1764 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1765 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1766 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1767 if (sscanf(cp, "%x", &a) == 1) {
1768 found_single_number = TRUE;
1777 if ((cp = strtok(NULL, " \t\n")) == NULL)
1780 if (found_single_number) {
1784 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1787 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1791 } /* parse_ipxnets_line */
1793 static FILE *ipxnet_p = NULL;
1796 set_ipxnetent(char *path)
1801 ipxnet_p = ws_fopen(path, "r");
1817 static ipxnet_t ipxnet;
1818 static int size = 0;
1819 static char *buf = NULL;
1821 if (ipxnet_p == NULL)
1824 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1825 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1832 } /* get_ipxnetent */
1835 get_ipxnetbyname(const gchar *name)
1839 set_ipxnetent(g_ipxnets_path);
1841 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1844 if (ipxnet == NULL) {
1847 set_ipxnetent(g_pipxnets_path);
1849 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1857 } /* get_ipxnetbyname */
1860 get_ipxnetbyaddr(guint32 addr)
1864 set_ipxnetent(g_ipxnets_path);
1866 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1868 if (ipxnet == NULL) {
1871 set_ipxnetent(g_pipxnets_path);
1873 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1881 } /* get_ipxnetbyaddr */
1884 initialize_ipxnets(void)
1886 /* Compute the pathname of the ipxnets file.
1888 * XXX - is there a notion of an "ipxnets file" in any flavor of
1889 * UNIX, or with any add-on Netware package for UNIX? If not,
1890 * should the UNIX version of the ipxnets file be in the datafile
1891 * directory as well?
1893 if (g_ipxnets_path == NULL) {
1894 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1895 get_systemfile_dir(), ENAME_IPXNETS);
1898 /* Set g_pipxnets_path here, but don't actually do anything
1899 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1901 if (g_pipxnets_path == NULL)
1902 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1904 } /* initialize_ipxnets */
1906 static hashipxnet_t *
1907 add_ipxnet_name(guint addr, const gchar *name)
1912 hash_idx = HASH_IPX_NET(addr);
1914 tp = ipxnet_table[hash_idx];
1917 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1920 if (tp->next == NULL) {
1921 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1930 g_strlcpy(tp->name, name, MAXNAMELEN);
1932 new_resolved_objects = TRUE;
1936 } /* add_ipxnet_name */
1939 ipxnet_name_lookup(const guint addr)
1945 hash_idx = HASH_IPX_NET(addr);
1947 tp = ipxnet_table[hash_idx];
1950 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1953 if (tp->addr == addr) {
1956 if (tp->next == NULL) {
1957 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1965 /* fill in a new entry */
1970 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1972 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1975 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1980 } /* ipxnet_name_lookup */
1983 ipxnet_addr_lookup(const gchar *name, gboolean *success)
1987 hashipxnet_t **table = ipxnet_table;
1990 /* to be optimized (hash table from name to addr) */
1991 for (i = 0; i < HASHIPXNETSIZE; i++) {
1994 if (strcmp(tp->name, name) == 0) {
2002 /* not in hash table : performs a file lookup */
2004 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
2009 /* add new entry in hash table */
2011 tp = add_ipxnet_name(ipxnet->addr, name);
2016 } /* ipxnet_addr_lookup */
2019 read_hosts_file (const char *hostspath)
2025 guint32 host_addr[4]; /* IPv4 or IPv6 */
2026 struct e_in6_addr ip6_addr;
2031 * See the hosts(4) or hosts(5) man page for hosts file format
2032 * (not available on all systems).
2034 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2037 while (fgetline(&line, &size, hf) >= 0) {
2038 if ((cp = strchr(line, '#')))
2041 if ((cp = strtok(line, " \t")) == NULL)
2042 continue; /* no tokens in the line */
2044 ret = inet_pton(AF_INET6, cp, &host_addr);
2046 continue; /* error parsing */
2051 /* Not valid IPv6 - valid IPv4? */
2052 if (inet_pton(AF_INET, cp, &host_addr) != 1)
2057 if ((cp = strtok(NULL, " \t")) == NULL)
2058 continue; /* no host name */
2061 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2062 add_ipv6_name(&ip6_addr, cp);
2064 add_ipv4_name(host_addr[0], cp);
2067 * Add the aliases, too, if there are any.
2069 while ((cp = strtok(NULL, " \t")) != NULL) {
2071 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2072 add_ipv6_name(&ip6_addr, cp);
2074 add_ipv4_name(host_addr[0], cp);
2081 } /* read_hosts_file */
2084 add_ip_name_from_string (const char *addr, const char *name)
2086 guint32 host_addr[4]; /* IPv4 */
2087 struct e_in6_addr ip6_addr; /* IPv6 */
2091 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2093 /* Error parsing address */
2100 /* Not valid IPv6 - valid IPv4? */
2101 if (inet_pton(AF_INET, addr, &host_addr) != 1)
2102 return FALSE; /* no */
2107 add_ipv6_name(&ip6_addr, name);
2109 add_ipv4_name(host_addr[0], name);
2113 } /* add_ip_name_from_string */
2116 /* Read in a list of subnet definition - name pairs.
2117 * <line> = <comment> | <entry> | <whitespace>
2118 * <comment> = <whitespace>#<any>
2119 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2120 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2121 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2122 * <subnet_mask_length> is a decimal 1-31
2123 * <subnet_name> is a string containing no whitespace.
2124 * <whitespace> = (space | tab)+
2125 * Any malformed entries are ignored.
2126 * Any trailing data after the subnet_name is ignored.
2131 read_subnets_file (const char *subnetspath)
2137 guint32 host_addr; /* IPv4 ONLY */
2140 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2143 while (fgetline(&line, &size, hf) >= 0) {
2144 if ((cp = strchr(line, '#')))
2147 if ((cp = strtok(line, " \t")) == NULL)
2148 continue; /* no tokens in the line */
2151 /* Expected format is <IP4 address>/<subnet length> */
2152 cp2 = strchr(cp, '/');
2157 *cp2 = '\0'; /* Cut token */
2160 /* Check if this is a valid IPv4 address */
2161 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
2165 mask_length = atoi(cp2);
2166 if(0 >= mask_length || mask_length > 31) {
2167 continue; /* invalid mask length */
2170 if ((cp = strtok(NULL, " \t")) == NULL)
2171 continue; /* no subnet name */
2173 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2179 } /* read_subnets_file */
2181 static subnet_entry_t
2182 subnet_lookup(const guint32 addr)
2184 subnet_entry_t subnet_entry;
2187 /* Search mask lengths linearly, longest first */
2189 i = SUBNETLENGTHSIZE;
2190 while(have_subnet_entry && i > 0) {
2191 guint32 masked_addr;
2192 subnet_length_entry_t* length_entry;
2194 /* Note that we run from 31 (length 32) to 0 (length 1) */
2196 g_assert(i < SUBNETLENGTHSIZE);
2199 length_entry = &subnet_length_entries[i];
2201 if(NULL != length_entry->subnet_addresses) {
2205 masked_addr = addr & length_entry->mask;
2206 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2208 tp = length_entry->subnet_addresses[hash_idx];
2209 while(tp != NULL && tp->addr != masked_addr) {
2214 subnet_entry.mask = length_entry->mask;
2215 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2216 subnet_entry.name = tp->name;
2217 return subnet_entry;
2222 subnet_entry.mask = 0;
2223 subnet_entry.mask_length = 0;
2224 subnet_entry.name = NULL;
2226 return subnet_entry;
2229 /* Add a subnet-definition - name pair to the set.
2230 * The definition is taken by masking the address passed in with the mask of the
2234 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2236 subnet_length_entry_t* entry;
2240 g_assert(mask_length > 0 && mask_length <= 32);
2242 entry = &subnet_length_entries[mask_length - 1];
2244 subnet_addr &= entry->mask;
2246 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2248 if(NULL == entry->subnet_addresses) {
2249 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
2252 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2253 if(tp->addr == subnet_addr) {
2254 return; /* XXX provide warning that an address was repeated? */
2256 hashipv4_t * new_tp = g_new(hashipv4_t,1);
2261 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
2265 tp->addr = subnet_addr;
2266 tp->is_dummy_entry = FALSE; /*Never used again...*/
2267 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2268 have_subnet_entry = TRUE;
2272 get_subnet_mask(const guint32 mask_length) {
2274 static guint32 masks[SUBNETLENGTHSIZE];
2275 static gboolean initialised = FALSE;
2278 memset(masks, 0, sizeof(masks));
2282 /* XXX There must be a better way to do this than
2283 * hand-coding the values, but I can't seem to
2287 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2288 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2289 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2290 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2291 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2292 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2293 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2294 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2296 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2297 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2298 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2299 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2300 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2301 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2302 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2303 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2305 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2306 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2307 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2308 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2309 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2310 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2311 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2312 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2314 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2315 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2316 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2317 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2318 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2319 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2320 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2321 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2324 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2325 g_assert_not_reached();
2328 return masks[mask_length - 1];
2333 subnet_name_lookup_init(void)
2338 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2339 guint32 length = i + 1;
2341 subnet_length_entries[i].subnet_addresses = NULL;
2342 subnet_length_entries[i].mask_length = length;
2343 subnet_length_entries[i].mask = get_subnet_mask(length);
2346 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2347 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2348 report_open_failure(subnetspath, errno, FALSE);
2350 g_free(subnetspath);
2353 * Load the global subnets file, if we have one.
2355 subnetspath = get_datafile_path(ENAME_SUBNETS);
2356 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2357 report_open_failure(subnetspath, errno, FALSE);
2359 g_free(subnetspath);
2363 * External Functions
2367 host_name_lookup_init(void) {
2370 #ifdef HAVE_GNU_ADNS
2373 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2374 static char rootpath_ot[] = "\\hosts";
2376 #endif /*GNU_ADNS */
2379 * Load the user's hosts file, if they have one.
2381 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2382 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2383 report_open_failure(hostspath, errno, FALSE);
2388 * Load the global hosts file, if we have one.
2390 hostspath = get_datafile_path(ENAME_HOSTS);
2391 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2392 report_open_failure(hostspath, errno, FALSE);
2397 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2398 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2400 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2401 async_dns_initialized = TRUE;
2403 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2407 #ifdef HAVE_GNU_ADNS
2409 * We're using GNU ADNS, which doesn't check the system hosts file;
2410 * we load that file ourselves.
2414 sysroot = getenv_utf8("WINDIR");
2415 if (sysroot != NULL) {
2417 * The file should be under WINDIR.
2418 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2419 * %WINDIR%\system32\drivers\etc\hosts.
2420 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2422 * XXX - should we base it on the dwPlatformId value from
2425 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2426 if (!read_hosts_file(hostspath)) {
2428 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2429 read_hosts_file(hostspath);
2434 read_hosts_file("/etc/hosts");
2437 /* XXX - Any flags we should be using? */
2438 /* XXX - We could provide config settings for DNS servers, and
2439 pass them to ADNS with adns_init_strcfg */
2440 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2442 * XXX - should we report the error? I'm assuming that some crashes
2443 * reported on a Windows machine with TCP/IP not configured are due
2444 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2445 * ADNS in a state where it crashes due to that. We'll still try
2446 * doing name resolution anyway.
2450 async_dns_initialized = TRUE;
2451 async_dns_in_flight = 0;
2452 #endif /* HAVE_GNU_ADNS */
2453 #endif /* HAVE_C_ARES */
2455 subnet_name_lookup_init();
2460 host_name_lookup_process(gpointer data _U_) {
2461 async_dns_queue_msg_t *caqm;
2462 struct timeval tv = { 0, 0 };
2465 gboolean nro = new_resolved_objects;
2467 new_resolved_objects = FALSE;
2469 if (!async_dns_initialized)
2470 /* c-ares not initialized. Bail out and cancel timers. */
2473 async_dns_queue_head = g_list_first(async_dns_queue_head);
2475 while (async_dns_queue_head != NULL && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2476 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2477 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2478 if (caqm->family == AF_INET) {
2479 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2480 c_ares_ghba_cb, caqm);
2481 async_dns_in_flight++;
2482 } else if (caqm->family == AF_INET6) {
2483 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2484 AF_INET6, c_ares_ghba_cb, caqm);
2485 async_dns_in_flight++;
2491 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2493 select(nfds, &rfds, &wfds, NULL, &tv);
2494 ares_process(ghba_chan, &rfds, &wfds);
2497 /* Any new entries? */
2502 host_name_lookup_cleanup(void) {
2505 cur = g_list_first(async_dns_queue_head);
2508 cur = g_list_next (cur);
2511 g_list_free(async_dns_queue_head);
2513 if (async_dns_initialized) {
2514 ares_destroy(ghba_chan);
2515 ares_destroy(ghbn_chan);
2517 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2518 ares_library_cleanup();
2520 async_dns_initialized = FALSE;
2523 #elif defined(HAVE_GNU_ADNS)
2525 /* XXX - The ADNS "documentation" isn't very clear:
2526 * - Do we need to keep our query structures around?
2529 host_name_lookup_process(gpointer data _U_) {
2530 async_dns_queue_msg_t *almsg;
2532 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2537 gboolean nro = new_resolved_objects;
2539 new_resolved_objects = FALSE;
2540 async_dns_queue_head = g_list_first(async_dns_queue_head);
2542 cur = async_dns_queue_head;
2543 while (cur && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2544 almsg = (async_dns_queue_msg_t *) cur->data;
2545 if (! almsg->submitted && almsg->type == AF_INET) {
2546 addr_bytes = (guint8 *) &almsg->ip4_addr;
2547 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2548 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2549 /* XXX - what if it fails? */
2550 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2551 almsg->submitted = TRUE;
2552 async_dns_in_flight++;
2557 cur = async_dns_queue_head;
2560 almsg = (async_dns_queue_msg_t *) cur->data;
2561 if (almsg->submitted) {
2562 ret = adns_check(ads, &almsg->query, &ans, NULL);
2564 if (ans->status == adns_s_ok) {
2565 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2572 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2574 async_dns_in_flight--;
2578 /* Keep the timeout in place */
2583 host_name_lookup_cleanup(void) {
2586 async_dns_queue_head = g_list_first(async_dns_queue_head);
2587 while (async_dns_queue_head) {
2588 qdata = async_dns_queue_head->data;
2589 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2593 if (async_dns_initialized)
2595 async_dns_initialized = FALSE;
2598 #else /* HAVE_GNU_ADNS */
2601 host_name_lookup_process(gpointer data _U_) {
2602 gboolean nro = new_resolved_objects;
2604 new_resolved_objects = FALSE;
2610 host_name_lookup_cleanup(void) {
2613 #endif /* HAVE_C_ARES */
2615 extern const gchar *
2616 get_hostname(const guint addr)
2619 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2620 hashipv4_t *tp = host_lookup(addr, resolve, &found);
2628 /* -------------------------- */
2630 extern const gchar *
2631 get_hostname6(const struct e_in6_addr *addr)
2634 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2635 hashipv6_t *tp = host_lookup6(addr, resolve, &found);
2643 /* -------------------------- */
2645 add_ipv4_name(const guint addr, const gchar *name)
2650 hash_idx = HASH_IPV4_ADDRESS(addr);
2652 tp = ipv4_table[hash_idx];
2655 tp = ipv4_table[hash_idx] = new_ipv4(addr);
2658 if (tp->addr == addr) {
2659 /* address already known */
2660 if (!tp->is_dummy_entry) {
2663 /* replace this dummy entry with the new one */
2667 if (tp->next == NULL) {
2668 tp->next = new_ipv4(addr);
2675 g_strlcpy(tp->name, name, MAXNAMELEN);
2677 new_resolved_objects = TRUE;
2678 } /* add_ipv4_name */
2680 /* -------------------------- */
2682 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2687 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2689 tp = ipv6_table[hash_idx];
2692 tp = ipv6_table[hash_idx] = new_ipv6(addrp);
2695 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2696 /* address already known */
2697 if (!tp->is_dummy_entry) {
2700 /* replace this dummy entry with the new one */
2704 if (tp->next == NULL) {
2705 tp->next = new_ipv6(addrp);
2713 g_strlcpy(tp->name, name, MAXNAMELEN);
2715 new_resolved_objects = TRUE;
2717 } /* add_ipv6_name */
2719 /* -----------------
2720 * unsigned integer to ascii
2725 gchar *bp = ep_alloc(MAXNAMELEN);
2727 /* XXX, guint32_to_str() ? */
2728 guint32_to_str_buf(port, bp, MAXNAMELEN);
2734 get_udp_port(guint port)
2737 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2738 return ep_utoa(port);
2741 return serv_name_lookup(port, PT_UDP);
2743 } /* get_udp_port */
2746 get_dccp_port(guint port)
2749 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2750 return ep_utoa(port);
2753 return serv_name_lookup(port, PT_DCCP);
2755 } /* get_dccp_port */
2759 get_tcp_port(guint port)
2762 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2763 return ep_utoa(port);
2766 return serv_name_lookup(port, PT_TCP);
2768 } /* get_tcp_port */
2771 get_sctp_port(guint port)
2774 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2775 return ep_utoa(port);
2778 return serv_name_lookup(port, PT_SCTP);
2780 } /* get_sctp_port */
2783 get_addr_name(const address *addr)
2785 const gchar *result;
2787 result = solve_address_to_name(addr);
2792 /* if it gets here, either it is of type AT_NONE, */
2793 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
2795 if (addr->type == AT_NONE){
2799 /* We need an ephemeral allocated string */
2800 return ep_address_to_str(addr);
2804 se_get_addr_name(const address *addr)
2806 const gchar *result;
2808 result = se_solve_address_to_name(addr);
2813 /* if it gets here, either it is of type AT_NONE, */
2814 /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
2816 if (addr->type == AT_NONE){
2820 /* We need a "permanently" allocated string */
2821 return se_address_to_str(addr);
2825 get_addr_name_buf(const address *addr, gchar *buf, gsize size)
2827 const gchar *result = get_addr_name(addr);
2829 g_strlcpy(buf, result, size);
2830 } /* get_addr_name_buf */
2834 get_ether_name(const guint8 *addr)
2837 gboolean resolve = (gbl_resolv_flags & RESOLV_MAC) != 0;
2839 if (resolve && !eth_resolution_initialized) {
2840 initialize_ethers();
2841 eth_resolution_initialized = TRUE;
2844 tp = eth_name_lookup(addr, resolve);
2846 return resolve ? tp->resolved_name : tp->hexaddr;
2848 } /* get_ether_name */
2850 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2851 * If it's not found, simply return NULL.
2854 get_ether_name_if_known(const guint8 *addr)
2858 /* Initialize ether structs if we're the first
2859 * ether-related function called */
2860 if (!(gbl_resolv_flags & RESOLV_MAC))
2863 if (!eth_resolution_initialized) {
2864 initialize_ethers();
2865 eth_resolution_initialized = TRUE;
2868 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
2869 tp = eth_name_lookup(addr, TRUE);
2870 g_assert(tp != NULL);
2872 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
2873 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
2874 return tp->resolved_name;
2877 /* Name was created */
2883 get_ether_addr(const gchar *name)
2886 /* force resolution (do not check gbl_resolv_flags) */
2888 if (!eth_resolution_initialized) {
2889 initialize_ethers();
2890 eth_resolution_initialized = TRUE;
2893 return eth_addr_lookup(name);
2895 } /* get_ether_addr */
2898 add_ether_byip(const guint ip, const guint8 *eth)
2904 /* first check that IP address can be resolved */
2905 if (!(gbl_resolv_flags & RESOLV_NETWORK))
2908 if ((host = host_name_lookup(ip, &found)) == NULL)
2911 /* ok, we can add this entry in the ethers hashtable */
2914 add_eth_name(eth, host);
2916 } /* add_ether_byip */
2918 extern const gchar *
2919 get_ipxnet_name(const guint32 addr)
2922 if (!(gbl_resolv_flags & RESOLV_NETWORK)) {
2923 return ipxnet_to_str_punct(addr, '\0');
2926 if (!ipxnet_resolution_initialized) {
2927 initialize_ipxnets();
2928 ipxnet_resolution_initialized = 1;
2931 return ipxnet_name_lookup(addr);
2933 } /* get_ipxnet_name */
2936 get_ipxnet_addr(const gchar *name, gboolean *known)
2941 /* force resolution (do not check gbl_resolv_flags) */
2943 if (!ipxnet_resolution_initialized) {
2944 initialize_ipxnets();
2945 ipxnet_resolution_initialized = 1;
2948 addr = ipxnet_addr_lookup(name, &success);
2953 } /* get_ipxnet_addr */
2955 extern const gchar *
2956 get_manuf_name(const guint8 *addr)
2961 if ((gbl_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2962 initialize_ethers();
2963 eth_resolution_initialized = TRUE;
2966 if (!(gbl_resolv_flags & RESOLV_MAC) || ((mtp = manuf_name_lookup(addr)) == NULL)) {
2967 cur=ep_strdup_printf("%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2973 } /* get_manuf_name */
2977 get_manuf_name_if_known(const guint8 *addr)
2981 if (!eth_resolution_initialized) {
2982 initialize_ethers();
2983 eth_resolution_initialized = TRUE;
2986 if ((mtp = manuf_name_lookup(addr)) == NULL) {
2992 } /* get_manuf_name_if_known */
2996 #define GHI_TIMEOUT (250 * 1000)
2998 #if ( ( ARES_VERSION_MAJOR < 1 ) \
2999 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3000 c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
3002 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3005 * XXX - If we wanted to be really fancy we could cache results here and
3006 * look them up in get_host_ipaddr* below.
3008 async_hostent_t *ahp = arg;
3009 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3010 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3011 ahp->copied = hp->h_length;
3014 #endif /* HAVE_C_ARES */
3016 /* Translate a string, assumed either to be a dotted-quad IP address or
3017 * a host name, to a numeric IP address. Return TRUE if we succeed and
3018 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3019 * Used more in the dfilter parser rather than in packet dissectors */
3021 get_host_ipaddr(const char *host, guint32 *addrp)
3023 struct in_addr ipaddr;
3025 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3028 async_hostent_t ahe;
3029 #else /* HAVE_C_ARES */
3031 #endif /* HAVE_C_ARES */
3034 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3035 * inet_pton(AF_INET) does not support hexadecimal notation nor
3036 * less-than-4 octet notation.
3038 if (!inet_aton(host, &ipaddr)) {
3039 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3042 /* It's not a valid dotted-quad IP address; is it a valid
3045 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3046 prefs.name_resolve_concurrency < 1 ||
3047 ! async_dns_initialized) {
3050 ahe.addr_size = (int) sizeof (struct in_addr);
3053 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3056 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3058 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3059 select(nfds, &rfds, &wfds, NULL, tvp);
3060 ares_process(ghbn_chan, &rfds, &wfds);
3062 ares_cancel(ghbn_chan);
3063 if (ahe.addr_size == ahe.copied) {
3067 #else /* ! HAVE_C_ARES */
3068 hp = gethostbyname(host);
3072 /* Apparently, some versions of gethostbyaddr can
3073 * return IPv6 addresses. */
3074 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3075 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3079 #endif /* HAVE_C_ARES */
3081 /* Does the string really contain dotted-quad IP?
3082 * Check against inet_atons that accept strings such as
3083 * "130.230" as valid addresses and try to convert them
3084 * to some form of a classful (host.net) notation.
3086 unsigned int a0, a1, a2, a3;
3087 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3091 *addrp = g_ntohl(ipaddr.s_addr);
3096 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3097 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3098 * return FALSE if we fail.
3101 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3104 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3107 async_hostent_t ahe;
3108 #elif defined(HAVE_GETHOSTBYNAME2)
3110 #endif /* HAVE_C_ARES */
3112 if (inet_pton(AF_INET6, host, addrp) == 1)
3115 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3121 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3122 prefs.name_resolve_concurrency < 1 ||
3123 ! async_dns_initialized) {
3126 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3129 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3132 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3134 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3135 select(nfds, &rfds, &wfds, NULL, tvp);
3136 ares_process(ghbn_chan, &rfds, &wfds);
3138 ares_cancel(ghbn_chan);
3139 if (ahe.addr_size == ahe.copied) {
3142 #elif defined(HAVE_GETHOSTBYNAME2)
3143 hp = gethostbyname2(host, AF_INET6);
3144 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3145 memcpy(addrp, hp->h_addr, hp->h_length);
3154 * Find out whether a hostname resolves to an ip or ipv6 address
3155 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3156 * that we don't know)
3158 const char* host_ip_af(const char *host
3159 #ifndef HAVE_GETHOSTBYNAME2
3164 #ifdef HAVE_GETHOSTBYNAME2
3166 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";