设 为 首 页
加 入 收 藏
联 系 我 们
推荐栏目 | Sniffer | Ethereal | IPV6 | IPTV | MPLS | TCP/IP | SNMP | WLAN | 中文RFC文档 | 编码交流 |
   

您现在的位置: 首页>>协议分析>>协议分析技术>>Winpcap>>正文


Winpcap学习笔记(2)

   今天在阅读Winpcap Manual的时候发现一句话:

       “This means that on shared media (like non-switched Ethernet), WinPcap will be able to capture the packets of other hosts.”

   我理解为:如果在通过没有交换功能的集线器连接的网络上,只要把网卡设置为混杂(promiscuous)模式,winpcap能够捕获到其他主机通信的数据包。如果是具有交换功能的集线器连接的网络winpcap还能管用吗?这个在后边的实习中将会进行试验。

    试验程序2: 

/*
* 截获数据包的试验。先打印出所有网络适配器的列表,然后选择
* 想在哪个适配器上截获数据包。然后通过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);
}

 

函数1:

pcap_t *pcap_open(const char *               source,

int                              snaplen,

int                              flags,

本新闻共2页,当前在第1页  1  2  

 


上篇文章:

Winpcap学习笔记(1)

 
下篇文章:

WinpCap学习笔记(3)

版权所有 中国协议分析网 联系信箱:wayky#126.com(请把"#"改成"@")
Copyright (C) www.Cnpaf.Net 2004-2008  All Rights Reserved. [京ICP备05002225号]