home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tcpled10.zip / tcpleds.c < prev    next >
C/C++ Source or Header  |  1998-06-14  |  5KB  |  180 lines

  1. //
  2. // tcpleds -- TCP trafic monitor on your keyboard LEDs
  3. // started on 13.01.98
  4. // by Oleg Titov [oleg.titov@bigfoot.com]
  5. //
  6. #pragma comment (copyright ,"(c) 1998 by Oleg Titov; June 14, 1998.")
  7. #pragma comment (compiler)
  8. #pragma comment (date)
  9. #pragma comment (timestamp)
  10. //
  11. // Definitions
  12. //
  13. #define INCL_DOSDEVICES   // Device values 
  14. #define INCL_DOSDEVIOCTL  //
  15. #define INCL_DOSERRORS    // Error values 
  16. #define INCL_DOSPROCESS   // for DosSleep
  17. #define TCPV40HDRS        // all magic is her :)
  18. #define NumLock    0x0002 // Key definitions (NumLock)
  19. #define CapsLock   0x0004 // Key definitions (CapsLock)
  20. #define ScrollLock 0x0001 // Key definitions (ScrollLock)
  21. //
  22. // Header files
  23. //
  24. #include <os2.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. //
  28. // TCP/IP stuff starts here
  29. //
  30. #include <types.h>       // socket related
  31. #include <sys/socket.h>
  32. #include <sys/ioctl.h>   // ioctl related
  33. #include <netinet/tcp.h>
  34. //
  35. // Prototypes
  36. //
  37. VOID APIENTRY CloseAll(ULONG ulTermCode);
  38. //
  39. // some experiments goes here
  40. // i was playing around with some undoc functions
  41. //
  42. //APIRET16 APIENTRY16 WinSetTitle(PSZ String);
  43. //#pragma import (WinSetTitle, , "pmshapi", 93)
  44. //APIRET16 APIENTRY16 WinSetTitleAndIcon(PSZ szTitle, PSZ szIconPath);
  45. //#pragma import (WinSetTitleAndIcon, , "pmshapi", 97)
  46. //
  47. // Usefull variables
  48. //
  49. USHORT keystate = 0x00;
  50. ULONG  keystate_size = sizeof(keystate);
  51.  
  52. HFILE hf;
  53. ULONG ulAction;
  54. ULONG ulLength;
  55. SHIFTSTATE ss;    // ShiftState, i will use it to get CapsLock state 
  56.         // and to restore LEDs to right state on exit 
  57.         // (the last one is not implemented yet)
  58. int stat;
  59.  
  60.     ULONG    sleep    = 100; // default sleep time (in ms)
  61.     
  62.     ULONG
  63.         step    =   0; // by default we will see all traffic
  64.  
  65.     ULONG 
  66.         tcpsend    =   0, 
  67.         tcprcvd    =   0;
  68.  
  69.     int speed;
  70.  
  71.     int rc;
  72.  
  73.     struct tcpstat mystat;
  74. //
  75. // Main()
  76. //
  77. int main(int argc, char *argv[]) {
  78.  
  79.     // some undoc features os OS/2 :)
  80.     // WinSetTitle("tcp LEDs");
  81.  
  82.     // needs more work :(
  83.     // WinSetTitleAndIcon("tcp LEDs test", "D:\\Development\\TCPIP\\tcpleds\\green.ico");
  84.  
  85.     stat = socket(PF_INET, SOCK_RAW, 0); // i need a socket 
  86.                          // (even non-connected)
  87.     // handler for keyboard
  88.     rc = DosOpen("KBD$",
  89.                &hf,
  90.                &ulAction,
  91.                0L,
  92.                0,
  93.                FILE_OPEN,
  94.                OPEN_ACCESS_READONLY|OPEN_SHARE_DENYNONE,
  95.                0);
  96.  
  97.     // prepare the exit
  98.     DosExitList(EXLST_ADD,(PFNEXITLIST) CloseAll);
  99.  
  100.  
  101.     // let's rock that joint
  102.     if(argc > 1) {
  103.      speed = atoi(argv[1]);
  104.      if(argc == 3) sleep = atoi(argv[2]);
  105.      if(sleep < 10) sleep = 10;        // sleep time was too small
  106.      step = speed * sleep / 1000;        // recalculation of speed
  107.     }
  108.      
  109.     // from here till friday midnight
  110.     for(;;) {
  111.  
  112.     // getting statistics for interface
  113.     rc = ioctl(stat, SIOSTATTCP, (caddr_t) &mystat, sizeof(struct tcpstat));
  114.  
  115.     if(mystat.tcps_sndbyte - step > tcpsend)
  116.         keystate |= ScrollLock;
  117.     else 
  118.         keystate &= ~ScrollLock;
  119.  
  120.     tcpsend = mystat.tcps_sndbyte;
  121.  
  122.     if(mystat.tcps_rcvbyte - step > tcprcvd) 
  123.         keystate |= NumLock;
  124.     else 
  125.         keystate &= ~NumLock;
  126.  
  127.     tcprcvd = mystat.tcps_rcvbyte;
  128.  
  129.     //check for CapsLock
  130.     ulLength = sizeof(ss);
  131.  
  132.     rc = DosDevIOCtl(hf,
  133.                    IOCTL_KEYBOARD,
  134.                    KBD_GETSHIFTSTATE,
  135.                    0,
  136.                    0,
  137.                    &ulAction,
  138.                    &ss,
  139.                    sizeof(ss),
  140.                    &ulLength);
  141.  
  142.     if(ss.fsState & 64) 
  143.         keystate |= CapsLock;
  144.     else 
  145.         keystate &= ~CapsLock;
  146.  
  147.     //update LEDs
  148.  
  149.     rc = DosDevIOCtl(hf,            // Handle to device
  150.                     IOCTL_KEYBOARD,     // Category of request
  151.                     KBD_ALTERKBDLED,    // Function being requested
  152.                     (PVOID)&keystate,   // Input/Output parameter list
  153.                     sizeof(keystate),   // Maximum output parameter size
  154.                     &keystate_size,     // Input:  size of parameter list
  155.                                         // Output: size of parameters returned
  156.                     NULL,               // Input/Output data area
  157.                     0,                  // Maximum output data size
  158.                     0);                 // Input:  size of input data area
  159.                                         // Output: size of data returned
  160.  
  161.     // time to take some rest... whoaaa....
  162.     DosSleep(sleep);
  163.  
  164.     }// end of for()
  165.  
  166.     // we will never get here. pitty.
  167.     return 1;
  168. } // end of main()
  169.  
  170. VOID APIENTRY CloseAll(ULONG ulTermCode){
  171.     // closing keyboard
  172.     DosClose(hf);
  173.     // closing socket
  174.     soclose(stat);
  175.  
  176.     DosExitList(EXLST_EXIT,0);
  177.  
  178. }// end of CloseAll()
  179.  
  180.