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

📄 ipping.cpp

📁 本代码是自己封装的采用WINPCAP技术对在线设备分配IP地址。
💻 CPP
字号:
// ipping.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "ipping.h"
#include "iphlpapi.h"
#include "Hex.h"
#include "iptypes.h"
#include "YGPcap.h"
#include "pcap.h"
#include "ip6_misc.h"
#include "initsock.h"
#include "IPExport.h"
#include "ipping.h"

#pragma comment(lib,"iphlpapi.lib")
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"Packet.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CIppingApp

BEGIN_MESSAGE_MAP(CIppingApp, CWinApp)
	//{{AFX_MSG_MAP(CIppingApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIppingApp construction

CIppingApp::CIppingApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CIppingApp object

CIppingApp theApp;
bool _stdcall Allodeviecip(CString devicemac)
{
	PIP_ADAPTER_INFO pAdapterInfo = NULL;
	ULONG ulLen = 0;
	CHex bMac(12);
	CYGPcap m_cap;
	char dstip[16];
	CString m_macstr;
	DWORD strip;
	CString ipsend;
	CString sendip;
	unsigned   char   *cip; 
	pcap_if_t  *ppDev;
	pcap_t*	adhandle;	
	// 为适配器结构申请内存::
	GetAdaptersInfo(pAdapterInfo,&ulLen);
	pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);
	
	// 取得本地适配器结构信息::::
	if(GetAdaptersInfo(pAdapterInfo,&ulLen) ==  ERROR_SUCCESS)
	{
		if(pAdapterInfo != NULL)
		{
			bMac.Add((void *)pAdapterInfo->Address,6);
			
			m_macstr=(CString)bMac;
			strip=ntohl(inet_addr(pAdapterInfo->IpAddressList.IpAddress.String));
			pcap_if_t*		alldevs;	
			char			errbuf[PCAP_ERRBUF_SIZE];
			pcap_findalldevs( &alldevs, errbuf);
			ppDev=alldevs->next;
			
			
		}
	}
	int ipc=2;
	if((adhandle=m_cap.OpenLive(ppDev))== NULL)
	{
		AfxMessageBox("无法打开网卡!");
		
		//return ;
	}
	cip   =   (unsigned   char*)&strip;   
	u_char TPA[4];
	TPA[0]=*(cip+3);
	TPA[1]=*(cip+2);
	TPA[2]=*(cip+1);
	while(ipc<255)
	{
		
		TPA[3]=ipc;
		ipsend.Format("%u.%u.%u.%u",TPA[3],TPA[2],TPA[1],TPA[0]);
		sendip.Format("%u.%u.%u.%u",TPA[0],TPA[1],TPA[2],TPA[3]);
		
		// 发送ARP请求
		u_char arDestMac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
		ULONG ulLen = 6;
		
		if (NO_ERROR!=SendARP(::inet_addr(sendip), 0, (ULONG*)arDestMac, &ulLen))
		{
			break;
		}
		else
		{
			ipc=ipc+1;
			continue;
		}
		
		
	}
	ipsend.Format("%u.%u.%u.%u",TPA[3],TPA[2],TPA[1],TPA[0]);
	static int nPacketNum=0;
	CHex bPacket(64);
	DWORD dwAddress;
	//数据链路层
	bPacket<<devicemac;				//Destination MAC
	bPacket<<m_macstr;					//Source MAC
	bPacket.AddTwoByte(0x8035);
	//arp层
	bPacket.AddTwoByte(0x0001);			//Harware type
	bPacket.AddTwoByte(0x0800);			//protocol type
	bPacket.AddOneByte(0x6);			//hardware size
	bPacket.AddOneByte(0x4);			//protocol size
	;
	bPacket.AddTwoByte(4);//code type
	bPacket<<m_macstr;					//sender mac address
	
	dwAddress=inet_addr(ipsend); 
	//AfxMessageBox(strip);
//	AfxMessageBox(devicemac);
///	AfxMessageBox(ipsend);
	
	bPacket.AddUINT(strip);					//sender ip address
	bPacket.Add((void *)"\x0\x0\x0\x0\x0\x0",6);//target mac address
	bPacket.AddUINT(dwAddress);					//target ip address
	
	m_cap.SendPacket(bPacket);
	return true;
}

⌨️ 快捷键说明

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