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

📄 spoofbase.cpp

📁 一个可订制ip packet的程序
💻 CPP
字号:
// SpoofBase.cpp: implementation of the CSpoofBase class.
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/*
 *
 *
 *  Copyright (c) 2000 Barak Weichselbaum <barak@komodia.com>
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 * Contact info:
 * Site: http://www.komodia.com
 * Email: barak@komodia.com
 */

#include "stdafx.h"
#include "SpoofBase.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//----------------------- CSpoofLog start -----------------------
//##ModelId=3B43E6F30384
CSpoofBase::CSpoofLog* CSpoofBase::m_Log=NULL;

//##ModelId=3B43E6F4005A
CSpoofBase::CSpoofLog::CSpoofLog()
{
}

//##ModelId=3B43E6F40064
CSpoofBase::CSpoofLog::~CSpoofLog()
{
}
//----------------------- CSpoofLog end -----------------------

//##ModelId=3B43E6F40000
BOOL CSpoofBase::m_bMultiThreaded=FALSE;
//##ModelId=3B43E6F303AB
BOOL CSpoofBase::m_Initialized=FALSE;
//##ModelId=3B43E6F4001E
int CSpoofBase::m_NumberOfThreads=0;

//##ModelId=3B43E6F302CF
CSpoofBase::CSpoofBase()
{
	try
	{
		//No name
		m_lpClassName=NULL;

		//Set it
		SetName("CSpoofBase");

		//No local log
		m_LocalLog=NULL;
	}
	ERROR_HANDLER("CSpoofBase")
}

//##ModelId=3B43E6F302D0
CSpoofBase::~CSpoofBase()
{
	try
	{
		//Dispose of the name
		free(m_lpClassName);
	}
	ERROR_HANDLER("~CSpoofBase")
}

//##ModelId=3B43E6F30351
void CSpoofBase::SetLastError(LPCSTR lpMethod)
{
	try
	{
		//First set the error
		m_LastError=WSAGetLastError();

		//Check if there is an error
		if (m_LastError)
			ReportError(m_lpClassName,m_LastError);
	}
	ERROR_HANDLER("SetLastError")
}

//##ModelId=3B43E6F3035C
void CSpoofBase::SetLastError(LPCSTR lpMethod,int iErrorCode)
{
	try
	{
		//First set the error
		m_LastError=iErrorCode;

		//Check if there is an error
		if (m_LastError)
			ReportError(m_lpClassName,m_LastError);
	}
	ERROR_HANDLER("SetLastError")
}

//##ModelId=3B43E6F30347
void CSpoofBase::SetName(LPCSTR lpName)
{
	try
	{
		//if exists dispose of it
		if (m_lpClassName)
			free(m_lpClassName);

		m_lpClassName=strdup(lpName);
	}
	ERROR_HANDLER("SetName")
}

//##ModelId=3B43E6F30335
void CSpoofBase::ReportError(LPCSTR lpMethod,int iErrorCode)
{
	if (!GetLog())
		return;

	try
	{
		//Get the log
		CSpoofLog* pLog;
		pLog=GetLog();

		//Report to the log
		pLog->ReportSocketError(m_lpClassName,lpMethod,iErrorCode);
	}
	catch (...)
	{
		//Can't do anything to avoid circular catch
	}
}

//##ModelId=3B43E6F3032A
void CSpoofBase::ReportError(LPCSTR lpMethod, LPCSTR lpMessage)
{
	if (!GetLog())
		return;

	try
	{
		CSpoofLog* pLog;
		pLog=GetLog();

		//Report to the log
		pLog->ReportInitiatedError(m_lpClassName,lpMethod,lpMessage);
	}
	catch (...)
	{
		//Can't do anything to avoid circular catch
	}
}

//##ModelId=3B43E6F302C6
int CSpoofBase::GetLastError()
{
	return m_LastError;
}

//##ModelId=3B43E6F302B2
BOOL CSpoofBase::InitializeSockets(BOOL bMultiThreaded,int iNumberOfThreads)
{
	//To avoid double initialize
	if (m_Initialized)
		return TRUE;

	try
	{
		//Initialize the sockets
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
 
		wVersionRequested = MAKEWORD( 2, 2 );
 
		err = WSAStartup( wVersionRequested, &wsaData );
		if (err!=0)
			/* Tell the user that we could not find a usable */
			/* WinSock DLL.                                  */
			return FALSE;
 
		/* Confirm that the WinSock DLL supports 2.2.*/
		/* Note that if the DLL supports versions greater    */
		/* than 2.2 in addition to 2.2, it will still return */
		/* 2.2 in wVersion since that is the version we      */
		/* requested.                                        */
 
		if (LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
		{
			/* Tell the user that we could not find a usable */
			/* WinSock DLL.                                  */
			WSACleanup();
			return FALSE;
		}

		//Save the threading information
		m_bMultiThreaded=bMultiThreaded;
		m_NumberOfThreads=iNumberOfThreads;

		//And we are initialized
		m_Initialized=TRUE;

		return TRUE;
	}
	catch (...)
	{
		return FALSE;
	}
}

//##ModelId=3B43E6F302BD
BOOL CSpoofBase::ShutdownSockets()
{
	//Only if initialized
	if (!m_Initialized)
		return TRUE;

	try
	{
		//Notify shutdown class
		if (m_pShutdownClass)
		{
			m_pShutdownClass->NotifyShutdown();
			delete m_pShutdownClass;
		}

		if (WSACleanup()==SOCKET_ERROR)
			return FALSE;
		
		m_Initialized=FALSE;
		return TRUE;
	}
	catch (...)
	{
		return FALSE;
	}
}

//##ModelId=3B43E6F30317
void CSpoofBase::NotifyShutdown()
{
}

//##ModelId=3B43E6F3031F
void CSpoofBase::RegisterShutdown(CSpoofBase* pBase)
{
	try
	{
		//Check if we already have a class
		if (m_pShutdownClass)
			delete m_pShutdownClass;

		m_pShutdownClass=pBase;
	}
	ERROR_HANDLER("RegisterShutdown")
}


//##ModelId=3B43E6F303DE
CSpoofBase* CSpoofBase::m_pShutdownClass=NULL;

//##ModelId=3B43E6F302A8
void CSpoofBase::SetLog(CSpoofLog *pLog)
{
	//Save the new log
	m_Log=pLog;
}

//##ModelId=3B43E6F302ED
void CSpoofBase::ReportError(LPCSTR lpMethod)
{
	if (!GetLog())
		return;

	try
	{
		//Unknown error
		LPVOID lpMsgBuf;

		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			::GetLastError(),
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL);

		//Report the error
		//Get the log
		GetLog()->ReportCatchError(m_lpClassName,lpMethod,(LPSTR)lpMsgBuf);

		//Free the resources
		LocalFree(lpMsgBuf);
	}
	catch (...)
	{
	}
}

//##ModelId=3B43E6F3029F
char FAR * CSpoofBase::LongToString(long lAddr)
{
	try
	{
		//First create the address
		in_addr addr;

		//Assign it
		addr.S_un.S_addr=lAddr;

		//Return the value
		return inet_ntoa(addr);
	}
	ERROR_HANDLER_RETURN("LongToString",NULL)
}


//##ModelId=3B43E6F3036F
CSpoofBase::CSpoofLog* CSpoofBase::GetLog()
{
	try
	{
		if (m_LocalLog)
			return m_LocalLog;
		else
			return m_Log;
	}
	catch (...)
	{
		return NULL;
	}
}

//##ModelId=3B43E6F3029D
void CSpoofBase::SetLocalLog(CSpoofLog *pLog)
{
	m_LocalLog=pLog;
}

//##ModelId=3B43E6F302E4
BOOL CSpoofBase::IsMultiThreaded()
{
	return m_bMultiThreaded;
}

//##ModelId=3B43E6F302DA
int CSpoofBase::GetNumberOfThreads()
{
	return m_NumberOfThreads;
}

//##ModelId=3B43E6F302F8
void CSpoofBase::ReportStaticError(LPCSTR lpClass,LPCSTR lpMethod)
{
	if (!m_Log)
		return;

	try
	{
		//Unknown error
		LPVOID lpMsgBuf;

		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			::GetLastError(),
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL);

		//Report the error
		m_Log->ReportCatchError(lpClass,lpMethod,(LPSTR)lpMsgBuf);

		//Free the resources
		LocalFree(lpMsgBuf);
	}
	catch (...)
	{
	}
}

//##ModelId=3B43E6F3030B
void CSpoofBase::ReportStaticError(LPCSTR lpClass,LPCSTR lpMethod,LPCSTR lpMessage)
{
	if (m_Log)
		return;

	try
	{
		//Report to the log
		m_Log->ReportInitiatedError(lpClass,lpMethod,lpMessage);
	}
	catch (...)
	{
		//Can't do anything to avoid circular catch
	}
}

⌨️ 快捷键说明

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