=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/finger/net.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- src/usr.bin/finger/net.c 1998/07/10 15:45:16 1.6 +++ src/usr.bin/finger/net.c 1999/12/11 10:05:04 1.7 @@ -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 @@ -60,44 +60,54 @@ { 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)