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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)net.c 8.4 (Berkeley) 4/28/95
33 * $FreeBSD: src/usr.bin/finger/net.c,v 1.12.2.4 2002/12/16 08:41:28 roam Exp $
36 #include <sys/param.h>
37 #include <sys/socket.h>
51 static void cleanup(int sig);
52 static int do_protocol(const char *name, const struct addrinfo *ai);
53 static void trying(const struct addrinfo *ai);
60 struct addrinfo *ai, *ai0;
61 static struct addrinfo hint;
63 host = strrchr(name, '@');
67 signal(SIGALRM, cleanup);
70 hint.ai_flags = AI_CANONNAME;
71 hint.ai_family = family;
72 hint.ai_socktype = SOCK_STREAM;
74 error = getaddrinfo(host, "finger", &hint, &ai0);
76 warnx("%s: %s", host, gai_strerror(error));
80 multi = (ai0->ai_next) != 0;
82 /* ai_canonname may not be filled in if the user specified an IP. */
83 if (ai0->ai_canonname == 0)
84 printf("[%s]\n", host);
86 printf("[%s]\n", ai0->ai_canonname);
88 for (ai = ai0; ai != NULL; ai = ai->ai_next) {
92 error = do_protocol(name, ai);
101 do_protocol(const char *name, const struct addrinfo *ai)
103 int cnt, line_len, s;
108 static char slash_w[] = "/W ";
109 static char neteol[] = "\r\n";
111 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
113 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
118 msg.msg_name = (void *)ai->ai_addr;
119 msg.msg_namelen = ai->ai_addrlen;
123 msg.msg_controllen = 0;
126 /* -l flag for remote fingerd */
128 iov[msg.msg_iovlen].iov_base = slash_w;
129 iov[msg.msg_iovlen++].iov_len = 3;
131 /* send the name followed by <CR><LF> */
132 iov[msg.msg_iovlen].iov_base = strdup(name);
133 iov[msg.msg_iovlen++].iov_len = strlen(name);
134 iov[msg.msg_iovlen].iov_base = neteol;
135 iov[msg.msg_iovlen++].iov_len = 2;
138 * -T disables data-on-SYN: compatibility option to finger broken
139 * hosts. Also, the implicit-open API is broken on IPv6, so do
140 * the explicit connect there, too.
142 if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
143 && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
149 if (sendmsg(s, &msg, 0) < 0) {
156 * Read from the remote system; once we're connected, we assume some
157 * data. If none arrives, we hang until the user interrupts.
159 * If we see a <CR> or a <CR> with the high bit set, treat it as
160 * a newline; if followed by a newline character, only output one
163 * Otherwise, all high bits are stripped; if it isn't printable and
164 * it isn't a space, we can simply set the 7th bit. Every ASCII
165 * character with bit 7 set is printable.
168 if ((fp = fdopen(s, "r")) != NULL) {
171 while ((c = getc(fp)) != EOF) {
172 if (++cnt > OUTPUT_MAX) {
173 printf("\n\n Output truncated at %d bytes...\n",
178 if (lastc == '\r') /* ^M^M - skip dupes */
183 if (!(eightflag || isprint(c) || isspace(c))) {
187 if (lastc != '\r' || c != '\n')
195 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
200 if (lastc == '\n' || lastc == '\r')
205 * Assume that whatever it was set errno...
207 warn("reading from network");
218 trying(const struct addrinfo *ai)
220 char buf[NI_MAXHOST];
222 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
223 NULL, 0, NI_NUMERICHOST) != 0)
224 return; /* XXX can't happen */
226 printf("Trying %s...\n", buf);
230 cleanup(int sig __unused)
232 #define ERRSTR "Timed out.\n"
233 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);