From 7b94a2e6e0aa5c147ccda2ad3b09931ed77fb6ff Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Wed, 15 Jan 2025 20:30:24 +0000 Subject: [PATCH] IPv6: Use lifetime_left function to reduce code size No functional change intended. --- src/common.c | 10 +++++++++- src/common.h | 2 +- src/eloop.c | 2 +- src/ipv6.c | 55 +++++++++++++++------------------------------------- src/ipv6.h | 4 ++-- src/ipv6nd.c | 36 ++++++++++++---------------------- 6 files changed, 41 insertions(+), 68 deletions(-) diff --git a/src/common.c b/src/common.c index 03bb33c1..0b56e9f4 100644 --- a/src/common.c +++ b/src/common.c @@ -217,13 +217,21 @@ is_root_local(void) } uint32_t -lifetime_left(uint32_t lifetime, const struct timespec *acquired, const struct timespec *now) +lifetime_left(uint32_t lifetime, const struct timespec *acquired, struct timespec *now) { uint32_t elapsed; + struct timespec n; if (lifetime == INFINITE_LIFETIME) return lifetime; + if (now == NULL) { + timespecclear(&n); + now = &n; + } + if (!timespecisset(now)) + clock_gettime(CLOCK_MONOTONIC, now); + elapsed = (uint32_t)eloop_timespec_diff(now, acquired, NULL); if (elapsed > lifetime) return 0; diff --git a/src/common.h b/src/common.h index 096f6bd4..a2ab05ea 100644 --- a/src/common.h +++ b/src/common.h @@ -150,5 +150,5 @@ ssize_t writefile(const char *, mode_t, const void *, size_t); int filemtime(const char *, time_t *); char *get_line(char ** __restrict, ssize_t * __restrict); int is_root_local(void); -uint32_t lifetime_left(uint32_t, const struct timespec *, const struct timespec *); +uint32_t lifetime_left(uint32_t, const struct timespec *, struct timespec *); #endif diff --git a/src/eloop.c b/src/eloop.c index 523455d7..d602fdcf 100644 --- a/src/eloop.c +++ b/src/eloop.c @@ -464,7 +464,7 @@ eloop_timespec_diff(const struct timespec *tsp, const struct timespec *usp, unsigned long long tsecs, usecs, secs; long nsecs; - if (tsp->tv_sec < 0) /* time wreapped */ + if (tsp->tv_sec < 0) /* time wrapped */ tsecs = UTIME_MAX - (unsigned long long)(-tsp->tv_sec); else tsecs = (unsigned long long)tsp->tv_sec; diff --git a/src/ipv6.c b/src/ipv6.c index c867626e..bc48a2a1 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -674,7 +674,7 @@ ipv6_getstate(struct interface *ifp) } static int -ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now) +ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now) { struct interface *ifp; uint32_t pltime, vltime; @@ -711,31 +711,11 @@ ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now) ia->prefix_vltime = ia->prefix_pltime = ND6_INFINITE_LIFETIME; } - if (timespecisset(&ia->acquired) && - (ia->prefix_pltime != ND6_INFINITE_LIFETIME || - ia->prefix_vltime != ND6_INFINITE_LIFETIME)) - { - uint32_t elapsed; - struct timespec n; - - if (now == NULL) { - clock_gettime(CLOCK_MONOTONIC, &n); - now = &n; - } - elapsed = (uint32_t)eloop_timespec_diff(now, &ia->acquired, - NULL); - if (ia->prefix_pltime != ND6_INFINITE_LIFETIME) { - if (elapsed > ia->prefix_pltime) - ia->prefix_pltime = 0; - else - ia->prefix_pltime -= elapsed; - } - if (ia->prefix_vltime != ND6_INFINITE_LIFETIME) { - if (elapsed > ia->prefix_vltime) - ia->prefix_vltime = 0; - else - ia->prefix_vltime -= elapsed; - } + if (timespecisset(&ia->acquired)) { + ia->prefix_pltime = lifetime_left(ia->prefix_pltime, + &ia->acquired, now); + ia->prefix_vltime = lifetime_left(ia->prefix_vltime, + &ia->acquired, now); } loglevel = ia->flags & IPV6_AF_NEW ? LOG_INFO : LOG_DEBUG; @@ -880,7 +860,7 @@ ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl) #endif int -ipv6_addaddr(struct ipv6_addr *ia, const struct timespec *now) +ipv6_addaddr(struct ipv6_addr *ia, struct timespec *now) { int r; #ifdef ALIAS_ADDR @@ -975,8 +955,6 @@ ipv6_doaddr(struct ipv6_addr *ia, struct timespec *now) IN6_IS_ADDR_UNSPECIFIED(&ia->addr)) return 0; - if (!timespecisset(now)) - clock_gettime(CLOCK_MONOTONIC, now); ipv6_addaddr(ia, now); return ia->flags & IPV6_AF_NEW ? 1 : 0; } @@ -1070,12 +1048,7 @@ ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop, ipv6_deleteaddr(ap); if (!(ap->iface->options->options & DHCPCD_EXITING) && apf) - { - if (!timespecisset(&now)) - clock_gettime(CLOCK_MONOTONIC, - &now); ipv6_addaddr(apf, &now); - } if (drop == 2) ipv6_freeaddr(ap); } @@ -2073,7 +2046,7 @@ ipv6_settemptime(struct ipv6_addr *ia, int flags) } void -ipv6_addtempaddrs(struct interface *ifp, const struct timespec *now) +ipv6_addtempaddrs(struct interface *ifp, struct timespec *now) { struct ipv6_state *state; struct ipv6_addr *ia; @@ -2306,7 +2279,7 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) if (ctx->ra_routers == NULL) return 0; - clock_gettime(CLOCK_MONOTONIC, &now); + timespecclear(&now); TAILQ_FOREACH(rap, ctx->ra_routers, next) { if (rap->expired) @@ -2328,7 +2301,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) #ifdef HAVE_ROUTE_PREF rt->rt_pref = ipv6nd_rtpref(rinfo->flags); #endif - rt->rt_expires = lifetime_left(rinfo->lifetime, &rinfo->acquired, &now); + rt->rt_expires = lifetime_left(rinfo->lifetime, + &rinfo->acquired, &now); rt_proto_add(routes, rt); } @@ -2343,7 +2317,9 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) #ifdef HAVE_ROUTE_PREF rt->rt_pref = ipv6nd_rtpref(rap->flags); #endif - rt->rt_expires = lifetime_left(addr->prefix_vltime, &addr->acquired, &now); + rt->rt_expires = + lifetime_left(addr->prefix_vltime, + &addr->acquired, &now); rt_proto_add(routes, rt); } @@ -2376,7 +2352,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) #ifdef HAVE_ROUTE_PREF rt->rt_pref = ipv6nd_rtpref(rap->flags); #endif - rt->rt_expires = lifetime_left(rap->lifetime, &rap->acquired, &now); + rt->rt_expires = lifetime_left(rap->lifetime, + &rap->acquired, &now); rt_proto_add(routes, rt); } diff --git a/src/ipv6.h b/src/ipv6.h index 6351cd4c..2304d95c 100644 --- a/src/ipv6.h +++ b/src/ipv6.h @@ -254,7 +254,7 @@ int ipv6_userprefix( const struct in6_addr *, short prefix_len, void ipv6_checkaddrflags(void *); void ipv6_markaddrsstale(struct interface *, unsigned int); void ipv6_deletestaleaddrs(struct interface *); -int ipv6_addaddr(struct ipv6_addr *, const struct timespec *); +int ipv6_addaddr(struct ipv6_addr *, struct timespec *); int ipv6_doaddr(struct ipv6_addr *, struct timespec *); ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs); void ipv6_deleteaddr(struct ipv6_addr *); @@ -289,7 +289,7 @@ void ipv6_freedrop(struct interface *, int); struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *, const struct timespec *); struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int); -void ipv6_addtempaddrs(struct interface *, const struct timespec *); +void ipv6_addtempaddrs(struct interface *, struct timespec *); void ipv6_regentempaddrs(void *); #endif diff --git a/src/ipv6nd.c b/src/ipv6nd.c index bfaf9274..b1bdfecb 100644 --- a/src/ipv6nd.c +++ b/src/ipv6nd.c @@ -1702,7 +1702,6 @@ ipv6nd_expirera(void *arg) struct interface *ifp; struct ra *rap, *ran; struct timespec now; - uint32_t elapsed; bool expired, valid; struct ipv6_addr *ia; struct routeinfo *rinfo, *rinfob; @@ -1714,11 +1713,11 @@ ipv6nd_expirera(void *arg) #endif struct nd_opt_dnssl dnssl; struct nd_opt_rdnss rdnss; - unsigned int next = 0, ltime; + uint32_t next = 0, ltime, elapsed; size_t nexpired = 0; ifp = arg; - clock_gettime(CLOCK_MONOTONIC, &now); + timespecclear(&now); expired = false; TAILQ_FOREACH_SAFE(rap, ifp->ctx->ra_routers, next, ran) { @@ -1726,10 +1725,10 @@ ipv6nd_expirera(void *arg) continue; valid = false; /* lifetime may be set to infinite by rfc4191 route information */ - if (rap->lifetime && rap->lifetime != ND6_INFINITE_LIFETIME) { - elapsed = (uint32_t)eloop_timespec_diff(&now, - &rap->acquired, NULL); - if (elapsed >= rap->lifetime || rap->doexpire) { + if (rap->lifetime) { + ltime = lifetime_left(rap->lifetime, + &rap->acquired, &now); + if (ltime == 0 || rap->doexpire) { if (!rap->expired) { logwarnx("%s: %s: router expired", ifp->name, rap->sfrom); @@ -1738,7 +1737,6 @@ ipv6nd_expirera(void *arg) } } else { valid = true; - ltime = rap->lifetime - elapsed; if (next == 0 || ltime < next) next = ltime; } @@ -1750,15 +1748,9 @@ ipv6nd_expirera(void *arg) TAILQ_FOREACH(ia, &rap->addrs, next) { if (ia->prefix_vltime == 0) continue; - if (ia->prefix_vltime == ND6_INFINITE_LIFETIME && - !rap->doexpire) - { - valid = true; - continue; - } - elapsed = (uint32_t)eloop_timespec_diff(&now, - &ia->acquired, NULL); - if (elapsed >= ia->prefix_vltime || rap->doexpire) { + ltime = lifetime_left(ia->prefix_vltime, + &ia->acquired, &now); + if (ltime == 0 || rap->doexpire) { if (ia->flags & IPV6_AF_ADDED) { logwarnx("%s: expired %s %s", ia->iface->name, @@ -1776,7 +1768,6 @@ ipv6nd_expirera(void *arg) expired = true; } else { valid = true; - ltime = ia->prefix_vltime - elapsed; if (next == 0 || ltime < next) next = ltime; } @@ -1784,12 +1775,9 @@ ipv6nd_expirera(void *arg) /* Expire route information */ TAILQ_FOREACH_SAFE(rinfo, &rap->rinfos, next, rinfob) { - if (rinfo->lifetime == ND6_INFINITE_LIFETIME && - !rap->doexpire) - continue; - elapsed = (uint32_t)eloop_timespec_diff(&now, - &rinfo->acquired, NULL); - if (elapsed >= rinfo->lifetime || rap->doexpire) { + ltime = lifetime_left(rinfo->lifetime, + &rinfo->acquired, &now); + if (ltime == 0 || rap->doexpire) { logwarnx("%s: expired route %s", rap->iface->name, rinfo->sprefix); TAILQ_REMOVE(&rap->rinfos, rinfo, next);