diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-09-21 19:18:06 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-09-22 18:34:56 +0200 |
commit | 6197db53ba3c61de2268eb723a7a9cd4b3f5f87c (patch) | |
tree | 794fa7aea81507c85c179e0bb4c18143fce7cf43 /src/libsystemd-network | |
parent | sd-ndisc: rename function arguments for storing results (diff) | |
download | systemd-6197db53ba3c61de2268eb723a7a9cd4b3f5f87c.tar.xz systemd-6197db53ba3c61de2268eb723a7a9cd4b3f5f87c.zip |
sd-ndisc: make sd_ndisc return time values in usec
This also introduces sd_ndisc_router_get_lifetime_timestamp() and
friends that return timestamp rather than timespan.
Diffstat (limited to 'src/libsystemd-network')
-rw-r--r-- | src/libsystemd-network/ndisc-router.c | 68 | ||||
-rw-r--r-- | src/libsystemd-network/ndisc-router.h | 4 | ||||
-rw-r--r-- | src/libsystemd-network/sd-ndisc.c | 4 | ||||
-rw-r--r-- | src/libsystemd-network/test-ndisc-rs.c | 30 |
4 files changed, 70 insertions, 36 deletions
diff --git a/src/libsystemd-network/ndisc-router.c b/src/libsystemd-network/ndisc-router.c index f03a9f18dd..74331591f0 100644 --- a/src/libsystemd-network/ndisc-router.c +++ b/src/libsystemd-network/ndisc-router.c @@ -59,6 +59,38 @@ int sd_ndisc_router_get_timestamp(sd_ndisc_router *rt, clockid_t clock, uint64_t return 0; } +#define DEFINE_GET_TIMESTAMP(name) \ + int sd_ndisc_router_##name##_timestamp( \ + sd_ndisc_router *rt, \ + clockid_t clock, \ + uint64_t *ret) { \ + \ + usec_t s, t; \ + int r; \ + \ + assert_return(rt, -EINVAL); \ + assert_return(ret, -EINVAL); \ + \ + r = sd_ndisc_router_##name(rt, &s); \ + if (r < 0) \ + return r; \ + \ + r = sd_ndisc_router_get_timestamp(rt, clock, &t); \ + if (r < 0) \ + return r; \ + \ + *ret = time_span_to_stamp(s, t); \ + return 0; \ + } + +DEFINE_GET_TIMESTAMP(get_lifetime); +DEFINE_GET_TIMESTAMP(prefix_get_valid_lifetime); +DEFINE_GET_TIMESTAMP(prefix_get_preferred_lifetime); +DEFINE_GET_TIMESTAMP(route_get_lifetime); +DEFINE_GET_TIMESTAMP(rdnss_get_lifetime); +DEFINE_GET_TIMESTAMP(dnssl_get_lifetime); +DEFINE_GET_TIMESTAMP(prefix64_get_lifetime); + int sd_ndisc_router_get_raw(sd_ndisc_router *rt, const void **ret, size_t *ret_size) { assert_return(rt, -EINVAL); assert_return(ret, -EINVAL); @@ -110,8 +142,8 @@ int ndisc_router_parse(sd_ndisc *nd, sd_ndisc_router *rt) { rt->hop_limit = a->nd_ra_curhoplimit; rt->flags = a->nd_ra_flags_reserved; /* the first 8 bits */ - rt->lifetime = be16toh(a->nd_ra_router_lifetime); - rt->icmp6_ratelimit_msec = be32toh(a->nd_ra_retransmit); + rt->lifetime_usec = be16_sec_to_usec(a->nd_ra_router_lifetime, /* max_as_infinity = */ false); + rt->icmp6_ratelimit_usec = be32_msec_to_usec(a->nd_ra_retransmit, /* max_as_infinity = */ false); rt->preference = (rt->flags >> 3) & 3; if (!IN_SET(rt->preference, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_HIGH)) @@ -243,11 +275,11 @@ int sd_ndisc_router_get_hop_limit(sd_ndisc_router *rt, uint8_t *ret) { return 0; } -int sd_ndisc_router_get_icmp6_ratelimit(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_get_icmp6_ratelimit(sd_ndisc_router *rt, uint64_t *ret) { assert_return(rt, -EINVAL); assert_return(ret, -EINVAL); - *ret = rt->icmp6_ratelimit_msec; + *ret = rt->icmp6_ratelimit_usec; return 0; } @@ -259,11 +291,11 @@ int sd_ndisc_router_get_flags(sd_ndisc_router *rt, uint64_t *ret) { return 0; } -int sd_ndisc_router_get_lifetime(sd_ndisc_router *rt, uint16_t *ret) { +int sd_ndisc_router_get_lifetime(sd_ndisc_router *rt, uint64_t *ret) { assert_return(rt, -EINVAL); assert_return(ret, -EINVAL); - *ret = rt->lifetime; + *ret = rt->lifetime_usec; return 0; } @@ -389,7 +421,7 @@ static int get_prefix_info(sd_ndisc_router *rt, struct nd_opt_prefix_info **ret) return 0; } -int sd_ndisc_router_prefix_get_valid_lifetime(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_prefix_get_valid_lifetime(sd_ndisc_router *rt, uint64_t *ret) { struct nd_opt_prefix_info *ri; int r; @@ -400,11 +432,11 @@ int sd_ndisc_router_prefix_get_valid_lifetime(sd_ndisc_router *rt, uint32_t *ret if (r < 0) return r; - *ret = be32toh(ri->nd_opt_pi_valid_time); + *ret = be32_sec_to_usec(ri->nd_opt_pi_valid_time, /* max_as_infinity = */ true); return 0; } -int sd_ndisc_router_prefix_get_preferred_lifetime(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_prefix_get_preferred_lifetime(sd_ndisc_router *rt, uint64_t *ret) { struct nd_opt_prefix_info *pi; int r; @@ -415,7 +447,7 @@ int sd_ndisc_router_prefix_get_preferred_lifetime(sd_ndisc_router *rt, uint32_t if (r < 0) return r; - *ret = be32toh(pi->nd_opt_pi_preferred_time); + *ret = be32_sec_to_usec(pi->nd_opt_pi_preferred_time, /* max_as_infinity = */ true); return 0; } @@ -502,7 +534,7 @@ static int get_route_info(sd_ndisc_router *rt, uint8_t **ret) { return 0; } -int sd_ndisc_router_route_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_route_get_lifetime(sd_ndisc_router *rt, uint64_t *ret) { uint8_t *ri; int r; @@ -513,7 +545,7 @@ int sd_ndisc_router_route_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { if (r < 0) return r; - *ret = be32toh(*(uint32_t*) (ri + 4)); + *ret = unaligned_be32_sec_to_usec(ri + 4, /* max_as_infinity = */ true); return 0; } @@ -603,7 +635,7 @@ int sd_ndisc_router_rdnss_get_addresses(sd_ndisc_router *rt, const struct in6_ad return (NDISC_ROUTER_OPTION_LENGTH(rt) - 8) / 16; } -int sd_ndisc_router_rdnss_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_rdnss_get_lifetime(sd_ndisc_router *rt, uint64_t *ret) { uint8_t *ri; int r; @@ -614,7 +646,7 @@ int sd_ndisc_router_rdnss_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { if (r < 0) return r; - *ret = be32toh(*(uint32_t*) (ri + 4)); + *ret = unaligned_be32_sec_to_usec(ri + 4, /* max_as_infinity = */ true); return 0; } @@ -731,7 +763,7 @@ int sd_ndisc_router_dnssl_get_domains(sd_ndisc_router *rt, char ***ret) { return k; } -int sd_ndisc_router_dnssl_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { +int sd_ndisc_router_dnssl_get_lifetime(sd_ndisc_router *rt, uint64_t *ret) { uint8_t *ri; int r; @@ -742,7 +774,7 @@ int sd_ndisc_router_dnssl_get_lifetime(sd_ndisc_router *rt, uint32_t *ret) { if (r < 0) return r; - *ret = be32toh(*(uint32_t*) (ri + 4)); + *ret = unaligned_be32_sec_to_usec(ri + 4, /* max_as_infinity = */ true); return 0; } @@ -862,7 +894,7 @@ int sd_ndisc_router_prefix64_get_prefixlen(sd_ndisc_router *rt, unsigned *ret) { return 0; } -int sd_ndisc_router_prefix64_get_lifetime_sec(sd_ndisc_router *rt, uint16_t *ret) { +int sd_ndisc_router_prefix64_get_lifetime(sd_ndisc_router *rt, uint64_t *ret) { struct nd_opt_prefix64_info *pi; uint16_t lifetime_prefix_len; int r; @@ -876,6 +908,6 @@ int sd_ndisc_router_prefix64_get_lifetime_sec(sd_ndisc_router *rt, uint16_t *ret lifetime_prefix_len = be16toh(pi->lifetime_and_plc); - *ret = lifetime_prefix_len & PREF64_SCALED_LIFETIME_MASK; + *ret = (lifetime_prefix_len & PREF64_SCALED_LIFETIME_MASK) * USEC_PER_SEC; return 0; } diff --git a/src/libsystemd-network/ndisc-router.h b/src/libsystemd-network/ndisc-router.h index cf702f52c3..0a55e1ac57 100644 --- a/src/libsystemd-network/ndisc-router.h +++ b/src/libsystemd-network/ndisc-router.h @@ -23,11 +23,11 @@ struct sd_ndisc_router { uint64_t flags; unsigned preference; - uint16_t lifetime; + uint64_t lifetime_usec; uint8_t hop_limit; uint32_t mtu; - uint32_t icmp6_ratelimit_msec; + uint64_t icmp6_ratelimit_usec; }; static inline void* NDISC_ROUTER_RAW(const sd_ndisc_router *rt) { diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index d7d9e7e2d9..1beed5d0ce 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -189,10 +189,10 @@ static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) { if (r < 0) return r; - log_ndisc(nd, "Received Router Advertisement: flags %s preference %s lifetime %" PRIu16 " sec", + log_ndisc(nd, "Received Router Advertisement: flags %s preference %s lifetime %s", rt->flags & ND_RA_FLAG_MANAGED ? "MANAGED" : rt->flags & ND_RA_FLAG_OTHER ? "OTHER" : "none", rt->preference == SD_NDISC_PREFERENCE_HIGH ? "high" : rt->preference == SD_NDISC_PREFERENCE_LOW ? "low" : "medium", - rt->lifetime); + FORMAT_TIMESPAN(rt->lifetime_usec, USEC_PER_SEC)); ndisc_callback(nd, SD_NDISC_EVENT_ROUTER, rt); return 0; diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c index 09e292a2ec..d94cc1ceb7 100644 --- a/src/libsystemd-network/test-ndisc-rs.c +++ b/src/libsystemd-network/test-ndisc-rs.c @@ -28,9 +28,9 @@ static sd_ndisc *test_timeout_nd; static void router_dump(sd_ndisc_router *rt) { struct in6_addr addr; uint8_t hop_limit; - uint64_t t, flags; + usec_t t, lifetime; + uint64_t flags; uint32_t mtu; - uint16_t lifetime; unsigned preference; int r; @@ -62,7 +62,8 @@ static void router_dump(sd_ndisc_router *rt) { preference == SD_NDISC_PREFERENCE_HIGH ? "high" : "medium"); assert_se(sd_ndisc_router_get_lifetime(rt, &lifetime) >= 0); - log_info("Lifetime: %" PRIu16, lifetime); + assert_se(sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0); + log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t)); if (sd_ndisc_router_get_mtu(rt, &mtu) < 0) log_info("No MTU set"); @@ -99,16 +100,17 @@ static void router_dump(sd_ndisc_router *rt) { } case SD_NDISC_OPTION_PREFIX_INFORMATION: { - uint32_t lifetime_valid, lifetime_preferred; unsigned prefix_len; uint8_t pfl; struct in6_addr a; - assert_se(sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_valid) >= 0); - log_info("Valid Lifetime: %" PRIu32, lifetime_valid); + assert_se(sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime) >= 0); + assert_se(sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0); + log_info("Valid Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t)); - assert_se(sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime_preferred) >= 0); - log_info("Preferred Lifetime: %" PRIu32, lifetime_preferred); + assert_se(sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime) >= 0); + assert_se(sd_ndisc_router_prefix_get_preferred_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0); + log_info("Preferred Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t)); assert_se(sd_ndisc_router_prefix_get_flags(rt, &pfl) >= 0); log_info("Flags: <%s|%s>", @@ -126,7 +128,6 @@ static void router_dump(sd_ndisc_router *rt) { case SD_NDISC_OPTION_RDNSS: { const struct in6_addr *a; - uint32_t lt; int n, i; n = sd_ndisc_router_rdnss_get_addresses(rt, &a); @@ -135,14 +136,14 @@ static void router_dump(sd_ndisc_router *rt) { for (i = 0; i < n; i++) log_info("DNS: %s", IN6_ADDR_TO_STRING(a + i)); - assert_se(sd_ndisc_router_rdnss_get_lifetime(rt, <) >= 0); - log_info("Lifetime: %" PRIu32, lt); + assert_se(sd_ndisc_router_rdnss_get_lifetime(rt, &lifetime) >= 0); + assert_se(sd_ndisc_router_rdnss_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0); + log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t)); break; } case SD_NDISC_OPTION_DNSSL: { _cleanup_strv_free_ char **l = NULL; - uint32_t lt; int n, i; n = sd_ndisc_router_dnssl_get_domains(rt, &l); @@ -151,8 +152,9 @@ static void router_dump(sd_ndisc_router *rt) { for (i = 0; i < n; i++) log_info("Domain: %s", l[i]); - assert_se(sd_ndisc_router_dnssl_get_lifetime(rt, <) >= 0); - log_info("Lifetime: %" PRIu32, lt); + assert_se(sd_ndisc_router_dnssl_get_lifetime(rt, &lifetime) >= 0); + assert_se(sd_ndisc_router_dnssl_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0); + log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t)); break; }} |