📄 frmmain.h
字号:
this->colIPAddrDataGridViewTextBoxColumn->HeaderText = L"ColIPAddr";
this->colIPAddrDataGridViewTextBoxColumn->Name = L"colIPAddrDataGridViewTextBoxColumn";
//
// colIPMaskDataGridViewTextBoxColumn
//
this->colIPMaskDataGridViewTextBoxColumn->DataPropertyName = L"ColIPMask";
this->colIPMaskDataGridViewTextBoxColumn->HeaderText = L"ColIPMask";
this->colIPMaskDataGridViewTextBoxColumn->Name = L"colIPMaskDataGridViewTextBoxColumn";
//
// Col1
//
this->Col1->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::ColumnHeader;
this->Col1->DataPropertyName = L"ColChoose";
this->Col1->FlatStyle = System::Windows::Forms::FlatStyle::Popup;
this->Col1->Frozen = true;
this->Col1->HeaderText = L"选择";
this->Col1->MinimumWidth = 40;
this->Col1->Name = L"Col1";
this->Col1->Resizable = System::Windows::Forms::DataGridViewTriState::False;
this->Col1->Width = 40;
//
// Col2
//
this->Col2->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::ColumnHeader;
this->Col2->DataPropertyName = L"ColRange";
dataGridViewCellStyle2->ForeColor = System::Drawing::Color::DimGray;
this->Col2->DefaultCellStyle = dataGridViewCellStyle2;
this->Col2->FillWeight = 26.93603F;
this->Col2->HeaderText = L"所属网段";
this->Col2->MinimumWidth = 110;
this->Col2->Name = L"Col2";
this->Col2->ReadOnly = true;
this->Col2->Resizable = System::Windows::Forms::DataGridViewTriState::False;
this->Col2->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;
this->Col2->Width = 110;
//
// Col6
//
this->Col6->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::ColumnHeader;
this->Col6->DataPropertyName = L"ColIPMask";
dataGridViewCellStyle3->ForeColor = System::Drawing::Color::DimGray;
this->Col6->DefaultCellStyle = dataGridViewCellStyle3;
this->Col6->FillWeight = 18.36061F;
this->Col6->HeaderText = L"网段掩码";
this->Col6->MinimumWidth = 110;
this->Col6->Name = L"Col6";
this->Col6->ReadOnly = true;
this->Col6->Resizable = System::Windows::Forms::DataGridViewTriState::False;
this->Col6->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;
this->Col6->Width = 110;
//
// Col3
//
this->Col3->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::ColumnHeader;
this->Col3->DataPropertyName = L"ColRangeL";
this->Col3->FillWeight = 44.81265F;
this->Col3->HeaderText = L"设置区间下界";
this->Col3->MinimumWidth = 110;
this->Col3->Name = L"Col3";
this->Col3->Resizable = System::Windows::Forms::DataGridViewTriState::False;
this->Col3->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;
this->Col3->Width = 110;
//
// Col4
//
this->Col4->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::ColumnHeader;
this->Col4->DataPropertyName = L"ColRangeH";
this->Col4->FillWeight = 45.16115F;
this->Col4->HeaderText = L"设置区间上界";
this->Col4->MinimumWidth = 110;
this->Col4->Name = L"Col4";
this->Col4->Resizable = System::Windows::Forms::DataGridViewTriState::False;
this->Col4->SortMode = System::Windows::Forms::DataGridViewColumnSortMode::NotSortable;
this->Col4->Width = 110;
//
// Col5
//
this->Col5->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::Fill;
this->Col5->DataPropertyName = L"ColIPAddr";
this->Col5->DataSource = this->dsAll;
this->Col5->DisplayMember = L"Range.ColIPAddr";
this->Col5->FillWeight = 364.7296F;
this->Col5->HeaderText = L"捕获用源IP";
this->Col5->MinimumWidth = 110;
this->Col5->Name = L"Col5";
this->Col5->ReadOnly = true;
this->Col5->Resizable = System::Windows::Forms::DataGridViewTriState::True;
//
// frmMain
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"$this.BackgroundImage")));
this->ClientSize = System::Drawing::Size(716, 508);
this->Controls->Add(this->lvMain);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->statusStrip1);
this->Controls->Add(this->groupBox1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->MaximizeBox = false;
this->Name = L"frmMain";
this->Text = L"ArpWinPcap";
this->Load += gcnew System::EventHandler(this, &frmMain::frmMain_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->tabControl1->ResumeLayout(false);
this->tabPage1->ResumeLayout(false);
this->tabPage1->PerformLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dgvOpt))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dsAll))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dtRange))->EndInit();
this->tabPage2->ResumeLayout(false);
this->tabPage2->PerformLayout();
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->statusStrip1->ResumeLayout(false);
this->statusStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private:
/**************************************
* 主窗体载入时调用的函数:
* 本函数功能:1、实现设备检测
2、信息存储到strDevInfo里
3、构造设备列表
**************************************/
System::Void frmMain_Load(System::Object^ sender, System::EventArgs^ e)
{
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE];
///获取本地机器设备列表
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
fnErrorMsg("Error in pcap_findalldevs_ex: " + gcnew String(errbuf));
exit(1);
}
///使用NetworkInterface获取信息并存储
array<System::Net::NetworkInformation::NetworkInterface^> ^nwInterface=System::Net::NetworkInformation::NetworkInterface::GetAllNetworkInterfaces();
strDevInfo=gcnew array<String ^,2>(nwInterface->Length,8);
///for循环对每一个设备操作一次,以采集信息
for(int i=0;i<nwInterface->Length;i++)
{
if(nwInterface[i]->NetworkInterfaceType!=System::Net::NetworkInformation::NetworkInterfaceType::Loopback &&
nwInterface[i]->NetworkInterfaceType!=System::Net::NetworkInformation::NetworkInterfaceType::Unknown) //若设备为可识别硬件设备时
{
cboxDev->Items->Add(nwInterface[i]->Description); //添加设备列表
///通过NetworkInterface获得各信息
strDevInfo[i,0]="设 备 ID: " +nwInterface[i]->Id;
strDevInfo[i,1]="名 称: " +nwInterface[i]->Name;
strDevInfo[i,2]="描 述: " +nwInterface[i]->Description;
strDevInfo[i,3]="MAC 地址: " +nwInterface[i]->GetPhysicalAddress()->ToString()
->Insert(10,"-")->Insert(8,"-")->Insert(6,"-")->Insert(4,"-")->Insert(2,"-");
if(nwInterface[i]->Speed>=1000000000 && nwInterface[i]->Speed%1000000000==0)
strDevInfo[i,6]="速 度: " +nwInterface[i]->Speed/1000000000 + " Gbps";
else if (nwInterface[i]->Speed>=1000000 && nwInterface[i]->Speed%1000000==0)
strDevInfo[i,6]="速 度: " +nwInterface[i]->Speed/1000000 + " Mbps";
else if (nwInterface[i]->Speed>=1000 && nwInterface[i]->Speed%1000==0)
strDevInfo[i,6]="速 度: " +nwInterface[i]->Speed/1000 + " Kbps";
else
strDevInfo[i,6]="速 度: " +nwInterface[i]->Speed+ " bps";
String ^strTmp;
///for循环在WinPcap打开的设备列表中找到当前设备并得到“地址簇”和“IP地址”信息等NetworkInterface不能得到的信息
for(d= alldevs; d != NULL; d= d->next)
{
pcap_addr_t *a;
char ip6str[128];
strTmp=gcnew String(d->name);
a=d->addresses;
if(strTmp->Contains(nwInterface[i]->Id))
{
if (a->addr->sa_family==AF_INET || a->addr->sa_family==AF_INET6)
strDevInfo[i,4]="地 址 簇: "+"IPv" +(a->addr->sa_family==AF_INET?4:6);
else
strDevInfo[i,4]="地 址 簇: 未知";
for(int j=1;a;j++,a=a->next)
{
switch(a->addr->sa_family)
{
case AF_INET:
if (a->addr)
strDevInfo[i,5]+="地 址"+ j +": "+ gcnew String(iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr))+" ";
if (a->netmask)
strDevInfo[i,5]+="\t掩码"+ j +": "+ gcnew String(iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr))+" ";
if (a->broadaddr)
strDevInfo[i,5]+="\t广播"+ j +": "+ gcnew String(iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
if (a->dstaddr)
strDevInfo[i,5]+="\t目的"+ j +": "+ gcnew String(iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
strDevInfo[i,5]+= "\r\n";
break;
case AF_INET6:
if (a->addr)
strDevInfo[i,5]+=" 地址"+ j +": "+ gcnew String(ip6tos(a->addr, ip6str, sizeof(ip6str)))+ "\r\n";
break;
}
}
strDevInfo[i,5]=strDevInfo[i,5]->Remove(strDevInfo[i,5]->Length-2,2);
}
}
///利用System.Net下不能获得不在网的适配器IP地址,来判断设备是否在线
array<System::Net::IPAddress ^> ^ ipAddr=System::Net::Dns::GetHostAddresses(System::Net::Dns::GetHostName());
strDevInfo[i,7]="网络连通: 否";
for each(System::Net::IPAddress^ j in ipAddr)
{
if(strDevInfo[i,5]->Contains(j->ToString()))
{
strDevInfo[i,7]="网络连通: 是";
}
}
}
}
///不再需要设备列表了,释放它
pcap_freealldevs(alldevs);
///调用自动选择函数已选择在线的设备
buttAutoSel_Click(sender,e);
}
/************************************************
* 改变被选择的设备时调用的函数:
* 本函数功能:1、根据当前设备更新设备信息一栏
2、根据当前设备更新详细设置一栏
3、根据当前设备更新提示信息
************************************************/
System::Void cboxDev_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
///从strDevInfo里读出数据更新设备信息一栏
txtDev->Clear();
fnPrint(txtDev,"",true);
for(int i=0;i<8;i++)
{
fnPrint(txtDev,strDevInfo[cboxDev->SelectedIndex,i],true);
}
///从strDevInfo里读出数据更新到数据集dsAll,以更新和dsAll绑定的详细设置一栏
array<String ^>^ strTmp=gcnew array<String ^>(1){"\r\n"};
array<String ^,2>^ strAddr;
strTmp=strDevInfo[cboxDev->SelectedIndex,5]->Split(strTmp,System::StringSplitOptions::RemoveEmptyEntries);
strAddr=gcnew array<String ^,2>(strTmp->Length,2);
dsAll->Tables["Range"]->Clear();
for(int i=0;i<strTmp->Length;i++)
{
strAddr[i,0]=strTmp[i]->Substring(strTmp[i]->IndexOf(":")+1,strTmp[i]->IndexOf("掩")-strTmp[i]->IndexOf(":")-1)->Trim();
strAddr[i,1]=strTmp[i]->Substring(strTmp[i]->IndexOf(":",strTmp[i]->IndexOf(":")+1)+1,strTmp[i]->IndexOf("广")-strTmp[i]->IndexOf(":",strTmp[i]->IndexOf(":")+1)-1)->Trim();
array<Byte,2>^ bAddr=gcnew array<Byte,2>(2,4);
for(int j=0;j<2;j++)
{
array<String ^>^ strTmp1=gcnew array<String ^>(1){"."};
strTmp1=strAddr[i,j]->Split(strTmp1,System::StringSplitOptions::RemoveEmptyEntries);
for(int k=0;k<4;k++)
{
bAddr[j,k]=Convert::ToByte(strTmp1[k]);
}
}
for(int k=0;k<4;k++)
{
bAddr[0,k]=bAddr[0,k]&bAddr[1,k];
}
bool bExist=false;
for(int k=0;k<dsAll->Tables["Range"]->Rows->Count;k++)
{
array<System::Object ^>^ objTmp=dsAll->Tables["Range"]->Rows[k]->ItemArray;
if(Convert::ToString(objTmp[1])==String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[0,0],(unsigned char)bAddr[0,1],(unsigned char)bAddr[0,2],(unsigned char)bAddr[0,3])
&& Convert::ToString(objTmp[5])==String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[1,0],(unsigned char)bAddr[1,1],(unsigned char)bAddr[1,2],(unsigned char)bAddr[1,3]))
{
bExist=true;
objTmp[4]+="." + strAddr[i,0];
dsAll->Tables["Range"]->Rows[k]->ItemArray[4]=objTmp[4];
}
}
if(!bExist)
dsAll->Tables["Range"]->Rows->Add(true,String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[0,0],(unsigned char)bAddr[0,1],(unsigned char)bAddr[0,2],(unsigned char)bAddr[0,3]),
String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[0,0],(unsigned char)bAddr[0,1],(unsigned char)bAddr[0,2],(unsigned char)(bAddr[0,3]|0x01)),
String::Format("{0}.{1}.{2}.{3}",(unsigned char)(bAddr[0,0]|(~bAddr[1,0])),(unsigned char)(bAddr[0,1]|(~bAddr[1,1])),(unsigned char)(bAddr[0,2]|(~bAddr[1,2])),(unsigned char)((bAddr[0,3]|(~bAddr[1,3]))&0xfe)),
strAddr[i,0],String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[1,0],(unsigned char)bAddr[1,1],(unsigned char)bAddr[1,2],(unsigned char)bAddr[1,3]),
String::Format("{0}.{1}.{2}.{3}",(unsigned char)bAddr[0,0],(unsigned char)bAddr[0,1],(unsigned char)bAddr[0,2],(unsigned char)(bAddr[0,3]|0x01)),
String::Format("{0}.{1}.{2}.{3}",(unsigned char)(bAddr[0,0]|(~bAddr[1,0])),(unsigned char)(bAddr[0,1]|(~bAddr[1,1])),(unsigned char)(bAddr[0,2]|(~bAddr[1,2])),(unsigned char)((bAddr[0,3]|(~bAddr[1,3]))&0xfe))
);
}
///更新提示信息
if(strDevInfo[cboxDev->SelectedIndex,7]=="网络连通: 是")
labStatus->Text="提示: 当前适配器网络连接正常。";
else
labStatus->Text="提示: 当前适配器处于未连网状态。";
}
/******************************************************************
* 点下自动选择按钮时调用的函数:
* 本函数功能:根据strDevInfo中是否在线的信息自动选择在线的设备
******************************************************************/
System::Void buttAutoSel_Click(System::Object^ sender, System::EventArgs^ e)
{
for(int i=0;i<strDevInfo->Length/8;i++)
{
if(strDevInfo[i,7]=="网络连通: 是")
{
cboxDev->SelectedIndex=i;
return;
}
}
if(cboxDev->SelectedIndex==-1)
cboxDev->SelectedIndex=0;
fnErrorMsg("未发现可用网络连接,无法选择适配器。\n请手动选择或确认网络连接后再点击“自动选择”。");
}
/*******************************************************************************
* 点下开始捕获按钮时调用的函数:
* 本函数是整个程序的主要部分:
1、判断开始条件是否满足
2、找到设备并打开
3、编译设置过滤器
4、向范围内各IP发送ARP包
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -