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

[英]copy libpcap packet data from const u_char* to another const u_char*


I am trying to make a deep copy of const u_char* to another const u_char* in c/++.

我正在尝试将const u_char*复制到另一个const u_char* in c/++中。

I have the following code -

我有下面的代码。

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);
    }
}

But I dont think that it is getting copied right, coz the result of operations on the 2 copies are different.

但是我不认为它是被复制的对,因为两个拷贝的操作结果是不同的。

The data has to be in const u_char* form as dictated by pcap loop method signature.

数据必须在const u_char*表单中,由pcap loop方法签名决定。

I am testing with a larger sample of the type [obtained using tcpdump], where each packet is read sequentially and I receive a const u_char* pointer to it -

我正在测试一个更大的类型的样本(使用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

What I am trying to do is load all the packets from the pcap file into a memory buffer to go high speed testing which can be slow if each packet is read from the file.

我要做的是将所有的数据包从pcap文件装入一个内存缓冲区,以进行高速测试,如果从文件中读取每个数据包,就会很慢。

Any pointers will be really appreciated.

任何指示都将非常感谢。

1 个解决方案

#1


0  

I was able to figure out my problem. The length was not right. I should have used the length in the packet header. Either way, I feel using strlen is not the right choice for unsigned char.

我能解出我的问题。长度不对。我应该在包标题中使用长度。不管怎样,我觉得使用strlen并不是无符号字符的正确选择。

I figured it out using another post - vector of packets in libpcap

我在libpcap中使用另一个post - vector包找到了它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/12/29/d07dccf49493c17b3062056beb70a249.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com