查看单个帖子
旧 2009-03-22, 10:53 PM   #3
huangyhg
超级版主
 
huangyhg的头像
 
注册日期: 04-03
帖子: 18592
精华: 36
现金: 249466 标准币
资产: 1080358888 标准币
huangyhg 向着好的方向发展
默认 回复: 【转帖】bug分析

结构体对齐问题


//定义的结构体如下:
typedef struct arp_header{
//-----------------------DLC Header----------------------------------------
unsigned char DesMAC[6]; //目标MAC
unsigned char SrcMAC[6]; //源MAC
unsigned short EtherType; //EtherType默认为0x0806是ARP帧的类型值
//-----------------------ARP Frame-----------------------------------------
unsigned short HW_Type; //硬件类型
unsigned short Prot_Type; //上层协议类型
unsigned char HW_Addr_Len; //MAC地址长度
unsigned char Prot_Addr_Len; //IP地址类型
unsigned short Opcode; //操作码 0x01表示ARP请求包 0x02表示RARP应答包

unsigned char Send_MAC_Addr[6]; //发送方MAC
unsigned long Send_IP_Addr; //发送方IP
unsigned char Targ_MAC_Addr[6];//接收方MAC
unsigned long arg_IP_Addr; //接受方IP
unsigned char padding[18]; //填充数据
}ARPHEADER,*PARPHEADER;

填充:
m_ARPHdr = new ARPHEADER;
//--------------------------------填充ARP头--------------------------------
m_ARPHdr->DesMAC[0] = 0Xff;
m_ARPHdr->DesMAC[1] = 0Xff;
m_ARPHdr->DesMAC[2] = 0Xff;
m_ARPHdr->DesMAC[3] = 0Xff;
m_ARPHdr->DesMAC[4] = 0Xff;
m_ARPHdr->DesMAC[5] = 0Xff;

m_ARPHdr->SrcMAC[0] = 0X00;
m_ARPHdr->SrcMAC[1] = 0X08;
m_ARPHdr->SrcMAC[2] = 0X02;
m_ARPHdr->SrcMAC[3] = 0XDB;
m_ARPHdr->SrcMAC[4] = 0X3B;
m_ARPHdr->SrcMAC[5] = 0X95;

m_ARPHdr->EtherType = 0X0608;
// m_ARPHdr->

m_ARPHdr->HW_Type =0X0100; //硬件类型
m_ARPHdr->Prot_Type = 0X0008; //上层协议类型
m_ARPHdr->HW_Addr_Len = 0X06; //MAC地址长度
m_ARPHdr->Prot_Addr_Len =0X04; //IP地址类型
m_ARPHdr->Opcode = 0X0100; //操作码 0x01表示ARP请求包 0x02表示RARP应答包

// m_ARPHdr->Send_MAC_Addr[6]; //发送方MAC
m_ARPHdr->Send_MAC_Addr[0] = 0X00;
m_ARPHdr->Send_MAC_Addr[1] = 0X08;
m_ARPHdr->Send_MAC_Addr[2] = 0X02;
m_ARPHdr->Send_MAC_Addr[3] = 0XDB;
m_ARPHdr->Send_MAC_Addr[4] = 0X3B;
m_ARPHdr->Send_MAC_Addr[5] = 0X95;
m_ARPHdr->Send_IP_Addr = inet_addr("192.168.11.162"); //发送方IP
// m_ARPHdr->Targ_MAC_Addr[6];//接收方MAC
m_ARPHdr->Targ_MAC_Addr[0] = 0X00;
m_ARPHdr->Targ_MAC_Addr[1] = 0X00;
m_ARPHdr->Targ_MAC_Addr[2] = 0X00;
m_ARPHdr->Targ_MAC_Addr[3] = 0X00;
m_ARPHdr->Targ_MAC_Addr[4] = 0X00;
m_ARPHdr->Targ_MAC_Addr[5] = 0X00;
m_ARPHdr->arg_IP_Addr = inet_addr("192.168.11.154"); //接受方IP
//m_ARPHdr->padding[18] ; //填充数据
memset(&(m_ARPHdr->padding[0]),0,18);

//填充后m_ARPHdr 值如下:
//FF FF FF FF FF FF 00 08 02 DB 3B 95 08 06 00 01 08 00 06 04 00 01 00 08 02 DB 3B 95 C0 A8 0B A2 00 00 00 00 00 00 C0 A8 0B 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//转换:
u_char* packet = (u_char*)m_ARPHdr;
//转换后packet等于
//FF FF FF FF FF FF 00 08 02 DB 3B 95 08 06 00 01 08 00 06 04 00 01 00 08 02 DB 3B 95 C0 A8 0B A2 00 00 00 00 00 00 CD CD C0 A8 0B 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

//而且还有一个问题:
int n = sizeof(ARPHEADER);
//等于64;而不是60
为什么C0 A8 0B 02前怎么不是CD CD

posted on 2008-05-25 21:50 isabc 阅读(165) 评论(1) 编辑 收藏 引用 所属分类: BUG分析


评论
# re: 结构体对齐问题 2008-05-25 21:51 isabc
#pragma pack(push)
#pragma pack(1)
typedef struct arp_header{
//-----------------------DLC Header----------------------------------------
unsigned char DesMAC[6]; //目标MAC
unsigned char SrcMAC[6]; //源MAC
unsigned short EtherType; //EtherType默认为0x0806是ARP帧的类型值
//-----------------------ARP Frame-----------------------------------------
unsigned short HW_Type; //硬件类型
unsigned short Prot_Type; //上层协议类型
unsigned char HW_Addr_Len; //MAC地址长度
unsigned char Prot_Addr_Len; //IP地址类型
unsigned short Opcode; //操作码 0x01表示ARP请求包 0x02表示RARP应答包

unsigned char Send_MAC_Addr[6]; //发送方MAC
unsigned long Send_IP_Addr; //发送方IP
unsigned char Targ_MAC_Addr[6];//接收方MAC
unsigned long arg_IP_Addr; //接受方IP
unsigned char padding[18]; //填充数据
}ARPHEADER,*PARPHEADER;
#pragma pack(pop)
__________________
借用达朗贝尔的名言:前进吧,你会得到信心!
[url="http://www.dimcax.com"]几何尺寸与公差标准[/url]
huangyhg离线中   回复时引用此帖