⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipmondlg.cpp

📁 sniffer 用原始套接字写的sniffer软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
							//////////////////保存目的ip/////////////////
							for(int k=0;k<16;k++)
							{
								iphead[pcount-1].destip +=szDest[k];
							}
							//////////////////////////////////////////////
							CString str, strProto, strSourPort, strDestPort, strData, strSize;
							strProto = get_proto_name( pIpHeader->proto );//获取协议类型
							HdrLen = pIpHeader->header_len&0xf;
							HdrLen *= 4;									//4个字节为一个单位

							iphead[pcount-1].headerlen .Format("%d",HdrLen);//保存头长cstring形式
							iphead[pcount-1].ttlen.Format("%d",pIpHeader->ttl);//生存时间
							iphead[pcount-1].checksum .Format("%d",pIpHeader->checksum );
							iphead[pcount-1].strversion .Format ("%d",pIpHeader->version );
							iphead[pcount-1].identf .Format("%d",pIpHeader->ident );

							totallen = ntohs(pIpHeader->total_len);
							strSize.Format("%d",totallen);

							iphead[pcount-1].totallen =strSize;		        //保存总长cstring形式

							totallen-=HdrLen;
							switch(pIpHeader->proto)
							{
							case IPPROTO_ICMP:
								{	
									iphead[pcount-1].pro ="ICMP";
									iphead[pcount-1].nextpro =0;
									pICMPHead=(struct ICMPPacketHead *)(buf+HdrLen);//去掉ip头
									//strL4.Format(" type:%d code:%d\n",pICMPHead->Type,pICMPHead->Code);
									strSourPort = "-";
									strDestPort = "-";
									pdata=((BYTE *)pICMPHead)+ICMP_HEAD_LEN;//去掉icmp头
									totallen -= ICMP_HEAD_LEN;
						///////////////////////////////保存包/////////////////////////////////
									icmphead[pcount-1].chksum .Format("%d",pICMPHead->ChkSum );
									icmphead[pcount-1].code .Format("%d",pICMPHead->Code);
									icmphead[pcount-1].type .Format("%d",pICMPHead->Type );
									icmphead[pcount-1].hlen  .Format("%d",ICMP_HEAD_LEN );	
									icmphead[pcount-1].dlen  .Format("%d",totallen);
									icmphead[pcount-1].sport="-";
									icmphead[pcount-1].dport="-";
						///////////////////////////////////////////////////////////////////////

									edit_icmp++;				//icmp 包个数加1
									break;
								}
							case IPPROTO_TCP:
								{
									iphead[pcount-1].pro ="TCP";
									iphead[pcount-1].nextpro =1;
									pTCPHead=(struct TCPPacketHead *)(buf+HdrLen);
									sourport = ntohs(pTCPHead->SourPort);
									destport = ntohs(pTCPHead->DestPort);
									//strL4.Format(" sour port:%d,dest port:%d",sourport,destport);
									strSourPort.Format("%d",sourport);
									strDestPort.Format("%d",destport);
 									HdrLen = (pTCPHead->HLen)>>4;  //HdrLen有8位,HLen4位,在高4位
									HdrLen *= 4;					//4个字节为一个单位
									pdata=((BYTE *)pTCPHead)+HdrLen;//去掉tcp头
									totallen -= HdrLen;

						//////////////////////////////保存包///////////////////////////
									tcphead[pcount-1].sourport =strSourPort;
									tcphead[pcount-1].destport =strDestPort;
									char temp1[32];
									itoa(pTCPHead->SeqNo,temp1,32);
									tcphead[pcount-1].seqno.Format("%ld",temp1 ); 
									char temp2[32];
									itoa(pTCPHead->AckNo,temp2,32);
									tcphead[pcount-1].ackno.Format("%ld",temp2); 
									tcphead[pcount-1].flag.Format("%d",pTCPHead->Flag); 
									tcphead[pcount-1].hlen.Format("%d",HdrLen);
									tcphead[pcount-1].dlen .Format("%d",totallen);
									tcphead[pcount-1].wndsize.Format("%d",pTCPHead->WndSize);
									tcphead[pcount-1].urgptr.Format("%d",pTCPHead->UrgPtr);
						
						///////////////////////////////////////////////////////////////

									edit_tcp++;
									break;
								}
							case IPPROTO_UDP:
								{
									iphead[pcount-1].pro ="UDP";
									iphead[pcount-1].nextpro =2;
									pUDPHead=(struct UDPPacketHead *)(buf+HdrLen);
									sourport = ntohs(pUDPHead->SourPort);
									destport = ntohs(pUDPHead->DestPort);
									//strL4.Format(" sour port:%d,dest port:%d",sourport,destport);
									strSourPort.Format("%d",sourport);
									strDestPort.Format("%d",destport);
									pdata=((BYTE *)pUDPHead)+UDP_HEAD_LEN;
									totallen -= UDP_HEAD_LEN;
						/////////////////////////存udp包///////////////////////////////////

									udphead[pcount-1].sourport =strSourPort;
									udphead[pcount-1].destport =strDestPort;
									udphead[pcount-1].chksum.Format("%d",pUDPHead->ChkSum );
									udphead[pcount-1].len  .Format("%d",UDP_HEAD_LEN );
									udphead[pcount-1].dlen.Format("%d",totallen);
						///////////////////////////////////////////////////////////////////

									edit_udp++;
									break;
								}
							default:
								{
									edit_other++;
								}								
							}

							if(pIpHeader->proto == IPPROTO_ICMP)
								strData.Format("type:%d code:%d data:%s",pICMPHead->Type,pICMPHead->Code,pdata);
							else strData.Format("  %s",pdata);

//							strSize.Format("%d",totallen);

							pDlg->AddData(strno,strProto,szSource,strSourPort,szDest,strDestPort,strSize,strData);
														
							pDlg->showcount();
//							pDlg->UpdateData(false);
							break ; // pIpHeader->total_len == iRet and go out
					
					
				}   // while终止
				if( pLastBuf )
					delete [ ] pLastBuf ;
			}
			else
			{
				AfxMessageBox( "No data on network" ) ;
				continue ;
			}
	}

	return TRUE ;

}

//向listview中添加数据
void CIpmonDlg::AddData(char *s,CString s0,CString s1, CString s2, CString s3, CString s4, CString s5, CString s6)
{
//	int List_index;
//	List_index=m_ctrList.GetItemCount();
//		List_index+=1;
	List_index = m_ctrList.InsertItem(pcount,s,0);
	m_ctrList.SetItem(List_index,1,LVIF_TEXT,s0, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,2,LVIF_TEXT,s1, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,3,LVIF_TEXT,s2, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,4,LVIF_TEXT,s3, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,5,LVIF_TEXT,s4, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,6,LVIF_TEXT,s5, 0, 0, 0,0);
	m_ctrList.SetItem(List_index,7,LVIF_TEXT,s6, 0, 0, 0,0);
	
}




//开始菜单
void CIpmonDlg::OnStart() 
{

	// TODO: Add your command handler code here
	NO=true;
	char        szErr [ 50 ] , szHostName[MAX_PATH];
	DWORD       dwErr ;
    SOCKADDR_IN sa;

	gethostname(szHostName, sizeof(szHostName)) ;//获取主机名
	m_iphostsource = m_ipsource ;
    
 	m_ipcheckedhost = ntohl(m_iphost) ;//将主机名由网络类型转换成主机类型

    	DWORD dwBufferLen[10] ;
		DWORD dwBufferInLen= 1 ;
		DWORD dwBytesReturned = 0 ;
		m_s = socket( AF_INET , SOCK_RAW , IPPROTO_IP ) ;//建立tcp socket
		if( INVALID_SOCKET == m_s )			//INVALID_SOCKET 套接字建立出错的返回值
		{
			dwErr = WSAGetLastError() ;		//获取出错的原因
			sprintf( szErr , "Error socket() = %ld " , dwErr ) ;	//把error socket=。。。放在szErr中
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;			//关闭socket
			return ;

		}
		//设置该socket的超时参数等选项
		int rcvtimeo = 5000 ; 
        if( setsockopt( m_s , SOL_SOCKET , SO_RCVTIMEO , (const char *)&rcvtimeo , sizeof(rcvtimeo) ) == SOCKET_ERROR)	//// 设置IP头操作选项
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error WSAIoctl = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;
		}
	//	将该socket与本机的某个网络接口绑定(注意绑定的IP地址不能是INADDR_ANY):
		sa.sin_family = AF_INET;			//代协议族,在socket编程中只能是AF_INET
 		sa.sin_port = htons(80);			//存储端口号(使用网络字节顺序)
		sa.sin_addr.s_addr= m_iphostsource;	//存储IP地址,使用in_addr这个数据结构
        if (bind(m_s,(PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR)
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error bind() = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;
		} 
   /*SIO_RCVALL属性。可以利用这个socket来读取经过本机的数据包了
	IPMon创建了一个新线程专门来读取该socket,以防止处理用户输入的主线程被阻塞。
    socket设置SIO_RCVALL属性,这样该socket就可以收到所有经过本机的数据
	socket,参数必须是 AF_INET、SOCK_RAW和IPPROTO_IP,否则不能设置SIO_RCVALL属性
	设置socket,用SIO_RCVALL来接受所有的数据包*/
        if( SOCKET_ERROR != WSAIoctl( m_s, SIO_RCVALL , &dwBufferInLen, sizeof(dwBufferInLen),             
                                      &dwBufferLen, sizeof(dwBufferLen),
									  &dwBytesReturned , NULL , NULL ) )
            AfxBeginThread( threadFunc , (LPVOID)this );
		else
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error WSAIoctl = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;
		}

 }

//暂停菜单
void CIpmonDlg::OnStop() 
{
	// TODO: Add your command handler code here
	NO=false;//由No控制抓包

}


void CIpmonDlg::OnExit() 
{
	// TODO: Add your command handler code here
		if( NULL != m_threadID )		//线程没有关闭
 			PostThreadMessage(m_threadID,WM_CLOSE,0,0) ;//发送消息关闭线程
	    if( m_IPArr.GetSize() )
			m_IPArr.RemoveAll() ;
		CDialog::OnOK();
}

//listview单击事件
void CIpmonDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString edit;
	int index;
	POSITION pos;
	pos=m_ctrList.GetFirstSelectedItemPosition();	//
	index=m_ctrList.GetNextSelectedItem(pos);
	if(index==-1)					//点到空行时
	{
		m_edit="";
		m_tree.DeleteAllItems();
	}
	else
	{
//		m_edit==;
		m_edit=packet[index].pack;
		this->SetDlgItemText(IDC_EDIT,m_edit);
		showtree(index);
	}
	UpdateData(false);
	*pResult = 0;

}

//显示数据包统计信息
 void CIpmonDlg::showcount()
 {
 	m_edit_tcp.Format("%ld",edit_tcp);
	this->SetDlgItemText(IDC_EDIT_TCP,m_edit_tcp);
 	m_edit_udp.Format("%ld",edit_udp);
 	this->SetDlgItemText(IDC_EDIT_UDP,m_edit_udp);
 	m_edit_icmp.Format("%ld",edit_icmp);
 	this->SetDlgItemText(IDC_EDIT_ICMP,m_edit_icmp);
 	m_edit_all.Format("%ld",edit_all);
 	this->SetDlgItemText(IDC_EDIT_ALL,m_edit_all);
 	m_edit_other.Format("%ld",edit_other);
 	this->SetDlgItemText(IDC_EDIT_OTHER,m_edit_other);

	///////////////进程控件显示//////////////////////////

	m_pro_udp.SetRange(0,500);			//设置范围
	float tempudp=0;
	tempudp=(float)edit_udp/edit_all;
	m_pro_udp.SetPos((int)(tempudp*500));
	m_pro_tcp.SetRange(0,500);			//设置范围
	float temptcp=0;
	temptcp=(float)edit_tcp/edit_all;
	m_pro_tcp.SetPos((int)(temptcp*500));
	m_pro_icmp.SetRange(0,500);			//设置范围
	float tempicmp=0;
	tempicmp=(float)edit_icmp/edit_all;
	m_pro_icmp.SetPos((int)(tempicmp*500));
	m_pro_other.SetRange(0,500);			//设置范围
	float tempother=0;
	tempother=(float)edit_other/edit_all;
	m_pro_other.SetPos((int)(tempother*500));

 }

//显示树状控件
void CIpmonDlg::showtree(int n)
{
	m_tree.DeleteAllItems();
	CString stree;
/*
	CString a;
	a.Format("%d",sizeof(long));
	MessageBox(a,"aa",MB_OK);
*/
	if(n!=-1)
	{	stree.Format("第%ld个数据包",n+1);
		HTREEITEM hRoot = m_tree.InsertItem(stree);
			HTREEITEM hSubItem = m_tree.InsertItem("网络层数据",hRoot);
				HTREEITEM hItem = m_tree.InsertItem("协议 : IP",hSubItem);
				hItem = m_tree.InsertItem("源IP : "+iphead[n].sourceip ,hSubItem);
				hItem = m_tree.InsertItem("目的IP : "+iphead[n].destip,hSubItem);
				hItem = m_tree.InsertItem("版本号: "+iphead[n].strversion ,hSubItem);
				hItem = m_tree.InsertItem("总长: "+iphead[n].totallen  ,hSubItem);
				hItem = m_tree.InsertItem("头长: "+iphead[n].headerlen  ,hSubItem);
				hItem = m_tree.InsertItem("下层协议: "+iphead[n].pro ,hSubItem);
				hItem = m_tree.InsertItem("校验和: "+iphead[n].checksum  ,hSubItem);
				hItem = m_tree.InsertItem("生存时间: "+iphead[n].ttlen ,hSubItem);
				hItem = m_tree.InsertItem("标识: "+iphead[n].identf  ,hSubItem);
			hSubItem=m_tree.InsertItem("传输层协议",hRoot);
			switch(iphead[n].nextpro  )
			{
			case 0:
			{
				hItem = m_tree.InsertItem("源端口 : "+icmphead[n].sport  ,hSubItem);
				hItem = m_tree.InsertItem("目的端口 : "+icmphead[n].dport ,hSubItem);
				hItem = m_tree.InsertItem("头长 : "+icmphead[n].hlen  ,hSubItem);
				hItem = m_tree.InsertItem("数据长 : "+icmphead[n].dlen  ,hSubItem);
				hItem = m_tree.InsertItem("校验和 : "+icmphead[n].chksum  ,hSubItem);
				hItem = m_tree.InsertItem("类型 : "+icmphead[n].type  ,hSubItem);
				hItem = m_tree.InsertItem("代码 : "+icmphead[n].code  ,hSubItem);
				break;
			}
			case 1:
				{
					hItem = m_tree.InsertItem("源端口 : "+tcphead[n].sourport  ,hSubItem);
					hItem = m_tree.InsertItem("目的端口 : "+tcphead[n].destport  ,hSubItem);
					hItem = m_tree.InsertItem("序号 : "+tcphead[n].seqno ,hSubItem);
					hItem = m_tree.InsertItem("确认号 : "+tcphead[n].ackno   ,hSubItem);
					hItem = m_tree.InsertItem("头长 : "+tcphead[n].hlen   ,hSubItem);
				    hItem = m_tree.InsertItem("数据长 : "+tcphead[n].dlen   ,hSubItem);
					hItem = m_tree.InsertItem("标识: "+tcphead[n].flag ,hSubItem);
					hItem = m_tree.InsertItem("窗口大小: "+tcphead[n].wndsize  ,hSubItem);
					hItem = m_tree.InsertItem("紧急指针: "+tcphead[n].urgptr  ,hSubItem);
					break;
				}
			case 2:
				{
					hItem=m_tree.InsertItem("源端口 : "+udphead[n].sourport  ,hSubItem);
					hItem=m_tree.InsertItem("目的端口 : "+udphead[n].destport  ,hSubItem);
					hItem=m_tree.InsertItem("校验和 : "+udphead[n].chksum  ,hSubItem);
					hItem=m_tree.InsertItem("头长 : "+udphead[n].len  ,hSubItem);
					hItem = m_tree.InsertItem("数据长 : "+udphead[n].dlen  ,hSubItem);

				}
			default:
				break;
			}
			
	}

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -