如何分配内存发送使用WinPcap的高性能大型PCAP文件(大小大于可用内存)? [英] How to allocate a memory to send a large pcap file (of size larger than available memory) with high performance using winpcap?

查看:2014
本文介绍了如何分配内存发送使用WinPcap的高性能大型PCAP文件(大小大于可用内存)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了code从WinPcap的例子,从的此链接

它工作正常送小PCAP文件,但如果我试图发送大量PCAP文件(超过可用内存大小说2 GB),这将肯定失败。这code是用来在内存中分配的文件大小,以便稍后发送

  caplen = FTELL(capfile) - 的sizeof(结构pcap_file_header);
...
/ *分配一个发送队列* /
SQUEUE = pcap_sendqueue_alloc(caplen);

问题是如何使这项工作对于大文件(GB或超过可用于分配的最大内存空间大)?我应该只分配100例如兆和发送队列,然后采取下一步100Mb的?如果有什么是正确的缓冲区大小?而如何做到这一点?

在这里一个重要的问题是发送此文件,它需要被作为CAN(我发送的视频包)作为做得风生水起的性能。

在短期如何管理这里的内存来实现这一目标?

有没有人有一个妥善的解决办法或建议?


从片段例如code(不必要code为这个问题所取代......)

  ...
#包括LT&;&pcap.h GT;
#包括LT&;远程ext.h>...无效的主要(INT ARGC,字符** argv的)
{
pcap_t * indesc,* outdesc;
...
FILE * capfile;
INT caplen,同步;
...
pcap_send_queue * SQUEUE;
结构pcap_pkthdr * pktheader;
u_char * pktdata;
.../ *检索捕获文件*长度/
capfile =的fopen(的argv [1],RB);
如果(!capfile){
    的printf(捕捉找不到文件\\ n!);
    返回;
}fseek的(capfile,0,SEEK_END);
caplen = FTELL(capfile) - 的sizeof(结构pcap_file_header);
FCLOSE(capfile);....../ *打开捕获文件* /
如果((indesc = pcap_open(来源65536,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf))== NULL)
{
    fprintf中(标准错误,\\ n无法打开文件%s \\ n,源);
    返回;
}/ *打开输出适配器* /
如果((outdesc = pcap_open(的argv [2],100,PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf))== NULL)
{
    fprintf中(标准错误,\\ n无法打开适配器%S \\ n,源);
    返回;
}.../ *分配一个发送队列* /
SQUEUE = pcap_sendqueue_alloc(caplen);/ *从文件*填写队列中的报文/
而((RES = pcap_next_ex(indesc,&安培; pktheader,&安培; pktdata))== 1)
{
    如果(pcap_sendqueue_queue(SQUEUE,pktheader,pktdata)== -1)
    {
        的printf(警告:数据包缓冲区太小,不是所有的数据包将被发送\\ n);
        打破;
    }    npacks ++;
}如果(RES == -1)
{
    的printf(损坏输入文件\\ n);
    pcap_sendqueue_destroy(SQUEUE);
    返回;
}/ *发送队列* /如果((RES = pcap_sendqueue_transmit(outdesc,SQUEUE,同步))< squeue-> LEN)
{
    的printf(发生错误发送数据包:%s的只有%d个字节发送\\ n,pcap_geterr(outdesc),RES);
}
/ *免费发送队列* /
pcap_sendqueue_destroy(SQUEUE);/ *关闭文件* /
pcap_close(indesc);/ *
 *失去了输出适配器
 *重要:记得关闭适配器,否则会出现不能保证所有的
 *数据包将被发送!
 * /
pcap_close(outdesc);
返回;
}


解决方案

刚刚封顶它在说50 MB(这是一个有点武断,但我认为合理的起点),增强code,警告时的位不是所有的分组装配在队列以便它只是发送它所具有迄今并开始填充从开始队列与剩余的分组。

I have used the code from winpcap example to send pcap file(original code from winpcap documenation found at this link)

It works fine to send a small pcap files but if I tried to send a large pcap file (larger than available memory size say 2 Gb) it will fail for sure. This code is used to allocate size of the file in memory in order to send it later

caplen= ftell(capfile)- sizeof(struct pcap_file_header);
...
/* Allocate a send queue */
squeue = pcap_sendqueue_alloc(caplen);

The question is how to make this work for large files(in Gb or larger than maximum memory space available for allocation)? Should I allocate only 100 Mb for example and send the queue and then take the next 100Mb? If yes what is the proper buffer size? And how to do this?

an important issue here is the performance of sending this file which needed to be done as fast as can (i am sending a video packets).

In short how to manage the memory here to achieve this goal?

Would anyone have a proper solution or suggestion?


Snippet from example code (unnecessary code for this question replaced by ... )

...
#include <pcap.h>
#include <remote-ext.h>

...

void main(int argc, char **argv)
{
pcap_t *indesc,*outdesc;
...
FILE *capfile;
int caplen, sync;
...
pcap_send_queue *squeue;
struct pcap_pkthdr *pktheader;
u_char *pktdata;
...

/* Retrieve the length of the capture file */
capfile=fopen(argv[1],"rb");
if(!capfile){
    printf("Capture file not found!\n");
    return;
}

fseek(capfile , 0, SEEK_END);
caplen= ftell(capfile)- sizeof(struct pcap_file_header);
fclose(capfile);

...

...

/* Open the capture file */
if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
{
    fprintf(stderr,"\nUnable to open the file %s.\n", source);
    return;
}

/* Open the output adapter */
if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
{
    fprintf(stderr,"\nUnable to open adapter %s.\n", source);
    return;
}

...

/* Allocate a send queue */
squeue = pcap_sendqueue_alloc(caplen);

/* Fill the queue with the packets from the file */
while ((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1)
{
    if (pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1)
    {
        printf("Warning: packet buffer too small, not all the packets will be sent.\n");
        break;
    }

    npacks++;
}

if (res == -1)
{
    printf("Corrupted input file.\n");
    pcap_sendqueue_destroy(squeue);
    return;
}

/* Transmit the queue */



if ((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) < squeue->len)
{
    printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res);
}


/* free the send queue */
pcap_sendqueue_destroy(squeue);

/* Close the input file */
pcap_close(indesc);

/* 
 * lose the output adapter 
 * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the 
 * packets will be sent!
 */
pcap_close(outdesc);


return;
}

解决方案

Just cap it at say 50 MB (this is a somewhat arbitrary but I think reasonable starting point), and enhance the bit of code that warns when not all the packets fit in the queue so that it just sends what it has so far and starts filling the queue from the beginning with the remaining packets.

这篇关于如何分配内存发送使用WinPcap的高性能大型PCAP文件(大小大于可用内存)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆