chiark / gitweb /
sd-dhcp-lease: add sd_dhcp_lease_get_next_server()
authorTom Gundersen <teg@jklm.no>
Mon, 3 Mar 2014 16:13:59 +0000 (17:13 +0100)
committerTom Gundersen <teg@jklm.no>
Mon, 3 Mar 2014 16:18:22 +0000 (17:18 +0100)
src/libsystemd-network/dhcp-lease-internal.h
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/sd-dhcp-lease.c
src/systemd/sd-dhcp-lease.h

index 1b157fd..70f1aa1 100644 (file)
@@ -41,6 +41,7 @@ struct sd_dhcp_lease {
         be32_t server_address;
         be32_t subnet_mask;
         be32_t router;
+        be32_t next_server;
         struct in_addr *dns;
         size_t dns_size;
         uint16_t mtu;
index 1f676cc..b6810a9 100644 (file)
@@ -587,6 +587,8 @@ static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
         if (r != DHCP_OFFER)
                 return -ENOMSG;
 
+        lease->next_server = offer->siaddr;
+
         lease->address = offer->yiaddr;
 
         if (lease->address == INADDR_ANY ||
@@ -621,6 +623,8 @@ static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
         if (r != DHCP_ACK)
                 return -ENOMSG;
 
+        lease->next_server = ack->siaddr;
+
         lease->address = ack->yiaddr;
 
         if (lease->address == INADDR_ANY ||
index 6bd9728..f7a204a 100644 (file)
@@ -135,6 +135,15 @@ int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *ad
         return 0;
 }
 
+int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) {
+        assert_return(lease, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        addr->s_addr = lease->next_server;
+
+        return 0;
+}
+
 sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
         if (lease)
                 assert_se(REFCNT_INC(lease->n_ref) >= 2);
@@ -350,6 +359,18 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
                         "SERVER_ADDRESS=%s\n", string);
         }
 
+        r = sd_dhcp_lease_get_next_server(lease, &address);
+        if (r >= 0) {
+                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
+                if (!string) {
+                        r = -errno;
+                        goto finish;
+                }
+
+                fprintf(f,
+                        "NEXT_SERVER=%s\n", string);
+        }
+
         r = sd_dhcp_lease_get_mtu(lease, &mtu);
         if (r >= 0)
                 fprintf(f, "MTU=%" PRIu16 "\n", mtu);
@@ -388,7 +409,8 @@ finish:
 int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
         _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
-                            *server_address = NULL, *mtu = NULL;
+                            *server_address = NULL, *next_server = NULL,
+                            *mtu = NULL;
         struct in_addr addr;
         int r;
 
@@ -404,6 +426,7 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
                            "ROUTER", &router,
                            "NETMASK", &netmask,
                            "SERVER_IDENTIFIER", &server_address,
+                           "NEXT_SERVER", &next_server,
                            "MTU", &mtu,
                            "DOMAINNAME", &lease->domainname,
                            "HOSTNAME", &lease->hostname,
@@ -443,6 +466,14 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
                 lease->server_address = addr.s_addr;
         }
 
+        if (next_server) {
+                r = inet_pton(AF_INET, next_server, &addr);
+                if (r < 0)
+                        return r;
+
+                lease->next_server = addr.s_addr;
+        }
+
         if (mtu) {
                 uint16_t u;
                 if (sscanf(mtu, "%" SCNu16, &u) > 0)
index 7e737a9..f3595eb 100644 (file)
@@ -33,6 +33,7 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease);
 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr);
+int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size);
 int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu);