benwad
benwad

Reputation: 6604

Mac/Windows different formats for in_addr structure (IPv4 address)

I'm porting some Windows code to the Mac. I've come across some code using the type in_addr and found the documentation for the Windows implementation here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738571%28v=vs.85%29.aspx

Specifically the problem I am having is that the code tries to access the S_un property of in_addr, which the compiler (CLang) complains doesn't exist. Looking at the definition for the Mac implementation I get this (in usr/include/netinet/in.h):

struct in_addr {
    in_addr_t s_addr;
};

...and in_addr_t is defined as follows (in usr/include/sys/types.h):

typedef __uint32_t      in_addr_t;

Does anyone know of a good workaround for this, or if there is a representation of IPv4 addresses on the Mac that is more closely tied to the Windows implementation?

Upvotes: 1

Views: 2220

Answers (1)

Analog File
Analog File

Reputation: 5316

See my comment. But also ... you can just implement the windows declaration in your code (just do not call it in_addr to avoid conflicts). You could do something like:

 typedef struct Win_in_addr {
   union {
     struct {
       __uint8_t s_b1,s_b2,s_b3,s_b4;
     } S_un_b;
     struct {
       __uint16_t s_w1,s_w2;
     } S_un_w;
     __uint32_t S_addr;
   } S_un;
 } Win_IN_ADDR, *Win_PIN_ADDR, *Win_LPIN_ADDR;

Note however that this is not portable and will only work as intended on little endian CPUs.

Upvotes: 1

Related Questions