标题:请大家帮我看看程序
取消只看楼主
vencent930
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-11-30
 问题点数:0 回复次数:0 
请大家帮我看看程序

我是在Winpcap基础上编写了一个数据包捕获的程序,我的原意捕获后将它以文件的形式保存到我的硬盘里,可是无论捕获多长时间,保存下来的数据就2行,我是将保存的文件格式设置为.bat,如果设置成,txt,则保存下来的是几个乱码

大家帮我看看 谢谢了:)



#include <stdlib.h>
#include <stdio.h>

#include <pcap.h>

#define LINE_LEN 16


int main(int argc, char **argv)
{
pcap_if_t *alldevs, *d;
pcap_t *fp;
u_int inum, i=0;
pcap_dumper_t *dumpfile;
char errbuf[PCAP_ERRBUF_SIZE];
int res;
struct pcap_pkthdr *header;
const u_char *pkt_data;

if(argc < 3)
{
printf("\nNo adapter selected: printing the device list:\n");
/* The user didn't provide a packet source: Retrieve the local device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}

/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s\n ", ++i, d->name);

if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}

if (i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}


printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);


if (inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");

/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* Jump to the selected adapter */
for (d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* Open the adapter */
if ((fp = pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nError opening adapter\n");
return -1;
}
}
else
{
/* Do not check for the switch type ('-s') */
if ((fp = pcap_open_live(argv[2], // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nError opening adapter\n");
return -1;
}
}
fprintf(stderr,"\nError opening output file\n");

//设置保存数据包的文件为"Packet.dat"
dumpfile=pcap_dump_open(adhandle, "Packet.dat");


/* Read the packets */
while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
{

if(res == 0)
/* Timeout elapsed */
continue;

/* print pkt timestamp and pkt len */
printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);

/* Print the packet */
for (i=1; (i < header->caplen + 1 ) ; i++)
{
printf("%.2x ", pkt_data[i-1]);
if ( (i % LINE_LEN) == 0) printf("\n");
}

printf("\n\n");
}

if(res == -1)
{
printf("Error reading the packets: %s\n", pcap_geterr(fp));
return -1;
}

pcap_close(fp);
return 0;

//将捕获的数据包保存到刚才建立的Packet.bat文件中
pcap_dump((unsigned char*)dumpfile,header,pkt_data);
}

搜索更多相关主题的帖子: include 数据包 
2005-12-09 20:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-37530-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.115872 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved