zoukankan      html  css  js  c++  java
  • WinPcap初学(2)


        还是按照“winpcap使用系列”http://www.smatrix.org/bbs/read.php?tid=359&fpage=4 一步步学习。先看文章里头包含的头文件。
    #include "pcap.h"
    #ifndef WIN32
    #include 
    <sys/socket.h>
    #include 
    <netinet/in.h>
    #else
    #include 
    <winsock.h>
    #endif
        由于是Win32平台的开发,#ifndef WIN32...#else那段都是没用的。真正要使用到的是#include<winsock.h>。首先声名一下,我做出来的测试程序是MFC的窗口程序。而MFC程序中的自动生成的stdafx.h文件中有这么一句 #include <afxwin.h// MFC 核心组件和标准组件,而在afxwin.h里头有就winsock2.h的声名。事实上winsock与winsock2是干同样活的,不过是新旧之分,所以假如你在程序中加上#include<winsock.h>的话就必然会出现函数同名的错误(winsock与winsock2很多同名的函数)。因此在我的程序中只须声名#include "pcap.h"。
        接着,看下代码。
    Code

        这些使用到一些控制台程序的函数,不过对于MFC程序没有大碍。先不说ifprint和iptos的作用,我们来看来pcap_if_t这个结构。
        在pcap.h中它有一个另名pcap_if,至于为什么要改名字我也不太清楚。(为了说明这个一个type?)在官方的文档中(...\WpdPack\docs\html\structpcap__if.html)对pcap_if进行如下定义:
    pcap_if next
      if not NULL, a pointer to the next element in the list; NULL for the last element of the list
    char *  name
      a pointer to a string giving a name for the device to pass to pcap_open_live()
    char *  description
      if not NULL, a pointer to a string giving a human-readable description of the device
    pcap_addr addresses
      a pointer to the first element of a list of addresses for the interface
    u_int  flags
      PCAP_IF_ interface flags. Currently the only possible flag is PCAP_IF_LOOPBACK, that is set if the interface is a loopback interface.

        next是对下一个设备驱动的指针,name是本驱动的名字(基本上是一些不知所云的数字),description是驱动的描述(如Realtek RTL8169/8110 Family Gigabit Ethernet NIC,这个程序员就比较清楚了),pcap_addr则是另一个pcap.h中定义的结构,最后的flags目前为0。 官方文档对pcap_addr定义如下:
    pcap_addr next
      if not NULL, a pointer to the next element in the list; NULL for the last element of the list
    sockaddr *  addr
      a pointer to a struct sockaddr containing an address
    sockaddr *  netmask
      if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address pointed to by addr.
    sockaddr *  broadaddr
      if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre­ sponding to the address pointed to by addr; may be null if the interface doesn't support broadcasts
    sockaddr *  dstaddr
      if not NULL, a pointer to a struct sockaddr that contains the destination address corre­ sponding to the address pointed to by addr; may be null if the interface isn't a point- to-point interface
        其中,addr为IP地址,netmask为子网掩码(以上两个都为平时常用的网络连接设置),broadaddr为广播地址(说明参照百度百科http://baike.baidu.com/view/473043.htm#3),dstaddr为目标地址(具体使用不太清楚,什么叫点对点接口?什么情况下会有这个接口?哪位高人给我指点一下!)。以上4部分都是sockaddr这个结构,它的定义在ws2def.h中。
    Code

        上面的0x0600是vista的版本号,就是说当系统为XP或以下的时候用u_short的定义(其实ADDRESS_FAMILY也就是个ushort,只是换个名字)。还是回来看下它的结构,主要是后面的sa_data[14],这个参考http://baike.baidu.com/view/2355183.html,里面说得很详细。下面是截图,可以很清楚地看到前2个字节都是0,接下来的4个是有值的,后面的都是0。
        实际上那4个有值的字节就是32位的地址,如192.168.0.1等。
        现在让我们回来看那两个函数ifprint及iptos。在看完pcap_if_t等结构的说明后,想必你也看出来ifprint就是对pcap_if_t结构的解析,iptos就是对sockaddr的解析了吧。
        目前为止还没有自己写的东西,谁叫我还是个新手,一步步学吧。下一步是做一个可以测试在1000M网卡下WinPcap发送能力的极限能到多少的小程序,以前100M的话好像只能到60M。
  • 相关阅读:
    多个tab切换demo
    react添加和删除定时器的地方
    编写C语言的两种方法----Visual Studio/CodeBlocks
    C++学习笔记---引用的本质
    C++学习笔记---指针
    C++学习笔记---数据类型
    博客园皮肤SimpleMemory深色风格皮肤
    SQL DELETE语句如何让表使用别名的方法
    Asp.Net实现局部刷新,ScriptManager和UpdatePanel控件的使用
    由于可能不会将凭据发送到远程计算机,因此将不会进行连接。若要获得协助,请与您的系统管理员联系。(转)
  • 原文地址:https://www.cnblogs.com/sober/p/1544719.html
Copyright © 2011-2022 走看看