resolved-etc-hosts.c revision dd0bc0f1414cc1d0fa73a29470bd14944e4942d3
/***
This file is part of systemd.
Copyright 2016 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "fd-util.h"
#include "fileio.h"
#include "hostname-util.h"
#include "resolved-etc-hosts.h"
#include "resolved-dns-synthesize.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
typedef struct EtcHostsItem {
int family;
union in_addr_union address;
char **names;
} EtcHostsItem;
typedef struct EtcHostsItemByName {
char *name;
void manager_etc_hosts_flush(Manager *m) {
}
}
m->etc_hosts_mtime = USEC_INFINITY;
}
const EtcHostsItem *item = p;
}
static int etc_hosts_item_compare_func(const void *a, const void *b) {
const EtcHostsItem *x = a, *y = b;
return trivial_compare_func(a, b);
}
static const struct hash_ops etc_hosts_item_ops = {
};
EtcHostsItem key = {
};
char **n;
int r;
assert(m);
if (r < 0)
return r;
if (r > 0)
/* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to
* nothing. */
else {
/* If this is a normal address, then, simply add entry mapping it to the specified names */
if (item) {
if (r < 0)
return log_oom();
} else {
if (r < 0)
return log_oom();
if (!item)
return log_oom();
if (r < 0) {
return log_oom();
}
}
}
STRV_FOREACH(n, names) {
if (!bn) {
if (r < 0)
return log_oom();
if (!bn)
return log_oom();
return log_oom();
}
if (r < 0) {
return log_oom();
}
}
if (item) {
return log_oom();
}
}
return 0;
}
union in_addr_union in;
bool suppressed = false;
int family, r;
assert(m);
if (r < 0)
if (r == 0) {
return -EINVAL;
}
if (r < 0)
for (;;) {
if (r < 0)
if (r == 0)
break;
r = dns_name_is_valid(name);
if (r <= 0)
if (is_localhost(name)) {
/* Suppress the "localhost" line that is often seen */
suppressed = true;
continue;
}
if (r < 0)
return log_oom();
}
if (strv_isempty(names)) {
if (suppressed)
return 0;
return -EINVAL;
}
/* Takes possession of the names strv */
if (r < 0)
return r;
return r;
}
int manager_etc_hosts_read(Manager *m) {
unsigned nr = 0;
int r;
return 0;
m->etc_hosts_last = ts;
if (m->etc_hosts_mtime != USEC_INFINITY) {
r = 0;
goto clear;
}
}
/* Did the mtime change? If not, there's no point in re-reading the file. */
return 0;
}
if (!f) {
r = 0;
goto clear;
}
}
/* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
* invocation */
if (r < 0)
char *l;
nr ++;
if (isempty(l))
continue;
if (l[0] == '#')
continue;
r = parse_line(m, nr, l);
if (r == -ENOMEM) /* On OOM we abandon the half-built-up structure. All other errors we ignore and proceed */
goto clear;
}
m->etc_hosts_last = ts;
return 1;
return r;
}
bool found_a = false, found_aaaa = false;
EtcHostsItem k = {};
DnsResourceKey *t;
const char *name;
unsigned i;
int r;
assert(m);
assert(q);
r = manager_etc_hosts_read(m);
if (r < 0)
return r;
name = dns_question_first_name(q);
if (!name)
return 0;
if (r > 0) {
if (!item)
return 0;
/* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data
* we'll only return if the request was for PTR. */
DNS_QUESTION_FOREACH(t, q) {
continue;
continue;
if (r < 0)
return r;
if (r > 0) {
found_ptr = t;
break;
}
}
if (found_ptr) {
char **n;
if (r < 0)
return r;
if (!rr)
return -ENOMEM;
return -ENOMEM;
if (r < 0)
return r;
}
}
return 1;
}
if (!bn)
return 0;
if (r < 0)
return r;
DNS_QUESTION_FOREACH(t, q) {
continue;
continue;
if (r < 0)
return r;
if (r == 0)
continue;
found_a = true;
found_aaaa = true;
if (found_a && found_aaaa)
break;
}
continue;
if (r < 0)
return r;
if (r < 0)
return r;
}
return 1;
}