home *** CD-ROM | disk | FTP | other *** search
/ Windows NT Super Tune-Up Kit / PIE-WindowsNTSuperTuneUpKit-1997.iso / MAIL / MUDWHONT / MUDWHO.C next >
C/C++ Source or Header  |  1994-07-18  |  6KB  |  278 lines

  1. /*
  2.     Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
  3. */
  4.  
  5. #ifndef    lint
  6. static    char    RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RWHO/RCS/mudwho.c,v 1.6 91/06/16 18:33:51 mjr Exp $";
  7. #endif
  8.  
  9.  
  10. #include    <stdio.h>
  11. #include    <sys/types.h>
  12. #include    <errno.h>
  13. extern    int    errno;
  14. #include    <ctype.h>
  15. #include    <fcntl.h>
  16.  
  17. #ifdef WIN32
  18.  
  19. #include <winsock.h>
  20. #include <io.h>
  21. #include <time.h>
  22. #include <stdlib.h>
  23.  
  24. #else
  25.  
  26. #include    <sys/time.h>
  27. #include    <sys/file.h>
  28. #include    <sys/socket.h>
  29. #include    <netinet/in.h>
  30. #include    <netdb.h>
  31.  
  32. #endif
  33.  
  34. #define    STREAMPORT        6889
  35.  
  36. #define    ENVHOST        "MUDWHOSERVER"
  37. #define    ENVPORT        "MUDWHOPORT"
  38.  
  39.  
  40. #ifndef    NO_HUGE_RESOLVER_CODE 
  41. #ifndef WIN32
  42. extern    struct    hostent    *gethostbyname();
  43. #endif
  44. #endif
  45.  
  46. extern    char    *getenv();
  47. extern    char    *optarg;
  48.  
  49.  
  50. int usage()
  51. {
  52.  
  53.     fprintf(stderr,"usage: mudwho [-S server] [-P servport] [-u user] [-m mud]\n");
  54.     fprintf(stderr,"\twhere user is someone to scan for\n");
  55.     fprintf(stderr,"\tmud is a specific MUD to scan for\n");
  56.     fprintf(stderr,"\t%s and %s can be set in the environment\n",ENVPORT,ENVHOST);
  57.     return(1);
  58. }
  59.  
  60.  
  61. void main(argc,argv)
  62. int    argc;
  63. char    *argv[];
  64. {
  65.     struct    sockaddr_in    addr;
  66.     struct    hostent        *hp;
  67.     int            fd;
  68.     char            *p;
  69.     int            red;
  70.     char            rbuf[1024];
  71.     char            *srv;
  72.     u_short            portnum = STREAMPORT;
  73.     char            *checkwho = (char *)0;
  74.     char            *checkmud = (char *)0;
  75.     WSADATA WSAData;
  76.     int err;
  77.  
  78.  int current_arg = 1;
  79.    BOOLEAN more_args = TRUE;
  80.    BOOLEAN done = FALSE;
  81.   
  82.     srv = getenv("ENVHOST");
  83.     if (srv == NULL)
  84.        printf("get env host failed \n");
  85.  
  86.     if((p = getenv("ENVPORT")) != (char *)0)
  87.         portnum = atoi(p);
  88.  
  89. // lgk no getopt function in win32 so write it
  90. // what is it doing here?
  91.    /* process arguments */
  92.         
  93.     do 
  94.      {
  95.      // only if we have args
  96.       /* get the first one and if it doesn't start with a - we have a problem */
  97.     
  98.     if (argc > current_arg)
  99.       {    
  100.        switch (argv[current_arg][1])
  101.            {
  102.  
  103.             case 'S':    // get the server
  104.             if (argc < current_arg + 2)
  105.               exit(usage());
  106.             srv = argv[current_arg+1];
  107.             ++current_arg;
  108.             break;
  109.  
  110.         case 'P':
  111.             if (argc < current_arg + 2)
  112.               exit(usage());
  113.             portnum = atoi(argv[current_arg+1]);
  114.             ++current_arg;
  115.             break;
  116.  
  117.         case 'u':
  118.             if (argc < current_arg + 2)
  119.               exit(usage());
  120.             checkwho = argv[current_arg+1];
  121.             ++current_arg;
  122.             break;
  123.  
  124.         case 'm':
  125.             if (argc < current_arg + 2)
  126.               exit(usage());
  127.             checkmud = argv[current_arg+1];
  128.             ++current_arg;
  129.             break;
  130.  
  131.         default:
  132.             exit(usage());             // assign paffin value 
  133.     
  134.     } /* end of switch */
  135.                          
  136.     }           
  137.     ++current_arg;
  138.     if (current_arg >= argc)
  139.       more_args = FALSE;
  140.     
  141.  
  142.     } while ((!done) && (more_args));
  143.  
  144.  
  145.  
  146.     if(srv == (char *)0) {
  147.         fprintf(stderr,"rwho server host must be provided [-S host]\n");
  148.         exit(1);
  149.     }
  150.  
  151. /*    if(checkmud != (char *)0 && checkwho != (char *)0) {
  152.         fprintf(stderr,"You can only search for one user or MUD entry\n");
  153.         exit(1);
  154.     }
  155.  
  156. */    
  157.   if ((err = WSAStartup((WORD)0x0101, &WSAData))!=0)  // register task with
  158.   {                                         // winsock tcp/ip API
  159.     fprintf(stderr,"ERROR: WSAStartup failed errorcode = %d\n",WSAGetLastError());
  160.   } else 
  161.   {
  162.  
  163.     p = srv;
  164.     while(*p != '\0' && (*p == '.' || isdigit(*p)))
  165.         p++;
  166.  
  167.     /* not all digits or dots */
  168.     if(*p != '\0') 
  169.      {
  170. #ifndef    NO_HUGE_RESOLVER_CODE
  171.         if((hp = gethostbyname(srv)) == NULL)
  172.          {
  173.             fprintf(stderr,"unknown host %s\n",srv);
  174.             WSACleanup();
  175.             exit(1);                  
  176.         }
  177.    
  178.   // map host to ip, allow ip
  179.  
  180. #ifdef WIN32
  181.          memcpy((char *)&addr.sin_addr,hp->h_addr,hp->h_length);
  182. #else
  183.         (void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
  184. #endif
  185.  
  186. #else
  187.         fprintf(stderr,"must use numerical notation for host name\n");
  188.         exit(1);
  189. #endif
  190.     } else {
  191.         unsigned long    f;
  192.  
  193.         if((f = inet_addr(srv)) == -1L) 
  194.         {
  195.             fprintf(stderr,"unknown host %s\n",srv);
  196.             exit(1);
  197.         }
  198. #ifdef WIN32
  199.         memcpy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
  200. #else
  201.         (void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
  202.  
  203. #endif
  204.     }
  205. #ifdef WIN32
  206.     addr.sin_port = htons(portnum);
  207. #else
  208.     addr.sin_port = htons(portnum);
  209. #endif
  210.     addr.sin_family = AF_INET;
  211.  
  212.     if((fd = socket(AF_INET,SOCK_STREAM,0)) < 0) 
  213.     {
  214.         perror("socket");
  215.         WSACleanup();
  216.         exit(1);
  217.     }
  218.  
  219.   printf("Connecting to %s ...\n",inet_ntoa(addr.sin_addr));
  220.   fflush(stdout);
  221.  
  222.     if(connect(fd,
  223. #ifdef WIN32
  224. (struct sockaddr *)
  225. #endif
  226.  
  227. &addr,sizeof(addr)) < 0) 
  228.     {
  229.         perror("connect");
  230.         exit(1);
  231.     }
  232.  
  233.  // lgk the mudwho servers have changed and now do not support requests but
  234.  //on connection seem to return the data so modify program accordingly.
  235.  
  236.     /* send request if one */
  237.     if(checkmud != (char *)0 || checkwho != (char *)0) {
  238.         char    xuf[512];
  239.         int    xlen;
  240.  
  241.         sprintf(xuf,"%s=%.30s",
  242.             checkmud == (char *)0 ? "who" : "mud",
  243.             checkmud == (char *)0 ? checkwho : checkmud);
  244.  
  245.         xlen = strlen(xuf) + 1;
  246. // lgk cannot do write in win32 so change to send
  247. #ifdef WIN32
  248.         if(send(fd,xuf,xlen,0) != xlen) {
  249. #else
  250.         if(write(fd,xuf,xlen) != xlen) {
  251. #endif
  252.             perror("write to rwho server failed");
  253.             closesocket(fd);
  254.               WSACleanup();
  255.  
  256.             exit(1);
  257.         }
  258.     }
  259.  
  260. // lgk cannot do read in win32 so change to recv
  261. #ifdef WIN32
  262.     while((red = recv(fd,rbuf,sizeof(rbuf),0)) > 0)
  263.         write(1,rbuf,red);
  264.  
  265.         closesocket(fd);
  266.         WSACleanup();
  267.     exit(0);
  268. #else
  269.     while((red = read(fd,rbuf,sizeof(rbuf))) > 0)
  270.         write(1,rbuf,red);
  271.     exit(0);
  272.  
  273. #endif
  274.     
  275. }
  276. }
  277.  
  278.