我在网上截获了一个数据包,我想数据包中数据的内容显示出来, 怎么处理?请指教,谢谢

解决方案 »

  1.   

    参见代码:#ifndef __ADIPANALYSE_H__
    #define __ADIPANALYSE_H__
    #pragma once
    #include <Winsock2.h>
    #include <process.h>#ifndef SIO_RCVALL
    #define SIO_RCVALL  _WSAIOW(IOC_VENDOR,1)
    #endif#pragma pack(8)
    typedef struct
    {
        unsigned char  ih_len:4;          // header length
        unsigned char  ih_ver:4;       // version
        unsigned char  ih_tos;            // type of service
        unsigned short ih_totallen;       // length of the packet
        unsigned short ih_ident;          // unique identifier
        unsigned short ih_flags;          
        unsigned char  ih_ttl;            
        unsigned char  ih_proto;          // protocol ( IP , TCP, UDP etc)
        unsigned short ih_chksum;       
        unsigned int   ih_sIP;
        unsigned int   ih_dIP;
    }AD_IPHEADER;
    #pragma pack()int    __stdcall AdGetHostIPs(DWORD **pdwIPs);
    char * __stdcall AdMapProto2Str(int nProto);
    int    __stdcall AdMapStr2Proto(char *pProtoStr);//TCP Header
    typedef struct

       unsigned short th_sport;        // Source port: 16 bits
       unsigned short th_dport;        // Destination port: 16 bits
       unsigned long  th_seq;          // Sequence Number: 32 bits
       unsigned long  th_ack;          // Acknowledgment Number: 32 bits
       unsigned char  th_lenres;       // Data Offset / reserved
       unsigned char  th_flag;         // ECN / Control Bits: 6 bits
       unsigned short th_win;          // Window: 16 bits
       unsigned short th_sum;          // Checksum: 16 bits
       unsigned short th_urp;          // Urgent Pointer: 16 bits
    }AD_TCPHEADER;// UDP Header
    typedef struct
    {
    unsigned short uh_sport;       // Source port:16 bits
    unsigned short uh_dport;       // Destination port:16bits
    unsigned short uh_len;         // Data length:16 bits
    unsigned short uh_chksum;      // Checksum:16 bits
    }AD_UDPHEADER;union AD_IPADDRESS
    {
    struct { unsigned char b1, b2, b3, b4; } b;
    DWORD w;
    };typedef struct _tagAD_IPPACKDATA
    {
    AD_IPADDRESS   ipHost;
    char          *pAllData;
    int            nAllLen;
    char           chSrcIP[16];
    AD_IPADDRESS   ipSrc;
    char           chDstIP[16];
    AD_IPADDRESS   ipDst;
    int            nProto;
    char           chProto[5];
    unsigned short nSrcPort;
    unsigned short nDstPort;
    char          *pData;
    int            nLen;
    _tagAD_IPPACKDATA()
    {
    memset(this, 0, sizeof(AD_IPPACKDATA));
    }
    }AD_IPPACKDATA;//////////////////////////////////////////////////////////////////////////
    template <class T>
    class CAdIpAnalyse
    {
    public:
    CAdIpAnalyse();
    ~CAdIpAnalyse();

    public:
    virtual BOOL Init(T *pObj, u_short usPort, DWORD dwData = 0, u_long *ulIP = NULL);
    virtual BOOL Close();
    virtual BOOL Start();
    virtual BOOL Stop();
    virtual BOOL IsStart();
    DWORD   GetIP();
    u_short GetPort();protected:
    static unsigned __stdcall _ThreadProc(void *lParam);
    virtual DWORD ThreadRun();protected:
    T      *m_pObj;
    u_short m_usPort;
    u_long  m_ulIP;
    DWORD   m_dwData;
    SOCKET  m_s;
        HANDLE  m_hThread;
    BOOL    m_bStop;
    BOOL    m_bInit;
    };//////////////////////////////////////////////////////////////////////////
    template <class T>
    CAdIpAnalyse<T>::CAdIpAnalyse()
    : m_pObj(NULL)
    {
    m_s = INVALID_SOCKET;
    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(2, 2);
    int nResult = ::WSAStartup(wVersionRequested, &wsaData);
    ASSERT(nResult == 0);
    m_hThread = NULL;
    m_bStop   = TRUE;
    m_usPort  = 60;
    m_ulIP    = 0x0100007f;
    m_dwData  = NULL;
    }template <class T>
    CAdIpAnalyse<T>::~CAdIpAnalyse()
    {
    Stop();
    Close();
    }template <class T>
    DWORD CAdIpAnalyse<T>::GetIP()
    {
    return m_ulIP;
    }template <class T>
    u_short CAdIpAnalyse<T>::GetPort()
    {
    return m_usPort;
    }template <class T>
    BOOL CAdIpAnalyse<T>::Init(T *pObj, u_short usPort, DWORD dwData, u_long *ulIP)
    {
    m_s = ::socket(AF_INET, SOCK_RAW, IPPROTO_IP);
    if(m_s == INVALID_SOCKET)
    return FALSE; int nRecvTimeOut = 3000;
    if(setsockopt(m_s, SOL_SOCKET, SO_RCVTIMEO, (const char *)&nRecvTimeOut, sizeof(nRecvTimeOut)) == SOCKET_ERROR)
    return FALSE; m_usPort = usPort;
    m_ulIP   = 0x0100007f;
    if(ulIP != NULL)
    {
    m_ulIP = *ulIP;
    }
    else
    {
    char chName[256];
    int nRet = ::gethostname(chName, 256);
    if(nRet != 0)
    return FALSE;

    hostent *pEnt = ::gethostbyname(chName);
    if(pEnt == NULL)
    return FALSE;

    m_ulIP = *((u_long *)pEnt->h_addr_list[0]);
    }    SOCKADDR_IN sa;
    sa.sin_family      = AF_INET;
    sa.sin_port        = htons(m_usPort);
    sa.sin_addr.s_addr = m_ulIP;
    if(bind(m_s, (PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR)
    return FALSE; DWORD dwBufferLen[10];
    DWORD dwBufferInLen = 1;
    DWORD dwBytesReturned = 0;

    if(::WSAIoctl(m_s, SIO_RCVALL, &dwBufferInLen, sizeof(dwBufferInLen), &dwBufferLen, 
    sizeof(dwBufferLen), &dwBytesReturned, NULL, NULL) == SOCKET_ERROR)
    return FALSE; m_pObj   = pObj;
    m_dwData = dwData;
    m_bInit  = TRUE; return TRUE;
    }template <class T>
    BOOL CAdIpAnalyse<T>::Close()
    {
    if(m_s != INVALID_SOCKET)
    {
    ::closesocket(m_s);
    m_s = INVALID_SOCKET;
    }
    m_bInit = FALSE;
    return TRUE;
    }template <class T>
    BOOL CAdIpAnalyse<T>::Start()
    {
    if(m_hThread != NULL)
    return FALSE; unsigned nThreadId = 0;
    m_hThread = (HANDLE)_beginthreadex(NULL, 0, CAdIpAnalyse::_ThreadProc, this, 0, &nThreadId);
    m_bStop = (m_hThread == NULL);
    return TRUE;
    }
    template <class T>
    BOOL CAdIpAnalyse<T>::Stop()
    {
    if(m_hThread == NULL)
    return TRUE; m_bStop = TRUE;
    WaitForSingleObject(m_hThread, INFINITE);
    m_hThread = NULL; return TRUE;
    }template <class T>
    BOOL CAdIpAnalyse<T>::IsStart()
    {
    return !m_bStop;
    }template <class T>
    unsigned __stdcall CAdIpAnalyse<T>::_ThreadProc(void *lParam)
    {
        CoInitialize(NULL);
      CAdIpAnalyse *pThis = static_cast<CAdIpAnalyse *>(lParam); DWORD dwRet = pThis->ThreadRun(); CoUninitialize();
    _endthreadex(dwRet);
    return 0;
    }template <class T>
    DWORD CAdIpAnalyse<T>::ThreadRun()
    {
    const int nBufSize = 4096;
    char *pBuf = new char[nBufSize];
    in_addr ia;
    while(!m_bStop)
    {
    memset(pBuf, 0, nBufSize);
    int nRet = ::recv(m_s, pBuf, nBufSize, 0);
    if(nRet <= 0)
    continue ; if(m_bStop)
    break ;
    char *pTempBuffer = pBuf;
    AD_IPHEADER *pIpHeader = (AD_IPHEADER *)pTempBuffer;
    int nLen = ntohs(pIpHeader->ih_totallen); AD_IPPACKDATA ipd; ipd.ipHost.w = m_ulIP;
    ipd.pAllData = pBuf;
    ipd.nAllLen  = nLen; ia.S_un.S_addr = pIpHeader->ih_sIP;
    char *chSrc = ::inet_ntoa(ia);
    strcpy(ipd.chSrcIP, chSrc);
    ipd.ipSrc.w = pIpHeader->ih_sIP;
    ia.S_un.S_addr = pIpHeader->ih_dIP;
    char *chDst = ::inet_ntoa(ia);
    strcpy(ipd.chDstIP, chDst);
    ipd.ipDst.w = pIpHeader->ih_dIP;
    ipd.nProto = pIpHeader->ih_proto;
    strcpy(ipd.chProto, AdMapProto2Str(ipd.nProto));

    switch(ipd.nProto)
    {
    case IPPROTO_TCP:
    {
    AD_TCPHEADER *pTCP = (AD_TCPHEADER *)(pTempBuffer + sizeof(AD_IPHEADER));
    ipd.nSrcPort = ::htons(pTCP->th_sport);
    ipd.nDstPort = ::htons(pTCP->th_dport);
    ipd.pData = pTempBuffer + sizeof(AD_IPHEADER) + sizeof(AD_TCPHEADER);
    ipd.nLen = nLen - sizeof(AD_IPHEADER) - sizeof(AD_TCPHEADER);
    }
    break ;
    case IPPROTO_UDP:
    {
    AD_UDPHEADER *pUDP = (AD_UDPHEADER *)(pTempBuffer + sizeof(AD_IPHEADER));
    ipd.nSrcPort = ::htons(pUDP->uh_sport);
    ipd.nDstPort = ::htons(pUDP->uh_dport);
    ipd.pData = pTempBuffer + sizeof(AD_IPHEADER) + sizeof(AD_UDPHEADER);
    ipd.nLen = nLen - sizeof(AD_IPHEADER) - sizeof(AD_UDPHEADER);
    }
    break ;
    } if(m_pObj != NULL)
    {
    m_pObj->IPPackDataCallBack(&ipd, m_dwData);
    }
    } delete []pBuf;

    return 0;
    }#endif // __ADIPANALYSE_H__