]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
IPv6: Use lifetime_left function to reduce code size
authorRoy Marples <roy@marples.name>
Wed, 15 Jan 2025 20:30:24 +0000 (20:30 +0000)
committerRoy Marples <roy@marples.name>
Wed, 15 Jan 2025 20:30:24 +0000 (20:30 +0000)
No functional change intended.

src/common.c
src/common.h
src/eloop.c
src/ipv6.c
src/ipv6.h
src/ipv6nd.c

index 03bb33c158c65d80d4b5583a5357d98b3b8986bc..0b56e9f48827b3a74c80fa560a8633dd9ef9c4ca 100644 (file)
@@ -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;
index 096f6bd4f92a0323640596e05d089ee19019df05..a2ab05ea4b73098f5fe29d3968310d4f3539040d 100644 (file)
@@ -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
index 523455d7dc89a2349f2ddeee6c7ee63946007c80..d602fdcfe300933d5dd34f90096e11862ae0442c 100644 (file)
@@ -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;
index c867626e80d38e9e4ef33edf7ae3698153e41493..bc48a2a1fa4befb82a481146f6e39186eb30b7e2 100644 (file)
@@ -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 @@ find_unit:
 #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 @@ valid:
 }
 
 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);
        }
index 6351cd4c01461fb50ef4504db78ca53d3853159e..2304d95cd777d48ae8a55cb44186effbd08286db 100644 (file)
@@ -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
 
index bfaf92743d968a433f51e07e2e513527225f7f98..b1bdfecb345a0a9ea1366d72eb85f3afdb715ab9 100644 (file)
@@ -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);