views:

33

answers:

2

I am adding IPv6 support to a network application. Some control packets should contain a mixed list of both IPv4 and IPv6 addresses (the current code only supports IPv4):

struct control_packet {
    [...]
    ushort          ip1_afi;
    struct in_addr  ip1;
    ushort          ip2_afi;
    struct in_addr  ip2;
    uchar           reserved1;
    uchar           prefix_mask_len;
    ushort          prefix_afi;
    struct in_addr  prefix;
} __attribute__ ((__packed__));

How should I replace in_addr structures in oder to support both IPv4 and IPv6, depeding an the AFI value before?

I don't think I can use sockaddr_storage for this, because it reserves more space than is necessary for the packet.

I have seen people using uchar ip1[0], but then I will probably have to assemble the packet manually, not using a struct. Any suggestions?

I also accept good RTFM links :)

Thanks!

+2  A: 

You can simply use a union of struct in_addr and struct in6_addr in place of the struct in_addr you have now.

caf
A: 

It seems to me that basically the sockaddr structure already implements something in the same line of thoughts; the sa_family member tells whether this is ip4 or ip6.

I'd go for something that glues together your afi and in_addr fields into one proper abstraction of an ip[46] address.

Jens Gustedt