changeset 4439:5ed513825ce5 draft

if: Add a generic function to create an aliased address name Reduces complexity between IPv4 and IPv6 and silences a warning about potential string trunctaion if the LUN makes too big an interface name.
author Roy Marples <roy@marples.name>
date Tue, 16 Apr 2019 18:12:16 +0000
parents fea97b24d884
children 116946128ea1
files src/if.c src/if.h src/ipv4.c src/ipv6.c
diffstat 4 files changed, 32 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/if.c	Tue Apr 16 18:09:50 2019 +0000
+++ b/src/if.c	Tue Apr 16 18:12:16 2019 +0000
@@ -784,6 +784,17 @@
 	return 0;
 }
 
+#ifdef ALIAS_ADDR
+int
+if_makealias(char *alias, size_t alias_len, const char *ifname, int lun)
+{
+
+	if (lun == 0)
+		return strlcpy(alias, ifname, alias_len);
+	return snprintf(alias, alias_len, "%s:%u", ifname, lun);
+}
+#endif
+
 /* Sort the interfaces into a preferred order - best first, worst last. */
 void
 if_sortinterfaces(struct dhcpcd_ctx *ctx)
--- a/src/if.h	Tue Apr 16 18:09:50 2019 +0000
+++ b/src/if.h	Tue Apr 16 18:12:16 2019 +0000
@@ -129,6 +129,10 @@
 #define if_setmtu(ifp, mtu) if_domtu((ifp), (mtu))
 int if_carrier(struct interface *);
 
+#ifdef ALIAS_ADDR
+int if_makealias(char *, size_t, const char *, int);
+#endif
+
 int if_carrier_os(struct interface *);
 int if_mtu_os(const struct interface *);
 
--- a/src/ipv4.c	Tue Apr 16 18:09:50 2019 +0000
+++ b/src/ipv4.c	Tue Apr 16 18:12:16 2019 +0000
@@ -563,10 +563,12 @@
 	lun = 0;
 	state = IPV4_STATE(ia->iface);
 find_lun:
-	if (lun == 0)
-		strlcpy(alias, ia->iface->name, sizeof(alias));
-	else
-		snprintf(alias, sizeof(alias), "%s:%u", ia->iface->name, lun);
+	if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >=
+	    IF_NAMESIZE)
+	{
+		errno = ENOMEM;
+		return -1;
+	}
 	TAILQ_FOREACH(iap, &state->addrs, next) {
 		if (iap->alias[0] != '\0' && iap->addr.s_addr == INADDR_ANY) {
 			/* No address assigned? Lets use it. */
--- a/src/ipv6.c	Tue Apr 16 18:09:50 2019 +0000
+++ b/src/ipv6.c	Tue Apr 16 18:12:16 2019 +0000
@@ -763,13 +763,13 @@
 }
 
 #ifdef ALIAS_ADDR
-/* Find the next logical aliase address we can use. */
+/* Find the next logical alias address we can use. */
 static int
 ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl)
 {
 	struct ipv6_state *state;
 	struct ipv6_addr *iap;
-	unsigned int unit;
+	unsigned int lun;
 	char alias[IF_NAMESIZE];
 
 	if (ia->alias[0] != '\0')
@@ -788,12 +788,14 @@
 		}
 	}
 
-	unit = 0;
+	lun = 0;
 find_unit:
-	if (unit == 0)
-		strlcpy(alias, ia->iface->name, sizeof(alias));
-	else
-		snprintf(alias, sizeof(alias), "%s:%u", ia->iface->name, unit);
+	if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >=
+	    IF_NAMESIZE)
+	{
+		errno = ENOMEM;
+		return -1;
+	}
 	TAILQ_FOREACH(iap, &state->addrs, next) {
 		if (iap->alias[0] == '\0')
 			continue;
@@ -809,11 +811,11 @@
 	}
 
 	if (iap != NULL) {
-		if (unit == UINT_MAX) {
+		if (lun == UINT_MAX) {
 			errno = ERANGE;
 			return -1;
 		}
-		unit++;
+		lun++;
 		goto find_unit;
 	}