mirror of
https://github.com/systemd/systemd.git
synced 2024-12-26 03:22:00 +03:00
sd-network: expose DNS/NTP servers as strings
This avoids having to distinguish between IPv4 and IPv6, allowing us to keep their internal orderings. The consumers now has to turn the strings into addresses.
This commit is contained in:
parent
6073b6f26a
commit
6f4dedb250
@ -134,12 +134,13 @@ _public_ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int network_get_in_addr(const char *key, int ifindex, struct in_addr **addr) {
|
||||
static int network_get_strv(const char *key, int ifindex, char ***ret) {
|
||||
_cleanup_free_ char *p = NULL, *s = NULL;
|
||||
_cleanup_strv_free_ char **a = NULL;
|
||||
int r;
|
||||
|
||||
assert_return(ifindex > 0, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
|
||||
return -ENOMEM;
|
||||
@ -147,63 +148,31 @@ static int network_get_in_addr(const char *key, int ifindex, struct in_addr **ad
|
||||
r = parse_env_file(p, NEWLINE, key, &s, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
else if (!s)
|
||||
return -EIO;
|
||||
else if (!s) {
|
||||
*ret = NULL;
|
||||
|
||||
return deserialize_in_addrs(addr, s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_network_get_dns(int ifindex, struct in_addr **addr) {
|
||||
return network_get_in_addr("DNS", ifindex, addr);
|
||||
}
|
||||
|
||||
static int network_get_in6_addr(const char *key, int ifindex, struct in6_addr **addr) {
|
||||
_cleanup_free_ char *p = NULL, *s = NULL;
|
||||
int r;
|
||||
|
||||
assert_return(ifindex > 0, -EINVAL);
|
||||
assert_return(addr, -EINVAL);
|
||||
|
||||
if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
|
||||
a = strv_split(s, " ");
|
||||
if (!a)
|
||||
return -ENOMEM;
|
||||
|
||||
r = parse_env_file(p, NEWLINE, key, &s, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
else if (!s)
|
||||
return -EIO;
|
||||
strv_uniq(a);
|
||||
r = strv_length(a);
|
||||
|
||||
return deserialize_in6_addrs(addr, s);
|
||||
*ret = a;
|
||||
a = NULL;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
_public_ int sd_network_get_dns6(int ifindex, struct in6_addr **addr) {
|
||||
return network_get_in6_addr("DNS", ifindex, addr);
|
||||
_public_ int sd_network_get_dns(int ifindex, char ***ret) {
|
||||
return network_get_strv("DNS", ifindex, ret);
|
||||
}
|
||||
|
||||
static int network_get_boolean(const char *key, int ifindex) {
|
||||
_cleanup_free_ char *p = NULL, *s = NULL;
|
||||
int r;
|
||||
|
||||
assert_return(ifindex > 0, -EINVAL);
|
||||
|
||||
if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
r = parse_env_file(p, NEWLINE, key, &s, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
else if (!s)
|
||||
return false;
|
||||
|
||||
return parse_boolean(s);
|
||||
}
|
||||
|
||||
_public_ int sd_network_dhcp_use_dns(int ifindex) {
|
||||
return network_get_boolean("DHCP_USE_DNS", ifindex);
|
||||
}
|
||||
|
||||
_public_ int sd_network_dhcp_use_ntp(int ifindex) {
|
||||
return network_get_boolean("DHCP_USE_NTP", ifindex);
|
||||
_public_ int sd_network_get_ntp(int ifindex, char ***ret) {
|
||||
return network_get_strv("NTP", ifindex, ret);
|
||||
}
|
||||
|
||||
static inline int MONITOR_TO_FD(sd_network_monitor *m) {
|
||||
|
@ -133,49 +133,33 @@ int link_update_rtnl(Link *l, sd_rtnl_message *m) {
|
||||
}
|
||||
|
||||
static int link_update_dns_servers(Link *l) {
|
||||
_cleanup_free_ struct in_addr *nameservers = NULL;
|
||||
_cleanup_free_ struct in6_addr *nameservers6 = NULL;
|
||||
_cleanup_strv_free_ char **nameservers = NULL;
|
||||
char **nameserver;
|
||||
DnsServer *s, *nx;
|
||||
int r, n, i;
|
||||
int r;
|
||||
|
||||
assert(l);
|
||||
|
||||
LIST_FOREACH(servers, s, l->dns_servers)
|
||||
s->marked = true;
|
||||
|
||||
n = sd_network_get_dns(l->ifindex, &nameservers);
|
||||
if (n < 0) {
|
||||
r = n;
|
||||
r = sd_network_get_dns(l->ifindex, &nameservers);
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
union in_addr_union a = { .in = nameservers[i] };
|
||||
STRV_FOREACH(nameserver, nameservers) {
|
||||
union in_addr_union a;
|
||||
int family;
|
||||
|
||||
s = link_find_dns_server(l, AF_INET, &a);
|
||||
r = in_addr_from_string_auto(*nameserver, &family, &a);
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
|
||||
s = link_find_dns_server(l, family, &a);
|
||||
if (s)
|
||||
s->marked = false;
|
||||
else {
|
||||
r = dns_server_new(l->manager, NULL, l, AF_INET, &a);
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
}
|
||||
}
|
||||
|
||||
n = sd_network_get_dns6(l->ifindex, &nameservers6);
|
||||
if (n < 0) {
|
||||
r = n;
|
||||
goto clear;
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
union in_addr_union a = { .in6 = nameservers6[i] };
|
||||
|
||||
s = link_find_dns_server(l, AF_INET6, &a);
|
||||
if (s)
|
||||
s->marked = false;
|
||||
else {
|
||||
r = dns_server_new(l->manager, NULL, l, AF_INET6, &a);
|
||||
r = dns_server_new(l->manager, NULL, l, family, &a);
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
}
|
||||
|
@ -76,17 +76,13 @@ int sd_network_get_operational_state(char **state);
|
||||
/* Get DHCPv4 lease from ifindex. */
|
||||
int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret);
|
||||
|
||||
/* Returns true if link is configured to respect DNS entries received by DHCP */
|
||||
int sd_network_dhcp_use_dns(int ifindex);
|
||||
/* Get DNS entries for a given link. These are string representations of
|
||||
* IP addresses */
|
||||
int sd_network_get_dns(int ifindex, char ***addr);
|
||||
|
||||
/* Returns true if link is configured to respect NTP entries received by DHCP */
|
||||
int sd_network_dhcp_use_ntp(int ifindex);
|
||||
|
||||
/* Get IPv4 DNS entries statically configured for the link */
|
||||
int sd_network_get_dns(int ifindex, struct in_addr **addr);
|
||||
|
||||
/* Get IPv6 DNS entries statically configured for the link */
|
||||
int sd_network_get_dns6(int ifindex, struct in6_addr **addr);
|
||||
/* Get NTP entries for a given link. These are domain names or string
|
||||
* reperesentations of IP addresses */
|
||||
int sd_network_get_ntp(int ifindex, char ***addr);
|
||||
|
||||
/* Monitor object */
|
||||
typedef struct sd_network_monitor sd_network_monitor;
|
||||
|
Loading…
Reference in New Issue
Block a user