📄 listendlg.cpp
字号:
}
//将协议int转为字符串
char* CListenDlg::CheckProtocol(int iProtocol)
{
for(int i=0; i<MAX_PROTO_NUM; i++)
{
if(ProtoMap[i].ProtoNum==iProtocol)
{
return ProtoMap[i].ProtoText;
}
}
return "";
}
//得到本机IP
char* GetIp()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
char *ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
return ip;
}
//得到当前时间
CString GetNowTime()
{
CTime time;
CString str;
time = CTime::GetCurrentTime();
str = time.Format("%Y-%m-%d %H:%M:%S");
return str;
}
//写入文件
BOOL CreateLogFile(char *strInfo,char *strFileName)
{
if (lstrlen(strInfo)<1)
return FALSE;
CFile file;
file.Open(strFileName,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate/*modeWrite|CFile::modeRead)*/);//CFile::modeNoTruncate为不截去以前的数据,追加新数据
file.SeekToEnd();
CString temp;
char *pStrInfo;
temp=strInfo;
temp=temp+"\r\n";
pStrInfo=temp.GetBuffer(temp.GetLength());
file.Write(pStrInfo,::lstrlen(pStrInfo));
file.Close();
return TRUE;
}
//复制到剪贴板
BOOL CListenDlg::SetClipboard(char* pData)
{
// pData="12345";
if(::lstrlen(pData)<1)
return FALSE;
if(!::OpenClipboard(m_hWnd))
return FALSE;
::EmptyClipboard();
HANDLE hData=::GlobalAlloc(GHND|GMEM_SHARE,::lstrlen(pData)+1); //申请全局内存
char* pToData=(char*)::GlobalLock(hData); //得到该内存的指针,锁定改内存
::lstrcpy(pToData,pData); //将该字符串复制给该内存
::GlobalUnlock(hData); //解锁
::SetClipboardData(CF_TEXT,hData);
::CloseClipboard();
return TRUE;
}
void CListenDlg::OnButton3()
{
// TODO: Add your control notification handler code here
DelListBuf(); //列表框清空
}
//将IP记录显示到ListBox
void CListenDlg::ShowIpInfo(char *pData, int len)
{
if(len<0)
return;
CString str;
char szSourceIP[32]={0};
char szDestIP[32]={0};
char szSourcePort[16]={0}; //截取到的源端口
char szDestPort[16]={0}; //截取到的目标端口
char szSourceIPt[32]={0}; //截取到源IP
char szDestIPt[32]={0}; //截取到目标IP
char szWprot[5]={0}; //取得输入端口
char szIP[32]={0}; //取得输入IP
char szProto[16]={0}; //取得输入协议
if(strcmp(m_HostIp,m_szSourceIP)==0)
{
strcat(szSourceIP,"(本机)");
strcat(szSourceIP,m_szSourceIP);
}
else
strncpy(szSourceIP,m_szSourceIP,16);
if(strcmp(m_HostIp,m_szDestIP)==0)
{
strcat(szDestIP,"(本机)");
strcat(szDestIP,m_szDestIP);
}
else
strncpy(szDestIP,m_szDestIP,16);
strncpy(szSourcePort,m_szSourcePort,5);
strncpy(szDestPort,m_szDestPort,5);
strncpy(szDestIPt,m_szSourceIP,32);
strncpy(szSourceIPt,m_szDestIP,32);
strncpy(szProto,m_szProtocol,16);
wsprintf(szWprot,"%d",GetDlgItemInt(IDC_EDIT1));
GetDlgItem(IDC_EDIT2)->GetWindowText(szIP,32);
BOOL bProt=m_IsAllPort|(strcmp(szSourcePort,szWprot)==0)|
(strcmp(szDestPort,szWprot)==0); //判断端口规则
BOOL bIp=m_IsAllIp|(strcmp(szSourceIPt,szIP)==0)|
(strcmp(szDestIPt,szIP)==0); //判断IP规则
BOOL bProto=m_IsAllProto|(strcmp(szProto,m_szProto)==0); //判断协议规则
if(bProt&&bIp&&bProto)
{
CString sNowTime=GetNowTime();
str.Format("[%s] %s:%s->%s:%s 包大小(头/总)%d/%d %s",m_szProtocol,szSourceIP,
m_szSourcePort,szDestIP,m_szDestPort,m_ihLen,len,
sNowTime.GetBuffer(sNowTime.GetLength()));
int nCount=((CListBox*)GetDlgItem(IDC_LIST1))->GetCount();
if (nCount>=1000)
DelListBuf();
//显示
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(str);
//绑定数据
IPDATA* pIpData = new IPDATA;
pIpData->len = len;
pIpData->buf = new char[len];
memcpy(pIpData->buf, pData, len);
((CListBox*)GetDlgItem(IDC_LIST1))->SetItemData(nCount,(DWORD)pIpData);
((CListBox*)GetDlgItem(IDC_LIST1))->SetCaretIndex(nCount);
int nCheck=((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
if(nCheck)
CreateLogFile(str.GetBuffer(/*str.GetLength()*/0));
}
}
//端口过滤
void CListenDlg::OnRadio2()
{
m_IsAllPort=TRUE;
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
}
void CListenDlg::OnRadio3()
{
m_IsAllPort=FALSE;
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
}
//IP过滤
void CListenDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
m_IsAllIp=TRUE;
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}
void CListenDlg::OnRadio4()
{
// TODO: Add your control notification handler code here
m_IsAllIp=FALSE;
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}
//文本框只允许输入数字
BOOL CListenDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(WM_CHAR==pMsg->message)
{
int nChar=(int)(pMsg->wParam);
if((nChar<48||nChar>57)&&(nChar!=8))
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
//协议过滤
void CListenDlg::OnRadio6()
{
// TODO: Add your control notification handler code here
m_IsAllProto=FALSE;
strncpy(m_szProto,"TCP ",5);
}
void CListenDlg::OnRadio5()
{
// TODO: Add your control notification handler code here
m_IsAllProto=TRUE;
}
void CListenDlg::OnRadio7()
{
// TODO: Add your control notification handler code here
m_IsAllProto=FALSE;
strncpy(m_szProto,"UDP ",5);
}
void CListenDlg::OnRadio8()
{
// TODO: Add your control notification handler code here
m_IsAllProto=FALSE;
strncpy(m_szProto,"ICMP",5);
}
//复制源IP,右键菜单
void CListenDlg::OnCopySip()
{
// TODO: Add your command handler code here
int n=m_NewList.GetCurSel();
CString SelAllStr,sSIp,sDIp;
m_NewList.GetText(n,SelAllStr);
//从得到CLIST中得到目标IP
int nBegin=SelAllStr.Find("] ");
int nEnd=SelAllStr.Find("->");
sSIp=SelAllStr.Mid(nBegin+2,nEnd-nBegin-2);
TRACE(sSIp);
if('('==sSIp.GetAt(0))
{
nBegin=sSIp.Find(')');
sSIp=sSIp.Mid(nBegin+1,sSIp.GetLength());
}
// AfxMessageBox(sSIp.GetAt(0));
SetClipboard(sSIp.GetBuffer(sSIp.GetLength())); //拷入剪贴板
}
//复制目标IP,右键菜单
void CListenDlg::OnCopyDip()
{
// TODO: Add your command handler code here
int n=m_NewList.GetCurSel();
CString SelAllStr,sSIp,sDIp;
m_NewList.GetText(n,SelAllStr);
int nBegin=SelAllStr.Find('>');
int nEnd=SelAllStr.Find("包");
sDIp=SelAllStr.Mid(nBegin+1,nEnd-nBegin-2);
TRACE(sDIp);
if('('==sDIp.GetAt(0))
{
nBegin=sDIp.Find(')');
sDIp=sDIp.Mid(nBegin+1,sDIp.GetLength());
}
// AfxMessageBox(sDIp);
BOOL N=SetClipboard(sDIp.GetBuffer(sDIp.GetLength())); //拷入剪贴板
}
//关于
void CListenDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if(IDM_SYSMENU==nID)
DialogBox(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDD_DIALOG1),m_hWnd,DialogProc);
CDialog::OnSysCommand (nID, lParam);
}
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,
WPARAM wParam,LPARAM lParam)
{
if(uMsg==WM_COMMAND&&LOWORD(wParam)==IDCLOSE)
{
EndDialog(hwndDlg,1);
return TRUE;
}
return FALSE;
}
void CListenDlg::OnSelchangeList1() //选择列表
{
// TODO: Add your control notification handler code here
int n=m_NewList.GetCurSel();
CString SelAllStr;
DWORD data=m_NewList.GetItemData(n); //得到绑定数据在m_IpShowDlg中显示
if (data<1)
return;
IPDATA* pIpData=(IPDATA*)data;
m_IpShowDlg.ShowIpData(pIpData->buf,pIpData->len);
// CreateLogFile(pIpData->buf);
//AfxMessageBox(SelAllStr);
}
//清除和ListBox绑定的数据和列表
void CListenDlg::DelListBuf()
{
for(int i=0;i<m_NewList.GetCount();i++)
{
DWORD data=m_NewList.GetItemData(i);
if (data>0)
{
IPDATA* pData=(IPDATA*)data;
delete pData; //清除堆内存
}
}
m_IpShowDlg.m_CharEdit.SetWindowText("");
m_IpShowDlg.m_HexEdit.SetWindowText("");
m_IpShowDlg.m_NumEdit.SetWindowText("");
m_NewList.ResetContent();
}
//程序退出
void CListenDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
DelListBuf();
DeleteCriticalSection(&m_ls);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -