]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp6: fix check if serverid is set
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 15 Feb 2018 08:37:44 +0000 (09:37 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 15 Feb 2018 09:04:02 +0000 (10:04 +0100)
Ever since the initial implementation in 631bbe71298ec892f77f44f94feb612646fe6853,
client_parse_message() was supposed to check that the message contains exactly
one serverid. The check that no more than one is given was implemented
correctly, but the check that at least one is given was not. Simplify the whole
thing by making dhcp6_lease_get_serverid() return an error if the id is not
set, and do not require the arguments to be present if the contents of the id
are not needed.

src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd-network/sd-dhcp6-lease.c

index 2d38e51424fc217fe057507d6da9c21ba60caba1..056bfa3d3d98c9373068b51dcfdd896d9ef97ea2 100644 (file)
@@ -771,8 +771,6 @@ static int client_parse_message(
         size_t pos = 0;
         int r;
         bool clientid = false;
-        uint8_t *id = NULL;
-        size_t id_len;
         uint32_t lt_t1 = ~0, lt_t2 = ~0;
 
         assert(client);
@@ -817,8 +815,8 @@ static int client_parse_message(
                         break;
 
                 case SD_DHCP6_OPTION_SERVERID:
-                        r = dhcp6_lease_get_serverid(lease, &id, &id_len);
-                        if (r >= 0 && id) {
+                        r = dhcp6_lease_get_serverid(lease, NULL, NULL);
+                        if (r >= 0) {
                                 log_dhcp6_client(client, "%s contains multiple serverids",
                                                  dhcp6_message_type_to_string(message->type));
                                 return -EINVAL;
@@ -956,21 +954,23 @@ static int client_parse_message(
         }
 
         if (client->state != DHCP6_STATE_INFORMATION_REQUEST) {
-                r = dhcp6_lease_get_serverid(lease, &id, &id_len);
-                if (r < 0)
+                r = dhcp6_lease_get_serverid(lease, NULL, NULL);
+                if (r < 0) {
                         log_dhcp6_client(client, "%s has no server id",
                                          dhcp6_message_type_to_string(message->type));
-                return r;
-        }
+                        return -EINVAL;
+                }
 
-        if (lease->ia.addresses) {
-                lease->ia.ia_na.lifetime_t1 = htobe32(lt_t1);
-                lease->ia.ia_na.lifetime_t2 = htobe32(lt_t2);
-        }
+        } else {
+                if (lease->ia.addresses) {
+                        lease->ia.ia_na.lifetime_t1 = htobe32(lt_t1);
+                        lease->ia.ia_na.lifetime_t2 = htobe32(lt_t2);
+                }
 
-        if (lease->pd.addresses) {
-                lease->pd.ia_pd.lifetime_t1 = htobe32(lt_t1);
-                lease->pd.ia_pd.lifetime_t2 = htobe32(lt_t2);
+                if (lease->pd.addresses) {
+                        lease->pd.ia_pd.lifetime_t1 = htobe32(lt_t1);
+                        lease->pd.ia_pd.lifetime_t2 = htobe32(lt_t2);
+                }
         }
 
         return 0;
index 1f3a782f8c0a692f9de204ce38b93efe8abcacae..0089fbb7370d14fa3645b231dead010a5f292873 100644 (file)
@@ -95,11 +95,14 @@ int dhcp6_lease_set_serverid(sd_dhcp6_lease *lease, const uint8_t *id,
 
 int dhcp6_lease_get_serverid(sd_dhcp6_lease *lease, uint8_t **id, size_t *len) {
         assert_return(lease, -EINVAL);
-        assert_return(id, -EINVAL);
-        assert_return(len, -EINVAL);
 
-        *id = lease->serverid;
-        *len = lease->serverid_len;
+        if (!lease->serverid)
+                return -ENOMSG;
+
+        if (id)
+                *id = lease->serverid;
+        if (len)
+                *len = lease->serverid_len;
 
         return 0;
 }