Linux 網絡編程—— libpcap 詳解

概述

libpcap 是一個網絡數據包捕獲函數庫,功能非常強大,Linux 下著名的 tcpdump 就是以它爲基礎的。


libpcap主要的作用

1)捕獲各種數據包,列如:網絡流量統計。

2)過濾網絡數據包,列如:過濾掉本地上的一些數據,類似防火牆。

3)分析網絡數據包,列如:分析網絡協議,數據的採集。

4)存儲網絡數據包,列如:保存捕獲的數據以爲將來進行分析。


libpcap 的安裝



libpcap 的抓包框架

pcap_lookupdev():函數用於查找網絡設備,返回可被 pcap_open_live() 函數調用的網絡設備名指針。

pcap_lookupnet():函數獲得指定網絡設備的網絡號和掩碼。
pcap_open_live(): 函數用於打開網絡設備,並且返回用於捕獲網絡數據包的數據包捕獲描述字。對於此網絡設備的操作都要基於此網絡設備描述字。
pcap_compile(): 函數用於將用戶制定的過濾策略編譯到過濾程序中。
pcap_setfilter():函數用於設置過濾器。
pcap_loop():函數 pcap_dispatch() 函數用於捕獲數據包,捕獲後還可以進行處理,此外 pcap_next() 和 pcap_next_ex() 兩個函數也可以用來捕獲數據包。
pcap_close():函數用於關閉網絡設備,釋放資源。


利用 libpcap 函數庫開發應用程序的基本步驟

1、打開網絡設備

2、設置過濾規則

3、捕獲數據

4、關閉網絡設備

抓包詳細步驟

首先要使用 libpcap,我們必須包含 pcap.h 頭文件,可以在 /usr/local/include/pcap/pcap.h 找到,其中包含了每個類型定義的詳細說明。



1、獲取網絡接口設備名

char *pcap_lookupdev(char *errbuf);

功能:

得到可用的網絡設備名指針

參數:

errbuf:存放出錯信息字符串,裏面有個宏定義:PCAP_ERRBUF_SIZE,爲錯誤緩衝區大小

返回值:

成功返回設備名指針(第一個合適的網絡接口的字符串指針);

失敗返回 NULL,同時,errbuf 存放出錯信息字符串。


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯信息  
  2. char *dev = pcap_lookupdev(error_content);  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  

2、獲取網絡號(ip 地址)和掩碼

int pcap_lookupnet(    char *device,                    

bpf_u_int32 *netp, 

bpf_u_int32 *maskp,     

char *errbuf  );

功能:

獲取指定網卡的 ip 地址,子網掩碼

參數:

device:網絡設備名,爲第一步獲取的網絡接口字符串(pcap_lookupdev() 的返回值 ),也可人爲指定,如“eth0”。

netp:存放 ip 地址的指針,bpf_u_int32 爲 32 位無符號整型

maskp:存放子網掩碼的指針,bpf_u_int32 爲 32 位無符號整型

errbuf:存放出錯信息

返回值:

成功返回 0,失敗返回 -1


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯信息  
  2. char *dev = pcap_lookupdev(error_content);  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8.   
  9.   
  10. bpf_u_int32 netp = 0, maskp = 0;  
  11. pcap_t * pcap_handle = NULL;  
  12. int ret = 0;  
  13.   
  14. //獲得網絡號和掩碼  
  15. ret = pcap_lookupnet(dev, &netp, &maskp, error_content);  
  16. if(ret == -1)  
  17. {  
  18.     printf(error_content);  
  19.     exit(-1);  
  20. }  

3、打開網絡接口

pcap_t *pcap_open_live(  const char *device,

int snaplen,

int promisc,

int to_ms,

char *ebuf );

功能

打開一個用於捕獲數據的網絡接口

參數:

device:網絡接口的名字,爲第一步獲取的網絡接口字符串(pcap_lookupdev() 的返回值 ),也可人爲指定,如“eth0”。

snaplen:捕獲數據包的長度,長度不能大於 65535 個字節。

promise:“1” 代表混雜模式,其它非混雜模式。什麼爲混雜模式,請看《原始套接字編程》。

to_ms:指定需要等待的毫秒數,超過這個數值後,獲取數據包的函數就會立即返回(這個函數不會阻塞,後面的抓包函數纔會阻塞)。0 表示一直等待直到有數據包到來。

ebuf:存儲錯誤信息。

返回值:

返回 pcap_t 類型指針,後面的所有操作都要使用這個指針


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯信息  
  2. char *dev = pcap_lookupdev(error_content);  // 獲取網絡接口  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8.   
  9. // 打開網絡接口  
  10. pcap_t * pcap_handle = pcap_open_live(dev, 1024, 1, 0, error_content);  
  11. if(NULL == pcap_handle)  
  12. {  
  13.     printf(error_content);  
  14.     exit(-1);  
  15. }  

4、獲取數據包

a)

const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);

功能:

捕獲一個網絡數據包,收到一個數據包立即返回

參數:

p:pcap_open_live()返回的 pcap_t 類型的指針
h:數據包頭


pcap_pkthdr 類型的定義如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. struct pcap_pkthdr  
  2. {  
  3.     struct timeval ts; // 抓到包的時間  
  4.     bpf_u_int32 caplen; // 表示抓到的數據長度  
  5.     bpf_u_int32 len; // 表示數據包的實際長度  
  6. }  

len 和 caplen的區別:
因爲在某些情況下你不能保證捕獲的包是完整的,例如一個包長 1480,但是你捕獲到 1000 的時候,可能因爲某些原因就中止捕獲了,所以 caplen 是記錄實際捕獲的包長,也就是 1000,而 len 就是 1480。 

返回值:

成功返回捕獲數據包的地址,失敗返回 NULL


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. const unsigned char *p_packet_content = NULL; // 保存接收到的數據包的起始地址  
  2. pcap_t *pcap_handle = NULL;  
  3. struct pcap_pkthdr protocol_header;  
  4.   
  5. pcap_handle = pcap_open_live("eth0", 1024, 1, 0,NULL);  
  6.   
  7. p_packet_content = pcap_next(pcap_handle, &protocol_header);   
  8. //p_packet_content  所捕獲數據包的地址  
  9.           
  10. printf("Capture Time is :%s",ctime((const time_t *)&protocol_header.ts.tv_sec)); // 時間  
  11. printf("Packet Lenght is :%d\n",protocol_header.len);   // 數據包的實際長度  
  12.   
  13. // 分析以太網中的 源mac、目的mac  
  14. struct ether_header *ethernet_protocol = NULL;  
  15. unsigned char *p_mac_string = NULL;         // 保存mac的地址,臨時變量  
  16.   
  17. ethernet_protocol = (struct ether_header *)p_packet_content;  //struct ether_header 以太網幀頭部  
  18.   
  19. p_mac_string = (unsigned char *)ethernet_protocol->ether_shost;//獲取源mac  
  20. printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  21.   
  22. p_mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//獲取目的mac  
  23. printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  

b)

int pcap_loop(     pcap_t *p,

int cnt,

pcap_handler callback,

u_char *user );

功能:

循環捕獲網絡數據包,直到遇到錯誤或者滿足退出條件。每次捕獲一個數據包就會調用 callback 指定的回調函數,所以,可以在回調函數中進行數據包的處理操作。

參數:

ppcap_open_live()返回的 pcap_t 類型的指針

cnt:指定捕獲數據包的個數,一旦抓到了 cnt 個數據包,pcap_loop 立即返回。如果是 -1,就會永無休止的捕獲,直到出現錯誤。

callback:回調函數,名字任意,根據需要自行起名。

user:向回調函數中傳遞的參數。


callback 回調函數的定義

void callback(  u_char *userarg, 

const struct pcap_pkthdr * pkthdr, 

const u_char * packet )

userarg:pcap_loop() 的最後一個參數,當收到足夠數量的包後 pcap_loop 會調用callback 回調函數,同時將pcap_loop()的user參數傳遞給它
pkthdr:是收到數據包的 pcap_pkthdr 類型的指針,和 pcap_next() 第二個參數是一樣的。
packet :收到的數據包數據

返回值:

成功返回0,失敗返回負數


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. if( pcap_loop(pcap_handle, -1, ethernet_protocol_callback, NULL) < 0 )  
  2. {  
  3.     perror("pcap_loop");  
  4. }  
  5.   
  6. /*******************************回調函數************************************/  
  7. void ethernet_protocol_callback(unsigned char *argument,const struct pcap_pkthdr *packet_heaher,const unsigned char *packet_content)  
  8. {  
  9.     unsigned char *mac_string;              //  
  10.     struct ether_header *ethernet_protocol;  
  11.     unsigned short ethernet_type;           //以太網類型  
  12.     printf("----------------------------------------------------\n");  
  13.     printf("%s\n", ctime((time_t *)&(packet_heaher->ts.tv_sec))); //轉換時間  
  14.     ethernet_protocol = (struct ether_header *)packet_content;  
  15.       
  16.     mac_string = (unsigned char *)ethernet_protocol->ether_shost;//獲取源mac地址  
  17.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  18.       
  19.     mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//獲取目的mac  
  20.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  21.       
  22.     ethernet_type = ntohs(ethernet_protocol->ether_type);//獲得以太網的類型  
  23.     printf("Ethernet type is :%04x\n",ethernet_type);  
  24.     switch(ethernet_type)  
  25.     {  
  26.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  27.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  28.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  29.         default:break;  
  30.     }  
  31.     usleep(800*1000);  
  32. }  


c)

int pcap_dispatch(pcap_t * p, int cnt, pcap_handler callback, u_char * user);

這個函數和 pcap_loop() 非常類似,只是在超過 to_ms 毫秒後就會返回( to_ms 是pcap_open_live() 的第4個參數 )


5、釋放網絡接口

void pcap_close(pcap_t *p);

功能:

關閉 pcap_open_live() 打開的網絡接口(即其返回值,pcap_t 類型指針),並釋放相關資源。注意,操作完網絡接口,應該釋放其資源。

參數:

p:需要關閉的網絡接口,pcap_open_live() 的返回值(pcap_t 類型指針

返回值:


實例如下:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. // 打開網絡接口  
  2. pcap_t * pcap_handle = pcap_open_live("eth0", 1024, 1, 0, error_content);  
  3. if(NULL == pcap_handle)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8.   
  9. //// ……  
  10. //// ……  
  11.   
  12. pcap_close(pcap_handle); //釋放網絡接口  

例子1(接收一個數據包):

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <stdio.h>  
  2. #include <pcap.h>  
  3. #include <arpa/inet.h>  
  4. #include <time.h>  
  5. #include <stdlib.h>  
  6. struct ether_header  
  7. {  
  8.     unsigned char ether_dhost[6];   //目的mac  
  9.     unsigned char ether_shost[6];   //源mac  
  10.     unsigned short ether_type;      //以太網類型  
  11. };  
  12. #define BUFSIZE 1514  
  13.   
  14. int main(int argc,char *argv[])  
  15. {  
  16.     pcap_t * pcap_handle = NULL;  
  17.     char error_content[100] = "";   // 出錯信息  
  18.     const unsigned char *p_packet_content = NULL;       // 保存接收到的數據包的起始地址  
  19.     unsigned char *p_mac_string = NULL;         // 保存mac的地址,臨時變量  
  20.     unsigned short ethernet_type = 0;           // 以太網類型  
  21.     char *p_net_interface_name = NULL;      // 接口名字  
  22.     struct pcap_pkthdr protocol_header;  
  23.     struct ether_header *ethernet_protocol;  
  24.   
  25.     //獲得接口名  
  26.     p_net_interface_name = pcap_lookupdev(error_content);  
  27.     if(NULL == p_net_interface_name)  
  28.     {  
  29.         perror("pcap_lookupdev");  
  30.         exit(-1);  
  31.     }  
  32.       
  33.     //打開網絡接口  
  34.     pcap_handle = pcap_open_live(p_net_interface_name,BUFSIZE,1,0,error_content);  
  35.     p_packet_content = pcap_next(pcap_handle,&protocol_header);  
  36.       
  37.     printf("------------------------------------------------------------------------\n");  
  38.     printf("capture a Packet from p_net_interface_name :%s\n",p_net_interface_name);  
  39.     printf("Capture Time is :%s",ctime((const time_t *)&protocol_header.ts.tv_sec));  
  40.     printf("Packet Lenght is :%d\n",protocol_header.len);  
  41.       
  42.     /* 
  43.     *分析以太網中的 源mac、目的mac 
  44.     */  
  45.     ethernet_protocol = (struct ether_header *)p_packet_content;  
  46.     p_mac_string = (unsigned char *)ethernet_protocol->ether_shost;//獲取源mac  
  47.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  48.     p_mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//獲取目的mac  
  49.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  50.   
  51.     /* 
  52.     *獲得以太網的數據包的地址,然後分析出上層網絡協議的類型 
  53.     */  
  54.     ethernet_type = ntohs(ethernet_protocol->ether_type);  
  55.     printf("Ethernet type is :%04x\t",ethernet_type);  
  56.     switch(ethernet_type)  
  57.     {  
  58.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  59.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  60.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  61.         default:printf("The network layer unknow!\n");break;  
  62.     }  
  63.       
  64.     pcap_close(pcap_handle);  
  65.     return 0;  
  66. }  

注意:gcc 編譯時需要加上 -lpcap,運行時需要使用超級權限



例子2(接收多個數據包):

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <stdio.h>  
  2. #include <pcap.h>  
  3. #include <arpa/inet.h>  
  4. #include <time.h>  
  5. #include <stdlib.h>  
  6.   
  7. #define BUFSIZE 1514  
  8.   
  9. struct ether_header  
  10. {  
  11.     unsigned char ether_dhost[6];   //目的mac  
  12.     unsigned char ether_shost[6];   //源mac  
  13.     unsigned short ether_type;      //以太網類型  
  14. };  
  15.   
  16. /*******************************回調函數************************************/  
  17. void ethernet_protocol_callback(unsigned char *argument,const struct pcap_pkthdr *packet_heaher,const unsigned char *packet_content)  
  18. {  
  19.     unsigned char *mac_string;              //  
  20.     struct ether_header *ethernet_protocol;  
  21.     unsigned short ethernet_type;           //以太網類型  
  22.     printf("----------------------------------------------------\n");  
  23.     printf("%s\n", ctime((time_t *)&(packet_heaher->ts.tv_sec))); //轉換時間  
  24.     ethernet_protocol = (struct ether_header *)packet_content;  
  25.       
  26.     mac_string = (unsigned char *)ethernet_protocol->ether_shost;//獲取源mac地址  
  27.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  28.     mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//獲取目的mac  
  29.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  30.       
  31.     ethernet_type = ntohs(ethernet_protocol->ether_type);//獲得以太網的類型  
  32.     printf("Ethernet type is :%04x\n",ethernet_type);  
  33.     switch(ethernet_type)  
  34.     {  
  35.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  36.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  37.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  38.         default:break;  
  39.     }  
  40.     usleep(800*1000);  
  41. }  
  42.   
  43. int main(int argc, char *argv[])  
  44. {  
  45.     char error_content[100];    //出錯信息  
  46.     pcap_t * pcap_handle;  
  47.     unsigned char *mac_string;                
  48.     unsigned short ethernet_type;           //以太網類型  
  49.     char *net_interface = NULL;                 //接口名字  
  50.     struct pcap_pkthdr protocol_header;  
  51.     struct ether_header *ethernet_protocol;  
  52.       
  53.     //獲取網絡接口  
  54.     net_interface = pcap_lookupdev(error_content);  
  55.     if(NULL == net_interface)  
  56.     {  
  57.         perror("pcap_lookupdev");  
  58.         exit(-1);  
  59.     }  
  60.   
  61.     pcap_handle = pcap_open_live(net_interface,BUFSIZE,1,0,error_content);//打開網絡接口  
  62.           
  63.     if(pcap_loop(pcap_handle,-1,ethernet_protocol_callback,NULL) < 0)  
  64.     {  
  65.         perror("pcap_loop");  
  66.     }  
  67.       
  68.     pcap_close(pcap_handle);  
  69.     return 0;  
  70. }  

運行情況如下:


過濾數據包

我們抓到的數據包往往很多,如何過濾掉我們不感興趣的數據包呢?


幾乎所有的操作系統( BSD, AIX, Mac OS, Linux 等)都會在內核中提供過濾數據包的方法,主要都是基於 BSD Packet Filter( BPF ) 結構的。libpcap 利用 BPF 來過濾數據包。


1)設置過濾條件
BPF 使用一種類似於彙編語言的語法書寫過濾表達式,不過 libpcap 和 tcpdump 都把它封裝成更高級且更容易的語法了,具體可以通過 man tcpdump查看:




以下是一些例子:

src host 192.168.1.177

只接收源 ip 地址是 192.168.1.177 的數據包


dst port 80

只接收 tcp/udp 的目的端口是 80 的數據包


not tcp

只接收不使用 tcp 協議的數據包


tcp[13] == 0x02 and (dst port 22 or dst port 23)

只接收 SYN 標誌位置位且目標端口是 22 或 23 的數據包( tcp 首部開始的第 13 個字節)


icmp[icmptype] == icmp-echoreply or icmp[icmptype] == icmp-echo

只接收 icmp 的 ping 請求和 ping 響應的數據包


ehter dst 00:e0:09:c1:0e:82

只接收以太網 mac 地址是 00:e0:09:c1:0e:82 的數據包


ip[8] == 5

只接收 ip 的 ttl=5 的數據包(ip首部開始的第8個字節)


2)編譯 BPF 過濾規則

int pcap_compile(  pcap_t *p,

struct bpf_program *fp,

char *buf,

int optimize,

bpf_u_int32 mask );

功能:

編譯 BPF 過濾規則

參數:

p:pcap_open_live() 返回的 pcap_t 類型的指針

fp:存放編譯後的 bpf,應用過濾規則時需要用到這個指針

buf:過濾條件

optimize:是否需要優化過濾表達式

mask:指定本地網絡的網絡掩碼,不需要時可寫 0

返回值:

成功返回 0,失敗返回 -1


3)應用 BPF 過濾規則

int pcap_setfilter( pcap_t * p,  struct bpf_program * fp );

功能:

應用 BPF 過濾規則,簡單理解爲讓過濾生效

參數:

p:pcap_open_live() 返回的 pcap_t 類型的指針

fp:pcap_compile() 的第二個參數

返回值:

成功返回 0,失敗返回 -1


這個編譯應用過程,有點類似於,我們寫一個 C 程序,先編譯,後運行的過程。


應用完過濾表達式之後我們便可以使用 pcap_loop() 或 pcap_next() 等抓包函數來抓包了。


下面的程序演示瞭如何過濾數據包,我們只接收目的端口是 80 的數據包:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <pcap.h>  
  2. #include <time.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5.   
  6. void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet)  
  7. {  
  8.   int * id = (int *)arg;  
  9.     
  10.   printf("id: %d\n", ++(*id));  
  11.   printf("Packet length: %d\n", pkthdr->len);  
  12.   printf("Number of bytes: %d\n", pkthdr->caplen);  
  13.   printf("Recieved time: %s", ctime((const time_t *)&pkthdr->ts.tv_sec));   
  14.     
  15.   int i;  
  16.   for(i=0; i<pkthdr->len; ++i)  
  17.   {  
  18.     printf(" %02x", packet[i]);  
  19.     if( (i + 1) % 16 == 0 )  
  20.     {  
  21.       printf("\n");  
  22.     }  
  23.   }  
  24.     
  25.   printf("\n\n");  
  26. }  
  27.   
  28. int main()  
  29. {  
  30.   char errBuf[PCAP_ERRBUF_SIZE], * devStr;  
  31.     
  32.   /* get a device */  
  33.   devStr = pcap_lookupdev(errBuf);  
  34.     
  35.   if(devStr)  
  36.   {  
  37.     printf("success: device: %s\n", devStr);  
  38.   }  
  39.   else  
  40.   {  
  41.     printf("error: %s\n", errBuf);  
  42.     exit(1);  
  43.   }  
  44.     
  45.   /* open a device, wait until a packet arrives */  
  46.   pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);  
  47.     
  48.   if(!device)  
  49.   {  
  50.     printf("error: pcap_open_live(): %s\n", errBuf);  
  51.     exit(1);  
  52.   }  
  53.     
  54.   /* construct a filter */  
  55.   struct bpf_program filter;  
  56.   pcap_compile(device, &filter, "dst port 80", 1, 0);  
  57.   pcap_setfilter(device, &filter);  
  58.     
  59.   /* wait loop forever */  
  60.   int id = 0;  
  61.   pcap_loop(device, -1, getPacket, (u_char*)&id);  
  62.     
  63.   pcap_close(device);  
  64.   
  65.   return 0;  
  66. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章