int Sniffer::SinfferData() { FileLog(SnifferLogFileName, "等待数据..."); int iRet = -1; sockaddr_in remoteAd; bzero(&remoteAd, sizeof(remoteAd)); int iRemoteAdLen = sizeof(sockaddr_in); unsigned char buff[2048]; memset(buff, '\0', sizeof(buff)); iRet = recvfrom(mSocket, buff, sizeof(buff), 0, (struct sockaddr *)&remoteAd, &iRemoteAdLen); timeval tout; tout.tv_sec = 3; tout.tv_usec = 0; fd_set rset; FD_ZERO(&rset); FD_SET(mSocket, &rset); int iRetS = select(mSocket + 1, &rset, NULL, NULL, &tout); if(iRetS > 0) { iRet = recvfrom(mSocket, buff, sizeof(buff), 0, (struct sockaddr *)&remoteAd, &iRemoteAdLen); if(iRet > 0) { AnalyseData(buff, iRet); } } else if(iRetS == 0){ iRet = 0; // showLog("Jni.Sniffer.SinfferData", "Sinffer no data for 3 second!"); } else { // ILog("Jni.Sniffer.SinfferData", "Sinffer data error!"); } return iRet; }
BOOL MultiProtocolProcess::DataProcessData(int nSocketID,const char *recvData ,int nRecvLen) { if(m_nBufLenCount + nRecvLen > MAX_RECVICE_BUFFER) { memset(m_RecvBuf,0,MAX_RECVICE_BUFFER); return FALSE; } memcpy(m_RecvBuf+m_nBufLenCount,recvData,nRecvLen); m_nBufLenCount = m_nBufLenCount + nRecvLen; int nSpaceLen = m_nBufLenCount; char *pCurPoint = m_RecvBuf; int nPackageLen = 0; char *m_strHead = Cmdtypelist[0].HeadData; unsigned int temp_nHeadDataLen = strlen(m_strHead); char *m_strTail = Cmdtypelist[0].TailData; unsigned int temp_nTailDataLen = strlen(m_strTail); while(nSpaceLen >= MAIN_DATA_PACKAGE_LEN) { int nHeadPoint = -1; char *temppHeadCurPoint = pCurPoint; for (int i = 0;i<nSpaceLen;i++) { if(strncmp(pCurPoint,m_strHead,temp_nHeadDataLen) != 0) //检查包头 { ++pCurPoint; } else { nHeadPoint = i; break; } } if (-1 == nHeadPoint) { TRACE(_T("[ID:%d]包头错:len:%d,recv:%s\n"),nSocketID,nSpaceLen,temppHeadCurPoint); gWriteLogApp.WriteLog(FOR_ALARM,"%s [MultiProtocolProcess::ProcessData]%s 包头错 %s,没有找到%s\n",CTime::GetCurrentTime().Format("%H:%M:%S"),recvData,temppHeadCurPoint,m_strHead); nSpaceLen = 0; break; } char *ptempTailCurPoint = pCurPoint; int nTailPoint = 0; for(int i = 0 ; i < nSpaceLen; i++ ) { if(strncmp(ptempTailCurPoint,m_strTail,temp_nTailDataLen) != 0) //检查包尾 { ++ptempTailCurPoint; } else { nTailPoint = i; break; } } if( nTailPoint > 0 ) { nPackageLen = nTailPoint + temp_nTailDataLen; if( nPackageLen < MAX_DATA_PACKAGE_LEN ) { ZeroMemory(pCurPoint + nTailPoint,temp_nTailDataLen); AnalyseData(nSocketID,pCurPoint+temp_nHeadDataLen,nPackageLen-temp_nHeadDataLen-temp_nTailDataLen,DWORD(this)); TRACE("[ID:%d]AnalyseData:%s \n",nSocketID,pCurPoint); } else { TRACE(_T("[ID:%d]包长度超过允许的最大值:len:%d,recv:%s\n"),nSocketID,nSpaceLen,recvData); gWriteLogApp.WriteLog(FOR_ALARM,"%s [MultiProtocolProcess::ProcessData]%s包头错,没有找到%s\n",CTime::GetCurrentTime().Format("%H:%M:%S"),pCurPoint,m_strHead); } pCurPoint = pCurPoint + nPackageLen; nSpaceLen = nSpaceLen - nPackageLen - nHeadPoint; } else { if( nSpaceLen > MAX_DATA_PACKAGE_LEN ) { TRACE(_T("[ID:%d]超长的数据包:len:%d,recv:%s\n"),nSocketID,nSpaceLen,recvData); } else { TRACE(_T("[ID:%d]没有找到包尾:len:%d,recv:%s\n"),nSocketID,nSpaceLen,pCurPoint); } nSpaceLen = 0; break; } } if( nSpaceLen > 0 ) { m_nBufLenCount = nSpaceLen; if( pCurPoint != m_RecvBuf ) { memcpy(m_RecvBuf,pCurPoint,m_nBufLenCount); memset(m_RecvBuf+m_nBufLenCount,0,MAX_RECVICE_BUFFER-m_nBufLenCount); } } else { m_nBufLenCount = 0; memset(m_RecvBuf,0,MAX_RECVICE_BUFFER); } return TRUE; }