winpcap 學習筆記(1)

來源:互聯網
上載者:User

著作權資訊:

本文來自internet,轉載這裡供網路編程愛好者學習和研究使用,請尊重作者的勞動成果。未經授權而在商業上使用原作者的文章屬侵權行為,後果由使用者自負,本人不承擔任何法律責任。

 

說明:本系列文章是我閱讀winpcap手冊後整理的一個學習筆記。文章中出現的所有代碼是我根據winpcap手冊中的範例程式碼進行了學習,並調試通過,其中對部分代碼作了修改,關於代碼的著作權我尊重winpcap手冊中的著作權說明,如果你使用了本系列文章中的代碼而引起任何的著作權或造成安全威脅等問題,我將不負任何責任。
 
       下載好了WpdPack_3_2_alpha1.zip(:http://www.winpcap.org/install/bin/WpdPack_3_2_alpha1.zip),解壓後除了有文檔,例子外還有Include和lib,於是想用TC2來做開發環境,但是編譯的時候老是出問題,於是放棄。後來閱讀了Winpcap手冊後才知道因為是在windows上開發,所以它推薦用VC++6.0,於是改用VC。 

    第一個實驗是:

#include 
#include 

int main() {
 pcap_if_t *alldevs;
 pcap_if_t *d;
 int i = 0;
 char errbuf[PCAP_ERRBUF_SIZE];

 /* Retrieve the device list from the local machine*/
 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
 {
  printf("Error in pcap_findalldevs_ex: %s/n", errbuf);
  exit(1);
 }

 /* Print the list */
 for (d = alldevs; d != NULL; d = d->next)
 {
  /* Print the device’s name */
  printf("%d. %s", ++ i, d->name);

  /* Print the device’s dscription */
  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 0;
 }

 /* We don’t need any more the device list. Free it */
 pcap_freealldevs(alldevs);

 return 1;
}

       編譯的時候又遇到問題——“無法開啟pcap.h”。又查看開發手冊才找到解決方案:

1.安裝Winpcap驅動。:http://www.winpcap.org/install/bin/WinPcap_3_1.exe。

2.將Winpcap的Include,Lib目錄添加進VC6.0的環境變數中;

3. 針對每一個項目,先用VC開啟項目,然後在"Project->Settings",標籤欄出選擇"C/C++",在"Preprocessor definitions"的輸入框裡添加"WPCAP",再選擇"Link",在"Object/library modules"的輸入框裡添加"wpcap.lib Packet.lib"。

       再編譯時間終於OK了。之後,閱讀代碼並查看開發手冊學到了下面的東西:

pcap_if是一個結構體,具體點它是一個鏈表的結點,他的定義如下:

struct pcap_if {

struct pcap_if *next;

char *name;

char *description;

struct pcap_addr *addresses;

u_int flags;

}

    另外,在pcap.h中有一句“typedef struct pcap_if pcap_if_t;”,所以也可以用pcap_if_t代替pcap_if。

int pcap_findalldevs_ex(char * source,

struct pcap_rmtauth * auth,

pcap_if_t ** alldevs,

char * errbuf

)

    這個函數是’pcap_findalldevs()’的一個超集。’pcap_findalldevs()’比較老,他只允許列出本地機器上的裝置。然而,’pcap_findalldevs_ex()’除了可以列出本地及其上的裝置,還可以列出遠程機器上的裝置。此外,它還能列出所有可用的pcap檔案到指定的檔案夾。’pcap_findalldevs_ex()’是平台無關的,然而它以來於標準的’pcap_findalldevs()’來獲得本地機器的地址。
 

本篇文章來源於 中國協議分析網|www.cnpaf.net 原文連結:http://www.cnpaf.net/Class/winpcap/200610/16292.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.