xref: /csrg-svn/usr.bin/finger/net.c (revision 40027)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
5*40027Sbostic  * This code is derived from software contributed to Berkeley by
6*40027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7*40027Sbostic  *
837660Sbostic  * Redistribution and use in source and binary forms are permitted
937660Sbostic  * provided that the above copyright notice and this paragraph are
1037660Sbostic  * duplicated in all such forms and that any documentation,
1137660Sbostic  * advertising materials, and other materials related to such
1237660Sbostic  * distribution and use acknowledge that the software was developed
1337660Sbostic  * by the University of California, Berkeley.  The name of the
1437660Sbostic  * University may not be used to endorse or promote products derived
1537660Sbostic  * from this software without specific prior written permission.
1637660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1737660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1837660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1937660Sbostic  */
2037660Sbostic 
2137660Sbostic #ifndef lint
22*40027Sbostic static char sccsid[] = "@(#)net.c	5.3 (Berkeley) 02/07/90";
2337660Sbostic #endif /* not lint */
2437660Sbostic 
2537660Sbostic #include <sys/types.h>
2637660Sbostic #include <sys/socket.h>
2737660Sbostic #include <netinet/in.h>
2837660Sbostic #include <netdb.h>
2937660Sbostic #include <stdio.h>
3037660Sbostic #include <ctype.h>
3137660Sbostic 
3237660Sbostic netfinger(name)
3337660Sbostic 	char *name;
3437660Sbostic {
3537660Sbostic 	extern int lflag;
3637660Sbostic 	register FILE *fp;
3737660Sbostic 	register int c, lastc;
3837660Sbostic 	struct in_addr defaddr;
3937660Sbostic 	struct hostent *hp, def;
4037660Sbostic 	struct servent *sp;
4137660Sbostic 	struct sockaddr_in sin;
4237660Sbostic 	int s;
4337660Sbostic 	char *alist[1], *host, *rindex();
4437660Sbostic 	u_long inet_addr();
4537660Sbostic 
4637660Sbostic 	if (!(host = rindex(name, '@')))
4737660Sbostic 		return;
4837660Sbostic 	*host++ = NULL;
4937660Sbostic 	if (!(hp = gethostbyname(host))) {
5037660Sbostic 		defaddr.s_addr = inet_addr(host);
5137660Sbostic 		if (defaddr.s_addr == -1) {
5237660Sbostic 			(void)fprintf(stderr,
5337660Sbostic 			    "finger: unknown host: %s\n", host);
5437660Sbostic 			return;
5537660Sbostic 		}
5637660Sbostic 		def.h_name = host;
5737660Sbostic 		def.h_addr_list = alist;
5837660Sbostic 		def.h_addr = (char *)&defaddr;
5937660Sbostic 		def.h_length = sizeof(struct in_addr);
6037660Sbostic 		def.h_addrtype = AF_INET;
6137660Sbostic 		def.h_aliases = 0;
6237660Sbostic 		hp = &def;
6337660Sbostic 	}
6437660Sbostic 	if (!(sp = getservbyname("finger", "tcp"))) {
6537660Sbostic 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
6637660Sbostic 		return;
6737660Sbostic 	}
6837660Sbostic 	sin.sin_family = hp->h_addrtype;
6937660Sbostic 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
7037660Sbostic 	sin.sin_port = sp->s_port;
7137660Sbostic 	if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
7237660Sbostic 		perror("finger: socket");
7337660Sbostic 		return;
7437660Sbostic 	}
7537660Sbostic 
7637660Sbostic 	/* have network connection; identify the host connected with */
7737660Sbostic 	(void)printf("[%s]\n", hp->h_name);
7837660Sbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
7937660Sbostic 		perror("finger: connect");
8037660Sbostic 		(void)close(s);
8137660Sbostic 		return;
8237660Sbostic 	}
8337660Sbostic 
8437660Sbostic 	/* -l flag for remote fingerd  */
8537660Sbostic 	if (lflag)
8637660Sbostic 		write(s, "/W ", 3);
8737660Sbostic 	/* send the name followed by <CR><LF> */
8837660Sbostic 	(void)write(s, name, strlen(name));
8937660Sbostic 	(void)write(s, "\r\n", 2);
9037660Sbostic 
9137660Sbostic 	/*
9237660Sbostic 	 * read back what we get from the remote system.
9337660Sbostic 	 *
9437660Sbostic 	 * Note: once we connected to the remote site, we assume some data.
9537660Sbostic 	 * If it can't/won't send any data, we hang here until Mr. User
9637660Sbostic 	 * gets sufficiently bored to hit ^C.
9737660Sbostic 	 *
9837660Sbostic 	 * Some systems use the return key as a line terminator.  These
9937660Sbostic 	 * systems tend to also set the parity bit on every byte.  If we
10037660Sbostic 	 * see a <CR> with the 8th bit set, treat it as a newline character.
10137660Sbostic 	 * 0x8d == <CR> with high bit set.
10237660Sbostic 	 *
10337660Sbostic 	 * Otherwise, all high bits are stripped; if it isn't printable and
10437660Sbostic 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
10537660Sbostic 	 * character with bit 7 set is printable.
10637660Sbostic 	 */
10737660Sbostic 	if (fp = fdopen(s, "r"))
10837660Sbostic 		while ((c = getc(fp)) != EOF) {
10937660Sbostic 			if (c == 0x8d)
11037660Sbostic 				c = '\n';
11137660Sbostic 			c &= 0x7f;
11237660Sbostic 			if (!isprint(c) && !isspace(c))
11337660Sbostic 				c |= 0x40;
11437660Sbostic 			lastc = c;
11537660Sbostic 			putchar(c);
11637660Sbostic 		}
11737660Sbostic 	if (lastc != '\n')
11837660Sbostic 		putchar('\n');
11937660Sbostic 	(void)fclose(fp);
12037660Sbostic }
121