aboutsummaryrefslogtreecommitdiffstats
path: root/epan/addr_resolv.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2004-11-17 08:34:36 +0000
committerGuy Harris <guy@alum.mit.edu>2004-11-17 08:34:36 +0000
commit3eb79de07d9d967e2e2183220ed006dabb25e738 (patch)
tree1c10b804e69afcbc3dc3a7e8bc5df565d0c520e0 /epan/addr_resolv.c
parent75ce5ae4043d33fd1c344d0bb199b87eb3667f4a (diff)
Based on code from Francisco Alcoba, read the hosts file on startup if
we're using ADNS, so that we can resolve host names from the hosts file as well as from DNS. "fgetline()" strips newlines from the line, so don't bother looking for them when tokenizing the line. svn path=/trunk/; revision=12534
Diffstat (limited to 'epan/addr_resolv.c')
-rw-r--r--epan/addr_resolv.c82
1 files changed, 79 insertions, 3 deletions
diff --git a/epan/addr_resolv.c b/epan/addr_resolv.c
index 6096a91633..181dad386a 100644
--- a/epan/addr_resolv.c
+++ b/epan/addr_resolv.c
@@ -631,7 +631,7 @@ static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
gboolean manuf_file)
{
/*
- * See man ethers(4) for ethers file format
+ * 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 Ethereal's '.' separator.
@@ -642,13 +642,13 @@ static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
if ((cp = strchr(line, '#')))
*cp = '\0';
- if ((cp = strtok(line, " \t\n")) == NULL)
+ if ((cp = strtok(line, " \t")) == NULL)
return -1;
if (!parse_ether_address(cp, eth, mask, manuf_file))
return -1;
- if ((cp = strtok(NULL, " \t\n")) == NULL)
+ if ((cp = strtok(NULL, " \t")) == NULL)
return -1;
strncpy(eth->name, cp, MAXNAMELEN);
@@ -1442,6 +1442,59 @@ static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
} /* ipxnet_addr_lookup */
+#ifdef HAVE_GNU_ADNS
+static void
+read_hosts_file (FILE *hf)
+{
+ char *line = NULL;
+ int size = 0;
+ gchar *cp;
+ guint32 host_addr[4]; /* IPv4 or IPv6 */
+ gboolean is_ipv6;
+ int ret;
+
+ /*
+ * See the hosts(4) or hosts(5) man page for hosts file format
+ * (not available on all systems).
+ */
+ while (fgetline(&line, &size, hf) >= 0) {
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
+
+ if ((cp = strtok(line, " \t")) == NULL)
+ continue; /* no tokens in the line */
+
+ ret = inet_pton(AF_INET6, cp, &host_addr);
+ if (ret == -1)
+ continue; /* error parsing */
+ if (ret == 1) {
+ /* Valid IPv6 */
+ is_ipv6 = TRUE;
+ } else {
+ /* Not valid IPv6 - valid IPv4? */
+ if (inet_pton(AF_INET, cp, &host_addr) != 1)
+ continue; /* no */
+ is_ipv6 = FALSE;
+ }
+
+ if ((cp = strtok(NULL, " \t")) == NULL)
+ continue; /* no host name */
+
+ if (!is_ipv6)
+ add_host_name(host_addr[0], cp);
+
+ /*
+ * Add the aliases, too, if there are any.
+ */
+ while ((cp = strtok(NULL, " \t")) != NULL) {
+ if (!is_ipv6)
+ add_host_name(host_addr[0], cp);
+ }
+ }
+ if (line != NULL)
+ g_free(line);
+} /* read_hosts_file */
+#endif
/*
* External Functions
@@ -1451,6 +1504,29 @@ static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
void
host_name_lookup_init(void) {
+
+ FILE *hf;
+ char *hostspath;
+ #ifdef WIN32
+ char *sysroot;
+ static char rootpath[] = "\\system32\\drivers\\etc\\hosts";
+ #endif
+
+#ifdef WIN32
+ sysroot = getenv("SYSTEMROOT");
+ hostspath = g_malloc(strlen(sysroot) + sizeof rootpath);
+ strcpy(hostspath, sysroot)
+ strcat(hostpath, rootpath);
+#else
+ hostspath = g_strdup("/etc/hosts");
+#endif
+
+ if ((hf = fopen(hostspath, "r")) != NULL) {
+ read_hosts_file(hf);
+ fclose(hf);
+ }
+ g_free(hostspath);
+
/* 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 */