⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 change_limit.c

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 C
字号:
/*
 * This file is part of KCeasy (http://www.kceasy.com)
 * Copyright (C) 2002-2005 Markus Kern <mkern@kceasy.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
/*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
#include "tcpip_patcher.h"

/*****************************************************************************/

BOOL LoadDriver(const char *DriverName, const char *DriverImagePath);
BOOL UnloadDriver(const char *DriverName);

/*****************************************************************************/

#define DRIVER_IMAGE_NAME "tcpip_patcher.sys"
#define DRIVER_NAME "tcpip_patcher"
#define DEVICE_PATH "\\\\.\\" PATCHER_DEVICE_NAME_A
#define LOG_BUFFER_SIZE (16*1024)

int main (int argc, char *argv[])
{
	HANDLE hDevice;
	PATCHER_LIMIT_DATA *LimitData;
	DWORD BytesReturned;

	if (argc > 2)
	{
		printf ("Usage: %s [new limit]\n", argv[0]);
		return 1;
	}

	printf ("Opening device '%s'\n", DEVICE_PATH);

	hDevice = CreateFile (DEVICE_PATH,
	                      GENERIC_READ | GENERIC_WRITE,
	                      0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
	                      NULL);

	if (hDevice == INVALID_HANDLE_VALUE)
	{
		char ImagePath[MAX_PATH];

		printf ("Couldn't open device. Loading driver.\n");

		if (SearchPath (NULL, DRIVER_IMAGE_NAME, NULL, MAX_PATH, ImagePath, NULL) == 0)
		{
			printf ("Could find image %s anywhere\n", DRIVER_IMAGE_NAME);
			return 1;
		}

		printf ("Loading driver from image '%s'\n", ImagePath);

		if (!LoadDriver (DRIVER_NAME, ImagePath))
		{
			printf ("LoadDriver() failed\n");
			return 1;
		}

		printf ("Successfully loaded driver. Now trying to open device again.\n");

		hDevice = CreateFile (DEVICE_PATH,
		                      GENERIC_READ | GENERIC_WRITE,
		                      0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
		                      NULL);

		if (hDevice == INVALID_HANDLE_VALUE)
		{
			printf ("Still cannot open device. Unloading driver again.\n");
			
			if (!UnloadDriver (DRIVER_NAME))
			{
				printf ("UnloadDriver() failed\n");
				return 1;
			}

			printf ("Driver unloaded\n");
			return 1;
		}
	}

	printf ("Device '%s' opened successfully\n", DEVICE_PATH);

	LimitData = (PATCHER_LIMIT_DATA*) malloc (sizeof (PATCHER_LIMIT_DATA) + LOG_BUFFER_SIZE);

	if (argc > 1)
	{	
		LimitData->HalfOpenLimit = atoi (argv[1]);
		printf ("Setting half open limit to %d\n", LimitData->HalfOpenLimit);

		if (!DeviceIoControl (hDevice, IOCTL_PATCHER_SET_LIMIT,
		                      (LPVOID)LimitData, sizeof (PATCHER_LIMIT_DATA),
		                      (LPVOID)LimitData, sizeof (PATCHER_LIMIT_DATA) + LOG_BUFFER_SIZE,
		                      &BytesReturned, NULL))
		{
			printf ("DeviceIoControl(IOCTL_PATCHER_SET_LIMIT) failed\n");
			free (LimitData);
			CloseHandle (hDevice);
			if (!UnloadDriver (DRIVER_NAME))
				printf ("UnloadDriver() failed\n");
			return 1;
		}

		if (LimitData->ErrorCode != PATCHER_ERROR_SUCCESS)
			printf ("Driver failed to set new limit\n");
	}
	else
	{
		printf ("Retrieving data from device\n");

		if (!DeviceIoControl (hDevice, IOCTL_PATCHER_GET_LIMIT,
		                      NULL, 0,
		                      (LPVOID)LimitData, sizeof (PATCHER_LIMIT_DATA) + LOG_BUFFER_SIZE,
		                      &BytesReturned, NULL))
		{
			printf ("DeviceIoControl(IOCTL_PATCHER_GET_LIMIT) failed\n");
			free (LimitData);
			CloseHandle (hDevice);
			if (!UnloadDriver (DRIVER_NAME))
				printf ("UnloadDriver() failed\n");
			return 1;
		}

		if (LimitData->ErrorCode != PATCHER_ERROR_SUCCESS)
			printf ("Driver failed to return limit data\n");
	}

	printf ("Driver returned the following data:\n");
	printf ("ErrorCode: %d\n",       LimitData->ErrorCode);
	printf ("CurrentHalfOpen: %d\n", LimitData->CurrentHalfOpen);
	printf ("HalfOpenLimit: %d\n",   LimitData->HalfOpenLimit);
	printf ("Driver Log:\n");
	printf ("==========================\n");
	printf ("%s", LimitData->LogString);
	printf ("==========================\n");

	free (LimitData);
	CloseHandle (hDevice);

	printf ("Unloading driver\n");
	if (!UnloadDriver (DRIVER_NAME))
	{
		printf ("UnloadDriver() failed\n");
		return 1;
	}

	printf ("Driver unloaded successfully\n");
	return 0;
}

/*****************************************************************************/

BOOL LoadDriver(const char *DriverName, const char *DriverImagePath)
{
	SC_HANDLE hSCManager, hService;

    if (!DriverName || !DriverImagePath)
		return FALSE;

	if (!(hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)))
	{
		printf ("OpenSCManager() failed\n");
		return FALSE;
	}

	hService = CreateService (hSCManager,
	                          DriverName,
	                          DriverName,
	                          SERVICE_ALL_ACCESS,
	                          SERVICE_KERNEL_DRIVER,
	                          SERVICE_DEMAND_START,
	                          SERVICE_ERROR_NORMAL,
	                          DriverImagePath,
	                          NULL,
	                          NULL,
	                          NULL,
	                          NULL,
	                          NULL);

	if (!hService)
	{
		if(GetLastError() != ERROR_SERVICE_EXISTS)
		{
			printf ("CreateService() failed\n");
			CloseServiceHandle (hSCManager);
			return FALSE;
		}

		printf ("Driver already loaded, opening it for starting\n");

		/* try to open the service */
		if(!(hService = OpenService (hSCManager, DriverName, SERVICE_ALL_ACCESS)))
		{
			printf ("OpenService() failed\n");
			CloseServiceHandle (hSCManager);
			return FALSE;
		}
	}

	if (!StartService (hService, 0, NULL))
	{
		if (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING)
		{
			printf ("StartService() failed\n");
			CloseServiceHandle (hService);
			CloseServiceHandle (hSCManager);
			return FALSE;
		}

		printf ("Driver already running\n");
	}
	
	CloseServiceHandle (hService);
	CloseServiceHandle (hSCManager);

	return TRUE;
}

BOOL UnloadDriver(const char *DriverName)
{
	SC_HANDLE hSCManager, hService;
	SERVICE_STATUS ServiceStatus;

    if (!DriverName)
		return FALSE;

	if (!(hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)))
	{
		printf ("OpenSCManager() failed\n");
		return FALSE;
	}

	if(!(hService = OpenService (hSCManager, DriverName, SERVICE_ALL_ACCESS)))
	{
		printf ("OpenService() failed\n");
		CloseServiceHandle(hSCManager);
		return FALSE;
	}

	if (!ControlService (hService, SERVICE_CONTROL_STOP, &ServiceStatus))
	{
		if (GetLastError() != ERROR_SERVICE_NOT_ACTIVE)
		{
			printf ("ControlService(SERVICE_CONTROL_STOP) failed\n");
			CloseServiceHandle(hService);
			CloseServiceHandle(hSCManager);
			return FALSE;
		}

		printf ("Driver not running\n");
	}

	if (!DeleteService (hService))
	{
		if (GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE)
		{
			printf ("DeleteService() failed\n");
			CloseServiceHandle(hService);
			CloseServiceHandle(hSCManager);
			return FALSE;
		}

		printf ("Driver already marked for delete\n");
	}

	CloseServiceHandle(hService);
	CloseServiceHandle(hSCManager);
	
	return TRUE;
}

/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -