diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2024-08-19 07:28:23 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2024-08-19 07:28:23 +0000 |
commit | db1f959bb10b43a24c95056c5da5366ab2a06bc4 (patch) | |
tree | 2b1fa119c23b7b3b941d43cb3f9d91ec8c8d8120 /usr.bin | |
parent | 8bd0094544fc4f028b56694945b1147e6d50ea2f (diff) |
Get rid of inet_aton(3).
inet_aton(3) is not a good interface to figure out if something looks
like an IP address and a reverse DNS lookup should be performed.
The modern way to do this is to chain getaddrinfo(3) with
getnameinfo(3). As a bonus this gives us reverse lookup for IPv6, too.
OK kn
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/w/w.c | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index c21c0b6a362..d5d50833119 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -1,4 +1,4 @@ -/* $OpenBSD: w.c,v 1.68 2022/12/04 23:50:50 cheloha Exp $ */ +/* $OpenBSD: w.c,v 1.69 2024/08/19 07:28:22 florian Exp $ */ /*- * Copyright (c) 1980, 1991, 1993, 1994 @@ -98,13 +98,13 @@ static void pr_header(time_t *, int); static struct stat *ttystat(char *); static void usage(int); +static char *hostlookup(char *, char *); int main(int argc, char *argv[]) { extern char *__progname; struct kinfo_proc *kp; - struct hostent *hp; struct stat *stp; FILE *ut; struct in_addr addr; @@ -295,29 +295,27 @@ main(int argc, char *argv[]) for (ep = ehead; ep != NULL; ep = ep->next) { p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; - for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++) + for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++) { if (p[i] == ':') { x = &p[i]; *x++ = '\0'; break; } - if (!nflag && inet_aton(p, &addr) && - (hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) { - if (domain[0] != '\0') { - p = hp->h_name; - p += strlen(hp->h_name); - p -= strlen(domain); - if (p > hp->h_name && - strcasecmp(p, domain) == 0) - *p = '\0'; - } - p = hp->h_name; } + if (x) { (void)snprintf(buf, sizeof(buf), "%s:%.*s", p, (int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x); p = buf; } + + if (!nflag) { + char *tmp; + + if ((tmp = hostlookup(p, domain)) != NULL) + p = tmp; + } + (void)printf("%-*.*s %-2.2s %-*.*s ", NAME_WIDTH, UT_NAMESIZE, ep->utmp.ut_name, strncmp(ep->utmp.ut_line, "tty", 3) ? @@ -509,3 +507,32 @@ usage(int wcmd) "usage: uptime\n"); exit (1); } + +static char* +hostlookup(char *host, char *domain) +{ + static char buf[NI_MAXHOST]; + struct addrinfo hints, *res; + int error; + char *p; + + memset(&hints, 0, sizeof(hints)); + if (getaddrinfo(host, NULL, &hints, &res) != 0) + return NULL; + + error = getnameinfo(res->ai_addr, res->ai_addr->sa_len, buf, + sizeof(buf), NULL, 0, 0); + freeaddrinfo(res); + + if (error) + return NULL; + + if (domain[0] != '\0') { + p = buf; + p += strlen(buf); + p -= strlen(domain); + if (p > buf && strcasecmp(p, domain) == 0) + *p = '\0'; + } + return buf; +} |