Skip to content

Commit 7b94a2e

Browse files
committed
IPv6: Use lifetime_left function to reduce code size
No functional change intended.
1 parent 1481407 commit 7b94a2e

File tree

6 files changed

+41
-68
lines changed

6 files changed

+41
-68
lines changed

src/common.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,21 @@ is_root_local(void)
217217
}
218218

219219
uint32_t
220-
lifetime_left(uint32_t lifetime, const struct timespec *acquired, const struct timespec *now)
220+
lifetime_left(uint32_t lifetime, const struct timespec *acquired, struct timespec *now)
221221
{
222222
uint32_t elapsed;
223+
struct timespec n;
223224

224225
if (lifetime == INFINITE_LIFETIME)
225226
return lifetime;
226227

228+
if (now == NULL) {
229+
timespecclear(&n);
230+
now = &n;
231+
}
232+
if (!timespecisset(now))
233+
clock_gettime(CLOCK_MONOTONIC, now);
234+
227235
elapsed = (uint32_t)eloop_timespec_diff(now, acquired, NULL);
228236
if (elapsed > lifetime)
229237
return 0;

src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,5 @@ ssize_t writefile(const char *, mode_t, const void *, size_t);
150150
int filemtime(const char *, time_t *);
151151
char *get_line(char ** __restrict, ssize_t * __restrict);
152152
int is_root_local(void);
153-
uint32_t lifetime_left(uint32_t, const struct timespec *, const struct timespec *);
153+
uint32_t lifetime_left(uint32_t, const struct timespec *, struct timespec *);
154154
#endif

src/eloop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ eloop_timespec_diff(const struct timespec *tsp, const struct timespec *usp,
464464
unsigned long long tsecs, usecs, secs;
465465
long nsecs;
466466

467-
if (tsp->tv_sec < 0) /* time wreapped */
467+
if (tsp->tv_sec < 0) /* time wrapped */
468468
tsecs = UTIME_MAX - (unsigned long long)(-tsp->tv_sec);
469469
else
470470
tsecs = (unsigned long long)tsp->tv_sec;

src/ipv6.c

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ ipv6_getstate(struct interface *ifp)
674674
}
675675

676676
static int
677-
ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now)
677+
ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now)
678678
{
679679
struct interface *ifp;
680680
uint32_t pltime, vltime;
@@ -711,31 +711,11 @@ ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now)
711711
ia->prefix_vltime = ia->prefix_pltime = ND6_INFINITE_LIFETIME;
712712
}
713713

714-
if (timespecisset(&ia->acquired) &&
715-
(ia->prefix_pltime != ND6_INFINITE_LIFETIME ||
716-
ia->prefix_vltime != ND6_INFINITE_LIFETIME))
717-
{
718-
uint32_t elapsed;
719-
struct timespec n;
720-
721-
if (now == NULL) {
722-
clock_gettime(CLOCK_MONOTONIC, &n);
723-
now = &n;
724-
}
725-
elapsed = (uint32_t)eloop_timespec_diff(now, &ia->acquired,
726-
NULL);
727-
if (ia->prefix_pltime != ND6_INFINITE_LIFETIME) {
728-
if (elapsed > ia->prefix_pltime)
729-
ia->prefix_pltime = 0;
730-
else
731-
ia->prefix_pltime -= elapsed;
732-
}
733-
if (ia->prefix_vltime != ND6_INFINITE_LIFETIME) {
734-
if (elapsed > ia->prefix_vltime)
735-
ia->prefix_vltime = 0;
736-
else
737-
ia->prefix_vltime -= elapsed;
738-
}
714+
if (timespecisset(&ia->acquired)) {
715+
ia->prefix_pltime = lifetime_left(ia->prefix_pltime,
716+
&ia->acquired, now);
717+
ia->prefix_vltime = lifetime_left(ia->prefix_vltime,
718+
&ia->acquired, now);
739719
}
740720

741721
loglevel = ia->flags & IPV6_AF_NEW ? LOG_INFO : LOG_DEBUG;
@@ -880,7 +860,7 @@ ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl)
880860
#endif
881861

882862
int
883-
ipv6_addaddr(struct ipv6_addr *ia, const struct timespec *now)
863+
ipv6_addaddr(struct ipv6_addr *ia, struct timespec *now)
884864
{
885865
int r;
886866
#ifdef ALIAS_ADDR
@@ -975,8 +955,6 @@ ipv6_doaddr(struct ipv6_addr *ia, struct timespec *now)
975955
IN6_IS_ADDR_UNSPECIFIED(&ia->addr))
976956
return 0;
977957

978-
if (!timespecisset(now))
979-
clock_gettime(CLOCK_MONOTONIC, now);
980958
ipv6_addaddr(ia, now);
981959
return ia->flags & IPV6_AF_NEW ? 1 : 0;
982960
}
@@ -1070,12 +1048,7 @@ ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop,
10701048
ipv6_deleteaddr(ap);
10711049
if (!(ap->iface->options->options &
10721050
DHCPCD_EXITING) && apf)
1073-
{
1074-
if (!timespecisset(&now))
1075-
clock_gettime(CLOCK_MONOTONIC,
1076-
&now);
10771051
ipv6_addaddr(apf, &now);
1078-
}
10791052
if (drop == 2)
10801053
ipv6_freeaddr(ap);
10811054
}
@@ -2073,7 +2046,7 @@ ipv6_settemptime(struct ipv6_addr *ia, int flags)
20732046
}
20742047

20752048
void
2076-
ipv6_addtempaddrs(struct interface *ifp, const struct timespec *now)
2049+
ipv6_addtempaddrs(struct interface *ifp, struct timespec *now)
20772050
{
20782051
struct ipv6_state *state;
20792052
struct ipv6_addr *ia;
@@ -2306,7 +2279,7 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
23062279
if (ctx->ra_routers == NULL)
23072280
return 0;
23082281

2309-
clock_gettime(CLOCK_MONOTONIC, &now);
2282+
timespecclear(&now);
23102283

23112284
TAILQ_FOREACH(rap, ctx->ra_routers, next) {
23122285
if (rap->expired)
@@ -2328,7 +2301,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
23282301
#ifdef HAVE_ROUTE_PREF
23292302
rt->rt_pref = ipv6nd_rtpref(rinfo->flags);
23302303
#endif
2331-
rt->rt_expires = lifetime_left(rinfo->lifetime, &rinfo->acquired, &now);
2304+
rt->rt_expires = lifetime_left(rinfo->lifetime,
2305+
&rinfo->acquired, &now);
23322306

23332307
rt_proto_add(routes, rt);
23342308
}
@@ -2343,7 +2317,9 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
23432317
#ifdef HAVE_ROUTE_PREF
23442318
rt->rt_pref = ipv6nd_rtpref(rap->flags);
23452319
#endif
2346-
rt->rt_expires = lifetime_left(addr->prefix_vltime, &addr->acquired, &now);
2320+
rt->rt_expires =
2321+
lifetime_left(addr->prefix_vltime,
2322+
&addr->acquired, &now);
23472323

23482324
rt_proto_add(routes, rt);
23492325
}
@@ -2376,7 +2352,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
23762352
#ifdef HAVE_ROUTE_PREF
23772353
rt->rt_pref = ipv6nd_rtpref(rap->flags);
23782354
#endif
2379-
rt->rt_expires = lifetime_left(rap->lifetime, &rap->acquired, &now);
2355+
rt->rt_expires = lifetime_left(rap->lifetime,
2356+
&rap->acquired, &now);
23802357

23812358
rt_proto_add(routes, rt);
23822359
}

src/ipv6.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ int ipv6_userprefix( const struct in6_addr *, short prefix_len,
254254
void ipv6_checkaddrflags(void *);
255255
void ipv6_markaddrsstale(struct interface *, unsigned int);
256256
void ipv6_deletestaleaddrs(struct interface *);
257-
int ipv6_addaddr(struct ipv6_addr *, const struct timespec *);
257+
int ipv6_addaddr(struct ipv6_addr *, struct timespec *);
258258
int ipv6_doaddr(struct ipv6_addr *, struct timespec *);
259259
ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs);
260260
void ipv6_deleteaddr(struct ipv6_addr *);
@@ -289,7 +289,7 @@ void ipv6_freedrop(struct interface *, int);
289289
struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *,
290290
const struct timespec *);
291291
struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int);
292-
void ipv6_addtempaddrs(struct interface *, const struct timespec *);
292+
void ipv6_addtempaddrs(struct interface *, struct timespec *);
293293
void ipv6_regentempaddrs(void *);
294294
#endif
295295

src/ipv6nd.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,6 @@ ipv6nd_expirera(void *arg)
17021702
struct interface *ifp;
17031703
struct ra *rap, *ran;
17041704
struct timespec now;
1705-
uint32_t elapsed;
17061705
bool expired, valid;
17071706
struct ipv6_addr *ia;
17081707
struct routeinfo *rinfo, *rinfob;
@@ -1714,22 +1713,22 @@ ipv6nd_expirera(void *arg)
17141713
#endif
17151714
struct nd_opt_dnssl dnssl;
17161715
struct nd_opt_rdnss rdnss;
1717-
unsigned int next = 0, ltime;
1716+
uint32_t next = 0, ltime, elapsed;
17181717
size_t nexpired = 0;
17191718

17201719
ifp = arg;
1721-
clock_gettime(CLOCK_MONOTONIC, &now);
1720+
timespecclear(&now);
17221721
expired = false;
17231722

17241723
TAILQ_FOREACH_SAFE(rap, ifp->ctx->ra_routers, next, ran) {
17251724
if (rap->iface != ifp || rap->expired)
17261725
continue;
17271726
valid = false;
17281727
/* lifetime may be set to infinite by rfc4191 route information */
1729-
if (rap->lifetime && rap->lifetime != ND6_INFINITE_LIFETIME) {
1730-
elapsed = (uint32_t)eloop_timespec_diff(&now,
1731-
&rap->acquired, NULL);
1732-
if (elapsed >= rap->lifetime || rap->doexpire) {
1728+
if (rap->lifetime) {
1729+
ltime = lifetime_left(rap->lifetime,
1730+
&rap->acquired, &now);
1731+
if (ltime == 0 || rap->doexpire) {
17331732
if (!rap->expired) {
17341733
logwarnx("%s: %s: router expired",
17351734
ifp->name, rap->sfrom);
@@ -1738,7 +1737,6 @@ ipv6nd_expirera(void *arg)
17381737
}
17391738
} else {
17401739
valid = true;
1741-
ltime = rap->lifetime - elapsed;
17421740
if (next == 0 || ltime < next)
17431741
next = ltime;
17441742
}
@@ -1750,15 +1748,9 @@ ipv6nd_expirera(void *arg)
17501748
TAILQ_FOREACH(ia, &rap->addrs, next) {
17511749
if (ia->prefix_vltime == 0)
17521750
continue;
1753-
if (ia->prefix_vltime == ND6_INFINITE_LIFETIME &&
1754-
!rap->doexpire)
1755-
{
1756-
valid = true;
1757-
continue;
1758-
}
1759-
elapsed = (uint32_t)eloop_timespec_diff(&now,
1760-
&ia->acquired, NULL);
1761-
if (elapsed >= ia->prefix_vltime || rap->doexpire) {
1751+
ltime = lifetime_left(ia->prefix_vltime,
1752+
&ia->acquired, &now);
1753+
if (ltime == 0 || rap->doexpire) {
17621754
if (ia->flags & IPV6_AF_ADDED) {
17631755
logwarnx("%s: expired %s %s",
17641756
ia->iface->name,
@@ -1776,20 +1768,16 @@ ipv6nd_expirera(void *arg)
17761768
expired = true;
17771769
} else {
17781770
valid = true;
1779-
ltime = ia->prefix_vltime - elapsed;
17801771
if (next == 0 || ltime < next)
17811772
next = ltime;
17821773
}
17831774
}
17841775

17851776
/* Expire route information */
17861777
TAILQ_FOREACH_SAFE(rinfo, &rap->rinfos, next, rinfob) {
1787-
if (rinfo->lifetime == ND6_INFINITE_LIFETIME &&
1788-
!rap->doexpire)
1789-
continue;
1790-
elapsed = (uint32_t)eloop_timespec_diff(&now,
1791-
&rinfo->acquired, NULL);
1792-
if (elapsed >= rinfo->lifetime || rap->doexpire) {
1778+
ltime = lifetime_left(rinfo->lifetime,
1779+
&rinfo->acquired, &now);
1780+
if (ltime == 0 || rap->doexpire) {
17931781
logwarnx("%s: expired route %s",
17941782
rap->iface->name, rinfo->sprefix);
17951783
TAILQ_REMOVE(&rap->rinfos, rinfo, next);

0 commit comments

Comments
 (0)