From: Yu Watanabe Date: Tue, 5 Sep 2023 06:50:23 +0000 (+0900) Subject: sd-dhcp6-client: use be32_sec_to_usec() at more places X-Git-Tag: v255-rc1~470^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9132cbd5adeae45817fb253a45f7b2b09358d6db;p=thirdparty%2Fsystemd.git sd-dhcp6-client: use be32_sec_to_usec() at more places --- diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index 76db8717b23..83f40f3f022 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -15,6 +15,7 @@ #include "dns-domain.h" #include "escape.h" #include "memory-util.h" +#include "network-common.h" #include "strv.h" #include "unaligned.h" @@ -585,7 +586,7 @@ static int dhcp6_option_parse_ia_options(sd_dhcp6_client *client, const uint8_t static int dhcp6_option_parse_ia_address(sd_dhcp6_client *client, DHCP6IA *ia, const uint8_t *data, size_t len) { _cleanup_free_ DHCP6Address *a = NULL; - uint32_t lt_valid, lt_pref; + usec_t lt_valid, lt_pref; int r; assert(ia); @@ -604,17 +605,18 @@ static int dhcp6_option_parse_ia_address(sd_dhcp6_client *client, DHCP6IA *ia, c memcpy(&a->iaaddr, data, sizeof(struct iaaddr)); - lt_valid = be32toh(a->iaaddr.lifetime_valid); - lt_pref = be32toh(a->iaaddr.lifetime_preferred); + lt_valid = be32_sec_to_usec(a->iaaddr.lifetime_valid, /* max_as_infinity = */ true); + lt_pref = be32_sec_to_usec(a->iaaddr.lifetime_preferred, /* max_as_infinity = */ true); if (lt_valid == 0) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "Received an IA address with zero valid lifetime, ignoring."); if (lt_pref > lt_valid) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), - "Received an IA address with preferred lifetime %"PRIu32 - " larger than valid lifetime %"PRIu32", ignoring.", - lt_pref, lt_valid); + "Received an IA address with preferred lifetime %s " + "larger than valid lifetime %s, ignoring.", + FORMAT_TIMESPAN(lt_pref, USEC_PER_SEC), + FORMAT_TIMESPAN(lt_valid, USEC_PER_SEC)); if (len > sizeof(struct iaaddr)) { r = dhcp6_option_parse_ia_options(client, data + sizeof(struct iaaddr), len - sizeof(struct iaaddr)); @@ -628,7 +630,7 @@ static int dhcp6_option_parse_ia_address(sd_dhcp6_client *client, DHCP6IA *ia, c static int dhcp6_option_parse_ia_pdprefix(sd_dhcp6_client *client, DHCP6IA *ia, const uint8_t *data, size_t len) { _cleanup_free_ DHCP6Address *a = NULL; - uint32_t lt_valid, lt_pref; + usec_t lt_valid, lt_pref; int r; assert(ia); @@ -647,17 +649,18 @@ static int dhcp6_option_parse_ia_pdprefix(sd_dhcp6_client *client, DHCP6IA *ia, memcpy(&a->iapdprefix, data, sizeof(struct iapdprefix)); - lt_valid = be32toh(a->iapdprefix.lifetime_valid); - lt_pref = be32toh(a->iapdprefix.lifetime_preferred); + lt_valid = be32_sec_to_usec(a->iapdprefix.lifetime_valid, /* max_as_infinity = */ true); + lt_pref = be32_sec_to_usec(a->iapdprefix.lifetime_preferred, /* max_as_infinity = */ true); if (lt_valid == 0) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "Received a PD prefix with zero valid lifetime, ignoring."); if (lt_pref > lt_valid) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), - "Received a PD prefix with preferred lifetime %"PRIu32 - " larger than valid lifetime %"PRIu32", ignoring.", - lt_pref, lt_valid); + "Received a PD prefix with preferred lifetime %s " + "larger than valid lifetime %s, ignoring.", + FORMAT_TIMESPAN(lt_pref, USEC_PER_SEC), + FORMAT_TIMESPAN(lt_valid, USEC_PER_SEC)); if (len > sizeof(struct iapdprefix)) { r = dhcp6_option_parse_ia_options(client, data + sizeof(struct iapdprefix), len - sizeof(struct iapdprefix)); @@ -678,7 +681,7 @@ int dhcp6_option_parse_ia( DHCP6IA **ret) { _cleanup_(dhcp6_ia_freep) DHCP6IA *ia = NULL; - uint32_t lt_t1, lt_t2; + usec_t lt_t1, lt_t2; size_t header_len; int r; @@ -728,17 +731,18 @@ int dhcp6_option_parse_ia( "from the one chosen by the client, ignoring."); /* It is not necessary to check if the lifetime_t2 is zero here, as in that case it will be updated later. */ - lt_t1 = be32toh(ia->header.lifetime_t1); - lt_t2 = be32toh(ia->header.lifetime_t2); + lt_t1 = be32_sec_to_usec(ia->header.lifetime_t1, /* max_as_infinity = */ true); + lt_t2 = be32_sec_to_usec(ia->header.lifetime_t2, /* max_as_infinity = */ true); if (lt_t1 > lt_t2) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), - "Received an IA option with T1 %"PRIu32"sec > T2 %"PRIu32"sec, ignoring.", - lt_t1, lt_t2); + "Received an IA option with T1 %s > T2 %s, ignoring.", + FORMAT_TIMESPAN(lt_t1, USEC_PER_SEC), + FORMAT_TIMESPAN(lt_t2, USEC_PER_SEC)); if (lt_t1 == 0 && lt_t2 > 0) return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), - "Received an IA option with zero T1 and non-zero T2 (%"PRIu32"sec), ignoring.", - lt_t2); + "Received an IA option with zero T1 and non-zero T2 (%s), ignoring.", + FORMAT_TIMESPAN(lt_t2, USEC_PER_SEC)); for (size_t offset = header_len; offset < option_data_len;) { const uint8_t *subdata; diff --git a/src/libsystemd-network/sd-dhcp6-lease.c b/src/libsystemd-network/sd-dhcp6-lease.c index 2be084d45cd..db9b471961d 100644 --- a/src/libsystemd-network/sd-dhcp6-lease.c +++ b/src/libsystemd-network/sd-dhcp6-lease.c @@ -36,30 +36,26 @@ int sd_dhcp6_lease_get_timestamp(sd_dhcp6_lease *lease, clockid_t clock, uint64_ return 0; } -static usec_t sec2usec(uint32_t sec) { - return sec == UINT32_MAX ? USEC_INFINITY : sec * USEC_PER_SEC; -} - static void dhcp6_lease_set_lifetime(sd_dhcp6_lease *lease) { - uint32_t t1 = UINT32_MAX, t2 = UINT32_MAX, min_valid_lt = UINT32_MAX; + usec_t t1 = USEC_INFINITY, t2 = USEC_INFINITY, min_valid_lt = USEC_INFINITY; assert(lease); assert(lease->ia_na || lease->ia_pd); if (lease->ia_na) { - t1 = MIN(t1, be32toh(lease->ia_na->header.lifetime_t1)); - t2 = MIN(t2, be32toh(lease->ia_na->header.lifetime_t2)); + t1 = MIN(t1, be32_sec_to_usec(lease->ia_na->header.lifetime_t1, /* max_as_infinity = */ true)); + t2 = MIN(t2, be32_sec_to_usec(lease->ia_na->header.lifetime_t2, /* max_as_infinity = */ true)); LIST_FOREACH(addresses, a, lease->ia_na->addresses) - min_valid_lt = MIN(min_valid_lt, be32toh(a->iaaddr.lifetime_valid)); + min_valid_lt = MIN(min_valid_lt, be32_sec_to_usec(a->iaaddr.lifetime_valid, /* max_as_infinity = */ true)); } if (lease->ia_pd) { - t1 = MIN(t1, be32toh(lease->ia_pd->header.lifetime_t1)); - t2 = MIN(t2, be32toh(lease->ia_pd->header.lifetime_t2)); + t1 = MIN(t1, be32_sec_to_usec(lease->ia_pd->header.lifetime_t1, /* max_as_infinity = */ true)); + t2 = MIN(t2, be32_sec_to_usec(lease->ia_pd->header.lifetime_t2, /* max_as_infinity = */ true)); LIST_FOREACH(addresses, a, lease->ia_pd->addresses) - min_valid_lt = MIN(min_valid_lt, be32toh(a->iapdprefix.lifetime_valid)); + min_valid_lt = MIN(min_valid_lt, be32_sec_to_usec(a->iapdprefix.lifetime_valid, /* max_as_infinity = */ true)); } if (t2 == 0 || t2 > min_valid_lt) { @@ -69,9 +65,9 @@ static void dhcp6_lease_set_lifetime(sd_dhcp6_lease *lease) { t2 = min_valid_lt / 10 * 8; } - lease->lifetime_valid = sec2usec(min_valid_lt); - lease->lifetime_t1 = sec2usec(t1); - lease->lifetime_t2 = sec2usec(t2); + lease->lifetime_valid = min_valid_lt; + lease->lifetime_t1 = t1; + lease->lifetime_t2 = t2; } int dhcp6_lease_get_lifetime(sd_dhcp6_lease *lease, usec_t *ret_t1, usec_t *ret_t2, usec_t *ret_valid) { @@ -747,7 +743,7 @@ static int dhcp6_lease_parse_message( return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "Received information refresh time option with an invalid length (%zu).", optlen); - irt = unaligned_read_be32(optval) * USEC_PER_SEC; + irt = unaligned_be32_sec_to_usec(optval, /* max_as_infinity = */ false); break; } }