Home | History | Annotate | Line # | Download | only in finger
net.c revision 1.2
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * This code is derived from software contributed to Berkeley by
      6  1.1      cgd  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
      7  1.1      cgd  *
      8  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1      cgd  * modification, are permitted provided that the following conditions
     10  1.1      cgd  * are met:
     11  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1      cgd  *    must display the following acknowledgement:
     18  1.1      cgd  *	This product includes software developed by the University of
     19  1.1      cgd  *	California, Berkeley and its contributors.
     20  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      cgd  *    may be used to endorse or promote products derived from this software
     22  1.1      cgd  *    without specific prior written permission.
     23  1.1      cgd  *
     24  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      cgd  * SUCH DAMAGE.
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.1      cgd #ifndef lint
     38  1.2  mycroft /*static char sccsid[] = "from: @(#)net.c	5.5 (Berkeley) 6/1/90";*/
     39  1.2  mycroft static char rcsid[] = "$Id: net.c,v 1.2 1993/08/01 18:16:00 mycroft Exp $";
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #include <sys/types.h>
     43  1.1      cgd #include <sys/socket.h>
     44  1.1      cgd #include <netinet/in.h>
     45  1.1      cgd #include <netdb.h>
     46  1.1      cgd #include <stdio.h>
     47  1.1      cgd #include <ctype.h>
     48  1.1      cgd 
     49  1.1      cgd netfinger(name)
     50  1.1      cgd 	char *name;
     51  1.1      cgd {
     52  1.1      cgd 	extern int lflag;
     53  1.1      cgd 	register FILE *fp;
     54  1.1      cgd 	register int c, lastc;
     55  1.1      cgd 	struct in_addr defaddr;
     56  1.1      cgd 	struct hostent *hp, def;
     57  1.1      cgd 	struct servent *sp;
     58  1.1      cgd 	struct sockaddr_in sin;
     59  1.1      cgd 	int s;
     60  1.1      cgd 	char *alist[1], *host, *rindex();
     61  1.1      cgd 	u_long inet_addr();
     62  1.1      cgd 
     63  1.1      cgd 	if (!(host = rindex(name, '@')))
     64  1.1      cgd 		return;
     65  1.1      cgd 	*host++ = NULL;
     66  1.1      cgd 	if (!(hp = gethostbyname(host))) {
     67  1.1      cgd 		defaddr.s_addr = inet_addr(host);
     68  1.1      cgd 		if (defaddr.s_addr == -1) {
     69  1.1      cgd 			(void)fprintf(stderr,
     70  1.1      cgd 			    "finger: unknown host: %s\n", host);
     71  1.1      cgd 			return;
     72  1.1      cgd 		}
     73  1.1      cgd 		def.h_name = host;
     74  1.1      cgd 		def.h_addr_list = alist;
     75  1.1      cgd 		def.h_addr = (char *)&defaddr;
     76  1.1      cgd 		def.h_length = sizeof(struct in_addr);
     77  1.1      cgd 		def.h_addrtype = AF_INET;
     78  1.1      cgd 		def.h_aliases = 0;
     79  1.1      cgd 		hp = &def;
     80  1.1      cgd 	}
     81  1.1      cgd 	if (!(sp = getservbyname("finger", "tcp"))) {
     82  1.1      cgd 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
     83  1.1      cgd 		return;
     84  1.1      cgd 	}
     85  1.1      cgd 	sin.sin_family = hp->h_addrtype;
     86  1.1      cgd 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
     87  1.1      cgd 	sin.sin_port = sp->s_port;
     88  1.1      cgd 	if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
     89  1.1      cgd 		perror("finger: socket");
     90  1.1      cgd 		return;
     91  1.1      cgd 	}
     92  1.1      cgd 
     93  1.1      cgd 	/* have network connection; identify the host connected with */
     94  1.1      cgd 	(void)printf("[%s]\n", hp->h_name);
     95  1.1      cgd 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
     96  1.1      cgd 		perror("finger: connect");
     97  1.1      cgd 		(void)close(s);
     98  1.1      cgd 		return;
     99  1.1      cgd 	}
    100  1.1      cgd 
    101  1.1      cgd 	/* -l flag for remote fingerd  */
    102  1.1      cgd 	if (lflag)
    103  1.1      cgd 		write(s, "/W ", 3);
    104  1.1      cgd 	/* send the name followed by <CR><LF> */
    105  1.1      cgd 	(void)write(s, name, strlen(name));
    106  1.1      cgd 	(void)write(s, "\r\n", 2);
    107  1.1      cgd 
    108  1.1      cgd 	/*
    109  1.1      cgd 	 * Read from the remote system; once we're connected, we assume some
    110  1.1      cgd 	 * data.  If none arrives, we hang until the user interrupts.
    111  1.1      cgd 	 *
    112  1.1      cgd 	 * If we see a <CR> or a <CR> with the high bit set, treat it as
    113  1.1      cgd 	 * a newline; if followed by a newline character, only output one
    114  1.1      cgd 	 * newline.
    115  1.1      cgd 	 *
    116  1.1      cgd 	 * Otherwise, all high bits are stripped; if it isn't printable and
    117  1.1      cgd 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
    118  1.1      cgd 	 * character with bit 7 set is printable.
    119  1.1      cgd 	 */
    120  1.1      cgd 	if (fp = fdopen(s, "r"))
    121  1.1      cgd 		while ((c = getc(fp)) != EOF) {
    122  1.1      cgd 			c &= 0x7f;
    123  1.1      cgd 			if (c == 0x0d) {
    124  1.1      cgd 				c = '\n';
    125  1.1      cgd 				lastc = '\r';
    126  1.1      cgd 			} else {
    127  1.1      cgd 				if (!isprint(c) && !isspace(c))
    128  1.1      cgd 					c |= 0x40;
    129  1.1      cgd 				if (lastc != '\r' || c != '\n')
    130  1.1      cgd 					lastc = c;
    131  1.1      cgd 				else {
    132  1.1      cgd 					lastc = '\n';
    133  1.1      cgd 					continue;
    134  1.1      cgd 				}
    135  1.1      cgd 			}
    136  1.1      cgd 			putchar(c);
    137  1.1      cgd 		}
    138  1.1      cgd 	if (lastc != '\n')
    139  1.1      cgd 		putchar('\n');
    140  1.1      cgd 	(void)fclose(fp);
    141  1.1      cgd }
    142