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)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1222 set_ethent(g_ethers_path);
1224 while ((eth = get_ethent(NULL, FALSE)) && 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)) && memcmp(addr, eth->addr, 6) != 0)
1248 set_ethent(g_ethers_path);
1250 while ((eth = get_ethent(NULL, FALSE)) && 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 /* This is a well-known MAC address; just add this to the Ethernet
1338 add_eth_name(addr, name);
1343 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1345 hash_idx = HASH_ETH_MANUF(addr);
1346 mtp = manuf_table[hash_idx];
1349 manuf_table[hash_idx] = manuf_hash_new_entry(addr, name);
1353 if (mtp->next == NULL) {
1354 mtp->next = manuf_hash_new_entry(addr, name);
1362 /* This is a range of well-known addresses; add it to the appropriate
1363 well-known-address table, creating that table if necessary. */
1364 wka_tp = wka_table[mask];
1366 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1368 hash_idx = hash_eth_wka(addr, mask);
1370 wtp = (*wka_tp)[hash_idx];
1373 (*wka_tp)[hash_idx] = wka_hash_new_entry(addr, name);
1377 if (memcmp(wtp->addr, addr, sizeof(wtp->addr)) == 0) {
1378 /* address already known */
1381 if (wtp->next == NULL) {
1382 wtp->next = wka_hash_new_entry(addr, name);
1388 } /* add_manuf_name */
1390 static hashmanuf_t *
1391 manuf_name_lookup(const guint8 *addr)
1395 guint8 stripped_addr[3];
1397 hash_idx = HASH_ETH_MANUF(addr);
1399 /* first try to find a "perfect match" */
1400 mtp = manuf_table[hash_idx];
1401 while(mtp != NULL) {
1402 if (memcmp(mtp->addr, addr, sizeof(mtp->addr)) == 0) {
1408 /* Mask out the broadcast/multicast flag but not the locally
1409 * administered flag as localy administered means: not assigend
1410 * by the IEEE but the local administrator instead.
1411 * 0x01 multicast / broadcast bit
1412 * 0x02 locally administered bit */
1413 memcpy(stripped_addr, addr, 3);
1414 stripped_addr[0] &= 0xFE;
1416 mtp = manuf_table[hash_idx];
1417 while(mtp != NULL) {
1418 if (memcmp(mtp->addr, stripped_addr, sizeof(mtp->addr)) == 0) {
1426 } /* manuf_name_lookup */
1429 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1432 hashwka_t *(*wka_tp)[HASHETHSIZE];
1434 guint8 masked_addr[6];
1438 wka_tp = wka_table[mask];
1439 if (wka_tp == NULL) {
1440 /* There are no entries in the table for that mask value, as there is
1441 no table for that mask value. */
1445 /* Get the part of the address covered by the mask. */
1446 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1447 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1448 /* Mask out the first masked octet */
1449 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1451 /* Zero out completely-masked-out octets */
1455 hash_idx = hash_eth_wka(masked_addr, mask);
1457 wtp = (*wka_tp)[hash_idx];
1459 while(wtp != NULL) {
1460 if (memcmp(wtp->addr, masked_addr, sizeof(wtp->addr)) == 0) {
1468 } /* wka_name_lookup */
1471 initialize_ethers(void)
1477 /* Compute the pathname of the ethers file. */
1478 if (g_ethers_path == NULL) {
1479 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1480 get_systemfile_dir(), ENAME_ETHERS);
1483 /* Set g_pethers_path here, but don't actually do anything
1484 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1486 if (g_pethers_path == NULL)
1487 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1489 /* manuf hash table initialization */
1491 /* Compute the pathname of the manuf file */
1492 manuf_path = get_datafile_path(ENAME_MANUF);
1494 /* Read it and initialize the hash table */
1495 set_ethent(manuf_path);
1497 while ((eth = get_ethent(&mask, TRUE))) {
1498 add_manuf_name(eth->addr, mask, eth->name);
1505 } /* initialize_ethers */
1507 /* Resolve ethernet address */
1508 static hashether_t *
1509 eth_addr_resolve(hashether_t *tp) {
1511 const guint8 *addr = tp->addr;
1513 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1514 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1515 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1522 /* Unknown name. Try looking for it in the well-known-address
1523 tables for well-known address ranges smaller than 2^24. */
1526 /* Only the topmost 5 bytes participate fully */
1527 if ((wtp = wka_name_lookup(addr, mask+40)) != NULL) {
1528 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1529 wtp->name, addr[5] & (0xFF >> mask));
1530 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1540 /* Only the topmost 4 bytes participate fully */
1541 if ((wtp = wka_name_lookup(addr, mask+32)) != NULL) {
1542 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1543 wtp->name, addr[4] & (0xFF >> mask), addr[5]);
1544 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1554 /* Only the topmost 3 bytes participate fully */
1555 if ((wtp = wka_name_lookup(addr, mask+24)) != NULL) {
1556 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1557 wtp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1558 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1566 /* Now try looking in the manufacturer table. */
1567 if ((mtp = manuf_name_lookup(addr)) != NULL) {
1568 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1569 mtp->name, addr[3], addr[4], addr[5]);
1570 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1574 /* Now try looking for it in the well-known-address
1575 tables for well-known address ranges larger than 2^24. */
1578 /* Only the topmost 2 bytes participate fully */
1579 if ((wtp = wka_name_lookup(addr, mask+16)) != NULL) {
1580 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1581 wtp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1583 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1593 /* Only the topmost byte participates fully */
1594 if ((wtp = wka_name_lookup(addr, mask+8)) != NULL) {
1595 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1596 wtp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1598 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1606 for (mask = 7; mask > 0; mask--) {
1607 /* Not even the topmost byte participates fully */
1608 if ((wtp = wka_name_lookup(addr, mask)) != NULL) {
1609 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1610 wtp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1611 addr[3], addr[4], addr[5]);
1612 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1617 /* No match whatsoever. */
1618 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s", ether_to_str(addr));
1619 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1622 g_assert_not_reached();
1623 } /* eth_addr_resolve */
1625 static hashether_t *
1626 eth_hash_new_entry(const guint8 *addr, const gboolean resolve) {
1629 tp = (hashether_t *)g_malloc(sizeof(hashether_t));
1630 memcpy(tp->addr, addr, sizeof(tp->addr));
1631 tp->status = HASHETHER_STATUS_UNRESOLVED;
1632 g_strlcpy(tp->hexaddr, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexaddr));
1633 tp->resolved_name[0] = '\0';
1637 eth_addr_resolve(tp);
1640 } /* eth_hash_new_entry */
1642 static hashether_t *
1643 add_eth_name(const guint8 *addr, const gchar *name)
1648 hash_idx = HASH_ETH_ADDRESS(addr);
1650 tp = eth_table[hash_idx];
1652 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, FALSE);
1655 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1656 /* address already known */
1657 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME)
1658 return tp; /* Entry with a name already in table; ignore attempted replacement */
1659 break; /* Update name of existing entry */
1661 if (tp->next == NULL) {
1662 tp = tp->next = eth_hash_new_entry(addr, FALSE);
1669 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1670 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1671 new_resolved_objects = TRUE;
1674 } /* add_eth_name */
1676 static hashether_t *
1677 eth_name_lookup(const guint8 *addr, const gboolean resolve) {
1681 hash_idx = HASH_ETH_ADDRESS(addr);
1683 tp = eth_table[hash_idx];
1685 tp = eth_table[hash_idx] = eth_hash_new_entry(addr, resolve);
1689 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1690 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED))
1691 eth_addr_resolve(tp); /* Found but needs to be resolved */
1694 if (tp->next == NULL) {
1695 tp->next = eth_hash_new_entry(addr, resolve);
1701 } /* eth_name_lookup */
1704 eth_addr_lookup(const gchar *name)
1708 hashether_t **table = eth_table;
1711 /* to be optimized (hash table from name to addr) */
1712 for (i = 0; i < HASHETHSIZE; i++) {
1715 if (strcmp(tp->resolved_name, name) == 0)
1721 /* not in hash table : performs a file lookup */
1723 if ((eth = get_ethbyname(name)) == NULL)
1726 /* add new entry in hash table */
1728 tp = add_eth_name(eth->addr, name);
1732 } /* eth_addr_lookup */
1737 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1740 * We allow three address separators (':', '-', and '.'),
1741 * as well as no separators
1745 guint32 a, a0, a1, a2, a3;
1746 gboolean found_single_number = FALSE;
1748 if ((cp = strchr(line, '#')))
1751 if ((cp = strtok(line, " \t\n")) == NULL)
1754 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1755 * fill a and found_single_number is TRUE,
1758 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1759 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1760 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1761 if (sscanf(cp, "%x", &a) == 1) {
1762 found_single_number = TRUE;
1771 if ((cp = strtok(NULL, " \t\n")) == NULL)
1774 if (found_single_number) {
1778 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1781 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1785 } /* parse_ipxnets_line */
1787 static FILE *ipxnet_p = NULL;
1790 set_ipxnetent(char *path)
1795 ipxnet_p = ws_fopen(path, "r");
1811 static ipxnet_t ipxnet;
1812 static int size = 0;
1813 static char *buf = NULL;
1815 if (ipxnet_p == NULL)
1818 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1819 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1826 } /* get_ipxnetent */
1829 get_ipxnetbyname(const gchar *name)
1833 set_ipxnetent(g_ipxnets_path);
1835 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1838 if (ipxnet == NULL) {
1841 set_ipxnetent(g_pipxnets_path);
1843 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1851 } /* get_ipxnetbyname */
1854 get_ipxnetbyaddr(guint32 addr)
1858 set_ipxnetent(g_ipxnets_path);
1860 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1862 if (ipxnet == NULL) {
1865 set_ipxnetent(g_pipxnets_path);
1867 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1875 } /* get_ipxnetbyaddr */
1878 initialize_ipxnets(void)
1880 /* Compute the pathname of the ipxnets file.
1882 * XXX - is there a notion of an "ipxnets file" in any flavor of
1883 * UNIX, or with any add-on Netware package for UNIX? If not,
1884 * should the UNIX version of the ipxnets file be in the datafile
1885 * directory as well?
1887 if (g_ipxnets_path == NULL) {
1888 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1889 get_systemfile_dir(), ENAME_IPXNETS);
1892 /* Set g_pipxnets_path here, but don't actually do anything
1893 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1895 if (g_pipxnets_path == NULL)
1896 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1898 } /* initialize_ipxnets */
1900 static hashipxnet_t *
1901 add_ipxnet_name(guint addr, const gchar *name)
1906 hash_idx = HASH_IPX_NET(addr);
1908 tp = ipxnet_table[hash_idx];
1911 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1914 if (tp->next == NULL) {
1915 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1924 g_strlcpy(tp->name, name, MAXNAMELEN);
1926 new_resolved_objects = TRUE;
1930 } /* add_ipxnet_name */
1933 ipxnet_name_lookup(const guint addr)
1939 hash_idx = HASH_IPX_NET(addr);
1941 tp = ipxnet_table[hash_idx];
1944 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1947 if (tp->addr == addr) {
1950 if (tp->next == NULL) {
1951 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1959 /* fill in a new entry */
1964 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1966 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1969 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1974 } /* ipxnet_name_lookup */
1977 ipxnet_addr_lookup(const gchar *name, gboolean *success)
1981 hashipxnet_t **table = ipxnet_table;
1984 /* to be optimized (hash table from name to addr) */
1985 for (i = 0; i < HASHIPXNETSIZE; i++) {
1988 if (strcmp(tp->name, name) == 0) {
1996 /* not in hash table : performs a file lookup */
1998 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
2003 /* add new entry in hash table */
2005 tp = add_ipxnet_name(ipxnet->addr, name);
2010 } /* ipxnet_addr_lookup */
2013 read_hosts_file (const char *hostspath)
2019 guint32 host_addr[4]; /* IPv4 or IPv6 */
2020 struct e_in6_addr ip6_addr;
2025 * See the hosts(4) or hosts(5) man page for hosts file format
2026 * (not available on all systems).
2028 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2031 while (fgetline(&line, &size, hf) >= 0) {
2032 if ((cp = strchr(line, '#')))
2035 if ((cp = strtok(line, " \t")) == NULL)
2036 continue; /* no tokens in the line */
2038 ret = inet_pton(AF_INET6, cp, &host_addr);
2040 continue; /* error parsing */
2045 /* Not valid IPv6 - valid IPv4? */
2046 if (inet_pton(AF_INET, cp, &host_addr) != 1)
2051 if ((cp = strtok(NULL, " \t")) == NULL)
2052 continue; /* no host name */
2055 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2056 add_ipv6_name(&ip6_addr, cp);
2058 add_ipv4_name(host_addr[0], cp);
2061 * Add the aliases, too, if there are any.
2063 while ((cp = strtok(NULL, " \t")) != NULL) {
2065 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2066 add_ipv6_name(&ip6_addr, cp);
2068 add_ipv4_name(host_addr[0], cp);
2075 } /* read_hosts_file */
2078 add_ip_name_from_string (const char *addr, const char *name)
2080 guint32 host_addr[4]; /* IPv4 */
2081 struct e_in6_addr ip6_addr; /* IPv6 */
2085 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2087 /* Error parsing address */
2094 /* Not valid IPv6 - valid IPv4? */
2095 if (inet_pton(AF_INET, addr, &host_addr) != 1)
2096 return FALSE; /* no */
2101 add_ipv6_name(&ip6_addr, name);
2103 add_ipv4_name(host_addr[0], name);
2107 } /* add_ip_name_from_string */
2110 /* Read in a list of subnet definition - name pairs.
2111 * <line> = <comment> | <entry> | <whitespace>
2112 * <comment> = <whitespace>#<any>
2113 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2114 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2115 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2116 * <subnet_mask_length> is a decimal 1-31
2117 * <subnet_name> is a string containing no whitespace.
2118 * <whitespace> = (space | tab)+
2119 * Any malformed entries are ignored.
2120 * Any trailing data after the subnet_name is ignored.
2125 read_subnets_file (const char *subnetspath)
2131 guint32 host_addr; /* IPv4 ONLY */
2134 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2137 while (fgetline(&line, &size, hf) >= 0) {
2138 if ((cp = strchr(line, '#')))
2141 if ((cp = strtok(line, " \t")) == NULL)
2142 continue; /* no tokens in the line */
2145 /* Expected format is <IP4 address>/<subnet length> */
2146 cp2 = strchr(cp, '/');
2151 *cp2 = '\0'; /* Cut token */
2154 /* Check if this is a valid IPv4 address */
2155 if (inet_pton(AF_INET, cp, &host_addr) != 1) {
2159 mask_length = atoi(cp2);
2160 if(0 >= mask_length || mask_length > 31) {
2161 continue; /* invalid mask length */
2164 if ((cp = strtok(NULL, " \t")) == NULL)
2165 continue; /* no subnet name */
2167 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2173 } /* read_subnets_file */
2175 static subnet_entry_t
2176 subnet_lookup(const guint32 addr)
2178 subnet_entry_t subnet_entry;
2181 /* Search mask lengths linearly, longest first */
2183 i = SUBNETLENGTHSIZE;
2184 while(have_subnet_entry && i > 0) {
2185 guint32 masked_addr;
2186 subnet_length_entry_t* length_entry;
2188 /* Note that we run from 31 (length 32) to 0 (length 1) */
2190 g_assert(i < SUBNETLENGTHSIZE);
2193 length_entry = &subnet_length_entries[i];
2195 if(NULL != length_entry->subnet_addresses) {
2199 masked_addr = addr & length_entry->mask;
2200 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2202 tp = length_entry->subnet_addresses[hash_idx];
2203 while(tp != NULL && tp->addr != masked_addr) {
2208 subnet_entry.mask = length_entry->mask;
2209 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2210 subnet_entry.name = tp->name;
2211 return subnet_entry;
2216 subnet_entry.mask = 0;
2217 subnet_entry.mask_length = 0;
2218 subnet_entry.name = NULL;
2220 return subnet_entry;
2223 /* Add a subnet-definition - name pair to the set.
2224 * The definition is taken by masking the address passed in with the mask of the
2228 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2230 subnet_length_entry_t* entry;
2234 g_assert(mask_length > 0 && mask_length <= 32);
2236 entry = &subnet_length_entries[mask_length - 1];
2238 subnet_addr &= entry->mask;
2240 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2242 if(NULL == entry->subnet_addresses) {
2243 entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
2246 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2247 if(tp->addr == subnet_addr) {
2248 return; /* XXX provide warning that an address was repeated? */
2250 hashipv4_t * new_tp = g_new(hashipv4_t,1);
2255 tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
2259 tp->addr = subnet_addr;
2260 tp->is_dummy_entry = FALSE; /*Never used again...*/
2261 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2262 have_subnet_entry = TRUE;
2266 get_subnet_mask(const guint32 mask_length) {
2268 static guint32 masks[SUBNETLENGTHSIZE];
2269 static gboolean initialised = FALSE;
2272 memset(masks, 0, sizeof(masks));
2276 /* XXX There must be a better way to do this than
2277 * hand-coding the values, but I can't seem to
2281 inet_pton(AF_INET, "128.0.0.0", &masks[0]);
2282 inet_pton(AF_INET, "192.0.0.0", &masks[1]);
2283 inet_pton(AF_INET, "224.0.0.0", &masks[2]);
2284 inet_pton(AF_INET, "240.0.0.0", &masks[3]);
2285 inet_pton(AF_INET, "248.0.0.0", &masks[4]);
2286 inet_pton(AF_INET, "252.0.0.0", &masks[5]);
2287 inet_pton(AF_INET, "254.0.0.0", &masks[6]);
2288 inet_pton(AF_INET, "255.0.0.0", &masks[7]);
2290 inet_pton(AF_INET, "255.128.0.0", &masks[8]);
2291 inet_pton(AF_INET, "255.192.0.0", &masks[9]);
2292 inet_pton(AF_INET, "255.224.0.0", &masks[10]);
2293 inet_pton(AF_INET, "255.240.0.0", &masks[11]);
2294 inet_pton(AF_INET, "255.248.0.0", &masks[12]);
2295 inet_pton(AF_INET, "255.252.0.0", &masks[13]);
2296 inet_pton(AF_INET, "255.254.0.0", &masks[14]);
2297 inet_pton(AF_INET, "255.255.0.0", &masks[15]);
2299 inet_pton(AF_INET, "255.255.128.0", &masks[16]);
2300 inet_pton(AF_INET, "255.255.192.0", &masks[17]);
2301 inet_pton(AF_INET, "255.255.224.0", &masks[18]);
2302 inet_pton(AF_INET, "255.255.240.0", &masks[19]);
2303 inet_pton(AF_INET, "255.255.248.0", &masks[20]);
2304 inet_pton(AF_INET, "255.255.252.0", &masks[21]);
2305 inet_pton(AF_INET, "255.255.254.0", &masks[22]);
2306 inet_pton(AF_INET, "255.255.255.0", &masks[23]);
2308 inet_pton(AF_INET, "255.255.255.128", &masks[24]);
2309 inet_pton(AF_INET, "255.255.255.192", &masks[25]);
2310 inet_pton(AF_INET, "255.255.255.224", &masks[26]);
2311 inet_pton(AF_INET, "255.255.255.240", &masks[27]);
2312 inet_pton(AF_INET, "255.255.255.248", &masks[28]);
2313 inet_pton(AF_INET, "255.255.255.252", &masks[29]);
2314 inet_pton(AF_INET, "255.255.255.254", &masks[30]);
2315 inet_pton(AF_INET, "255.255.255.255", &masks[31]);
2318 if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
2319 g_assert_not_reached();
2322 return masks[mask_length - 1];
2327 subnet_name_lookup_init(void)
2332 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2333 guint32 length = i + 1;
2335 subnet_length_entries[i].subnet_addresses = NULL;
2336 subnet_length_entries[i].mask_length = length;
2337 subnet_length_entries[i].mask = get_subnet_mask(length);
2340 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, FALSE);
2341 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2342 report_open_failure(subnetspath, errno, FALSE);
2344 g_free(subnetspath);
2347 * Load the global subnets file, if we have one.
2349 subnetspath = get_datafile_path(ENAME_SUBNETS);
2350 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2351 report_open_failure(subnetspath, errno, FALSE);
2353 g_free(subnetspath);
2357 * External Functions
2361 host_name_lookup_init(void) {
2364 #ifdef HAVE_GNU_ADNS
2367 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2368 static char rootpath_ot[] = "\\hosts";
2370 #endif /*GNU_ADNS */
2373 * Load the user's hosts file, if they have one.
2375 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
2376 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2377 report_open_failure(hostspath, errno, FALSE);
2382 * Load the global hosts file, if we have one.
2384 hostspath = get_datafile_path(ENAME_HOSTS);
2385 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2386 report_open_failure(hostspath, errno, FALSE);
2391 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2392 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2394 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2395 async_dns_initialized = TRUE;
2397 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2401 #ifdef HAVE_GNU_ADNS
2403 * We're using GNU ADNS, which doesn't check the system hosts file;
2404 * we load that file ourselves.
2408 sysroot = getenv_utf8("WINDIR");
2409 if (sysroot != NULL) {
2411 * The file should be under WINDIR.
2412 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2413 * %WINDIR%\system32\drivers\etc\hosts.
2414 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2416 * XXX - should we base it on the dwPlatformId value from
2419 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2420 if (!read_hosts_file(hostspath)) {
2422 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2423 read_hosts_file(hostspath);
2428 read_hosts_file("/etc/hosts");
2431 /* XXX - Any flags we should be using? */
2432 /* XXX - We could provide config settings for DNS servers, and
2433 pass them to ADNS with adns_init_strcfg */
2434 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
2436 * XXX - should we report the error? I'm assuming that some crashes
2437 * reported on a Windows machine with TCP/IP not configured are due
2438 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2439 * ADNS in a state where it crashes due to that. We'll still try
2440 * doing name resolution anyway.
2444 async_dns_initialized = TRUE;
2445 async_dns_in_flight = 0;
2446 #endif /* HAVE_GNU_ADNS */
2447 #endif /* HAVE_C_ARES */
2449 subnet_name_lookup_init();
2454 host_name_lookup_process(gpointer data _U_) {
2455 async_dns_queue_msg_t *caqm;
2456 struct timeval tv = { 0, 0 };
2459 gboolean nro = new_resolved_objects;
2461 new_resolved_objects = FALSE;
2463 if (!async_dns_initialized)
2464 /* c-ares not initialized. Bail out and cancel timers. */
2467 async_dns_queue_head = g_list_first(async_dns_queue_head);
2469 while (async_dns_queue_head && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2470 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2471 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2472 if (caqm->family == AF_INET) {
2473 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2474 c_ares_ghba_cb, caqm);
2475 async_dns_in_flight++;
2476 } else if (caqm->family == AF_INET6) {
2477 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2478 AF_INET6, c_ares_ghba_cb, caqm);
2479 async_dns_in_flight++;
2485 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2487 select(nfds, &rfds, &wfds, NULL, &tv);
2488 ares_process(ghba_chan, &rfds, &wfds);
2491 /* Any new entries? */
2496 host_name_lookup_cleanup(void) {
2499 cur = g_list_first(async_dns_queue_head);
2502 cur = g_list_next (cur);
2505 g_list_free(async_dns_queue_head);
2507 if (async_dns_initialized) {
2508 ares_destroy(ghba_chan);
2509 ares_destroy(ghbn_chan);
2511 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2512 ares_library_cleanup();
2514 async_dns_initialized = FALSE;
2517 #elif defined(HAVE_GNU_ADNS)
2519 /* XXX - The ADNS "documentation" isn't very clear:
2520 * - Do we need to keep our query structures around?
2523 host_name_lookup_process(gpointer data _U_) {
2524 async_dns_queue_msg_t *almsg;
2526 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2531 gboolean nro = new_resolved_objects;
2533 new_resolved_objects = FALSE;
2534 async_dns_queue_head = g_list_first(async_dns_queue_head);
2536 cur = async_dns_queue_head;
2537 while (cur && async_dns_in_flight <= prefs.name_resolve_concurrency) {
2538 almsg = (async_dns_queue_msg_t *) cur->data;
2539 if (! almsg->submitted && almsg->type == AF_INET) {
2540 addr_bytes = (guint8 *) &almsg->ip4_addr;
2541 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2542 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2543 /* XXX - what if it fails? */
2544 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
2545 almsg->submitted = TRUE;
2546 async_dns_in_flight++;
2551 cur = async_dns_queue_head;
2554 almsg = (async_dns_queue_msg_t *) cur->data;
2555 if (almsg->submitted) {
2556 ret = adns_check(ads, &almsg->query, &ans, NULL);
2558 if (ans->status == adns_s_ok) {
2559 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2566 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2568 async_dns_in_flight--;
2572 /* Keep the timeout in place */
2577 host_name_lookup_cleanup(void) {
2580 async_dns_queue_head = g_list_first(async_dns_queue_head);
2581 while (async_dns_queue_head) {
2582 qdata = async_dns_queue_head->data;
2583 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2587 if (async_dns_initialized)
2589 async_dns_initialized = FALSE;
2592 #else /* HAVE_GNU_ADNS */
2595 host_name_lookup_process(gpointer data _U_) {
2596 gboolean nro = new_resolved_objects;
2598 new_resolved_objects = FALSE;
2604 host_name_lookup_cleanup(void) {
2607 #endif /* HAVE_C_ARES */
2609 extern const gchar *
2610 get_hostname(const guint addr)
2613 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2614 hashipv4_t *tp = host_lookup(addr, resolve, &found);
2622 /* -------------------------- */
2624 extern const gchar *
2625 get_hostname6(const struct e_in6_addr *addr)
2628 gboolean resolve = gbl_resolv_flags & RESOLV_NETWORK;
2629 hashipv6_t *tp = host_lookup6(addr, resolve, &found);
2631 if (!resolve || E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
2636 /* -------------------------- */
2638 add_ipv4_name(const guint addr, const gchar *name)
2643 hash_idx = HASH_IPV4_ADDRESS(addr);
2645 tp = ipv4_table[hash_idx];
2648 tp = ipv4_table[hash_idx] = new_ipv4(addr);
2651 if (tp->addr == addr) {
2652 /* address already known */
2653 if (!tp->is_dummy_entry) {
2656 /* replace this dummy entry with the new one */
2660 if (tp->next == NULL) {
2661 tp->next = new_ipv4(addr);
2668 g_strlcpy(tp->name, name, MAXNAMELEN);
2670 new_resolved_objects = TRUE;
2671 } /* add_ipv4_name */
2673 /* -------------------------- */
2675 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2680 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2682 tp = ipv6_table[hash_idx];
2685 tp = ipv6_table[hash_idx] = new_ipv6(addrp);
2688 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2689 /* address already known */
2690 if (!tp->is_dummy_entry) {
2693 /* replace this dummy entry with the new one */
2697 if (tp->next == NULL) {
2698 tp->next = new_ipv6(addrp);
2706 g_strlcpy(tp->name, name, MAXNAMELEN);
2708 new_resolved_objects = TRUE;
2710 } /* add_ipv6_name */
2712 /* -----------------
2713 * unsigned integer to ascii
2718 gchar *bp = ep_alloc(MAXNAMELEN);
2720 /* XXX, guint32_to_str() ? */
2721 guint32_to_str_buf(port, bp, MAXNAMELEN);
2727 get_udp_port(guint port)
2730 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2731 return ep_utoa(port);
2734 return serv_name_lookup(port, PT_UDP);
2736 } /* get_udp_port */
2739 get_dccp_port(guint port)
2742 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2743 return ep_utoa(port);
2746 return serv_name_lookup(port, PT_DCCP);
2748 } /* get_dccp_port */
2752 get_tcp_port(guint port)
2755 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2756 return ep_utoa(port);
2759 return serv_name_lookup(port, PT_TCP);
2761 } /* get_tcp_port */
2764 get_sctp_port(guint port)
2767 if (!(gbl_resolv_flags & RESOLV_TRANSPORT)) {
2768 return ep_utoa(port);
2771 return serv_name_lookup(port, PT_SCTP);
2773 } /* get_sctp_port */
2776 get_addr_name(const address *addr)
2778 const gchar *result;
2780 result = solve_address_to_name(addr);
2785 /* if it gets here, either it is of type AT_NONE, */
2786 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
2788 if (addr->type == AT_NONE){
2792 /* We need an ephemeral allocated string */
2793 return ep_address_to_str(addr);
2797 se_get_addr_name(const address *addr)
2799 const gchar *result;
2801 result = se_solve_address_to_name(addr);
2806 /* if it gets here, either it is of type AT_NONE, */
2807 /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
2809 if (addr->type == AT_NONE){
2813 /* We need a "permanently" allocated string */
2814 return se_address_to_str(addr);
2818 get_addr_name_buf(const address *addr, gchar *buf, gsize size)
2820 const gchar *result = get_addr_name(addr);
2822 g_strlcpy(buf, result, size);
2823 } /* get_addr_name_buf */
2827 get_ether_name(const guint8 *addr)
2830 gboolean resolve = (gbl_resolv_flags & RESOLV_MAC) != 0;
2832 if (resolve && !eth_resolution_initialized) {
2833 initialize_ethers();
2834 eth_resolution_initialized = TRUE;
2837 tp = eth_name_lookup(addr, resolve);
2839 return resolve ? tp->resolved_name : tp->hexaddr;
2841 } /* get_ether_name */
2843 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2844 * If it's not found, simply return NULL.
2847 get_ether_name_if_known(const guint8 *addr)
2851 /* Initialize ether structs if we're the first
2852 * ether-related function called */
2853 if (!(gbl_resolv_flags & RESOLV_MAC))
2856 if (!eth_resolution_initialized) {
2857 initialize_ethers();
2858 eth_resolution_initialized = TRUE;
2861 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
2862 tp = eth_name_lookup(addr, TRUE);
2863 g_assert(tp != NULL);
2865 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
2866 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
2867 return tp->resolved_name;
2870 /* Name was created */
2876 get_ether_addr(const gchar *name)
2879 /* force resolution (do not check gbl_resolv_flags) */
2881 if (!eth_resolution_initialized) {
2882 initialize_ethers();
2883 eth_resolution_initialized = TRUE;
2886 return eth_addr_lookup(name);
2888 } /* get_ether_addr */
2891 add_ether_byip(const guint ip, const guint8 *eth)
2897 /* first check that IP address can be resolved */
2898 if (!(gbl_resolv_flags & RESOLV_NETWORK))
2901 if ((host = host_name_lookup(ip, &found)) == NULL)
2904 /* ok, we can add this entry in the ethers hashtable */
2907 add_eth_name(eth, host);
2909 } /* add_ether_byip */
2911 extern const gchar *
2912 get_ipxnet_name(const guint32 addr)
2915 if (!(gbl_resolv_flags & RESOLV_NETWORK)) {
2916 return ipxnet_to_str_punct(addr, '\0');
2919 if (!ipxnet_resolution_initialized) {
2920 initialize_ipxnets();
2921 ipxnet_resolution_initialized = 1;
2924 return ipxnet_name_lookup(addr);
2926 } /* get_ipxnet_name */
2929 get_ipxnet_addr(const gchar *name, gboolean *known)
2934 /* force resolution (do not check gbl_resolv_flags) */
2936 if (!ipxnet_resolution_initialized) {
2937 initialize_ipxnets();
2938 ipxnet_resolution_initialized = 1;
2941 addr = ipxnet_addr_lookup(name, &success);
2946 } /* get_ipxnet_addr */
2948 extern const gchar *
2949 get_manuf_name(const guint8 *addr)
2954 if ((gbl_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2955 initialize_ethers();
2956 eth_resolution_initialized = TRUE;
2959 if (!(gbl_resolv_flags & RESOLV_MAC) || ((mtp = manuf_name_lookup(addr)) == NULL)) {
2960 cur=ep_strdup_printf("%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2966 } /* get_manuf_name */
2970 get_manuf_name_if_known(const guint8 *addr)
2974 if (!eth_resolution_initialized) {
2975 initialize_ethers();
2976 eth_resolution_initialized = TRUE;
2979 if ((mtp = manuf_name_lookup(addr)) == NULL) {
2985 } /* get_manuf_name_if_known */
2989 #define GHI_TIMEOUT (250 * 1000)
2991 #if ( ( ARES_VERSION_MAJOR < 1 ) \
2992 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
2993 c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
2995 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
2998 * XXX - If we wanted to be really fancy we could cache results here and
2999 * look them up in get_host_ipaddr* below.
3001 async_hostent_t *ahp = arg;
3002 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3003 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3004 ahp->copied = hp->h_length;
3007 #endif /* HAVE_C_ARES */
3009 /* Translate a string, assumed either to be a dotted-quad IP address or
3010 * a host name, to a numeric IP address. Return TRUE if we succeed and
3011 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3012 * Used more in the dfilter parser rather than in packet dissectors */
3014 get_host_ipaddr(const char *host, guint32 *addrp)
3016 struct in_addr ipaddr;
3018 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3021 async_hostent_t ahe;
3022 #else /* HAVE_C_ARES */
3024 #endif /* HAVE_C_ARES */
3027 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3028 * inet_pton(AF_INET) does not support hexadecimal notation nor
3029 * less-than-4 octet notation.
3031 if (!inet_aton(host, &ipaddr)) {
3032 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3035 /* It's not a valid dotted-quad IP address; is it a valid
3038 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3039 prefs.name_resolve_concurrency < 1 ||
3040 ! async_dns_initialized) {
3043 ahe.addr_size = (int) sizeof (struct in_addr);
3046 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3049 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3051 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3052 select(nfds, &rfds, &wfds, NULL, tvp);
3053 ares_process(ghbn_chan, &rfds, &wfds);
3055 ares_cancel(ghbn_chan);
3056 if (ahe.addr_size == ahe.copied) {
3060 #else /* ! HAVE_C_ARES */
3061 hp = gethostbyname(host);
3065 /* Apparently, some versions of gethostbyaddr can
3066 * return IPv6 addresses. */
3067 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3068 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3072 #endif /* HAVE_C_ARES */
3074 /* Does the string really contain dotted-quad IP?
3075 * Check against inet_atons that accept strings such as
3076 * "130.230" as valid addresses and try to convert them
3077 * to some form of a classful (host.net) notation.
3079 unsigned int a0, a1, a2, a3;
3080 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3084 *addrp = g_ntohl(ipaddr.s_addr);
3089 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3090 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3091 * return FALSE if we fail.
3094 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3097 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3100 async_hostent_t ahe;
3101 #elif defined(HAVE_GETHOSTBYNAME2)
3103 #endif /* HAVE_C_ARES */
3105 if (inet_pton(AF_INET6, host, addrp) == 1)
3108 if (! (gbl_resolv_flags & RESOLV_NETWORK)) {
3114 if (! (gbl_resolv_flags & RESOLV_CONCURRENT) ||
3115 prefs.name_resolve_concurrency < 1 ||
3116 ! async_dns_initialized) {
3119 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3122 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3125 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3127 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3128 select(nfds, &rfds, &wfds, NULL, tvp);
3129 ares_process(ghbn_chan, &rfds, &wfds);
3131 ares_cancel(ghbn_chan);
3132 if (ahe.addr_size == ahe.copied) {
3135 #elif defined(HAVE_GETHOSTBYNAME2)
3136 hp = gethostbyname2(host, AF_INET6);
3137 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3138 memcpy(addrp, hp->h_addr, hp->h_length);
3147 * Find out whether a hostname resolves to an ip or ipv6 address
3148 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3149 * that we don't know)
3151 const char* host_ip_af(const char *host
3152 #ifndef HAVE_GETHOSTBYNAME2
3157 #ifdef HAVE_GETHOSTBYNAME2
3159 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";