[cs631apue] Final Project Socket IPv6 Configuration

Jan Schaumann jschauma at stevens.edu
Thu Nov 8 23:41:13 EST 2018

Keyur M Ved <kved at stevens.edu> wrote:
> To set up the socket, I'm using the sockaddr_in6 with the PF_INET6
> family and in6addr_any as the default address. Does this configuration
> also support IPv4 or do I need to do something else to support both?

An IPv6 socket can be set to also accept IPv4 connections by setting the
'IPV6_V6ONLY' option to 0 (see setsockopt(2) and ip6(4)).  In that case,
the socket will be dual stack and sockaddr_in6 will be able to represent
the IPv4 information as a so-called IPv4-mapped address.  That is, it
takes the v4 address and maps it into a v6 address of the format
"::ffff:<ipv4 address here>".

An alternative approach might be to use a struct sockaddr_storage,
although you may still have to perform a check for the right address
family at the very least when calling inet_ntop(3).


More information about the cs631apue mailing list