137660Sbostic /* 237660Sbostic * Copyright (c) 1989 The Regents of the University of California. 337660Sbostic * All rights reserved. 437660Sbostic * 540027Sbostic * This code is derived from software contributed to Berkeley by 640027Sbostic * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 740027Sbostic * 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*40190Sbostic static char sccsid[] = "@(#)net.c 5.4 (Berkeley) 02/21/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 /* 92*40190Sbostic * Read from the remote system; once we're connected, we assume some 93*40190Sbostic * data. If none arrives, we hang until the user interrupts. 9437660Sbostic * 95*40190Sbostic * If we see a <CR> or a <CR> with the high bit set, treat it as 96*40190Sbostic * a newline; if followed by a newline character, only output one 97*40190Sbostic * newline. 9837660Sbostic * 9937660Sbostic * Otherwise, all high bits are stripped; if it isn't printable and 10037660Sbostic * it isn't a space, we can simply set the 7th bit. Every ASCII 10137660Sbostic * character with bit 7 set is printable. 10237660Sbostic */ 10337660Sbostic if (fp = fdopen(s, "r")) 10437660Sbostic while ((c = getc(fp)) != EOF) { 105*40190Sbostic c &= 0x7f; 106*40190Sbostic if (c == 0x0d) { 10737660Sbostic c = '\n'; 108*40190Sbostic lastc = '\r'; 109*40190Sbostic } else { 110*40190Sbostic if (!isprint(c) && !isspace(c)) 111*40190Sbostic c |= 0x40; 112*40190Sbostic if (lastc != '\r' || c != '\n') 113*40190Sbostic lastc = c; 114*40190Sbostic else { 115*40190Sbostic lastc = '\n'; 116*40190Sbostic continue; 117*40190Sbostic } 118*40190Sbostic } 11937660Sbostic putchar(c); 12037660Sbostic } 12137660Sbostic if (lastc != '\n') 12237660Sbostic putchar('\n'); 12337660Sbostic (void)fclose(fp); 12437660Sbostic } 125