resolved: cache formatted server string in DnsServer structure

This makes it easier to log information about a specific DnsServer object.
This commit is contained in:
Lennart Poettering 2016-01-08 20:59:03 +01:00
parent 6bb2c08597
commit 6cb08a8930
4 changed files with 28 additions and 28 deletions

View File

@ -135,6 +135,7 @@ DnsServer* dns_server_unref(DnsServer *s) {
if (s->n_ref > 0)
return NULL;
free(s->server_string);
free(s);
return NULL;
}
@ -316,12 +317,10 @@ void dns_server_packet_truncated(DnsServer *s, DnsServerFeatureLevel level) {
}
void dns_server_packet_rrsig_missing(DnsServer *s) {
_cleanup_free_ char *ip = NULL;
assert(s);
assert(s->manager);
in_addr_to_string(s->family, &s->address, &ip);
log_warning("DNS server %s does not augment replies with RRSIG records, DNSSEC not available.", strna(ip));
log_warning("DNS server %s does not augment replies with RRSIG records, DNSSEC not available.", dns_server_string(s));
s->rrsig_missing = true;
}
@ -350,7 +349,6 @@ DnsServerFeatureLevel dns_server_possible_feature_level(DnsServer *s) {
if (s->possible_feature_level != DNS_SERVER_FEATURE_LEVEL_BEST &&
dns_server_grace_period_expired(s)) {
_cleanup_free_ char *ip = NULL;
s->possible_feature_level = DNS_SERVER_FEATURE_LEVEL_BEST;
s->n_failed_udp = 0;
@ -360,9 +358,9 @@ DnsServerFeatureLevel dns_server_possible_feature_level(DnsServer *s) {
s->verified_usec = 0;
s->rrsig_missing = false;
in_addr_to_string(s->family, &s->address, &ip);
log_info("Grace period over, resuming full feature set (%s) for DNS server %s",
dns_server_feature_level_to_string(s->possible_feature_level), strna(ip));
dns_server_feature_level_to_string(s->possible_feature_level),
dns_server_string(s));
} else if (s->possible_feature_level <= s->verified_feature_level)
s->possible_feature_level = s->verified_feature_level;
@ -405,7 +403,6 @@ DnsServerFeatureLevel dns_server_possible_feature_level(DnsServer *s) {
s->possible_feature_level--;
if (p != s->possible_feature_level) {
_cleanup_free_ char *ip = NULL;
/* We changed the feature level, reset the counting */
s->n_failed_udp = 0;
@ -414,9 +411,9 @@ DnsServerFeatureLevel dns_server_possible_feature_level(DnsServer *s) {
s->packet_truncated = false;
s->verified_usec = 0;
in_addr_to_string(s->family, &s->address, &ip);
log_warning("Using degraded feature set (%s) for DNS server %s",
dns_server_feature_level_to_string(s->possible_feature_level), strna(ip));
dns_server_feature_level_to_string(s->possible_feature_level),
dns_server_string(s));
}
}
@ -451,6 +448,15 @@ int dns_server_adjust_opt(DnsServer *server, DnsPacket *packet, DnsServerFeature
return dns_packet_append_opt(packet, packet_size, edns_do, NULL);
}
const char *dns_server_string(DnsServer *server) {
assert(server);
if (!server->server_string)
(void) in_addr_to_string(server->family, &server->address, &server->server_string);
return strna(server->server_string);
}
static void dns_server_hash_func(const void *p, struct siphash *state) {
const DnsServer *s = p;
@ -542,12 +548,8 @@ DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
if (m->current_dns_server == s)
return s;
if (s) {
_cleanup_free_ char *ip = NULL;
in_addr_to_string(s->family, &s->address, &ip);
log_info("Switching to system DNS server %s.", strna(ip));
}
if (s)
log_info("Switching to system DNS server %s.", dns_server_string(s));
dns_server_unref(m->current_dns_server);
m->current_dns_server = dns_server_ref(s);

View File

@ -61,6 +61,8 @@ struct DnsServer {
int family;
union in_addr_union address;
char *server_string;
usec_t resend_timeout;
usec_t max_rtt;
@ -112,6 +114,8 @@ DnsServerFeatureLevel dns_server_possible_feature_level(DnsServer *s);
int dns_server_adjust_opt(DnsServer *server, DnsPacket *packet, DnsServerFeatureLevel level);
const char *dns_server_string(DnsServer *server);
DnsServer *dns_server_find(DnsServer *first, int family, const union in_addr_union *in_addr);
void dns_server_unlink_all(DnsServer *first);

View File

@ -463,12 +463,8 @@ DnsServer* link_set_dns_server(Link *l, DnsServer *s) {
if (l->current_dns_server == s)
return s;
if (s) {
_cleanup_free_ char *ip = NULL;
in_addr_to_string(s->family, &s->address, &ip);
log_info("Switching to DNS server %s for interface %s.", strna(ip), l->name);
}
if (s)
log_info("Switching to DNS server %s for interface %s.", dns_server_string(s), l->name);
dns_server_unref(l->current_dns_server);
l->current_dns_server = dns_server_ref(s);

View File

@ -147,16 +147,14 @@ clear:
}
static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
assert(count);
r = in_addr_to_string(s->family, &s->address, &t);
if (r < 0) {
log_warning_errno(r, "Invalid DNS address. Ignoring: %m");
(void) dns_server_string(s);
if (!s->server_string) {
log_warning("Our of memory, or invalid DNS address. Ignoring server.");
return;
}
@ -164,7 +162,7 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
(*count) ++;
fprintf(f, "nameserver %s\n", t);
fprintf(f, "nameserver %s\n", s->server_string);
}
static void write_resolv_conf_search(