packet32 -- 打印packet32捕获的包

void PrintPackets(LPPACKET lpPacket)
{

    ULONG    i, j, ulLines, ulen, ulBytesReceived;
    char    *pChar, *pLine, *base;
    char    *buf;
    u_int off=0;
    u_int tlen,tlen1;
    struct bpf_hdr *hdr;

    ulBytesReceived = lpPacket->ulBytesReceived;


    buf = lpPacket->Buffer;

    off=0;

    while(off<ulBytesReceived){    
        if(_kbhit())return;
        hdr=(struct bpf_hdr *)(buf+off);
        tlen1=hdr->bh_datalen;
        tlen=hdr->bh_caplen;
        printf("Packet length, captured portion: %ld, %ld\n", tlen1, tlen);
        off+=hdr->bh_hdrlen;

        ulLines = (tlen + 15) / 16;

        pChar =(char*)(buf+off);
        base=pChar;
        off=Packet_WORDALIGN(off+tlen);

        //if(ulLines >10) ulLines = 10;

        for ( i=0; i<ulLines; i++ )
        {

            pLine =pChar;

            printf( "%08lx : ", pChar-base );

            ulen=tlen;
            ulen = ( ulen > 16 ) ? 16 : ulen;
            tlen -= ulen;

            for ( j=0; j<ulen; j++ )
                printf( "%02x ", *(BYTE *)pChar++ );

            if ( ulen < 16 )
                printf( "%*s", (16-ulen)*3, " " );

            pChar = pLine;

            for ( j=0; j<ulen; j++, pChar++ )
                printf( "%c", isprint( (unsigned char)*pChar ) ? *pChar : '.' );

            printf( "\n" );
        } 

        printf( "\n" );
    }
} 

 

posted @ 2017-08-25 17:03  嘿.嘿.嘿  阅读(339)  评论(0编辑  收藏  举报