資源簡介
在linux簡單實現網絡的抓包程序,僅供參考喔

代碼片段和文件信息
QUOTE
//example.c
//使用方法:example〈網絡接口名〉?>?〈輸出文件名〉
//例如:example?etho?>?temp.txe
//結束方法:ctrl+c
//程序開始,讀入頭文件
#include
#include
#include
#include
#include
#include
#include
#include?//pcap程序庫
#include?//DNS檢索使用?
#define?MAXSTRINGSIZE?256?//字符串長度
#define?MAXSIZE?1024?//主機高速緩存中的最大記錄條數
#fefine?DEFAULT_SNAPLEN?68?/數據包數據的長度
typedef?struct
{
unsigned?long?int?ipaddr;?//IP地址
char?hostname[MAXSTRINGSIZE];?//主機名
}dnstable;?//高速緩存數據結構
typedef?struct?
{
dnstable?table[MAXSIZE];
int?front;
int?rear;
}sequeue;?
sequeue?*sq;?//定義緩存隊列
sq->rear=sq->front=0;?//初始化隊列
//輸出MAC地址函數
void?print_hwadd(u_char?*?hwadd)
{
for(int?i=0i<5;++i)
printf(“%2x:“hwadd);
printf(“%2x“hwadd);
}
//輸出IP地址的函數
void?print_ipadd(u_char?*ipadd)
{
for(int?i=0;i<3;++i)
printf(“%d.“ipadd);
printf(“%d“ipadd);
}
//查詢端口函數
void?getportname(int?portnochar?portna[]char*?proto)
{
if(getservbyport(htons(portno)proto)!=NULL)
{
strcpy(portnagetservbyport(htons(portno)proto)->s_name);
}
else
sprintf(portna“%d“portno);
}
//將IP轉化為DNS名
void?iptohost(unsigned?long?int?ipadchar*?hostn)
{
struct?hostent?*?shostname;
int?mni;
m=sq->rear;
n=sq->front;
for(i=n%MAXSIZE;i=m%MAXSIZE;i=(++n)%MAXSIZE)
{
//檢查IP是否第一次出現
if(sq->table.ipaddr==ipad)
{
strcpy(hostnsq->table.hostname);
break;
}
}
if(i=m%MAXSIZE)
{//不存在則從域名服務器查詢并把結果放入高速緩存
if((sq->rear+1)%MAXSIZE=sq->front)?//判隊滿
sq->front=(sq->front+1)%MAXSIZE;?//出隊列
sq->table.ipaddr=ipad;
shostname=gethostbyaddr((char*)&ipadsizeof(ipad)AF_INET);
if(shostname!=NULL)
strcpy(sq->table.hostnameshostname->h_name);
else
strcpy(sq->table.hostname““);
sq->rear=(sq->rear+1)%MAXSIZE;
}
}
void?print_hostname(u_char*?ipadd)
{
unsigned?long?int?ipad;
char?hostn[MAXSTRINTSIZE];
ipad=*((unsigned?long?int?*)ipadd);
iptohost(ipadhostn)
if(strlen(hostn)>0)
printf(“%s“hostn);
else
print_ipadd(ipadd);
}
//處理數據包的函數
void?packet_proce(u_char*?packetsconst?struct?pcap_pkthdr?*?headerconst?u_char?
*pp)
{
struct?ether_header?*?eth;?//以太網幀報頭指針
struct?ether_arp?*?arth;?//ARP報頭
struct?ip?*?iph;?//IP報頭
struct?tcphdr?*?tcph;
struct?udphdr?*?udph;
u_short?srcportdstport;?//端口號
char?protocol[MAXSTRINGSIZE];?//協議類型名
char?srcp[MAXSTRINGSIZE]dstp[MAXSTRINGSIZE];?//端口名
unsigned?int?ptype;?//協議類型變量
u_char?*?data;?//數據包數據指針
u_char?tcpudpdata[MAXSTRINGSIZE];?//數據包數據
int?i;
eth=(struct?ether_header?*)pp;
ptype=ntohs(((struct?ether_header?*)pp)->ether_type);
if((ptype==ETHERTYPE_ARP)||(ptype==ETHERTYPE_RARP))
{
arph=(struct?ether_arp?*)(pp+sizeof(struct?ether_header));
if(ptype==ETHERTYPE_ARP)
printf(“arp?“);
else
printf(“rarp?“);?//輸出協議類型
print_hwadd((u_char?*)&(arph->arp_sha));
printf(“(“);
print_hostname((u_char?*)&(arph->arp_spa));
printf(“)->“);
print_hwadd((u_char?*)&(arph->arp_tha));
printf(“(“);
print_hostname((u_char?*)&(arph->arp_tpa));
printf(“)tpacketlen:%d“header->len);
}
else?if(ptype==ETHERTYPE_IP)?//IP數據報
{
iph=(struct?ip?*)(pp+sizeof(struct?ether_
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5155??2011-12-08?17:17??testpcap.c
-----------?---------??----------?-----??----
?????????????????5155????????????????????1
- 上一篇:PDFLib v4.0.2.0珍藏版
- 下一篇:Geany暗黑主題文件
評論
共有 條評論