📄 assigndlg.cpp
字号:
// AssignDlg.cpp : implementation file
//
#include "stdafx.h"
#include "netfinder.h"
#include "AssignDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CNetfinderApp theApp;
#define ASSIGN_TIMER 2
/////////////////////////////////////////////////////////////////////////////
// CAssignDlg dialog
CAssignDlg::CAssignDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAssignDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAssignDlg)
m_progress_text = _T("");
m_password = _T("");
//}}AFX_DATA_INIT
}
void CAssignDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAssignDlg)
DDX_Control(pDX, IDOK, m_ok_button);
DDX_Control(pDX, IDCANCEL, m_cancel_button);
DDX_Control(pDX, IDC_GATEWAY, m_assign_gateway);
DDX_Control(pDX, IDC_SUBNET, m_assign_subnet);
DDX_Control(pDX, IDC_IP, m_assign_ip);
DDX_Control(pDX, IDC_PROGRESS, m_progress);
DDX_Text(pDX, IDC_EDIT1, m_progress_text);
DDX_Text(pDX, IDC_EDIT2, m_password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAssignDlg, CDialog)
//{{AFX_MSG_MAP(CAssignDlg)
ON_BN_CLICKED(IDOK, OnOK)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAssignDlg message handlers
BOOL CAssignDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//-----------------------------------------------------------------
// Add pointer to this dialog to <theApp> to provide global access
//-----------------------------------------------------------------
theApp.pAssignDlg = this;
//-----------------------------------------------------------------
// Register the Operation to allow packets to be received
//-----------------------------------------------------------------
theApp.m_operation = ASSIGN;//登记允许包接收的操作
//-----------------------------------------------------------------
// Initialize the network error flag
//-----------------------------------------------------------------
m_networkerror = 0;
//-----------------------------------------------------------------
// Initialize the Progress Bar
//-----------------------------------------------------------------
#define RANGE 10*1000 // 10 seconds
#define STEP 250 // 250 ms
m_progress.SetRange( 0, RANGE ); // Set Range
m_progress.SetStep( -STEP ); // Same as Timer Interval
m_progress.SetPos(RANGE);
m_interval = 0; // Clear timer counter
m_timeout = 0; // Clear timeout flag
m_error = 0; // Clear error flag
m_progress_text.Format("%i 秒剩余时间", RANGE/1000);
UpdateData(FALSE);
unsigned char IP[4];
theApp.pMainDlg->m_netdisplay.GetIPAddress(&IP[0]); // Get old address and store in IP
m_assign_ip.SetAddress(IP[0],IP[1],IP[2],IP[3]);
// Set Default subnet mask to 255.255.255.0
// Set Default gatway address to 255.255.255.255
m_assign_subnet.SetAddress(0xFFFFFF00);
m_assign_gateway.SetAddress(0xFFFFFFFF);
m_cancel_button.EnableWindow();
m_ok_button.EnableWindow();
// Initialize dialog boxes
CheckDlgButton(IDC_ASSIGNRELOAD, FALSE);
CheckDlgButton(IDC_ASSIGNCLOSE, FALSE);
CheckDlgButton(IDC_USEPING, TRUE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAssignDlg::OnCancel()
{
// Close the socket
theApp.m_operation = PORT_CLOSED;
// Close the dialog
CDialog::OnCancel();
}
void CAssignDlg::OnOK()
{
UpdateData();
if(m_password!="888888"){
AfxMessageBox("请输入密码!");
return;
}
if(!m_timeout && !m_error){
SendBroadcast();
// Build an assignment packet and clear ARP Table
Build_Buffer();
Clear_ARP_Cache();
// Start the Timer
SetTimer(ASSIGN_TIMER, STEP, NULL);//设置ASSIGN_TIMER定时器
// Disable the OK button
m_ok_button.EnableWindow(FALSE);
} else {
// Handle OK pressed after timeout
theApp.m_operation = PORT_CLOSED;
CDialog::OnOK();
}
}
void CAssignDlg::OnTimer(UINT nIDEvent)
{
//-----------------------------------------------------
// Perform Tasks for first time
//-----------------------------------------------------
if(m_interval == 0){
}
//-----------------------------------------------------
// Perform Tasks for each interval
//-----------------------------------------------------
// Adjust Progress Bar
m_progress.StepIt();
// Set Progress Bar Text
m_progress_text.Format("%i", m_progress.GetPos()/1000);
m_progress_text += " 秒剩余时间";
UpdateData(FALSE);
// Check for timeout
if( (m_progress.GetPos() == 0) && !IsDlgButtonChecked(IDC_ASSIGNRELOAD) ){
// Stop Timer on timeout
KillTimer(ASSIGN_TIMER);
// Set the timeout flag and enable the OK button
m_timeout = 1;
// Update the progress bar text
m_progress_text = "Timeout -- Did not receive an acknowlegement";
UpdateData(FALSE);
// Enable OK button
m_ok_button.SetWindowText("关闭");
m_ok_button.EnableWindow();
} else
// Add ARP Entry (after 500ms)
if(m_progress.GetPos() == (RANGE-(2*STEP)) ){
Add_ARP_Entry();
} else
// Send Ping Gleaning Command (after 1 second)
if(m_progress.GetPos() == (RANGE-(4*STEP)) ){
if(IsDlgButtonChecked(IDC_USEPING)){
Ping_Gleaning();
} else {
SendAssignment();
}
}
// Send Assignment Packet (after 6 seconds)
if(m_progress.GetPos() == (RANGE-(24*STEP)) ){
if(IsDlgButtonChecked(IDC_USEPING)){
SendAssignment();
}
}
//-----------------------------------------------------
// Increment Interval
//-----------------------------------------------------
m_interval++;
CDialog::OnTimer(nIDEvent);
}
void CAssignDlg::ReceivePacket()
{
unsigned char buffer[4096] = {0};
int num_bytes;
//---------------------------------------------------------------
// Receive Packet from Buffer
//---------------------------------------------------------------
// IP and Port of sender.
CString destIP;
UINT destPort;
num_bytes = theApp.m_socket.ReceiveFrom(buffer, 4096, destIP, destPort);
// Check for errors
if(num_bytes >= 10000){
// Handle Error
AfxMessageBox("接受数据错误!");
return;
} else {
// Handle Error
//AfxMessageBox("Received Packet");
}
//---------------------------------------------------------------
// Verify Packet
//---------------------------------------------------------------
if( (num_bytes == 4) && (buffer[0] == 0x03) &&
(buffer[2] == (m_rand >> 8)) && (buffer[3] == (m_rand & 0x00FF)))
{
// Packet verified, kill timer
KillTimer(ASSIGN_TIMER);
// Decode ACK type
switch(buffer[1]){
case 0x01: // Programming Sucessful
// Update the progress bar text
m_progress_text = "成功 -- 装置写入";
UpdateData(FALSE);
// Enable OK button
m_error = 1; // Set error flag to close window on OK
m_ok_button.SetWindowText("关闭");
m_ok_button.EnableWindow();
// If autoclose, close the dialog
if(IsDlgButtonChecked(IDC_ASSIGNCLOSE)){
theApp.m_operation = PORT_CLOSED;
CDialog::OnOK();
}
break;
case 0x00: // Address Rejected due to mismatched MAC address
// Update the progress bar text
m_progress_text = "故障 -- 装置不识MAC地址";
UpdateData(FALSE);
// Enable OK button
m_error = 1;
m_ok_button.SetWindowText("关闭");
m_ok_button.EnableWindow();
break;
case 0xFF: // Unknown Error
// Update the progress bar text
m_progress_text = "故障 -- 不明错误";
UpdateData(FALSE);
// Enable OK button
m_error = 1;
m_ok_button.EnableWindow();
break;
}
} else
{
AfxMessageBox("接受无效设置包");
}
}
void CAssignDlg::Build_Buffer()
{
unsigned char IP[4]; // contains the old IP address
int i;
//-----------------------------------------------------------------
// Fill buffer with Assignment Packet
//-----------------------------------------------------------------
//
for(i = 0; i < sizeof(m_assignment_buff); i++){
m_assignment_buff[i] = 0;
}
m_rand = rand();
m_assignment_buff[0] = 0x02;
m_assignment_buff[1] = 0x00;
m_assignment_buff[2] = (m_rand >> 8);
m_assignment_buff[3] = (m_rand & 0x00FF);
// Get IP Address, Subnet, and Gateway
m_assign_ip.GetAddress(m_assignment_buff[4],m_assignment_buff[5], m_assignment_buff[6], m_assignment_buff[7]); // Get new address
m_assign_subnet.GetAddress(m_assignment_buff[8],m_assignment_buff[9], m_assignment_buff[10], m_assignment_buff[11]);
m_assign_gateway.GetAddress(m_assignment_buff[12],m_assignment_buff[13], m_assignment_buff[14], m_assignment_buff[15]);
// If a cell is selected, get the current (old) IP and MAC address (buff 16 - 21)
if(theApp.pMainDlg->m_netdisplay.IsCellSelected()){
theApp.pMainDlg->m_netdisplay.GetMACAddress(&m_assignment_buff[16]);
theApp.pMainDlg->m_netdisplay.GetIPAddress(&IP[0]); // Get old address and store in IP
} else {
AfxMessageBox("没有选择嵌入单元!");
}
// buff 22 + 23 are zero
}
void CAssignDlg::Clear_ARP_Cache()
{
// Delete ARP Table
WinExec("arp -d *", SW_HIDE);
}
void CAssignDlg::Add_ARP_Entry()
{
//-----------------------------------------------------------------
// Add MAC address with new IP address to the ARP Table
//-----------------------------------------------------------------
CString arp_call;
CString temp_str;
// Build the ARP String
arp_call = "arp -s ";
// Add new IP Address
temp_str.Format("%i.%i.%i.%i ", m_assignment_buff[4], m_assignment_buff[5], m_assignment_buff[6], m_assignment_buff[7]);
arp_call += temp_str;
// Add MAC Address
temp_str.Format("%x-%x-%x-%x-%x-%x", m_assignment_buff[16], m_assignment_buff[17], m_assignment_buff[18],m_assignment_buff[19],m_assignment_buff[20],m_assignment_buff[21]);
arp_call += temp_str;
// Add to ARP Table
WinExec(arp_call, SW_HIDE); // Add ARP entry
}
void CAssignDlg::Ping_Gleaning()
{
//-----------------------------------------------------------------
// Ping the device at the new IP Address
//-----------------------------------------------------------------
CString ping_call;
CString temp_str;
// Build the ARP String
ping_call = "ping -n 6 ";
// Add new IP Address
temp_str.Format("%i.%i.%i.%i ", m_assignment_buff[4], m_assignment_buff[5], m_assignment_buff[6], m_assignment_buff[7]);
ping_call += temp_str;
// Add to ARP Table
WinExec(ping_call, SW_SHOW); // Add ARP entry
}
void CAssignDlg::SendAssignment()
{
CString temp_str;
//-----------------------------------------------------------------
// Send the packet directly to the device's MAC address (use new IP address)
//-----------------------------------------------------------------
temp_str.Format("%i.%i.%i.%i ", m_assignment_buff[4], m_assignment_buff[5], m_assignment_buff[6], m_assignment_buff[7]);
if(SOCKET_ERROR == theApp.m_socket.SendTo(m_assignment_buff, 24, theApp.pMainDlg->m_netdisplay.GetPort(), temp_str)){
CString str;
str.Format("%i", GetLastError());
AfxMessageBox(str + " Unable To Send");
}
}
void CAssignDlg::SendBroadcast()
{
//-----------------------------------------------------------------
// Send Broadcast clear IP Packets
//-----------------------------------------------------------------
// Note: This section may be moved to a function to allow
// multiple re-tries.
//
unsigned char buff[10];
buff[0] = 0x06;
buff[1] = 0x00;
do{
m_rand = (rand()); // rand returns a number between 0 and 0x7FFF
} while(m_rand == 0); // make sure m_rand is not zero
buff[2] = (m_rand >> 8);
buff[3] = (m_rand & 0x00FF);
unsigned int i;
for(i = 0; i < theApp.m_numports; i++){
if(m_networkerror){
return;
}
theApp.pMainDlg->m_netdisplay.GetMACAddress(&buff[4]);
if(SOCKET_ERROR == theApp.m_socket.SendTo(buff, 10, theApp.m_ports[i], NULL)){
m_networkerror = 1;
CString str;
str.Format("错误代码: %i.", GetLastError());
AfxMessageBox(str + " 不能够发送包. 请检查网络连接.");
OnCancel();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -