]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
if: Add a generic function to create an aliased address name
authorRoy Marples <roy@marples.name>
Tue, 16 Apr 2019 18:12:16 +0000 (18:12 +0000)
committerRoy Marples <roy@marples.name>
Tue, 16 Apr 2019 18:12:16 +0000 (18:12 +0000)
Reduces complexity between IPv4 and IPv6 and silences a warning
about potential string trunctaion if the LUN makes too big an
interface name.

src/if.c
src/if.h
src/ipv4.c
src/ipv6.c

index 783a92bb0014d13f1a458de961823f7d0bfb6793..efc4314c94dbb897d257416a24bcea2e1d3cf874 100644 (file)
--- a/src/if.c
+++ b/src/if.c
@@ -784,6 +784,17 @@ if_cmp(const struct interface *si, const struct interface *ti)
        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)
index c8e406da35455360a56563902d92b12d81b84657..f10a8c37c89076789f3b17286a40be011602dc84 100644 (file)
--- a/src/if.h
+++ b/src/if.h
@@ -129,6 +129,10 @@ int if_domtu(const struct interface *, short int);
 #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 *);
 
index 57f70a77b53f1aec49d3338e1ddcb65ec83046be..7a39c2c5d0ddd5e8899df6ed98a5cd44b1cbb92b 100644 (file)
@@ -563,10 +563,12 @@ ipv4_aliasaddr(struct ipv4_addr *ia, struct ipv4_addr **repl)
        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. */
index ffa44f8c0fc7b055032480007d700f92a3b244ab..38b838c70744aa630caf71d35e300d34a23e9b55 100644 (file)
@@ -763,13 +763,13 @@ ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now)
 }
 
 #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 @@ ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl)
                }
        }
 
-       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 @@ find_unit:
        }
 
        if (iap != NULL) {
-               if (unit == UINT_MAX) {
+               if (lun == UINT_MAX) {
                        errno = ERANGE;
                        return -1;
                }
-               unit++;
+               lun++;
                goto find_unit;
        }