lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] Problem With dns.c Using 32-Bit Compilers


From: Jonathan Larmour
Subject: Re: [lwip-users] Problem With dns.c Using 32-Bit Compilers
Date: Wed, 27 Aug 2008 18:34:28 +0100
User-agent: Thunderbird 1.5.0.12 (X11/20070530)

Marek Matej wrote:
> This is mentioned structure:
> struct dns_answer {
>   /* DNS answer record starts with either a domain name or a pointer
>      to a name already present somewhere in the packet. */
>   u16_t type;
>   u16_t class;
>   u32_t ttl;
>   u16_t len;
> } PACK_STRUCT_STRUCT;
> 
> and I think situation could be desribed like this:
> 
> assume we have variable
> struct dns_answer dnsa;
> while allocating variable "dnsa" 32bit compiller will probably use
> starting address aligned to 4bytes, and is unable to effectively use
> remaining 2bytes for another variable to allocate. This result to
> alocating 12bytes instead of 10bytes.

That's nearly but not quite the reason. The alignment requirements of a
u32_t are that it must be 4-byte aligned, which means that the structure as
a whole must be 4-bytes aligned. The C standard mandates that you should be
able to put data types in an array, and be able to refer to succeeding
members by adding sizeof(that type). So to maintain alignment sizeof(struct
dns_answer) must be 12. At least on a target where u32_t must be 4-byte
aligned - in other words nearly all of them.

The problem this causes in dns.c is because the data after the answer
record is addressed using something like: (answer_packet + sizeof(struct
dns_answer)).

One workaround may be to dispense with the uses of sizeof for those DNS
records in that file, and replace them with e.g.
#define SIZEOF_DNS_ANSWER 10
and use that instead. The format and size is fixed after all.

Nick, if you find that works for you please feel free to submit a patch you
can confirm works at http://savannah.nongnu.org/patch/?group=lwip and if
so, please do it for the other DNS structures in dns.c too. Thanks.

Jifl

> 2008/8/27 Nick Thomas <address@hidden>:
>>
>> -----Original Message-----
>> From: address@hidden
>> [mailto:address@hidden Behalf
>> Of Jonathan Larmour
>> Sent: 27 August 2008 17:24
>> To: Mailing list for lwIP users
>> Subject: Re: [lwip-users] Problem With dns.c Using 32-Bit Compilers
>>
>>
>> Nick Thomas wrote:
>>> I have the same problem as another user on this mailing list.
>>> Again, is to do with the size of the structure dns_answers in dns.c .
>>>
>>> sizeof(struct dns_answers) reports 12. But it really needs to be 10 to
>> work.
>>> I am using an ST5119 chip, and there doesn't seem to be much control over
>>> the structure padding/alignment.
>> Then this may not be the only problem you have. You need to look at your
>> compiler documentation (you didn't say which compiler it was) and see what
>> it requires for implementing things like packing and alignment. Most do.
>> GCC uses __attribute__ things, but most other compilers use #pragma so it's
>> worth looking for that.
>>
>> Hi, I am using st20cc.
>> The compiler documentation details -falignN on the command line. I am
>> currently using -falign1 . The documentation says that this can be used to
>> ensure alignment to 1 byte.
>> However, it doesn't explain the extra padding at the end of the struct!
>>
>>
>> Regards
>>
>> Nick
>>
>> Jifl


-- 
eCosCentric Limited      http://www.eCosCentric.com/     The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK.       Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["Si fractum non sit, noli id reficere"]------       Opinions==mine




reply via email to

[Prev in Thread] Current Thread [Next in Thread]