[PATCH v2 1/9] various: refactor pref(erred) to preferred_lt (lifetime)

Paul Donald newtwen+github at gmail.com
Fri Apr 5 04:06:45 PDT 2024


From: Paul Donald <newtwen at gmail.com>

Refactor "preferred" (preferred what?) and "pref" (obscure) to
"preferred_lt".

It is now more difficult to conflate prefix, preference and other "pref"
related terminology with preferred_lifetime.

Signed-off-by: Paul Donald <newtwen at gmail.com>
Reviewed-by: Daniel Golle <daniel at makrotopia.org>
---
 src/dhcpv6-ia.c | 84 ++++++++++++++++++++++++-------------------------
 src/dhcpv6.c    |  2 +-
 src/dhcpv6.h    |  4 +--
 src/netlink.c   | 34 ++++++++++----------
 src/odhcpd.c    |  6 ++--
 src/odhcpd.h    |  2 +-
 src/router.c    | 14 ++++-----
 7 files changed, 73 insertions(+), 73 deletions(-)

diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c
index 1fbed44..6a9098e 100644
--- a/src/dhcpv6-ia.c
+++ b/src/dhcpv6-ia.c
@@ -120,7 +120,7 @@ static inline bool valid_prefix_length(const struct dhcp_assignment *a, const ui
 
 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
 {
-	return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
+	return (addr->prefix <= 96 && addr->preferred_lt > (uint32_t)now);
 }
 
 static size_t get_preferred_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
@@ -128,8 +128,8 @@ static size_t get_preferred_addr(const struct odhcpd_ipaddr *addrs, const size_t
 	size_t i, m;
 
 	for (i = 0, m = 0; i < addrlen; ++i) {
-		if (addrs[i].preferred > addrs[m].preferred ||
-				(addrs[i].preferred == addrs[m].preferred &&
+		if (addrs[i].preferred_lt > addrs[m].preferred_lt ||
+				(addrs[i].preferred_lt == addrs[m].preferred_lt &&
 				memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
 			m = i;
 	}
@@ -226,7 +226,7 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
 
 	for (size_t i = 0; i < addrlen; ++i) {
 		struct in6_addr addr;
-		uint32_t pref, valid;
+		uint32_t preferred_lt, valid;
 		int prefix = c->managed ? addrs[i].prefix : c->length;
 
 		if (!valid_addr(&addrs[i], now))
@@ -242,7 +242,7 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
 		}
 
 		addr = addrs[i].addr.in6;
-		pref = addrs[i].preferred;
+		preferred_lt = addrs[i].preferred_lt;
 		valid = addrs[i].valid;
 
 		if (c->flags & OAF_DHCPV6_NA) {
@@ -259,14 +259,14 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
 			addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
 		}
 
-		if (pref > (uint32_t)c->preferred_until)
-			pref = c->preferred_until;
+		if (preferred_lt > (uint32_t)c->preferred_until)
+			preferred_lt = c->preferred_until;
 
-		if (pref > (uint32_t)c->valid_until)
-			pref = c->valid_until;
+		if (preferred_lt > (uint32_t)c->valid_until)
+			preferred_lt = c->valid_until;
 
-		if (pref != UINT32_MAX)
-			pref -= now;
+		if (preferred_lt != UINT32_MAX)
+			preferred_lt -= now;
 
 		if (valid > (uint32_t)c->valid_until)
 			valid = c->valid_until;
@@ -274,7 +274,7 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
 		if (valid != UINT32_MAX)
 			valid -= now;
 
-		func(&addr, prefix, pref, valid, arg);
+		func(&addr, prefix, preferred_lt, valid, arg);
 	}
 }
 
@@ -288,7 +288,7 @@ struct write_ctxt {
 	int buf_idx;
 };
 
-static void dhcpv6_write_ia_addrhosts(struct in6_addr *addr, int prefix, _unused uint32_t pref,
+static void dhcpv6_write_ia_addrhosts(struct in6_addr *addr, int prefix, _unused uint32_t pref_lt,
 				_unused uint32_t valid, void *arg)
 {
 	struct write_ctxt *ctxt = (struct write_ctxt *)arg;
@@ -308,7 +308,7 @@ static void dhcpv6_write_ia_addrhosts(struct in6_addr *addr, int prefix, _unused
 	}
 }
 
-static void dhcpv6_write_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
+static void dhcpv6_write_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref_lt,
 				_unused uint32_t valid, void *arg)
 {
 	struct write_ctxt *ctxt = (struct write_ctxt *)arg;
@@ -629,7 +629,7 @@ static void set_border_assignment_size(struct interface *iface, struct dhcp_assi
 		if (ADDR_MATCH_PIO_FILTER(addr, iface))
 			continue;
 
-		if (addr->preferred > (uint32_t)now &&
+		if (addr->preferred_lt > (uint32_t)now &&
 		    addr->prefix < 64 &&
 		    addr->prefix > minprefix)
 			minprefix = addr->prefix;
@@ -678,20 +678,20 @@ static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
 				continue;
 
 			x = strtok_r(NULL, ",", &saveptr2);
-			if (sscanf(x, "%u", &n->preferred) < 1)
+			if (sscanf(x, "%u", &n->preferred_lt) < 1)
 				continue;
 
 			x = strtok_r(NULL, ",", &saveptr2);
 			if (sscanf(x, "%u", &n->valid) < 1)
 				continue;
 
-			if (n->preferred > n->valid)
+			if (n->preferred_lt > n->valid)
 				continue;
 
-			if (UINT32_MAX - now < n->preferred)
-				n->preferred = UINT32_MAX;
+			if (UINT32_MAX - now < n->preferred_lt)
+				n->preferred_lt = UINT32_MAX;
 			else
-				n->preferred += now;
+				n->preferred_lt += now;
 
 			if (UINT32_MAX - now < n->valid)
 				n->valid = UINT32_MAX;
@@ -1037,14 +1037,14 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 	}
 
 	if (a) {
-		uint32_t leasetime, pref;
+		uint32_t leasetime, preferred_lt;
 
 		if (a->leasetime) {
 			leasetime = a->leasetime;
-			pref = a->leasetime;
+			preferred_lt = a->leasetime;
 		} else {
 			leasetime = iface->dhcp_leasetime;
-			pref = iface->preferred_lifetime;
+			preferred_lt = iface->preferred_lifetime;
 		}
 
 		uint32_t valid = leasetime;
@@ -1054,7 +1054,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 		size_t m = get_preferred_addr(addrs, addrlen);
 
 		for (size_t i = 0; i < addrlen; ++i) {
-			uint32_t prefix_pref, prefix_valid;
+			uint32_t prefix_preferred_lt, prefix_valid;
 
 			if (!valid_addr(&addrs[i], now))
 				continue;
@@ -1068,14 +1068,14 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 				continue;
 			}
 
-			prefix_pref = addrs[i].preferred;
+			prefix_preferred_lt = addrs[i].preferred_lt;
 			prefix_valid = addrs[i].valid;
 
-			if (prefix_pref != UINT32_MAX)
-				prefix_pref -= now;
+			if (prefix_preferred_lt != UINT32_MAX)
+				prefix_preferred_lt -= now;
 
-			if (prefix_pref > pref)
-				prefix_pref = pref;
+			if (prefix_preferred_lt > preferred_lt)
+				prefix_preferred_lt = preferred_lt;
 
 			if (prefix_valid != UINT32_MAX)
 				prefix_valid -= now;
@@ -1083,14 +1083,14 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 			if (prefix_valid > leasetime)
 				prefix_valid = leasetime;
 
-			if (prefix_pref > prefix_valid)
-				prefix_pref = prefix_valid;
+			if (prefix_preferred_lt > prefix_valid)
+				prefix_preferred_lt = prefix_valid;
 
 			if (a->flags & OAF_DHCPV6_PD) {
 				struct dhcpv6_ia_prefix o_ia_p = {
 					.type = htons(DHCPV6_OPT_IA_PREFIX),
 					.len = htons(sizeof(o_ia_p) - 4),
-					.preferred = htonl(prefix_pref),
+					.preferred_lt = htonl(prefix_preferred_lt),
 					.valid = htonl(prefix_valid),
 					.prefix = (a->managed_size) ? addrs[i].prefix : a->length,
 					.addr = addrs[i].addr.in6,
@@ -1114,7 +1114,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 					.type = htons(DHCPV6_OPT_IA_ADDR),
 					.len = htons(sizeof(o_ia_a) - 4),
 					.addr = addrs[i].addr.in6,
-					.preferred = htonl(prefix_pref),
+					.preferred_lt = htonl(prefix_preferred_lt),
 					.valid = htonl(prefix_valid)
 				};
 
@@ -1132,9 +1132,9 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 			}
 
 			/* Calculate T1 / T2 based on non-deprecated addresses */
-			if (prefix_pref > 0) {
-				if (prefix_pref < pref)
-					pref = prefix_pref;
+			if (prefix_preferred_lt > 0) {
+				if (prefix_preferred_lt < preferred_lt)
+					preferred_lt = prefix_preferred_lt;
 
 				if (prefix_valid < valid)
 					valid = prefix_valid;
@@ -1147,10 +1147,10 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 
 		if (!INFINITE_VALID(a->preferred_until))
 			/* UINT32_MAX is considered as infinite leasetime */
-			a->preferred_until = (pref == UINT32_MAX) ? 0 : pref + now;
+			a->preferred_until = (preferred_lt == UINT32_MAX) ? 0 : preferred_lt + now;
 
-		o_ia.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
-		o_ia.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
+		o_ia.t1 = htonl((preferred_lt == UINT32_MAX) ? preferred_lt : preferred_lt * 5 / 10);
+		o_ia.t2 = htonl((preferred_lt == UINT32_MAX) ? preferred_lt : preferred_lt * 8 / 10);
 
 		if (!o_ia.t1)
 			o_ia.t1 = htonl(1);
@@ -1211,7 +1211,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 					struct dhcpv6_ia_prefix o_ia_p = {
 						.type = htons(DHCPV6_OPT_IA_PREFIX),
 						.len = htons(sizeof(o_ia_p) - 4),
-						.preferred = 0,
+						.preferred_lt = 0,
 						.valid = 0,
 						.prefix = ia_p->prefix,
 						.addr = ia_p->addr,
@@ -1227,7 +1227,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 						.type = htons(DHCPV6_OPT_IA_ADDR),
 						.len = htons(sizeof(o_ia_a) - 4),
 						.addr = ia_a->addr,
-						.preferred = 0,
+						.preferred_lt = 0,
 						.valid = 0,
 					};
 
@@ -1252,7 +1252,7 @@ struct log_ctxt {
 	int buf_idx;
 };
 
-static void dhcpv6_log_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref,
+static void dhcpv6_log_ia_addr(struct in6_addr *addr, int prefix, _unused uint32_t pref_lt,
 				_unused uint32_t valid, void *arg)
 {
 	struct log_ctxt *ctxt = (struct log_ctxt *)arg;
diff --git a/src/dhcpv6.c b/src/dhcpv6.c
index b086fd8..b0e5a79 100644
--- a/src/dhcpv6.c
+++ b/src/dhcpv6.c
@@ -773,7 +773,7 @@ static struct odhcpd_ipaddr *relay_link_address(struct interface *iface)
 		if (iface->addr6[i].valid <= (uint32_t)now)
 			continue;
 
-		if (iface->addr6[i].preferred > (uint32_t)now) {
+		if (iface->addr6[i].preferred_lt > (uint32_t)now) {
 			addr = &iface->addr6[i];
 			break;
 		}
diff --git a/src/dhcpv6.h b/src/dhcpv6.h
index f344969..8681d7f 100644
--- a/src/dhcpv6.h
+++ b/src/dhcpv6.h
@@ -130,7 +130,7 @@ struct dhcpv6_ia_hdr {
 struct dhcpv6_ia_prefix {
 	uint16_t type;
 	uint16_t len;
-	uint32_t preferred;
+	uint32_t preferred_lt;
 	uint32_t valid;
 	uint8_t prefix;
 	struct in6_addr addr;
@@ -140,7 +140,7 @@ struct dhcpv6_ia_addr {
 	uint16_t type;
 	uint16_t len;
 	struct in6_addr addr;
-	uint32_t preferred;
+	uint32_t preferred_lt;
 	uint32_t valid;
 } _packed;
 
diff --git a/src/netlink.c b/src/netlink.c
index c2b9576..cf77035 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -206,8 +206,8 @@ static void refresh_iface_addr6(int ifindex)
 			for (ssize_t i = 0; !change && i < len; ++i) {
 				if (!IN6_ARE_ADDR_EQUAL(&addr[i].addr.in6, &iface->addr6[i].addr.in6) ||
 				    addr[i].prefix != iface->addr6[i].prefix ||
-				    (addr[i].preferred > (uint32_t)now) != (iface->addr6[i].preferred > (uint32_t)now) ||
-				    addr[i].valid < iface->addr6[i].valid || addr[i].preferred < iface->addr6[i].preferred)
+				    (addr[i].preferred_lt > (uint32_t)now) != (iface->addr6[i].preferred_lt > (uint32_t)now) ||
+				    addr[i].valid < iface->addr6[i].valid || addr[i].preferred_lt < iface->addr6[i].preferred_lt)
 					change = true;
 			}
 
@@ -258,7 +258,7 @@ static void refresh_iface_addr6(int ifindex)
 						iface->invalid_addr6 = new_invalid_addr6;
 						iface->invalid_addr6_len++;
 						memcpy(&iface->invalid_addr6[pos], &iface->addr6[i], sizeof(*iface->invalid_addr6));
-						iface->invalid_addr6[pos].valid = iface->invalid_addr6[pos].preferred = (uint32_t)now;
+						iface->invalid_addr6[pos].valid = iface->invalid_addr6[pos].preferred_lt = (uint32_t)now;
 
 						if (iface->invalid_addr6[pos].prefix < 64)
 							iface->invalid_addr6[pos].prefix = 64;
@@ -630,12 +630,12 @@ static int cb_addr_valid(struct nl_msg *msg, void *arg)
 	if (nla[IFA_CACHEINFO]) {
 		struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
 
-		addrs[ctxt->ret].preferred = ifc->ifa_prefered;
+		addrs[ctxt->ret].preferred_lt = ifc->ifa_prefered;
 		addrs[ctxt->ret].valid = ifc->ifa_valid;
 	}
 
 	if (ifa->ifa_flags & IFA_F_DEPRECATED)
-		addrs[ctxt->ret].preferred = 0;
+		addrs[ctxt->ret].preferred_lt = 0;
 
 	if (ifa->ifa_family == AF_INET6 &&
 	    ifa->ifa_flags & IFA_F_TENTATIVE)
@@ -689,9 +689,9 @@ static int prefix_cmp(const void *va, const void *vb)
 static int prefix6_cmp(const void *va, const void *vb)
 {
 	const struct odhcpd_ipaddr *a = va, *b = vb;
-	uint32_t a_pref = IN6_IS_ADDR_ULA(&a->addr.in6) ? 1 : a->preferred;
-	uint32_t b_pref = IN6_IS_ADDR_ULA(&b->addr.in6) ? 1 : b->preferred;
-	return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
+	uint32_t a_pref_lt = IN6_IS_ADDR_ULA(&a->addr.in6) ? 1 : a->preferred_lt;
+	uint32_t b_pref_lt = IN6_IS_ADDR_ULA(&b->addr.in6) ? 1 : b->preferred_lt;
+	return (a_pref_lt < b_pref_lt) ? 1 : (a_pref_lt > b_pref_lt) ? -1 : 0;
 }
 
 
@@ -749,8 +749,8 @@ ssize_t netlink_get_interface_addrs(int ifindex, bool v6, struct odhcpd_ipaddr *
 	qsort(addr, ctxt.ret, sizeof(*addr), v6 ? prefix6_cmp : prefix_cmp);
 
 	for (ssize_t i = 0; i < ctxt.ret; ++i) {
-		if (addr[i].preferred < UINT32_MAX - now)
-			addr[i].preferred += now;
+		if (addr[i].preferred_lt < UINT32_MAX - now)
+			addr[i].preferred_lt += now;
 
 		if (addr[i].valid < UINT32_MAX - now)
 			addr[i].valid += now;
@@ -1114,14 +1114,14 @@ int netlink_setup_addr(struct odhcpd_ipaddr *addr,
 						.tstamp = 0 };
 		time_t now = odhcpd_time();
 
-		if (addr->preferred) {
-			int64_t preferred = addr->preferred - now;
-			if (preferred < 0)
-				preferred = 0;
-			else if (preferred > UINT32_MAX)
-				preferred = UINT32_MAX;
+		if (addr->preferred_lt) {
+			int64_t preferred_lt = addr->preferred_lt - now;
+			if (preferred_lt < 0)
+				preferred_lt = 0;
+			else if (preferred_lt > UINT32_MAX)
+				preferred_lt = UINT32_MAX;
 
-			cinfo.ifa_prefered = preferred;
+			cinfo.ifa_prefered = preferred_lt;
 		}
 
 		if (addr->valid) {
diff --git a/src/odhcpd.c b/src/odhcpd.c
index 554e5f1..e2e89c0 100644
--- a/src/odhcpd.c
+++ b/src/odhcpd.c
@@ -277,8 +277,8 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr
 			continue;
 		}
 
-		if (iface->addr6[m].preferred >= (uint32_t)now &&
-				iface->addr6[i].preferred < (uint32_t)now)
+		if (iface->addr6[m].preferred_lt >= (uint32_t)now &&
+				iface->addr6[i].preferred_lt < (uint32_t)now)
 			continue;
 
 		if (IN6_IS_ADDR_ULA(&iface->addr6[i].addr.in6)) {
@@ -289,7 +289,7 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr
 		} else if (IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6))
 			continue;
 
-		if (iface->addr6[i].preferred > iface->addr6[m].preferred)
+		if (iface->addr6[i].preferred_lt > iface->addr6[m].preferred_lt)
 			m = i;
 	}
 
diff --git a/src/odhcpd.h b/src/odhcpd.h
index 02b6ac0..df27c14 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -126,7 +126,7 @@ struct netevent_handler {
 struct odhcpd_ipaddr {
 	union if_addr addr;
 	uint8_t prefix;
-	uint32_t preferred;
+	uint32_t preferred_lt;
 	uint32_t valid;
 
 	union {
diff --git a/src/router.c b/src/router.c
index d5ef7f8..55f3acb 100644
--- a/src/router.c
+++ b/src/router.c
@@ -301,7 +301,7 @@ static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size
 static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
 {
 	struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
-					.dprefix = 0, .preferred = 0, .valid = 0};
+					.dprefix = 0, .preferred_lt = 0, .valid = 0};
 	bool found_default = false;
 	char line[512], ifname[16];
 
@@ -549,7 +549,7 @@ static int send_router_advert(struct interface *iface, const struct in6_addr *fr
 	for (ssize_t i = 0; i < valid_addr_cnt + invalid_addr_cnt; ++i) {
 		struct odhcpd_ipaddr *addr = &addrs[i];
 		struct nd_opt_prefix_info *p = NULL;
-		uint32_t preferred = 0;
+		uint32_t preferred_lt = 0;
 		uint32_t valid = 0;
 
 		if (addr->prefix > 96 || (i < valid_addr_cnt && addr->valid <= (uint32_t)now)) {
@@ -587,12 +587,12 @@ static int send_router_advert(struct interface *iface, const struct in6_addr *fr
 			memset(p, 0, sizeof(*p));
 		}
 
-		if (addr->preferred > (uint32_t)now) {
-			preferred = TIME_LEFT(addr->preferred, now);
+		if (addr->preferred_lt > (uint32_t)now) {
+			preferred_lt = TIME_LEFT(addr->preferred_lt, now);
 
 			if (iface->ra_useleasetime &&
-			    preferred > iface->preferred_lifetime)
-				preferred = iface->preferred_lifetime;
+			    preferred_lt > iface->preferred_lifetime)
+				preferred_lt = iface->preferred_lifetime;
 		}
 
 		if (addr->valid > (uint32_t)now) {
@@ -620,7 +620,7 @@ static int send_router_advert(struct interface *iface, const struct in6_addr *fr
 			p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
 		if (iface->ra_advrouter)
 			p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
-		p->nd_opt_pi_preferred_time = htonl(preferred);
+		p->nd_opt_pi_preferred_time = htonl(preferred_lt);
 		p->nd_opt_pi_valid_time = htonl(valid);
 	}
 
-- 
2.44.0




More information about the openwrt-devel mailing list