diff options
author | allanjude <allanjude@FreeBSD.org> | 2016-06-02 03:16:02 +0000 |
---|---|---|
committer | allanjude <allanjude@FreeBSD.org> | 2016-06-02 03:16:02 +0000 |
commit | 8e48e062788518254874792647e9b1d699395b16 (patch) | |
tree | 5bac17cb439b70f3c84c9264afaf9c385a62dd34 /sbin/ifconfig | |
parent | e90f6cad4e35288d0076cd5e822dfc17d21dc819 (diff) | |
download | FreeBSD-src-8e48e062788518254874792647e9b1d699395b16.zip FreeBSD-src-8e48e062788518254874792647e9b1d699395b16.tar.gz |
Address feedback from hrs@ re: r301059 (ifconfig subnet mask)
- Use NI_MAXHOST to size buffers for getnameinfo()
- remove non-standard 'full' inet6 address printing
- remove 'no scope' option
- use strchr(3) to optimize replacing separator character in lladdrs
Reviewed by: gnn, jhb
Differential Revision: https://reviews.freebsd.org/D2856
Diffstat (limited to 'sbin/ifconfig')
-rw-r--r-- | sbin/ifconfig/af_inet.c | 2 | ||||
-rw-r--r-- | sbin/ifconfig/af_inet6.c | 47 | ||||
-rw-r--r-- | sbin/ifconfig/af_link.c | 23 | ||||
-rw-r--r-- | sbin/ifconfig/ifconfig.8 | 18 | ||||
-rw-r--r-- | sbin/ifconfig/ifconfig.c | 8 |
5 files changed, 31 insertions, 67 deletions
diff --git a/sbin/ifconfig/af_inet.c b/sbin/ifconfig/af_inet.c index 5414d00..a386741 100644 --- a/sbin/ifconfig/af_inet.c +++ b/sbin/ifconfig/af_inet.c @@ -54,7 +54,7 @@ static const char rcsid[] = static struct in_aliasreq in_addreq; static struct ifreq in_ridreq; -static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ +static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/ extern char *f_inet, *f_addr; static void diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c index 0c8e952..3ddf303 100644 --- a/sbin/ifconfig/af_inet6.c +++ b/sbin/ifconfig/af_inet6.c @@ -65,13 +65,13 @@ static int ip6lifetime; static int prefix(void *, int); static char *sec2str(time_t); static int explicit_prefix = 0; -extern char *f_inet6, *f_addr, *f_scope; +extern char *f_inet6, *f_addr; extern void setnd6flags(const char *, int, int, const struct afswtch *); extern void setnd6defif(const char *, int, int, const struct afswtch *); extern void nd6_status(int); -static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ +static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/ static void setifprefixlen(const char *addr, int dummy __unused, int s, @@ -173,10 +173,9 @@ in6_status(int s __unused, const struct ifaddrs *ifa) struct in6_ifreq ifr6; int s6; u_int32_t flags6; - const u_int16_t *a; struct in6_addrlifetime lifetime; struct timespec now; - int error, n_flags, i; + int error, n_flags; clock_gettime(CLOCK_MONOTONIC_FAST, &now); @@ -208,30 +207,19 @@ in6_status(int s __unused, const struct ifaddrs *ifa) lifetime = ifr6.ifr_ifru.ifru_lifetime; close(s6); - if (f_addr != NULL && strcmp(f_addr, "full") == 0) { - a = (const u_int16_t *)&sin->sin6_addr; - printf("\tinet6 "); - for (i = 0; i < 8; i++) { - printf("%04hx", ntohs(*(a + i))); - if (i < 7) - printf(":"); - } - } else { - if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) - n_flags = 0; - else if (f_addr != NULL && strcmp(f_addr, "host") == 0) - n_flags = NI_NOFQDN; - else - n_flags = NI_NUMERICHOST; - error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, - addr_buf, sizeof(addr_buf), NULL, 0, - n_flags); - if (error != 0 || - (f_scope != NULL && strcmp(f_scope, "none") == 0)) - inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, - sizeof(addr_buf)); - printf("\tinet6 %s", addr_buf); - } + if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) + n_flags = 0; + else if (f_addr != NULL && strcmp(f_addr, "host") == 0) + n_flags = NI_NOFQDN; + else + n_flags = NI_NUMERICHOST; + error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, + addr_buf, sizeof(addr_buf), NULL, 0, + n_flags); + if (error != 0) + inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, + sizeof(addr_buf)); + printf("\tinet6 %s", addr_buf); if (ifa->ifa_flags & IFF_POINTOPOINT) { sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr; @@ -280,8 +268,7 @@ in6_status(int s __unused, const struct ifaddrs *ifa) if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0) printf("prefer_source "); - if ((f_scope == NULL || strcmp(f_scope, "none") != 0) && - ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id) + if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id) printf("scopeid 0x%x ", ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id); diff --git a/sbin/ifconfig/af_link.c b/sbin/ifconfig/af_link.c index e89be23..c96cbd2 100644 --- a/sbin/ifconfig/af_link.c +++ b/sbin/ifconfig/af_link.c @@ -58,25 +58,22 @@ link_status(int s __unused, const struct ifaddrs *ifa) { /* XXX no const 'cuz LLADDR is defined wrong */ struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr; - char *ether_format; - int i; + char *ether_format, *format_char; if (sdl != NULL && sdl->sdl_alen > 0) { if ((sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_L2VLAN || sdl->sdl_type == IFT_BRIDGE) && - sdl->sdl_alen == ETHER_ADDR_LEN) + sdl->sdl_alen == ETHER_ADDR_LEN) { + ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl)); if (f_ether != NULL && strcmp(f_ether, "dash") == 0) { - ether_format = ether_ntoa((struct ether_addr *)LLADDR(sdl)); - for (i = 0; i < strlen(ether_format); i++) { - if (ether_format[i] == ':') - ether_format[i] = '-'; - } - printf("\tether %s\n", ether_format); - } else - printf("\tether %s\n", - ether_ntoa((struct ether_addr *)LLADDR(sdl))); - else { + for (format_char = strchr(ether_format, ':'); + format_char != NULL; + format_char = strchr(ether_format, ':')) + *format_char = '-'; + } + printf("\tether %s\n", ether_format); + } else { int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; printf("\tlladdr %s\n", link_ntoa(sdl) + n); diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index 6559976..ef9bc2b 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -213,7 +213,7 @@ The and their associated .Sy format strings are: -.Bl -tag -width scope +.Bl -tag -width ether .It Sy addr Adjust the display of inet and inet6 addresses .Bl -tag -width default @@ -223,9 +223,6 @@ Display inet and inet6 addresses in the default format, .It Sy fqdn Display inet and inet6 addresses as fully qualified domain names .Pq FQDN -.It Sy full -Display inet6 addresses without suppressing zeroes. -Only applicable to inet6 .It Sy host Display inet and inet6 addresses as unqualified hostnames .It Sy numeric @@ -276,19 +273,6 @@ Display subnet prefix in integer format, for example: .br prefixlen 64 .El -.It Sy scope -Controls the display of the interface scope as part of the address. -Only applicable to inet6 addresses. -.Bl -tag -width default -.It Sy default -The interface scope and scopeid are included in the address, for example: -.br -inet6 fe80::1%lo0 prefixlen 64 scopeid 0x7 -.It Sy none -The interface scope and scopeid are not displayed, for example: -.br -inet6 fe80::1 prefixlen 64 -.El .El .Pp The following parameters may be set with diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 9449fe3..cd61166 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -99,7 +99,7 @@ int supmedia = 0; int printkeys = 0; /* Print keying material for interfaces. */ /* Formatter Strings */ -char *f_inet, *f_inet6, *f_ether, *f_addr, *f_scope; +char *f_inet, *f_inet6, *f_ether, *f_addr; static int ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *afp); @@ -257,8 +257,6 @@ static void freeformat(void) free(f_ether); if (f_addr != NULL) free(f_addr); - if (f_scope != NULL) - free(f_scope); } static void setformat(char *input) @@ -286,8 +284,6 @@ static void setformat(char *input) f_inet = strdup(modifier); else if (strcmp(category, "inet6") == 0) f_inet6 = strdup(modifier); - else if (strcmp(category, "scope") == 0) - f_scope = strdup(modifier); } free(formatstr); } @@ -372,7 +368,7 @@ main(int argc, char *argv[]) size_t iflen; all = downonly = uponly = namesonly = noload = verbose = 0; - f_inet = f_inet6 = f_ether = f_addr = f_scope = NULL; + f_inet = f_inet6 = f_ether = f_addr = NULL; envformat = getenv("IFCONFIG_FORMAT"); if (envformat != NULL) |