chiark / gitweb /
networkd/sd-network: expose statically configured NTP servers
authorTom Gundersen <teg@jklm.no>
Mon, 19 May 2014 16:42:14 +0000 (18:42 +0200)
committerTom Gundersen <teg@jklm.no>
Mon, 19 May 2014 16:42:14 +0000 (18:42 +0200)
src/network/networkd-address.c
src/network/networkd-link.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd.h
src/network/sd-network.c
src/systemd/sd-network.h

index 34863b2..afd36a0 100644 (file)
@@ -335,8 +335,18 @@ int config_parse_dns(const char *unit,
                 return 0;
         }
 
-        LIST_FIND_TAIL(addresses, network->dns, tail);
-        LIST_INSERT_AFTER(addresses, network->dns, tail, n);
+        if (streq(lvalue, "DNS")) {
+                LIST_FIND_TAIL(addresses, network->dns, tail);
+                LIST_INSERT_AFTER(addresses, network->dns, tail, n);
+        } else if (streq(lvalue, "NTP")) {
+                LIST_FIND_TAIL(addresses, network->ntp, tail);
+                LIST_INSERT_AFTER(addresses, network->ntp, tail, n);
+        } else {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Key is invalid, ignoring assignment: %s=%s", lvalue, rvalue);
+                return 0;
+        }
+
         n = NULL;
 
         return 0;
index a90aff9..3ca8626 100644 (file)
@@ -2027,8 +2027,10 @@ int link_save(Link *link) {
                 "FLAGS=%u\n",
                 admin_state, oper_state, link->flags);
 
-        if (link->network)
+        if (link->network) {
                 serialize_addresses(f, "DNS", link->network->dns);
+                serialize_addresses(f, "NTP", link->network->ntp);
+        }
 
         if (link->dhcp_lease) {
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
@@ -2037,8 +2039,11 @@ int link_save(Link *link) {
 
                 fprintf(f,
                         "DHCP_LEASE=%s\n"
-                        "DHCP_USE_DNS=%s\n",
-                        link->lease_file, yes_no(link->network->dhcp_dns));
+                        "DHCP_USE_DNS=%s\n"
+                        "DHCP_USE_NTP=%s\n",
+                        link->lease_file,
+                        yes_no(link->network->dhcp_dns),
+                        yes_no(link->network->dhcp_ntp));
         } else
                 unlink(link->lease_file);
 
index 4bb8425..5038cb5 100644 (file)
@@ -34,6 +34,7 @@ Network.IPv4LL,              config_parse_bool,                  0,
 Network.Address,             config_parse_address,               0,                             0
 Network.Gateway,             config_parse_gateway,               0,                             0
 Network.DNS,                 config_parse_dns,                   0,                             offsetof(Network, dns)
+Network.NTP,                 config_parse_dns,                   0,                             offsetof(Network, ntp)
 Network.Tunnel,              config_parse_tunnel,                0,                             offsetof(Network, tunnel)
 Address.Address,             config_parse_address,               0,                             0
 Address.Broadcast,           config_parse_broadcast,             0,                             0
index 1b8856a..a6cbee2 100644 (file)
@@ -81,6 +81,7 @@ static int network_load_one(Manager *manager, const char *filename) {
         if (!network->filename)
                 return log_oom();
 
+        network->dhcp_ntp = true;
         network->dhcp_dns = true;
         network->dhcp_hostname = true;
         network->dhcp_domainname = true;
@@ -160,6 +161,11 @@ void network_free(Network *network) {
 
         free(network->description);
 
+        while ((address = network->ntp)) {
+                LIST_REMOVE(addresses, network->ntp, address);
+                address_free(address);
+        }
+
         while ((address = network->dns)) {
                 LIST_REMOVE(addresses, network->dns, address);
                 address_free(address);
@@ -234,7 +240,7 @@ int network_apply(Manager *manager, Network *network, Link *link) {
 
         link->network = network;
 
-        if (network->dns) {
+        if (network->dns || network->ntp) {
                 r = link_save(link);
                 if (r < 0)
                         return r;
index 3e4e1f2..46ed2ac 100644 (file)
@@ -138,6 +138,7 @@ struct Network {
         Hashmap *macvlans;
         bool dhcp;
         bool dhcp_dns;
+        bool dhcp_ntp;
         bool dhcp_mtu;
         bool dhcp_hostname;
         bool dhcp_domainname;
@@ -151,6 +152,7 @@ struct Network {
         Hashmap *routes_by_section;
 
         LIST_HEAD(Address, dns);
+        LIST_HEAD(Address, ntp);
 
         LIST_FIELDS(Network, networks);
 };
index 7ae15ed..e7cda0d 100644 (file)
@@ -168,7 +168,7 @@ _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
         return 0;
 }
 
-_public_ int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *addr_size) {
+static int network_get_in_addr(const char *key, unsigned index, struct in_addr **addr, size_t *addr_size) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
@@ -179,7 +179,7 @@ _public_ int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *a
         if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
                 return -ENOMEM;
 
-        r = parse_env_file(p, NEWLINE, "DNS", &s, NULL);
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
         if (r < 0)
                 return r;
         else if (!s)
@@ -188,7 +188,15 @@ _public_ int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *a
         return deserialize_in_addrs(addr, addr_size, s);
 }
 
-_public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t *addr_size) {
+_public_ int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *addr_size) {
+        return network_get_in_addr("DNS", index, addr, addr_size);
+}
+
+_public_ int sd_network_get_ntp(unsigned index, struct in_addr **addr, size_t *addr_size) {
+        return network_get_in_addr("NTP", index, addr, addr_size);
+}
+
+static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr **addr, size_t *addr_size) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
@@ -199,7 +207,7 @@ _public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t
         if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
                 return -ENOMEM;
 
-        r = parse_env_file(p, NEWLINE, "DNS", &s, NULL);
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
         if (r < 0)
                 return r;
         else if (!s)
@@ -208,7 +216,15 @@ _public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t
         return deserialize_in6_addrs(addr, addr_size, s);
 }
 
-_public_ int sd_network_dhcp_use_dns(unsigned index) {
+_public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t *addr_size) {
+        return network_get_in6_addr("DNS", index, addr, addr_size);
+}
+
+_public_ int sd_network_get_ntp6(unsigned index, struct in6_addr **addr, size_t *addr_size) {
+        return network_get_in6_addr("NTP", index, addr, addr_size);
+}
+
+static int network_get_boolean(const char *key, unsigned index) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
@@ -217,7 +233,7 @@ _public_ int sd_network_dhcp_use_dns(unsigned index) {
         if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
                 return -ENOMEM;
 
-        r = parse_env_file(p, NEWLINE, "DHCP_USE_DNS", &s, NULL);
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
         if (r < 0)
                 return r;
         else if (!s)
@@ -226,6 +242,14 @@ _public_ int sd_network_dhcp_use_dns(unsigned index) {
         return parse_boolean(s);
 }
 
+_public_ int sd_network_dhcp_use_dns(unsigned index) {
+        return network_get_boolean("DHCP_USE_DNS", index);
+}
+
+_public_ int sd_network_dhcp_use_ntp(unsigned index) {
+        return network_get_boolean("DHCP_USE_NTP", index);
+}
+
 _public_ int sd_network_get_ifindices(unsigned **indices) {
         _cleanup_closedir_ DIR *d;
         int r = 0;
index 860325a..e42d106 100644 (file)
@@ -83,12 +83,21 @@ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret);
 /* Returns true if link is configured to respect DNS entries received by DHCP */
 int sd_network_dhcp_use_dns(unsigned index);
 
+/* Returns true if link is configured to respect NTP entries received by DHCP */
+int sd_network_dhcp_use_ntp(unsigned index);
+
 /* Get IPv4 DNS entries statically configured for the link */
 int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *addr_size);
 
+/* Get IPv4 NTP entries statically configured for the link */
+int sd_network_get_ntp(unsigned index, struct in_addr **addr, size_t *addr_size);
+
 /* Get IPv6 DNS entries statically configured for the link */
 int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t *addr_size);
 
+/* Get IPv6 NTP entries statically configured for the link */
+int sd_network_get_ntp6(unsigned index, struct in6_addr **addr, size_t *addr_size);
+
 /* Get all network interfaces' indices, and store them in *indices. Returns
  * the number of indices. If indices is NULL, only returns the number of indices. */
 int sd_network_get_ifindices(unsigned **indices);