亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

winPcap_5_打開適配器并捕獲數據包

系統 1941 0

?

  知道如何獲取適配器的信息了,那我們就開始一項更具意義的工作,打開適配器并捕獲數據包。編寫一個程序,將每一個 通過適配器的數據包 打印出來。

?

打開設備的函數是? pcap_open()

(Open a generic source in order to capture / send (WinPcap only) traffic.)

      pcap_t* pcap_open  ( 
      
        const
      
      
        char
      
       *
      
          source,

                     
      
      
        int
      
      
          snaplen,

                     
      
      
        int
      
      
          flags,

                     
      
      
        int
      
      
          read_timeout,

                     
      
      
        struct
      
       pcap_rmtauth *
      
          auth,

                     
      
      
        char
      
       *
      
          errbuf

                   )
      
    

?

  · snaplen 制定要捕獲數據包中的哪些部分。 在一些操作系統中 (比如 xBSD 和 Win32), 驅動可以被配置成 只捕獲數據包的初始化部分 : 這樣可以減少應用程序間復制數據的量,從而提高捕獲效率。本例中,我們將值定為65535,它比我們能遇到的最大的MTU還要大。因此,我們確信我們總能收到完整的數據包。

  · flags : 最最重要的flag是用來指示適配器是否要被設置成 混雜模式 一般情況下,適配器只接收發給它自己的數據包, 而那些在其他機器之間通訊的數據包,將會被丟棄 。 相反,如果適配器是混雜模式,那么不管這個數據包是不是發給我的,我都會去捕獲。也就是說,我會去捕獲所有的數據包。 這意味著在一個共享媒介(比如總線型以太網),WinPcap能捕獲其他主機的所有的數據包。 大多數用于數據捕獲的應用程序都會將適配器設置成混雜模式 ,所以,我們也會在下面的范例中,使用混雜模式。?

  · read_timeout(to_ms): 指定 讀取數據的超時時間 ,以毫秒計(1s=1000ms)。在適配器上進行讀取操作(比如用 pcap_dispatch() pcap_next_ex() ) 都會在 to_ms 毫秒時間內響應,即使在網絡上沒有可用的數據包。 在統計模式下 to_ms 還可以用來定義 統計的時間間隔 。 將 to_ms 設置為0意味著沒有超時,那么如果沒有數據包到達的話,讀操作將永遠不會返回。 如果設置成-1,則情況恰好相反,無論有沒有數據包到達,讀操作都會立即返回。

Returns:
A pointer to a 'pcap_t' which can be used as a parameter to the following calls ( pcap_compile() and so on) and that specifies an opened WinPcap session. In case of problems, it returns NULL and the 'errbuf' variable keeps the error message.
Warning:
The source cannot be larger than PCAP_BUF_SIZE.

The following formats are not allowed as 'source' strings:

  • rpcap:// [to open the first local adapter]
  • rpcap://hostname/ [to open the first remote adapter]?

?

?

      
        int
      
       pcap_dispatch  ( pcap_t *
      
          p,  

  
      
      
        int
      
      
          cnt,  

  pcap_handler  callback,  

  u_char 
      
      *
      
          user   

 )
      
    

?

Collect a group of packets.?

?

      
        int
      
       pcap_loop  ( pcap_t *
      
          p,  

  
      
      
        int
      
      
          cnt,  

  pcap_handler  callback,  

  u_char 
      
      *
      
          user   

 ) 
      
    

?

Collect a group of packets.

?

pcap_dispatch()與pcap_loop()的區別:

  當適配器被打開,捕獲工作就可以用 pcap_dispatch() pcap_loop() 進行。 這兩個函數非常的相似, 區別就是 pcap_ dispatch() 當超時時間到了(timeout expires)就返回 (盡管不能保證) ,而 pcap_loop() 不會因此而返回 ,只有當 cnt 數據包被捕獲,所以,pcap_loop()會在一小段時間內,阻塞網絡的利用。 pcap_loop() 對于我們這個簡單的范例來說,可以滿足需求,不過, pcap_dispatch() 函數一般用于比較復雜的程序中。

這兩個函數都有一個 回調 參數, packet_handler 指向一個可以接收數據包的函數 。 這個函數會在收到每個新的數據包并收到一個通用狀態時被libpcap所調用 ( 與函數 pcap_loop() pcap_dispatch() 中的 user 參數相似),數據包的首部一般有一些諸如時間戳,數據包長度的信息,還有包含了協議首部的實際數據。 注意:冗余校驗碼CRC不再支持,因為幀到達適配器,并經過校驗確認以后,適配器就會將CRC刪除,與此同時,大部分適配器會直接丟棄CRC錯誤的數據包,所以,WinPcap沒法捕獲到它們。

上面的程序將每一個數據包的時間戳和長度從 pcap_pkthdr 的首部解析出來,并打印在屏幕上。

請注意,使用 pcap_loop() 函數可能會遇到障礙,主要因為它直接由數據包捕獲驅動所調用。因此,用戶程序是不能直接控制它的。另一個實現方法(也是提高可讀性的方法),是使用 pcap_next_ex() 函數。有關這個函數的使用,請看 ( 不用回調方法捕獲數據包 ).

?

      
        struct
      
      
          pcap_pkthdr{

    timeval    ts

    
      
      
        //
      
      
        time stamp
      
      
            bpf_u_int32    caplen

    
      
      
        //
      
      
        length of portion present
      
      
            bpf_u_int32    len

    
      
      
        //
      
      
        length this packet (off wire)
      
      

};
    

Detailed Description

Header of a packet in the dump file.

Each packet in the dump file is prepended with this generic header. This gets around the problem of different headers for different packet interfaces.?

?

        
           1
        
         #include 
        
          "
        
        
          pcap.h
        
        
          "
        
        
           2
        
        
          #pragma
        
         comment(lib, "wpcap.lib")


        
           3
        
        
          #pragma
        
         comment(lib, "Packet.lib")


        
           4
        
        
          #pragma
        
         comment(lib, "wsock32.lib")


        
           5
        
        
           6
        
        
           7
        
         #include 
        
          "
        
        
          pcap.h
        
        
          "
        
        
           8
        
        
           9
        
        
          /*
        
        
           packet handler 函數原型 
        
        
          */
        
        
          10
        
        
          void
        
         packet_handler(u_char *param, 
        
          const
        
        
          struct
        
         pcap_pkthdr *header, 
        
          const
        
         u_char *
        
          pkt_data);


        
        
          11
        
        
          12
        
        
          main()


        
        
          13
        
        
          {


        
        
          14
        
             pcap_if_t *
        
          alldevs;


        
        
          15
        
             pcap_if_t *
        
          d;


        
        
          16
        
        
          int
        
        
           inum;


        
        
          17
        
        
          int
        
         i=
        
          0
        
        
          ;


        
        
          18
        
             pcap_t *
        
          adhandle;


        
        
          19
        
        
          char
        
        
           errbuf[PCAP_ERRBUF_SIZE];


        
        
          20
        
        
          21
        
        
          /*
        
        
           獲取本機設備列表 
        
        
          */
        
        
          22
        
        
          if
        
         (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -
        
          1
        
        
          )


        
        
          23
        
        
              {


        
        
          24
        
                 fprintf(stderr,
        
          "
        
        
          Error in pcap_findalldevs: %s\n
        
        
          "
        
        
          , errbuf);


        
        
          25
        
                 exit(
        
          1
        
        
          );


        
        
          26
        
        
              }


        
        
          27
        
        
          28
        
        
          /*
        
        
           打印列表 
        
        
          */
        
        
          29
        
        
          for
        
        (d=alldevs; d; d=d->
        
          next)


        
        
          30
        
        
              {


        
        
          31
        
                 printf(
        
          "
        
        
          %d. %s
        
        
          "
        
        , ++i, d->
        
          name);


        
        
          32
        
        
          if
        
         (d->
        
          description)


        
        
          33
        
                     printf(
        
          "
        
        
           (%s)\n
        
        
          "
        
        , d->
        
          description);


        
        
          34
        
        
          else
        
        
          35
        
                     printf(
        
          "
        
        
           (No description available)\n
        
        
          "
        
        
          );


        
        
          36
        
        
              }


        
        
          37
        
        
          38
        
        
          if
        
        (i==
        
          0
        
        
          )


        
        
          39
        
        
              {


        
        
          40
        
                 printf(
        
          "
        
        
          \nNo interfaces found! Make sure WinPcap is installed.\n
        
        
          "
        
        
          );


        
        
          41
        
        
          return
        
         -
        
          1
        
        
          ;


        
        
          42
        
        
              }


        
        
          43
        
        
          44
        
             printf(
        
          "
        
        
          Enter the interface number (1-%d):
        
        
          "
        
        
          ,i);


        
        
          45
        
             scanf(
        
          "
        
        
          %d
        
        
          "
        
        , &
        
          inum);


        
        
          46
        
        
          47
        
        
          if
        
        (inum < 
        
          1
        
         || inum >
        
           i)


        
        
          48
        
        
              {


        
        
          49
        
                 printf(
        
          "
        
        
          \nInterface number out of range.\n
        
        
          "
        
        
          );


        
        
          50
        
        
          /*
        
        
           釋放設備列表 
        
        
          */
        
        
          51
        
        
                  pcap_freealldevs(alldevs);


        
        
          52
        
        
          return
        
         -
        
          1
        
        
          ;


        
        
          53
        
        
              }


        
        
          54
        
        
          55
        
        
          /*
        
        
           跳轉到選中的適配器 
        
        
          */
        
        
          56
        
        
          for
        
        (d=alldevs, i=
        
          0
        
        ; i< inum-
        
          1
        
         ;d=d->next, i++
        
          );


        
        
          57
        
        
          58
        
        
          /*
        
        
           打開設備 
        
        
          */
        
        
          59
        
        
          if
        
         ( (adhandle= pcap_open(d->name,          
        
          //
        
        
           設備名
        
        
          60
        
        
          65536
        
        ,            
        
          //
        
        
           65535保證能捕獲到不同數據鏈路層上的每個數據包的全部內容
        
        
          61
        
                 PCAP_OPENFLAG_PROMISCUOUS,    
        
          //
        
        
           混雜模式
        
        
          62
        
        
          1000
        
        ,             
        
          //
        
        
           讀取超時時間
        
        
          63
        
                 NULL,             
        
          //
        
        
           遠程機器驗證
        
        
          64
        
                 errbuf            
        
          //
        
        
           錯誤緩沖池
        
        
          65
        
                 ) ) ==
        
           NULL)


        
        
          66
        
        
              {


        
        
          67
        
                 fprintf(stderr,
        
          "
        
        
          \nUnable to open the adapter. %s is not supported by WinPcap\n
        
        
          "
        
        , d->
        
          name);


        
        
          68
        
        
          /*
        
        
           釋放設備列表 
        
        
          */
        
        
          69
        
        
                  pcap_freealldevs(alldevs);


        
        
          70
        
        
          return
        
         -
        
          1
        
        
          ;


        
        
          71
        
        
              }


        
        
          72
        
        
          73
        
             printf(
        
          "
        
        
          \nlistening on %s...\n
        
        
          "
        
        , d->
        
          description);


        
        
          74
        
        
          75
        
        
          /*
        
        
           釋放設備列表 
        
        
          */
        
        
          76
        
        
              pcap_freealldevs(alldevs);


        
        
          77
        
        
          78
        
        
          /*
        
        
           開始捕獲 
        
        
          */
        
        
          79
        
             pcap_loop(adhandle, 
        
          0
        
        
          , packet_handler, NULL);


        
        
          80
        
        
          81
        
        
          return
        
        
          0
        
        
          ;


        
        
          82
        
        
          }


        
        
          83
        
        
          84
        
        
          85
        
        
          /*
        
        
           每次捕獲到數據包時,libpcap都會自動調用這個回調函數 
        
        
          */
        
        
          86
        
        
          void
        
         packet_handler(u_char *param, 
        
          const
        
        
          struct
        
         pcap_pkthdr *header, 
        
          const
        
         u_char *
        
          pkt_data)


        
        
          87
        
        
          {


        
        
          88
        
        
          struct
        
         tm *
        
          ltime;


        
        
          89
        
        
          char
        
         timestr[
        
          16
        
        
          ];


        
        
          90
        
        
              time_t local_tv_sec;


        
        
          91
        
        
          92
        
        
          /*
        
        
           將時間戳轉換成可識別的格式 
        
        
          */
        
        
          93
        
             local_tv_sec = header->
        
          ts.tv_sec;


        
        
          94
        
             ltime=localtime(&
        
          local_tv_sec);


        
        
          95
        
             strftime( timestr, 
        
          sizeof
        
         timestr, 
        
          "
        
        
          %H:%M:%S
        
        
          "
        
        
          , ltime);


        
        
          96
        
        
          97
        
             printf(
        
          "
        
        
          %s,%.6d len:%d\n
        
        
          "
        
        , timestr, header->ts.tv_usec, header->
        
          len); 


        
        
          98
        
         }
      
打開適配器并捕獲數據包.c

  *結果:

winPcap_5_打開適配器并捕獲數據包

時間戳(精確到微妙),包長度;

?

timeval

The? timeval ?structure is used to specify time values. It is associated with the Berkeley Software Distribution (BSD) file Time.h.

      
        struct
      
      
         timeval {

  
      
      
        long
      
          tv_sec;         
      
        //
      
      
         seconds 
      
      
        long
      
          tv_usec;        
      
        //
      
      
         and microseconds 
      
      

};
    

Members

tv_sec
Time value, in seconds.
tv_usec
Time value, in microseconds.?

?

localtime

Converts a time value and corrects for the local time zone.

      
        struct
      
       tm *localtime( 
      
        const
      
       time_t *timer );
    

?

?

      
        struct
      
      
         tm {

        
      
      
        int
      
       tm_sec;     
      
        /*
      
      
         seconds after the minute - [0,59] 
      
      
        */
      
      
        int
      
       tm_min;     
      
        /*
      
      
         minutes after the hour - [0,59] 
      
      
        */
      
      
        int
      
       tm_hour;    
      
        /*
      
      
         hours since midnight - [0,23] 
      
      
        */
      
      
        int
      
       tm_mday;    
      
        /*
      
      
         day of the month - [1,31] 
      
      
        */
      
      
        int
      
       tm_mon;     
      
        /*
      
      
         months since January - [0,11] 
      
      
        */
      
      
        int
      
       tm_year;    
      
        /*
      
      
         years since 1900 
      
      
        */
      
      
        int
      
       tm_wday;    
      
        /*
      
      
         days since Sunday - [0,6] 
      
      
        */
      
      
        int
      
       tm_yday;    
      
        /*
      
      
         days since January 1 - [0,365] 
      
      
        */
      
      
        int
      
       tm_isdst;   
      
        /*
      
      
         daylight savings time flag 
      
      
        */
      
      
        

        };
      
    

?

?

?

asctime, _wasctime

Converts a? tm? time structure to a character string.

      
        char
      
       *asctime( 
      
        const
      
      
        struct
      
       tm *
      
        timeptr );



wchar_t 
      
      *_wasctime( 
      
        const
      
      
        struct
      
       tm *timeptr );
    

?

Return Value

asctime ?returns a pointer to the character string result;? _wasctime ?returns a pointer to the wide-character string result. There is no error return value.

?

?

time

Gets the system time.

      time_t time( time_t *timer );
    

?

?

?

strftime, wcsftime

Format a time string.

      size_t strftime( 
      
        char
      
       *strDest, size_t maxsize, 
      
        const
      
      
        char
      
       *format, 
      
        const
      
      
        struct
      
       tm *
      
        timeptr );



size_t wcsftime( wchar_t 
      
      *strDest, size_t maxsize, 
      
        const
      
       wchar_t *format, 
      
        const
      
      
        struct
      
       tm *timeptr );
    

?

Return Value

strftime ?returns the number of characters placed in? strDest ?if the total number of resulting characters, including the terminating null, is not more than? maxsize .

wcsftime ?returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of? strDest ?is indeterminate.

?

?

      
        void
      
       pcap_freealldevs  ( pcap_if_t *  alldevsp   )  
    

Free an interface list returned by? pcap_findalldevs() . ?

?

winPcap_5_打開適配器并捕獲數據包


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米777第四 | 九七影院97影院理论片 | www.成人在线视频 | 欧美一级高清免费a | 久久国产精品亚洲一区二区 | 在线亚洲国产精品区 | 在线日韩欧美 | 亚洲一欧洲中文字幕在线 | 播五月综合 | 亚洲国产精品婷婷久久 | 黄页网址大全免费观看美女 | 色偷偷91综合久久噜噜 | 日韩最新视频一区二区三 | 亚洲色婷婷综合开心网 | 色综合精品久久久久久久 | 国产成人在线网站 | 久久久噜久噜久久gif动图 | 五月婷婷视频 | 国产一区二区在线免费观看 | 国产成人亚洲精品老王 | 亚洲综合久久久久久888 | 天天透天天操 | 国产视频欧美 | 免费国产一级特黄aa大片在线 | 激情欧美 | 欧美午夜在线播放 | 欧洲一级黄色 | 妇女网站爱嘿嘿视频免费观看 | 日韩国产欧美在线观看 | 久久欧美精品欧美久久欧美 | 在线xxxx | 曰本一区二区 | 国产福利资源 | 热久久这里只有 | 亚洲精品久久中文字幕 | 国产成人精品s8p视频 | 国产精品国产国产aⅴ | 狠狠色噜噜狠狠狠888奇米 | 国产精品热久久 | 免费在线a| 中文字幕一区二区三区视频在线 |