本程序的主要目標是展示如何解析所捕獲的數據包的協議首部。這個程序可以稱爲UDPdump,打印一些網絡上傳輸的UDP數據的信息。
我們選擇分析和現實UDP協議而不是TCP等其它協議,是因爲它比其它的協議更簡單
#include "pcap.h"
/* 4字節的IP地址 */
typedef struct ip_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
/* IPv4 首部 */
typedef struct ip_header{
u_char ver_ihl; // 版本 (4 bits) + 首部長度 (4 bits)
u_char tos; // 服務類型(Type of service)
u_short tlen; // 總長(Total length)
u_short identification; // 標識(Identification)
u_short flags_fo; // 標誌位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
u_char ttl; // 存活時間(Time to live)
u_char proto; // 協議(Protocol)
u_short crc; // 首部校驗和(Header checksum)
ip_address saddr; // 源地址(Source address)
ip_address daddr; // 目的地址(Destination address)
u_int op_pad; // 選項與填充(Option + Padding)
}ip_header;
/* UDP 首部*/
typedef struct udp_header{
u_short sport; // 源端口(Source port)
u_short dport; // 目的端口(Destination port)
u_short len; // UDP數據包長度(Datagram length)
u_short crc; // 校驗和(Checksum)
}udp_header;
/* 回調函數原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;
/* 獲得設備列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 打印列表 */
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;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳轉到已選設備 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打開適配器 */
if ( (adhandle= pcap_open(d->name, // 設備名
65536, // 要捕捉的數據包的部分
// 65535保證能捕獲到不同數據鏈路層上的每個數據包的全部內容
PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式
1000, // 讀取超時時間
NULL, // 遠程機器驗證
errbuf // 錯誤緩衝池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 檢查數據鏈路層,爲了簡單,我們只考慮以太網 */
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
if(d->addresses != NULL)
/* 獲得接口第一個地址的掩碼 */
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* 如果接口沒有地址,那麼我們假設一個C類的掩碼 */
netmask=0xffffff;
//編譯過濾器
if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
//設置過濾器
if (pcap_setfilter(adhandle, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter.\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
/* 開始捕捉 */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* 回調函數,當收到每一個數據包時會被libpcap所調用 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
time_t local_tv_sec;
/* 將時間戳轉換成可識別的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* 打印數據包的時間戳和長度 */
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
/* 獲得IP數據包頭部的位置 */
ih = (ip_header *) (pkt_data +
14); //以太網頭部長度
/* 獲得UDP首部的位置 */
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
/* 將網絡字節序列轉換成主機字節序列 */
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
/* 打印IP地址和UDP端口 */
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}
首先,我們將過濾器設置成"ip and udp"。在這種方式下,我們確信packet_handler()只會收到基於IPv4的UDP數據包;這將簡化解析過程,提高程序的效率。
我們還分別創建了用於描述IP首部和UDP首部的結構體。這些結構體中的各種數據會被packet_handler()合理地定位。
packet_handler(), 儘管只受限於單個協議的解析(比如基於IPv4的UDP),不過它展示了捕捉器(sniffers)是多麼的複雜,就像TcpDump或WinDump對網絡數據流進行解碼那樣。因爲我們對MAC首部不感興趣,所以我們跳過它。爲了簡潔,我們在開始捕捉前,使用了pcap_datalink()對MAC層進行了檢測,以確保我們是在處理一個以太網絡。這樣,我們就能確保MAC首部是14位的。
IP數據包的首部就位於MAC首部的後面。我們將從IP數據包的首部解析到源IP地址和目的IP地址。
處理UDP的首部有一些複雜,因爲IP數據包的首部的長度並不是固定的。然而,我們可以通過IP數據包的length域來得到它的長度。一旦我們知道了UDP首部的位置,我們就能解析到源端口和目的端口。
被解析出來的值被打印在屏幕上,形式如下所示:
1. \Device\Packet_{A7FD048A-5D4B-478E-B3C1-34401AC3B72F} (Xircom t 10/100 Adapter)
Enter the interface number (1-2):1
listening on Xircom CardBus Ethernet 10/100 Adapter...
16:13:15.312784 len:87 130.192.31.67.2682 -> 130.192.3.21.53
16:13:15.314796 len:137 130.192.3.21.53 -> 130.192.31.67.2682
16:13:15.322101 len:78 130.192.31.67.2683 -> 130.192.3.21.53
最後3行中的每一行,分別代表了一個數據包