ARP(地址解析协议)结构未对齐 [英] ARP(Address resolution protocol) structure mis-aligned

查看:100
本文介绍了ARP(地址解析协议)结构未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示ARP(地址解析协议)的结构,但内存中的字节未对齐,它是一个运行32位操作系统的64位处理器。



结构为28字节,如下所示:



ARP结构:



I have a structure to represent the ARP (address resolution protocol) but the bytes in memory are misaligned, its a 64 bit capable processor running 32 bit OS.

The structure is 28 byte and is as follows:

ARP structure:

unsigned short hardware;     //could use USHORT but like it this way
unsigned short protocol;
unsigned char Haddr;
unsigned char prot_addr;
unsigned short operation;
unsigned char Sender_MAC[6];
unsigned long int src_addr;
unsigned char Receiver_MAC[6];
unsigned long int dst_addr;





虽然它是28字节(可被4整除)结构,但编译器增加了额外的4字节填充(是因为它支持64位的处理器?)。

如果这就是原因那么为什么我用于以太网的结构不是14字节有任何填充?



以太网结构:





Althought it's 28 bytes (divisible by 4) structure , the compiler adds extra 4 byte padding(is it because of 64 bit capable processor?).
If that is the reason then why isn't the structure I use for Ethernet which is 14 bytes have any padding?

Ethernet structure:

unsigned char srcmac[6];
unsigned char dstmac[6];
unsigned short ip;







在ARP结构中,内存中的字节很好,直到Sender_MAC然后全部精神上。

任何帮助都将不胜感激。





PS我可以提供wireshark嗅探看看所有混乱的数据,万一。




In the ARP structure the bytes in memory are fine until Sender_MAC then they all go mental.
Any help would be greatly appreciated.


PS I can provide the wireshark sniff to see all the jumbled data, in case.

推荐答案

数据对齐是一个简单的答案。



A char是单个字节,因此它可以在奇数或偶数字节边界上对齐 - 字符的地址可以是奇数或偶数。

一般来说,任何非字节长的数据类型都需要在偶数地址上对齐(或更长时间取决于编译器和选项)。

所以当你的结构包含:

Data alignment is the simple answer.

A char is a single byte, so it can be aligned on an odd or an even byte boundary - the address of the character can be odd or even.
Generally speaking any datatype which is not byte long needs to be aligned on an even address (or longer depending on the compiler and options).
So when your structure contains:
unsigned short operation;
unsigned char Sender_MAC[6];
unsigned long int src_addr;
unsigned char Receiver_MAC[6];
unsigned long int dst_addr;

然后至少额外字节将被添加到填充寻址到 long 值的合适地址 - 可能是一个4字节的地址,每个地址需要2个额外的字节(因为早期数据不会在正确的位置结束。



如果您需要与外部结构完全匹配,请使用pack [ ^ ] pragma覆盖默认行为。

then at the very least extra bytes will be added to "pad" the addressing out to a suitable address for the long values - probably a 4 byte address, which will require 2 extra bytes for each (since the earlier data does not "end" in the right place.

If you need to exactly match an external structure, then use the pack[^] pragma to override the default behaviour.


这篇关于ARP(地址解析协议)结构未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆