home *** CD-ROM | disk | FTP | other *** search
/ BUG 2 / BUGCD1997_01.iso / internet / winproxy / data.z / ProxyLog.c next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  6.2 KB  |  300 lines

  1. // This is the proxy log application that will
  2. // recieve messages from the proxy and log them.
  3.  
  4. // What happens is this guy listens on a port at an IP address
  5. // and when the Proxy Server starts up, it connects to this and 
  6. // sends all of its debug messages here
  7.  
  8. #define        PROXYLOG_VERSION    "0.93"
  9.  
  10. #include <windows.h>
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15.  
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19.  
  20. int Port=8000;
  21.  
  22. HANDLE StdIn;
  23.  
  24. void GetKey( void )
  25. {
  26.     printf( "\nPress any key to continue" );
  27.     getch();
  28.     printf("\n");
  29. }
  30.  
  31. int Wait( void )
  32. {
  33. INPUT_RECORD Input;
  34. long NumRead=0;
  35.  
  36.     GetNumberOfConsoleInputEvents( StdIn, &NumRead );
  37.     if( NumRead == 0 )
  38.         return( TRUE );
  39.  
  40.     ReadConsoleInput( StdIn, &Input, 1, &NumRead );
  41.     if( !NumRead )
  42.         return( TRUE );
  43.  
  44.     if( Input.EventType == KEY_EVENT && Input.Event.KeyEvent.bKeyDown )
  45.     {
  46.         if( Input.Event.KeyEvent.uChar.AsciiChar == 'x' 
  47.             || Input.Event.KeyEvent.uChar.AsciiChar == 'X' 
  48.             )
  49.             return( FALSE );
  50.     }
  51.  
  52.     return( TRUE );
  53. }
  54.  
  55. void main( int argc, char **argv, char **argp )
  56. {
  57. WSADATA SocketData;
  58. SOCKET NewSock=INVALID_SOCKET, MySock=INVALID_SOCKET;
  59. fd_set Set;
  60. char *FileName=NULL;
  61. int FileHandle=(-1);
  62.  
  63.     printf("\nWinProxy logging application V"PROXYLOG_VERSION"\n\n" );
  64.  
  65.     StdIn = GetStdHandle( STD_INPUT_HANDLE );
  66.  
  67.     if( argc <= 1 )
  68.     {
  69.         printf("\nNo port number specified, listening for a logging connection on port %d\n\n", Port );
  70.     }
  71.     else
  72.     {
  73.         argc--;
  74.         argv++;
  75.  
  76.         while( argc )
  77.         {
  78.         char *String = *argv;
  79.         int NewPort;
  80.  
  81.             if( *String == '/' )
  82.             {
  83.                 switch( String[1] )
  84.                 {
  85.                 case 'P':
  86.                 case 'p':
  87.                     NewPort = atoi( &String[3] );
  88.                     if( Port <= 1024 || Port >= 32000 )
  89.                     {
  90.                         printf( "\nThe port number must be between 1024 and 32000\n\
  91. Using port %d instead.\n\n", Port );
  92.                     }
  93.                     else
  94.                         Port = NewPort;
  95.  
  96.                     break;
  97.  
  98.                 case 'F':
  99.                 case 'f':
  100.                     FileName = &String[3];
  101.                     printf("\nSending output to file %s\n", FileName );
  102.                     break;
  103.  
  104.                 default:
  105.                     printf("\nUsage:\n\n\
  106. ProxyLog [/P:portnum] [/F:filename]\n\n\
  107. portnum   must be between 1024 and 32,000 and designates the TCP/IP port where the application\n\
  108.           will listen for a connection.  If no port is specified, then port %d will be used.\n\n\
  109. filename  optionally specifies a filename that will be used for logging.  All logged data\n\
  110.           is saved to this file.\n\n", Port );
  111.                     return;
  112.                 }
  113.             }
  114.  
  115.             argc--;
  116.             argv++;
  117.         }
  118.     }
  119.  
  120.     if( FileName )
  121.     {
  122.         FileHandle = open( FileName, _O_APPEND|_O_CREAT|_O_WRONLY, _S_IREAD|_S_IWRITE );
  123.  
  124.         if( FileHandle == (-1) )
  125.         {
  126.             printf("\nUnable to open file %d for writing\n", FileName );
  127.             GetKey();
  128.             return;
  129.         }
  130.     }
  131.  
  132.     if( WSAStartup( 0x0101, &SocketData ) )
  133.     {
  134.         printf("Unable to start up Winsock!!\n\n" );
  135.         GetKey();
  136.         return;
  137.     }
  138.  
  139.     if( SocketData.wVersion < 0x0101 )
  140.     {
  141.         printf("Old version of Winsock present!\nVersion 1.1 is required!\n" );
  142.         GetKey();
  143.         goto AllDone;
  144.     }
  145.  
  146. ListenAgain:
  147.  
  148.     MySock = socket( PF_INET, SOCK_STREAM, 0 );
  149.     if( MySock == INVALID_SOCKET )
  150.     {
  151.         printf("Unable to create socket\n\n");
  152.         GetKey();
  153.         goto AllDone;
  154.     }
  155.  
  156.     {
  157.     SOCKADDR_IN sin;
  158.         sin.sin_family = AF_INET;
  159.         sin.sin_port = htons( (u_short)Port );
  160.         sin.sin_addr.s_addr = 0;  // Don't bind a any card in particular!
  161.  
  162.            if( bind( MySock, (LPSOCKADDR)&sin, sizeof( sin ) ) )
  163.            {
  164.                printf("Unable to bind socket to port %d\n\n", Port );
  165.             GetKey();
  166.                goto AllDone;
  167.            }
  168.     }
  169.  
  170.         // now the sokcet is ready and bound... listen for connection!
  171.     if( listen( MySock, 5 ) )
  172.     {
  173.         printf("WINSOCK error!  Unable to listen for connection on socket.\n\n" );
  174.         GetKey();
  175.         goto AllDone;
  176.     }
  177.  
  178.     printf("Waiting for a connection\nPress 'X' to abort\n\n" );
  179.  
  180.         // now just wait for a connection!
  181.     {
  182.     struct sockaddr Address;
  183.     int AddrLen;
  184.     struct timeval timeout;
  185.  
  186.         printf(" " );
  187.         while( 1 )
  188.         {
  189.         static char Spin[]="-/|\\";
  190.         static int CurChar=0;
  191.         static int Count=0;
  192.         int Ready;
  193.  
  194.                FD_ZERO( &Set );
  195.                FD_SET( MySock, &Set );
  196.                timeout.tv_sec = 0;
  197.                timeout.tv_usec = 0;
  198.    
  199.                Ready = select( 0, &Set, NULL, NULL, &timeout );
  200.    
  201.                if( Ready )
  202.                {
  203.                 AddrLen = sizeof( Address );
  204.                    NewSock = accept( MySock, &Address, &AddrLen );
  205.                    break;
  206.                }
  207.  
  208.             Count++;
  209.  
  210.             if( Count >= 1000 )
  211.             {
  212.                 Count = 0;
  213.                 printf("\b%c", Spin[CurChar++] );
  214.  
  215.                 if( Spin[CurChar] == '\0' )
  216.                     CurChar = 0;
  217.             }
  218.  
  219.             if( !Wait() )
  220.                 goto AllDone;
  221.         }
  222.  
  223.         if( NewSock == INVALID_SOCKET )
  224.         {
  225.             printf("Failed to connect to proxy server\n\n" );
  226.             goto AllDone;
  227.         }
  228.  
  229.         printf("Connected!!\n" );
  230.  
  231.             // Don't need this guy any more!
  232.         closesocket( MySock );
  233.         MySock = INVALID_SOCKET;
  234.     }
  235.  
  236.         // OK! now we are connected and ready to go!
  237.         // Just read the input from the socket and put it on
  238.         // the screen!
  239.     while( Wait() )
  240.     {
  241.     static struct fd_set ReadSet;
  242.     struct timeval timeout;
  243.     int HaveData;
  244.  
  245.         FD_ZERO( &Set );
  246.         FD_SET( NewSock, &Set );
  247.         timeout.tv_sec = 0;
  248.         timeout.tv_usec = 0;
  249.         
  250.         HaveData = select( 0, &Set, NULL, NULL, &timeout );
  251.  
  252.         if( HaveData )
  253.         {
  254.         static char Buffer[4096];
  255.         int NumRecieved, i;
  256.  
  257.             NumRecieved = recv( NewSock, Buffer, sizeof( Buffer ), 0 );
  258.  
  259.             if( NumRecieved == SOCKET_ERROR )
  260.             {
  261.                 printf("Socket error recieving.  WinProxy may have closed the socket\n");
  262.                 closesocket( NewSock );
  263.                 NewSock = INVALID_SOCKET;
  264.                 break;
  265.             }
  266.             else
  267.             for( i = 0; i < NumRecieved; i++ )
  268.                 putchar( Buffer[i] );
  269.  
  270.             if( NumRecieved && FileHandle != (-1) )
  271.             {
  272.                 write( FileHandle, Buffer, NumRecieved );
  273.                 write( FileHandle, Buffer, 0 );    // flush the buffers.
  274.             }
  275.  
  276.         }
  277.     }
  278.  
  279.     if( NewSock != INVALID_SOCKET )
  280.     {
  281.         closesocket( NewSock );
  282.         NewSock = INVALID_SOCKET;
  283.     }
  284.  
  285.     printf("Logging application completed successfully\n\
  286. Waiting for another connection.\n" );
  287.  
  288.     goto ListenAgain;    // yeah... can you believe it! oh, well... the whiles get nested
  289.                         // too deep otherwise... and I can't break out very well.  It is
  290.                         // just a sample.
  291.  
  292. AllDone:
  293.     if( MySock != INVALID_SOCKET )
  294.         closesocket( MySock );
  295.  
  296.     WSACleanup();
  297.  
  298.     if( FileHandle != (-1) )
  299.         close( FileHandle );
  300. }