From: Roy Marples Date: Tue, 16 Apr 2019 18:12:16 +0000 (+0000) Subject: if: Add a generic function to create an aliased address name X-Git-Tag: v7.2.0~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5c199f49fefb0d157a3929c87dba50bd56b3523;p=thirdparty%2Fdhcpcd.git 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. --- diff --git a/src/if.c b/src/if.c index 783a92bb..efc4314c 100644 --- 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) diff --git a/src/if.h b/src/if.h index c8e406da..f10a8c37 100644 --- 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 *); diff --git a/src/ipv4.c b/src/ipv4.c index 57f70a77..7a39c2c5 100644 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -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. */ diff --git a/src/ipv6.c b/src/ipv6.c index ffa44f8c..38b838c7 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -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; }