/* * whoisclient.c - main * from Doug Comer Internetworking with TCP/IP vol. 1 pp. 357 */ #include #include #include #include #include #include #define bcopy(a,b,c) strncpy(b,a,c) /* -------------------------------------------------------------- * Prgram : whoisclient * Purpose : UNIX application program that becomes a client * for the Inetnet "whois" service. * Use : whois hostname username * Author : Barry Shein, Boston University * Date : January, 1987 * * Modification : Since the original program requires super-user * privilege we modify the program to use a * non-privilege port so every one can execute it. * We use port 2500 (randomly chosen). * By Xiannong Meng, Bucknell University, 1/16/93 *-------------------------------------------------------------- */ #define WHOISPORT 2500 /* a non-privilege port so all can use */ main(argc,argv) int argc; char *argv[]; /* C command line arguments */ { int s; /* socket descriptor */ int len; /* length of received data */ struct sockaddr_in sa; /* socket addr. structure */ struct hostent * hp; /* host entry */ struct servent * sp; /* service entry */ char buf[BUFSIZ + 1]; /* buffer to read whois info */ char *myname; /* pointer to name of this program */ char *host; /* pointer to remote host name */ char *user; /* pointer to 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]; user = argv[2]; /* * 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; /* * Put the whois socket number into the socket structure. */ sa.sin_port = WHOISPORT; /* * Allocate an open socket. */ if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } /* * Connect to the remote server. */ if (connect(s,(struct sockaddr *)(&sa),sizeof sa) < 0) { perror("connect"); exit(1); } /* * Send the request */ if (write(s,user,strlen(user)) != strlen(user)) { perror("write"); 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); }