diff options
author | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 1999-12-11 10:05:05 +0000 |
---|---|---|
committer | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 1999-12-11 10:05:05 +0000 |
commit | 4a038e3f66a4f255b18613d9a2356ced4301ba2f (patch) | |
tree | 2a4a4e425f7cec0a62011d44b21335e44c2bc151 | |
parent | 27b5ecbc7a7f3149fa0d7c2d17040dbff5bf0ab2 (diff) |
IPv6 support for finger client.
-rw-r--r-- | usr.bin/finger/net.c | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/usr.bin/finger/net.c b/usr.bin/finger/net.c index a39548ae752..1329c2e309c 100644 --- a/usr.bin/finger/net.c +++ b/usr.bin/finger/net.c @@ -1,4 +1,4 @@ -/* $OpenBSD: net.c,v 1.6 1998/07/10 15:45:16 mickey Exp $ */ +/* $OpenBSD: net.c,v 1.7 1999/12/11 10:05:04 itojun Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -38,7 +38,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)net.c 5.5 (Berkeley) 6/1/90";*/ -static char rcsid[] = "$OpenBSD: net.c,v 1.6 1998/07/10 15:45:16 mickey Exp $"; +static char rcsid[] = "$OpenBSD: net.c,v 1.7 1999/12/11 10:05:04 itojun Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -60,44 +60,54 @@ netfinger(name) { FILE *fp; int c, lastc; - struct hostent *hp; - struct servent *sp; - struct sockaddr_in sin; int s; char *host; + struct addrinfo hints, *res0, *res; + int error; + char hbuf[NI_MAXHOST]; lastc = 0; if (!(host = strrchr(name, '@'))) return; *host++ = '\0'; - if (inet_aton(host, &sin.sin_addr) == 0) { - hp = gethostbyname(host); - if (hp == 0) { - warnx("unknown host: %s", host); - return; - } - sin.sin_family = hp->h_addrtype; - bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); - host = hp->h_name; - } else - sin.sin_family = AF_INET; - if (!(sp = getservbyname("finger", "tcp"))) { - warnx("tcp/finger: unknown service\n"); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + error = getaddrinfo(host, "finger", &hints, &res0); + if (error) { + warnx("%s", gai_strerror(error)); return; } - sin.sin_port = sp->s_port; - if ((s = socket(sin.sin_family, SOCK_STREAM, 0)) < 0) { - perror("finger: socket"); + + s = -1; + for (res = res0; res; res = res->ai_next) { + if ((s = socket(res->ai_family, res->ai_socktype, + res->ai_protocol)) < 0) { + continue; + } + if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { + (void)close(s); + s = -1; + continue; + } + + break; + } + + if (s < 0) { + perror("finger"); + freeaddrinfo(res0); return; } /* have network connection; identify the host connected with */ - (void)printf("[%s]\n", host); - if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - perror("finger: connect"); - (void)close(s); - return; + if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), + NULL, 0, NI_NUMERICHOST) != 0) { + strcpy(hbuf, "(invalid)"); } + (void)printf("[%s/%s]\n", host, hbuf); + + freeaddrinfo(res0); /* -l flag for remote fingerd */ if (lflag) |