[systemd-commits] 7 commits - man/systemd-networkd.service.xml src/libsystemd src/network src/systemd units/systemd-networkd.service.in

Tom Gundersen tomegun at kemper.freedesktop.org
Thu Jan 16 11:32:30 PST 2014


 man/systemd-networkd.service.xml  |    7 +
 src/libsystemd/sd-dhcp-client.c   |  251 ++++++++++++++++++--------------------
 src/network/networkd-gperf.gperf  |    1 
 src/network/networkd-link.c       |   85 +++++++++++-
 src/network/networkd-manager.c    |   30 +++-
 src/network/networkd-network.c    |    1 
 src/network/networkd.c            |   35 ++++-
 src/network/networkd.h            |    4 
 src/systemd/sd-dhcp-client.h      |    3 
 units/systemd-networkd.service.in |    1 
 10 files changed, 262 insertions(+), 156 deletions(-)

New commits:
commit 5d8364b3c5e06b8dbda75c07fdb707148dfd1656
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 20:20:08 2014 +0100

    networkd: resolv.conf - extend the comment a bit

diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 3b54214..27f806a 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -334,7 +334,11 @@ int manager_update_resolv_conf(Manager *m) {
 
         fchmod(fileno(f), 0644);
 
-        fputs("# This file is managed by systemd-networkd(8). Do not edit.\n", f);
+        fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n", f);
+        fputs("# Third party programs must not access this file directly, but\n", f);
+        fputs("# only through the symlink at /etc/resolv.conf. To manage your\n", f);
+        fputs("# own static resolv.conf(5), replace the symlink by a static\n", f);
+        fputs("# file at /etc/resolv.conf.\n\n", f);
 
         HASHMAP_FOREACH(link, m->links, i) {
                 if (link->dhcp) {

commit 5ee482dfd395b2576605bd18cda7d19abdb0487a
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 19:55:25 2014 +0100

    sd-dhcp-client: minor style fixes
    
    Also introduce a cleanup macro for DHCPLease

diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c
index c5bc115..26ed35e 100644
--- a/src/libsystemd/sd-dhcp-client.c
+++ b/src/libsystemd/sd-dhcp-client.c
@@ -87,8 +87,7 @@ static int client_receive_message(sd_event_source *s, int fd,
                                   uint32_t revents, void *userdata);
 
 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
-                                void *userdata)
-{
+                                void *userdata) {
         assert_return(client, -EINVAL);
 
         client->cb = cb;
@@ -97,8 +96,7 @@ int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
         return 0;
 }
 
-int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
-{
+int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
         size_t i;
 
         assert_return(client, -EINVAL);
@@ -130,8 +128,7 @@ int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
 }
 
 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
-                                       const struct in_addr *last_addr)
-{
+                                       const struct in_addr *last_addr) {
         assert_return(client, -EINVAL);
         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
 
@@ -143,8 +140,7 @@ int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
         return 0;
 }
 
-int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
-{
+int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
         assert_return(client, -EINVAL);
         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
         assert_return(interface_index >= -1, -EINVAL);
@@ -155,8 +151,7 @@ int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
 }
 
 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
-                           const struct ether_addr *addr)
-{
+                           const struct ether_addr *addr) {
         assert_return(client, -EINVAL);
         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
 
@@ -165,8 +160,7 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
         return 0;
 }
 
-int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
-{
+int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr) {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
 
@@ -189,8 +183,7 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
         return 0;
 }
 
-int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu)
-{
+int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu) {
         assert_return(client, -EINVAL);
         assert_return(mtu, -EINVAL);
 
@@ -216,8 +209,7 @@ int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu)
         return 0;
 }
 
-int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size)
-{
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size) {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
         assert_return(addr_size, -EINVAL);
@@ -245,8 +237,7 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t
         return 0;
 }
 
-int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname)
-{
+int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) {
         assert_return(client, -EINVAL);
         assert_return(hostname, -EINVAL);
 
@@ -272,8 +263,7 @@ int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname)
         return 0;
 }
 
-int sd_dhcp_client_prefixlen(const struct in_addr *addr)
-{
+int sd_dhcp_client_prefixlen(const struct in_addr *addr) {
         int len = 0;
         uint32_t mask;
 
@@ -288,8 +278,7 @@ int sd_dhcp_client_prefixlen(const struct in_addr *addr)
         return len;
 }
 
-int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
-{
+int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
 
@@ -312,8 +301,7 @@ int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
         return 0;
 }
 
-int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
-{
+int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
 
@@ -336,16 +324,26 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
         return 0;
 }
 
-static int client_notify(sd_dhcp_client *client, int event)
-{
+static int client_notify(sd_dhcp_client *client, int event) {
         if (client->cb)
                 client->cb(client, event, client->userdata);
 
         return 0;
 }
 
-static int client_stop(sd_dhcp_client *client, int error)
-{
+static void lease_free(DHCPLease *lease) {
+        if (!lease)
+                return;
+
+        free(lease->hostname);
+        free(lease->dns);
+        free(lease);
+}
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, lease_free);
+#define _cleanup_lease_free_ _cleanup_(lease_freep)
+
+static int client_stop(sd_dhcp_client *client, int error) {
         assert_return(client, -EINVAL);
 
         client->receive_message =
@@ -385,8 +383,7 @@ static int client_stop(sd_dhcp_client *client, int error)
         }
 
         if (client->lease) {
-                free(client->lease->dns);
-                free(client->lease);
+                lease_free(client->lease);
                 client->lease = NULL;
         }
 
@@ -395,8 +392,7 @@ static int client_stop(sd_dhcp_client *client, int error)
 
 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
                               DHCPMessage *message, uint16_t secs,
-                              uint8_t **opt, size_t *optlen)
-{
+                              uint8_t **opt, size_t *optlen) {
         int err;
         be16_t max_size;
 
@@ -462,8 +458,7 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type,
         return 0;
 }
 
-static uint16_t client_checksum(void *buf, int len)
-{
+static uint16_t client_checksum(void *buf, int len) {
         uint32_t sum;
         uint16_t *check;
         int i;
@@ -486,8 +481,7 @@ static uint16_t client_checksum(void *buf, int len)
         return ~sum;
 }
 
-static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
-{
+static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) {
         packet->ip.version = IPVERSION;
         packet->ip.ihl = DHCP_IP_SIZE / 4;
         packet->ip.tot_len = htobe16(len);
@@ -508,8 +502,7 @@ static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
         packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
 }
 
-static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
-{
+static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
         int err = 0;
         _cleanup_free_ DHCPPacket *discover;
         size_t optlen, len;
@@ -548,8 +541,7 @@ static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
         return err;
 }
 
-static int client_send_request(sd_dhcp_client *client, uint16_t secs)
-{
+static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
         _cleanup_free_ DHCPPacket *request;
         size_t optlen, len;
         int err;
@@ -601,8 +593,7 @@ static int client_send_request(sd_dhcp_client *client, uint16_t secs)
 }
 
 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
-                                 void *userdata)
-{
+                                 void *userdata) {
         sd_dhcp_client *client = userdata;
         usec_t next_timeout = 0;
         uint32_t time_left;
@@ -703,8 +694,7 @@ error:
         return 0;
 }
 
-static int client_initialize_events(sd_dhcp_client *client, usec_t usec)
-{
+static int client_initialize_events(sd_dhcp_client *client, usec_t usec) {
         int r;
 
         r = sd_event_add_io(client->event, client->fd, EPOLLIN,
@@ -726,8 +716,7 @@ error:
 }
 
 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
-                                 void *userdata)
-{
+                                 void *userdata) {
         sd_dhcp_client *client = userdata;
 
         client_stop(client, DHCP_EVENT_EXPIRED);
@@ -735,8 +724,7 @@ static int client_timeout_expire(sd_event_source *s, uint64_t usec,
         return 0;
 }
 
-static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
-{
+static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
         sd_dhcp_client *client = userdata;
         int r;
 
@@ -761,8 +749,7 @@ static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
         return client_initialize_events(client, usec);
 }
 
-static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
-{
+static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) {
         sd_dhcp_client *client = userdata;
         int r;
 
@@ -782,8 +769,7 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
 }
 
 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
-                              void *user_data)
-{
+                              void *user_data) {
         DHCPLease *lease = user_data;
         be32_t val;
 
@@ -875,8 +861,7 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
 }
 
 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
-                                 size_t len)
-{
+                                 size_t len) {
         size_t hdrlen;
 
         if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
@@ -912,23 +897,23 @@ static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
 }
 
 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
-                                size_t len)
-{
-        int err;
-        DHCPLease *lease;
+                                size_t len) {
+        _cleanup_lease_free_ DHCPLease *lease = NULL;
+        int r;
 
-        err = client_verify_headers(client, offer, len);
-        if (err < 0)
-                return err;
+        r = client_verify_headers(client, offer, len);
+        if (r < 0)
+                return r;
 
         lease = new0(DHCPLease, 1);
         if (!lease)
                 return -ENOMEM;
 
         len = len - DHCP_IP_UDP_SIZE;
-        if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
-                              lease) != DHCP_OFFER)
-                goto error;
+        r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
+                              lease);
+        if (r != DHCP_OFFER)
+                return -ENOMSG;
 
         lease->address = offer->dhcp.yiaddr;
 
@@ -936,25 +921,20 @@ static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
             lease->server_address == INADDR_ANY ||
             lease->subnet_mask == INADDR_ANY ||
             lease->lifetime == 0)
-                goto error;
+                return -ENOMSG;
 
         client->lease = lease;
+        lease = NULL;
 
         return 0;
-
-error:
-        free(lease);
-
-        return -ENOMSG;
 }
 
 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
-                              size_t len)
-{
-        int r;
+                              size_t len) {
         DHCPPacket *ack;
         DHCPMessage *dhcp;
-        DHCPLease *lease;
+        _cleanup_lease_free_ DHCPLease *lease = NULL;
+        int r;
 
         if (client->state == DHCP_STATE_RENEWING) {
                 dhcp = (DHCPMessage *)buf;
@@ -974,25 +954,18 @@ static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
                 return -ENOMEM;
 
         r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
+        if (r == DHCP_NAK)
+                return DHCP_EVENT_NO_LEASE;
 
-        if (r == DHCP_NAK) {
-                r = DHCP_EVENT_NO_LEASE;
-                goto error;
-        }
-
-        if (r != DHCP_ACK) {
-                r = -ENOMSG;
-                goto error;
-        }
+        if (r != DHCP_ACK)
+                return -ENOMSG;
 
         lease->address = dhcp->yiaddr;
 
         if (lease->address == INADDR_ANY ||
             lease->server_address == INADDR_ANY ||
-            lease->subnet_mask == INADDR_ANY || lease->lifetime == 0) {
-                r = -ENOMSG;
-                goto error;
-        }
+            lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
+                return -ENOMSG;
 
         r = DHCP_EVENT_IP_ACQUIRE;
         if (client->lease) {
@@ -1002,30 +975,24 @@ static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
                         r = DHCP_EVENT_IP_CHANGE;
                 }
 
-                free(client->lease);
+                lease_free(client->lease);
         }
 
         client->lease = lease;
-
-        return r;
-
-error:
-        free(lease);
+        lease = NULL;
 
         return r;
 }
 
 static uint64_t client_compute_timeout(uint64_t request_sent,
-                                       uint32_t lifetime)
-{
+                                       uint32_t lifetime) {
         return request_sent + (lifetime - 3) * USEC_PER_SEC +
                 + (random_u32() & 0x1fffff);
 }
 
-static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
-{
-        int err;
+static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
         uint64_t next_timeout;
+        int r;
 
         if (client->lease->lifetime < 10)
                 return -EINVAL;
@@ -1042,12 +1009,12 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
         if (next_timeout < usec)
                 return -EINVAL;
 
-        err = sd_event_add_monotonic(client->event, next_timeout,
+        r = sd_event_add_monotonic(client->event, next_timeout,
                                      10 * USEC_PER_MSEC,
                                      client_timeout_t1, client,
                                      &client->timeout_t1);
-        if (err < 0)
-                return err;
+        if (r < 0)
+                return r;
 
         if (!client->lease->t2)
                 client->lease->t2 = client->lease->lifetime * 7 / 8;
@@ -1063,31 +1030,30 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
         if (next_timeout < usec)
                 return -EINVAL;
 
-        err = sd_event_add_monotonic(client->event, next_timeout,
+        r = sd_event_add_monotonic(client->event, next_timeout,
                                      10 * USEC_PER_MSEC,
                                      client_timeout_t2, client,
                                      &client->timeout_t2);
-        if (err < 0)
-                return err;
+        if (r < 0)
+                return r;
 
         next_timeout = client_compute_timeout(client->request_sent,
                                               client->lease->lifetime);
         if (next_timeout < usec)
                 return -EINVAL;
 
-        err = sd_event_add_monotonic(client->event, next_timeout,
+        r = sd_event_add_monotonic(client->event, next_timeout,
                                      10 * USEC_PER_MSEC,
                                      client_timeout_expire, client,
                                      &client->timeout_expire);
-        if (err < 0)
-                return err;
+        if (r < 0)
+                return r;
 
         return 0;
 }
 
 static int client_receive_message(sd_event_source *s, int fd,
-                                  uint32_t revents, void *userdata)
-{
+                                  uint32_t revents, void *userdata) {
         sd_dhcp_client *client = userdata;
         uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
         int buflen = sizeof(buf);
@@ -1181,8 +1147,7 @@ error:
         return 0;
 }
 
-int sd_dhcp_client_start(sd_dhcp_client *client)
-{
+int sd_dhcp_client_start(sd_dhcp_client *client) {
         int r;
 
         assert_return(client, -EINVAL);
@@ -1205,13 +1170,11 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
         return client_initialize_events(client, client->start_time);
 }
 
-int sd_dhcp_client_stop(sd_dhcp_client *client)
-{
+int sd_dhcp_client_stop(sd_dhcp_client *client) {
         return client_stop(client, DHCP_EVENT_STOP);
 }
 
-sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
-{
+sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client) {
         assert_return(client, NULL);
 
         sd_dhcp_client_stop(client);
@@ -1223,8 +1186,7 @@ sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
         return NULL;
 }
 
-sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
-{
+sd_dhcp_client *sd_dhcp_client_new(sd_event *event) {
         sd_dhcp_client *client;
 
         assert_return(event, NULL);

commit 7ae4ef6d23bc884d0e0ee2ec9cf87ec99d0686bb
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 19:32:22 2014 +0100

    sd-dhcp-client: refactor DNS support
    
    Rather than keeping an array of pointers to addresses, just keep an array of addresses.

diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c
index c5d8371..c5bc115 100644
--- a/src/libsystemd/sd-dhcp-client.c
+++ b/src/libsystemd/sd-dhcp-client.c
@@ -41,7 +41,8 @@ struct DHCPLease {
         be32_t server_address;
         be32_t subnet_mask;
         be32_t router;
-        struct in_addr **dns;
+        struct in_addr *dns;
+        size_t dns_size;
         uint16_t mtu;
         char *hostname;
 };
@@ -215,10 +216,11 @@ int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu)
         return 0;
 }
 
-int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr)
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size)
 {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
+        assert_return(addr_size, -EINVAL);
 
         switch (client->state) {
         case DHCP_STATE_INIT:
@@ -231,9 +233,10 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr)
         case DHCP_STATE_BOUND:
         case DHCP_STATE_RENEWING:
         case DHCP_STATE_REBINDING:
-                if (client->lease->dns)
+                if (client->lease->dns_size) {
+                        *addr_size = client->lease->dns_size;
                         *addr = client->lease->dns;
-                else
+                } else
                         return -ENOENT;
 
                 break;
@@ -341,18 +344,6 @@ static int client_notify(sd_dhcp_client *client, int event)
         return 0;
 }
 
-static void in_addrs_free(struct in_addr **addrs) {
-        unsigned i;
-
-        if (!addrs)
-                return;
-
-        for (i = 0; addrs[i]; i++)
-                free(addrs[i]);
-
-        free(addrs);
-}
-
 static int client_stop(sd_dhcp_client *client, int error)
 {
         assert_return(client, -EINVAL);
@@ -394,7 +385,7 @@ static int client_stop(sd_dhcp_client *client, int error)
         }
 
         if (client->lease) {
-                in_addrs_free(client->lease->dns);
+                free(client->lease->dns);
                 free(client->lease);
                 client->lease = NULL;
         }
@@ -828,18 +819,16 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
                 if (len >= 4) {
                         unsigned i;
 
-                        in_addrs_free(lease->dns);
+                        lease->dns_size = len / 4;
 
-                        lease->dns = new0(struct in_addr*, len / 4 + 1);
+                        free(lease->dns);
+                        lease->dns = new0(struct in_addr, lease->dns_size);
                         if (!lease->dns)
                                 return -ENOMEM;
 
-                        for (i = 0; i < len / 4; i++) {
-                                lease->dns[i] = new0(struct in_addr, 1);
-                                memcpy(&lease->dns[i]->s_addr, option + 4 * i, 4);
+                        for (i = 0; i < lease->dns_size; i++) {
+                                memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
                         }
-
-                        lease->dns[len / 4] = NULL;
                 }
 
                 break;
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 444af8f..7f692d1 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -491,7 +491,8 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
         if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
                 _cleanup_address_free_ Address *addr = NULL;
                 _cleanup_route_free_ Route *rt = NULL;
-                struct in_addr **nameservers;
+                struct in_addr *nameservers;
+                size_t nameservers_size;
 
                 log_struct_link(LOG_INFO, link,
                                 "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
@@ -535,7 +536,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                 rt = NULL;
 
                 if (link->network->dhcp_dns) {
-                        r = sd_dhcp_client_get_dns(client, &nameservers);
+                        r = sd_dhcp_client_get_dns(client, &nameservers, &nameservers_size);
                         if (r >= 0) {
                                 r = manager_update_resolv_conf(link->manager);
                                 if (r < 0)
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index ca781e2..3b54214 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -338,14 +338,15 @@ int manager_update_resolv_conf(Manager *m) {
 
         HASHMAP_FOREACH(link, m->links, i) {
                 if (link->dhcp) {
-                        struct in_addr **nameservers;
+                        struct in_addr *nameservers;
+                        size_t nameservers_size;
 
-                        r = sd_dhcp_client_get_dns(link->dhcp, &nameservers);
+                        r = sd_dhcp_client_get_dns(link->dhcp, &nameservers, &nameservers_size);
                         if (r >= 0) {
                                 unsigned j;
 
-                                for (j = 0; nameservers[j]; j++)
-                                        append_dns(f, nameservers[j], AF_INET, &count);
+                                for (j = 0; j < nameservers_size; j++)
+                                        append_dns(f, &nameservers[j], AF_INET, &count);
                         }
                 }
         }
diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h
index e387d54..beb8642 100644
--- a/src/systemd/sd-dhcp-client.h
+++ b/src/systemd/sd-dhcp-client.h
@@ -54,7 +54,7 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_prefixlen(const struct in_addr *addr);
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
-int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr);
+int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size);
 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu);
 int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname);
 

commit 1346b1f0388f4100bb3c2a2bb23bc881769c020c
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Jan 13 23:48:28 2014 +0100

    sd-dhcp-client/networkd: add transient hostname support

diff --git a/man/systemd-networkd.service.xml b/man/systemd-networkd.service.xml
index 02a211e..2209737 100644
--- a/man/systemd-networkd.service.xml
+++ b/man/systemd-networkd.service.xml
@@ -260,6 +260,13 @@
                                                 from the DHCP server will be used on the current link.</para>
                                         </listitem>
                                 </varlistentry>
+                                <varlistentry>
+                                        <term><varname>UseHostname</varname></term>
+                                        <listitem>
+                                                <para>When true (the default) the hostname received from the DHCP server
+                                                will be used as the transient hostname.</para>
+                                        </listitem>
+                                </varlistentry>
                        </variablelist>
 
                 </refsect2>
diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c
index 76abcbd..c5d8371 100644
--- a/src/libsystemd/sd-dhcp-client.c
+++ b/src/libsystemd/sd-dhcp-client.c
@@ -43,6 +43,7 @@ struct DHCPLease {
         be32_t router;
         struct in_addr **dns;
         uint16_t mtu;
+        char *hostname;
 };
 
 typedef struct DHCPLease DHCPLease;
@@ -241,6 +242,33 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr)
         return 0;
 }
 
+int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname)
+{
+        assert_return(client, -EINVAL);
+        assert_return(hostname, -EINVAL);
+
+        switch (client->state) {
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+                return -EADDRNOTAVAIL;
+
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+                if (client->lease->hostname)
+                        *hostname = client->lease->hostname;
+                else
+                        return -ENOENT;
+
+                break;
+        }
+
+        return 0;
+}
+
 int sd_dhcp_client_prefixlen(const struct in_addr *addr)
 {
         int len = 0;
@@ -829,6 +857,14 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
 
                 break;
 
+        case DHCP_OPTION_HOST_NAME:
+                if (len >= 1) {
+                        free(lease->hostname);
+                        lease->hostname = strndup((const char *)option, len);
+                }
+
+                break;
+
         case DHCP_OPTION_RENEWAL_T1_TIME:
                 if (len == 4) {
                         memcpy(&val, option, 4);
diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf
index d4c7a26..7686cdf 100644
--- a/src/network/networkd-gperf.gperf
+++ b/src/network/networkd-gperf.gperf
@@ -32,5 +32,6 @@ Route.Gateway,           config_parse_gateway,          0,       0
 Route.Destination,       config_parse_destination,      0,       0
 DHCPv4.UseDNS,           config_parse_bool,             0,       offsetof(Network, dhcp_dns)
 DHCPv4.UseMTU,           config_parse_bool,             0,       offsetof(Network, dhcp_mtu)
+DHCPv4.UseHostname,      config_parse_bool,             0,       offsetof(Network, dhcp_hostname)
 Bridge.Description,      config_parse_string,           0,       offsetof(Bridge, description)
 Bridge.Name,             config_parse_ifname,           0,       offsetof(Bridge, name)
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 296886d..444af8f 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -25,6 +25,7 @@
 #include "networkd.h"
 #include "libudev-private.h"
 #include "util.h"
+#include "bus-util.h"
 
 int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         _cleanup_link_free_ Link *link = NULL;
@@ -310,6 +311,46 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat
         return 1;
 }
 
+static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
+        int r;
+
+        r = sd_bus_message_get_errno(m);
+        if (r < 0)
+                log_warning("Could not set hostname: %s", strerror(-r));
+
+        return 1;
+}
+
+static int set_hostname(sd_bus *bus, const char *hostname) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        int r = 0;
+
+        assert(bus);
+        assert(hostname);
+
+        log_debug("Setting transient hostname: '%s'", hostname);
+
+        r = sd_bus_message_new_method_call(
+                        bus,
+                        "org.freedesktop.hostname1",
+                        "/org/freedesktop/hostname1",
+                        "org.freedesktop.hostname1",
+                        "SetHostname",
+                        &m);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(m, "sb", hostname, false);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL);
+        if (r < 0)
+                log_error("Could not set transient hostname: %s", strerror(-r));
+
+        return r;
+}
+
 static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         Link *link = userdata;
         int r;
@@ -370,6 +411,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
 
         assert(link);
         assert(link->network);
+        assert(link->manager);
 
         if (link->state == LINK_STATE_FAILED)
                 return;
@@ -410,6 +452,12 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                                 }
                         }
                 }
+
+                if (link->network->dhcp_hostname) {
+                        r = set_hostname(link->manager->bus, "");
+                        if (r < 0)
+                                log_error("Failed to reset transient hostname");
+                }
         }
 
         r = sd_dhcp_client_get_address(client, &address);
@@ -507,6 +555,18 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                         }
                 }
 
+                if (link->network->dhcp_hostname) {
+                        const char *hostname;
+
+                        r = sd_dhcp_client_get_hostname(client, &hostname);
+                        if (r >= 0) {
+                                r = set_hostname(link->manager->bus, hostname);
+                                if (r < 0)
+                                        log_error("Failed to set transient hostname "
+                                                  "to '%s'", hostname);
+                        }
+                }
+
                 link_enter_set_addresses(link);
         }
 
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 90328a1..ca781e2 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -54,6 +54,10 @@ int manager_new(Manager **ret) {
         if (r < 0)
                 return r;
 
+        r = sd_bus_default_system(&m->bus);
+        if (r < 0)
+                return r;
+
         m->udev = udev_new();
         if (!m->udev)
                 return -ENOMEM;
@@ -85,6 +89,7 @@ void manager_free(Manager *m) {
 
         udev_monitor_unref(m->udev_monitor);
         udev_unref(m->udev);
+        sd_bus_unref(m->bus);
         sd_event_source_unref(m->udev_event_source);
         sd_event_unref(m->event);
 
@@ -280,6 +285,16 @@ int manager_rtnl_listen(Manager *m) {
         return 0;
 }
 
+int manager_bus_listen(Manager *m) {
+        int r;
+
+        r = sd_bus_attach_event(m->bus, m->event, 0);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
         char buf[INET6_ADDRSTRLEN];
         const char *address;
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 02ffdb1..ff54423 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -67,6 +67,7 @@ static int network_load_one(Manager *manager, const char *filename) {
 
         network->dhcp_dns = true;
         network->dhcp_mtu = true;
+        network->dhcp_hostname = true;
 
         r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
                         (void*) network_gperf_lookup, false, false, network);
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 0e38920..00e9a5f 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -70,6 +70,12 @@ int main(int argc, char *argv[]) {
                 goto out;
         }
 
+        r = manager_bus_listen(m);
+        if (r < 0) {
+                log_error("Could not connect to system bus: %s", strerror(-r));
+                goto out;
+        }
+
         /* write out empty resolv.conf to avoid a
          * dangling symlink */
         r = manager_update_resolv_conf(m);
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 5bd7e75..89f4cf2 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -26,6 +26,7 @@
 
 #include "sd-event.h"
 #include "sd-rtnl.h"
+#include "sd-bus.h"
 #include "sd-dhcp-client.h"
 #include "udev.h"
 
@@ -87,6 +88,7 @@ struct Network {
         bool dhcp;
         bool dhcp_dns;
         bool dhcp_mtu;
+        bool dhcp_hostname;
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
@@ -174,6 +176,7 @@ struct Link {
 struct Manager {
         sd_rtnl *rtnl;
         sd_event *event;
+        sd_bus *bus;
         struct udev *udev;
         struct udev_monitor *udev_monitor;
         sd_event_source *udev_event_source;
@@ -199,6 +202,7 @@ int manager_udev_enumerate_links(Manager *m);
 int manager_udev_listen(Manager *m);
 
 int manager_rtnl_listen(Manager *m);
+int manager_bus_listen(Manager *m);
 
 int manager_update_resolv_conf(Manager *m);
 
diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h
index a148084..e387d54 100644
--- a/src/systemd/sd-dhcp-client.h
+++ b/src/systemd/sd-dhcp-client.h
@@ -56,6 +56,7 @@ int sd_dhcp_client_prefixlen(const struct in_addr *addr);
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr);
 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu);
+int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname);
 
 int sd_dhcp_client_stop(sd_dhcp_client *client);
 int sd_dhcp_client_start(sd_dhcp_client *client);
diff --git a/units/systemd-networkd.service.in b/units/systemd-networkd.service.in
index c12f398..850193b 100644
--- a/units/systemd-networkd.service.in
+++ b/units/systemd-networkd.service.in
@@ -9,6 +9,7 @@
 Description=Network Service
 Documentation=man:systemd-networkd.service(8)
 DefaultDependencies=no
+After=dbus.socket
 Before=network.target
 Wants=network.target
 ConditionCapability=CAP_NET_ADMIN

commit 6fc73498945da749744041d4e10cf8dfac5c3bc6
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 16:50:19 2014 +0100

    networkd: dhcp - only set the MTU option once
    
    Only set MTU request when creating the dhcp client, not every time it is restarted.

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 6e0139c..296886d 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -538,16 +538,16 @@ static int link_acquire_conf(Link *link) {
                 r = sd_dhcp_client_set_callback(link->dhcp, dhcp_handler, link);
                 if (r < 0)
                         return r;
+
+                if (link->network->dhcp_mtu) {
+                        r = sd_dhcp_client_set_request_option(link->dhcp, 26);
+                        if (r < 0)
+                                return r;
+                }
         }
 
         log_debug_link(link, "acquiring DHCPv4 lease");
 
-        if (link->network->dhcp_mtu) {
-                r = sd_dhcp_client_set_request_option(link->dhcp, 26);
-                if (r < 0)
-                        return r;
-        }
-
         r = sd_dhcp_client_start(link->dhcp);
         if (r < 0)
                 return r;

commit 396945dc1f37fc95eb6681dfb62f328920079a55
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 15:09:57 2014 +0100

    networkd: link - only save original MTU when necessary

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 8960b60..6e0139c 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -828,9 +828,11 @@ int link_update(Link *link, sd_rtnl_message *m) {
         }
 
         while (sd_rtnl_message_read(m, &type, &data) > 0) {
-                if (type == IFLA_MTU && link->network->dhcp_mtu && !link->original_mtu) {
+                if (type == IFLA_MTU && link->network->dhcp &&
+                    link->network->dhcp_mtu && !link->original_mtu) {
                         link->original_mtu = *(uint16_t *) data;
-                        log_debug_link(link, "saved original MTU: %" PRIu16, link->original_mtu);
+                        log_debug_link(link, "saved original MTU: %" PRIu16,
+                                       link->original_mtu);
                 }
         }
 

commit 1f6d9bc9ee894d4023b7eacea1754986f25bce57
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Jan 16 14:59:26 2014 +0100

    networkd: improve logging

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 993931d..8960b60 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -580,6 +580,7 @@ static int link_update_flags(Link *link, unsigned flags) {
                         if (link->network->dhcp) {
                                 r = link_acquire_conf(link);
                                 if (r < 0) {
+                                        log_warning_link(link, "Could not acquire DHCPv4 lease: %s", strerror(-r));
                                         link_enter_failed(link);
                                         return r;
                                 }
@@ -590,6 +591,7 @@ static int link_update_flags(Link *link, unsigned flags) {
                         if (link->network->dhcp) {
                                 r = sd_dhcp_client_stop(link->dhcp);
                                 if (r < 0) {
+                                        log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
                                         link_enter_failed(link);
                                         return r;
                                 }
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 055200c..0e38920 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -41,39 +41,52 @@ int main(int argc, char *argv[]) {
         }
 
         r = manager_new(&m);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Could not create manager: %s", strerror(-r));
                 goto out;
+        }
 
         r = manager_load_config(m);
-        if (r < 0)
-                return EXIT_FAILURE;
+        if (r < 0) {
+                log_error("Could not load configuration files: %s", strerror(-r));
+                goto out;
+        }
 
         r = manager_udev_listen(m);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Could not connect to udev: %s", strerror(-r));
                 goto out;
+        }
 
         r = manager_udev_enumerate_links(m);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Could not enumerate links: %s", strerror(-r));
                 goto out;
+        }
 
         r = manager_rtnl_listen(m);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Could not connect to rtnl: %s", strerror(-r));
                 goto out;
-
+        }
 
         /* write out empty resolv.conf to avoid a
          * dangling symlink */
         r = manager_update_resolv_conf(m);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Could not create resolv.conf: %s", strerror(-r));
                 goto out;
+        }
 
         sd_notify(false,
                   "READY=1\n"
                   "STATUS=Processing requests...");
 
         r = sd_event_loop(m->event);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Event loop failed: %s", strerror(-r));
                 goto out;
+        }
 
 out:
         sd_notify(false,



More information about the systemd-commits mailing list