一个由 winpcap 发送ARP报文引发的问题

ricci 2008-11-02 08:58:48
#include "stdio.h"
#include "pcap.h"
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"WS2_32.lib")
struct EthernetHeader
{
u_char DestMAC[6];
u_char SourMAC[6];
u_short EthType;
};
struct ArpHeader
{
u_short HardwareType;
u_short ProtocolType;
u_char MACLength;
u_char IpAddLength;
u_short OperationCode;
u_char SourceMAC[6];
unsigned long SourceIP;
u_char DestinationMAC[6];
unsigned long DestinationIP;
};
int main(int argc, char **argv)
{
struct EthernetHeader ethernet;
struct ArpHeader arp;
int Result;
unsigned char SendBuffer[200];
pcap_if_t *NetwokDevice;
pcap_t *WinpcapHandle;
pcap_if_t *Device;
char WinpcapError[PCAP_ERRBUF_SIZE];
int DeviceIndex = 0;
char DeviceName[100][1000];
int i;
Result = pcap_findalldevs(&NetwokDevice, WinpcapError);
if (Result == - 1)
{
printf("pcap_findalldevs Error");
return 0;
} for (Device = NetwokDevice, i = 0; Device && i < 10; Device = Device->next, i++)
{
printf("Number %d:", i);
printf("%s:", Device->name);
printf("%s\n", Device->description);
sprintf(DeviceName[i], "%s", Device->name);
}
for (;;)
{
printf("Please Choose the Device Number:(0-%d)", i - 1);
scanf("%d", &DeviceIndex);
if (DeviceIndex > i - 1 || DeviceIndex < 0)
{
printf("Device Number Error\n");
continue;
}
else
break;
}
WinpcapHandle = pcap_open_live(LPCTSTR(DeviceName[DeviceIndex]), 65535, 1, 1000, WinpcapError);
if (WinpcapHandle == NULL)
{
printf("pcap_open_live error");
pcap_freealldevs(NetwokDevice);
return 0;
}
memset(ðernet, 0, sizeof(ethernet));
memset(ethernet.DestMAC, 0xff, 6);
BYTE hostmac[8];
hostmac[0] = 0x00;
hostmac[1] = 0xe0;
hostmac[2] = 0x4c;
hostmac[3] = 0x03;
hostmac[4] = 0x5b;
hostmac[5] = 0x00;
memcpy(ethernet.SourMAC, hostmac, 6);
ethernet.EthType = htons(0x0806);
memcpy(&SendBuffer, ðernet, 14);
arp.HardwareType = htons(0x0001);
arp.ProtocolType = htons(0x0800);
arp.MACLength = 6;
arp.IpAddLength = 4;
arp.OperationCode = htons(0x0001);
memcpy(arp.SourceMAC, ethernet.SourMAC, 6);

struct in_addr addr1,addr2;
arp.SourceIP=inet_addr("192.168.5.11"); //这里用本机IP,以便于测试 memcpy(&addr1,&arp.SourceIP, 4);
printf("\n --- %s ---\n",inet_ntoa(addr1));

memset(arp.DestinationMAC, 0, 6);
arp.DestinationIP=inet_addr("192.168.0.1"); //随便一个本地IP memcpy(&addr2,&arp.DestinationIP, 4);
printf("\n --- %s ---\n",inet_ntoa(addr2));

memcpy(&SendBuffer[14],&arp, 28);
Result = pcap_sendpacket(WinpcapHandle, SendBuffer, 14+28);
if (Result != 0)
{
printf("Send Error!\n");
}
else
{
printf("Send ARP Packet.\n");
printf("HardwareType:%d\n", ntohs(arp.HardwareType));
printf("ProtocolType:%d\n", ntohs(arp.ProtocolType));
printf("MACLength:%d\n", arp.MACLength);
printf("IpAddLength:%d\n", arp.IpAddLength);
printf("OperationCode:%d\n", ntohs(arp.OperationCode));
printf("SourceMAC:%02x:%02x:%02x:%02x:%02x:%02x\n", arp.SourceMAC[0], arp.SourceMAC[1], arp.SourceMAC[2], arp.SourceMAC[3], arp.SourceMAC[4], arp.SourceMAC[5]);
printf("DestinationMac:%02x:%02x:%02x:%02x:%02x:%02x\n", arp.DestinationMAC[0], arp.DestinationMAC[1], arp.DestinationMAC[2], arp.DestinationMAC[3], arp.DestinationMAC[4], arp.DestinationMAC[5]);
}
pcap_freealldevs(NetwokDevice);
pcap_close(WinpcapHandle);
return 1;
}

关于网协议中的数据类型老弄不清,在发送以上ARP报文的时候没有成功(由于源地址是用本机IP,所以会出现IP冲突)!大虾们看看哪里有问题呀?
...全文
165 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jia_xiaoxin 2008-11-03
  • 打赏
  • 举报
回复
Mark
帅得不敢出门 2008-11-02
  • 打赏
  • 举报
回复
代码好长

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧