/* whoisclient.c - main */ /* * APOLOGY and EXPLANATION * * This code was scanned using OmniPage Pro from Comer, D "Internetworking * With TCP/IP, Vol 1, 3rd Ed", PP357-359, then edited by hand. * It may have undiscovered errors. * * It has been modified to request the well-known "finger" * service, instead of "whois", because none of our machines at * Bendigo run a whois server. * * In order to connect to the finger server, a slight modification * has been made - the username is copied from the command line * into a local string, and a CRLF pair is appended before * it is written to the socket. * * PSc 16-4-97 * */ #include #include #include #include #include /* * ORIGINAL HEADER DOCUMENTATION FOLLOWS: * * Program: whoisclient * * Purpose: UNIX application program that becomes a client for the * Internet "whois" service. * * Use: whois hostname username * * Author: Barry Sheln, Boston University * Date: Janu~ry, 1987 * */ main(argc, argv) int argc; /* stanA~rd UNIX argument declarations */ char *argv[]; { int s; /* socket descriptor */ int len; /* length of received data */ struct sockaddr_in sa; /* Internet socket addr. structure */ struct hostent *hp; /* result of host name lookup */ struct servent *sp; /* result of service lookup */ char buf[BUFSIZ+1]; /* buffer to read finger information */ char *myname; /* pointer to name of this program */ char *host; /* pointer to remote host name */ char user[BUFSIZ+1]; /* space to hold remote user name */ myname = argv[0]; /* * Check that there are two command line arguments */ if(argc != 3) { fprintf(stderr, "Usage: %s host username\n", myname); exit(1); } host = argv[1]; strncpy(user, argv[2], BUFSIZ-2); strcat(user, "\n\r"); /* * Look up the specified hostname */ if((hp = gethostbyname(host)) == NULL) { fprintf(stderr,"%s: %s: no such host?\n", myname, host); exit(1); } /* * Put host's address and address type into socket structure */ bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; /* * Look up the socket number for the finger service */ if((sp = getservbyname("finger","tcp")) == NULL) { fprintf(stderr,"%s: No finger service on this host\n", myname); exit(1); } /* * Put the finger socket number into the socket structure. */ sa.sin_port = sp->s_port; /* * Allocate an open socket */ if((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } /* * Ccnnect to the remote server */ if(connect(s, &sa, sizeof sa) < 0) { perror("connect"); exit(1); } /* * Send the request */ if(write(s, user, strlen(user)) != strlen(user)) { fprintf(stderr, "%s: write error\n", myname); exit(1); } /* * Read the reply and put to user's output */ while( (len = read(s, buf, BUFSIZ)) > 0) write(1, buf, len); close(s); exit(0); }