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

📄 inetthread.cpp

📁 一些网络工具的源程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// InetThread.cpp : implementation file
//

#include "stdafx.h"
#include "nettools.h"
#include <snmp.h>
#include <winsock.h>
#include "InetThreadParms.h"
#include "InetThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

struct TCPStates
{
	CHAR state[12];
};

struct TCPStates StateTable[12] =
{
	{"Closed     "},
	{"Listen     "},
	{"SynSent    "},
	{"SynReceived"},
	{"Established"},
	{"FinWait1   "},
	{"FinWait2   "},
	{"CloseWait  "},
	{"LastAck    "},
	{"Closing    "},
	{"TimeWait   "},
	{"DeleteTCB  "}
};

//typedef BOOL (CALLBACK* SEQUERY)(BYTE,RFC1157VarBindList*,AsnInteger*,AsnInteger*);
//typedef BOOL (CALLBACK* SEINIT)(DWORD,HANDLE*,AsnObjectIdentifier*);


//WSADATA Inetwsad;
//HANDLE snmpevent = NULL;
//SEINIT SnmpExtensionInit;
//SEQUERY SnmpExtensionQuery;
//HINSTANCE inetdll;

UINT CInetThreadProc(LPVOID pParam)
{
	CInetThreadParms *lpInetParms;
	CInetThread *mythread;

	lpInetParms = (CInetThreadParms*)pParam;
	mythread = (CInetThread*)lpInetParms->m_mythread;

	while (TRUE)
	{

	
	

		// Wait until the main application thread asks this thread to do
		//      another calculation.
		if (WaitForSingleObject(lpInetParms->m_hEventStartInet,
								INFINITE) != WAIT_OBJECT_0)
			break;

		// Exit the thread if the main application sets the "kill recalc"
		// event. The main application will set the "start recalc" event
		// before setting the "kill recalc" event.

		if (WaitForSingleObject(lpInetParms->m_hEventKillInet, 0)
			== WAIT_OBJECT_0)
			break; // Terminate this thread by existing the proc.

		// Reset event to indicate "not done", that is, recalculation is in progress.
		//ResetEvent(lpInetParms->m_hEventInetDone);

	
		mythread->RunInet(lpInetParms);
			

		// Set event to indicate that recalculation is done (i.e., no longer in progres),
		// even if perhaps interrupted by "kill recalc" event detected in the SlowAdd function.
		SetEvent(lpInetParms->m_hEventInetDone);

		//if (!bPingThreadComplete)  // If interrupted by kill then...
		//	break; // terminate this thread by exiting the proc.

		::PostMessage(lpInetParms->m_hwndNotifyInetDone,
			WM_USER_INET_DONE, 0, (LONG)lpInetParms);
	}

	//if (!bPingThreadComplete)
	
	SetEvent(lpInetParms->m_hEventInetDead);

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CInetThread

IMPLEMENT_DYNCREATE(CInetThread, CWinThread)

CInetThread::CInetThread()
{
}

CInetThread::CInetThread(CInetThreadParms *inetparms)
{
	
	inetparms->m_mythread = this;
	m_bAutoDelete = FALSE;
	m_pfnThreadProc = CInetThreadProc;
	m_pThreadParams = inetparms;

}

CInetThread::~CInetThread()
{
}

BOOL CInetThread::InitInstance()
{
	WSADATA Inetwsad;
	// TODO:  perform and per-thread initialization here
	inetdll = LoadLibrary("INETMIB1.DLL");
    if(!inetdll)
    {
        
		return FALSE;
	}
	
    
	SnmpExtensionQuery = (SEQUERY)GetProcAddress(inetdll,"SnmpExtensionQuery");
	SnmpExtensionInit = (SEINIT)GetProcAddress(inetdll,"SnmpExtensionInit");

  	if (SnmpExtensionQuery == NULL || SnmpExtensionInit == NULL)
		return FALSE;


	
	AsnObjectIdentifier mview;
	DWORD mtime;
	mtime = GetCurrentTime();
	snmpevent = NULL;
	BOOL rc;
    rc = (*SnmpExtensionInit)(mtime,&snmpevent,&mview);
	if (!rc)
	{
		
		return FALSE;
	}

	WSAStartup(0x0101,&Inetwsad);
	return TRUE;
}

int CInetThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CInetThread, CWinThread)
	//{{AFX_MSG_MAP(CInetThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInetThread message handlers

BOOL CInetThread::Start()
{
	BOOL rc = TRUE;
	bKill = FALSE;
	rc = CreateThread();
	if (!rc)
		return rc;

	rc = InitInstance();
	if (!rc)
		return rc;

	return rc;
	

}

BOOL CInetThread::RunInet(CInetThreadParms *inetparms)
{
	//
	// setup default tabs. some routines will change as needed
	//

	
	
	bKill = FALSE;
	
	if (inetparms->allopts)
	{
		inetparms->connections = TRUE;
		inetparms->icmpstats = TRUE;
		inetparms->ifstats = TRUE;
		inetparms->ipstats = TRUE;
		inetparms->routtable = TRUE;
		inetparms->tcpstats = TRUE;
		inetparms->udpstats = TRUE;
	}

	INT Tabs1[7]  = {32,32,32,32,32,32,32};
	

	::SendMessage(inetparms->m_hwndNotifyInetDone,
				WM_USER_INET_TABS, 0, (LONG)Tabs1);


	if (inetparms->connections)
		DoConnections(inetparms);
	
	if (bKill)
		return TRUE;

	if(inetparms->routtable)
		DoRouteTable(inetparms);

	if (bKill)
		return TRUE;

	INT Tabs[7]  = {100,100,100,100,100,100,100};
	::SendMessage(inetparms->m_hwndNotifyInetDone,
				WM_USER_INET_TABS, 0, (LONG)Tabs);

	if(inetparms->icmpstats)
		DoIcmpStats(inetparms);

	if (bKill)
		return TRUE;

	if(inetparms->ifstats)
		DoIfStats(inetparms);

	if (bKill)
		return TRUE;

	if(inetparms->ipstats)
		DoIpStats(inetparms);

	if (bKill)
		return TRUE;


	if(inetparms->tcpstats)
		DoTcpStats(inetparms);

	if (bKill)
		return TRUE;

	if(inetparms->udpstats)
		DoUdpStats(inetparms);

	

	return TRUE;




}

void CInetThread::Free_Var(RFC1157VarBind *var)
{
		// For NT we must SnmpMemFree the name and value.
  // for 95 the name we allocated ourselves, and the value
  // is static
  SnmpUtilMemFree(var->name.ids);
    // Free the mem block in the value bit depending on what it is...
  switch(var->value.asnType)
  {
    case ASN_SEQUENCE:
    case ASN_OCTETSTRING:
    case ASN_RFC1155_IPADDRESS:
    case ASN_RFC1155_OPAQUE:
      // AsnOctetString is the base type for all the complex types.
    if(var->value.asnValue.string.dynamic)
        SnmpUtilMemFree(var->value.asnValue.string.stream);
  }
  

}

void CInetThread::Make_Oid(AsnObjectIdentifier* dst,UINT n, UINT* src)
{
			// Fill in the length
	dst->idLength = n;
	dst->ids = NULL;
	dst->ids = (UINT*)SnmpUtilMemAlloc(sizeof(UINT)*n);
	CopyMemory(dst->ids,src,sizeof(UINT)*n);

}

BOOL CInetThread::DoConnections(CInetThreadParms *inetparms)
{
	int requestType;
	AsnInteger errorStatus;
	AsnInteger errorIndex;
	UINT ids_tcpconn[8] = {1,3,6,1,2,1,6,13};
	UINT ids_udpconn[8] = {1,3,6,1,2,1,7,5};
	AsnObjectIdentifier saveobj;
	RFC1157VarBind Getnextlist[1];
	RFC1157VarBindList Getnextbindlist = {Getnextlist,1};
	
	

	BOOL rc;
	CHAR textbuf[255];

	
	// pick up all the tcp connections
	Make_Oid(&Getnextlist[0].name,8,ids_tcpconn);

    Getnextlist[0].value.asnType = ASN_NULL;
   
      
    requestType = ASN_RFC1157_GETNEXTREQUEST;

	strcpy(textbuf,"Connection Information");
	PrintLine(inetparms,textbuf);
	PrintLine(inetparms,":");

	strcpy(textbuf,
	"Proto     Local Address\tForeign Address\tState");
	PrintLine(inetparms,textbuf);

   
    while(1)
	{

        
        rc = (*SnmpExtensionQuery)(requestType, &Getnextbindlist,
                           &errorStatus, &errorIndex);

		if (!rc)

            {
            // The API is indicating an error.
			wsprintf(textbuf,"SNMP Error Status = %d Index = %d",
						errorStatus,errorIndex);

            PrintLine(inetparms,textbuf);

            break;
            }
        else
            {
            // The API succeeded, errors may be indicated from the remote
            // agent.


            // Test for end of subtree or end of MIB.

            if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME)
			{
			
				break;
			}
			
			if (Getnextlist[0].value.asnType == 
				ASN_RFC1155_IPADDRESS)
			{
				
				break;

			}



            // Test for general error conditions or sucesss.

            if (errorStatus > 0)
			{
				wsprintf(textbuf,"SNMP Error Status = %d Index = %d",
						errorStatus,errorIndex);

				PrintLine(inetparms,textbuf);
			
				break;
			}
            else
			{
				PrintConnInfo(inetparms,
							  &Getnextlist[0].name,
							  "TCP");
				
			}

		} // end if


		if (bKill)
		{
			Free_Var(&Getnextlist[0]);
			PrintLine(inetparms,"Netstat Interupt!");
			return TRUE;
			break;
		}
		SnmpUtilOidCpy(&saveobj,&Getnextlist[0].name);
			         
		Free_Var(&Getnextlist[0]);
	
		Make_Oid(&Getnextlist[0].name,saveobj.idLength,saveobj.ids);	
		

		Getnextlist[0].value.asnType = ASN_NULL;
		SnmpUtilOidFree(&saveobj);
	
        
	} // end while()

	// get all UDP Connections

	Make_Oid(&Getnextlist[0].name,8,ids_udpconn);

    Getnextlist[0].value.asnType = ASN_NULL;
   
      
    requestType = ASN_RFC1157_GETNEXTREQUEST;

	
    while(1)
	{

        
        rc = (*SnmpExtensionQuery)(requestType, &Getnextbindlist,
                           &errorStatus, &errorIndex);

		if (!rc)

            {
            // The API is indicating an error.

            wsprintf(textbuf,"SNMP Error Status = %d Index = %d",
						errorStatus,errorIndex);

            PrintLine(inetparms,textbuf);

            break;
            }
        else
            {
            // The API succeeded, errors may be indicated from the remote
            // agent.


            // Test for end of subtree or end of MIB.

            if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME)
			{
			
				break;
			}
			
			if (Getnextlist[0].value.asnType == ASN_INTEGER)
			{
				
				break;

			}



            // Test for general error conditions or sucesss.

            if (errorStatus > 0)
			{
				wsprintf(textbuf,"SNMP Error Status = %d Index = %d",
						errorStatus,errorIndex);

				PrintLine(inetparms,textbuf);
			
				break;
			}
            else
			{
				PrintConnInfo(inetparms,
					&Getnextlist[0].name,"UDP");
				
			}

		} // end if

		if (bKill)
		{
			Free_Var(&Getnextlist[0]);
			PrintLine(inetparms,"Netstat Interupt!");
			return TRUE;
			break;
		}
		SnmpUtilOidCpy(&saveobj,&Getnextlist[0].name);
			         
		Free_Var(&Getnextlist[0]);
	
		Make_Oid(&Getnextlist[0].name,saveobj.idLength,saveobj.ids);	
		

		Getnextlist[0].value.asnType = ASN_NULL;
		SnmpUtilOidFree(&saveobj);
	
        
	} // end while()

	PrintLine(inetparms,":");
	return TRUE;

}

BOOL CInetThread::PrintConnInfo(CInetThreadParms *inetparms,
								AsnObjectIdentifier * obj,
								CHAR * conntype)
{

	INT tcpvarbinds = 5;
	INT udpvarbinds = 2;
	INT q;
	AsnInteger dispstate;
	AsnInteger localport;
	AsnInteger remoteport;

	AsnInteger errorStatus;
	AsnInteger errorIndex;
	
	RFC1157VarBind tcplist[5];
	RFC1157VarBindList tcpbindlist = {tcplist,tcpvarbinds};
	RFC1157VarBind udplist[2];
	RFC1157VarBindList udpbindlist = {udplist,udpvarbinds};
	CHAR textbuf[255];
	BYTE *routes;
	u_long localaddr;
	u_long remoteaddr;
	CHAR szlocaladdr[128];
	CHAR szremoteaddr[128];
	CHAR szlocalhost[128];
	CHAR *szTempName;
	struct in_addr convaddr;
	struct hostent *resolve_addr;
	int rc;

	rc = strcmp(conntype,"TCP");
	if (rc == 0)
	{
		for(q = 0; q<tcpvarbinds; q++)   // for for #of var binds
		{
			
			Make_Oid(&tcplist[q].name,obj->idLength,obj->ids);
			tcplist[q].name.ids[9] = q+1;
			tcplist[q].value.asnType = ASN_NULL;
		}
	
		

		rc = (*SnmpExtensionQuery)(ASN_RFC1157_GETREQUEST,
						&tcpbindlist,
						&errorStatus,
						&errorIndex);


		if (!rc)

            {
            // The API is indicating an error.

            wsprintf(textbuf,"SNMP Error Status = %d Index = %d",
						errorStatus,errorIndex);

            PrintLine(inetparms,textbuf);

            return FALSE;
            }

		//
		// build connstate table to look up the name. 
		// convert the ip addr from BYTE* to u_long then inet_ntoa

⌨️ 快捷键说明

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