///////////////////////////////////////////////////////////////////////////////
//I declare that the assignment here submitted is original except
//for source material(WinpCap Documentation) explicitly acknowledged.
//I also acknowledge that I am aware of University policy and regulations on
//honesty in academic work,and of the disciplinary guidelines and procedures
//applicable to breaches of such policy and regulations.
//
// 孙佳佳 103520430112002 2010年10月10日
//
// 项目名: Exer2_ARPing 开发平台: Windows Server 2003
// 开发环境: Visual Studio 2008 语言:C++
// arping.cpp
////////////////////////////////////////////////////////////////////////////////
#include "arping.h"
//初始化设备类中的静态数据成员
u_int Device::inputIP = 0;
Device::Device()
{
this->Packet = NULL;
this->device = NULL;
this->localMacAddr[0] = 0;
this->outputMAC[0] = 0;
this->localIpAddr = 0;
}
Device::~Device()
{
delete Packet;
}
void Device::BuildPack()
{
static ARPFrame_t ARPFrame; //建立一个ARP数据包留在内存中,使用时只需要改动部分就可使用
ARPFrame.RecvIP = localIpAddr;
ARPFrame.SendIP = htonl(0x55555555);
int i;
for(i=0; i<6; i++)
{
ARPFrame.DesMAC[i] =0xff;
ARPFrame.SrcMAC[i] = 0x66;
ARPFrame.SendMAC[i] = 0x66;
//ARPFrame.RecvMAC[i] = 0x00; //建立静态对象时已经初始化为零
}
ARPFrame.FrameType = htons(0x0806);
ARPFrame.HardwareType = htons(0x0001);
ARPFrame.ProtocolType = htons(0x0800);
ARPFrame.HardwareLen = 6;
ARPFrame.ProtocolLen = 4;
ARPFrame.Operation = htons(0x0001);
//ARPFrame.pading[0] = 0; //建立静态对象时已经初始化为零, 也可以没有Padding部分
this->Packet = &ARPFrame;
}
void Device::ChoseDev(void)
{
pcap_if_t *alldevs; //指向设备链表首部的指针
/* Retrieve the device list on the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, NULL) == -1)
{
cout<<"Error in pcap_findalldevs.\n";
return;
}
int i=0;
/* Print the list */
for (device=alldevs; device; device=device->next)
{
cout<<++i<<"."<<device->name<<endl;
if (device->description)
{
cout<<" "<<device->description<<endl;;
}
else
{
cout<<"No description available)\n";
return;
}
}
if (i==0)
{
cout<<"\nNo interfaces found! Make sure WinPcap is installed.\n";
return;
}
int inum ;
cout<<"Enter the interface number (1-"<<i<<") :";
cin>>inum;
if (inum < 1 || inum > i)
{
cout<<"\nInterface number out of range.\n";
/* Free the device list */
pcap_freealldevs(alldevs);
exit(1);
}
/* Jump to the selected adapter */
for (device=alldevs, i=0; i< inum-1 ;device=device->next, i++);
}
void Device::GotMyIpAddr(void) //获得本机IP的两种形式
{
/*
//获得4个字节的IP地址
int i;
for(i=0;i<4;i++)
{
localIpAddr[i]=device->addresses->addr->sa_data[i+2];
}
*/
//获得U_long类型的IP地址
localIpAddr = ((struct sockaddr_in *)(device->addresses)->addr)->sin_addr.s_addr;
cout<<"\t"<<Device::IpToStr(localIpAddr)<<"\t\t"<<"Got my IP Success ! "<<endl;
}
void Device::GotMyMacAddr(void) //获得选择网卡的MAC地址
{
pcap_t* pAdaptHandle; //打开网络接口函数pcap_open()的返回值,Descriptor of an open capture instance
char errbuf[PCAP_ERRBUF_SIZE + 1]; //错误信息
if((pAdaptHandle = pcap_open_live(device->name, //设备名称
60, //抓包的最大字节数
0, //设置网卡为 混杂模式(其实没必要,为0也可以获得自己的MAC地址)
100, //timeout 设置
errbuf)) == NULL)
{
cout<< "无法打开适配器,可能与之不兼容!\n";
return;
}
BuildPack(); //构建数据包
struct pcap_pkthdr *header; //数据包头,包含三个数据struct timeval ts ,caplen,len
const u_char *pkt_data; //收到的数据包
if (pcap_sendpacket(pAdaptHandle, (const u_char *)Packet,sizeof(ARPFrame_t)) != 0)
{
cout<<"Send the Package failed"<<endl;
return;
}
char flag = 0;
while(!flag)
{
int res;
//This function is used to retrieve the next available packet
res = pcap_next_ex(pAdaptHandle, &header, &pkt_data);
if (res == 0) //抓包发生超时
{
continue;
}
else if (res == 1) //正常收到包的情况
{
ARPFrame_t *ARPPacket = (ARPFrame_t *)pkt_data;
if ((ARPPacket->FrameType == htons(0x0806))
&&(ARPPacket->Operation== htons(0x0002))
&&(ARPPacket->SendIP== localIpAddr))
{
cout<<"\t";
int i;
for(i=0; i<6; i++)
{
localMacAddr[i] =ARPPacket->SendMAC[i];
cout<<hex<<localMacAddr[i] + 0<<"-";
}
cout<<"\b\t"<<"Got My MacAddr success ! \n"<<endl;
flag = 1;
pcap_close(pAdaptHandle);
}
else
continue;
}
else
{
cout<<"Error when catch a Packet!";
flag = 1;
pcap_close(pAdaptHandle);
}
}
}
void Device::SendRecvPack(void)
{
//将之前虚拟的数据包 更改4个参数后 即可获取输入IP所对应的MAC地址
int i;
for(i=0; i<6; i++)
{
Packet->SrcMAC[i] = localMacAddr[i];
Packet->SendMAC[i] = localMacAddr[i];
}
Packet->SendIP = localIpAddr;
Packet->RecvIP = inputIP;
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts; //用来计算接收包的延迟时间
u_int netmask; //地址掩码 用来过滤数据包
struct bpf_program fcode;
/* Open the output adapter */
if ( (fp= pcap_open(device->name, 100, 0, 1000,NULL,errbuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
return;
}
netmask = 0xffffffff;
//compile the filter 配置过滤器,只抓取ARP数据包
if (pcap_compile(fp, &fcode, "ether proto\\arp", 1, netmask) <0 )
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* Free the device list */
return;
}
//set the filter 激活过滤器
if (pcap_setfilter(fp, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter.\n");
pcap_close(fp);
return;
}
i = 4;
//发送四个IP请求包
while (i--)
{
if (pcap_sendpacket(fp, (const u_char *)Packet,sizeof(ARPFrame_t)) != 0)
{
cout<<"Send the Package failed"<<endl;
return;
}
}
cout<<"发送端MAC"<<"\t\t"<<"发送端IP"<<"\t"<<"接收端MAC"<<"\t\t"<<"接收端IP"<<endl;
/* Start the main loop */
pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);
pcap_close(fp);
return;
}
//抓到过滤后的数据包后的回调函数
void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
/* 时间延迟没能转化为正常时间模式
struct timeval *old_ts = (struct timeval *)state;
u_int delay;
delay=(header->ts.tv_sec - old_ts->tv_sec)*1000 - old_ts->tv_usec + header->ts.tv_usec;
*/
//将接收到的数据包强制转化为ARP数据包结构
ARPFrame_t *ARP = (ARPFrame_t *)pkt_data;
int i;
if (ARP->Operation== htons(0x0002))
{
for (i=0; i<6; i++)
{
cout<<ARP->SendMAC[i]+0<<":";
}
cout<<"\b\t"<<Device::IpToStr(ARP->SendIP)<<"\t";
for (i=0; i<6; i++)
{
cout<<ARP->RecvMAC[i]+0<<":";
}
cout<<"\b\t"<<Device::IpToStr(ARP->RecvIP)<<endl;
//输出延迟时间(ms)
//cout<<dec<<"Time Delay : "<<delay<<"ms\n"<<endl;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Exer2_ARPing.7z (46个子文件)
Exer2_ARPing
Exer2_ARPing.sln 902B
Debug
Exer2_ARPing.exe 46KB
Exer2_ARPing.ilk 569KB
Exer2_ARPing.pdb 731KB
Exer2_ARPing
main.cpp 441B
Include
ip6_misc.h 6KB
pcap-namedb.h 2KB
pcap-stdinc.h 3KB
pcap-bpf.h 2KB
Packet32.h 17KB
remote-ext.h 15KB
bittypes.h 4KB
pcap
namedb.h 3KB
usb.h 3KB
bpf.h 28KB
bluetooth.h 2KB
vlan.h 2KB
pcap.h 14KB
sll.h 5KB
pcap.h 2KB
Win32-Extensions.h 4KB
arping.cpp 7KB
Exer2_ARPing.vcproj.jasun-PC.jasun.user 1KB
arping.h 2KB
Exer2_ARPing.vcproj.杨坤-PC.杨坤.user 1KB
Exer2_ARPing.vcproj 4KB
arp.h 2KB
Debug
vc90.idb 883KB
BuildLog.htm 7KB
Exer2_ARPing.exe.intermediate.manifest 621B
Exer2_ARPing.exe.embed.manifest 663B
mt.dep 67B
main.obj 59KB
vc90.pdb 260KB
arping.obj 81KB
Exer2_ARPing.exe.embed.manifest.res 728B
BAT00000728282916.bat 71B
Exer2_ARPing.vcproj.MITI.lilin.user 1KB
Lib
libwpcap.a 53KB
libpacket.a 20KB
x64
wpcap.lib 18KB
Packet.lib 8KB
wpcap.lib 19KB
Packet.lib 8KB
arp.cpp 7KB
Exer2_ARPing.suo 70KB
共 46 条
- 1
yamhoney
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页