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

📄 ipmondlg.cpp

📁 sniffer for win 2k 用winrar解压
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
        if( PeekMessage( &msg , 0 , WM_CLOSE,WM_CLOSE,PM_NOREMOVE ) )
		{
 	        closesocket( pDlg->m_s ) ;
 	        pDlg->m_threadID = 0 ;
			// Only after you see Next message you can press on Start button 
			pDlg->m_start.EnableWindow(TRUE) ; 
			break ;
		}
		memset( buf , 0 , sizeof(buf) ) ;
		iRet = 	recv( pDlg->m_s , buf , sizeof( buf ) , 0 ) ;
		if( iRet == SOCKET_ERROR )
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
			continue ;
		}
		else
			if( *buf )
			{	
				// check IP here
				//  sprintf( szErr , "recv() = %ld " , iRet ) ;
				//	AfxMessageBox( szErr ) ;
				bufwork   = buf ;
        		pIpHeader = (IPHEADER *)bufwork ;
	            WORD iLen = ntohs(pIpHeader->total_len) ;
				while( TRUE )
				{
					if( iLen <= iRet )
					{
 					 	//if( pDlg->IsIpofHost( pIpHeader ) )
						{ // Form the line for m_list
 							ina.S_un.S_addr = pIpHeader->sourceIP ;
							pSource = inet_ntoa( ina ) ;
							strcpy( szSource , pSource ) ;
							ina.S_un.S_addr = pIpHeader->destIP ;
							pDest = inet_ntoa( ina ) ;
							strcpy( szDest , pDest ) ;
							CString str, strProto, strSourPort, strDestPort, strData, strSize;

/*							
							if( pIpHeader->sourceIP == ( pDlg->m_ipcheckedhost ? 
								           pDlg->m_ipcheckedhost : pDlg->m_iphostsource ) )
								strIP.Format("%s -> %s len = %d ttl=%d proto=%s" , szSource , szDest ,
						 					iLen , pIpHeader->ttl , get_proto_name( pIpHeader->proto ) ) ;
							else
								strIP.Format("%s -> %s len = %d ttl=%d proto=%s" , szSource , szDest ,
											iLen , pIpHeader->ttl , get_proto_name( pIpHeader->proto ) ) ;
*/							

							strProto = get_proto_name( pIpHeader->proto );
							
							/*-------------------zhuwei add(2002.11.9)-----------------------------*/
							HdrLen = pIpHeader->header_len&0xf;
							HdrLen *= 4;
							totallen = ntohs(pIpHeader->total_len);
							totallen-=HdrLen;
							switch(pIpHeader->proto)
							{
							case IPPROTO_ICMP:
								{
									pICMPHead=(struct ICMPPacketHead *)(buf+HdrLen);
									//strL4.Format(" type:%d code:%d\n",pICMPHead->Type,pICMPHead->Code);
									strSourPort = "-";
									strDestPort = "-";
									pdata=((BYTE *)pICMPHead)+ICMP_HEAD_LEN;
									totallen -= ICMP_HEAD_LEN;
									break;
								}
							case IPPROTO_TCP:
								{
									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;  //in fact only 4 bits
									HdrLen *= 4;
									pdata=((BYTE *)pTCPHead)+HdrLen;
									totallen -= HdrLen;
									break;
								}
							case IPPROTO_UDP:
								{
									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;
									break;
								}
							}

							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);

							//str = strIP+strL4+strData;
							pDlg->AddData(strProto,szSource,strSourPort,szDest,strDestPort,strSize,strData);
							/*---------------------------------------------------------------------*/
				  			//pDlg->m_list.AddString( str ) ;  /* zhuwei add(2002.11.9) comment */
							//pDlg->m_list.InsertString(0,str);  /* zhuwei add(2002.11.9) */

							// Sleep stabilize work of list , otherwise  sometimes pressing on scroll
							// cased close of program

							//Sleep(50) ; /* zhuwei add(2002.11.9) comment */
						}
						if( iLen < iRet )
						{
							iRet -= iLen ;
							bufwork  += iLen ;
        					pIpHeader = (IPHEADER *)bufwork ;
						}
						else
							break ; // pIpHeader->total_len == iRet and go out
					}
					else
					{ // read last part of buf. I wrote it , but always recv() read exactly 
                      // the lenght of the packet
						int iLast = iLen - iRet ;
						pLastBuf = new char [ iLen ] ;
						int iReaden = iRet ;
						memcpy( pLastBuf , bufwork , iReaden ) ;
						iRet = 	recv( pDlg->m_s , pLastBuf + iReaden , iLast , 0 ) ;
						if( iRet == SOCKET_ERROR )
						{
							dwErr = WSAGetLastError() ;
							sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
							break ;
						}
						else
						{
							bufwork = pLastBuf ;	
       						pIpHeader = (IPHEADER *)bufwork ;
							if( iRet == iLast )
								iRet = iLen ;
							else
							{ // read all last data
								iReaden += iRet ;
								iLast -= iRet ;
								while( TRUE )
								{
									iRet = recv( pDlg->m_s , pLastBuf +iReaden , iLast , 0 ) ;
									if( iRet == SOCKET_ERROR )
									{
										dwErr = WSAGetLastError() ;
										sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
										break ;
									}
									else
									{
								        iReaden += iRet ;
								        iLast -= iRet ;
									    if( iLast <= 0 ) 
										    break ;
									}	
								} // while
							}
						}
					}	
				}   // while
				if( pLastBuf )
					delete [ ] pLastBuf ;
			}
			else
			{
				AfxMessageBox( "No data on network" ) ;
				continue ;
			}
		//Sleep( 100 ) ; //  Polling each 100 millisecond /* zhuwei add(2002.11.9) comment */
	}

	return TRUE ;
}

void CIpmonDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	char        szErr [ 50 ] , szHostName[MAX_PATH];
	DWORD       dwErr ;
    SOCKADDR_IN sa;
	//int         iRet ;
	

	//m_Local = m_check.GetCheck() ;
  	//iRet = m_ipctrl.GetAddress( m_ipsource ) ;
	gethostname(szHostName, sizeof(szHostName)) ;


 	//m_iphostsource = ntohl(m_ipsource) ;
	m_iphostsource = m_ipsource ;
    

  	//iRet = m_ipctrlhost.GetAddress( m_iphost ) ;
 	m_ipcheckedhost = ntohl(m_iphost) ;

 	//if( IsThisHostIP() ) // Check only specific IP of this host
	{
		CString str ;
		if( 0 == m_threadID )
		{
			str.LoadString( IDS_STOP ) ;
			m_start.SetWindowText( str ) ;
		}
		else
		{
			if( m_threadID )
			{
 	            PostThreadMessage(m_threadID,WM_CLOSE,0,0) ;
			    str.LoadString( IDS_START ) ;
		        m_start.SetWindowText( str ) ;
				m_start.EnableWindow(FALSE) ;
			}
			return ;
		}
    	DWORD dwBufferLen[10] ;
		DWORD dwBufferInLen= 1 ;
		DWORD dwBytesReturned = 0 ;
		m_s = socket( AF_INET , SOCK_RAW , IPPROTO_IP ) ;
		if( INVALID_SOCKET == m_s )
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error socket() = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;

		}
		int rcvtimeo = 5000 ; // 5 sec insteadof 45 as default
        if( setsockopt( m_s , SOL_SOCKET , SO_RCVTIMEO , (const char *)&rcvtimeo , sizeof(rcvtimeo) ) == SOCKET_ERROR)
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error WSAIoctl = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;
		}
		sa.sin_family = AF_INET;
 		sa.sin_port = htons(7000);
		sa.sin_addr.s_addr= m_iphostsource;
        if (bind(m_s,(PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR)
		{
			dwErr = WSAGetLastError() ;
			sprintf( szErr , "Error bind() = %ld " , dwErr ) ;
			AfxMessageBox( szErr ) ;
			closesocket( m_s ) ;
			return ;
		} 
        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 ;
		}
	}
	//else
	{
	//	AfxMessageBox( "IP not of this host" ) ;
	}
}


// If you want check other computers simply return TRUE always
//DEL BOOL CIpmonDlg::IsThisHostIP() 
//DEL {
//DEL 	char  szHostName[MAX_PATH];
//DEL 	gethostname(szHostName, sizeof(szHostName)) ;
//DEL 	HOSTENT *pHE  = gethostbyaddr ( (const char *)&m_iphostsource , 4, PF_INET ) ;
//DEL 	if( pHE && m_iphostsource )
//DEL 	{
//DEL 	    char *pDelim =  strchr( pHE->h_name , '.' ) ;
//DEL //		if( !stricmp( pDelim ? pDelim + 1 : pHE->h_name , szHostName ) )
//DEL         if(  pDelim ) *pDelim = 0 ;  
//DEL         if( !stricmp( pDelim ? pDelim + 1 : pHE->h_name , szHostName ) || 
//DEL 			!stricmp( pHE->h_name , szHostName ) )
//DEL 		    return TRUE ;	
//DEL 		else
//DEL 		    return FALSE ;
//DEL 	}	
//DEL 	else
//DEL 		return FALSE ;	
//DEL }	


void CIpmonDlg::close_thread()
{
	if( NULL != m_threadID )
 	    PostThreadMessage(m_threadID,WM_CLOSE,0,0) ;
	if( m_IPArr.GetSize() )
	    m_IPArr.RemoveAll() ;
}

void CIpmonDlg::OnOK() 
{
	// TODO: Add extra validation here
	close_thread() ;
	CDialog::OnOK();
}

void CIpmonDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	close_thread() ;
	CDialog::OnCancel();
}



void CIpmonDlg::AddData(CString s0, CString s1, CString s2, CString s3, CString s4, CString s5, CString s6)
{
	int index;

	index = m_ctrList.InsertItem(0,s0);
	m_ctrList.SetItem(index,1,LVIF_TEXT,s1, 0, 0, 0,0);
	m_ctrList.SetItem(index,2,LVIF_TEXT,s2, 0, 0, 0,0);
	m_ctrList.SetItem(index,3,LVIF_TEXT,s3, 0, 0, 0,0);
	m_ctrList.SetItem(index,4,LVIF_TEXT,s4, 0, 0, 0,0);
	m_ctrList.SetItem(index,5,LVIF_TEXT,s5, 0, 0, 0,0);
	m_ctrList.SetItem(index,6,LVIF_TEXT,s6, 0, 0, 0,0);
	//m_ctrList.SetItem(index,1,LVIF_TEXT,s2, 0, 0, 0,0);

}




⌨️ 快捷键说明

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