home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ipspy140.zip / iptracx.c < prev    next >
C/C++ Source or Header  |  1998-05-31  |  6KB  |  204 lines

  1. //*****************************************************************************
  2. //***  NAME              : iptracx.c                                        ***
  3. //***  PART OF           : IpSpy                                            ***
  4. //***  SHORT DESCRIPTION : creates a iptrace file                           ***
  5. //--------------------------------------------------------------------------***
  6. //***  AUTHOR            : E.Buerkle                                        ***
  7. //***  CREATION DATE     : 08.04.97                                         ***
  8. //***  COPYRIGHT         : (C) 1997 E.Buerkle                               ***
  9. //*****************************************************************************
  10.  
  11. #define INCL_BASE
  12. #include <os2.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <stddef.h>
  17. #include <time.h>
  18.  
  19. #define  OS2
  20. #ifdef __EMX__
  21. #include <sys\types.h>
  22. #else
  23. #include <types.h>
  24. #endif
  25. #include <sys/socket.h>
  26. #include <netdb.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/select.h>
  29. #include <sys/ioctl.h>
  30.  
  31. #include <net\if.h>
  32. #include <net\route.h>
  33. #include <netinet\in.h>
  34. #include <netinet\in_systm.h>
  35. #include <netinet\ip.h>
  36. #include <netinet\ip_icmp.h>
  37. #include <netinet\if_ether.h>
  38. #include <netinet\tcp.h>
  39.  
  40. #include <ipspy.h>
  41.  
  42. #ifndef IP_MAXPACKET
  43. #define IP_MAXPACKET    65535           /* maximum packet size */
  44. #endif
  45.  
  46. typedef struct _STFRAMEDATA {
  47.   USHORT usType;
  48.   USHORT usLength;
  49.   ULONG  ulTimeStamp;
  50.   UCHAR  auchData[IP_MAXPACKET];
  51. } STFRAMEDATA;
  52.  
  53.  
  54. ULONG        ulHandle;
  55. UCHAR        auchInterface[IFNAMSIZ+1];
  56. USHORT       usOldMode;
  57. STFRAMEDATA  stFrameData;
  58.  
  59. //**************************************************************************
  60. //  Name       :  IpExitProc
  61. //  Description:  close interface and reset adapter to normal mode
  62. //  Parameters :  usTermCode - termination info
  63. //  Return     :  none
  64. //**************************************************************************
  65. VOID APIENTRY IpExitProc(ULONG usTermCode)
  66. {
  67.   APIRET rc;
  68.  
  69.   fflush(NULL);
  70.  
  71.   // end monitor
  72.   if((rc=IpSpy_Exit(ulHandle)) != RC_IPSPY_NOERROR)
  73.     printf("IpSpyExit Error: %d\n", rc);
  74.  
  75.   // ip stack relexation
  76.   if((rc=IpSpy_SetReceiveMode(usOldMode,
  77.                               auchInterface,
  78.                               NULL)) != RC_IPSPY_NOERROR)
  79.     printf("IpSpy_SetReceiveMode Error: %d\n", rc);
  80.  
  81.   printf("IPTracx ends\n");
  82. }
  83.  
  84. //**************************************************************************
  85. //  Name       :  main
  86. //  Description:
  87. //  Parameters :  argc argv env
  88. //  Return     :  none
  89. //**************************************************************************
  90. VOID main(ULONG argc, UCHAR **argv, UCHAR **env)
  91. {
  92.   APIRET rc;
  93.   UCHAR  *pVersion, **pIFs;
  94.   USHORT usUnknown;
  95.   UCHAR  *pSocketError;
  96.   ULONG  ulSocketError, i;
  97.   FILE   *fp;
  98.   ULONG  ulTime;
  99.  
  100.   // query all available interfaces
  101.   if((rc=IpSpy_QueryInterfaces(&pIFs)) != RC_IPSPY_NOERROR)
  102.   {
  103.     printf("IpSpyQueryIF Error: %d\n", rc);
  104.     return;
  105.   }
  106.  
  107.   // check arguments
  108.   if(argc != 2 || IFNAMSIZ < strlen(argv[1]))
  109.   {
  110.     printf("usage: iptracx [interface]\n");
  111.     printf(" where interface is one of:\n");
  112.     // show all available interfaces
  113.     if(pIFs != NULL)
  114.       for(i=0; pIFs[i] != 0; i++)
  115.         printf(" %s\n", pIFs[i]);
  116.  
  117.     return;
  118.   }
  119.  
  120.   strcpy(auchInterface, argv[1]);
  121.   printf("IPTracx [%s], quit with Ctrl-C\n", auchInterface);
  122.  
  123.   // view the version
  124.   if((rc=IpSpy_Version(&pVersion)) != RC_IPSPY_NOERROR)
  125.     printf("IpSpyVersion Error: %d\n", rc);
  126.   else
  127.     printf("IpSpy Version: %s\n", pVersion);
  128.  
  129.   // save receive mode
  130.   if((rc=IpSpy_QueryReceiveMode(&usOldMode, NULL)) != RC_IPSPY_NOERROR)
  131.     printf("IpSpy_QueryReceiveMode Error: %d\n", rc);
  132.  
  133.   // we want all packets
  134.   if((rc=IpSpy_SetReceiveMode(DIRECTED_MODE | BROADCAST_MODE | PROMISCUOUS_MODE,
  135.                               auchInterface,
  136.                               NULL)) != RC_IPSPY_NOERROR)
  137.   {
  138.     if(rc == RC_IPSPY_MODE_NOT_SUPPORTED)
  139.       printf("Promiscuous mode not supported\n");
  140.     else if(rc == RC_IPSPY_CANNOT_OPEN_DRIVER)
  141.       printf("IpSpy_SetReceiveMode Error: Cannot open ipspy.os2\n");
  142.     else
  143.       printf("IpSpy_SetReceiveMode Error: %d\n", rc);
  144.   }
  145.  
  146.   // init monitor
  147.   if((rc=IpSpy_Init(&ulHandle, auchInterface)) != RC_IPSPY_NOERROR)
  148.   {
  149.     if(rc == RC_IPSPY_SOCKET_ERROR)
  150.     {
  151.       IpSpy_GetLastSocketError(&ulSocketError, &pSocketError);
  152.       printf("IpSpyInit SocketError: [%d] %s\n", ulSocketError, pSocketError);
  153.     }
  154.     else
  155.       printf("IpSpyInit Error: %d\n", rc);
  156.     return;
  157.   }
  158.  
  159.   // good housekeeping
  160.   DosExitList(EXLST_ADD, IpExitProc);
  161.  
  162.   // opens the iptrace file
  163.   if((fp = fopen ("iptrace.dmp", "w+b")) == NULL)
  164.   {
  165.     printf("Could not open iptrace.dmp");
  166.     return;
  167.   }
  168.  
  169.   // loop forever
  170.   while(TRUE)
  171.   {
  172.     // read packet
  173.     stFrameData.usLength = sizeof(stFrameData.auchData);
  174.     if((rc=IpSpy_ReadRaw(ulHandle, stFrameData.auchData,
  175.                          &stFrameData.usLength, &stFrameData.usType,
  176.                          &stFrameData.ulTimeStamp, &usUnknown)) != RC_IPSPY_NOERROR)
  177.     {
  178.       printf("IpSpyRead Error: %d\n", rc);
  179.       return;
  180.     }
  181.  
  182.     // get systemtime
  183.     DosQuerySysInfo( QSV_MS_COUNT, QSV_MS_COUNT, &ulTime, sizeof(ulTime));
  184.  
  185.     printf("Packet [Type:%d Length:%d Time:%d msec]\t System [Time:%d msec]\n",
  186.               stFrameData.usType, stFrameData.usLength,
  187.               stFrameData.ulTimeStamp , ulTime);
  188.  
  189.     //
  190.     // if(MyFilter(.....) == FALSE)
  191.     //   continue;
  192.     //
  193.  
  194.     // write packet to iptrace.dmp
  195.     if(fwrite(&stFrameData, stFrameData.usLength + 8, 1, fp) != 1)
  196.     {
  197.       printf("Could not write to iptrace.dmp");
  198.       return;
  199.     }
  200.  
  201.   }
  202.  
  203. }
  204.