www.pudn.com > checkUnusedIP.rar > CheckIPFreeDlg.cpp


// CheckIPFreeDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CheckIPFree.h" 
#include "CheckIPFreeDlg.h" 
 
 
//#include  
 
#include "pcap.h" 
#pragma comment (lib,"wpcap") 
 
 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckIPFreeDlg dialog 
 
CCheckIPFreeDlg::CCheckIPFreeDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCheckIPFreeDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCheckIPFreeDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CCheckIPFreeDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCheckIPFreeDlg) 
	DDX_Control(pDX, IDC_IPADDRESS1, m_sss); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCheckIPFreeDlg, CDialog) 
	//{{AFX_MSG_MAP(CCheckIPFreeDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckIPFreeDlg message handlers 
 
BOOL CCheckIPFreeDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	/* 
	// TODO: Add extra initialization here 
	CString  strIP="192.168.3.244";   
	DWORD  dwIP;   
	dwIP  =  inet_addr(strIP);   
	unsigned  char  *pIP  =  (unsigned  char*)&dwIP;   
	m_IPAddr1.SetAddress(*pIP,  *(pIP+1),  *(pIP+2),  *(pIP+3)); 
 
	strIP="192.168.3.244";   
	dwIP  =  inet_addr(strIP);   
	unsigned  char  *pIP1  =  (unsigned  char*)&dwIP;   
	m_IPAddr2.SetAddress(*pIP1,  *(pIP1+1),  *(pIP1+2),  *(pIP1+3)); 
*/ 
 
 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CCheckIPFreeDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CCheckIPFreeDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CCheckIPFreeDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CCheckIPFreeDlg::OnCheck()  
{ 
	pcap_t * adhandle = NULL; 
	char errbuf[PCAP_ERRBUF_SIZE]; 
 
	pcap_if_t * alldevs; 
	pcap_if_t * d; 
	int i=0; 
	int inum; 
	//char errbuf[PCAP_ERRBUF_SIZE]; 
 
	if (pcap_findalldevs(&alldevs,errbuf) == -1) 
	{ 
		//fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); 
		AfxMessageBox("Error in pcap_findalldevs"); 
		//exit(1); 
		return ; 
	} 
	for (d=alldevs;d;d=d->next) 
	{ 
		i++; 
	} 
 
	if (i == 0) 
	{ 
		AfxMessageBox("没有找到网络设备"); 
		return ; 
	} 
 
	//printf("Enter the interface number (1-%d):",i); 
	//scanf("%d", &inum); 
	inum=2; 
 
	for (d=alldevs,i=0;inext,i++); 
 
		//printf("\n%s\n",d->name); 
	if ((adhandle = pcap_open_live(d->name, 
							65535, 
							1, 
							1000, 
							errbuf 
							)) == NULL) 
 
	{	 
		pcap_freealldevs(alldevs); 
		return ; 
	} 
 
 
 
	/* 
	ffffffffffff 
	00-0B-74-17-5C-D7  //我得mac地址 
	0806 
	0001 
	0800 
	06 
	04 
	0001 
	ffffffffffff		//源mac 
	c0a80001             //源IP地址 
	000000000000		//目的mac 
	c0a8039b			//目的IP地址 
	c027dae85010ff9b57710000202020202020  //随机 
	*/ 
	u_char packet[]= 
 
	"\xff\xff\xff\xff\xff\xff" 
	"\x00\x0B\x74\x17\x5C\xD7"  //我得mac地址 
	"\x08\x06" 
	"\x00\x01" 
	"\x08\x00" 
	"\x06" 
	"\x04" 
	"\x00\x01" 
	"\xff\xff\xff\xff\xff\xff"		//源mac 
	"\xc0\xa8\x00\x02"             //源IP地址 
	"\x00\x00\x00\x00\x00\x00"		//目的mac 
	"\xc0\xa8\x03\x0a"			//目的IP地址 
	"\xc0\x27\xda\xe8\x50\x10\xff\x9b\x57\x71\x00\x00\x20\x20\x20\x20\x20\x20"  //随机 
	; 
  
  
 
		/* Send down the packet */ 
		if (pcap_sendpacket(adhandle, packet, 60 /* size */) != 0) 
		{ 
			//fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(adhandle)); 
			AfxMessageBox("Error sending the packet"); 
			return ; 
		} 
 
 
		pcap_freealldevs(alldevs); 
 
 
		return ; 
	 
 
	 
	//CDialog::OnOK(); 
}