Skip to content
Advertisement

Getting IP address from struct sockaddr doesn’t work for 32bit compilation

I have a small client/server app that sends/receives UDP discovery packets. When a UDP packet is received I want to display the source IP. The client/server code is based on the UDP example from Beej: https://beej.us/guide/bgnet/html/multi/clientserver.html

When I compile for 64bit the IP is displayed as expected but when I compile for 32bit (-m32 option) it doesn’t get the right value at all.

Code snippit:

JavaScript

Correct output when compiled for 64bit:

JavaScript

Wrong output when compiled with -m32:

JavaScript

I don’t really understand why there is a problem using get_in_addr or inet_ntop when compiling for 32bit?

Advertisement

Answer

addr_len should be set to the size of the address structure, not the size of the pointer (it’s happening to work in 64 bit mode, because pointers are twice as large in that mode). So it should be:

JavaScript

Incidentally, how is their_addr initialised? There has to be a real structure to point to.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement