home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / VirtualWiFihelper.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  16.9 KB  |  469 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\VirtualWiFihelper
  4.  * File Name: VirtualWiFihelper.cpp
  5.  * Purpose  : Implements utilities for checking the correctness of VirtualWiFi
  6.  */
  7.  
  8. #include <malloc.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <sys/types.h>
  13. #include <windows.h>
  14. #include <io.h>
  15. #include <string.h>
  16.  
  17. #include "ioctl.h"
  18.  
  19. LPCTSTR DriverName = TEXT("\\\\.\\VWiFi");
  20.  
  21. /*
  22. VOID Usage(char *binName)
  23. {
  24.     printf("Usage:  %s <adapter>\n", binName);
  25.     printf("V. IMP! DO NOT SWITCH THIS SERVICE OFF BEFORE UNINSTALLING VirtualWiFi\n"); 
  26.     exit(1);
  27. }
  28. */
  29.  
  30. VOID ioctlSet(DWORD dwIOControlCode,
  31.        LPVOID lpInBuffer,
  32.        DWORD lpInBufferSize)
  33. {
  34.  
  35.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  36.     BOOL b;
  37.     ULONG   bytesreturned;
  38.     UCHAR    testInput[10];
  39.     //ULONG   testLong;
  40.     char lpOutString[50];
  41.  
  42.     //RtlCopyMemory(testInput, lpInBuffer, lpInBufferSize);
  43.     //printf("The buffer is %s, buffer size is %d, string length %d\n", testInput, lpInBufferSize, strlen(testInput)); 
  44.     // testLong = atol(testInput);
  45.     //printf("The long input is %ld\n", testLong);
  46.  
  47.     //printf("Trying to open %s\n", DriverName);
  48.     
  49.     hDriver = CreateFile(DriverName,
  50.                          GENERIC_READ | GENERIC_WRITE, 
  51.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  52.                          0,                     // Default security
  53.                          OPEN_EXISTING,
  54.                          0,  
  55.                          0);                    // No template
  56.     if(hDriver == INVALID_HANDLE_VALUE) {
  57.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  58.         OutputDebugString(TEXT(lpOutString));
  59.     // Otherwise, print success and close the driver
  60.     } else {
  61.         sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  62.         OutputDebugString(TEXT(lpOutString));
  63.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  64.                     dwIOControlCode, // control code of operation to perform
  65.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  66.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  67.                     NULL,            //lpOutBuffer, pointer to buffer to receive output data
  68.                     0,               //nOutBufferSize, in bytes, of output buffer
  69.                     &bytesreturned,  // pointer to variable to receive byte count
  70.                     NULL         // pointer to structure for asynchronous 
  71.                     );
  72.         sprintf(lpOutString, "IOCTL performed\n");
  73.         OutputDebugString(TEXT(lpOutString));
  74.         if (!b) {
  75.             sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
  76.             OutputDebugString(TEXT(lpOutString));
  77.         } 
  78.         CloseHandle(hDriver); // Close the driver
  79.     }
  80. }
  81.  
  82. VOID ioctlGet(DWORD dwIOControlCode,
  83.        LPVOID lpOutBuffer,
  84.        DWORD lpOutBufferSize)
  85. {
  86.  
  87.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  88.     BOOL b;
  89.     ULONG   bytesreturned;
  90.     char lpOutString[50];
  91.  
  92.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  93.     
  94.     hDriver = CreateFile(DriverName,
  95.                          GENERIC_READ | GENERIC_WRITE, 
  96.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  97.                          0,                     // Default security
  98.                          OPEN_EXISTING,
  99.                          0,  
  100.                          0);                    // No template
  101.     if(hDriver == INVALID_HANDLE_VALUE) {
  102.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  103.         OutputDebugString(TEXT(lpOutString));
  104.     // Otherwise, print success and close the driver
  105.     } else {
  106.         sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  107.         OutputDebugString(TEXT(lpOutString));
  108.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  109.                     dwIOControlCode, // control code of operation to perform
  110.                     NULL,            //lpInBuffer, pointer to buffer to supply input data
  111.                     0,               //nInBufferSize, in bytes, of input buffer
  112.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  113.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  114.                     &bytesreturned,  // pointer to variable to receive byte count
  115.                     NULL         // pointer to structure for asynchronous 
  116.                     );
  117.         sprintf(lpOutString, "IOCTL performed\n");
  118.         OutputDebugString(TEXT(lpOutString));
  119.         if (!b) {
  120.             sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
  121.             OutputDebugString(TEXT(lpOutString));
  122.         } 
  123.         else {
  124.             sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %s, bytes received %d\n", lpOutBuffer, bytesreturned);
  125.             OutputDebugString(TEXT(lpOutString));
  126.         }
  127.         CloseHandle(hDriver); // Close the driver
  128.     }
  129. }
  130.  
  131. ULONG ioctlGetFromAdapter(DWORD dwIOControlCode,
  132.        LPVOID lpInBuffer,
  133.        DWORD  lpInBufferSize,
  134.        LPVOID lpOutBuffer,
  135.        DWORD  lpOutBufferSize)
  136. {
  137.  
  138.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  139.     BOOL b;
  140.     ULONG   bytesreturned;
  141.     ULONG output;
  142.     char lpOutString[50];
  143.  
  144.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  145.  
  146.     hDriver = CreateFile(DriverName,
  147.                          GENERIC_READ | GENERIC_WRITE, 
  148.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  149.                          0,                     // Default security
  150.                          OPEN_EXISTING,
  151.                          0,  
  152.                          0);                    // No template
  153.     if(hDriver == INVALID_HANDLE_VALUE) {
  154.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  155.         OutputDebugString(TEXT(lpOutString));
  156.     // Otherwise, print success and close the driver
  157.     } else {
  158.         sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  159.         OutputDebugString(TEXT(lpOutString));
  160.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  161.                     dwIOControlCode, // control code of operation to perform
  162.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  163.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  164.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  165.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  166.                     &bytesreturned,  // pointer to variable to receive byte count
  167.                     NULL         // pointer to structure for asynchronous 
  168.                     );
  169.         sprintf(lpOutString, "IOCTL performed\n");
  170.         OutputDebugString(TEXT(lpOutString));
  171.         if (!b) {
  172.             sprintf(lpOutString, "IOCTL FAILED! %ld\n", GetLastError());
  173.             OutputDebugString(TEXT(lpOutString));
  174.             CloseHandle(hDriver); // Close the driver
  175.         } 
  176.         else {
  177.             sscanf((char *)lpOutBuffer, "%ld", &output);
  178.             //sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %d, bytes received %d\n", 
  179.             //    output, bytesreturned);
  180.             //OutputDebugString(TEXT(lpOutString));
  181.             CloseHandle(hDriver); // Close the driver
  182.             return output;
  183.         }
  184.     }
  185.     return 0;
  186. }
  187.  
  188. ULONG ioctlGetULongFromAdapter(DWORD dwIOControlCode,
  189.        LPVOID lpInBuffer,
  190.        DWORD  lpInBufferSize)
  191. {
  192.  
  193.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  194.     BOOL b;
  195.     ULONG   bytesreturned;
  196.     ULONG   recvdULong;
  197.     char lpOutString[50];
  198.  
  199.     sprintf(lpOutString, "Trying to open %s\n", DriverName);
  200.  
  201.     hDriver = CreateFile(DriverName,
  202.                          GENERIC_READ | GENERIC_WRITE, 
  203.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  204.                          0,                     // Default security
  205.                          OPEN_EXISTING,
  206.                          0,  
  207.                          0);                    // No template
  208.     if(hDriver == INVALID_HANDLE_VALUE) {
  209.         sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  210.         OutputDebugString(TEXT(lpOutString));
  211.     // Otherwise, print success and close the driver
  212.     } else {
  213.         sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
  214.         OutputDebugString(TEXT(lpOutString));
  215.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  216.                     dwIOControlCode, // control code of operation to perform
  217.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  218.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  219.                     //lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  220.                     //lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  221.                     (LPVOID)&recvdULong,
  222.                     sizeof(ULONG),
  223.                     &bytesreturned,  // pointer to variable to receive byte count
  224.                     NULL         // pointer to structure for asynchronous 
  225.                     );
  226.         sprintf(lpOutString, "IOCTL performed\n");
  227.         OutputDebugString(TEXT(lpOutString));
  228.         if (!b) {
  229.             sprintf(lpOutString, "IOCTL FAILED! %s\n", GetLastError());
  230.             OutputDebugString(TEXT(lpOutString));
  231.             CloseHandle(hDriver); // Close the driver
  232.         } 
  233.         else {
  234.             sprintf(lpOutString, "IOCTL succeeded! long value is %ld, bytes received %d\n", 
  235.                 recvdULong, bytesreturned);
  236.             OutputDebugString(TEXT(lpOutString));
  237.             CloseHandle(hDriver); // Close the driver
  238.         }
  239.     }
  240.     return recvdULong;
  241. }
  242.  
  243. ULONG ioctlGetSSIDFromAdapter(DWORD dwIOControlCode,
  244.        LPVOID lpInBuffer,
  245.        DWORD  lpInBufferSize,
  246.        LPVOID lpOutBuffer,
  247.        DWORD  lpOutBufferSize)
  248. {
  249.  
  250.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  251.     BOOL b;
  252.     ULONG   bytesreturned;
  253.  
  254.     //printf("Trying to open %s\n", DriverName);
  255.     
  256.     hDriver = CreateFile(DriverName,
  257.                          GENERIC_READ | GENERIC_WRITE, 
  258.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  259.                          0,                     // Default security
  260.                          OPEN_EXISTING,
  261.                          0,  
  262.                          0);                    // No template
  263.     if(hDriver == INVALID_HANDLE_VALUE) {
  264.             printf("VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  265.     // Otherwise, print success and close the driver
  266.     } else {
  267.         //printf("VWiFi.sys - CreateFile Success.\n");
  268.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  269.                     dwIOControlCode, // control code of operation to perform
  270.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  271.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  272.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  273.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  274.                     &bytesreturned,  // pointer to variable to receive byte count
  275.                     NULL         // pointer to structure for asynchronous 
  276.                     );
  277.         //printf("IOCTL performed\n");
  278.         if (!b) {
  279.             printf("IOCTL FAILED!\n", GetLastError());
  280.             CloseHandle(hDriver); // Close the driver
  281.         } 
  282.         else {
  283.         }
  284.     }
  285.     return bytesreturned;
  286. }
  287.  
  288. ULONG ioctlGetULongLongFromAdapter(DWORD dwIOControlCode,
  289.        LPVOID lpInBuffer,
  290.        DWORD  lpInBufferSize,
  291.        LPVOID lpOutBuffer,
  292.        DWORD  lpOutBufferSize)
  293. {
  294.  
  295.     HANDLE  hDriver = NULL;    // handle to the Snap.sys driver
  296.     BOOL b;
  297.     ULONG   bytesreturned;
  298.  
  299.     //printf("Trying to open %s\n", DriverName);
  300.     
  301.     hDriver = CreateFile(DriverName,
  302.                          GENERIC_READ | GENERIC_WRITE, 
  303.                          FILE_SHARE_READ | FILE_SHARE_WRITE,
  304.                          0,                     // Default security
  305.                          OPEN_EXISTING,
  306.                          0,  
  307.                          0);                    // No template
  308.     if(hDriver == INVALID_HANDLE_VALUE) {
  309.             printf("VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
  310.     // Otherwise, print success and close the driver
  311.     } else {
  312.         //printf("VWiFi.sys - CreateFile Success.\n");
  313.         b = DeviceIoControl(hDriver,         // handle to a device, file, or directory 
  314.                     dwIOControlCode, // control code of operation to perform
  315.                     lpInBuffer,      //lpInBuffer, pointer to buffer to supply input data
  316.                     lpInBufferSize,  //nInBufferSize, in bytes, of input buffer
  317.                     lpOutBuffer,      //lpOutBuffer, pointer to buffer to receive output data
  318.                     lpOutBufferSize,  //nOutBufferSize, in bytes, of output buffer
  319.                     &bytesreturned,  // pointer to variable to receive byte count
  320.                     NULL         // pointer to structure for asynchronous 
  321.                     );
  322.         //printf("IOCTL performed\n");
  323.         if (!b) {
  324.             printf("IOCTL FAILED!\n", GetLastError());
  325.             CloseHandle(hDriver); // Close the driver
  326.         } 
  327.         else {
  328.         }
  329.     }
  330.     return bytesreturned;
  331. }
  332.  
  333.  
  334. VOID Usage()
  335. {
  336.     printf("Usage: VirtualWiFihelper -op <operation>\n");
  337.     printf("     <operation> : getNumNetworks      to get the number of networks \n");
  338.     printf("                 : getSwitchTime       to get the network switch time\n");
  339.     printf("                 : getWaitTime         to get the network wait time\n");
  340.     printf("                 : getSwitchAlgo       to get the switching strategy\n");
  341.     printf("                 : getSSID             to get the current SSID\n");
  342.     printf("                 : getMode             to get the current network mode\n");
  343.     printf("                 : getCardSwitchTime   to get the real network switch time\n");
  344.     exit(1);
  345. }
  346.  
  347. int __cdecl main(INT argc,CHAR **argv)
  348. {
  349.     if(argc < 3)
  350.         Usage();
  351.     if(_stricmp(argv[1], "-op"))
  352.         Usage();
  353.  
  354.     if(!_stricmp(argv[2], "getSwitchTime")) {
  355.         char *adapterNum = "1";
  356.         ULONG switchTime, tempSwitchTime;
  357.  
  358.         switchTime = ioctlGetFromAdapter(IOCTL_GET_SWITCH_TIME, adapterNum, 
  359.             (DWORD)strlen(adapterNum), &tempSwitchTime, sizeof(ULONG));
  360.         printf("The switch time for the current network is %d ms\n", switchTime);
  361.     }
  362.     else if(!_stricmp(argv[2], "getWaitTime")) {
  363.         char *adapterNum = "1";
  364.         ULONG waitTime, tempWaitTime;
  365.  
  366.         waitTime = ioctlGetFromAdapter(IOCTL_GET_WAIT_TIME, adapterNum, 
  367.             (DWORD)strlen(adapterNum), &tempWaitTime, sizeof(ULONG));
  368.         printf("The wait time for the current network is %d ms\n", waitTime);
  369.     }
  370.     else if(!_stricmp(argv[2], "getSwitchAlgo")) {
  371.         char *adapterNum = "1";
  372.         ULONG isAdaptiveScheduling = 0;
  373.         ULONG adaptiveScheduling = 0;
  374.  
  375.         adaptiveScheduling = ioctlGetFromAdapter(IOCTL_GET_SCHEDULING_SCHEME, adapterNum, 
  376.             (DWORD)strlen(adapterNum), &isAdaptiveScheduling, sizeof(ULONG));
  377.         if(adaptiveScheduling)
  378.             printf("Scheduling scheme is ADAPTIVE\n");
  379.         else
  380.             printf("Scheduling scheme is NON-ADAPTIVE\n");
  381.     }
  382.     else if(!_stricmp(argv[2], "getssid")) {
  383.         char currentSSID[32];
  384.         char *adapterNum = "1";
  385.         ULONG ssidLength;
  386.  
  387.         ssidLength = ioctlGetSSIDFromAdapter(IOCTL_GET_CURRENT_SSID, adapterNum, 
  388.             (DWORD)strlen(adapterNum), ¤tSSID, sizeof(currentSSID));
  389.  
  390.         currentSSID[ssidLength] = '\0';
  391.  
  392.         printf("The current ssid is %s, length %d\n", currentSSID, ssidLength);
  393.     }
  394.     else if(!_stricmp(argv[2], "getmode")) {
  395.         ULONG currentMode;
  396.         ULONG uCurrentMode;
  397.         char *adapterNum = "1";
  398.  
  399.         uCurrentMode = ioctlGetFromAdapter(IOCTL_GET_CURRENT_MODE, adapterNum, 
  400.             (DWORD)strlen(adapterNum), ¤tMode, sizeof(ULONG));
  401.  
  402.         if (uCurrentMode == MODE_IS)
  403.             printf("The current mode is Infrastructure\n");
  404.         else if (uCurrentMode == MODE_AH)
  405.             printf("The current mode is Ad hoc\n");
  406.         else
  407.             printf("The current mode is Unknown\n");
  408.     }
  409.     else if(!_stricmp(argv[2], "getcardswitchtime")) {
  410.         char cardSwitchTime[64];
  411.         char *adapterNum = "1";
  412.         ULONG timeLength;
  413.         double switchTime;
  414.  
  415.         timeLength = ioctlGetULongLongFromAdapter(IOCTL_GET_CARD_SWITCH_TIME, adapterNum, 
  416.             (DWORD)strlen(adapterNum), &cardSwitchTime, sizeof(cardSwitchTime));
  417.  
  418.         cardSwitchTime[timeLength] = '\0';
  419.         /*
  420.         switchTime = (double)cardSwitchTime/1000;
  421.  
  422.         printf("The card switch time is %f length %d\n", switchTime, timeLength);
  423.         */
  424.         switchTime = (double)atol(cardSwitchTime)/10000;
  425.         printf("The card switch Time is %f ms\n", switchTime);
  426.     }
  427.     else if(!_stricmp(argv[2], "setssid")) {
  428.         NetworkModeAndSSID networkModeAndSSID;
  429.         strcpy(networkModeAndSSID.networkSSID, "rover\0");
  430.         networkModeAndSSID.adapterNum = "1";
  431.         networkModeAndSSID.ssidLength = (ULONG)strlen(networkModeAndSSID.networkSSID);
  432.         networkModeAndSSID.networkMode = MODE_IS;
  433.  
  434.         ioctlSet(IOCTL_SET_CURRENT_SSID, &networkModeAndSSID, (DWORD) sizeof(networkModeAndSSID));
  435.  
  436.     }
  437.     else if(!_stricmp(argv[2], "setparam")) {
  438.         SetVirtualWiFiTimers setVirtualWiFiTimers;
  439.         setVirtualWiFiTimers.adapterNum = "1";
  440.         
  441.         if(argc < 5) Usage();
  442.  
  443.         setVirtualWiFiTimers.switchTime = atol(argv[3]);
  444.         setVirtualWiFiTimers.waitTime = atol(argv[4]);
  445.  
  446.         ioctlSet(IOCTL_SET_SWITCHING_PARAMETERS, &setVirtualWiFiTimers, 
  447.             (DWORD)sizeof(SetVirtualWiFiTimers));
  448.     }
  449.     else if(!_stricmp(argv[2], "switchssid")) {
  450.         char *adapterNum = "1";
  451.         
  452.         ioctlSet(IOCTL_SWITCH_SSID, adapterNum, sizeof(adapterNum));
  453.         Sleep(500);
  454.         ioctlSet(IOCTL_SEND_BUFFERED_PACKETS, adapterNum, sizeof(adapterNum));
  455.     }
  456.     else if(!_stricmp(argv[2], "getNumNetworks")) {
  457.         ULONG numNetworks = 0;
  458.         ULONG uNumNetworks = 0;
  459.         char *adapterNum = "1";
  460.  
  461.         uNumNetworks = ioctlGetFromAdapter(IOCTL_GET_NUM_NETWORKS, adapterNum, 
  462.             (DWORD)strlen(adapterNum), &numNetworks, sizeof(ULONG));
  463.         
  464.         printf("Number of Networks %d\n", uNumNetworks);
  465.     } else {
  466.         Usage();
  467.     }
  468. }
  469.