2

I have the following code copied from elsewhere to get the client port from a server socket connection. On a 64bit Linux intel arch this compiles with out issues,

struct sockaddr        *sa;
struct sockaddr_in     *sin;
unsigned short          sin_port;

sa = conn->local_sockaddr;
sin = (struct sockaddr_in *) sa;
sin_port = ntohs(sin->sin_port);

In Arm its complaining that 'error: cast increases required alignment of target type' which seems reasonable.

I don't think I can ignore this. Is there some way to tell GCC to fix the alignment so I can read the port?

teknopaul
  • 6,505
  • 2
  • 30
  • 24
  • 1
    See the question https://stackoverflow.com/questions/35551879/cast-from-sockaddr-to-sockaddr-in-increases-required-alignment – aneesh jose Dec 08 '17 at 01:30
  • 1
    See the "why you can ignore it" section of the accepted answer to the question linked by @aneeshjose. Try casting to void* first. – Galen Dec 08 '17 at 01:34
  • I don't think ignoring warnings is ever good advice for C code. I added my final implementation based on memcpy to align the data to the linked question. I think you should re-evaluate if these are the same question. I added the tag ARM. https://stackoverflow.com/questions/35551879/cast-from-sockaddr-to-sockaddr-in-increases-required-alignment/47727876#47727876 The answer is not the best one to that OP but is relevant to this question. – teknopaul Dec 09 '17 at 11:03

0 Answers0