Skip to contentSkip to conversation

Memory alignment issue in a network protocol header

DISCUSSION Eus 1 participant 1 message Apr 18, 2008

Subsystems (2)
lore
Share & tools
lore

Eus

author
#0 lore raw

Hi Ho!

I am implementing a networking protocol (http://sourceforge.net/projects/atn).
This networking protocol has the following header:

struct clnphdr {
	__u8 cnf_proto_id;
	__u8 cnf_hdr_len;
	__u8 cnf_vers;
	__u8 cnf_ttl;
	__u8 cnf_flag;
	__be16 cnf_seglen;
	__u8 cnf_cksum_msb;
	__u8 cnf_cksum_lsb;
	__u8 dest_len;
	__u8 dest_addr[20];
	__u8 src_len;
	__u8 src_addr[20];
	__u8 next_part[0];
};

Because of 4-byte memory alignment in i386, there will be 1-byte padding in `cnf_seglen'.

This alignment prevents me from doing:

        struct clnphdr clnph = (struct clnphdr *) skb->nh.raw;

because ntohs(clnph->cnf_seglen) will return the wrong result.

The wrong result will occur because in `skb->nh.raw' the data is packed (no hole).

The easiest solution will be to put __attribute__((packed)) in the definition of struct clnphdr, but this will incur performance penalty, won't this?

So, how should I handle this problem in the proper way as to permit portability to 64-bit machines as well?

Thank you very much.

Best regards,
Eus

      ____________________________________________________________________________________

Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ