mirror of https://github.com/c-ares/c-ares.git
Replace hosts parser, add caching capabilities (#591)
HOSTS FILE PROCESSING OVERVIEW ============================== The hosts file on the system contains static entries to be processed locally rather than querying the nameserver. Each row is an IP address followed by a list of space delimited hostnames that match the ip address. This is used for both forward and reverse lookups. We are caching the entire parsed hosts file for performance reasons. Some files may be quite sizable and as per Issue #458 can approach 1/2MB in size, and the parse overhead on a rapid succession of queries can be quite large. The entries are stored in forwards and backwards hashtables so we can get O(1) performance on lookup. The file is cached until the file modification timestamp changes (or 60s if there is no implemented stat() capability). The hosts file processing is quite unique. It has to merge all related hosts and ips into a single entry due to file formatting requirements. For instance take the below: ``` 127.0.0.1 localhost.localdomain localhost ::1 localhost.localdomain localhost 192.168.1.1 host.example.com host 192.168.1.5 host.example.com host 2620🔢:1 host.example.com host6.example.com host6 host ``` This will yield 2 entries. 1) ips: `127.0.0.1,::1` hosts: `localhost.localdomain,localhost` 2) ips: `192.168.1.1,192.168.1.5,2620🔢:1` hosts: `host.example.com,host,host6.example.com,host6` It could be argued that if searching for `192.168.1.1` that the `host6` hostnames should not be returned, but this implementation will return them since they are related (both ips have the fqdn of host.example.com). It is unlikely this will matter in the real world. Fix By: Brad House (@bradh352)pull/593/head
parent
58e6f1fa81
commit
8a3664b8cb
20 changed files with 1306 additions and 946 deletions
@ -1,289 +0,0 @@ |
||||
/* MIT License
|
||||
* |
||||
* Copyright (c) 1998, 2011 Massachusetts Institute of Technology |
||||
* Copyright (c) The c-ares project and its contributors |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice (including the next |
||||
* paragraph) shall be included in all copies or substantial portions of the |
||||
* Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include "ares_setup.h" |
||||
|
||||
#ifdef HAVE_NETINET_IN_H |
||||
# include <netinet/in.h> |
||||
#endif |
||||
#ifdef HAVE_NETDB_H |
||||
# include <netdb.h> |
||||
#endif |
||||
#ifdef HAVE_ARPA_INET_H |
||||
# include <arpa/inet.h> |
||||
#endif |
||||
|
||||
#include "ares.h" |
||||
#include "ares_inet_net_pton.h" |
||||
#include "ares_private.h" |
||||
|
||||
ares_status_t ares__get_hostent(FILE *fp, int family, struct hostent **host) |
||||
{ |
||||
char *line = NULL; |
||||
char *p; |
||||
char *q; |
||||
char **alias; |
||||
const char *txtaddr; |
||||
const char *txthost; |
||||
char *txtalias; |
||||
ares_status_t status; |
||||
size_t addrlen; |
||||
size_t linesize; |
||||
size_t naliases; |
||||
struct ares_addr addr; |
||||
struct hostent *hostent = NULL; |
||||
|
||||
*host = NULL; /* Assume failure */ |
||||
|
||||
/* Validate family */ |
||||
switch (family) { |
||||
case AF_INET: |
||||
case AF_INET6: |
||||
case AF_UNSPEC: |
||||
break; |
||||
default: |
||||
return ARES_EBADFAMILY; |
||||
} |
||||
|
||||
while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { |
||||
/* Trim line comment. */ |
||||
p = line; |
||||
while (*p && (*p != '#')) { |
||||
p++; |
||||
} |
||||
*p = '\0'; |
||||
|
||||
/* Trim trailing whitespace. */ |
||||
q = p - 1; |
||||
while ((q >= line) && ISSPACE(*q)) { |
||||
q--; |
||||
} |
||||
*++q = '\0'; |
||||
|
||||
/* Skip leading whitespace. */ |
||||
p = line; |
||||
while (*p && ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if empty. */ |
||||
continue; |
||||
} |
||||
|
||||
/* Pointer to start of IPv4 or IPv6 address part. */ |
||||
txtaddr = p; |
||||
|
||||
/* Advance past address part. */ |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if reached end of line. */ |
||||
continue; |
||||
} |
||||
|
||||
/* Null terminate address part. */ |
||||
*p = '\0'; |
||||
|
||||
/* Advance to host name */ |
||||
p++; |
||||
while (*p && ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if reached end of line. */ |
||||
continue; /* LCOV_EXCL_LINE: trailing whitespace already stripped */ |
||||
} |
||||
|
||||
/* Pointer to start of host name. */ |
||||
txthost = p; |
||||
|
||||
/* Advance past host name. */ |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
|
||||
/* Pointer to start of first alias. */ |
||||
txtalias = NULL; |
||||
if (*p) { |
||||
q = p + 1; |
||||
while (*q && ISSPACE(*q)) { |
||||
q++; |
||||
} |
||||
if (*q) { |
||||
txtalias = q; |
||||
} |
||||
} |
||||
|
||||
/* Null terminate host name. */ |
||||
*p = '\0'; |
||||
|
||||
/* find out number of aliases. */ |
||||
naliases = 0; |
||||
if (txtalias) { |
||||
p = txtalias; |
||||
while (*p) { |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
while (*p && ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
naliases++; |
||||
} |
||||
} |
||||
|
||||
/* Convert address string to network address for the requested family. */ |
||||
addrlen = 0; |
||||
addr.family = AF_UNSPEC; |
||||
addr.addrV4.s_addr = INADDR_NONE; |
||||
if ((family == AF_INET || family == AF_UNSPEC) && |
||||
ares_inet_pton(AF_INET, txtaddr, &addr.addrV4) > 0) { |
||||
/* Actual network address family and length. */ |
||||
addr.family = AF_INET; |
||||
addrlen = sizeof(addr.addrV4); |
||||
} |
||||
if ((family == AF_INET6 || (family == AF_UNSPEC && !addrlen)) && |
||||
ares_inet_pton(AF_INET6, txtaddr, &addr.addrV6) > 0) { |
||||
/* Actual network address family and length. */ |
||||
addr.family = AF_INET6; |
||||
addrlen = sizeof(addr.addrV6); |
||||
} |
||||
if (!addrlen) { |
||||
/* Ignore line if invalid address string for the requested family. */ |
||||
continue; |
||||
} |
||||
|
||||
/*
|
||||
** Actual address family possible values are AF_INET and AF_INET6 only. |
||||
*/ |
||||
|
||||
/* Allocate memory for the hostent structure. */ |
||||
hostent = ares_malloc(sizeof(struct hostent)); |
||||
if (!hostent) { |
||||
break; |
||||
} |
||||
|
||||
/* Initialize fields for out of memory condition. */ |
||||
hostent->h_aliases = NULL; |
||||
hostent->h_addr_list = NULL; |
||||
|
||||
/* Copy official host name. */ |
||||
hostent->h_name = ares_strdup(txthost); |
||||
if (!hostent->h_name) { |
||||
break; |
||||
} |
||||
|
||||
/* Copy network address. */ |
||||
hostent->h_addr_list = ares_malloc(2 * sizeof(char *)); |
||||
if (!hostent->h_addr_list) { |
||||
break; |
||||
} |
||||
hostent->h_addr_list[1] = NULL; |
||||
hostent->h_addr_list[0] = ares_malloc(addrlen); |
||||
if (!hostent->h_addr_list[0]) { |
||||
break; |
||||
} |
||||
if (addr.family == AF_INET) { |
||||
memcpy(hostent->h_addr_list[0], &addr.addrV4, sizeof(addr.addrV4)); |
||||
} else { |
||||
memcpy(hostent->h_addr_list[0], &addr.addrV6, sizeof(addr.addrV6)); |
||||
} |
||||
|
||||
/* Copy aliases. */ |
||||
hostent->h_aliases = ares_malloc((naliases + 1) * sizeof(char *)); |
||||
if (!hostent->h_aliases) { |
||||
break; |
||||
} |
||||
alias = hostent->h_aliases; |
||||
while (naliases) { |
||||
*(alias + naliases--) = NULL; |
||||
} |
||||
*alias = NULL; |
||||
while (txtalias) { |
||||
p = txtalias; |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
q = p; |
||||
while (*q && ISSPACE(*q)) { |
||||
q++; |
||||
} |
||||
*p = '\0'; |
||||
if ((*alias = ares_strdup(txtalias)) == NULL) { |
||||
break; |
||||
} |
||||
alias++; |
||||
txtalias = *q ? q : NULL; |
||||
} |
||||
if (txtalias) { |
||||
/* Alias memory allocation failure. */ |
||||
break; |
||||
} |
||||
|
||||
/* Copy actual network address family and length. */ |
||||
hostent->h_addrtype = addr.family; |
||||
hostent->h_length = (int)addrlen; |
||||
|
||||
/* Free line buffer. */ |
||||
ares_free(line); |
||||
|
||||
/* Return hostent successfully */ |
||||
*host = hostent; |
||||
return ARES_SUCCESS; |
||||
} |
||||
|
||||
/* If allocated, free line buffer. */ |
||||
if (line) { |
||||
ares_free(line); |
||||
} |
||||
|
||||
if (status == ARES_SUCCESS) { |
||||
/* Memory allocation failure; clean up. */ |
||||
if (hostent) { |
||||
if (hostent->h_name) { |
||||
ares_free(hostent->h_name); |
||||
} |
||||
if (hostent->h_aliases) { |
||||
for (alias = hostent->h_aliases; *alias; alias++) { |
||||
ares_free(*alias); |
||||
} |
||||
ares_free(hostent->h_aliases); |
||||
} |
||||
if (hostent->h_addr_list) { |
||||
if (hostent->h_addr_list[0]) { |
||||
ares_free(hostent->h_addr_list[0]); |
||||
} |
||||
ares_free(hostent->h_addr_list); |
||||
} |
||||
ares_free(hostent); |
||||
} |
||||
return ARES_ENOMEM; |
||||
} |
||||
|
||||
return status; |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,264 +0,0 @@ |
||||
/* MIT License
|
||||
* |
||||
* Copyright (c) 2019 Andrew Selivanov |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice (including the next |
||||
* paragraph) shall be included in all copies or substantial portions of the |
||||
* Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include "ares_setup.h" |
||||
|
||||
#ifdef HAVE_NETINET_IN_H |
||||
# include <netinet/in.h> |
||||
#endif |
||||
#ifdef HAVE_NETDB_H |
||||
# include <netdb.h> |
||||
#endif |
||||
#ifdef HAVE_ARPA_INET_H |
||||
# include <arpa/inet.h> |
||||
#endif |
||||
|
||||
#include "ares.h" |
||||
#include "ares_inet_net_pton.h" |
||||
#include "ares_private.h" |
||||
|
||||
#define MAX_ALIASES 40 |
||||
|
||||
ares_status_t ares__readaddrinfo(FILE *fp, const char *name, |
||||
unsigned short port, |
||||
const struct ares_addrinfo_hints *hints, |
||||
struct ares_addrinfo *ai) |
||||
{ |
||||
char *line = NULL; |
||||
char *p; |
||||
char *q; |
||||
const char *txtaddr; |
||||
const char *txthost; |
||||
char *txtalias; |
||||
char *aliases[MAX_ALIASES]; |
||||
size_t i; |
||||
size_t alias_count; |
||||
ares_status_t status = ARES_SUCCESS; |
||||
size_t linesize; |
||||
struct ares_addrinfo_cname *cname = NULL; |
||||
struct ares_addrinfo_cname *cnames = NULL; |
||||
struct ares_addrinfo_node *nodes = NULL; |
||||
ares_bool_t match_with_alias; |
||||
ares_bool_t match_with_canonical; |
||||
ares_bool_t want_cname = |
||||
(hints->ai_flags & ARES_AI_CANONNAME) ? ARES_TRUE : ARES_FALSE; |
||||
|
||||
/* Validate family */ |
||||
switch (hints->ai_family) { |
||||
case AF_INET: |
||||
case AF_INET6: |
||||
case AF_UNSPEC: |
||||
break; |
||||
default: |
||||
return ARES_EBADFAMILY; |
||||
} |
||||
|
||||
ai->name = ares_strdup(name); |
||||
if (!ai->name) { |
||||
status = ARES_ENOMEM; |
||||
goto fail; |
||||
} |
||||
|
||||
while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { |
||||
match_with_alias = ARES_FALSE; |
||||
match_with_canonical = ARES_FALSE; |
||||
alias_count = 0; |
||||
/* Trim line comment. */ |
||||
p = line; |
||||
while (*p && (*p != '#')) { |
||||
p++; |
||||
} |
||||
*p = '\0'; |
||||
|
||||
/* Trim trailing whitespace. */ |
||||
q = p - 1; |
||||
while ((q >= line) && ISSPACE(*q)) { |
||||
q--; |
||||
} |
||||
*++q = '\0'; |
||||
|
||||
/* Skip leading whitespace. */ |
||||
p = line; |
||||
while (*p && ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if empty. */ |
||||
continue; |
||||
} |
||||
|
||||
/* Pointer to start of IPv4 or IPv6 address part. */ |
||||
txtaddr = p; |
||||
|
||||
/* Advance past address part. */ |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if reached end of line. */ |
||||
continue; |
||||
} |
||||
|
||||
/* Null terminate address part. */ |
||||
*p = '\0'; |
||||
|
||||
/* Advance to host name */ |
||||
p++; |
||||
while (*p && ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
if (!*p) { |
||||
/* Ignore line if reached end of line. */ |
||||
continue; /* LCOV_EXCL_LINE: trailing whitespace already stripped */ |
||||
} |
||||
|
||||
/* Pointer to start of host name. */ |
||||
txthost = p; |
||||
|
||||
/* Advance past host name. */ |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
|
||||
/* Pointer to start of first alias. */ |
||||
txtalias = NULL; |
||||
if (*p) { |
||||
q = p + 1; |
||||
while (*q && ISSPACE(*q)) { |
||||
q++; |
||||
} |
||||
if (*q) { |
||||
txtalias = q; |
||||
} |
||||
} |
||||
|
||||
/* Null terminate host name. */ |
||||
*p = '\0'; |
||||
|
||||
/* Find out if host name matches with canonical host name. */ |
||||
if (strcasecmp(txthost, name) == 0) { |
||||
match_with_canonical = ARES_TRUE; |
||||
} |
||||
|
||||
/* Find out if host name matches with one of the aliases. */ |
||||
while (txtalias) { |
||||
p = txtalias; |
||||
while (*p && !ISSPACE(*p)) { |
||||
p++; |
||||
} |
||||
q = p; |
||||
while (*q && ISSPACE(*q)) { |
||||
q++; |
||||
} |
||||
*p = '\0'; |
||||
if (strcasecmp(txtalias, name) == 0) { |
||||
match_with_alias = ARES_TRUE; |
||||
if (!want_cname) { |
||||
break; |
||||
} |
||||
} |
||||
if (alias_count < MAX_ALIASES) { |
||||
aliases[alias_count++] = txtalias; |
||||
} |
||||
txtalias = *q ? q : NULL; |
||||
} |
||||
|
||||
/* Try next line if host does not match. */ |
||||
if (!match_with_alias && !match_with_canonical) { |
||||
continue; |
||||
} |
||||
|
||||
/*
|
||||
* Convert address string to network address for the requested families. |
||||
* Actual address family possible values are AF_INET and AF_INET6 only. |
||||
*/ |
||||
if ((hints->ai_family == AF_INET) || (hints->ai_family == AF_UNSPEC)) { |
||||
struct in_addr addr4; |
||||
if (ares_inet_pton(AF_INET, txtaddr, &addr4) == 1) { |
||||
status = ares_append_ai_node(AF_INET, port, 0, &addr4, &nodes); |
||||
if (status != ARES_SUCCESS) { |
||||
goto fail; |
||||
} |
||||
} |
||||
} |
||||
if ((hints->ai_family == AF_INET6) || (hints->ai_family == AF_UNSPEC)) { |
||||
struct ares_in6_addr addr6; |
||||
if (ares_inet_pton(AF_INET6, txtaddr, &addr6) == 1) { |
||||
status = ares_append_ai_node(AF_INET6, port, 0, &addr6, &nodes); |
||||
if (status != ARES_SUCCESS) { |
||||
goto fail; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (want_cname) { |
||||
for (i = 0; i < alias_count; ++i) { |
||||
cname = ares__append_addrinfo_cname(&cnames); |
||||
if (!cname) { |
||||
status = ARES_ENOMEM; |
||||
goto fail; |
||||
} |
||||
cname->alias = ares_strdup(aliases[i]); |
||||
cname->name = ares_strdup(txthost); |
||||
} |
||||
/* No aliases, cname only. */ |
||||
if (!alias_count) { |
||||
cname = ares__append_addrinfo_cname(&cnames); |
||||
if (!cname) { |
||||
status = ARES_ENOMEM; |
||||
goto fail; |
||||
} |
||||
cname->name = ares_strdup(txthost); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Last read failed. */ |
||||
if (status == ARES_ENOMEM) { |
||||
goto fail; |
||||
} |
||||
|
||||
/* If no results, its a failure */ |
||||
if (!nodes) { |
||||
status = ARES_ENOTFOUND; |
||||
goto fail; |
||||
} |
||||
|
||||
/* Free line buffer. */ |
||||
ares_free(line); |
||||
ares__addrinfo_cat_cnames(&ai->cnames, cnames); |
||||
ares__addrinfo_cat_nodes(&ai->nodes, nodes); |
||||
|
||||
return ARES_SUCCESS; |
||||
|
||||
fail: |
||||
ares_free(line); |
||||
ares__freeaddrinfo_cnames(cnames); |
||||
ares__freeaddrinfo_nodes(nodes); |
||||
ares_free(ai->name); |
||||
ai->name = NULL; |
||||
return status; |
||||
} |
Loading…
Reference in new issue