将 libpcap 包数据从 const u_char* 复制到另一个 const u_char*

copy libpcap packet data from const u_char* to another const u_char*

我正在尝试将 const u_char* 深拷贝到 c/++ 中的另一个 const u_char*。

我有以下代码 -

void packetHandler(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char* packet) 
{
    if(packet!=NULL)
    {
        int length = strlen((char*)packet);
        const u_char* newPacket = new u_char[length];
        memcpy((u_char*)newPacket, packet, length);
        cout<<"Packet - "<<packet<<endl;
        cout<<"New Packet - "<<newPacket<<endl;
        packets.push_back(newPacket);
        //processPacket(packet);
    }
}

但我认为它没有被正确复制,因为对 2 个副本的操作结果不同。

数据必须采用 pcap 循环方法签名规定的 const u_char* 形式。

我正在测试 [使用 tcpdump 获得] 类型的更大样本,其中每个数据包都是按顺序读取的,我收到一个指向它的 const u_char* 指针 -

Ôò¡          ÿÿ     …®xVÅ  H   H   RT 5 'Vc E  :c@ @Z˜
 À¨ìq 5 &Îïæ9        helloBelkin   …®xVcÆ  H   H   RT 5 'Vc E  :d@ @Z—
 À¨=š 5 &Îï%ý        helloBelkin   …®xVž} h   h    'VcRT 5 E  ZJ6  @a¥À¨
  5ìq FÀ6æ9€      helloBelkin   À     P ÆiôÀ     P Æiþ…®xV«ü H   H    'VcRT 5 E  :J7  @aÄÀ¨
  5=š &/#%ý€       helloBelkin   …®xV.ÿ J   J   RT 5 'Vc E  <ÿ‚@ @tµ
 ÆiôÕ PÚPq)     rƲ  ´
 Ž«    …®xVÒ
 J   J   RT 5 'Vc E  <Ä'@ @°
 ÆiôÕ P–´JÎ     rƲ  ´
 Žé    …®xVBg J   J   RT 5 'Vc E  <@ @ÒÛ
 6ïÕ¼- Pùu€ý     rZ  ´
 Žó    …®xVæi J   J   RT 5 'Vc E  <ÅX@ @‘
 6ïÕ¼. P€µK«     rZ  ´
 Žó    …®xVOo J   J   RT 5 'Vc E  <O@ @Ýš
 6ïÕ¼/ P õ¼     rZ  ´
 Žó    …®xV} ½  ½  RT 5 'Vc E ¯gB@ @p‰
 6ï€¨× PDMyå ¯SúPÿÿc  GET /uedata/nvp/unsticky/191-8029364-1340752/Gateway/ntpoffrw?ul&v=0.189.0&id=1S5BSQ8X89KSY51N9J21&m=1&sc=1S5BSQ8X89KSY51N9J21&ue=43&ns=154&af=266&cf=266&ne=290&be=1635&pc=79838&tc=-331&na_=-331&ul_=-72&_ul=-69&rd_=-1450749493906&_rd=-1450749493906&fe_=-321&lk_=-321&_lk=-321&co_=-321&_co=-321&rq_=-303&rs_=-73&_rs=681&dl_=-72&di_=1638&de_=1728&_de=1790&_dc=-1450749493906&ld_=-1450749493906&_ld=-1450749493906&ntd=-1&ty=0&rc=0&hob=42&hoe=43&ul=79839&t=1450749573745&ctb=1&csmtags=aui|aui:aui_build_date:3.15.11.2-2015-12-07|gwCFImgCache|fls-na|iss-on-time|aui:ajax&viz=visible:43&pty=Gateway&spty=desktop&pti=desktop&tid=1CRMQKHG3GSKG0WDK809&aftb=1&ui=2 HTTP/1.1
Host: www.amazon.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.amazon.com/
Cookie: skin=noskin; x-wl-uid=1vOeLJIaacYseg7dzznzr+FooV74I+GQLqTAS6uFsLJp5FSidiwiAV3ZsaLsOqphLaudQoD/mgJw=; session-id-time=2082787201l; session-id=191-8029364-1340752; ubid-main=187-1501615-8388412; session-token=Ak43AtGsZnoAOQQb+XIkTS4a549quhOhBlX1ts3xyzgs8iWj/uszLKnxvWgDp+UyqMHU8O4d0H9jTaMm+8pCNXH4wwHbtHD0GifwVGrOzBDR1/z4SWS6KjHXrH7kQqxAc45oXpmIyVN/z8n8heYWSbE+2TuReYT3rFTtTixrGR2cheyT9dAXsXkruQwcywgrtPp+kx+JzSLBFp2+tgSN66c+ZxSdl/PdbnQvY58fXJrYyJJoIN2LdoRhEBcwPaPy
Connection: keep-alive

…®xVȃ <   <    'VcRT 5 E  (J8  @Ó6ï€
  P¨× ¯SúDMlPÿÿpÌ        …®xVŽz <   <    'VcRT 5 E  ,J9  @ÖÀ6ïÕ
  P¼-!­ùu€þ`ÿÿÍ£  ´  …®xV¯z 6   6   RT 5 'Vc E  (@ @Òî
 6ïÕ¼- Pùu€þ!­PrYí  …®xV®{ ê  ê  RT 5 'Vc E Ü@ @Í9
 6ïÕ¼- Pùu€þ!­Pr_¡  GET /1/batch/1/OP/ATVPDKIKX0DER:191-8029364-1340752:1S5BSQ8X89KSY51N9J21$uedata=s:%2Fuedata%2Fnvp%2Funsticky%2F191-8029364-1340752%2FGateway%2Fntpoffrw%3Ful%26v%3D0.189.0%26id%3D1S5BSQ8X89KSY51N9J21%26m%3D1%26sc%3D1S5BSQ8X89KSY51N9J21%26ue%3D43%26ns%3D154%26af%3D266%26cf%3D266%26ne%3D290%26be%3D1635%26pc%3D79838%26tc%3D-331%26na_%3D-331%26ul_%3D-72%26_ul%3D-69%26rd_%3D-1450749493906%26_rd%3D-1450749493906%26fe_%3D-321%26lk_%3D-321%26_lk%3D-321%26co_%3D-321%26_co%3D-321%26rq_%3D-303%26rs_%3D-73%26_rs%3D681%26dl_%3D-72%26di_%3D1638%26de_%3D1728%26_de%3D1790%26_dc%3D-1450749493906%26ld_%3D-1450749493906%26_ld%3D-1450749493906%26ntd%3D-1%26ty%3D0%26rc%3D0%26hob%3D42%26hoe%3D43%26ul%3D79839%26t%3D1450749573745%26ctb%3D1%26csmtags%3Daui%7Caui%3Aaui_build_date%3A3.15.11.2-2015-12-07%7CgwCFImgCache%7Cfls-na%7Ciss-on-time%7Caui%3Aajax%26viz%3Dvisible%3A43%26pty%3DGateway%26spty%3Ddesktop%26pti%3Ddesktop%26tid%3D1CRMQKHG3GSKG0WDK809%26aftb%3D1%26ui%3D2:79840 HTTP/1.1
Host: fls-na.amazon.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.amazon.com/
Cookie: skin=noskin; x-wl-uid=1vOeLJIaacYseg7dzznzr+FooV74I+GQLqTAS6uFsLJp5FSidiwiAV3ZsaLsOqphLaudQoD/mgJw=; session-id-time=2082787201l; session-id=191-8029364-1340752; ubid-main=187-1501615-8388412; session-token=Ak43AtGsZnoAOQQb+X…®xVò{ @  @  RT 5 'Vc E 2@ @Ñâ
 6ïÕ¼- Pùu†²!­PrZ÷  IkTS4a549quhOhBlX1ts3xyzgs8iWj/uszLKnxvWgDp+UyqMHU8O4d0H9jTaMm+8pCNXH4wwHbtHD0GifwVGrOzBDR1/z4SWS6KjHXrH7kQqxAc45oXpmIyVN/z8n8heYWSbE+2TuReYT3rFTtTixrGR2cheyT9dAXsXkruQwcywgrtPp+kx+JzSLBFp2+tgSN66c+ZxSdl/PdbnQvY58fXJrYyJJoIN2LdoRhEBcwPaPy
Connection: keep-alive

我想做的是将 pcap 文件中的所有数据包加载到内存缓冲区中以进行高速测试,如果从文件中读取每个数据包,速度可能会很慢。

任何指点将不胜感激。

我能够找出我的问题。长度不对。我应该使用数据包中的长度 header。无论哪种方式,我觉得使用 strlen 都不是 unsigned char 的正确选择。

我用另一个 post - vector of packets in libpcap

弄明白了