50分:发送arp包的问题

coffeefish 2002-04-14 09:02:22
我用packet.dll的函数实现构造自己的ARP包
我定义了一个结构,里面定义了unsigned char mac[6]的数祖用于存放mac地址
假设我的mac地址假设为56-34-21-ab-23-11
那么直接memcpy((char 8)mac,"56-34-21-ab-23-11",6),可以吗
我听说要经过一个转换,但是我不知道,有人知道吗
...全文
54 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ExitWindows 2002-04-15
  • 打赏
  • 举报
回复
super_xxx()说的也有理。
111222 2002-04-15
  • 打赏
  • 举报
回复
听exitwindows的没错
ExitWindows 2002-04-15
  • 打赏
  • 举报
回复
要写成
mac[0] = (char)0x34;
mac[1] = (char)0x36;
...
ExitWindows 2002-04-15
  • 打赏
  • 举报
回复
super_xxx():
  按你的做法,要是mac中有一个是0x00岂不是......
super_xxx 2002-04-15
  • 打赏
  • 举报
回复
memcpy(mac,"\x34\x36\xab\x24\x09\x60",6);
coffeefish 2002-04-15
  • 打赏
  • 举报
回复

我的源程序如下:高手们帮忙一下,好吗,谢谢了
(我用的是packet.dll)
但是程序不出现ip冲突的提示


#include "Winsock2.h"
#include "Packet32.h"

#define EPT_IP 0x0800 /* type: IP */
#define EPT_ARP 0x0806 /* type: ARP */
#define EPT_RARP 0x8035 /* type: RARP */
#define ARP_HARDWARE 0x0001 /* Dummy type for 802.3 frames */
#define ARP_REQUEST 0x0001 /* ARP request */
#define ARP_REPLY 0x0002 /* ARP reply */

#define Max_Num_Adapter 10

#pragma pack(push, 1)

typedef struct ehhdr
{
unsigned char eh_dst[6]; /* destination ethernet addrress */
unsigned char eh_src[6]; /* source ethernet addresss */
unsigned short eh_type; /* ethernet pachet type */
}EHHDR, *PEHHDR;


typedef struct arphdr
{
unsigned short arp_hrd; /* format of hardware address */
unsigned short arp_pro; /* format of protocol address */
unsigned char arp_hln; /* length of hardware address */
unsigned char arp_pln; /* length of protocol address */
unsigned short arp_op; /* ARP/RARP operation */

unsigned char arp_sha[6]; /* sender hardware address */
unsigned long arp_spa; /* sender protocol address */
unsigned char arp_tha[6]; /* target hardware address */
unsigned long arp_tpa; /* target protocol address */
}ARPHDR, *PARPHDR;

typedef struct arpPacket
{
EHHDR ehhdr;
ARPHDR arphdr;
} ARPPACKET, *PARPPACKET;

#pragma pack(pop)

LPADAPTER lpAdapter;
LPPACKET lpPacket;
WCHAR AdapterName[2048];
ARPPACKET ARPPacket;
char szPacketBuf[600];
ULONG AdapterLength = 1024;
int AdapterNum = 0;
int nRetCode;

if(PacketGetAdapterNames((char*)AdapterName, &AdapterLength) ==
FALSE)
{
::AfxMessageBox("Unable to retrieve the list of the
adapters!");
return;
}

//我的机子上面只有一个网卡
lpAdapter = (LPADAPTER) PacketOpenAdapter((LPTSTR)AdapterName);
if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
{
nRetCode = GetLastError();
::AfxMessageBox("Unable to open the driver!");
return;
}

lpPacket = PacketAllocatePacket();
if(lpPacket == NULL)
{
::AfxMessageBox("Error:failed to allocate the LPPACKET
structure.");
return;
}

ZeroMemory(szPacketBuf, sizeof(szPacketBuf));
//52-54-ab-12-84-25 是其他人往卡的mac
ARPPacket.ehhdr.eh_src[0] = 0x52;
ARPPacket.ehhdr.eh_src[1] = 0x54;
ARPPacket.ehhdr.eh_src[2] = 0xab;
ARPPacket.ehhdr.eh_src[3] = 0x12;
ARPPacket.ehhdr.eh_src[4] = 0x84;
ARPPacket.ehhdr.eh_src[5] = 0x25;

ARPPacket.ehhdr.eh_dst[0] = 0xff;
ARPPacket.ehhdr.eh_dst[1] = 0xff;
ARPPacket.ehhdr.eh_dst[2] = 0xff;
ARPPacket.ehhdr.eh_dst[3] = 0xff;
ARPPacket.ehhdr.eh_dst[4] = 0xff;
ARPPacket.ehhdr.eh_dst[5] = 0xff;
ARPPacket.ehhdr.eh_type = EPT_ARP;

ARPPacket.arphdr.arp_hrd = htons(ARP_HARDWARE);
ARPPacket.arphdr.arp_pro = htons(EPT_IP);
ARPPacket.arphdr.arp_hln = 0x06;
ARPPacket.arphdr.arp_pln = 0x04;
ARPPacket.arphdr.arp_op = htons(0x0001);

//52-54-ab-12-84-25是其他人网卡的mac
ARPPacket.arphdr.arp_sha[0] = 0x52;
ARPPacket.arphdr.arp_sha[1] = 0x54;
ARPPacket.arphdr.arp_sha[2] = 0xab;
ARPPacket.arphdr.arp_sha[3] = 0x12;
ARPPacket.arphdr.arp_sha[4] = 0x84;
ARPPacket.arphdr.arp_sha[5] = 0x25;
ARPPacket.arphdr.arp_spa = inet_addr("202.119.19.216");//我的局域网ip

ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tha[0] = 0x00;
ARPPacket.arphdr.arp_tpa=inet_addr("202.119.19.216");//以我自己做实验

memcpy(szPacketBuf, (char*)&ARPPacket, sizeof(ARPPacket));
PacketInitPacket(lpPacket, szPacketBuf, 60);

if(PacketSetNumWrites(lpAdapter,2)==FALSE)
{
::AfxMessageBox("warning: Unable to send more than one packet in
a single write!");
return;
}
//循环1000次
for(int q=0;q<=1000;q++)
{
if(PacketSendPacket(lpAdapter, lpPacket, TRUE)==FALSE)
{
::AfxMessageBox("Error sending the packets!");
return;
}
}
coffeefish 2002-04-14
  • 打赏
  • 举报
回复
那么就是写成
memcpy((char *)mac,"0x34-0x36-0xab-0x24-0x09-0x60",6);吗
我试了一下
还是不对阿
ExitWindows 2002-04-14
  • 打赏
  • 举报
回复
不可以。
要把字符串形式转为二进制形式。
56->0x38
34->0x22
....
这样才能有6个字节。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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