将来自const u_char *的libpcap数据包数据复制到另一个const u_char *

时间:2015-12-29 08:00:14

标签: c++ c pointers memcpy libpcap

我正在尝试将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份副本上的操作结果是不同的。

数据必须是const u_char *形式,由pcap循环方法签名决定。

我正在测试更大的[使用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文件中的所有数据包加载到内存缓冲区中进行高速测试,如果从文件中读取每个数据包,这可能会很慢。

任何指针都会非常感激。

1 个答案:

答案 0 :(得分:0)

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

我使用其他帖子vector of packets in libpcap

来计算出来