私はwinpcapの例のコードを使用してpcapファイルを送信しました(このリンクにあるwinpcapドキュメントの元のコード)
小さなpcapファイルを送信することは問題なく機能しますが、大きなpcapファイル(使用可能なメモリサイズよりも大きい、たとえば2 Gb)を送信しようとすると、確実に失敗します。このコードは、後で送信するためにメモリ内のファイルのサイズを割り当てるために使用されます
caplen= ftell(capfile)- sizeof(struct pcap_file_header);
...
/* Allocate a send queue */
squeue = pcap_sendqueue_alloc(caplen);
問題は、これを大きなファイル(Gbまたは割り当てに使用できる最大メモリスペースよりも大きい)に対してどのように機能させるかです。たとえば、100 Mbのみを割り当ててキューを送信し、次の100 Mbを取得する必要がありますか?はいの場合、適切なバッファサイズはどれくらいですか?そして、これを行う方法は?
ここでの重要な問題は、このファイルを送信するパフォーマンスです。これは、できるだけ速く実行する必要があります(私はビデオパケットを送信しています)。
要するに、この目標を達成するためにここでメモリを管理する方法は?
誰かが適切な解決策や提案を持っていますか?
サンプルコードからの抜粋(この質問の不要なコードは...に置き換えられました)
...
#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;
}