home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\VirtualWiFihelper
- * File Name: VirtualWiFihelper.cpp
- * Purpose : Implements utilities for checking the correctness of VirtualWiFi
- */
-
- #include <malloc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <windows.h>
- #include <io.h>
- #include <string.h>
-
- #include "ioctl.h"
-
- LPCTSTR DriverName = TEXT("\\\\.\\VWiFi");
-
- /*
- VOID Usage(char *binName)
- {
- printf("Usage: %s <adapter>\n", binName);
- printf("V. IMP! DO NOT SWITCH THIS SERVICE OFF BEFORE UNINSTALLING VirtualWiFi\n");
- exit(1);
- }
- */
-
- VOID ioctlSet(DWORD dwIOControlCode,
- LPVOID lpInBuffer,
- DWORD lpInBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
- UCHAR testInput[10];
- //ULONG testLong;
- char lpOutString[50];
-
- //RtlCopyMemory(testInput, lpInBuffer, lpInBufferSize);
- //printf("The buffer is %s, buffer size is %d, string length %d\n", testInput, lpInBufferSize, strlen(testInput));
- // testLong = atol(testInput);
- //printf("The long input is %ld\n", testLong);
-
- //printf("Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- // Otherwise, print success and close the driver
- } else {
- sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
- OutputDebugString(TEXT(lpOutString));
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- lpInBuffer, //lpInBuffer, pointer to buffer to supply input data
- lpInBufferSize, //nInBufferSize, in bytes, of input buffer
- NULL, //lpOutBuffer, pointer to buffer to receive output data
- 0, //nOutBufferSize, in bytes, of output buffer
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- sprintf(lpOutString, "IOCTL performed\n");
- OutputDebugString(TEXT(lpOutString));
- if (!b) {
- sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- }
- CloseHandle(hDriver); // Close the driver
- }
- }
-
- VOID ioctlGet(DWORD dwIOControlCode,
- LPVOID lpOutBuffer,
- DWORD lpOutBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
- char lpOutString[50];
-
- sprintf(lpOutString, "Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- // Otherwise, print success and close the driver
- } else {
- sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
- OutputDebugString(TEXT(lpOutString));
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- NULL, //lpInBuffer, pointer to buffer to supply input data
- 0, //nInBufferSize, in bytes, of input buffer
- lpOutBuffer, //lpOutBuffer, pointer to buffer to receive output data
- lpOutBufferSize, //nOutBufferSize, in bytes, of output buffer
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- sprintf(lpOutString, "IOCTL performed\n");
- OutputDebugString(TEXT(lpOutString));
- if (!b) {
- sprintf(lpOutString, "IOCTL FAILED!\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- }
- else {
- sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %s, bytes received %d\n", lpOutBuffer, bytesreturned);
- OutputDebugString(TEXT(lpOutString));
- }
- CloseHandle(hDriver); // Close the driver
- }
- }
-
- ULONG ioctlGetFromAdapter(DWORD dwIOControlCode,
- LPVOID lpInBuffer,
- DWORD lpInBufferSize,
- LPVOID lpOutBuffer,
- DWORD lpOutBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
- ULONG output;
- char lpOutString[50];
-
- sprintf(lpOutString, "Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- // Otherwise, print success and close the driver
- } else {
- sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
- OutputDebugString(TEXT(lpOutString));
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- lpInBuffer, //lpInBuffer, pointer to buffer to supply input data
- lpInBufferSize, //nInBufferSize, in bytes, of input buffer
- lpOutBuffer, //lpOutBuffer, pointer to buffer to receive output data
- lpOutBufferSize, //nOutBufferSize, in bytes, of output buffer
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- sprintf(lpOutString, "IOCTL performed\n");
- OutputDebugString(TEXT(lpOutString));
- if (!b) {
- sprintf(lpOutString, "IOCTL FAILED! %ld\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- CloseHandle(hDriver); // Close the driver
- }
- else {
- sscanf((char *)lpOutBuffer, "%ld", &output);
- //sprintf(lpOutString, "IOCTL succeeded! lpoutBuffer value is %d, bytes received %d\n",
- // output, bytesreturned);
- //OutputDebugString(TEXT(lpOutString));
- CloseHandle(hDriver); // Close the driver
- return output;
- }
- }
- return 0;
- }
-
- ULONG ioctlGetULongFromAdapter(DWORD dwIOControlCode,
- LPVOID lpInBuffer,
- DWORD lpInBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
- ULONG recvdULong;
- char lpOutString[50];
-
- sprintf(lpOutString, "Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- sprintf(lpOutString, "VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- // Otherwise, print success and close the driver
- } else {
- sprintf(lpOutString, "VWiFi.sys - CreateFile Success.\n");
- OutputDebugString(TEXT(lpOutString));
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- lpInBuffer, //lpInBuffer, pointer to buffer to supply input data
- lpInBufferSize, //nInBufferSize, in bytes, of input buffer
- //lpOutBuffer, //lpOutBuffer, pointer to buffer to receive output data
- //lpOutBufferSize, //nOutBufferSize, in bytes, of output buffer
- (LPVOID)&recvdULong,
- sizeof(ULONG),
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- sprintf(lpOutString, "IOCTL performed\n");
- OutputDebugString(TEXT(lpOutString));
- if (!b) {
- sprintf(lpOutString, "IOCTL FAILED! %s\n", GetLastError());
- OutputDebugString(TEXT(lpOutString));
- CloseHandle(hDriver); // Close the driver
- }
- else {
- sprintf(lpOutString, "IOCTL succeeded! long value is %ld, bytes received %d\n",
- recvdULong, bytesreturned);
- OutputDebugString(TEXT(lpOutString));
- CloseHandle(hDriver); // Close the driver
- }
- }
- return recvdULong;
- }
-
- ULONG ioctlGetSSIDFromAdapter(DWORD dwIOControlCode,
- LPVOID lpInBuffer,
- DWORD lpInBufferSize,
- LPVOID lpOutBuffer,
- DWORD lpOutBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
-
- //printf("Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- printf("VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- // Otherwise, print success and close the driver
- } else {
- //printf("VWiFi.sys - CreateFile Success.\n");
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- lpInBuffer, //lpInBuffer, pointer to buffer to supply input data
- lpInBufferSize, //nInBufferSize, in bytes, of input buffer
- lpOutBuffer, //lpOutBuffer, pointer to buffer to receive output data
- lpOutBufferSize, //nOutBufferSize, in bytes, of output buffer
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- //printf("IOCTL performed\n");
- if (!b) {
- printf("IOCTL FAILED!\n", GetLastError());
- CloseHandle(hDriver); // Close the driver
- }
- else {
- }
- }
- return bytesreturned;
- }
-
- ULONG ioctlGetULongLongFromAdapter(DWORD dwIOControlCode,
- LPVOID lpInBuffer,
- DWORD lpInBufferSize,
- LPVOID lpOutBuffer,
- DWORD lpOutBufferSize)
- {
-
- HANDLE hDriver = NULL; // handle to the Snap.sys driver
- BOOL b;
- ULONG bytesreturned;
-
- //printf("Trying to open %s\n", DriverName);
-
- hDriver = CreateFile(DriverName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0, // Default security
- OPEN_EXISTING,
- 0,
- 0); // No template
- if(hDriver == INVALID_HANDLE_VALUE) {
- printf("VWiFi.sys CreateFile- Error %ld - Maybe its not INSTALLED\n", GetLastError());
- // Otherwise, print success and close the driver
- } else {
- //printf("VWiFi.sys - CreateFile Success.\n");
- b = DeviceIoControl(hDriver, // handle to a device, file, or directory
- dwIOControlCode, // control code of operation to perform
- lpInBuffer, //lpInBuffer, pointer to buffer to supply input data
- lpInBufferSize, //nInBufferSize, in bytes, of input buffer
- lpOutBuffer, //lpOutBuffer, pointer to buffer to receive output data
- lpOutBufferSize, //nOutBufferSize, in bytes, of output buffer
- &bytesreturned, // pointer to variable to receive byte count
- NULL // pointer to structure for asynchronous
- );
- //printf("IOCTL performed\n");
- if (!b) {
- printf("IOCTL FAILED!\n", GetLastError());
- CloseHandle(hDriver); // Close the driver
- }
- else {
- }
- }
- return bytesreturned;
- }
-
-
- VOID Usage()
- {
- printf("Usage: VirtualWiFihelper -op <operation>\n");
- printf(" <operation> : getNumNetworks to get the number of networks \n");
- printf(" : getSwitchTime to get the network switch time\n");
- printf(" : getWaitTime to get the network wait time\n");
- printf(" : getSwitchAlgo to get the switching strategy\n");
- printf(" : getSSID to get the current SSID\n");
- printf(" : getMode to get the current network mode\n");
- printf(" : getCardSwitchTime to get the real network switch time\n");
- exit(1);
- }
-
- int __cdecl main(INT argc,CHAR **argv)
- {
- if(argc < 3)
- Usage();
- if(_stricmp(argv[1], "-op"))
- Usage();
-
- if(!_stricmp(argv[2], "getSwitchTime")) {
- char *adapterNum = "1";
- ULONG switchTime, tempSwitchTime;
-
- switchTime = ioctlGetFromAdapter(IOCTL_GET_SWITCH_TIME, adapterNum,
- (DWORD)strlen(adapterNum), &tempSwitchTime, sizeof(ULONG));
- printf("The switch time for the current network is %d ms\n", switchTime);
- }
- else if(!_stricmp(argv[2], "getWaitTime")) {
- char *adapterNum = "1";
- ULONG waitTime, tempWaitTime;
-
- waitTime = ioctlGetFromAdapter(IOCTL_GET_WAIT_TIME, adapterNum,
- (DWORD)strlen(adapterNum), &tempWaitTime, sizeof(ULONG));
- printf("The wait time for the current network is %d ms\n", waitTime);
- }
- else if(!_stricmp(argv[2], "getSwitchAlgo")) {
- char *adapterNum = "1";
- ULONG isAdaptiveScheduling = 0;
- ULONG adaptiveScheduling = 0;
-
- adaptiveScheduling = ioctlGetFromAdapter(IOCTL_GET_SCHEDULING_SCHEME, adapterNum,
- (DWORD)strlen(adapterNum), &isAdaptiveScheduling, sizeof(ULONG));
- if(adaptiveScheduling)
- printf("Scheduling scheme is ADAPTIVE\n");
- else
- printf("Scheduling scheme is NON-ADAPTIVE\n");
- }
- else if(!_stricmp(argv[2], "getssid")) {
- char currentSSID[32];
- char *adapterNum = "1";
- ULONG ssidLength;
-
- ssidLength = ioctlGetSSIDFromAdapter(IOCTL_GET_CURRENT_SSID, adapterNum,
- (DWORD)strlen(adapterNum), ¤tSSID, sizeof(currentSSID));
-
- currentSSID[ssidLength] = '\0';
-
- printf("The current ssid is %s, length %d\n", currentSSID, ssidLength);
- }
- else if(!_stricmp(argv[2], "getmode")) {
- ULONG currentMode;
- ULONG uCurrentMode;
- char *adapterNum = "1";
-
- uCurrentMode = ioctlGetFromAdapter(IOCTL_GET_CURRENT_MODE, adapterNum,
- (DWORD)strlen(adapterNum), ¤tMode, sizeof(ULONG));
-
- if (uCurrentMode == MODE_IS)
- printf("The current mode is Infrastructure\n");
- else if (uCurrentMode == MODE_AH)
- printf("The current mode is Ad hoc\n");
- else
- printf("The current mode is Unknown\n");
- }
- else if(!_stricmp(argv[2], "getcardswitchtime")) {
- char cardSwitchTime[64];
- char *adapterNum = "1";
- ULONG timeLength;
- double switchTime;
-
- timeLength = ioctlGetULongLongFromAdapter(IOCTL_GET_CARD_SWITCH_TIME, adapterNum,
- (DWORD)strlen(adapterNum), &cardSwitchTime, sizeof(cardSwitchTime));
-
- cardSwitchTime[timeLength] = '\0';
- /*
- switchTime = (double)cardSwitchTime/1000;
-
- printf("The card switch time is %f length %d\n", switchTime, timeLength);
- */
- switchTime = (double)atol(cardSwitchTime)/10000;
- printf("The card switch Time is %f ms\n", switchTime);
- }
- else if(!_stricmp(argv[2], "setssid")) {
- NetworkModeAndSSID networkModeAndSSID;
- strcpy(networkModeAndSSID.networkSSID, "rover\0");
- networkModeAndSSID.adapterNum = "1";
- networkModeAndSSID.ssidLength = (ULONG)strlen(networkModeAndSSID.networkSSID);
- networkModeAndSSID.networkMode = MODE_IS;
-
- ioctlSet(IOCTL_SET_CURRENT_SSID, &networkModeAndSSID, (DWORD) sizeof(networkModeAndSSID));
-
- }
- else if(!_stricmp(argv[2], "setparam")) {
- SetVirtualWiFiTimers setVirtualWiFiTimers;
- setVirtualWiFiTimers.adapterNum = "1";
-
- if(argc < 5) Usage();
-
- setVirtualWiFiTimers.switchTime = atol(argv[3]);
- setVirtualWiFiTimers.waitTime = atol(argv[4]);
-
- ioctlSet(IOCTL_SET_SWITCHING_PARAMETERS, &setVirtualWiFiTimers,
- (DWORD)sizeof(SetVirtualWiFiTimers));
- }
- else if(!_stricmp(argv[2], "switchssid")) {
- char *adapterNum = "1";
-
- ioctlSet(IOCTL_SWITCH_SSID, adapterNum, sizeof(adapterNum));
- Sleep(500);
- ioctlSet(IOCTL_SEND_BUFFERED_PACKETS, adapterNum, sizeof(adapterNum));
- }
- else if(!_stricmp(argv[2], "getNumNetworks")) {
- ULONG numNetworks = 0;
- ULONG uNumNetworks = 0;
- char *adapterNum = "1";
-
- uNumNetworks = ioctlGetFromAdapter(IOCTL_GET_NUM_NETWORKS, adapterNum,
- (DWORD)strlen(adapterNum), &numNetworks, sizeof(ULONG));
-
- printf("Number of Networks %d\n", uNumNetworks);
- } else {
- Usage();
- }
- }
-