新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 讨论密码学、密码协议、入侵检测、访问控制等与安全理论研究有关的主题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机理论与工程『 安全理论 』 → [原创]Winpcap学习第三天 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 13731 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: [原创]Winpcap学习第三天 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     binaryluo 帅哥哟,离线,有人找我吗?
      
      
      威望:6
      等级:研二(Pi-Calculus看得一头雾水)(版主)
      文章:679
      积分:5543
      门派:IEEE.ORG.CN
      注册:2005/2/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给binaryluo发送一个短消息 把binaryluo加入好友 查看binaryluo的个人资料 搜索binaryluo在『 安全理论 』的所有贴子 引用回复这个贴子 回复这个贴子 查看binaryluo的博客楼主
    发贴心情 [原创]Winpcap学习第三天

    今天的试验程序与前天的功能是一样的,只是在捕获数据包的时候前天的程序用的是pcap_loop(),今天的代码用的是pcap_next_ex()。基于pcap_loop()抓包机制的回调很方便而且在某些情况下是一个不错的选择。但是,处理回调有些时候不适用——它使得程序更复杂,尤其是在应用程序与多线程或C++类有关的情况下。而pcap_next_ex()有的时候用起来更加方便。

        试验代码3:

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

    int main() {
    pcap_if_t* alldevs;
    pcap_if_t* d;
    int inum;
    int i = 0;
    pcap_t* adhandle;
    int res;
    char errbuf[PCAP_ERRBUF_SIZE];
    struct tm* ltime;
    char timestr[16];
    struct pcap_pkthdr* header;
    u_char* pkt_data;

    /* Retrieve the device 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)
    {
      printf("%d. %s", ++ 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;
    }

    /* 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 adpater */
    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 */
             /* 65536 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, "\nUnable 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);

    /* Retrieve the packets */
    while ((res = pcap_next_ex(adhandle, &header, &pkt_data)) >= 0)
    {
      if (res == 0)
      {
       /* Timeout elapsed */
       continue;
      }
      /* 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);
    }

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

    return 1;
    }

    函数1:

    pcap_next_ex(pcap_t*                       p,

                          struct pcap_pkthdr**   pkt_header,

                          const u_char*             pkt_data

    )

        从一个网络接口或离线捕获方式(例如读文件)读取一个数据包。该函数被用来重新获得下一个可用的数据包,没有使用libpcap提供的传统的回调方法。pcap_next_ex用指向头和下一个被捕获的数据包的指针为pkt_header和pkt_data参数赋值。

    返回值有下列几种情况:

    1,数据包被正确读取

    0,pcap_open_live()设置的超时时间到。在这种情况下pkt_header和pkt_data不指向有效数据包

    -1,发生错误

    -2,离线捕获的时候读取到EOF

        我们通常使用pcap_next_ex()而不是pcap_next(),因为pcap_next()有些缺点。首先,pcap_next()效率低,因为它隐藏了回调方法但是还是依赖于pcap_dispatch;第二,它不能检测EOF,所以当从一个文件获取数据包时它不是很有用。

    函数2:

    u_char* pcap_next(pcap_t*                      p,

    struct pcap_pkthdr*     h

    )

        返回下一个可用的数据包并且返回一个u_char指向该数据包数据部分的指针。如果发生错误或者活动的抓包没有读取到数据包(例如:数据包不能通过包过滤器而被丢弃,或者在支持读超时(read timeout)的平台上在任何数据包到来之前就超时终止,又或者是抓包设备的文件描述符在非阻塞(non-blocking)模式下并且没有数据包可以被读取),或者文件已被读完时返回NULL。不幸的是,没有办法检测是否发生错误。


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/6/1 9:30:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 安全理论 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/24 17:15:06

    本主题贴数4,分页: [1]

     *树形目录 (最近20个回帖) 顶端 
    主题:  [原创]Winpcap学习第三天(4153字) - binaryluo,2006年6月1日
        回复:  pcap_next_ex(adhandle, &header, &pkt_data))这里编译不..(66字) - waxic,2006年6月12日
            回复:  [quote][b]以下是引用[i]waxic在2006-6-12 11:18:00[/i]的发言..(159字) - binaryluo,2006年6月12日
                回复:  显示的错误是这样的:--------------------Configuration: wi..(600字) - picop,2006年12月21日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    7,050.781ms