Objective
Because of the increasing interconnection terminals, the IPv4 address length (32-bit) has not been able to meet the requirements, so the IPV6 address (128-bit), but the majority of existing applications are in IPV4 address form, it is necessary to resolve the interaction between IPV4 and IPV6, so that the existing IPV4-based Applications can communicate with IPV6-based applications. So how do we implement communication between the IPV4 client and the IPV6 server, the IPV6 client, and the IPV4 server.
IPV4 Client and IPV6 server
Assuming that our host is running two stacks, that is, there is the IPV4 protocol stack and the IPV6 protocol stack, the IPV6 server on the dual-stack host can handle both the IPV4 client and the IPV6 client, because IPV4 can be mapped to a IPV6 address. Is the communication process between the IPV4 client and the IPV6 server:
The IPV6 server program creates sockets bound to the IPV6 wildcard address and TCP port number 9999. Assuming both the client and the server host are on the same ethernet, when the two clients on the left send a SYN segment request to establish a connection to the server, the IPV4 client host loads the Syn,ipv6 client host in a IPV4 datagram to load the SYN in a IPV6 datagram. Ethernet header, IP header, TCP header, and TCP data are included on the Ethernet cable, depending on the Type field contained in the Ethernet header to differentiate whether the IP type is IPv4 or IPV6, so the destination IP address format in the IP header is divided into IPv4 address and IPV6 ground based on the Ethernet type field Access. The TCP header for both is the same, and the TCP header contains the destination port number (that is, the port number of the IPV6 server 9999).
The receiving data link for the server passes each frame to the appropriate IP module by looking at the Ethernet Type field. The IPV4 module, combined with its TCP module, detects that the destination port of the IPV4 datagram corresponds to a IPv6 socket, and translates the source IPv4 address in the datagram IPv4 header into an equivalent IPV4-mapped IPv6 address. When the Accept system call returns this accepted IPV4 client connection to the server process, the mapped address is returned to the server's IPV6 socket as the client's IPV6 address (that is, the server does not know that it is communicating with the IPV4 client, and the client is unaware of itself and IPV6 server communication), the rest of the datagram on the connection is a IPV4 datagram. For the IPV6 client, when the Accept system call returns the accepted IPV6 client connection to the server process, the client's IPV6 address is the source address in the original IPV6 header and does not need to be mapped, and the rest of the datagrams on that connection are IPV6 datagrams.
The steps for communicating between the IPV4 TCP client and the IPV6 TCP server are as follows:
- Start the IPV6 server first, create a IPV6 listener socket, and the server binds the wildcard address and port number 9999 to the socket;
- IPV4 client calls the GetHostByName function to find a record of the server host, the server contains both A and AAAA records, that is, supporting both IPV4 and IPV6, for the IPV4 client only need A record;
- The IPV4 client calls the Connect function to make a connection request to the server that the client host sends a IPV4 SYN datagram to the server host (the IPV4 destination in the SYN is the IPV6 socket);
- After the server host receives a SYN datagram from the client's IPv4, set a flag that indicates that the connection should use the IPV4 mapped IPV6 address and respond to a IPv4 syn and ACK datagram. When the link is established, the IPV6 address of the IPV4 map is returned to the server by the Accept function;
- When the server host sends a TCP segment to the IPV6 address of the IPV4 map, its IP stack generates a IPV4 that has the destination address for the mapped IPv4 address to send datagrams. That is, all communication between the client and the server uses IPV4 to send the datagram;
IPV6 Client and IPV4 server
The steps for communicating between the IPV6 TCP client and the IPV4 TCP server are as follows:
- Start the IPV4 server first, create a IPV4 listener socket;
- The IPV6 client calls the Getaddrinfo function to find the IPV6 address;
- The IPV6 client calls the Connect function to make a connection request to the server after setting the IPV6 address of the IPV4 map in the IPV6 socket address structure as a function parameter, and automatically sends a IPV4 SYN datagram to the server host after the kernel detects the mapped address;
- When the server host receives a SYN datagram from the client's IPv4, it responds to a IPv4 syn and ACK datagram. The connection is established by using the IPV4 datagram;
Summarize
The IPV6 server on the dual-stack host can serve both IPV4 customers and IPV6 customers. IPV4 the client sends to such a server is still the IPV4 datagram, but the server's protocol stack will convert the address of the client host to a IPV4 mapped IPv6 address. Similarly, the IPV6 client on a dual-stack host can communicate with the IPV4 server, and the client's parser returns all the A records of the server host as IPV6 addresses of the IPV4 mappings to the customer, and the customer specifies that one of these addresses calls connect will cause the double stack to send a IPv4 SYN Data report. To make socket programming portable, avoid using the gethostbyname and GETHOSTBYADDR functions as much as possible during the programming implementation, instead using the getaddrinfo and Getnameinfo functions.
Resources:
"Unix Network Programming"
"Network Programming" IPV4 and IPV6 interoperate