Reputation: 641
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!
Upvotes: 1
Views: 95
Reputation: 78913
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.
Upvotes: 0
Reputation: 239041
You can simply use a union
of struct in_addr
and struct in6_addr
in place of the struct in_addr
you have now.
Upvotes: 2