2004-08-09 22:31:21 +00:00
|
|
|
/* addr_resolv.c
|
1998-09-16 02:39:15 +00:00
|
|
|
* Routines for network object lookup
|
|
|
|
*
|
2003-01-26 19:35:31 +00:00
|
|
|
* Laurent Deniel <laurent.deniel@free.fr>
|
1998-09-16 02:39:15 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
1998-09-16 02:39:15 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
1998-09-16 02:39:15 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
1998-09-16 02:39:15 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-12-17 05:42:33 +00:00
|
|
|
#include <stdlib.h>
|
1998-09-16 02:39:15 +00:00
|
|
|
#include <string.h>
|
2005-01-13 09:09:56 +00:00
|
|
|
#include <errno.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
|
2003-11-20 05:04:57 +00:00
|
|
|
/*
|
2004-10-05 22:48:36 +00:00
|
|
|
* Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
|
|
|
|
* are most likely to take a long time, given the way address-to-name
|
|
|
|
* lookups are done over NBNS).
|
2003-11-20 05:04:57 +00:00
|
|
|
*
|
2004-10-05 22:48:36 +00:00
|
|
|
* Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
|
|
|
|
* call in a signal handler, you might crash, because the state of the
|
|
|
|
* resolution code that sends messages to lookupd might be inconsistent
|
|
|
|
* if you jump out of it in middle of a call.
|
|
|
|
*
|
|
|
|
* In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
|
|
|
|
* is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
|
2004-10-06 07:25:38 +00:00
|
|
|
* call (and possibly in other gethostbyaddr() calls), because there's
|
|
|
|
* a mutex lock held in gethostbyaddr() and it doesn't get released
|
|
|
|
* if we longjmp out of it.
|
2003-11-20 05:04:57 +00:00
|
|
|
*
|
|
|
|
* There's no guarantee that longjmp()ing out of name resolution calls
|
|
|
|
* will work on *any* platform; OpenBSD got rid of the alarm/longjmp
|
|
|
|
* code in tcpdump, to avoid those sorts of problems, and that was
|
|
|
|
* picked up by tcpdump.org tcpdump.
|
2004-10-05 22:48:36 +00:00
|
|
|
*
|
2014-12-31 22:47:04 +00:00
|
|
|
* So, for now, we do not use alarm() and SIGALRM to time out host name
|
|
|
|
* lookups. If we get a lot of complaints about lookups taking a long time,
|
|
|
|
* we can reconsider that decision. (Note that tcpdump originally added
|
|
|
|
* such a timeout mechanism that for the benefit of systems using NIS to
|
|
|
|
* look up host names; that might now be fixed in NIS implementations, for
|
|
|
|
* those sites still using NIS rather than DNS for that.... tcpdump no
|
|
|
|
* longer does that, for the same reasons that we don't.)
|
|
|
|
*
|
|
|
|
* If we're using an asynchronous DNS resolver, that shouldn't be an issue.
|
|
|
|
* If we're using a synchronous name lookup mechanism (which we'd do mainly
|
|
|
|
* to support resolving addresses and host names using more mechanisms than
|
|
|
|
* just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
|
|
|
|
* a separate thread, making it, in effect, asynchronous.
|
2003-11-20 05:04:57 +00:00
|
|
|
*/
|
1999-07-13 02:53:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-09-16 02:39:15 +00:00
|
|
|
#include <unistd.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
#endif
|
1998-09-16 02:39:15 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
1999-07-13 02:53:26 +00:00
|
|
|
#ifdef HAVE_NETDB_H
|
1998-09-16 02:39:15 +00:00
|
|
|
#include <netdb.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
#endif
|
|
|
|
|
2000-01-10 17:33:17 +00:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
Have "get_host_ipaddr()" return a Boolean indicating whether it
succeeded or failed, and, if it succeeded, have it fill in the IP
address if found through a pointer passed as the second argument.
Have it first try interpreting its first argument as a dotted-quad IP
address, with "inet_aton()", and, if that fails, have it try to
interpret it as a host name with "gethostbyname()"; don't bother with
"gethostbyaddr()", as we should be allowed to filter on IP addresses
even if there's no host name associated with them (there's no guarantee
that "gethostbyaddr()" will succeed if handed an IP address with no
corresponding name - and it looks as if FreeBSD 3.2, at least, may not
succeed in that case).
Add a "dfilter_fail()" routine that takes "printf()"-like arguments and
uses them to set an error message for the parse; doing so means that
even if the filter expression is syntactically valid, we treat it as
being invalid. (Is there a better way to force a parse to fail from
arbitrary places in routines called by the parser?)
Use that routine in the lexical analyzer.
If that error message was set, use it as is as the failure message,
rather than adding "Unable to parse filter string XXX" to it.
Have the code to handle IP addresses and host names in display filters
check whether "get_host_ipaddr()" succeeded or failed and, if it failed,
arrange that the parse fail with an error message indicating the source
of the problem.
svn path=/trunk/; revision=802
1999-10-11 03:03:12 +00:00
|
|
|
#include <arpa/inet.h>
|
2000-01-10 17:33:17 +00:00
|
|
|
#endif
|
Have "get_host_ipaddr()" return a Boolean indicating whether it
succeeded or failed, and, if it succeeded, have it fill in the IP
address if found through a pointer passed as the second argument.
Have it first try interpreting its first argument as a dotted-quad IP
address, with "inet_aton()", and, if that fails, have it try to
interpret it as a host name with "gethostbyname()"; don't bother with
"gethostbyaddr()", as we should be allowed to filter on IP addresses
even if there's no host name associated with them (there's no guarantee
that "gethostbyaddr()" will succeed if handed an IP address with no
corresponding name - and it looks as if FreeBSD 3.2, at least, may not
succeed in that case).
Add a "dfilter_fail()" routine that takes "printf()"-like arguments and
uses them to set an error message for the parse; doing so means that
even if the filter expression is syntactically valid, we treat it as
being invalid. (Is there a better way to force a parse to fail from
arbitrary places in routines called by the parser?)
Use that routine in the lexical analyzer.
If that error message was set, use it as is as the failure message,
rather than adding "Unable to parse filter string XXX" to it.
Have the code to handle IP addresses and host names in display filters
check whether "get_host_ipaddr()" succeeded or failed and, if it failed,
arrange that the parse fail with an error message indicating the source
of the problem.
svn path=/trunk/; revision=802
1999-10-11 03:03:12 +00:00
|
|
|
|
1999-07-13 02:53:26 +00:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2009-09-07 16:07:04 +00:00
|
|
|
#include <sys/socket.h> /* needed to define AF_ values on UNIX */
|
2002-10-29 00:28:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINSOCK2_H
|
2009-09-07 16:07:04 +00:00
|
|
|
#include <winsock2.h> /* needed to define AF_ values on Windows */
|
1999-07-13 02:53:26 +00:00
|
|
|
#endif
|
1998-09-16 02:39:15 +00:00
|
|
|
|
Clean up handling of missing functions.
With autotools, CMake, and nmake, if we have a function, #define
HAVE_{function_name_in_all_caps}, otherwise don't #define it.
If we provide our own version of a function in libwsutil, make sure we
have a header that declares it, and *ONLY* include that header if
HAVE_{function_name_in_all_caps} is *NOT* defined, so that we don't have
the system declaration and our declaration colliding.
Check for inet_aton, strncasecmp, and strptime with CMake, just as we do
with autotools.
Simplify the addition of {function_name_in_all_caps}_LO to libwsutil in
autotools.
Change-Id: Id5be5c73f79f81919a3a865324e400eca7b88889
Reviewed-on: https://code.wireshark.org/review/2903
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-07-06 21:02:39 +00:00
|
|
|
#ifndef HAVE_INET_ATON
|
2010-05-28 20:19:55 +00:00
|
|
|
# include "wsutil/inet_aton.h"
|
Apparently, on systems with glibc 2.2, "inet_aton()" is declared in
<arpa/inet.h>, but is, in some fashion, declared differently from the
way we declare it in "inet_v6defs.h", but "inet_ntop()" isn't defined,
so we include "inet_v6defs.h" in "inet_pton.c", which causes
"inet_pton.c" not to compile as we get a collision between the two
declarations.
Move the declaration of "inet_aton()" to "inet_aton.h", define
"NEED_INET_ATON_H" iff we didn't find "inet_aton()" in the system
libraries, and include "inet_aton.h" in the callers of "inet_aton()" iff
"NEED_INET_ATON_H" is defined, so that it doesn't get declared by us if
"inet_aton()" is defined by a system library (which hopefully means it's
declared in <arpa/inet.h> instead).
svn path=/trunk/; revision=2137
2000-07-14 07:11:53 +00:00
|
|
|
#endif
|
|
|
|
|
1999-10-14 06:55:11 +00:00
|
|
|
#ifdef NEED_INET_V6DEFS_H
|
2010-05-28 20:19:55 +00:00
|
|
|
# include "wsutil/inet_v6defs.h"
|
1999-10-14 06:55:11 +00:00
|
|
|
#endif
|
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
#if defined(_WIN32) && defined(INET6)
|
2003-08-18 18:40:10 +00:00
|
|
|
# include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-11 21:59:59 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
|
|
|
# if defined(_WIN32) && !defined(INET6)
|
|
|
|
# define socklen_t unsigned int
|
|
|
|
# endif
|
|
|
|
# include <ares.h>
|
2008-12-23 18:15:09 +00:00
|
|
|
# include <ares_version.h>
|
2008-08-11 21:59:59 +00:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_GNU_ADNS
|
|
|
|
# include <errno.h>
|
|
|
|
# include <adns.h>
|
2009-04-01 00:30:45 +00:00
|
|
|
# if defined(inet_aton) && defined(_WIN32)
|
2008-08-11 21:59:59 +00:00
|
|
|
# undef inet_aton
|
|
|
|
# endif
|
2009-09-07 16:07:04 +00:00
|
|
|
# endif /* HAVE_GNU_ADNS */
|
|
|
|
#endif /* HAVE_C_ARES */
|
2008-08-11 21:59:59 +00:00
|
|
|
|
|
|
|
|
2005-01-13 09:09:56 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
1998-09-16 02:39:15 +00:00
|
|
|
#include "packet.h"
|
2013-07-12 05:59:56 +00:00
|
|
|
#include "addr_and_mask.h"
|
2001-04-01 07:06:24 +00:00
|
|
|
#include "ipv6-utils.h"
|
2004-08-06 19:57:49 +00:00
|
|
|
#include "addr_resolv.h"
|
2013-11-17 02:55:14 +00:00
|
|
|
#include "wsutil/filesystem.h"
|
2007-10-26 05:42:12 +00:00
|
|
|
|
2013-07-15 02:48:26 +00:00
|
|
|
#include <wsutil/report_err.h>
|
2008-05-22 15:46:27 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2013-11-09 15:44:29 +00:00
|
|
|
#include <wsutil/pint.h>
|
2013-07-15 02:48:26 +00:00
|
|
|
|
|
|
|
#include <epan/strutil.h>
|
2013-12-19 20:53:44 +00:00
|
|
|
#include <epan/to_str-int.h>
|
2004-09-27 22:55:15 +00:00
|
|
|
#include <epan/prefs.h>
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2009-09-07 16:07:04 +00:00
|
|
|
#define ENAME_HOSTS "hosts"
|
|
|
|
#define ENAME_SUBNETS "subnets"
|
2009-09-13 17:24:51 +00:00
|
|
|
#define ENAME_ETHERS "ethers"
|
|
|
|
#define ENAME_IPXNETS "ipxnets"
|
2009-09-07 16:07:04 +00:00
|
|
|
#define ENAME_MANUF "manuf"
|
|
|
|
#define ENAME_SERVICES "services"
|
|
|
|
|
2010-06-28 22:43:13 +00:00
|
|
|
#define HASHETHSIZE 2048
|
|
|
|
#define HASHHOSTSIZE 2048
|
|
|
|
#define HASHIPXNETSIZE 256
|
|
|
|
#define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2004-11-17 08:53:20 +00:00
|
|
|
/* hash table used for IPv4 lookup */
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2009-09-04 10:53:37 +00:00
|
|
|
#define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
|
2001-06-07 22:07:02 +00:00
|
|
|
|
2013-08-11 19:02:26 +00:00
|
|
|
|
|
|
|
typedef struct sub_net_hashipv4 {
|
|
|
|
guint addr;
|
2013-10-10 19:17:48 +00:00
|
|
|
guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
|
2013-08-11 19:02:26 +00:00
|
|
|
struct sub_net_hashipv4 *next;
|
|
|
|
gchar ip[16];
|
|
|
|
gchar name[MAXNAMELEN];
|
|
|
|
} sub_net_hashipv4_t;
|
2004-11-17 08:53:20 +00:00
|
|
|
|
2008-01-21 21:44:34 +00:00
|
|
|
/* Array of entries of subnets of different lengths */
|
|
|
|
typedef struct {
|
2013-08-11 00:50:51 +00:00
|
|
|
gsize mask_length; /*1-32*/
|
|
|
|
guint32 mask; /* e.g. 255.255.255.*/
|
2013-08-11 19:02:26 +00:00
|
|
|
sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
|
2008-01-21 21:44:34 +00:00
|
|
|
} subnet_length_entry_t;
|
|
|
|
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-05 16:36:24 +00:00
|
|
|
#if 0
|
|
|
|
typedef struct serv_port {
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar *udp_name;
|
|
|
|
gchar *tcp_name;
|
|
|
|
gchar *sctp_name;
|
|
|
|
gchar *dccp_name;
|
2013-08-05 16:36:24 +00:00
|
|
|
} serv_port_t;
|
|
|
|
#endif
|
1999-11-21 16:32:23 +00:00
|
|
|
/* hash table used for IPX network lookup */
|
|
|
|
|
2001-06-07 22:07:02 +00:00
|
|
|
/* XXX - check goodness of hash function */
|
|
|
|
|
2009-09-07 16:07:04 +00:00
|
|
|
#define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
|
2001-06-07 22:07:02 +00:00
|
|
|
|
2004-11-17 08:53:20 +00:00
|
|
|
typedef struct hashipxnet {
|
2013-08-11 00:50:51 +00:00
|
|
|
guint addr;
|
|
|
|
struct hashipxnet *next;
|
|
|
|
gchar name[MAXNAMELEN];
|
2004-11-17 08:53:20 +00:00
|
|
|
} hashipxnet_t;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
/* hash tables used for ethernet and manufacturer lookup */
|
2010-07-01 15:11:02 +00:00
|
|
|
#define HASHETHER_STATUS_UNRESOLVED 1
|
|
|
|
#define HASHETHER_STATUS_RESOLVED_DUMMY 2
|
|
|
|
#define HASHETHER_STATUS_RESOLVED_NAME 3
|
|
|
|
|
2013-08-02 15:05:00 +00:00
|
|
|
#if 0
|
1998-09-25 23:24:07 +00:00
|
|
|
typedef struct hashether {
|
2013-08-11 00:50:51 +00:00
|
|
|
struct hashether *next;
|
|
|
|
guint status; /* (See above) */
|
|
|
|
guint8 addr[6];
|
|
|
|
char hexaddr[6*3];
|
|
|
|
char resolved_name[MAXNAMELEN];
|
1998-09-25 23:24:07 +00:00
|
|
|
} hashether_t;
|
2013-08-02 15:05:00 +00:00
|
|
|
#endif
|
1998-09-25 23:24:07 +00:00
|
|
|
/* internal ethernet type */
|
1998-09-16 02:39:15 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
typedef struct _ether
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint8 addr[6];
|
|
|
|
char name[MAXNAMELEN];
|
1998-09-25 23:24:07 +00:00
|
|
|
} ether_t;
|
|
|
|
|
1999-11-21 16:32:23 +00:00
|
|
|
/* internal ipxnet type */
|
|
|
|
|
|
|
|
typedef struct _ipxnet
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint addr;
|
|
|
|
char name[MAXNAMELEN];
|
1999-11-21 16:32:23 +00:00
|
|
|
} ipxnet_t;
|
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
static GHashTable *ipxnet_hash_table = NULL;
|
2013-08-11 19:02:26 +00:00
|
|
|
static GHashTable *ipv4_hash_table = NULL;
|
2013-08-18 19:02:48 +00:00
|
|
|
static GHashTable *ipv6_hash_table = NULL;
|
2013-08-11 19:02:26 +00:00
|
|
|
|
2013-10-23 14:04:19 +00:00
|
|
|
static GSList *manually_resolved_ipv4_list = NULL;
|
|
|
|
static GSList *manually_resolved_ipv6_list = NULL;
|
2013-10-23 12:02:15 +00:00
|
|
|
|
|
|
|
typedef struct _resolved_ipv4
|
|
|
|
{
|
|
|
|
guint32 host_addr;
|
|
|
|
char name[MAXNAMELEN];
|
|
|
|
} resolved_ipv4_t;
|
|
|
|
|
|
|
|
typedef struct _resolved_ipv6
|
|
|
|
{
|
|
|
|
struct e_in6_addr ip6_addr;
|
|
|
|
char name[MAXNAMELEN];
|
|
|
|
} resolved_ipv6_t;
|
|
|
|
|
2013-09-22 19:40:02 +00:00
|
|
|
static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
|
|
|
|
|
2009-09-07 16:07:04 +00:00
|
|
|
static gchar *cb_service;
|
2013-08-05 16:36:24 +00:00
|
|
|
static port_type cb_proto = PT_NONE;
|
|
|
|
|
2008-04-12 14:08:19 +00:00
|
|
|
|
2013-08-01 20:11:13 +00:00
|
|
|
static GHashTable *manuf_hashtable = NULL;
|
|
|
|
static GHashTable *wka_hashtable = NULL;
|
2013-08-02 15:05:00 +00:00
|
|
|
static GHashTable *eth_hashtable = NULL;
|
2013-08-05 16:36:24 +00:00
|
|
|
static GHashTable *serv_port_hashtable = NULL;
|
2013-08-01 20:11:13 +00:00
|
|
|
|
2008-01-21 21:44:34 +00:00
|
|
|
static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
|
|
|
|
static gboolean have_subnet_entry = FALSE;
|
|
|
|
|
2009-12-22 20:01:06 +00:00
|
|
|
static gboolean new_resolved_objects = FALSE;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2012-11-29 00:32:39 +00:00
|
|
|
static GPtrArray* extra_hosts_files = NULL;
|
2011-03-24 22:47:57 +00:00
|
|
|
|
2004-01-10 17:31:40 +00:00
|
|
|
static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
|
2010-04-03 18:18:50 +00:00
|
|
|
static void add_serv_port_cb(const guint32 port);
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
|
2014-02-25 20:42:35 +00:00
|
|
|
/* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
|
2013-08-18 19:02:48 +00:00
|
|
|
* One-at-a-Time hash
|
|
|
|
*/
|
2014-02-25 20:42:35 +00:00
|
|
|
static guint32
|
2013-08-18 19:02:48 +00:00
|
|
|
ipv6_oat_hash(gconstpointer key)
|
|
|
|
{
|
2013-10-23 12:02:15 +00:00
|
|
|
int len = 16;
|
2013-08-18 19:02:48 +00:00
|
|
|
const unsigned char *p = (const unsigned char *)key;
|
|
|
|
guint32 h = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0; i < len; i++ ) {
|
|
|
|
h += p[i];
|
|
|
|
h += ( h << 10 );
|
|
|
|
h ^= ( h >> 6 );
|
|
|
|
}
|
|
|
|
|
|
|
|
h += ( h << 3 );
|
|
|
|
h ^= ( h >> 11 );
|
|
|
|
h += ( h << 15 );
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
ipv6_equal(gconstpointer v1, gconstpointer v2)
|
|
|
|
{
|
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
if( memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0 ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-08-18 19:02:48 +00:00
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
return FALSE;
|
2013-08-18 19:02:48 +00:00
|
|
|
}
|
|
|
|
|
2002-01-13 20:35:12 +00:00
|
|
|
/*
|
|
|
|
* Flag controlling what names to resolve.
|
|
|
|
*/
|
2014-04-16 20:23:04 +00:00
|
|
|
e_addr_resolve gbl_resolv_flags = {TRUE, FALSE, FALSE, TRUE, TRUE, FALSE};
|
2012-07-06 07:34:08 +00:00
|
|
|
#if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
|
2012-07-06 04:48:36 +00:00
|
|
|
static guint name_resolve_concurrency = 500;
|
2012-07-06 07:34:08 +00:00
|
|
|
#endif
|
2002-01-13 20:35:12 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
/*
|
|
|
|
* Global variables (can be changed in GUI sections)
|
2001-10-21 19:54:49 +00:00
|
|
|
* XXX - they could be changed in GUI code, but there's currently no
|
|
|
|
* GUI code to change them.
|
1998-09-25 23:24:07 +00:00
|
|
|
*/
|
|
|
|
|
2010-04-27 22:46:39 +00:00
|
|
|
gchar *g_ethers_path = NULL; /* global ethers file */
|
|
|
|
gchar *g_pethers_path = NULL; /* personal ethers file */
|
|
|
|
gchar *g_ipxnets_path = NULL; /* global ipxnets file */
|
|
|
|
gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
|
2009-09-07 16:07:04 +00:00
|
|
|
gchar *g_services_path = NULL; /* global services file */
|
|
|
|
gchar *g_pservices_path = NULL; /* personal services file */
|
2010-04-27 22:46:39 +00:00
|
|
|
/* first resolving call */
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
/* c-ares */
|
|
|
|
#ifdef HAVE_C_ARES
|
|
|
|
/*
|
|
|
|
* Submitted queries trigger a callback (c_ares_ghba_cb()).
|
|
|
|
* Queries are added to c_ares_queue_head. During processing, queries are
|
|
|
|
* popped off the front of c_ares_queue_head and submitted using
|
|
|
|
* ares_gethostbyaddr().
|
|
|
|
* The callback processes the response, then frees the request.
|
|
|
|
*/
|
2009-09-13 17:24:51 +00:00
|
|
|
#define ASYNC_DNS
|
|
|
|
typedef struct _async_dns_queue_msg
|
2008-08-07 21:41:48 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
union {
|
|
|
|
guint32 ip4;
|
|
|
|
struct e_in6_addr ip6;
|
|
|
|
} addr;
|
|
|
|
int family;
|
2009-09-13 17:24:51 +00:00
|
|
|
} async_dns_queue_msg_t;
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2009-12-24 00:58:31 +00:00
|
|
|
typedef struct _async_hostent {
|
2013-08-11 00:50:51 +00:00
|
|
|
int addr_size;
|
|
|
|
int copied;
|
|
|
|
void *addrp;
|
2009-12-24 00:58:31 +00:00
|
|
|
} async_hostent_t;
|
|
|
|
|
2009-01-06 18:31:57 +00:00
|
|
|
#if ( ( ARES_VERSION_MAJOR < 1 ) \
|
2013-08-11 00:50:51 +00:00
|
|
|
|| ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
|
2008-12-23 18:15:09 +00:00
|
|
|
static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
|
|
|
|
#else
|
2008-08-07 21:41:48 +00:00
|
|
|
static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
|
2010-01-19 23:18:23 +00:00
|
|
|
#endif
|
2009-12-24 00:58:31 +00:00
|
|
|
|
|
|
|
ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
|
|
|
|
ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#else
|
|
|
|
/* GNU ADNS */
|
2003-05-04 18:50:56 +00:00
|
|
|
#ifdef HAVE_GNU_ADNS
|
2009-09-13 17:24:51 +00:00
|
|
|
#define ASYNC_DNS
|
2008-08-07 21:41:48 +00:00
|
|
|
/*
|
|
|
|
* Submitted queries have to be checked individually using adns_check().
|
|
|
|
* Queries are added to adns_queue_head. During processing, the list is
|
|
|
|
* iterated twice: once to request queries up to the concurrency limit,
|
|
|
|
* and once to check the status of each query.
|
|
|
|
*/
|
2004-11-10 04:57:34 +00:00
|
|
|
|
2003-05-04 18:50:56 +00:00
|
|
|
adns_state ads;
|
|
|
|
|
2009-09-13 17:24:51 +00:00
|
|
|
typedef struct _async_dns_queue_msg
|
2003-05-04 18:50:56 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean submitted;
|
|
|
|
guint32 ip4_addr;
|
|
|
|
int type;
|
|
|
|
adns_query query;
|
2009-09-13 17:24:51 +00:00
|
|
|
} async_dns_queue_msg_t;
|
2003-05-04 18:50:56 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_GNU_ADNS */
|
2008-08-07 21:41:48 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
2009-09-13 17:24:51 +00:00
|
|
|
#ifdef ASYNC_DNS
|
2010-06-28 22:43:13 +00:00
|
|
|
static gboolean async_dns_initialized = FALSE;
|
2012-07-06 04:48:36 +00:00
|
|
|
static guint async_dns_in_flight = 0;
|
2010-06-28 22:43:13 +00:00
|
|
|
static GList *async_dns_queue_head = NULL;
|
2009-09-13 17:24:51 +00:00
|
|
|
|
|
|
|
/* push a dns request */
|
|
|
|
static void
|
|
|
|
add_async_dns_ipv4(int type, guint32 addr)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_queue_msg_t *msg;
|
2009-09-13 17:24:51 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
msg = g_new(async_dns_queue_msg_t,1);
|
2009-09-13 17:24:51 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
msg->family = type;
|
|
|
|
msg->addr.ip4 = addr;
|
2009-09-13 17:24:51 +00:00
|
|
|
#else
|
2013-08-11 00:50:51 +00:00
|
|
|
msg->type = type;
|
|
|
|
msg->ip4_addr = addr;
|
|
|
|
msg->submitted = FALSE;
|
2009-09-13 17:24:51 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
|
2009-09-13 17:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2008-01-21 21:44:34 +00:00
|
|
|
typedef struct {
|
2013-08-11 00:50:51 +00:00
|
|
|
guint32 mask;
|
|
|
|
gsize mask_length;
|
|
|
|
const gchar* name; /* Shallow copy */
|
2008-01-21 21:44:34 +00:00
|
|
|
} subnet_entry_t;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Miscellaneous functions
|
|
|
|
*/
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static int
|
|
|
|
fgetline(char **buf, int *size, FILE *fp)
|
2007-08-06 08:44:26 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
int len;
|
|
|
|
int c;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (fp == NULL || buf == NULL)
|
|
|
|
return -1;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (*buf == NULL) {
|
|
|
|
if (*size == 0)
|
|
|
|
*size = BUFSIZ;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*buf = (char *)g_malloc(*size);
|
|
|
|
}
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_assert(*buf);
|
|
|
|
g_assert(*size > 0);
|
2009-09-06 14:53:21 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (feof(fp))
|
|
|
|
return -1;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
len = 0;
|
|
|
|
while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
|
|
|
|
if (len+1 >= *size) {
|
|
|
|
*buf = (char *)g_realloc(*buf, *size += BUFSIZ);
|
|
|
|
}
|
|
|
|
(*buf)[len++] = c;
|
2007-08-06 08:44:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (len == 0 && c == EOF)
|
|
|
|
return -1;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
(*buf)[len] = '\0';
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return len;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
|
|
|
} /* fgetline */
|
|
|
|
|
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
/*
|
2002-08-28 20:41:00 +00:00
|
|
|
* Local function definitions
|
1998-09-25 23:24:07 +00:00
|
|
|
*/
|
2008-01-21 21:44:34 +00:00
|
|
|
static subnet_entry_t subnet_lookup(const guint32 addr);
|
2010-04-03 18:18:50 +00:00
|
|
|
static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
2013-08-05 16:36:24 +00:00
|
|
|
add_service_name(port_type proto, const guint port, const char *service_name)
|
2007-08-06 08:44:26 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_t *serv_port_table;
|
|
|
|
int *key;
|
|
|
|
|
|
|
|
key = (int *)g_new(int, 1);
|
|
|
|
*key = port;
|
|
|
|
|
|
|
|
serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
|
|
|
|
if (serv_port_table == NULL) {
|
|
|
|
serv_port_table = g_new0(serv_port_t,1);
|
|
|
|
g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_free(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(proto){
|
|
|
|
case PT_TCP:
|
2013-08-15 02:38:00 +00:00
|
|
|
g_free(serv_port_table->tcp_name);
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_table->tcp_name = g_strdup(service_name);
|
|
|
|
break;
|
|
|
|
case PT_UDP:
|
2013-08-15 02:38:00 +00:00
|
|
|
g_free(serv_port_table->udp_name);
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_table->udp_name = g_strdup(service_name);
|
|
|
|
break;
|
|
|
|
case PT_SCTP:
|
2013-08-15 02:38:00 +00:00
|
|
|
g_free(serv_port_table->sctp_name);
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_table->sctp_name = g_strdup(service_name);
|
|
|
|
break;
|
|
|
|
case PT_DCCP:
|
2013-08-15 02:38:00 +00:00
|
|
|
g_free(serv_port_table->dccp_name);
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_table->dccp_name = g_strdup(service_name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
/* Should not happen */
|
|
|
|
}
|
|
|
|
|
|
|
|
new_resolved_objects = TRUE;
|
2007-08-06 08:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
parse_service_line (char *line)
|
2007-08-06 08:44:26 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* See the services(4) or services(5) man page for services file format
|
|
|
|
* (not available on all systems).
|
|
|
|
*/
|
|
|
|
|
|
|
|
gchar *cp;
|
|
|
|
gchar *service;
|
|
|
|
gchar *port;
|
|
|
|
port_type proto;
|
|
|
|
|
|
|
|
range_t *port_rng = NULL;
|
|
|
|
guint32 max_port = MAX_UDP_PORT;
|
|
|
|
|
|
|
|
if ((cp = strchr(line, '#')))
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
if ((cp = strtok(line, " \t")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
service = cp;
|
|
|
|
|
|
|
|
if ((cp = strtok(NULL, " \t")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
port = cp;
|
|
|
|
|
|
|
|
if (strtok(cp, "/") == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((cp = strtok(NULL, "/")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* seems we got all interesting things from the file */
|
|
|
|
if(strcmp(cp, "tcp") == 0) {
|
|
|
|
max_port = MAX_TCP_PORT;
|
|
|
|
proto = PT_TCP;
|
|
|
|
}
|
|
|
|
else if(strcmp(cp, "udp") == 0) {
|
|
|
|
max_port = MAX_UDP_PORT;
|
|
|
|
proto = PT_UDP;
|
|
|
|
}
|
|
|
|
else if(strcmp(cp, "sctp") == 0) {
|
|
|
|
max_port = MAX_SCTP_PORT;
|
|
|
|
proto = PT_SCTP;
|
|
|
|
}
|
|
|
|
else if(strcmp(cp, "dccp") == 0) {
|
|
|
|
max_port = MAX_DCCP_PORT;
|
|
|
|
proto = PT_DCCP;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
|
|
|
|
/* some assertion here? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb_service = service;
|
|
|
|
cb_proto = proto;
|
|
|
|
range_foreach(port_rng, add_serv_port_cb);
|
|
|
|
g_free (port_rng);
|
|
|
|
cb_proto = PT_NONE;
|
2007-08-06 08:44:26 +00:00
|
|
|
} /* parse_service_line */
|
|
|
|
|
|
|
|
|
2008-04-12 14:08:19 +00:00
|
|
|
static void
|
2010-04-03 18:18:50 +00:00
|
|
|
add_serv_port_cb(const guint32 port)
|
2008-04-12 14:08:19 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if ( port ) {
|
|
|
|
add_service_name(cb_proto, port, cb_service);
|
|
|
|
}
|
2008-04-12 14:08:19 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
parse_services_file(const char * path)
|
2007-08-06 08:44:26 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
FILE *serv_p;
|
|
|
|
static int size = 0;
|
|
|
|
static char *buf = NULL;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* services hash table initialization */
|
|
|
|
serv_p = ws_fopen(path, "r");
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (serv_p == NULL)
|
|
|
|
return;
|
2007-08-06 08:44:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (fgetline(&buf, &size, serv_p) >= 0) {
|
|
|
|
parse_service_line (buf);
|
|
|
|
}
|
2009-09-06 08:59:30 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
fclose(serv_p);
|
2007-08-06 08:44:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:36:24 +00:00
|
|
|
/* -----------------
|
|
|
|
* unsigned integer to ascii
|
|
|
|
*/
|
|
|
|
static gchar *
|
2015-01-07 21:24:17 +00:00
|
|
|
wmem_utoa(wmem_allocator_t *allocator, guint port)
|
2013-08-05 16:36:24 +00:00
|
|
|
{
|
2015-01-07 21:24:17 +00:00
|
|
|
gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
|
2013-08-05 16:36:24 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* XXX, guint32_to_str() ? */
|
|
|
|
guint32_to_str_buf(port, bp, MAXNAMELEN);
|
|
|
|
return bp;
|
2013-08-05 16:36:24 +00:00
|
|
|
}
|
2007-08-06 08:44:26 +00:00
|
|
|
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static gchar
|
|
|
|
*serv_name_lookup(const guint port, const port_type proto)
|
1998-09-16 02:39:15 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_t *serv_port_table;
|
|
|
|
gchar *name;
|
2013-08-05 16:36:24 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
|
|
|
|
|
|
|
|
if(serv_port_table){
|
|
|
|
/* Set which table we should look up port in */
|
|
|
|
switch(proto) {
|
|
|
|
case PT_UDP:
|
|
|
|
if(serv_port_table->udp_name){
|
|
|
|
return serv_port_table->udp_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PT_TCP:
|
|
|
|
if(serv_port_table->tcp_name){
|
|
|
|
return serv_port_table->tcp_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PT_SCTP:
|
|
|
|
if(serv_port_table->sctp_name){
|
|
|
|
return serv_port_table->sctp_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PT_DCCP:
|
|
|
|
if(serv_port_table->dccp_name){
|
|
|
|
return serv_port_table->dccp_name;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* not yet implemented */
|
|
|
|
return NULL;
|
|
|
|
/*NOTREACHED*/
|
|
|
|
} /* proto */
|
|
|
|
}
|
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
/* getservbyport() was used here but it was to expensive, if the functionality is desired
|
|
|
|
* it would be better to pre parse etc/services or C:\Windows\System32\drivers\etc at
|
|
|
|
* startup
|
|
|
|
*/
|
2013-09-16 17:53:41 +00:00
|
|
|
name = (gchar*)g_malloc(16);
|
|
|
|
guint32_to_str_buf(port, name, 16);
|
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
if(serv_port_table == NULL){
|
2013-08-11 00:50:51 +00:00
|
|
|
int *key;
|
|
|
|
|
|
|
|
key = (int *)g_new(int, 1);
|
|
|
|
*key = port;
|
|
|
|
serv_port_table = g_new0(serv_port_t,1);
|
|
|
|
g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
|
|
|
|
}
|
|
|
|
switch(proto) {
|
|
|
|
case PT_UDP:
|
|
|
|
serv_port_table->udp_name = name;
|
|
|
|
break;
|
|
|
|
case PT_TCP:
|
|
|
|
serv_port_table->tcp_name = name;
|
|
|
|
break;
|
|
|
|
case PT_SCTP:
|
|
|
|
serv_port_table->sctp_name = name;
|
|
|
|
break;
|
|
|
|
case PT_DCCP:
|
|
|
|
serv_port_table->dccp_name = name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
return name;
|
1998-09-16 02:39:15 +00:00
|
|
|
|
|
|
|
} /* serv_name_lookup */
|
|
|
|
|
2013-08-11 00:35:22 +00:00
|
|
|
static void
|
|
|
|
destroy_serv_port(gpointer data)
|
|
|
|
{
|
|
|
|
serv_port_t *table = (serv_port_t*)data;
|
|
|
|
g_free(table->udp_name);
|
|
|
|
g_free(table->tcp_name);
|
|
|
|
g_free(table->sctp_name);
|
|
|
|
g_free(table->dccp_name);
|
|
|
|
g_free(table);
|
|
|
|
}
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
static void
|
|
|
|
initialize_services(void)
|
|
|
|
{
|
2013-09-16 18:36:50 +00:00
|
|
|
#ifdef _WIN32
|
2013-10-23 12:02:15 +00:00
|
|
|
char *hostspath;
|
2013-09-16 18:36:50 +00:00
|
|
|
char *sysroot;
|
|
|
|
static char rootpath_nt[] = "\\system32\\drivers\\etc\\services";
|
|
|
|
#endif /* _WIN32 */
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* the hash table won't ignore duplicates, so use the personal path first */
|
|
|
|
g_assert(serv_port_hashtable == NULL);
|
|
|
|
serv_port_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, destroy_serv_port);
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-09-16 18:36:50 +00:00
|
|
|
/* Read the system services file first */
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
sysroot = getenv_utf8("WINDIR");
|
|
|
|
if (sysroot != NULL) {
|
|
|
|
/*
|
|
|
|
* The file should be under WINDIR.
|
|
|
|
* If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
|
|
|
|
* %WINDIR%\system32\drivers\etc\services.
|
|
|
|
*/
|
2013-10-23 12:02:15 +00:00
|
|
|
hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
|
|
|
|
parse_services_file(hostspath);
|
|
|
|
g_free(hostspath);
|
|
|
|
}
|
2013-09-16 18:36:50 +00:00
|
|
|
#else
|
2013-10-23 12:02:15 +00:00
|
|
|
parse_services_file("/etc/services");
|
2013-09-16 18:36:50 +00:00
|
|
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* set personal services path */
|
|
|
|
if (g_pservices_path == NULL)
|
|
|
|
g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
parse_services_file(g_pservices_path);
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Compute the pathname of the services file. */
|
|
|
|
if (g_services_path == NULL) {
|
|
|
|
g_services_path = get_datafile_path(ENAME_SERVICES);
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
parse_services_file(g_services_path);
|
2013-08-07 16:46:08 +00:00
|
|
|
|
|
|
|
} /* initialize_services */
|
|
|
|
|
|
|
|
static void
|
|
|
|
service_name_lookup_cleanup(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if(serv_port_hashtable){
|
|
|
|
g_hash_table_destroy(serv_port_hashtable);
|
|
|
|
serv_port_hashtable = NULL;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
/* Fill in an IP4 structure with info from subnets file or just with the
|
|
|
|
* string form of the address.
|
2008-01-21 21:44:34 +00:00
|
|
|
*/
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
|
2008-01-21 21:44:34 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_entry_t subnet_entry;
|
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY)
|
2013-08-11 00:50:51 +00:00
|
|
|
return; /* already done */
|
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
/* Do we have a subnet for this address? */
|
|
|
|
subnet_entry = subnet_lookup(addr);
|
|
|
|
if(0 != subnet_entry.mask) {
|
|
|
|
/* Print name, then '.' then IP address after subnet mask */
|
|
|
|
guint32 host_addr;
|
|
|
|
gchar buffer[MAX_IP_STR_LEN];
|
|
|
|
gchar* paddr;
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
host_addr = addr & (~(guint32)subnet_entry.mask);
|
|
|
|
ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
|
|
|
|
paddr = buffer;
|
|
|
|
|
|
|
|
/* Skip to first octet that is not totally masked
|
|
|
|
* If length of mask is 32, we chomp the whole address.
|
|
|
|
* If the address string starts '.' (should not happen?),
|
|
|
|
* we skip that '.'.
|
|
|
|
*/
|
|
|
|
i = subnet_entry.mask_length / 8;
|
|
|
|
while(*(paddr) != '\0' && i > 0) {
|
|
|
|
if(*(++paddr) == '.') {
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
2010-04-27 22:46:39 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* There are more efficient ways to do this, but this is safe if we
|
|
|
|
* trust g_snprintf and MAXNAMELEN
|
|
|
|
*/
|
|
|
|
g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
|
|
|
|
} else {
|
|
|
|
ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
|
2010-04-27 22:46:39 +00:00
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
|
|
|
|
|
|
|
static void
|
2013-08-11 00:50:51 +00:00
|
|
|
c_ares_ghba_cb(
|
|
|
|
void *arg,
|
|
|
|
int status,
|
2009-01-06 18:31:57 +00:00
|
|
|
#if ( ( ARES_VERSION_MAJOR < 1 ) \
|
2013-08-11 00:50:51 +00:00
|
|
|
|| ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
|
|
|
|
struct hostent *he
|
2008-12-23 18:15:09 +00:00
|
|
|
#else
|
2013-08-11 00:50:51 +00:00
|
|
|
int timeouts _U_,
|
|
|
|
struct hostent *he
|
2008-12-23 18:15:09 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
if (!caqm) return;
|
|
|
|
/* XXX, what to do if async_dns_in_flight == 0? */
|
|
|
|
async_dns_in_flight--;
|
|
|
|
|
|
|
|
if (status == ARES_SUCCESS) {
|
|
|
|
for (p = he->h_addr_list; *p != NULL; p++) {
|
|
|
|
switch(caqm->family) {
|
|
|
|
case AF_INET:
|
|
|
|
add_ipv4_name(caqm->addr.ip4, he->h_name);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
add_ipv6_name(&caqm->addr.ip6, he->h_name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Throw an exception? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(caqm);
|
2008-08-07 21:41:48 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_C_ARES */
|
|
|
|
|
2009-09-06 08:59:30 +00:00
|
|
|
/* --------------- */
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashipv4_t *
|
|
|
|
new_ipv4(const guint addr)
|
2009-09-06 08:59:30 +00:00
|
|
|
{
|
2013-08-11 19:02:26 +00:00
|
|
|
hashipv4_t *tp = g_new(hashipv4_t, 1);
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->addr = addr;
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = 0;
|
2013-08-29 22:08:23 +00:00
|
|
|
ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
|
2013-08-11 00:50:51 +00:00
|
|
|
return tp;
|
2009-09-06 08:59:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashipv4_t *
|
2012-07-08 01:31:48 +00:00
|
|
|
host_lookup(const guint addr, gboolean *found)
|
1998-09-16 02:39:15 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipv4_t * volatile tp;
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*found = TRUE;
|
2000-08-10 20:09:29 +00:00
|
|
|
|
2014-05-11 03:30:48 +00:00
|
|
|
tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
|
2013-08-11 19:02:26 +00:00
|
|
|
if(tp == NULL){
|
|
|
|
tp = new_ipv4(addr);
|
2014-05-11 03:30:48 +00:00
|
|
|
g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
|
2013-08-11 19:02:26 +00:00
|
|
|
}else{
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY){
|
2013-08-11 19:02:26 +00:00
|
|
|
goto try_resolv;
|
|
|
|
}
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
|
2013-08-11 19:02:26 +00:00
|
|
|
*found = FALSE;
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-13 01:41:51 +00:00
|
|
|
return tp;
|
1998-09-16 02:39:15 +00:00
|
|
|
}
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 19:02:26 +00:00
|
|
|
try_resolv:
|
2013-08-11 00:50:51 +00:00
|
|
|
if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
|
2013-08-11 19:02:26 +00:00
|
|
|
|
2009-09-13 17:24:51 +00:00
|
|
|
#ifdef ASYNC_DNS
|
2013-08-11 00:50:51 +00:00
|
|
|
if (gbl_resolv_flags.concurrent_dns &&
|
|
|
|
name_resolve_concurrency > 0 &&
|
|
|
|
async_dns_initialized) {
|
|
|
|
add_async_dns_ipv4(AF_INET, addr);
|
|
|
|
/* XXX found is set to TRUE, which seems a bit odd, but I'm not
|
|
|
|
* going to risk changing the semantics.
|
|
|
|
*/
|
|
|
|
fill_dummy_ip4(addr, tp);
|
|
|
|
return tp;
|
|
|
|
}
|
2009-09-13 17:24:51 +00:00
|
|
|
#endif /* ASYNC_DNS */
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* unknown host or DNS timeout */
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2010-04-27 22:46:39 +00:00
|
|
|
}
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*found = FALSE;
|
2009-09-06 08:59:30 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
fill_dummy_ip4(addr, tp);
|
|
|
|
return tp;
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2012-07-08 01:31:48 +00:00
|
|
|
} /* host_lookup */
|
2009-09-06 08:59:30 +00:00
|
|
|
|
|
|
|
/* --------------- */
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashipv6_t *
|
|
|
|
new_ipv6(const struct e_in6_addr *addr)
|
2009-09-06 08:59:30 +00:00
|
|
|
{
|
2013-08-18 19:02:48 +00:00
|
|
|
hashipv6_t *tp = g_new(hashipv6_t,1);
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->addr = *addr;
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = 0;
|
2013-08-29 22:08:23 +00:00
|
|
|
ip6_to_str_buf(addr, tp->ip6);
|
2013-08-11 00:50:51 +00:00
|
|
|
return tp;
|
2009-09-06 08:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------ */
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashipv6_t *
|
2012-07-08 01:31:48 +00:00
|
|
|
host_lookup6(const struct e_in6_addr *addr, gboolean *found)
|
1999-03-28 18:32:03 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipv6_t * volatile tp;
|
2009-09-29 09:49:43 +00:00
|
|
|
#ifdef INET6
|
2008-08-07 21:41:48 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_queue_msg_t *caqm;
|
2008-08-07 21:41:48 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
2010-10-05 17:57:23 +00:00
|
|
|
#endif /* INET6 */
|
2000-08-10 22:35:30 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*found = TRUE;
|
2004-11-17 09:44:41 +00:00
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
|
|
|
|
if(tp == NULL){
|
2013-10-23 12:02:15 +00:00
|
|
|
struct e_in6_addr *addr_key;
|
2004-11-17 09:44:41 +00:00
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
addr_key = g_new(struct e_in6_addr,1);
|
2013-08-18 19:02:48 +00:00
|
|
|
tp = new_ipv6(addr);
|
2013-10-23 12:02:15 +00:00
|
|
|
memcpy(addr_key, addr, 16);
|
2013-08-18 19:02:48 +00:00
|
|
|
g_hash_table_insert(ipv6_hash_table, addr_key, tp);
|
|
|
|
}else{
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY){
|
2013-08-18 19:02:48 +00:00
|
|
|
goto try_resolv;
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
|
2013-08-18 19:02:48 +00:00
|
|
|
*found = FALSE;
|
|
|
|
}
|
|
|
|
return tp;
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
try_resolv:
|
2013-08-11 00:50:51 +00:00
|
|
|
if (gbl_resolv_flags.network_name &&
|
|
|
|
gbl_resolv_flags.use_external_net_name_resolver) {
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
|
2009-09-06 08:59:30 +00:00
|
|
|
#ifdef INET6
|
2004-11-17 09:44:41 +00:00
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((gbl_resolv_flags.concurrent_dns) &&
|
|
|
|
name_resolve_concurrency > 0 &&
|
|
|
|
async_dns_initialized) {
|
|
|
|
caqm = g_new(async_dns_queue_msg_t,1);
|
|
|
|
caqm->family = AF_INET6;
|
|
|
|
memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
|
|
|
|
async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
|
|
|
|
|
|
|
|
/* XXX found is set to TRUE, which seems a bit odd, but I'm not
|
|
|
|
* going to risk changing the semantics.
|
|
|
|
*/
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0){
|
2013-08-29 22:08:23 +00:00
|
|
|
g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
|
2013-08-11 00:50:51 +00:00
|
|
|
ip6_to_str_buf(addr, tp->name);
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
return tp;
|
|
|
|
}
|
2008-08-07 21:41:48 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
|
|
|
|
2009-09-06 08:59:30 +00:00
|
|
|
#endif /* INET6 */
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
1999-03-28 18:32:03 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* unknown host or DNS timeout */
|
2013-10-10 19:17:48 +00:00
|
|
|
if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
|
|
|
|
tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
|
2013-08-29 22:08:23 +00:00
|
|
|
g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
*found = FALSE;
|
|
|
|
return tp;
|
2009-09-06 08:59:30 +00:00
|
|
|
|
|
|
|
} /* host_lookup6 */
|
2004-11-17 09:44:41 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static const gchar *
|
|
|
|
solve_address_to_name(const address *addr)
|
2005-02-22 01:55:04 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
switch (addr->type) {
|
2005-02-22 01:55:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
case AT_ETHER:
|
|
|
|
return get_ether_name((const guint8 *)addr->data);
|
2005-02-22 01:55:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
case AT_IPv4: {
|
|
|
|
guint32 ip4_addr;
|
|
|
|
memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
|
|
|
|
return get_hostname(ip4_addr);
|
|
|
|
}
|
2005-02-22 01:55:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
case AT_IPv6: {
|
|
|
|
struct e_in6_addr ip6_addr;
|
|
|
|
memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
|
|
|
|
return get_hostname6(&ip6_addr);
|
|
|
|
}
|
2005-02-22 01:55:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
case AT_STRINGZ:
|
|
|
|
return (const gchar *)addr->data;
|
2005-02-22 01:55:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-07 15:49:14 +00:00
|
|
|
}
|
2005-02-22 01:55:04 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
/*
|
|
|
|
* Ethernet / manufacturer resolution
|
|
|
|
*
|
|
|
|
* The following functions implement ethernet address resolution and
|
2002-08-28 20:41:00 +00:00
|
|
|
* ethers files parsing (see ethers(4)).
|
1998-09-25 23:24:07 +00:00
|
|
|
*
|
2002-08-28 20:41:00 +00:00
|
|
|
* The manuf file has the same format as ethers(4) except that names are
|
2010-11-30 22:28:34 +00:00
|
|
|
* truncated to MAXMANUFLEN-1 (8) characters and that an address contains
|
1998-09-25 23:24:07 +00:00
|
|
|
* only 3 bytes (instead of 6).
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
*
|
2002-08-28 20:41:00 +00:00
|
|
|
* I decide to not use the existing functions (see ethers(3) on some
|
1998-09-25 23:24:07 +00:00
|
|
|
* operating systems) for the following reasons:
|
|
|
|
* - performance gains (use of hash tables and some other enhancements),
|
|
|
|
* - use of two ethers files (system-wide and per user),
|
|
|
|
* - avoid the use of NIS maps,
|
|
|
|
* - lack of these functions on some systems.
|
|
|
|
*
|
|
|
|
* So the following functions do _not_ behave as the standard ones.
|
|
|
|
*
|
|
|
|
* -- Laurent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-09-09 19:38:11 +00:00
|
|
|
/*
|
|
|
|
* If "manuf_file" is FALSE, parse a 6-byte MAC address.
|
|
|
|
* If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
|
|
|
|
* mask.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
|
2013-08-11 00:50:51 +00:00
|
|
|
const gboolean manuf_file)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long num;
|
|
|
|
char *p;
|
|
|
|
char sep = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
/* Get a hex number, 1 or 2 digits, no sign characters allowed. */
|
2014-10-17 23:10:53 +00:00
|
|
|
if (!g_ascii_isxdigit(*cp))
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE;
|
|
|
|
num = strtoul(cp, &p, 16);
|
|
|
|
if (p == cp)
|
|
|
|
return FALSE; /* failed */
|
|
|
|
if (num > 0xFF)
|
|
|
|
return FALSE; /* not a valid octet */
|
|
|
|
eth->addr[i] = (guint8) num;
|
|
|
|
cp = p; /* skip past the number */
|
|
|
|
|
|
|
|
/* OK, what character terminated the octet? */
|
|
|
|
if (*cp == '/') {
|
|
|
|
/* "/" - this has a mask. */
|
|
|
|
if (!manuf_file) {
|
|
|
|
/* Entries with masks are allowed only in the "manuf" files. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
cp++; /* skip past the '/' to get to the mask */
|
2014-10-17 23:10:53 +00:00
|
|
|
if (!g_ascii_isdigit(*cp))
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE; /* no sign allowed */
|
|
|
|
num = strtoul(cp, &p, 10);
|
|
|
|
if (p == cp)
|
|
|
|
return FALSE; /* failed */
|
|
|
|
cp = p; /* skip past the number */
|
2014-10-17 23:10:53 +00:00
|
|
|
if (*cp != '\0' && !g_ascii_isspace(*cp))
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE; /* bogus terminator */
|
|
|
|
if (num == 0 || num >= 48)
|
|
|
|
return FALSE; /* bogus mask */
|
|
|
|
/* Mask out the bits not covered by the mask */
|
|
|
|
*mask = (int)num;
|
|
|
|
for (i = 0; num >= 8; i++, num -= 8)
|
|
|
|
; /* skip octets entirely covered by the mask */
|
|
|
|
/* Mask out the first masked octet */
|
|
|
|
eth->addr[i] &= (0xFF << (8 - num));
|
|
|
|
i++;
|
|
|
|
/* Mask out completely-masked-out octets */
|
|
|
|
for (; i < 6; i++)
|
|
|
|
eth->addr[i] = 0;
|
|
|
|
return TRUE;
|
2010-04-27 22:46:39 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
if (*cp == '\0') {
|
|
|
|
/* We're at the end of the address, and there's no mask. */
|
|
|
|
if (i == 2) {
|
|
|
|
/* We got 3 bytes, so this is a manufacturer ID. */
|
|
|
|
if (!manuf_file) {
|
|
|
|
/* Manufacturer IDs are only allowed in the "manuf"
|
|
|
|
files. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Indicate that this is a manufacturer ID (0 is not allowed
|
|
|
|
as a mask). */
|
|
|
|
*mask = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 5) {
|
|
|
|
/* We got 6 bytes, so this is a MAC address.
|
|
|
|
If we're reading one of the "manuf" files, indicate that
|
|
|
|
this is a MAC address (48 is not allowed as a mask). */
|
|
|
|
if (manuf_file)
|
|
|
|
*mask = 48;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We didn't get 3 or 6 bytes, and there's no mask; this is
|
|
|
|
illegal. */
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
if (sep == '\0') {
|
|
|
|
/* We don't know the separator used in this number; it can either
|
|
|
|
be ':', '-', or '.'. */
|
|
|
|
if (*cp != ':' && *cp != '-' && *cp != '.')
|
|
|
|
return FALSE;
|
|
|
|
sep = *cp; /* subsequent separators must be the same */
|
|
|
|
} else {
|
|
|
|
/* It has to be the same as the first separator */
|
|
|
|
if (*cp != sep)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return TRUE;
|
2002-09-09 19:38:11 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static int
|
|
|
|
parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
|
2013-08-11 00:50:51 +00:00
|
|
|
const gboolean manuf_file)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* See the ethers(4) or ethers(5) man page for ethers file format
|
|
|
|
* (not available on all systems).
|
|
|
|
* We allow both ethernet address separators (':' and '-'),
|
|
|
|
* as well as Wireshark's '.' separator.
|
|
|
|
*/
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar *cp;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strchr(line, '#')))
|
|
|
|
*cp = '\0';
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(line, " \t")) == NULL)
|
|
|
|
return -1;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!parse_ether_address(cp, eth, mask, manuf_file))
|
|
|
|
return -1;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(NULL, " \t")) == NULL)
|
|
|
|
return -1;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_strlcpy(eth->name, cp, MAXNAMELEN);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return 0;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* parse_ether_line */
|
|
|
|
|
|
|
|
static FILE *eth_p = NULL;
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
set_ethent(char *path)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if (eth_p)
|
|
|
|
rewind(eth_p);
|
|
|
|
else
|
|
|
|
eth_p = ws_fopen(path, "r");
|
1998-09-25 23:24:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
end_ethent(void)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if (eth_p) {
|
|
|
|
fclose(eth_p);
|
|
|
|
eth_p = NULL;
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static ether_t *
|
|
|
|
get_ethent(unsigned int *mask, const gboolean manuf_file)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
static ether_t eth;
|
|
|
|
static int size = 0;
|
|
|
|
static char *buf = NULL;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (eth_p == NULL)
|
|
|
|
return NULL;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (fgetline(&buf, &size, eth_p) >= 0) {
|
|
|
|
if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
|
|
|
|
return ð
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
}
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return NULL;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* get_ethent */
|
|
|
|
|
2013-08-02 15:27:19 +00:00
|
|
|
#if 0
|
2010-04-29 18:17:29 +00:00
|
|
|
static ether_t *
|
|
|
|
get_ethbyname(const gchar *name)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
ether_t *eth;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ethent(g_pethers_path);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
|
|
|
|
;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (eth == NULL) {
|
|
|
|
end_ethent();
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ethent(g_ethers_path);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
|
|
|
|
;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
end_ethent();
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return eth;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* get_ethbyname */
|
2013-08-02 15:27:19 +00:00
|
|
|
#endif
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static ether_t *
|
|
|
|
get_ethbyaddr(const guint8 *addr)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
ether_t *eth;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ethent(g_pethers_path);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
|
|
|
|
;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (eth == NULL) {
|
|
|
|
end_ethent();
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ethent(g_ethers_path);
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
|
|
|
|
;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
end_ethent();
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return eth;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* get_ethbyaddr */
|
|
|
|
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2014-05-07 01:45:16 +00:00
|
|
|
guint8 *wka_key;
|
|
|
|
int *manuf_key;
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* XXX - can we use Standard Annotation Language annotations to
|
|
|
|
* note that mask, as returned by parse_ethe)r_address() (and thus
|
|
|
|
* by the routines that call it, and thus passed to us) cannot be > 48,
|
|
|
|
* or is SAL too weak to express that?
|
|
|
|
*/
|
|
|
|
if (mask >= 48) {
|
|
|
|
/* This is a well-known MAC address; just add this to the Ethernet
|
|
|
|
hash table */
|
|
|
|
add_eth_name(addr, name);
|
|
|
|
return;
|
|
|
|
}
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (mask == 0) {
|
|
|
|
/* This is a manufacturer ID; add it to the manufacturer ID hash table */
|
|
|
|
|
|
|
|
/* manuf needs only the 3 most significant octets of the ethernet address */
|
|
|
|
manuf_key = (int *)g_new(int, 1);
|
2014-08-09 13:33:23 +00:00
|
|
|
*manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
g_hash_table_insert(manuf_hashtable, manuf_key, g_strdup(name));
|
|
|
|
return;
|
|
|
|
} /* mask == 0 */
|
|
|
|
|
|
|
|
/* This is a range of well-known addresses; add it to the appropriate
|
|
|
|
well-known-address table, creating that table if necessary. */
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
wka_key = (guint8 *)g_malloc(6);
|
|
|
|
memcpy(wka_key, addr, 6);
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
|
2002-09-09 19:38:11 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
} /* add_manuf_name */
|
|
|
|
|
2013-12-20 06:30:13 +00:00
|
|
|
static gchar *
|
2010-04-29 18:17:29 +00:00
|
|
|
manuf_name_lookup(const guint8 *addr)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gint32 manuf_key = 0;
|
|
|
|
guint8 oct;
|
|
|
|
gchar *name;
|
2013-08-01 20:11:13 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* manuf needs only the 3 most significant octets of the ethernet address */
|
|
|
|
manuf_key = addr[0];
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[1];
|
|
|
|
manuf_key = manuf_key | oct;
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[2];
|
|
|
|
manuf_key = manuf_key | oct;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2001-06-07 22:07:02 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* first try to find a "perfect match" */
|
2013-08-01 20:11:13 +00:00
|
|
|
name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
|
|
|
|
if(name != NULL){
|
2013-08-11 00:50:51 +00:00
|
|
|
return name;
|
2007-08-14 23:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Mask out the broadcast/multicast flag but not the locally
|
|
|
|
* administered flag as localy administered means: not assigend
|
|
|
|
* by the IEEE but the local administrator instead.
|
|
|
|
* 0x01 multicast / broadcast bit
|
|
|
|
* 0x02 locally administered bit */
|
|
|
|
if((manuf_key & 0x00010000) != 0){
|
|
|
|
manuf_key &= 0x00FEFFFF;
|
|
|
|
name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
|
|
|
|
if(name != NULL){
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* manuf_name_lookup */
|
|
|
|
|
2013-08-01 20:11:13 +00:00
|
|
|
static gchar *
|
2010-04-29 18:17:29 +00:00
|
|
|
wka_name_lookup(const guint8 *addr, const unsigned int mask)
|
2002-09-09 19:38:11 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint8 masked_addr[6];
|
|
|
|
guint num;
|
|
|
|
gint i;
|
|
|
|
gchar *name;
|
2002-09-09 19:38:11 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(wka_hashtable == NULL){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Get the part of the address covered by the mask. */
|
|
|
|
for (i = 0, num = mask; num >= 8; i++, num -= 8)
|
|
|
|
masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
|
|
|
|
/* Mask out the first masked octet */
|
|
|
|
masked_addr[i] = addr[i] & (0xFF << (8 - num));
|
|
|
|
i++;
|
|
|
|
/* Zero out completely-masked-out octets */
|
|
|
|
for (; i < 6; i++)
|
|
|
|
masked_addr[i] = 0;
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
return name;
|
2002-09-09 19:38:11 +00:00
|
|
|
|
|
|
|
} /* wka_name_lookup */
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
static guint
|
|
|
|
eth_addr_hash(gconstpointer key)
|
|
|
|
{
|
|
|
|
return wmem_strong_hash((const guint8 *)key, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
eth_addr_cmp(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
return (memcmp(a, b, 6) == 0);
|
|
|
|
}
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
static void
|
2010-04-29 18:17:29 +00:00
|
|
|
initialize_ethers(void)
|
1999-11-20 05:35:15 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
ether_t *eth;
|
|
|
|
char *manuf_path;
|
|
|
|
guint mask;
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* hash table initialization */
|
2014-05-07 01:45:16 +00:00
|
|
|
wka_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, g_free, g_free);
|
2013-08-11 00:50:51 +00:00
|
|
|
manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
|
2014-05-07 01:45:16 +00:00
|
|
|
eth_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, NULL, g_free);
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Compute the pathname of the ethers file. */
|
|
|
|
if (g_ethers_path == NULL) {
|
|
|
|
g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
|
|
|
|
get_systemfile_dir(), ENAME_ETHERS);
|
|
|
|
}
|
2001-10-21 19:54:49 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Set g_pethers_path here, but don't actually do anything
|
|
|
|
* with it. It's used in get_ethbyname() and get_ethbyaddr()
|
|
|
|
*/
|
|
|
|
if (g_pethers_path == NULL)
|
|
|
|
g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Compute the pathname of the manuf file */
|
|
|
|
manuf_path = get_datafile_path(ENAME_MANUF);
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Read it and initialize the hash table */
|
|
|
|
set_ethent(manuf_path);
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while ((eth = get_ethent(&mask, TRUE))) {
|
|
|
|
add_manuf_name(eth->addr, mask, eth->name);
|
|
|
|
}
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
end_ethent();
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_free(manuf_path);
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
|
1999-11-20 05:35:15 +00:00
|
|
|
} /* initialize_ethers */
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
/* this is only needed when shuting down application (if at all) */
|
|
|
|
static void
|
|
|
|
eth_name_lookup_cleanup(void)
|
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(manuf_hashtable) {
|
|
|
|
g_hash_table_destroy(manuf_hashtable);
|
|
|
|
manuf_hashtable = NULL;
|
|
|
|
}
|
|
|
|
if(wka_hashtable) {
|
|
|
|
g_hash_table_destroy(wka_hashtable);
|
|
|
|
wka_hashtable = NULL;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(eth_hashtable) {
|
|
|
|
g_hash_table_destroy(eth_hashtable);
|
|
|
|
eth_hashtable = NULL;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-01 15:11:02 +00:00
|
|
|
/* Resolve ethernet address */
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashether_t *
|
2010-07-01 15:11:02 +00:00
|
|
|
eth_addr_resolve(hashether_t *tp) {
|
2013-08-11 00:50:51 +00:00
|
|
|
ether_t *eth;
|
|
|
|
const guint8 *addr = tp->addr;
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ( (eth = get_ethbyaddr(addr)) != NULL) {
|
|
|
|
g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_NAME;
|
2010-04-27 22:46:39 +00:00
|
|
|
return tp;
|
2013-08-11 00:50:51 +00:00
|
|
|
} else {
|
|
|
|
guint mask;
|
|
|
|
gchar *name;
|
2014-12-20 02:42:14 +00:00
|
|
|
address ether_addr;
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
/* Unknown name. Try looking for it in the well-known-address
|
|
|
|
tables for well-known address ranges smaller than 2^24. */
|
|
|
|
mask = 7;
|
|
|
|
for (;;) {
|
|
|
|
/* Only the topmost 5 bytes participate fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
|
|
|
|
name, addr[5] & (0xFF >> mask));
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
if (mask == 0)
|
|
|
|
break;
|
|
|
|
mask--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 7;
|
|
|
|
for (;;) {
|
|
|
|
/* Only the topmost 4 bytes participate fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
|
|
|
|
name, addr[4] & (0xFF >> mask), addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
if (mask == 0)
|
|
|
|
break;
|
|
|
|
mask--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 7;
|
|
|
|
for (;;) {
|
|
|
|
/* Only the topmost 3 bytes participate fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
|
|
|
|
name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
if (mask == 0)
|
|
|
|
break;
|
|
|
|
mask--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try looking in the manufacturer table. */
|
|
|
|
if ((name = manuf_name_lookup(addr)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
|
|
|
|
name, addr[3], addr[4], addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try looking for it in the well-known-address
|
|
|
|
tables for well-known address ranges larger than 2^24. */
|
|
|
|
mask = 7;
|
|
|
|
for (;;) {
|
|
|
|
/* Only the topmost 2 bytes participate fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
|
|
|
|
name, addr[2] & (0xFF >> mask), addr[3], addr[4],
|
|
|
|
addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
if (mask == 0)
|
|
|
|
break;
|
|
|
|
mask--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 7;
|
|
|
|
for (;;) {
|
|
|
|
/* Only the topmost byte participates fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
|
|
|
|
name, addr[1] & (0xFF >> mask), addr[2], addr[3],
|
|
|
|
addr[4], addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
if (mask == 0)
|
|
|
|
break;
|
|
|
|
mask--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mask = 7; mask > 0; mask--) {
|
|
|
|
/* Not even the topmost byte participates fully */
|
|
|
|
if ((name = wka_name_lookup(addr, mask)) != NULL) {
|
|
|
|
g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
name, addr[0] & (0xFF >> mask), addr[1], addr[2],
|
|
|
|
addr[3], addr[4], addr[5]);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No match whatsoever. */
|
2014-12-20 02:42:14 +00:00
|
|
|
SET_ADDRESS(ðer_addr, AT_ETHER, 6, addr);
|
2015-01-05 16:12:08 +00:00
|
|
|
address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
|
2010-07-01 15:11:02 +00:00
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
|
2010-04-27 22:46:39 +00:00
|
|
|
return tp;
|
2002-09-09 19:38:11 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
g_assert_not_reached();
|
2010-07-01 15:11:02 +00:00
|
|
|
} /* eth_addr_resolve */
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-15 02:45:58 +00:00
|
|
|
static hashether_t *
|
|
|
|
eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
|
|
|
|
{
|
|
|
|
hashether_t *tp;
|
2013-12-20 10:08:35 +00:00
|
|
|
char *endp;
|
2013-08-15 02:45:58 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = g_new(hashether_t, 1);
|
|
|
|
memcpy(tp->addr, addr, sizeof(tp->addr));
|
|
|
|
tp->status = HASHETHER_STATUS_UNRESOLVED;
|
2013-12-20 10:08:35 +00:00
|
|
|
/* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
|
|
|
|
endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
|
|
|
|
*endp = '\0';
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->resolved_name[0] = '\0';
|
|
|
|
|
|
|
|
if (resolve)
|
|
|
|
eth_addr_resolve(tp);
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
g_hash_table_insert(eth_hashtable, tp->addr, tp);
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
return tp;
|
2010-07-01 15:11:02 +00:00
|
|
|
} /* eth_hash_new_entry */
|
|
|
|
|
|
|
|
static hashether_t *
|
|
|
|
add_eth_name(const guint8 *addr, const gchar *name)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashether_t *tp;
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
if( tp == NULL ){
|
|
|
|
tp = eth_hash_new_entry(addr, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
|
|
|
|
tp->status = HASHETHER_STATUS_RESOLVED_NAME;
|
|
|
|
new_resolved_objects = TRUE;
|
|
|
|
|
|
|
|
return tp;
|
2010-07-01 15:11:02 +00:00
|
|
|
} /* add_eth_name */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2010-07-01 15:11:02 +00:00
|
|
|
static hashether_t *
|
2013-08-15 02:45:58 +00:00
|
|
|
eth_name_lookup(const guint8 *addr, const gboolean resolve)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashether_t *tp;
|
|
|
|
|
2014-05-07 01:45:16 +00:00
|
|
|
tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
|
2013-08-11 00:50:51 +00:00
|
|
|
if( tp == NULL ) {
|
|
|
|
tp = eth_hash_new_entry(addr, resolve);
|
|
|
|
} else {
|
|
|
|
if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)){
|
|
|
|
eth_addr_resolve(tp); /* Found but needs to be resolved */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tp;
|
2013-08-02 15:05:00 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
} /* eth_name_lookup */
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static guint8 *
|
2013-08-02 15:27:19 +00:00
|
|
|
eth_addr_lookup(const gchar *name _U_)
|
1999-11-20 05:35:15 +00:00
|
|
|
{
|
2013-08-02 15:05:00 +00:00
|
|
|
#if 0
|
2013-10-23 12:02:15 +00:00
|
|
|
/* XXX Do we need reverse lookup??? */
|
2013-08-11 00:50:51 +00:00
|
|
|
ether_t *eth;
|
|
|
|
hashether_t *tp;
|
|
|
|
hashether_t **table = eth_table;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* to be optimized (hash table from name to addr) */
|
|
|
|
for (i = 0; i < HASHETHSIZE; i++) {
|
|
|
|
tp = table[i];
|
|
|
|
while (tp) {
|
|
|
|
if (strcmp(tp->resolved_name, name) == 0)
|
|
|
|
return tp->addr;
|
|
|
|
tp = tp->next;
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* not in hash table : performs a file lookup */
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((eth = get_ethbyname(name)) == NULL)
|
|
|
|
return NULL;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* add new entry in hash table */
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = add_eth_name(eth->addr, name);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return tp->addr;
|
2013-08-02 15:05:00 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
return NULL;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* eth_addr_lookup */
|
|
|
|
|
|
|
|
|
|
|
|
/* IPXNETS */
|
2010-04-29 18:17:29 +00:00
|
|
|
static int
|
|
|
|
parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* We allow three address separators (':', '-', and '.'),
|
|
|
|
* as well as no separators
|
|
|
|
*/
|
|
|
|
|
|
|
|
gchar *cp;
|
|
|
|
guint32 a, a0, a1, a2, a3;
|
|
|
|
gboolean found_single_number = FALSE;
|
|
|
|
|
|
|
|
if ((cp = strchr(line, '#')))
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
if ((cp = strtok(line, " \t\n")) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
|
|
|
|
* fill a and found_single_number is TRUE,
|
|
|
|
* or return -1
|
|
|
|
*/
|
|
|
|
if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
|
|
|
|
if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
|
|
|
|
if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
|
|
|
|
if (sscanf(cp, "%x", &a) == 1) {
|
|
|
|
found_single_number = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-04-27 22:46:39 +00:00
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(NULL, " \t\n")) == NULL)
|
|
|
|
return -1;
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (found_single_number) {
|
|
|
|
ipxnet->addr = a;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return 0;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* parse_ipxnets_line */
|
|
|
|
|
|
|
|
static FILE *ipxnet_p = NULL;
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
set_ipxnetent(char *path)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if (ipxnet_p)
|
|
|
|
rewind(ipxnet_p);
|
|
|
|
else
|
|
|
|
ipxnet_p = ws_fopen(path, "r");
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
end_ipxnetent(void)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
if (ipxnet_p) {
|
|
|
|
fclose(ipxnet_p);
|
|
|
|
ipxnet_p = NULL;
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static ipxnet_t *
|
|
|
|
get_ipxnetent(void)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
static ipxnet_t ipxnet;
|
|
|
|
static int size = 0;
|
|
|
|
static char *buf = NULL;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (ipxnet_p == NULL)
|
|
|
|
return NULL;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (fgetline(&buf, &size, ipxnet_p) >= 0) {
|
|
|
|
if (parse_ipxnets_line(buf, &ipxnet) == 0) {
|
|
|
|
return &ipxnet;
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return NULL;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* get_ipxnetent */
|
|
|
|
|
2013-08-20 06:35:45 +00:00
|
|
|
/* Unused ??? */
|
|
|
|
#if 0
|
2010-04-29 18:17:29 +00:00
|
|
|
static ipxnet_t *
|
|
|
|
get_ipxnetbyname(const gchar *name)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
ipxnet_t *ipxnet;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ipxnetent(g_ipxnets_path);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
|
|
|
|
;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (ipxnet == NULL) {
|
|
|
|
end_ipxnetent();
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ipxnetent(g_pipxnets_path);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
|
|
|
|
;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
end_ipxnetent();
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return ipxnet;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* get_ipxnetbyname */
|
2013-08-20 06:35:45 +00:00
|
|
|
#endif
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static ipxnet_t *
|
|
|
|
get_ipxnetbyaddr(guint32 addr)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
ipxnet_t *ipxnet;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ipxnetent(g_ipxnets_path);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (ipxnet == NULL) {
|
|
|
|
end_ipxnetent();
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
set_ipxnetent(g_pipxnets_path);
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
|
|
|
|
;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
end_ipxnetent();
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return ipxnet;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* get_ipxnetbyaddr */
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
initialize_ipxnets(void)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Compute the pathname of the ipxnets file.
|
|
|
|
*
|
|
|
|
* XXX - is there a notion of an "ipxnets file" in any flavor of
|
|
|
|
* UNIX, or with any add-on Netware package for UNIX? If not,
|
|
|
|
* should the UNIX version of the ipxnets file be in the datafile
|
|
|
|
* directory as well?
|
|
|
|
*/
|
|
|
|
if (g_ipxnets_path == NULL) {
|
|
|
|
g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
|
|
|
|
get_systemfile_dir(), ENAME_IPXNETS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set g_pipxnets_path here, but don't actually do anything
|
|
|
|
* with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
|
|
|
|
*/
|
|
|
|
if (g_pipxnets_path == NULL)
|
|
|
|
g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* initialize_ipxnets */
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
static void
|
|
|
|
ipx_name_lookup_cleanup(void)
|
|
|
|
{
|
2013-08-20 05:06:26 +00:00
|
|
|
if(ipxnet_hash_table){
|
|
|
|
g_hash_table_destroy(ipxnet_hash_table);
|
2013-10-23 12:02:15 +00:00
|
|
|
ipxnet_hash_table = NULL;
|
2013-08-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 06:35:45 +00:00
|
|
|
#if 0
|
2010-04-29 18:17:29 +00:00
|
|
|
static hashipxnet_t *
|
|
|
|
add_ipxnet_name(guint addr, const gchar *name)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipxnet_t *tp;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
|
|
|
|
if(tp){
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
}else{
|
|
|
|
int *key;
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
key = (int *)g_new(int, 1);
|
|
|
|
*key = addr;
|
|
|
|
tp = g_new(hashipxnet_t,1);
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
g_hash_table_insert(ipxnet_hash_table, key, tp);
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->addr = addr;
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
tp->next = NULL;
|
|
|
|
new_resolved_objects = TRUE;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return tp;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* add_ipxnet_name */
|
2013-08-20 06:35:45 +00:00
|
|
|
#endif
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static gchar *
|
2015-01-08 17:13:18 +00:00
|
|
|
ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipxnet_t *tp;
|
|
|
|
ipxnet_t *ipxnet;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
|
|
|
|
if(tp == NULL){
|
|
|
|
int *key;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
key = (int *)g_new(int, 1);
|
|
|
|
*key = addr;
|
|
|
|
tp = g_new(hashipxnet_t, 1);
|
|
|
|
g_hash_table_insert(ipxnet_hash_table, key, tp);
|
|
|
|
}else{
|
2015-01-08 17:13:18 +00:00
|
|
|
return wmem_strdup(allocator, tp->name);
|
1999-11-20 05:35:15 +00:00
|
|
|
}
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* fill in a new entry */
|
1999-11-20 05:35:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->addr = addr;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
|
|
|
|
/* unknown name */
|
|
|
|
g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
} else {
|
|
|
|
g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2015-01-08 17:13:18 +00:00
|
|
|
return wmem_strdup(allocator, tp->name);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* ipxnet_name_lookup */
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static guint
|
2013-08-20 05:27:48 +00:00
|
|
|
ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-10-23 12:02:15 +00:00
|
|
|
*success = FALSE;
|
|
|
|
return 0;
|
2013-08-20 05:06:26 +00:00
|
|
|
#if 0
|
2013-10-23 12:02:15 +00:00
|
|
|
/* XXX Do we need reverse lookup??? */
|
2013-08-11 00:50:51 +00:00
|
|
|
ipxnet_t *ipxnet;
|
|
|
|
hashipxnet_t *tp;
|
|
|
|
hashipxnet_t **table = ipxnet_table;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* to be optimized (hash table from name to addr) */
|
|
|
|
for (i = 0; i < HASHIPXNETSIZE; i++) {
|
|
|
|
tp = table[i];
|
|
|
|
while (tp) {
|
|
|
|
if (strcmp(tp->name, name) == 0) {
|
|
|
|
*success = TRUE;
|
|
|
|
return tp->addr;
|
|
|
|
}
|
|
|
|
tp = tp->next;
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* not in hash table : performs a file lookup */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
|
|
|
|
*success = FALSE;
|
|
|
|
return 0;
|
|
|
|
}
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* add new entry in hash table */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = add_ipxnet_name(ipxnet->addr, name);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*success = TRUE;
|
|
|
|
return tp->addr;
|
2013-08-20 05:06:26 +00:00
|
|
|
#endif
|
1999-11-21 16:32:23 +00:00
|
|
|
} /* ipxnet_addr_lookup */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2012-11-29 00:32:39 +00:00
|
|
|
static gboolean
|
2014-09-26 17:41:43 +00:00
|
|
|
read_hosts_file (const char *hostspath, gboolean store_entries)
|
2004-11-17 08:34:36 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
FILE *hf;
|
|
|
|
char *line = NULL;
|
|
|
|
int size = 0;
|
|
|
|
gchar *cp;
|
|
|
|
guint32 host_addr[4]; /* IPv4 or IPv6 */
|
|
|
|
struct e_in6_addr ip6_addr;
|
2014-09-26 17:41:43 +00:00
|
|
|
gboolean is_ipv6, entry_found = FALSE;
|
2013-08-11 00:50:51 +00:00
|
|
|
int ret;
|
2005-01-07 10:20:17 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* See the hosts(4) or hosts(5) man page for hosts file format
|
|
|
|
* (not available on all systems).
|
|
|
|
*/
|
|
|
|
if ((hf = ws_fopen(hostspath, "r")) == NULL)
|
|
|
|
return FALSE;
|
2004-11-17 08:34:36 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (fgetline(&line, &size, hf) >= 0) {
|
|
|
|
if ((cp = strchr(line, '#')))
|
|
|
|
*cp = '\0';
|
2004-11-17 08:34:36 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(line, " \t")) == NULL)
|
|
|
|
continue; /* no tokens in the line */
|
2004-11-17 08:34:36 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
ret = inet_pton(AF_INET6, cp, &host_addr);
|
|
|
|
if (ret < 0)
|
|
|
|
continue; /* error parsing */
|
|
|
|
if (ret > 0) {
|
|
|
|
/* Valid IPv6 */
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
} else {
|
|
|
|
/* Not valid IPv6 - valid IPv4? */
|
2013-12-21 16:16:56 +00:00
|
|
|
if (!str_to_ip(cp, &host_addr))
|
2013-08-11 00:50:51 +00:00
|
|
|
continue; /* no */
|
|
|
|
is_ipv6 = FALSE;
|
|
|
|
}
|
2004-11-17 08:34:36 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(NULL, " \t")) == NULL)
|
|
|
|
continue; /* no host name */
|
|
|
|
|
2014-09-26 17:41:43 +00:00
|
|
|
entry_found = TRUE;
|
|
|
|
if (store_entries) {
|
2013-08-11 00:50:51 +00:00
|
|
|
if (is_ipv6) {
|
|
|
|
memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
|
|
|
|
add_ipv6_name(&ip6_addr, cp);
|
|
|
|
} else
|
|
|
|
add_ipv4_name(host_addr[0], cp);
|
2014-09-26 17:41:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the aliases, too, if there are any.
|
|
|
|
* XXX - host_lookup() only returns the first entry.
|
|
|
|
*/
|
|
|
|
while ((cp = strtok(NULL, " \t")) != NULL) {
|
|
|
|
if (is_ipv6) {
|
|
|
|
memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
|
|
|
|
add_ipv6_name(&ip6_addr, cp);
|
|
|
|
} else
|
|
|
|
add_ipv4_name(host_addr[0], cp);
|
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(line);
|
2004-11-17 08:34:36 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
fclose(hf);
|
2014-09-26 17:41:43 +00:00
|
|
|
return entry_found ? TRUE : FALSE;
|
2004-11-17 08:34:36 +00:00
|
|
|
} /* read_hosts_file */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2013-08-13 01:41:51 +00:00
|
|
|
gboolean
|
2012-11-29 00:32:39 +00:00
|
|
|
add_hosts_file (const char *hosts_file)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean found = FALSE;
|
|
|
|
guint i;
|
2012-11-29 00:32:39 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!hosts_file)
|
|
|
|
return FALSE;
|
2012-11-29 00:32:39 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!extra_hosts_files)
|
|
|
|
extra_hosts_files = g_ptr_array_new();
|
2012-11-29 00:32:39 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
for (i = 0; i < extra_hosts_files->len; i++) {
|
|
|
|
if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
|
|
|
|
found = TRUE;
|
|
|
|
}
|
2012-11-29 00:32:39 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!found) {
|
|
|
|
g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
|
2014-09-26 17:41:43 +00:00
|
|
|
return read_hosts_file (hosts_file, FALSE);
|
2012-11-29 00:32:39 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
return TRUE;
|
2012-11-29 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 01:41:51 +00:00
|
|
|
gboolean
|
2010-04-27 22:46:39 +00:00
|
|
|
add_ip_name_from_string (const char *addr, const char *name)
|
2010-04-27 06:51:16 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint32 host_addr[4]; /* IPv4 */
|
|
|
|
struct e_in6_addr ip6_addr; /* IPv6 */
|
|
|
|
gboolean is_ipv6;
|
|
|
|
int ret;
|
2013-10-23 12:02:15 +00:00
|
|
|
resolved_ipv4_t *resolved_ipv4_entry;
|
|
|
|
resolved_ipv6_t *resolved_ipv6_entry;
|
2010-04-27 06:51:16 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
ret = inet_pton(AF_INET6, addr, &ip6_addr);
|
|
|
|
if (ret < 0)
|
|
|
|
/* Error parsing address */
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
/* Valid IPv6 */
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
} else {
|
|
|
|
/* Not valid IPv6 - valid IPv4? */
|
2013-12-21 16:16:56 +00:00
|
|
|
if (!str_to_ip(addr, &host_addr))
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE; /* no */
|
|
|
|
is_ipv6 = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_ipv6) {
|
2013-10-23 12:02:15 +00:00
|
|
|
resolved_ipv6_entry = g_new(resolved_ipv6_t, 1);
|
|
|
|
memcpy(&(resolved_ipv6_entry->ip6_addr), &ip6_addr, 16);
|
|
|
|
g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
|
2013-10-23 14:04:19 +00:00
|
|
|
manually_resolved_ipv6_list = g_slist_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
|
2013-08-11 00:50:51 +00:00
|
|
|
} else {
|
2013-10-23 12:02:15 +00:00
|
|
|
resolved_ipv4_entry = g_new(resolved_ipv4_t, 1);
|
|
|
|
resolved_ipv4_entry->host_addr = host_addr[0];
|
|
|
|
g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
|
2013-10-23 14:04:19 +00:00
|
|
|
manually_resolved_ipv4_list = g_slist_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2010-04-27 06:51:16 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return TRUE;
|
2010-04-27 22:46:39 +00:00
|
|
|
} /* add_ip_name_from_string */
|
2010-04-27 06:51:16 +00:00
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
/*
|
|
|
|
* Add the resolved addresses that are in use to the list used to create the NRB
|
|
|
|
*/
|
2013-09-22 19:40:02 +00:00
|
|
|
static void
|
|
|
|
ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
|
|
|
|
{
|
2013-09-22 20:01:32 +00:00
|
|
|
addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
|
|
|
|
hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
|
2013-09-22 19:40:02 +00:00
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
if((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
|
2013-09-22 20:01:32 +00:00
|
|
|
lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
|
|
|
|
}
|
2013-09-22 19:40:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
/*
|
|
|
|
* Add the resolved addresses that are in use to the list used to create the NRB
|
|
|
|
*/
|
|
|
|
|
2013-09-22 19:40:02 +00:00
|
|
|
static void
|
|
|
|
ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
|
|
|
|
{
|
2013-09-22 20:01:32 +00:00
|
|
|
addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
|
2013-09-22 19:40:02 +00:00
|
|
|
hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
|
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
if((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
|
2013-09-22 20:01:32 +00:00
|
|
|
lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
|
|
|
|
}
|
2013-09-22 19:40:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
addrinfo_lists_t *
|
2011-03-24 22:47:57 +00:00
|
|
|
get_addrinfo_list(void) {
|
2013-09-22 19:40:02 +00:00
|
|
|
|
2013-09-22 20:01:32 +00:00
|
|
|
if(ipv4_hash_table){
|
2013-09-22 20:18:25 +00:00
|
|
|
g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
|
2013-09-22 20:01:32 +00:00
|
|
|
}
|
2013-09-22 19:40:02 +00:00
|
|
|
|
2013-09-22 20:18:25 +00:00
|
|
|
if(ipv6_hash_table){
|
|
|
|
g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
|
2013-09-22 20:01:32 +00:00
|
|
|
}
|
2013-09-22 19:40:02 +00:00
|
|
|
|
|
|
|
return &addrinfo_lists;
|
2011-03-24 22:47:57 +00:00
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
|
|
|
|
/* Read in a list of subnet definition - name pairs.
|
|
|
|
* <line> = <comment> | <entry> | <whitespace>
|
|
|
|
* <comment> = <whitespace>#<any>
|
2008-05-22 15:46:27 +00:00
|
|
|
* <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
|
2008-01-21 21:44:34 +00:00
|
|
|
* <subnet_definition> = <ipv4_address> / <subnet_mask_length>
|
|
|
|
* <ipv4_address> is a full address; it will be masked to get the subnet-ID.
|
|
|
|
* <subnet_mask_length> is a decimal 1-31
|
|
|
|
* <subnet_name> is a string containing no whitespace.
|
|
|
|
* <whitespace> = (space | tab)+
|
|
|
|
* Any malformed entries are ignored.
|
|
|
|
* Any trailing data after the subnet_name is ignored.
|
|
|
|
*
|
|
|
|
* XXX Support IPv6
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
read_subnets_file (const char *subnetspath)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
FILE *hf;
|
|
|
|
char *line = NULL;
|
|
|
|
int size = 0;
|
|
|
|
gchar *cp, *cp2;
|
|
|
|
guint32 host_addr; /* IPv4 ONLY */
|
|
|
|
int mask_length;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((hf = ws_fopen(subnetspath, "r")) == NULL)
|
|
|
|
return FALSE;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
while (fgetline(&line, &size, hf) >= 0) {
|
|
|
|
if ((cp = strchr(line, '#')))
|
|
|
|
*cp = '\0';
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(line, " \t")) == NULL)
|
|
|
|
continue; /* no tokens in the line */
|
2008-01-21 21:44:34 +00:00
|
|
|
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Expected format is <IP4 address>/<subnet length> */
|
|
|
|
cp2 = strchr(cp, '/');
|
|
|
|
if(NULL == cp2) {
|
|
|
|
/* No length */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp2 = '\0'; /* Cut token */
|
|
|
|
++cp2 ;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Check if this is a valid IPv4 address */
|
2013-12-21 16:16:56 +00:00
|
|
|
if (!str_to_ip(cp, &host_addr)) {
|
2013-08-11 00:50:51 +00:00
|
|
|
continue; /* no */
|
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
mask_length = atoi(cp2);
|
|
|
|
if(0 >= mask_length || mask_length > 31) {
|
|
|
|
continue; /* invalid mask length */
|
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cp = strtok(NULL, " \t")) == NULL)
|
|
|
|
continue; /* no subnet name */
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_entry_set(host_addr, (guint32)mask_length, cp);
|
|
|
|
}
|
|
|
|
g_free(line);
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
fclose(hf);
|
|
|
|
return TRUE;
|
2008-01-21 21:44:34 +00:00
|
|
|
} /* read_subnets_file */
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static subnet_entry_t
|
|
|
|
subnet_lookup(const guint32 addr)
|
2008-01-21 21:44:34 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_entry_t subnet_entry;
|
|
|
|
guint32 i;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Search mask lengths linearly, longest first */
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
i = SUBNETLENGTHSIZE;
|
|
|
|
while(have_subnet_entry && i > 0) {
|
|
|
|
guint32 masked_addr;
|
|
|
|
subnet_length_entry_t* length_entry;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Note that we run from 31 (length 32) to 0 (length 1) */
|
|
|
|
--i;
|
|
|
|
g_assert(i < SUBNETLENGTHSIZE);
|
2008-01-21 21:44:34 +00:00
|
|
|
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
length_entry = &subnet_length_entries[i];
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(NULL != length_entry->subnet_addresses) {
|
2013-08-11 19:02:26 +00:00
|
|
|
sub_net_hashipv4_t * tp;
|
2013-08-11 00:50:51 +00:00
|
|
|
guint32 hash_idx;
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
masked_addr = addr & length_entry->mask;
|
|
|
|
hash_idx = HASH_IPV4_ADDRESS(masked_addr);
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = length_entry->subnet_addresses[hash_idx];
|
|
|
|
while(tp != NULL && tp->addr != masked_addr) {
|
|
|
|
tp = tp->next;
|
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(NULL != tp) {
|
|
|
|
subnet_entry.mask = length_entry->mask;
|
|
|
|
subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
|
|
|
|
subnet_entry.name = tp->name;
|
|
|
|
return subnet_entry;
|
|
|
|
}
|
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
}
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_entry.mask = 0;
|
|
|
|
subnet_entry.mask_length = 0;
|
|
|
|
subnet_entry.name = NULL;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return subnet_entry;
|
2008-01-21 21:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a subnet-definition - name pair to the set.
|
|
|
|
* The definition is taken by masking the address passed in with the mask of the
|
|
|
|
* given length.
|
|
|
|
*/
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
|
2008-01-21 21:44:34 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_length_entry_t* entry;
|
2013-08-11 19:02:26 +00:00
|
|
|
sub_net_hashipv4_t * tp;
|
2013-08-11 00:50:51 +00:00
|
|
|
gsize hash_idx;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_assert(mask_length > 0 && mask_length <= 32);
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
entry = &subnet_length_entries[mask_length - 1];
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_addr &= entry->mask;
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(NULL == entry->subnet_addresses) {
|
2015-01-16 17:35:14 +00:00
|
|
|
entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
|
|
|
|
if(tp->addr == subnet_addr) {
|
|
|
|
return; /* XXX provide warning that an address was repeated? */
|
|
|
|
} else {
|
2015-01-16 17:35:14 +00:00
|
|
|
sub_net_hashipv4_t * new_tp = g_new(sub_net_hashipv4_t, 1);
|
2013-08-11 00:50:51 +00:00
|
|
|
tp->next = new_tp;
|
|
|
|
tp = new_tp;
|
|
|
|
}
|
2008-01-21 21:44:34 +00:00
|
|
|
} else {
|
2015-01-16 17:35:14 +00:00
|
|
|
tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tp->next = NULL;
|
|
|
|
tp->addr = subnet_addr;
|
2013-10-23 12:02:15 +00:00
|
|
|
/* Clear DUMMY_ADDRESS_ENTRY */
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags & 0xfe; /*Never used again...*/
|
2013-08-11 00:50:51 +00:00
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
|
|
|
|
have_subnet_entry = TRUE;
|
2008-01-21 21:44:34 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
static void
|
|
|
|
subnet_name_lookup_init(void)
|
2008-01-21 21:44:34 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar* subnetspath;
|
|
|
|
guint32 i;
|
|
|
|
|
|
|
|
for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
|
|
|
|
guint32 length = i + 1;
|
|
|
|
|
|
|
|
subnet_length_entries[i].subnet_addresses = NULL;
|
|
|
|
subnet_length_entries[i].mask_length = length;
|
|
|
|
subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
|
|
|
|
}
|
|
|
|
|
|
|
|
subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
|
|
|
|
if (!read_subnets_file(subnetspath) && errno != ENOENT) {
|
|
|
|
report_open_failure(subnetspath, errno, FALSE);
|
|
|
|
}
|
|
|
|
g_free(subnetspath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the global subnets file, if we have one.
|
|
|
|
*/
|
|
|
|
subnetspath = get_datafile_path(ENAME_SUBNETS);
|
|
|
|
if (!read_subnets_file(subnetspath) && errno != ENOENT) {
|
|
|
|
report_open_failure(subnetspath, errno, FALSE);
|
|
|
|
}
|
|
|
|
g_free(subnetspath);
|
2008-01-21 21:44:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:35:14 +00:00
|
|
|
static void
|
|
|
|
cleanup_subnet_entry(sub_net_hashipv4_t* entry)
|
|
|
|
{
|
|
|
|
if ((entry != NULL) && (entry->next != NULL)) {
|
|
|
|
cleanup_subnet_entry(entry->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(entry);
|
|
|
|
}
|
2011-03-24 22:47:57 +00:00
|
|
|
|
2002-08-28 20:41:00 +00:00
|
|
|
/*
|
1998-09-25 23:24:07 +00:00
|
|
|
* External Functions
|
|
|
|
*/
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-13 01:41:51 +00:00
|
|
|
void
|
2012-07-06 04:48:36 +00:00
|
|
|
addr_resolve_pref_init(module_t *nameres)
|
|
|
|
{
|
|
|
|
prefs_register_bool_preference(nameres, "mac_name",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Resolve MAC addresses",
|
|
|
|
"Resolve Ethernet MAC address to manufacturer names",
|
|
|
|
&gbl_resolv_flags.mac_name);
|
2012-07-06 04:48:36 +00:00
|
|
|
|
|
|
|
prefs_register_bool_preference(nameres, "transport_name",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Resolve transport names",
|
|
|
|
"Resolve TCP/UDP ports into service names",
|
|
|
|
&gbl_resolv_flags.transport_name);
|
2012-07-06 04:48:36 +00:00
|
|
|
|
2012-07-10 07:24:01 +00:00
|
|
|
prefs_register_bool_preference(nameres, "network_name",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Resolve network (IP) addresses",
|
|
|
|
"Resolve IPv4, IPv6, and IPX addresses into host names."
|
|
|
|
" The next set of check boxes determines how name resolution should be performed."
|
|
|
|
" If no other options are checked name resolution is made from Wireshark's host file,"
|
|
|
|
" capture file name resolution blocks and DNS packets in the capture.",
|
|
|
|
&gbl_resolv_flags.network_name);
|
2012-07-10 07:24:01 +00:00
|
|
|
|
2012-07-08 01:31:48 +00:00
|
|
|
prefs_register_bool_preference(nameres, "use_external_name_resolver",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Use an external network name resolver",
|
|
|
|
"Use your system's configured name resolver"
|
|
|
|
" (usually DNS) to resolve network names."
|
|
|
|
" Only applies when network name resolution"
|
|
|
|
" is enabled.",
|
|
|
|
&gbl_resolv_flags.use_external_net_name_resolver);
|
2012-07-08 01:31:48 +00:00
|
|
|
|
2012-07-06 04:48:36 +00:00
|
|
|
#if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
|
|
|
|
prefs_register_bool_preference(nameres, "concurrent_dns",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Enable concurrent DNS name resolution",
|
|
|
|
"Enable concurrent DNS name resolution. Only"
|
|
|
|
" applies when network name resolution is"
|
|
|
|
" enabled. You probably want to enable this.",
|
|
|
|
&gbl_resolv_flags.concurrent_dns);
|
2012-07-06 04:48:36 +00:00
|
|
|
|
2012-07-08 01:31:48 +00:00
|
|
|
prefs_register_uint_preference(nameres, "name_resolve_concurrency",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Maximum concurrent requests",
|
|
|
|
"The maximum number of DNS requests that may"
|
|
|
|
" be active at any time. A large value (many"
|
|
|
|
" thousands) might overload the network or make"
|
|
|
|
" your DNS server behave badly.",
|
|
|
|
10,
|
|
|
|
&name_resolve_concurrency);
|
2012-07-06 04:48:36 +00:00
|
|
|
#else
|
2012-07-08 01:31:48 +00:00
|
|
|
prefs_register_static_text_preference(nameres, "concurrent_dns",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Enable concurrent DNS name resolution: N/A",
|
|
|
|
"Support for concurrent DNS name resolution was not"
|
|
|
|
" compiled into this version of Wireshark");
|
2012-07-06 04:48:36 +00:00
|
|
|
#endif
|
2012-07-11 08:52:51 +00:00
|
|
|
|
|
|
|
prefs_register_bool_preference(nameres, "hosts_file_handling",
|
2013-08-11 00:50:51 +00:00
|
|
|
"Only use the profile \"hosts\" file",
|
|
|
|
"By default \"hosts\" files will be loaded from multiple sources."
|
|
|
|
" Checking this box only loads the \"hosts\" in the current profile.",
|
|
|
|
&gbl_resolv_flags.load_hosts_file_from_profile_only);
|
2012-07-11 08:52:51 +00:00
|
|
|
|
2012-07-06 04:48:36 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
|
|
|
gboolean
|
2012-10-02 21:13:59 +00:00
|
|
|
host_name_lookup_process(void) {
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_queue_msg_t *caqm;
|
|
|
|
struct timeval tv = { 0, 0 };
|
|
|
|
int nfds;
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
gboolean nro = new_resolved_objects;
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
new_resolved_objects = FALSE;
|
2009-06-24 19:10:50 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!async_dns_initialized)
|
|
|
|
/* c-ares not initialized. Bail out and cancel timers. */
|
2013-08-07 16:46:08 +00:00
|
|
|
return nro;
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
async_dns_queue_head = g_list_first(async_dns_queue_head);
|
|
|
|
|
|
|
|
while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
|
|
|
|
caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
|
|
|
|
async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
|
|
|
|
if (caqm->family == AF_INET) {
|
|
|
|
ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
|
|
|
|
c_ares_ghba_cb, caqm);
|
|
|
|
async_dns_in_flight++;
|
|
|
|
} else if (caqm->family == AF_INET6) {
|
|
|
|
ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
|
|
|
|
AF_INET6, c_ares_ghba_cb, caqm);
|
|
|
|
async_dns_in_flight++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
nfds = ares_fds(ghba_chan, &rfds, &wfds);
|
|
|
|
if (nfds > 0) {
|
|
|
|
if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
|
2014-06-30 23:46:58 +00:00
|
|
|
fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
|
2013-08-11 00:50:51 +00:00
|
|
|
return nro;
|
|
|
|
}
|
|
|
|
ares_process(ghba_chan, &rfds, &wfds);
|
2012-11-05 09:18:43 +00:00
|
|
|
}
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Any new entries? */
|
|
|
|
return nro;
|
2008-08-07 21:41:48 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 20:50:25 +00:00
|
|
|
static void
|
|
|
|
_host_name_lookup_cleanup(void) {
|
2013-08-11 00:50:51 +00:00
|
|
|
GList *cur;
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
cur = g_list_first(async_dns_queue_head);
|
|
|
|
while (cur) {
|
|
|
|
g_free(cur->data);
|
|
|
|
cur = g_list_next (cur);
|
|
|
|
}
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_list_free(async_dns_queue_head);
|
|
|
|
async_dns_queue_head = NULL;
|
2008-08-07 21:41:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (async_dns_initialized) {
|
|
|
|
ares_destroy(ghba_chan);
|
|
|
|
ares_destroy(ghbn_chan);
|
|
|
|
}
|
2009-10-30 21:57:43 +00:00
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
|
2013-08-11 00:50:51 +00:00
|
|
|
ares_library_cleanup();
|
2009-10-30 21:57:43 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_initialized = FALSE;
|
2008-08-07 21:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_GNU_ADNS)
|
2005-01-13 09:09:56 +00:00
|
|
|
|
2003-05-04 18:50:56 +00:00
|
|
|
/* XXX - The ADNS "documentation" isn't very clear:
|
|
|
|
* - Do we need to keep our query structures around?
|
|
|
|
*/
|
2008-07-08 02:25:30 +00:00
|
|
|
gboolean
|
2012-10-02 21:13:59 +00:00
|
|
|
host_name_lookup_process(void) {
|
2013-08-11 00:50:51 +00:00
|
|
|
async_dns_queue_msg_t *almsg;
|
|
|
|
GList *cur;
|
|
|
|
char addr_str[] = "111.222.333.444.in-addr.arpa.";
|
|
|
|
guint8 *addr_bytes;
|
|
|
|
adns_answer *ans;
|
|
|
|
int ret;
|
|
|
|
gboolean dequeue;
|
|
|
|
gboolean nro = new_resolved_objects;
|
|
|
|
|
|
|
|
new_resolved_objects = FALSE;
|
|
|
|
async_dns_queue_head = g_list_first(async_dns_queue_head);
|
|
|
|
|
|
|
|
cur = async_dns_queue_head;
|
|
|
|
while (cur && async_dns_in_flight <= name_resolve_concurrency) {
|
|
|
|
almsg = (async_dns_queue_msg_t *) cur->data;
|
|
|
|
if (! almsg->submitted && almsg->type == AF_INET) {
|
|
|
|
addr_bytes = (guint8 *) &almsg->ip4_addr;
|
|
|
|
g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
|
|
|
|
addr_bytes[2], addr_bytes[1], addr_bytes[0]);
|
|
|
|
/* XXX - what if it fails? */
|
|
|
|
adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
|
|
|
|
almsg->submitted = TRUE;
|
|
|
|
async_dns_in_flight++;
|
2010-06-28 22:43:13 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
cur = cur->next;
|
2003-05-04 18:50:56 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
|
|
|
|
cur = async_dns_queue_head;
|
|
|
|
while (cur) {
|
|
|
|
dequeue = FALSE;
|
|
|
|
almsg = (async_dns_queue_msg_t *) cur->data;
|
|
|
|
if (almsg->submitted) {
|
|
|
|
ret = adns_check(ads, &almsg->query, &ans, NULL);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (ans->status == adns_s_ok) {
|
|
|
|
add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
|
|
|
|
}
|
|
|
|
dequeue = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
if (dequeue) {
|
|
|
|
async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
|
|
|
|
g_free(almsg);
|
|
|
|
/* XXX, what to do if async_dns_in_flight == 0? */
|
|
|
|
async_dns_in_flight--;
|
|
|
|
}
|
2003-05-04 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Keep the timeout in place */
|
|
|
|
return nro;
|
2003-05-04 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 20:50:25 +00:00
|
|
|
static void
|
|
|
|
_host_name_lookup_cleanup(void) {
|
2013-08-11 00:50:51 +00:00
|
|
|
void *qdata;
|
|
|
|
|
|
|
|
async_dns_queue_head = g_list_first(async_dns_queue_head);
|
|
|
|
while (async_dns_queue_head) {
|
|
|
|
qdata = async_dns_queue_head->data;
|
|
|
|
async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
|
|
|
|
g_free(qdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (async_dns_initialized)
|
|
|
|
adns_finish(ads);
|
|
|
|
async_dns_initialized = FALSE;
|
2003-05-04 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#else /* HAVE_GNU_ADNS */
|
2003-05-05 00:53:06 +00:00
|
|
|
|
2008-07-08 02:25:30 +00:00
|
|
|
gboolean
|
2012-10-02 21:13:59 +00:00
|
|
|
host_name_lookup_process(void) {
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean nro = new_resolved_objects;
|
2010-01-19 23:18:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
new_resolved_objects = FALSE;
|
2009-12-22 20:01:06 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return nro;
|
2003-05-05 00:53:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 20:50:25 +00:00
|
|
|
static void
|
|
|
|
_host_name_lookup_cleanup(void) {
|
2003-05-05 00:53:06 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 21:41:48 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
2003-05-04 18:50:56 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
2010-04-29 18:17:29 +00:00
|
|
|
get_hostname(const guint addr)
|
1998-09-16 02:39:15 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean found;
|
2012-07-08 01:31:48 +00:00
|
|
|
|
2013-08-29 22:08:23 +00:00
|
|
|
/* XXX why do we call this if we're not resolving? To create hash entries?
|
|
|
|
* Why?
|
|
|
|
*/
|
|
|
|
hashipv4_t *tp = host_lookup(addr, &found);
|
2013-08-29 18:59:49 +00:00
|
|
|
|
2013-08-29 22:08:23 +00:00
|
|
|
if (!gbl_resolv_flags.network_name)
|
|
|
|
return tp->ip;
|
2009-09-06 08:59:30 +00:00
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return tp->name;
|
2009-09-06 08:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------- */
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
2010-04-29 18:17:29 +00:00
|
|
|
get_hostname6(const struct e_in6_addr *addr)
|
1999-03-28 18:32:03 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean found;
|
2012-07-08 01:31:48 +00:00
|
|
|
|
2013-08-29 22:08:23 +00:00
|
|
|
/* XXX why do we call this if we're not resolving? To create hash entries?
|
|
|
|
* Why?
|
|
|
|
*/
|
|
|
|
hashipv6_t *tp = host_lookup6(addr, &found);
|
2011-01-25 22:11:59 +00:00
|
|
|
|
2013-08-29 22:08:23 +00:00
|
|
|
if (!gbl_resolv_flags.network_name)
|
|
|
|
return tp->ip6;
|
2013-08-29 18:59:49 +00:00
|
|
|
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
|
|
|
|
|
2013-08-29 22:08:23 +00:00
|
|
|
return tp->name;
|
2009-09-06 08:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------- */
|
2012-04-13 22:23:04 +00:00
|
|
|
void
|
2010-04-29 18:17:29 +00:00
|
|
|
add_ipv4_name(const guint addr, const gchar *name)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipv4_t *tp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't add zero-length names; apparently, some resolvers will return
|
|
|
|
* them if they get them from DNS.
|
|
|
|
*/
|
2013-08-29 22:08:23 +00:00
|
|
|
if (name[0] == '\0')
|
2013-08-11 00:50:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2014-05-11 03:30:48 +00:00
|
|
|
tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
|
2013-08-12 18:33:08 +00:00
|
|
|
if(tp){
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
}else{
|
2013-08-11 19:02:26 +00:00
|
|
|
tp = new_ipv4(addr);
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
2014-05-11 03:30:48 +00:00
|
|
|
g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-11 19:02:26 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
|
2013-08-11 00:50:51 +00:00
|
|
|
new_resolved_objects = TRUE;
|
|
|
|
|
2004-11-17 08:53:20 +00:00
|
|
|
} /* add_ipv4_name */
|
1998-09-25 23:24:07 +00:00
|
|
|
|
2009-09-06 08:59:30 +00:00
|
|
|
/* -------------------------- */
|
2012-04-13 22:23:04 +00:00
|
|
|
void
|
2010-04-29 18:17:29 +00:00
|
|
|
add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
|
2004-11-17 09:44:41 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashipv6_t *tp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't add zero-length names; apparently, some resolvers will return
|
|
|
|
* them if they get them from DNS.
|
|
|
|
*/
|
2013-08-29 22:08:23 +00:00
|
|
|
if (name[0] == '\0')
|
2013-08-11 00:50:51 +00:00
|
|
|
return;
|
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
|
|
|
|
if(tp){
|
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
}else{
|
2013-10-23 12:02:15 +00:00
|
|
|
struct e_in6_addr *addr_key;
|
2013-08-11 00:50:51 +00:00
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
addr_key = g_new(struct e_in6_addr,1);
|
2013-08-18 19:02:48 +00:00
|
|
|
tp = new_ipv6(addrp);
|
2013-10-23 12:02:15 +00:00
|
|
|
memcpy(addr_key, addrp, 16);
|
2013-08-18 19:02:48 +00:00
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
|
|
|
g_hash_table_insert(ipv6_hash_table, addr_key, tp);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-18 19:02:48 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
g_strlcpy(tp->name, name, MAXNAMELEN);
|
2013-10-10 19:17:48 +00:00
|
|
|
tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
|
2013-08-11 00:50:51 +00:00
|
|
|
new_resolved_objects = TRUE;
|
2011-03-24 22:47:57 +00:00
|
|
|
|
2004-11-17 09:44:41 +00:00
|
|
|
} /* add_ipv6_name */
|
|
|
|
|
2013-10-23 12:02:15 +00:00
|
|
|
static void
|
|
|
|
add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
|
|
|
|
{
|
|
|
|
resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
|
|
|
|
|
|
|
|
add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
|
|
|
|
{
|
|
|
|
resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
|
|
|
|
|
|
|
|
add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_manually_resolved(void)
|
|
|
|
{
|
2013-10-23 14:04:19 +00:00
|
|
|
if(manually_resolved_ipv4_list){
|
|
|
|
g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
|
2013-10-23 12:02:15 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 14:04:19 +00:00
|
|
|
if(manually_resolved_ipv6_list){
|
|
|
|
g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
|
2013-10-23 12:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
void
|
|
|
|
host_name_lookup_init(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
char *hostspath;
|
|
|
|
guint i;
|
2013-08-07 16:46:08 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GNU_ADNS
|
|
|
|
#ifdef _WIN32
|
2013-08-11 00:50:51 +00:00
|
|
|
char *sysroot;
|
|
|
|
static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
|
|
|
|
static char rootpath_ot[] = "\\hosts";
|
2013-08-07 16:46:08 +00:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
#endif /*GNU_ADNS */
|
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
g_assert(ipxnet_hash_table == NULL);
|
|
|
|
ipxnet_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
|
|
|
|
|
2013-08-12 18:33:08 +00:00
|
|
|
g_assert(ipv4_hash_table == NULL);
|
2014-05-11 03:30:48 +00:00
|
|
|
ipv4_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
|
2013-08-12 18:33:08 +00:00
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
g_assert(ipv6_hash_table == NULL);
|
|
|
|
ipv6_hash_table = g_hash_table_new_full(ipv6_oat_hash, ipv6_equal, g_free, g_free);
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* Load the global hosts file, if we have one.
|
|
|
|
*/
|
|
|
|
if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
|
|
|
|
hostspath = get_datafile_path(ENAME_HOSTS);
|
2014-09-26 17:41:43 +00:00
|
|
|
if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
|
2013-08-11 00:50:51 +00:00
|
|
|
report_open_failure(hostspath, errno, FALSE);
|
|
|
|
}
|
|
|
|
g_free(hostspath);
|
|
|
|
}
|
2013-08-13 01:41:51 +00:00
|
|
|
/*
|
|
|
|
* Load the user's hosts file no matter what, if they have one.
|
|
|
|
*/
|
|
|
|
hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
|
2014-09-26 17:41:43 +00:00
|
|
|
if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
|
2013-08-13 01:41:51 +00:00
|
|
|
report_open_failure(hostspath, errno, FALSE);
|
|
|
|
}
|
|
|
|
g_free(hostspath);
|
2013-08-07 16:46:08 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2014-06-30 23:46:58 +00:00
|
|
|
if (gbl_resolv_flags.concurrent_dns) {
|
2013-08-07 16:46:08 +00:00
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
|
2014-06-30 23:46:58 +00:00
|
|
|
if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
|
2013-08-07 16:46:08 +00:00
|
|
|
#endif
|
2014-06-30 23:46:58 +00:00
|
|
|
if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
|
|
|
|
async_dns_initialized = TRUE;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
|
2014-06-30 23:46:58 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
#endif
|
2014-06-30 23:46:58 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_GNU_ADNS
|
|
|
|
/*
|
2013-08-11 00:50:51 +00:00
|
|
|
* We're using GNU ADNS, which doesn't check the system hosts file;
|
|
|
|
* we load that file ourselves.
|
2013-08-07 16:46:08 +00:00
|
|
|
*/
|
2013-08-11 00:50:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
sysroot = getenv_utf8("WINDIR");
|
|
|
|
if (sysroot != NULL) {
|
|
|
|
/*
|
|
|
|
* The file should be under WINDIR.
|
|
|
|
* If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
|
|
|
|
* %WINDIR%\system32\drivers\etc\hosts.
|
|
|
|
* If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
|
|
|
|
* Try both.
|
|
|
|
* XXX - should we base it on the dwPlatformId value from
|
|
|
|
* GetVersionEx()?
|
|
|
|
*/
|
|
|
|
if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
|
|
|
|
hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
|
2014-09-26 17:41:43 +00:00
|
|
|
if (!read_hosts_file(hostspath, TRUE)) {
|
2013-08-11 00:50:51 +00:00
|
|
|
g_free(hostspath);
|
|
|
|
hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
|
2014-09-26 17:41:43 +00:00
|
|
|
read_hosts_file(hostspath, TRUE);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
|
|
|
g_free(hostspath);
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
#else /* _WIN32 */
|
2013-08-11 00:50:51 +00:00
|
|
|
if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
|
2014-09-26 17:41:43 +00:00
|
|
|
read_hosts_file("/etc/hosts", TRUE);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-06-30 23:46:58 +00:00
|
|
|
if (gbl_resolv_flags.concurrent_dns) {
|
|
|
|
/* XXX - Any flags we should be using? */
|
|
|
|
/* XXX - We could provide config settings for DNS servers, and
|
|
|
|
pass them to ADNS with adns_init_strcfg */
|
|
|
|
if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
|
|
|
|
/*
|
|
|
|
* XXX - should we report the error? I'm assuming that some crashes
|
|
|
|
* reported on a Windows machine with TCP/IP not configured are due
|
|
|
|
* to "adns_init()" failing (due to the lack of TCP/IP) and leaving
|
|
|
|
* ADNS in a state where it crashes due to that. We'll still try
|
|
|
|
* doing name resolution anyway.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
async_dns_initialized = TRUE;
|
|
|
|
async_dns_in_flight = 0;
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
#endif /* HAVE_GNU_ADNS */
|
|
|
|
#endif /* HAVE_C_ARES */
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if(extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only){
|
|
|
|
for (i = 0; i < extra_hosts_files->len; i++) {
|
2014-09-26 17:41:43 +00:00
|
|
|
read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
subnet_name_lookup_init();
|
2013-10-23 12:02:15 +00:00
|
|
|
|
|
|
|
add_manually_resolved();
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
host_name_lookup_cleanup(void)
|
|
|
|
{
|
2015-01-16 17:35:14 +00:00
|
|
|
guint32 i, j;
|
2013-08-11 00:50:51 +00:00
|
|
|
_host_name_lookup_cleanup();
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-20 22:41:49 +00:00
|
|
|
if(ipxnet_hash_table){
|
|
|
|
g_hash_table_destroy(ipxnet_hash_table);
|
2013-10-23 12:02:15 +00:00
|
|
|
ipxnet_hash_table = NULL;
|
2013-08-20 22:41:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 19:02:26 +00:00
|
|
|
if(ipv4_hash_table){
|
|
|
|
g_hash_table_destroy(ipv4_hash_table);
|
2013-10-23 12:02:15 +00:00
|
|
|
ipv4_hash_table = NULL;
|
2013-08-11 19:02:26 +00:00
|
|
|
}
|
2013-08-20 05:06:26 +00:00
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
if(ipv6_hash_table){
|
|
|
|
g_hash_table_destroy(ipv6_hash_table);
|
2013-10-23 12:02:15 +00:00
|
|
|
ipv6_hash_table = NULL;
|
2013-08-18 19:02:48 +00:00
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2015-01-16 17:35:14 +00:00
|
|
|
for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
|
|
|
|
if (subnet_length_entries[i].subnet_addresses != NULL) {
|
|
|
|
for (j = 0; j < HASHHOSTSIZE; j++) {
|
|
|
|
if (subnet_length_entries[i].subnet_addresses[j] != NULL)
|
|
|
|
{
|
|
|
|
cleanup_subnet_entry(subnet_length_entries[i].subnet_addresses[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(subnet_length_entries[i].subnet_addresses);
|
|
|
|
subnet_length_entries[i].subnet_addresses = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
have_subnet_entry = FALSE;
|
|
|
|
new_resolved_objects = FALSE;
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 14:04:19 +00:00
|
|
|
static void
|
|
|
|
free_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
|
2013-10-23 12:02:15 +00:00
|
|
|
{
|
2013-10-23 14:04:19 +00:00
|
|
|
resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
|
|
|
|
|
2014-04-19 19:33:22 +00:00
|
|
|
g_free(resolved_ipv4_entry);
|
2013-10-23 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
|
|
|
|
{
|
|
|
|
resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
|
|
|
|
|
2014-04-19 19:33:22 +00:00
|
|
|
g_free(resolved_ipv6_entry);
|
2013-10-23 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
manually_resolve_cleanup(void)
|
|
|
|
{
|
|
|
|
if(manually_resolved_ipv4_list){
|
2014-04-19 19:33:22 +00:00
|
|
|
g_slist_foreach(manually_resolved_ipv4_list, free_manually_resolved_ipv4, NULL);
|
2013-10-23 14:04:19 +00:00
|
|
|
g_slist_free(manually_resolved_ipv4_list);
|
2014-04-19 19:33:22 +00:00
|
|
|
manually_resolved_ipv4_list = NULL;
|
|
|
|
}
|
2013-10-23 14:04:19 +00:00
|
|
|
|
|
|
|
if(manually_resolved_ipv6_list){
|
2014-04-19 19:33:22 +00:00
|
|
|
g_slist_foreach(manually_resolved_ipv6_list, free_manually_resolved_ipv6, NULL);
|
2013-10-23 14:04:19 +00:00
|
|
|
g_slist_free(manually_resolved_ipv6_list);
|
2014-04-19 19:33:22 +00:00
|
|
|
manually_resolved_ipv6_list = NULL;
|
2013-10-23 12:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
gchar *
|
2015-01-07 21:24:17 +00:00
|
|
|
udp_port_to_display(wmem_allocator_t *allocator, guint port)
|
1998-09-16 02:39:15 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.transport_name) {
|
2015-01-07 21:24:17 +00:00
|
|
|
return wmem_utoa(allocator, port);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return serv_name_lookup(port, PT_UDP);
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2015-01-07 21:24:17 +00:00
|
|
|
} /* udp_port_to_display */
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
gchar *
|
2015-01-07 21:24:17 +00:00
|
|
|
dccp_port_to_display(wmem_allocator_t *allocator, guint port)
|
2005-09-20 15:18:28 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.transport_name) {
|
2015-01-07 21:24:17 +00:00
|
|
|
return wmem_utoa(allocator, port);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2005-09-20 15:18:28 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return serv_name_lookup(port, PT_DCCP);
|
2005-09-20 15:18:28 +00:00
|
|
|
|
2015-01-07 21:24:17 +00:00
|
|
|
} /* dccp_port_to_display */
|
2005-09-20 15:18:28 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
gchar *
|
2015-01-07 21:24:17 +00:00
|
|
|
tcp_port_to_display(wmem_allocator_t *allocator, guint port)
|
1998-09-16 02:39:15 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.transport_name) {
|
2015-01-07 21:24:17 +00:00
|
|
|
return wmem_utoa(allocator, port);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return serv_name_lookup(port, PT_TCP);
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2015-01-07 21:24:17 +00:00
|
|
|
} /* tcp_port_to_display */
|
1998-09-16 02:39:15 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
gchar *
|
2015-01-07 21:24:17 +00:00
|
|
|
sctp_port_to_display(wmem_allocator_t *allocator, guint port)
|
2000-08-19 08:26:04 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.transport_name) {
|
2015-01-07 21:24:17 +00:00
|
|
|
return wmem_utoa(allocator, port);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2000-08-19 08:26:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return serv_name_lookup(port, PT_SCTP);
|
2000-08-19 08:26:04 +00:00
|
|
|
|
2015-01-07 21:24:17 +00:00
|
|
|
} /* sctp_port_to_display */
|
2000-08-19 08:26:04 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
const gchar *
|
2015-01-05 02:40:05 +00:00
|
|
|
address_to_display(wmem_allocator_t *allocator, const address *addr)
|
2014-12-29 05:21:15 +00:00
|
|
|
{
|
|
|
|
gchar *str = NULL;
|
|
|
|
const gchar *result = solve_address_to_name(addr);
|
|
|
|
|
|
|
|
if (result != NULL) {
|
|
|
|
str = wmem_strdup(allocator, result);
|
|
|
|
}
|
|
|
|
else if (addr->type == AT_NONE) {
|
|
|
|
str = wmem_strdup(allocator, "NONE");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = (gchar *) wmem_alloc(allocator, MAX_ADDR_STR_LEN);
|
|
|
|
address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
const gchar *
|
2014-04-19 19:06:34 +00:00
|
|
|
get_addr_name(const address *addr)
|
2009-09-07 16:05:37 +00:00
|
|
|
{
|
2014-04-19 11:04:12 +00:00
|
|
|
guint32 ip4_addr;
|
|
|
|
struct e_in6_addr ip6_addr;
|
2009-09-07 16:05:37 +00:00
|
|
|
|
2014-04-19 11:04:12 +00:00
|
|
|
/*
|
|
|
|
* Try to look up a name for this address.
|
|
|
|
* If it's not found, this might return a string corresponding to
|
|
|
|
* the address, or it might return NULL.
|
|
|
|
*
|
|
|
|
* Whatever string is returned has at least session scope.
|
|
|
|
*/
|
|
|
|
switch (addr->type) {
|
2009-09-07 16:05:37 +00:00
|
|
|
|
2014-04-19 11:04:12 +00:00
|
|
|
case AT_ETHER:
|
|
|
|
return get_ether_name((const guint8 *)addr->data);
|
2009-09-07 16:05:37 +00:00
|
|
|
|
2014-04-19 11:04:12 +00:00
|
|
|
case AT_IPv4:
|
|
|
|
memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
|
|
|
|
return get_hostname(ip4_addr);
|
2006-01-19 23:01:24 +00:00
|
|
|
|
2014-04-19 11:04:12 +00:00
|
|
|
case AT_IPv6:
|
|
|
|
memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
|
|
|
|
return get_hostname6(&ip6_addr);
|
2006-01-19 23:01:24 +00:00
|
|
|
|
2014-04-19 11:04:12 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-07 16:05:37 +00:00
|
|
|
}
|
2006-01-19 23:01:24 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
gchar *
|
|
|
|
get_ether_name(const guint8 *addr)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashether_t *tp;
|
|
|
|
gboolean resolve = gbl_resolv_flags.mac_name;
|
2010-07-01 15:11:02 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = eth_name_lookup(addr, resolve);
|
2010-07-01 15:11:02 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return resolve ? tp->resolved_name : tp->hexaddr;
|
2010-07-01 15:11:02 +00:00
|
|
|
|
1998-09-25 23:24:07 +00:00
|
|
|
} /* get_ether_name */
|
|
|
|
|
2010-06-28 22:29:49 +00:00
|
|
|
/* Look for a (non-dummy) ether name in the hash, and return it if found.
|
|
|
|
* If it's not found, simply return NULL.
|
1999-11-21 16:32:23 +00:00
|
|
|
*/
|
2010-04-29 18:17:29 +00:00
|
|
|
gchar *
|
|
|
|
get_ether_name_if_known(const guint8 *addr)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
hashether_t *tp;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Initialize ether structs if we're the first
|
|
|
|
* ether-related function called */
|
|
|
|
if (!gbl_resolv_flags.mac_name)
|
|
|
|
return NULL;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
|
|
|
|
tp = eth_name_lookup(addr, TRUE);
|
|
|
|
g_assert(tp != NULL);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
|
|
|
|
/* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
|
|
|
|
return tp->resolved_name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Name was created */
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
guint8 *
|
2010-04-29 18:17:29 +00:00
|
|
|
get_ether_addr(const gchar *name)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* force resolution (do not check gbl_resolv_flags) */
|
|
|
|
return eth_addr_lookup(name);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* get_ether_addr */
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
void
|
2010-04-29 18:17:29 +00:00
|
|
|
add_ether_byip(const guint ip, const guint8 *eth)
|
2000-08-10 20:09:29 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gboolean found;
|
|
|
|
hashipv4_t *tp;
|
2000-08-10 20:09:29 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* first check that IP address can be resolved */
|
|
|
|
if (!gbl_resolv_flags.network_name)
|
|
|
|
return;
|
2000-08-10 20:09:29 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
tp = host_lookup(ip, &found);
|
|
|
|
if (found) {
|
|
|
|
/* ok, we can add this entry in the ethers hashtable */
|
|
|
|
add_eth_name(eth, tp->name);
|
|
|
|
}
|
2000-08-10 20:09:29 +00:00
|
|
|
|
|
|
|
} /* add_ether_byip */
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
2015-01-08 17:13:18 +00:00
|
|
|
get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
1999-11-22 06:03:46 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.network_name) {
|
2015-01-08 17:13:18 +00:00
|
|
|
return ipxnet_to_str_punct(allocator, addr, '\0');
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2015-01-08 17:13:18 +00:00
|
|
|
return ipxnet_name_lookup(allocator, addr);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* get_ipxnet_name */
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
guint32
|
2010-04-29 18:17:29 +00:00
|
|
|
get_ipxnet_addr(const gchar *name, gboolean *known)
|
1999-11-21 16:32:23 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint32 addr;
|
|
|
|
gboolean success;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* force resolution (do not check gbl_resolv_flags) */
|
|
|
|
addr = ipxnet_addr_lookup(name, &success);
|
1999-11-21 16:32:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*known = success;
|
|
|
|
return addr;
|
1999-11-21 16:32:23 +00:00
|
|
|
|
|
|
|
} /* get_ipxnet_addr */
|
|
|
|
|
2015-01-18 02:17:24 +00:00
|
|
|
gchar *
|
|
|
|
get_manuf_name(wmem_allocator_t *allocator, const guint8 *addr)
|
1998-09-25 23:24:07 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar *cur;
|
|
|
|
int manuf_key;
|
|
|
|
guint8 oct;
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* manuf needs only the 3 most significant octets of the ethernet address */
|
|
|
|
manuf_key = addr[0];
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[1];
|
|
|
|
manuf_key = manuf_key | oct;
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[2];
|
|
|
|
manuf_key = manuf_key | oct;
|
|
|
|
|
|
|
|
if (!gbl_resolv_flags.mac_name || ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL)) {
|
2015-01-18 02:17:24 +00:00
|
|
|
cur=wmem_strdup_printf(allocator, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
|
2013-08-11 00:50:51 +00:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2015-01-18 02:17:24 +00:00
|
|
|
return wmem_strdup(allocator, cur);
|
1998-09-25 23:24:07 +00:00
|
|
|
|
|
|
|
} /* get_manuf_name */
|
1999-07-07 22:52:57 +00:00
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
|
|
|
uint_get_manuf_name(const guint oid)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
guint8 addr[3];
|
2012-04-13 22:23:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
addr[0] = (oid >> 16) & 0xFF;
|
|
|
|
addr[1] = (oid >> 8) & 0xFF;
|
|
|
|
addr[2] = (oid >> 0) & 0xFF;
|
2015-01-18 02:17:24 +00:00
|
|
|
return get_manuf_name(wmem_packet_scope(), addr);
|
2012-04-13 22:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
2011-05-20 02:42:14 +00:00
|
|
|
tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
|
|
|
|
{
|
2015-01-18 02:17:24 +00:00
|
|
|
return get_manuf_name(wmem_packet_scope(), tvb_get_ptr(tvb, offset, 3));
|
2011-05-20 02:42:14 +00:00
|
|
|
}
|
1999-07-07 22:52:57 +00:00
|
|
|
|
2010-04-29 18:17:29 +00:00
|
|
|
const gchar *
|
|
|
|
get_manuf_name_if_known(const guint8 *addr)
|
2005-05-20 21:44:17 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar *cur;
|
|
|
|
int manuf_key;
|
|
|
|
guint8 oct;
|
2005-05-20 21:44:17 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* manuf needs only the 3 most significant octets of the ethernet address */
|
|
|
|
manuf_key = addr[0];
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[1];
|
|
|
|
manuf_key = manuf_key | oct;
|
|
|
|
manuf_key = manuf_key<<8;
|
|
|
|
oct = addr[2];
|
|
|
|
manuf_key = manuf_key | oct;
|
|
|
|
|
|
|
|
if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-05-20 21:44:17 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return cur;
|
2005-05-20 21:44:17 +00:00
|
|
|
|
|
|
|
} /* get_manuf_name_if_known */
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
2013-08-01 20:11:13 +00:00
|
|
|
uint_get_manuf_name_if_known(const guint manuf_key)
|
2012-04-13 22:23:04 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
gchar *cur;
|
2012-04-13 22:23:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-01 20:11:13 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return cur;
|
2012-04-13 22:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
2011-05-20 02:42:14 +00:00
|
|
|
tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
|
2011-05-20 02:42:14 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 22:23:04 +00:00
|
|
|
const gchar *
|
2015-01-08 18:16:01 +00:00
|
|
|
eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
|
2011-05-08 10:23:53 +00:00
|
|
|
{
|
2015-01-08 18:16:01 +00:00
|
|
|
gchar *name;
|
|
|
|
guint8 *addr = (guint8 *)wmem_alloc(allocator, 8);
|
2011-05-20 02:42:14 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Copy and convert the address to network byte order. */
|
|
|
|
*(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
|
2011-05-08 10:23:53 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!gbl_resolv_flags.mac_name || ((name = manuf_name_lookup(addr)) == NULL)) {
|
2015-01-08 18:16:01 +00:00
|
|
|
return wmem_strdup_printf(allocator, "%02x:%02x:%02x%02x:%02x:%02x%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
|
2013-08-11 00:50:51 +00:00
|
|
|
}
|
2015-01-08 18:16:01 +00:00
|
|
|
return wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", name, addr[3], addr[4], addr[5], addr[6], addr[7]);
|
2011-05-08 10:23:53 +00:00
|
|
|
|
2015-01-08 18:16:01 +00:00
|
|
|
} /* eui64_to_display */
|
1999-07-07 22:52:57 +00:00
|
|
|
|
2009-12-24 00:58:31 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
|
|
|
#define GHI_TIMEOUT (250 * 1000)
|
|
|
|
static void
|
2013-08-11 00:50:51 +00:00
|
|
|
c_ares_ghi_cb(
|
|
|
|
void *arg,
|
|
|
|
int status,
|
2009-12-24 00:58:31 +00:00
|
|
|
#if ( ( ARES_VERSION_MAJOR < 1 ) \
|
2013-08-11 00:50:51 +00:00
|
|
|
|| ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
|
|
|
|
struct hostent *hp
|
2009-12-24 00:58:31 +00:00
|
|
|
#else
|
2013-08-11 00:50:51 +00:00
|
|
|
int timeouts _U_,
|
|
|
|
struct hostent *hp
|
2009-12-24 00:58:31 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - If we wanted to be really fancy we could cache results here and
|
|
|
|
* look them up in get_host_ipaddr* below.
|
|
|
|
*/
|
|
|
|
async_hostent_t *ahp = (async_hostent_t *)arg;
|
|
|
|
if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
|
|
|
|
memcpy(ahp->addrp, hp->h_addr, hp->h_length);
|
|
|
|
ahp->copied = hp->h_length;
|
|
|
|
}
|
2009-12-24 00:58:31 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_C_ARES */
|
|
|
|
|
Have "get_host_ipaddr()" return a Boolean indicating whether it
succeeded or failed, and, if it succeeded, have it fill in the IP
address if found through a pointer passed as the second argument.
Have it first try interpreting its first argument as a dotted-quad IP
address, with "inet_aton()", and, if that fails, have it try to
interpret it as a host name with "gethostbyname()"; don't bother with
"gethostbyaddr()", as we should be allowed to filter on IP addresses
even if there's no host name associated with them (there's no guarantee
that "gethostbyaddr()" will succeed if handed an IP address with no
corresponding name - and it looks as if FreeBSD 3.2, at least, may not
succeed in that case).
Add a "dfilter_fail()" routine that takes "printf()"-like arguments and
uses them to set an error message for the parse; doing so means that
even if the filter expression is syntactically valid, we treat it as
being invalid. (Is there a better way to force a parse to fail from
arbitrary places in routines called by the parser?)
Use that routine in the lexical analyzer.
If that error message was set, use it as is as the failure message,
rather than adding "Unable to parse filter string XXX" to it.
Have the code to handle IP addresses and host names in display filters
check whether "get_host_ipaddr()" succeeded or failed and, if it failed,
arrange that the parse fail with an error message indicating the source
of the problem.
svn path=/trunk/; revision=802
1999-10-11 03:03:12 +00:00
|
|
|
/* Translate a string, assumed either to be a dotted-quad IP address or
|
|
|
|
* a host name, to a numeric IP address. Return TRUE if we succeed and
|
|
|
|
* set "*addrp" to that numeric IP address; return FALSE if we fail.
|
1999-07-07 22:52:57 +00:00
|
|
|
* Used more in the dfilter parser rather than in packet dissectors */
|
2010-04-29 18:17:29 +00:00
|
|
|
gboolean
|
|
|
|
get_host_ipaddr(const char *host, guint32 *addrp)
|
1999-07-07 22:52:57 +00:00
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
struct in_addr ipaddr;
|
2009-12-24 00:58:31 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
|
|
|
|
int nfds;
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
async_hostent_t ahe;
|
2009-12-24 00:58:31 +00:00
|
|
|
#else /* HAVE_C_ARES */
|
2013-08-11 00:50:51 +00:00
|
|
|
struct hostent *hp;
|
2009-12-24 00:58:31 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
2009-09-07 16:07:04 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* don't change it to inet_pton(AF_INET), they are not 100% compatible.
|
|
|
|
* inet_pton(AF_INET) does not support hexadecimal notation nor
|
|
|
|
* less-than-4 octet notation.
|
2012-07-10 15:02:23 +00:00
|
|
|
*/
|
2013-08-11 00:50:51 +00:00
|
|
|
if (!inet_aton(host, &ipaddr)) {
|
|
|
|
|
|
|
|
/* It's not a valid dotted-quad IP address; is it a valid
|
|
|
|
* host name?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* If we're not allowed to do name resolution, don't do name
|
|
|
|
* resolution...
|
|
|
|
*/
|
|
|
|
if (!gbl_resolv_flags.network_name ||
|
|
|
|
!gbl_resolv_flags.use_external_net_name_resolver) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-07-10 15:02:23 +00:00
|
|
|
|
2009-12-24 00:58:31 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
if (! (gbl_resolv_flags.concurrent_dns) ||
|
|
|
|
name_resolve_concurrency < 1 ||
|
|
|
|
! async_dns_initialized) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ahe.addr_size = (int) sizeof (struct in_addr);
|
|
|
|
ahe.copied = 0;
|
|
|
|
ahe.addrp = addrp;
|
|
|
|
ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
nfds = ares_fds(ghbn_chan, &rfds, &wfds);
|
|
|
|
if (nfds > 0) {
|
|
|
|
tvp = ares_timeout(ghbn_chan, &tv, &tv);
|
|
|
|
if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
|
2014-06-30 23:46:58 +00:00
|
|
|
fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ares_process(ghbn_chan, &rfds, &wfds);
|
|
|
|
}
|
|
|
|
ares_cancel(ghbn_chan);
|
|
|
|
if (ahe.addr_size == ahe.copied) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
return FALSE;
|
2009-12-24 00:58:31 +00:00
|
|
|
#else /* ! HAVE_C_ARES */
|
2013-08-11 00:50:51 +00:00
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (hp == NULL) {
|
|
|
|
/* No. */
|
|
|
|
return FALSE;
|
|
|
|
/* Apparently, some versions of gethostbyaddr can
|
|
|
|
* return IPv6 addresses. */
|
|
|
|
} else if (hp->h_length <= (int) sizeof (struct in_addr)) {
|
|
|
|
memcpy(&ipaddr, hp->h_addr, hp->h_length);
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_C_ARES */
|
2009-09-07 16:07:04 +00:00
|
|
|
} else {
|
2013-08-11 00:50:51 +00:00
|
|
|
/* Does the string really contain dotted-quad IP?
|
|
|
|
* Check against inet_atons that accept strings such as
|
|
|
|
* "130.230" as valid addresses and try to convert them
|
|
|
|
* to some form of a classful (host.net) notation.
|
|
|
|
*/
|
|
|
|
unsigned int a0, a1, a2, a3;
|
|
|
|
if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
|
|
|
|
return FALSE;
|
2009-09-07 16:07:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
*addrp = ipaddr.s_addr;
|
|
|
|
return TRUE;
|
1999-07-07 22:52:57 +00:00
|
|
|
}
|
1999-10-14 05:41:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
|
|
|
|
* Return TRUE if we succeed and set "*addrp" to that numeric IP address;
|
|
|
|
* return FALSE if we fail.
|
|
|
|
*/
|
2010-04-29 18:17:29 +00:00
|
|
|
gboolean
|
|
|
|
get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
|
1999-10-14 05:41:33 +00:00
|
|
|
{
|
2009-12-24 00:58:31 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
|
|
|
|
int nfds;
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
async_hostent_t ahe;
|
2010-08-09 11:19:15 +00:00
|
|
|
#elif defined(HAVE_GETHOSTBYNAME2)
|
2013-08-11 00:50:51 +00:00
|
|
|
struct hostent *hp;
|
2009-12-24 00:58:31 +00:00
|
|
|
#endif /* HAVE_C_ARES */
|
1999-10-14 05:41:33 +00:00
|
|
|
|
2013-12-21 16:16:56 +00:00
|
|
|
if (str_to_ip6(host, addrp))
|
2013-08-11 00:50:51 +00:00
|
|
|
return TRUE;
|
1999-10-14 05:41:33 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* It's not a valid dotted-quad IP address; is it a valid
|
|
|
|
* host name?
|
|
|
|
*/
|
2012-07-10 15:02:23 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* If we're not allowed to do name resolution, don't do name
|
|
|
|
* resolution...
|
|
|
|
*/
|
|
|
|
if (!gbl_resolv_flags.network_name ||
|
|
|
|
!gbl_resolv_flags.use_external_net_name_resolver) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-12-24 17:21:02 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/* try FQDN */
|
2009-12-24 00:58:31 +00:00
|
|
|
#ifdef HAVE_C_ARES
|
2013-08-11 00:50:51 +00:00
|
|
|
if (! (gbl_resolv_flags.concurrent_dns) ||
|
|
|
|
name_resolve_concurrency < 1 ||
|
|
|
|
! async_dns_initialized) {
|
2013-08-07 16:46:08 +00:00
|
|
|
return FALSE;
|
2012-11-05 09:18:43 +00:00
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
ahe.addr_size = (int) sizeof (struct e_in6_addr);
|
|
|
|
ahe.copied = 0;
|
|
|
|
ahe.addrp = addrp;
|
|
|
|
ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
nfds = ares_fds(ghbn_chan, &rfds, &wfds);
|
|
|
|
if (nfds > 0) {
|
|
|
|
tvp = ares_timeout(ghbn_chan, &tv, &tv);
|
|
|
|
if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
|
2014-06-30 23:46:58 +00:00
|
|
|
fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ares_process(ghbn_chan, &rfds, &wfds);
|
|
|
|
}
|
|
|
|
ares_cancel(ghbn_chan);
|
|
|
|
if (ahe.addr_size == ahe.copied) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-12-24 00:58:31 +00:00
|
|
|
#elif defined(HAVE_GETHOSTBYNAME2)
|
2013-08-11 00:50:51 +00:00
|
|
|
hp = gethostbyname2(host, AF_INET6);
|
|
|
|
if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
|
|
|
|
memcpy(addrp, hp->h_addr, hp->h_length);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-12-24 00:58:31 +00:00
|
|
|
#endif
|
1999-10-14 05:41:33 +00:00
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
return FALSE;
|
1999-10-14 05:41:33 +00:00
|
|
|
}
|
2004-06-25 07:00:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out whether a hostname resolves to an ip or ipv6 address
|
|
|
|
* Return "ip6" if it is IPv6, "ip" otherwise (including the case
|
|
|
|
* that we don't know)
|
|
|
|
*/
|
2004-12-27 13:17:20 +00:00
|
|
|
const char* host_ip_af(const char *host
|
|
|
|
#ifndef HAVE_GETHOSTBYNAME2
|
2013-08-11 00:50:51 +00:00
|
|
|
_U_
|
2004-12-27 13:17:20 +00:00
|
|
|
#endif
|
2013-08-11 00:50:51 +00:00
|
|
|
)
|
2004-06-25 07:00:54 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETHOSTBYNAME2
|
2013-08-11 00:50:51 +00:00
|
|
|
struct hostent *h;
|
|
|
|
return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
|
2004-06-25 07:00:54 +00:00
|
|
|
#else
|
2013-08-11 00:50:51 +00:00
|
|
|
return "ip";
|
2004-06-25 07:00:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-08-01 20:11:13 +00:00
|
|
|
|
|
|
|
GHashTable *
|
|
|
|
get_manuf_hashtable(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
return manuf_hashtable;
|
2013-08-01 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GHashTable *
|
|
|
|
get_wka_hashtable(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
return wka_hashtable;
|
2013-08-01 20:11:13 +00:00
|
|
|
}
|
2013-08-02 15:05:00 +00:00
|
|
|
|
|
|
|
GHashTable *
|
|
|
|
get_eth_hashtable(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
return eth_hashtable;
|
2013-08-02 15:05:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:36:24 +00:00
|
|
|
GHashTable *
|
|
|
|
get_serv_port_hashtable(void)
|
|
|
|
{
|
2013-08-11 00:50:51 +00:00
|
|
|
return serv_port_hashtable;
|
2013-08-07 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 05:06:26 +00:00
|
|
|
GHashTable *
|
|
|
|
get_ipxnet_hash_table(void)
|
|
|
|
{
|
|
|
|
return ipxnet_hash_table;
|
|
|
|
}
|
|
|
|
|
2013-08-11 19:02:26 +00:00
|
|
|
GHashTable *
|
|
|
|
get_ipv4_hash_table(void)
|
|
|
|
{
|
|
|
|
return ipv4_hash_table;
|
|
|
|
}
|
|
|
|
|
2013-08-18 19:02:48 +00:00
|
|
|
GHashTable *
|
|
|
|
get_ipv6_hash_table(void)
|
|
|
|
{
|
|
|
|
return ipv6_hash_table;
|
|
|
|
}
|
2013-08-07 16:46:08 +00:00
|
|
|
/* Initialize all the address resolution subsystems in this file */
|
|
|
|
void
|
|
|
|
addr_resolv_init(void)
|
|
|
|
{
|
|
|
|
initialize_services();
|
|
|
|
initialize_ethers();
|
|
|
|
initialize_ipxnets();
|
|
|
|
/* host name initialization is done on a per-capture-file basis */
|
|
|
|
/*host_name_lookup_init();*/
|
2013-08-05 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 16:46:08 +00:00
|
|
|
/* Clean up all the address resolution subsystems in this file */
|
|
|
|
void
|
|
|
|
addr_resolv_cleanup(void)
|
|
|
|
{
|
|
|
|
service_name_lookup_cleanup();
|
|
|
|
eth_name_lookup_cleanup();
|
|
|
|
ipx_name_lookup_cleanup();
|
|
|
|
/* host name initialization is done on a per-capture-file basis */
|
|
|
|
/*host_name_lookup_cleanup();*/
|
|
|
|
}
|
2013-08-11 00:50:51 +00:00
|
|
|
|
2013-12-21 16:16:56 +00:00
|
|
|
gboolean
|
|
|
|
str_to_ip(const char *str, void *dst)
|
|
|
|
{
|
|
|
|
return inet_pton(AF_INET, str, dst) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
str_to_ip6(const char *str, void *dst)
|
|
|
|
{
|
|
|
|
return inet_pton(AF_INET6, str, dst) > 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 00:50:51 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|