Skip to content

Commit

Permalink
IPv6: Use lifetime_left function to reduce code size
Browse files Browse the repository at this point in the history
No functional change intended.
  • Loading branch information
rsmarples committed Jan 15, 2025
1 parent 1481407 commit 7b94a2e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 68 deletions.
10 changes: 9 additions & 1 deletion src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/eloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 16 additions & 39 deletions src/ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down Expand Up @@ -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

Expand Down
36 changes: 12 additions & 24 deletions src/ipv6nd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1714,22 +1713,22 @@ 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) {
if (rap->iface != ifp || rap->expired)
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);
Expand All @@ -1738,7 +1737,6 @@ ipv6nd_expirera(void *arg)
}
} else {
valid = true;
ltime = rap->lifetime - elapsed;
if (next == 0 || ltime < next)
next = ltime;
}
Expand All @@ -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,
Expand All @@ -1776,20 +1768,16 @@ ipv6nd_expirera(void *arg)
expired = true;
} else {
valid = true;
ltime = ia->prefix_vltime - elapsed;
if (next == 0 || ltime < next)
next = ltime;
}
}

/* 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);
Expand Down

0 comments on commit 7b94a2e

Please sign in to comment.