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

📄 ygpcap.cpp

📁 本代码是自己封装的采用WINPCAP技术对在线设备分配IP地址。
💻 CPP
字号:
// YGPcap.cpp: implementation of the CYGPcap class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "YGPcap.h"
#pragma comment(lib,"Wpcap.lib")


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

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

CYGPcap::CYGPcap():m_nAdapterCount(0),m_hPcap(NULL),m_pAlldevs(NULL)
{
}

CYGPcap::~CYGPcap()
{
	if (m_pAlldevs) delete  m_pAlldevs;
}

int CYGPcap::FindAllAdapter()
{
	pcap_if_t *pTAlldevs;
	pcap_if_t  *pDev;
	int i=0;
	m_nAdapterCount=0;
	if (m_pAlldevs)
	{
		delete  m_pAlldevs;
		m_pAlldevs=NULL;
	}
	m_nReturn=pcap_findalldevs(&pTAlldevs,m_szErr);
	if (!pTAlldevs) return -2;			//居然一个网卡都冇	
	
	pDev=pTAlldevs;
	while(pDev) 
	{
		m_nAdapterCount++;
		pDev=pDev->next;
	}

	m_pAlldevs=new pcap_if_t *[m_nAdapterCount];//指针数组
	pDev=pTAlldevs;
	while(pDev) 
	{
		m_pAlldevs[i]=pDev;
		pDev=pDev->next;
		i++;
	}
	return m_nReturn;
}

pcap_t * CYGPcap::OpenLive(const pcap_if_t *pDev, int nPort, int nMode, int nReadTime)
{

	m_hPcap= pcap_open_live(pDev->name,nPort,nMode,nReadTime,m_szErr);
	return m_hPcap;
}

int CYGPcap::SendPacket(BYTE *bData, int nLen)
{
	ASSERT(m_hPcap);
	if (!m_hPcap) return 0;
	m_nReturn=pcap_sendpacket(m_hPcap,bData,nLen);
	return m_nReturn;
}

int CYGPcap::SendPacket(const CHex &bData)
{
	ASSERT(m_hPcap);
	if (!m_hPcap) return 0;
	m_nReturn=pcap_sendpacket(m_hPcap,bData.m_bHex,bData.GetCurPoint());
	return m_nReturn;	
}

int CYGPcap::GetAdapterNum() const
{
	return m_nAdapterCount;
}

void CYGPcap::ClosePcap()
{
	if (!m_hPcap) return ;
	pcap_close(m_hPcap);
	m_hPcap=NULL;
}

void CYGPcap::CloseAdapter()
{
	pcap_freealldevs(*m_pAlldevs);
}

const char * CYGPcap::GetErr()
{
	return m_szErr;
}

⌨️ 快捷键说明

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