]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp-client: use unaligned_be32_sec_to_usec()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 6 Sep 2023 19:33:05 +0000 (04:33 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 21 Sep 2023 08:52:03 +0000 (09:52 +0100)
Note, previously, we set 1 second if the time value is zero.
But the adjustment is dropped now, as for the lifetime we have explicit
check that the message has non-zero lifetime, and for T1 and T2 we have
better adjustment in client_set_lease_timeouts().

src/libsystemd-network/sd-dhcp-lease.c

index b8be34a9f43047c1d039ddc1bc5b4c0088defb31..e80425a34eada632105763e80cbd984f27b1eb0b 100644 (file)
@@ -420,36 +420,14 @@ static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) {
 
 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free);
 
-static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) {
+static int lease_parse_be32_seconds(const uint8_t *option, size_t len, bool max_as_infinity, usec_t *ret) {
         assert(option);
         assert(ret);
 
         if (len != 4)
                 return -EINVAL;
 
-        *ret = unaligned_read_be32((be32_t*) option);
-        if (*ret < min)
-                *ret = min;
-
-        return 0;
-}
-
-static int lease_parse_u32_seconds(const uint8_t *option, size_t len, usec_t *ret) {
-        uint32_t val;
-        int r;
-
-        assert(option);
-        assert(ret);
-
-        r = lease_parse_u32(option, len, &val, 1);
-        if (r < 0)
-                return r;
-
-        if (val == UINT32_MAX)
-                *ret = USEC_INFINITY;
-        else
-                *ret = val * USEC_PER_SEC;
-
+        *ret = unaligned_be32_sec_to_usec(option, max_as_infinity);
         return 0;
 }
 
@@ -722,7 +700,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void
         switch (code) {
 
         case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
-                r = lease_parse_u32_seconds(option, len, &lease->lifetime);
+                r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->lifetime);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse lease time, ignoring: %m");
 
@@ -847,13 +825,13 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void
                 break;
 
         case SD_DHCP_OPTION_RENEWAL_TIME:
-                r = lease_parse_u32_seconds(option, len, &lease->t1);
+                r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->t1);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse T1 time, ignoring: %m");
                 break;
 
         case SD_DHCP_OPTION_REBINDING_TIME:
-                r = lease_parse_u32_seconds(option, len, &lease->t2);
+                r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->t2);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse T2 time, ignoring: %m");
                 break;