The 12th chapter of UNIX Network Programming learning notes IPV4 and IPV6 interoperability

Source: Internet
Author: User

First, Introduction

Let's assume that the hosts discussed in this chapter support dual stacks, that is, support for IPV4 addresses, and Ipv6 addresses.

We are talking about the point: the client and server side use different types of addresses. Because the same type of address has nothing to say.

Second, IPV4 client and IPV6 server

That is, the client uses the IPV4 address socket to communicate, and the server side uses the IPV6 address socket communication.

Principle:

0. First IPV6 server host to ensure that both IPV4 address and IPV6 address.

1. IPV4 client through the Getaddrinfo function, find the server-side IPV4 address, and then connect.

2. The Syn of the IPV4 from the client arrives at the server side, and the server-side kernel maps this IPv4 syn to IPv6 syn (that is, the IP address of the IPV4 is mapped to the IPV6 IP address) and then to the process. Therefore, the server-side accept, recvfrom socket address are IPv6.

3. Then the server-side process responds with an ACK, the process sends a IPV6 ACK to the kernel, the kernel looks at the destination address and knows that the IPV6 address is mapped. So the kernel is the IPV6 address map sent out for the IPV4 address.

4. In this way, normal communication, that is, in fact, through the IPV4 packet communication. However, neither the server nor the client process is aware of the work done by the server kernel.

Note: This communication cannot be completed if the IPV6 server host does not have an IPv4 address.

Third, IPV6 client and IPV4 server

That is, the client uses the IPV6 address socket to communicate, and the server side uses the IPV4 address socket communication.

Principle:

0. First, there is no IPv6 address in the IPV4 server host.

1. The IPV6 client passes the Getaddrinfo function, and the flags in the hints structure are ai_v4mapped. Then we can get the IPV6 address of the server host's IPV4 address map through the getaddrinfo function.

2. The client then uses this IPV6 address to call Connect, and the kernel detects that the IPV6 address is mapped, so it turns the IPv6 address into a IPV4 address and sends it out.

3. The IPV4 server then receives the IPV4 SYN and responds to the IPV4 ack.

4. After the client receives this IPV4 ACK, the kernel will convert the IPV4 ack to IPV6 ack to communicate.

5. Communication is actually done through the IPV4 package. The server and client processes are not aware. Done by the client core.

Note: If the IPV4 server host has an IPV6 address at this point, the getaddrinfo also returns the original IPV6 address, even though the hints is ai_v4mapped in the structure, so the two parties cannot complete the communication. At this point we can specify that only a record is queried by adding 4 to the host name. So we can communicate.

Four, the wrong combination

Both of these error conditions are because the kernel cannot convert the IPV6 address to an IPV4 address because IPV6 has 128 bits and the IPV4 address is only 32 bits.

To summarize: that

That

1. Obviously if both parties are single-stack hosts, the agreement between the parties must be the same.

2. IPV4 dual-Stack client, unable to communicate with IPV6 's single-stack server. That is, the 1th error condition above.

3. Ipv6 single-Stack customer, unable to communicate with Ipv4 's dual-stack server.

4. Whether IPv6 dual-stack customers can communicate with IPV4 dual-stack customers depends on the implementation, that is, if getaddrinfo obtains the IPV6 address of the IPV4 map, it can communicate. If you get a true IPv6 address, you cannot communicate.

5. In fact, the problem is to achieve the IPv6 of the host, as far as possible to achieve IPv4, so that we can see, remove the second row and column of the table, the table (none) will not be. Only (None *) left.

Five, IPV6 address test macro

There are some IPV6 applications that want to know whether a IPV6 address is IPv4 mapped or is itself a IPv6 address. We can use macros:

#include     <netinet/in.h>int in6_is_addr_v4mapped (const structin6_addr* APT);

For testing.

VI. recommendations

Try to write some functions that are not related to a specific protocol.

Try to use functions unrelated to the specific protocol.

As can be seen from the table above, when we write the server, when the server host supports the dual-stack protocol, the address of the server is written as IPV6 address, so that you can receive any protocol client.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The 12th chapter of UNIX Network Programming learning notes IPV4 and IPV6 interoperability

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.