?? 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 + -