lwip-users
[Top][All Lists]
Advanced

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

Re: [lwip-users] struct ip_addr VS struct in_addr


From: Jonathan Larmour
Subject: Re: [lwip-users] struct ip_addr VS struct in_addr
Date: Fri, 29 Aug 2008 12:36:36 +0100
User-agent: Mozilla Thunderbird 1.0.8-1.1.fc3.4.legacy (X11/20060515)

Ceresoli Luca wrote:

For this I am trying to remove lwIP-specific code where possible from
the API of my code. The most notable part of this work seems to be
removing any usage of struct ip_addr (lwIP-specific), perhaps in favor
of struct in_addr (portable).
[snip]

int my_api_call(const struct in_addr *dst_in_addr, ...)
{
  struct ip_addr dst_ip_addr;
  ...
  memcpy(&dst_ip_addr, &dst_in_addr->s_addr, sizeof(dst_ip_addr.addr)); // 
<---- look here
  ret = udp_sendto(pcb, p, &dst_ip_addr, dst_port);
  ...
}

I wonder if my approach is correct and if something better than
memcpy() can be done (my CPU does not support unaligned access).

dst_in_addr will already be aligned, so does not need memcpy. You can just do:
dst_ip_addr.addr = dst_in_addr->addr;

However in the other direction - if there's an ip_addr you need to convert back to a struct in_addr, then you /would/ need memcpy, to safely guarantee alignment.

A more fancy idea: rename the unique field of ip_addr from addr to
s_addr, so one struct can be simply be type-cast to the other. I bet
there's some packing issue about this
(http://savannah.nongnu.org/bugs/?21664#comment4) but I'm quite unaware
of API implementation so I can't judge.

Yes there would be a packing issue.

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.
------["The best things in life aren't things."]------      Opinions==mine




reply via email to

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