|
因本人不再做技术,
这个blog将不再连载技术文章,
只作为心情点滴的记录,
想学技术的请绕道,谢谢!
联系方式:
feiyu_lili@163.com |
时 间 记 忆 |
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
blog名称:飞鱼的成长 日志总数:120 评论数量:488 留言数量:18 访问次数:1044961 建立时间:2006年2月27日 |
 | | | |
|
|
源代码
/** 截获数据包的试验。先打印出所有网络适配器的列表,然后选择* 想在哪个适配器上截获数据包。然后通过pcap_loop()函数将截获* 的数据包传给回调函数packet_handler()处理。
* 通过该程序初步了解了使用winpcap截获数据包的步骤以及一些在* 截获数据包时非常重要的函数和结构体。* 2006-1-26*/
#include <pcap.h>#include <remote-ext.h>
/* Prototype of the packet handler */void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main() { pcap_if_t *alldevs; pcap_if_t *d; int inum; int i = 0; pcap_t *adhandle; char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the devices list on the local machine */ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) { fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf); exit(1); }
/* Print the list */ for (d = alldevs; d; d = d->next) { /* Print name */ printf("%d. %s", ++ i, d->name);
/* Print description */ 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; }
/* Select an adapter */ 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 device */ if ((adhandle = pcap_open(d->name, /* name of the device */ 65536, /* portion of the packet to capture */ /* 65535 guarantees that the whole packet will be captured on all the link layers */ PCAP_OPENFLAG_PROMISCUOUS, /* promiscuous mode */ 1000, /* read timeout */ NULL, /* authentication on the remote machine */ errbuf /* error buffer */ )) == NULL) { fprintf(stderr, "\nnable to open the adapter. %s is not supported by Winpcap\n", d->name); /* Free the devices list */ pcap_freealldevs(alldevs);
return -1; }
printf("\nlistening on %s...\n", d->description);
/* At this point, we don't need any more the device list. Free it */ pcap_freealldevs(alldevs); /* start the capture */ pcap_loop(adhandle, 0, packet_handler, NULL);
return 1;}
/* Callback function invoked by libpcap for every incoming packet */void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) { struct tm *ltime; char timestr[16];
/* convert the timestamp to readable format */ ltime = localtime(&header->ts.tv_sec); strftime(timestr, sizeof(timestr), "%H:%M:%S", ltime); printf("%s, %.6d len:%d\n", timestr, header->ts.tv_usec, header->len);}
在运行第二天的代码的时候我发现下面的问题
500)this.width=500'>
一, 对应最下面的1这个是“printf("%s, %.6d len:%d\n", timestr, header->ts.tv_usec, header->len);”里面的header->ts.tv_usec的header是哪里来的,“void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)”中的“const struct pcap_pkthdr *header”但是pcap_pkthdr结构体中是没有header的阿?更何况是对象ts.tv_usec呢?
类似的header->len是来自哪里的阿?
二, 三: 对应2我只有一张网卡,怎么会有两个显示阿,其中的一是什么东西啊 ?
麻烦告诉我哦~
|
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
[ 2009/4/1 17:27:35 | By: ADFS(游客) ] |
空调维修 空调安装 空调移机 空调加氟 空调清洗 空调保养 空调修理 英语翻译 韩语翻译 婚纱摄影 秦皇岛婚纱摄影 摄影工作室 婚纱摄影工作室 海边摄影工作室 秦皇岛摄影工作室 秦皇岛海边摄影 北戴河婚纱摄影 婚纱外景 海边婚纱摄影 光盘印刷 光盘刻录 光盘复制 空调加氟 空调维修 空调移机 阀门 燃气阀 排气阀 蓄冰 节能空调 |
|
|
祝大家愚人节快乐! |
[ 2007/3/31 16:53:42 | By: 游子(游客) ] |
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
|
以下引用高山(游客)在2006-6-9 1:34:08的评论:
这两天一直忙这些东西,我可以告诉你第一个问题的答案。
A1:10:01:57.519816 指的是10点01分57.519816秒。帧到达的时间为:时:分:秒:分数秒。其中519816的值时0.549816秒(s),用毫秒表示则是549.816ms。许多网络问题都与很慢的响应时间有关,因而准确的时间非常重要。在观察网络数据流时,保持定时时很关键的。虽然是很久以前的问题了,不过还是学到东西了,谢拉 |
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
[ 2006/6/9 1:34:08 | By: 高山(游客) ] |
这两天一直忙这些东西,我可以告诉你第一个问题的答案。
A1:10:01:57.519816 指的是10点01分57.519816秒。帧到达的时间为:时:分:秒:分数秒。其中519816的值时0.549816秒(s),用毫秒表示则是549.816ms。许多网络问题都与很慢的响应时间有关,因而准确的时间非常重要。在观察网络数据流时,保持定时时很关键的。 |
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
|
以下引用binaryluo在2006-4-18 9:14:07的评论:
以下引用feiyu_lili在2006-4-13 11:31:17的评论:
这个我也有问题,不过这里不能贴图,我放在自己的blog上了,麻烦帮我解答一下
第二天的问题,请指教
地址:http://blogger.org.cn/blog/more.asp?name=feiyu_lili&id=13428A1:那是ts.tv_usec,具体的这个字段是什么意思你可以到SDK的文档中查下timeval这个结构体的说明。A2:有可能是modem,或者是什么软件虚拟出来的一个网络适配器……都有可能。A3:要么就是程序有问题;如果程序没有问题的话就是它在等待截获数据包,然而当前网络上没有数据包,所以它处于等待状态。原网址:http://blogger.org.cn/blog/more.asp?name=binaryluo&id=11459 |
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
|
以下引用bladliu在2006-4-13 15:04:12的评论:
void *packet_handler(...,*pcap_pkthdr header);
你翻翻winpcap的 manul,里面有pkthdr数据结构定义。里面有个len字段,本数据包的长度len是有了,那ts.tv_usec呢? |
|
|
回复:Binaryluo 的 [网络编程技术]Winpcap学习:第二天(20060126) 的问题 |
|
void *packet_handler(...,*pcap_pkthdr header);
你翻翻winpcap的 manul,里面有pkthdr数据结构定义。里面有个len字段,本数据包的长度 |
| » 1 »
| | |
|