山寨:不是最好的,是最适合我们的!欢迎体验山寨 中文版MSDN

Blog @ Blog

当华美的叶片落尽,生命的脉络才历历可见。 -- 聂鲁达

常用链接

统计

积分与排名

BBS

Blog

Web

最新评论

结构体对齐问题

 

//定义的结构体如下: 
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 阅读(593) 评论(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)
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


广告信息(免费广告联系)

中文版MSDN:
欢迎体验