nrelease - gui build www/firefox3 -> www/firefox
[dragonfly.git] / usr.bin / finger / net.c
blob17ff1886eaf91322decb94b0702ddfc126f1cccc
1 /*
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
10 * are met:
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
34 * SUCH DAMAGE.
36 * @(#)net.c 8.4 (Berkeley) 4/28/95
37 * $FreeBSD: src/usr.bin/finger/net.c,v 1.12.2.4 2002/12/16 08:41:28 roam Exp $
38 * $DragonFly: src/usr.bin/finger/net.c,v 1.5 2004/09/03 19:13:23 dillon Exp $
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/uio.h>
44 #include <ctype.h>
45 #include <db.h>
46 #include <err.h>
47 #include <netdb.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <utmp.h>
54 #include "finger.h"
56 static void cleanup(int sig);
57 static int do_protocol(const char *name, const struct addrinfo *ai);
58 static void trying(const struct addrinfo *ai);
60 void
61 netfinger(char *name)
63 int error, multi;
64 char *host;
65 struct addrinfo *ai, *ai0;
66 static struct addrinfo hint;
68 host = strrchr(name, '@');
69 if (host == 0)
70 return;
71 *host++ = '\0';
72 signal(SIGALRM, cleanup);
73 alarm(TIME_LIMIT);
75 hint.ai_flags = AI_CANONNAME;
76 hint.ai_family = family;
77 hint.ai_socktype = SOCK_STREAM;
79 error = getaddrinfo(host, "finger", &hint, &ai0);
80 if (error) {
81 warnx("%s: %s", host, gai_strerror(error));
82 return;
85 multi = (ai0->ai_next) != 0;
87 /* ai_canonname may not be filled in if the user specified an IP. */
88 if (ai0->ai_canonname == 0)
89 printf("[%s]\n", host);
90 else
91 printf("[%s]\n", ai0->ai_canonname);
93 for (ai = ai0; ai != 0; ai = ai->ai_next) {
94 if (multi)
95 trying(ai);
97 error = do_protocol(name, ai);
98 if (!error)
99 break;
101 alarm(0);
102 freeaddrinfo(ai0);
105 static int
106 do_protocol(const char *name, const struct addrinfo *ai)
108 int cnt, line_len, s;
109 FILE *fp;
110 int c, lastc;
111 struct iovec iov[3];
112 struct msghdr msg;
113 static char slash_w[] = "/W ";
114 static char neteol[] = "\r\n";
116 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
117 if (s < 0) {
118 warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
119 ai->ai_protocol);
120 return -1;
123 msg.msg_name = (void *)ai->ai_addr;
124 msg.msg_namelen = ai->ai_addrlen;
125 msg.msg_iov = iov;
126 msg.msg_iovlen = 0;
127 msg.msg_control = 0;
128 msg.msg_controllen = 0;
129 msg.msg_flags = 0;
131 /* -l flag for remote fingerd */
132 if (lflag) {
133 iov[msg.msg_iovlen].iov_base = slash_w;
134 iov[msg.msg_iovlen++].iov_len = 3;
136 /* send the name followed by <CR><LF> */
137 iov[msg.msg_iovlen].iov_base = strdup(name);
138 iov[msg.msg_iovlen++].iov_len = strlen(name);
139 iov[msg.msg_iovlen].iov_base = neteol;
140 iov[msg.msg_iovlen++].iov_len = 2;
143 * -T disables data-on-SYN: compatibility option to finger broken
144 * hosts. Also, the implicit-open API is broken on IPv6, so do
145 * the explicit connect there, too.
147 if ((Tflag || ai->ai_addr->sa_family == AF_INET6)
148 && connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
149 warn("connect");
150 close(s);
151 return -1;
154 if (sendmsg(s, &msg, 0) < 0) {
155 warn("sendmsg");
156 close(s);
157 return -1;
161 * Read from the remote system; once we're connected, we assume some
162 * data. If none arrives, we hang until the user interrupts.
164 * If we see a <CR> or a <CR> with the high bit set, treat it as
165 * a newline; if followed by a newline character, only output one
166 * newline.
168 * Otherwise, all high bits are stripped; if it isn't printable and
169 * it isn't a space, we can simply set the 7th bit. Every ASCII
170 * character with bit 7 set is printable.
172 lastc = 0;
173 if ((fp = fdopen(s, "r")) != NULL) {
174 cnt = 0;
175 line_len = 0;
176 while ((c = getc(fp)) != EOF) {
177 if (++cnt > OUTPUT_MAX) {
178 printf("\n\n Output truncated at %d bytes...\n",
179 cnt - 1);
180 break;
182 if (c == 0x0d) {
183 if (lastc == '\r') /* ^M^M - skip dupes */
184 continue;
185 c = '\n';
186 lastc = '\r';
187 } else {
188 if (!(eightflag || isprint(c) || isspace(c))) {
189 c &= 0x7f;
190 c |= 0x40;
192 if (lastc != '\r' || c != '\n')
193 lastc = c;
194 else {
195 lastc = '\n';
196 continue;
199 putchar(c);
200 if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
201 putchar('\\');
202 putchar('\n');
203 lastc = '\r';
205 if (lastc == '\n' || lastc == '\r')
206 line_len = 0;
208 if (ferror(fp)) {
210 * Assume that whatever it was set errno...
212 warn("reading from network");
214 if (lastc != '\n')
215 putchar('\n');
217 fclose(fp);
219 return 0;
222 static void
223 trying(const struct addrinfo *ai)
225 char buf[NI_MAXHOST];
227 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, buf, sizeof buf,
228 NULL, 0, NI_NUMERICHOST) != 0)
229 return; /* XXX can't happen */
231 printf("Trying %s...\n", buf);
234 void
235 cleanup(int sig __unused)
237 #define ERRSTR "Timed out.\n"
238 write(STDERR_FILENO, ERRSTR, sizeof ERRSTR);
239 exit(1);