TCP/IP Programming Manual
1. The application calls getaddrinfo and passes the hostname (host1), the AF_INET6 address
family hint, and the AI_ADDRCONFIG and AI_V4MAPPED flag hints. The flag hints tell the
function that if an IPv4 address is found for host1, return it as an IPv4-mapped IPv6 address.
See addrinfo for a description of hints fields and values.
2. The search finds an IPv4 address, 1.2.3.4, for host1 in the hosts database, and getaddrinfo
returns the IPv4-mapped IPv6 address ::ffff:1.2.3.4 in one or more structures of type addrinfo.
3. The application calls socket to create an AF_INET6 socket, using the address family and socket
type contained in the addrinfo structure. The socket is a datagram socket (UDP) in this example,
but could be a stream socket (TCP).
4. If the socket call is successful, the application calls connect to establish a connection to host1,
using the host address and length in the addrinfo structure. If the connect call is successful,
the application sends information to the ::ffff:1.2.3.4 address.
NOTE: After using the information in the addrinfo structures, the application calls freeaddrinfo
to free system resources used by the structures.
5. The socket layer passes the information and address to the UDP module.
6. The TCP module identifies the IPv4-mapped IPv6 address, puts the 1.2.3.4 address into the
packet header, and passes the information to the IPv4 module for transmission.
From this point, the application can do the following:
• Call recv to wait for a response from the server system.
• After the application receives a response, call getpeername to determine the address of the
connected socket. The address is returned in a structure of type sockaddr_in6. If you want
your application to be protocol-independent, use the sockaddr_storage structure instead of
the sockaddr_in6 structure.
• Call getnameinfo using the NI_NAMEREQD flag to obtain the server name.
• Call getnameinfo using the NI_NUMERICHOST flag to convert the server address to a text
string. Chapter 5 contains sample client program code that demonstrates these steps.
Using AF_INET6 Guardian Sockets to Receive Messages
AF_INET6 sockets can receive messages sent to either IPv4 or IPv6 addresses. An AF_INET6 socket
uses the IPv4-mapped IPv6 address format to represent IPv4 addresses. Figure 2-3 shows the
sequence of events for a server application that uses an AF_INET6 socket to receive IPv4 packets.
Using AF_INET6 Guardian Sockets to Receive Messages 51