2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 4/28/95";
41 static const char rcsid[] =
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
65 extern int lflag; /* XXX finger.h? */
66 extern int Tflag; /* XXX finger.h? */
68 static void cleanup(int sig);;
69 static int do_protocol(const char *name, const struct addrinfo *ai);
70 static void trying(const struct addrinfo *ai);
78 struct addrinfo *ai, *ai0;
79 static struct addrinfo hint;
81 host = strrchr(name, '@');
85 signal(SIGALRM, cleanup);
88 hint.ai_flags = AI_CANONNAME;
89 hint.ai_family = PF_UNSPEC;
90 hint.ai_socktype = SOCK_STREAM;
92 error = getaddrinfo(host, "finger", &hint, &ai0);
94 warnx("%s: %s", host, gai_strerror(error));
98 multi = (ai0->ai_next) != 0;
100 /* ai_canonname may not be filled in if the user specified an IP. */
101 if (ai0->ai_canonname == 0)
102 printf("[%s]\n", host);
104 printf("[%s]\n", ai0->ai_canonname);
106 for (ai = ai0; ai != 0; ai = ai->ai_next) {
110 error = do_protocol(name, ai);
119 do_protocol(const char *name, const struct addrinfo *ai)
121 int cnt, error, line_len, s;
123 register int c, lastc;
127 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
129 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
134 msg.msg_name = (void *)ai->ai_addr;
135 msg.msg_namelen = ai->ai_addrlen;
139 msg.msg_controllen = 0;
142 /* -l flag for remote fingerd */
144 iov[msg.msg_iovlen].iov_base = "/W ";
145 iov[msg.msg_iovlen++].iov_len = 3;
147 /* send the name followed by <CR><LF> */
148 iov[msg.msg_iovlen].iov_base = name;
149 iov[msg.msg_iovlen++].iov_len = strlen(name);
150 iov[msg.msg_iovlen].iov_base = "\r\n";
151 iov[msg.msg_iovlen++].iov_len = 2;
154 * -T disables data-on-SYN: compatibility option to finger broken
155 * hosts. Also, the implicit-open API is broken on IPv6, so do
156 * the explicit connect there, too.
158 if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
159 && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
165 if (sendmsg(s, &msg, 0) < 0) {
172 * Read from the remote system; once we're connected, we assume some
173 * data. If none arrives, we hang until the user interrupts.
175 * If we see a <CR> or a <CR> with the high bit set, treat it as
176 * a newline; if followed by a newline character, only output one
179 * Otherwise, all high bits are stripped; if it isn't printable and
180 * it isn't a space, we can simply set the 7th bit. Every ASCII
181 * character with bit 7 set is printable.
184 if ((fp = fdopen(s, "r")) != NULL) {
187 while ((c = getc(fp)) != EOF) {
188 if (++cnt > OUTPUT_MAX) {
189 printf("\n\n Output truncated at %d bytes...\n",
194 if (lastc == '\r') /* ^M^M - skip dupes */
199 if (!isprint(c) && !isspace(c)) {
203 if (lastc != '\r' || c != '\n')
211 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
216 if (lastc == '\n' || lastc == '\r')
221 * Assume that whatever it was set errno...
223 warn("reading from network");
234 trying(const struct addrinfo *ai)
236 char buf[NI_MAXHOST];
238 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
239 (char *)0, 0, NI_NUMERICHOST) != 0)
240 return; /* XXX can't happen */
242 printf("Trying %s...\n", buf);
248 #define ERRSTR "Timed out.\n"
249 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);