network: introduce link_get_use_dns()

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2024-04-19 10:23:00 +09:00
parent 7a169cb40f
commit 9646ffe2ca
11 changed files with 63 additions and 76 deletions
+1 -1
View File
@@ -342,7 +342,7 @@ static int link_push_uplink_to_dhcp_server(
addresses[n_addresses++] = ia;
}
use_dhcp_lease_data = link->network->dhcp_use_dns;
use_dhcp_lease_data = link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4);
break;
case SD_DHCP_LEASE_NTP: {
+1 -1
View File
@@ -728,7 +728,7 @@ static int dhcp4_request_routes_to_dns(Link *link) {
assert(link->dhcp_lease);
assert(link->network);
if (!link->network->dhcp_use_dns ||
if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
!link->network->dhcp_routes_to_dns)
return 0;
+1 -1
View File
@@ -633,7 +633,7 @@ static int dhcp6_configure(Link *link) {
return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to set MUD URL: %m");
}
if (link->network->dhcp6_use_dns) {
if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVER);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to request DNS servers: %m");
+37 -51
View File
@@ -53,6 +53,43 @@ UseDomains link_get_use_domains(Link *link, NetworkConfigSource proto) {
return USE_DOMAINS_NO;
}
bool link_get_use_dns(Link *link, NetworkConfigSource proto) {
int n, c;
assert(link);
if (!link->network)
return false;
switch (proto) {
case NETWORK_CONFIG_SOURCE_DHCP4:
n = link->network->dhcp_use_dns;
c = link->network->compat_dhcp_use_dns;
break;
case NETWORK_CONFIG_SOURCE_DHCP6:
n = link->network->dhcp6_use_dns;
c = link->network->compat_dhcp_use_dns;
break;
case NETWORK_CONFIG_SOURCE_NDISC:
n = link->network->ndisc_use_dns;
c = -1;
break;
default:
assert_not_reached();
}
/* If per-network and per-protocol setting is specified, use it. */
if (n >= 0)
return n;
/* If compat setting is specified, use it. */
if (c >= 0)
return c;
/* Otherwise, defaults to yes. */
return true;
}
int config_parse_domains(
const char *unit,
const char *filename,
@@ -243,57 +280,6 @@ int config_parse_dnssec_negative_trust_anchors(
}
}
int config_parse_dhcp_use_dns(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
int r;
assert(filename);
assert(lvalue);
assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
assert(rvalue);
assert(data);
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue);
return 0;
}
switch (ltype) {
case AF_INET:
network->dhcp_use_dns = r;
network->dhcp_use_dns_set = true;
break;
case AF_INET6:
network->dhcp6_use_dns = r;
network->dhcp6_use_dns_set = true;
break;
case AF_UNSPEC:
/* For backward compatibility. */
if (!network->dhcp_use_dns_set)
network->dhcp_use_dns = r;
if (!network->dhcp6_use_dns_set)
network->dhcp6_use_dns = r;
break;
default:
assert_not_reached();
}
return 0;
}
static const char* const use_domains_table[_USE_DOMAINS_MAX] = {
[USE_DOMAINS_NO] = "no",
[USE_DOMAINS_ROUTE] = "route",
+1
View File
@@ -16,6 +16,7 @@ typedef enum UseDomains {
} UseDomains;
UseDomains link_get_use_domains(Link *link, NetworkConfigSource proto);
bool link_get_use_dns(Link *link, NetworkConfigSource proto);
const char* use_domains_to_string(UseDomains p) _const_;
UseDomains use_domains_from_string(const char *s) _pure_;
+3 -3
View File
@@ -448,7 +448,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
return r;
}
if (link->dhcp_lease && link->network->dhcp_use_dns) {
if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *dns;
union in_addr_union s;
int n_dns;
@@ -469,7 +469,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
}
}
if (link->dhcp6_lease && link->network->dhcp6_use_dns) {
if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *dns;
union in_addr_union s;
int n_dns;
@@ -490,7 +490,7 @@ static int dns_append_json(Link *link, JsonVariant **v) {
}
}
if (link->network->ndisc_use_dns) {
if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
NDiscRDNSS *a;
SET_FOREACH(a, link->ndisc_rdnss) {
+1 -1
View File
@@ -1409,7 +1409,7 @@ static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt) {
assert(link->network);
assert(rt);
if (!link->network->ndisc_use_dns)
if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC))
return 0;
r = sd_ndisc_router_get_sender_address(rt, &router);
+4 -4
View File
@@ -222,7 +222,7 @@ NextHop.Blackhole, config_parse_nexthop_blackhole,
NextHop.Group, config_parse_nexthop_group, 0, 0
DHCPv4.RequestAddress, config_parse_in_addr_non_null, AF_INET, offsetof(Network, dhcp_request_address)
DHCPv4.ClientIdentifier, config_parse_dhcp_client_identifier, 0, offsetof(Network, dhcp_client_identifier)
DHCPv4.UseDNS, config_parse_dhcp_use_dns, AF_INET, 0
DHCPv4.UseDNS, config_parse_tristate, 0, offsetof(Network, dhcp_use_dns)
DHCPv4.RoutesToDNS, config_parse_bool, 0, offsetof(Network, dhcp_routes_to_dns)
DHCPv4.UseNTP, config_parse_dhcp_use_ntp, AF_INET, 0
DHCPv4.RoutesToNTP, config_parse_bool, 0, offsetof(Network, dhcp_routes_to_ntp)
@@ -271,7 +271,7 @@ DHCPv4.NFTSet, config_parse_nft_set,
DHCPv4.RapidCommit, config_parse_tristate, 0, offsetof(Network, dhcp_use_rapid_commit)
DHCPv6.UseAddress, config_parse_bool, 0, offsetof(Network, dhcp6_use_address)
DHCPv6.UseDelegatedPrefix, config_parse_bool, 0, offsetof(Network, dhcp6_use_pd_prefix)
DHCPv6.UseDNS, config_parse_dhcp_use_dns, AF_INET6, 0
DHCPv6.UseDNS, config_parse_tristate, 0, offsetof(Network, dhcp6_use_dns)
DHCPv6.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp6_use_hostname)
DHCPv6.UseDomains, config_parse_use_domains, 0, offsetof(Network, dhcp6_use_domains)
DHCPv6.UseNTP, config_parse_dhcp_use_ntp, AF_INET6, 0
@@ -299,7 +299,7 @@ IPv6AcceptRA.UseRoutePrefix, config_parse_bool,
IPv6AcceptRA.UseAutonomousPrefix, config_parse_bool, 0, offsetof(Network, ndisc_use_autonomous_prefix)
IPv6AcceptRA.UseOnLinkPrefix, config_parse_bool, 0, offsetof(Network, ndisc_use_onlink_prefix)
IPv6AcceptRA.UsePREF64, config_parse_bool, 0, offsetof(Network, ndisc_use_pref64)
IPv6AcceptRA.UseDNS, config_parse_bool, 0, offsetof(Network, ndisc_use_dns)
IPv6AcceptRA.UseDNS, config_parse_tristate, 0, offsetof(Network, ndisc_use_dns)
IPv6AcceptRA.UseDomains, config_parse_use_domains, 0, offsetof(Network, ndisc_use_domains)
IPv6AcceptRA.UseMTU, config_parse_bool, 0, offsetof(Network, ndisc_use_mtu)
IPv6AcceptRA.UseHopLimit, config_parse_bool, 0, offsetof(Network, ndisc_use_hop_limit)
@@ -588,7 +588,7 @@ IPv6PrefixDelegation.Domains, config_parse_radv_search_domains,
IPv6PrefixDelegation.DNSLifetimeSec, config_parse_sec, 0, offsetof(Network, router_dns_lifetime_usec)
DHCPv4.BlackList, config_parse_in_addr_prefixes, AF_INET, offsetof(Network, dhcp_deny_listed_ip)
DHCP.ClientIdentifier, config_parse_dhcp_client_identifier, 0, offsetof(Network, dhcp_client_identifier)
DHCP.UseDNS, config_parse_dhcp_use_dns, AF_UNSPEC, 0
DHCP.UseDNS, config_parse_tristate, 0, offsetof(Network, compat_dhcp_use_dns)
DHCP.UseNTP, config_parse_dhcp_use_ntp, AF_UNSPEC, 0
DHCP.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_use_mtu)
DHCP.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_use_hostname)
+4 -3
View File
@@ -381,6 +381,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.keep_configuration = manager->keep_configuration,
.compat_dhcp_use_domains = _USE_DOMAINS_INVALID,
.compat_dhcp_use_dns = -1,
.dhcp_duid.type = _DUID_TYPE_INVALID,
.dhcp_critical = -1,
@@ -388,7 +389,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.dhcp_routes_to_ntp = true,
.dhcp_use_sip = true,
.dhcp_use_captive_portal = true,
.dhcp_use_dns = true,
.dhcp_use_dns = -1,
.dhcp_routes_to_dns = true,
.dhcp_use_domains = _USE_DOMAINS_INVALID,
.dhcp_use_hostname = true,
@@ -406,7 +407,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.dhcp6_use_address = true,
.dhcp6_use_pd_prefix = true,
.dhcp6_use_dns = true,
.dhcp6_use_dns = -1,
.dhcp6_use_domains = _USE_DOMAINS_INVALID,
.dhcp6_use_hostname = true,
.dhcp6_use_ntp = true,
@@ -480,7 +481,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.ndisc = -1,
.ndisc_use_redirect = true,
.ndisc_use_dns = true,
.ndisc_use_dns = -1,
.ndisc_use_gateway = true,
.ndisc_use_captive_portal = true,
.ndisc_use_route_prefix = true,
+4 -5
View File
@@ -115,6 +115,7 @@ struct Network {
/* For backward compatibility, only applied to DHCPv4 and DHCPv6. */
UseDomains compat_dhcp_use_domains;
int compat_dhcp_use_dns;
/* DHCP Client Support */
AddressFamily dhcp;
@@ -147,8 +148,7 @@ struct Network {
int dhcp_broadcast;
int dhcp_ipv6_only_mode;
int dhcp_use_rapid_commit;
bool dhcp_use_dns;
bool dhcp_use_dns_set;
int dhcp_use_dns;
bool dhcp_routes_to_dns;
bool dhcp_use_ntp;
bool dhcp_use_ntp_set;
@@ -180,8 +180,7 @@ struct Network {
bool dhcp6_use_pd_prefix;
bool dhcp6_send_hostname;
bool dhcp6_send_hostname_set;
bool dhcp6_use_dns;
bool dhcp6_use_dns_set;
int dhcp6_use_dns;
bool dhcp6_use_hostname;
bool dhcp6_use_ntp;
bool dhcp6_use_ntp_set;
@@ -340,7 +339,7 @@ struct Network {
/* NDisc support */
int ndisc;
bool ndisc_use_redirect;
bool ndisc_use_dns;
int ndisc_use_dns;
bool ndisc_use_gateway;
bool ndisc_use_route_prefix;
bool ndisc_use_autonomous_prefix;
+6 -6
View File
@@ -101,7 +101,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
if (r < 0)
return r;
if (link->dhcp_lease && link->network->dhcp_use_dns) {
if (link->dhcp_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *addresses;
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
@@ -112,7 +112,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
}
}
if (link->dhcp6_lease && link->network->dhcp6_use_dns) {
if (link->dhcp6_lease && link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *addresses;
r = sd_dhcp6_lease_get_dns(link->dhcp6_lease, &addresses);
@@ -123,7 +123,7 @@ static int link_put_dns(Link *link, OrderedSet **s) {
}
}
if (link->network->ndisc_use_dns) {
if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
NDiscRDNSS *a;
SET_FOREACH(a, link->ndisc_rdnss) {
@@ -666,14 +666,14 @@ static int link_save(Link *link) {
serialize_addresses(f, NULL, &space,
NULL,
link->dhcp_lease,
link->network->dhcp_use_dns,
link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4),
SD_DHCP_LEASE_DNS,
link->dhcp6_lease,
link->network->dhcp6_use_dns,
link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP6),
sd_dhcp6_lease_get_dns,
NULL);
if (link->network->ndisc_use_dns) {
if (link_get_use_dns(link, NETWORK_CONFIG_SOURCE_NDISC)) {
NDiscRDNSS *dd;
SET_FOREACH(dd, link->ndisc_rdnss)