当前位置:网站首页>:: ffff:192.168.31.101 what address is it?

:: ffff:192.168.31.101 what address is it?

2022-07-05 13:45:00 Wangwanlin Ben

problem

::ffff:192.168.31.101 What address is it ?

practice

nslookup Check whether there is reverse parsing , Show none .

nslookup ::ffff:192.168.31.101

 ping command , Display is ipa-server-001.icinfra.cn(192.168.31.101) Address ,

 nslookup see ipa-server-001.icinfra.cn Of IP Address , yes 192.168.31.101

  that , As the title ,::ffff:192.168.31.101 What address is it ?

answer

::ffff:192.168.31.101 It's a IPv4-Mapped IPv6 Address.

IPv4-mapped IPv6 The address format is ::FFFF:IPv4-address, for example ::FFFF:192.168.1.1, Running on dual stack devices IPv6 Application and utilization IPv4-mapped IPv6 Addresses can be based on IPv4 Application interworking .

be based on IPv6 The server application of receives a message from IPv4 When the connection request of the client , Internal use IPv4-mapped IPv6 Address to represent this connection .

be based on IPv6 Client application connection IPv4 Server time of , use IPv4-mapped IPv6 Address of access peer .

IPv4-mapped IPv6 The address is only used inside the system , Cannot be set on the network card as IPv6 The address of , It will not appear in the source or destination of network packets IP In the address field .

Reference material

IP Network application migration in dual stack environment - Xinhua Group 3 -H3C

1. Background introduction

IPv4 towards IPv6 There are many ways to migrate , When choosing a specific migration method , Whether the application running in the current environment supports IPv6 Is one of the important considerations , At the same time, when writing new applications , We need to consider that the newly compiled application can not only adapt to the current mainstream IPv4 Environmental Science , It should also be able to run on IPv6 Environmental Science . At present, the main ICT Infrastructure supports IPv6, However, there are still many customer applications based on IPv4 Environment developed , If the application is not modified to support IPv6 In the case of access , All migration methods are hard to say is complete . This essay is intended to be correct IPv4 Apply to IPv6 Briefly introduce the relevant contents involved in application migration , There are many ways to transform double stacks , This article takes ipv4- mapped ipv6 For example , Expect to understand the need IPv6 Readers of application migration are helpful .

2. IPv4-mapped IPv6 Address

IPv4-mapped IPv6 The address format is ::FFFF:IPv4-address, for example ::FFFF:192.168.1.1, Running on dual stack devices IPv6 Application and utilization IPv4-mapped IPv6 Addresses can be based on IPv4 Application interworking .

be based on IPv6 The server application of receives a message from IPv4 When the connection request of the client , Internal use IPv4-mapped IPv6 Address to represent this connection .

be based on IPv6 Client application connection IPv4 Server time of , use IPv4-mapped IPv6 Address of access peer .

IPv4-mapped IPv6 The address is only used inside the system , Cannot be set on the network card as IPv6 The address of , It will not appear in the source or destination of network packets IP In the address field .

3. IP Application access in dual stack environment

IP The dual stack host runs at the same time IPv4 and IPv6 agreement , To configure IPv4 and IPv6 Address , Can be at the same time with IPv4 as well as IPv6 Device communication :

chart 1 IPv4 as well as IPv6 The device communicates at the same time

  Protocol paths for different network client applications to send packets in a dual stack environment :

 

chart 2 Protocol paths for different network client applications to send packets

Protocol paths for different network server applications to accept packets in a dual stack environment :

 

chart 3 Different network server applications accept the protocol path of data packets

4. IPv4 Customer access IPv4 service

This is the most common access method at present ,IPv4 The client of is creating socket Used when IPv4 Address family AF_INET,IPv4 Server side socket Also used IPv4 Address family AF_INET, The network communication between client and server is through IPv4 Protocol stack , The source and purpose of communication between client and server IP yes IPv4 Address .

chart 4  IPv4 Customer access IPv4 service

5. IPv4 Customer access IPv6 service

The retained client network application was created in the early stage socket The address family used in is IPv4 Address family AF_INET, But the server to adapt IPV6 Environmental Science , After modification , Creating socket When we use IPv6 Address family AF_INET6, And in bind The specific IPv6 Address or designation IPv4-mapped IPv6 Address ( The practical application will not adopt , Technically feasible ), that IPv4 The client can be accessed by connecting to the other end IPv4 Address to access the service .

The network packets sent and received on the client host pass IPv4 Protocol stack , The server host is from IPv4 The protocol stack receives a packet , according to IPv4 The transport layer protocol in the packet is sent to TCP or UDP Module processing ,TCP/UDP Module search TCP/UDP Protocol control block , Find the protocol control block that meets the conditions ( If the destination port is the same and V6only by 0 etc. ) To further receive / Processing packets , At the same time, it is used inside the protocol stack IPv4-mapped IPv6 Address to mark this connection .

chart 5 IPv4 Customer access IPv6 service

All of the following python The examples of are based on Centos 7.5, Adopted python The version is 2.7, The example is only a function demonstration , Error handling has been ignored :

n  Create a IPv6 Address family socket, Bind the service listening port 7788, Waiting for customer's connection request , Server receive IPv4/v6 The customer connects the request and outputs the received data , And print relevant TCP Connection information

#python                                                                       

>>> from socket import *                                                             

>>> s=socket(AF_INET6,SOCK_STREAM)                           

>>> s.bind(("",7788))                                                                              

>>> s.listen(5)                                                                                              

>>> s1,c1=s.accept()                                               

>>> s1.recv(100)                                                                                          

'aaaaaaaaa'                                                                                                         

>>> print c1                                               

('::ffff:192.168.100.165', 33178, 0, 0)

>>> from os import *

>>> system("ss -tn|grep 7788")

ESTAB    0   0       ::ffff:192.168.100.128:7788    ::ffff:192.168.100.165:33178             

>>> 

n  establish IPv4 Address family client , Connect to server 192.168.100.128 Service port on 7788 And send data , At the same time, at the system level ss Command output TCP Connection information

#python

>>> from socket import *

>>> s=socket(AF_INET,SOCK_STREAM)

>>> s.connect(("192.168.100.128",7788))

>>> s.send(b"aaaaaaaaa")

9

>>> from os import *

>>> system("ss -tn|grep 7788")

ESTAB      0   0  192.168.100.165:33178     192.168.100.128:7788              

>>> 

6. IPv6 Customer access IPv6 service

Newly developed network client and server applications socket The proposal USES IPv6 Address family AF_INET6, Such communication is to IPv6 The ultimate goal of migration , The source and destination addresses of communication are 128bit Of IPv6 Address .

By default IPv6 The server of can accept the data from IPv4 and IPv6 Connection request of the client , The only IPv6 Under the environment of , Can pass setsockopt Set up IPv6_V6ONLY by 1 or Set up sysctl Parameters of net.IPv6.bindv6only=1(LINUX Environmental Science ) Make the service program accept only from IPv6 Connection request of address family . stay IPv6 The same setting can be used for the client of , The client can only communicate with IPv6 Address for communication .

chart 6 IPv6 Customer access IPv6 service

7. IPv6 Client access IPv4 Server side

In order to adapt to IPv6 Environmental Science , use IPv6 Address family AF_INET6 Of socket, But what remains IPv4 The server of cannot be transformed for the time being , Still used IPv4 Address family AF_INET, Because the client adopts IPv6 Address family ,IPv6 Client access IPv4 The server can use the IPv4-mapped IPv6 Address method and IPv4 Server communication .

IPv6 The client passes the IPv4 Protocol stack , The source and destination network layer addresses of the packet header are standard IPv4 Address ,IPv4 The server cannot distinguish between client applications based on IPv4 The address family is still based on IPv6 Address family .

chart 7 IPv6 Client access IPv4 Server side

n  establish IPv4 Address family socket, Bind the service listening port 8888, Receive the client connection request on this port and output the received data and the connection request client IP Address and TCP Port number information

#python

>>> from socket import *

>>> s=socket(AF_INET,SOCK_STREAM)

>>> s.bind(("",8888))

>>> s.listen(4)

>>> s1,c1=s.accept()

>>> s1.recv(100)

'xxxxxxxxx'

>>> print c1

('192.168.100.128', 49314)

>>> from os import *

>>> system("ss -tn")

State      Recv-Q Send-Q        Local Address:Port               Peer Address:Port              

ESTAB        0      0         192.168.100.165:8888            192.168.100.128:49314                    

>>> 

n  establish IPv6 Address family socket client , use IPV4-mapped Address connection server 192.168.100.165 Service listening port on 8888, Send data to the server after successful connection , At the same time, at the system level ss Command output TCP Connection information

#python

>>> from socket import *

>>> s=socket(AF_INET6,SOCK_STREAM)

>>> s.connect(("::ffff:192.168.100.165",8888))

>>> s.send("xxxxxxxxx")

>>> from os import *

>>> system("ss -tn")

State     Recv-Q Send-Q       Local Address:Port                 Peer Address:Port                

ESTAB    0       0      ::ffff:192.168.100.128:49314           ::ffff:192.168.100.165:8888                 

8. IPv6 V6ONLY application

be based on IPv6 By default, the application of address family can be based on IPv4 as well as IPv6 Application interworking of address family , Set up IPv6_V6ONLY Parameters can change this behavior , This parameter is in HPUX、Linux The default is 0,Windows The default is 1, If set to 1, Then the application can only be used with IPv6 Application interworking of address family , In one there is only IPv6 In the network environment , You can set IPv6_V6ONLY It's true . The following describes the application settings of the client and server respectively .

n  IPv6 Address family socket Set up V6ONLY The option is true client access IPv4 Server side ( Failure )

stay IPv6 In the client program of setsockopt To set up IPv6_V6ONLY It's true , Then this client can only work with IPv6 Communicate with the server of , Cannot pass IPv4-mapped IPv6 The form of the address and IPv4 Communicate with the server of .

chart 8 IPv6 V6ONLY application

n  IPv4 Customer access IPv6 V6ONLY Server side ( Failure )

use IPv6 Address family AF_INET6 Of socket By default, the server application of can accept from IPv4 and IPv6 Client's connection request (windows Under the system IPv6 Application of address family V6ONLY Default is true ), The only IPv6 Under the environment of , call setsockopt Set up IPv6_V6ONLY by 1 To refuse from IPv4 Client connection request for , Accept only from IPv6 Client's connection request .

Because the server receives IPv4 The address of the client is IPv4 Of , The server finds the corresponding TCP/UDP After the protocol control block , The discovery protocol control block is marked V6ONLY, Therefore, the connection request is rejected .

chart 9 IPv6 V6ONLY application

V6ONLY Server example , Create one based on IPv6 Address family socket, Set up new socket Of IPV6_V6ONLY The option of is true , Bind service port 9999 And start service monitoring

# python

>>> from socket import *

>>> from os import *

>>> s=socket(AF_INET6,SOCK_STREAM)

>>> s.setsockopt(IPPROTO_IPV6,IPV6_V6ONLY,1)

>>> s.bind(('',9999))

>>> s.listen(8)

>>> system("ss -6ltne|grep 9999")

LISTEN     0      8       :::9999       :::*    ino:42509 sk:ffff8f46b9528880 v6only:1 <->0

>>> 

9. Transformation of existing applications

With source code , Will a IPv4 The server program of is transformed into support IPv6 The program , The application after transformation can accept from IPv4 and IPv6 Service request of the client , The transformation mainly involves the change of address structure .

With C Source code for example , Existing support IPv4 Server code of address family :

int sock,sock_new;

unsigned short srv-port;

struct sockaddr_in serv,cli;

socklen_t cli_len=soizeof(cli);

sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

memset((&serv,0,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_len=sizeof(serv);

serv.sin_port=htons(srv-port));

bind(sock,(struct sockaddr*)&serv,sizeof(serv));

listen(sock,5);

sock_new=accept(sock,(struct sockaddr*)&cli,&len);

After the transformation, it is based on IPv6 Service program of address family , Can accept from IPv4 and IPv6 Client's connection request :

int sock,sock_new;

unsigned short srv-port;

struct sockaddr_in6 serv,cli;

socklen_t cli_len=soizeof(cli);

sock=socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP);

memset((&serv,0,sizeof(serv));

serv.sin6_family=AF_INET6;

serv.sin6_len=sizeof(serv);

serv.sin6_port=htons(srv-port));

bind(sock,(struct sockaddr*)&serv,sizeof(serv));

listen(sock,5);

sock_new=accept(sock,(struct sockaddr*)&cli,&len);

10.    IPv6 Address family server programming

In the transitional stage of agreement migration , In order to simultaneously respond to IPv4 and IPv6 Client application request , There are two common ways to design server programs :

Create only 1 individual IPv6 Address family socket monitor

Build only one AF_INET6 Address family listening socket To respond at the same time from IPv4 and IPv6 Service request of the client . When the received request destination address belongs to IPv4 Address family time , The server adopts IPv4-mapped IPv6 Address means , This connection request passed IPv4 Protocol stack processing , When the received request destination address belongs to IPv6 Address family time , The connection request passed IPv6 Protocol stack processing .

Linux In common use ftp The server vsftpd In this way , As can be seen from the output below , in the light of TCP Port 21 Only one is based on AF_INET6 Address family listener , also V6ONLY The options are 0, That means this TCP port 21 Can accept from IPv4 and IPv6 Client's connection request :

#ss -6ltnep|grep :21

LISTEN   0  32  :::21    :::*  users:(("vsftpd",pid=1290,fd=3)) ino:16150 sk:12 v6only:0 <->

Create at the same time 1 individual IPv4 socket Monitoring and 1 individual IPv6 socket monitor

To avoid using IPv4-mapped IPv6 Address , Create AF_INET and AF_INET6 Two address families socket, Listen on the same port at the same time ,IPv4 Address family AF_INET Listening in socket Responsible for receiving information from IPv4 Connection of the client , And set up IPv6 Address family socket Of IPv6_V6ONLY Attribute is true , Thus only responsible for accepting from IPv6 Connection of the client , Because there are two in the listening state socket, call select To deal with different socket Connection request of address family .

Linux In the system sshd Is achieved in this way , from strace track sshd Output or view of related system calls openssh The source code can be verified :

socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3

setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0

bind(3, {sa_family=AF_INET, sin_port=htons(22), sin_addr=inet_addr("0.0.0.0")}, 16) = 0

listen(3, 128)                          = 0

 …………………………………………………………………………

socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP) = 4

setsockopt(4, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0

setsockopt(4, SOL_IPv6, IPv6_V6ONLY, [1], 4) = 0

bind(4, {sa_family=AF_INET6, sin6_port=htons(22), inet_pton(AF_INET6, "::", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0

listen(4, 128)                          = 0

select(5, [3 4], NULL, NULL, NULL)

…………………………………………………………………………..

ss The instruction output can intuitively indicate its mechanism :

# ss -ltnep|grep 22

LISTEN     0  128   *:22  *:*   users:(("sshd",pid=3236,fd=3)) ino:88651 sk:1f <->

LISTEN     0  128   :::22  :::*   users:(("sshd",pid=3236,fd=4)) ino:88653 sk:20 v6only:1 <->

11.    IPv6 Client Programming

IPv6 The client accesses by specifying an address or domain name IPv4 as well as IPv6 The service side , If the service provider is IPv4, So we need to use IPv4-mapped IPv6 Visit in the form of address . For domain name access , Need to adopt DNS Implement name to resolve , stay IPv4 There are two parsing library function calls in the environment :gethostbyname( Forward analysis )、gethostbyaddr( Reverse DNS ), This kind of parsing is used for IPv4 Environmental Science .IPv6 In the environment getaddrinfo and getnameinfo Parsing IPv4 and IPv6 Address information ,getaddrinfo According to the domain name / Service name and related hints Wait for the information to return to a group A and AAAA Record , Each returned record is attached with a for creating socket The required AF_xxx as well as SOCK_xxx Etc , Client programs usually use circular statements for getaddrinfo Return to the address list for each attempt to create socket as well as connect Opposite end , Until I found that for an address record socket and connect All calls return success .

The client only needs to know the domain name and service port of the server , use getaddrinfo The address list information returned by the call , You can create a suitable address family socket Connect to the server .

Python Of getaddrinfo Examples of use

analysis www.hpe.com Address information for , Returns a containing IPv4 as well as IPv6 List of addresses

#python

>>> from socket import *

>>>from os import *

>>> from pprint import pprint

>>> list=getaddrinfo("www.hpe.com","www")

>>> pprint(list)

[(2, 1, 6, '', ('23.7.213.221', 80)),

 (2, 2, 17, '', ('23.7.213.221', 80)),

 (10, 1, 6, '', ('2600:1417:a000:195::1463', 80, 0, 0)),

 (10, 2, 17, '', ('2600:1417:a000:195::1463', 80, 0, 0)),

 (10, 1, 6, '', ('2600:1417:a000:1b4::1463', 80, 0, 0)),

 (10, 2, 17, '', ('2600:1417:a000:1b4::1463', 80, 0, 0))]

>>> list[0][0:3]

(2, 1, 6)

>>> list[0][4]

('23.7.213.221', 80)

Connect DNS Parse the first item in the return list IPv4 Address , After success, output at the system level TCP Connected Quad information :

>>> s=socket(*list[0][0:3])

>>> s.connect(list[0][4])

>>> system("ss -tn|grep 80")

ESTAB      0      0      192.168.100.165:39838      23.7.213.221:80    0

Try to use DNS Parse the third item in the returned list IPv6 Address information to connect www.hpe.com, Due to the test system centos Unable to reach the server IPv6 Address , The connection fails :

>>> s1=socket(*list[2][0:3])

>>> s1.connect(list[2][4])

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/usr/lib64/python2.7/socket.py", line 224, in meth

    return getattr(self._sock,name)(*args)

socket.error: [Errno 101] Network is unreachable

>>> 

C Linguistic getaddrinfo Examples of use :

main(int argc, char **argv)

{

         struct addrinfo         *res, *ainfo;

         struct addrinfo        hints;

         int   error;

         struct sockaddr_in6        peeraddr6;

         struct sockaddr_in6         addr6;

         char connect_addr[INET6_ADDRSTRLEN];

         if (argc != 2) {

                   fprintf(stderr, "Usage:  %s <remote host>\n", argv[0]);

                   exit(1);

         }

         memset ((char *)&hints, 0, sizeof(hints));

         hints.ai_socktype = SOCK_STREAM;

         error =  getaddrinfo(argv[1], "www", &hints, &res);

         if (error != 0)

                   exit(1);

         for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {

s = socket (ainfo->ai_family,ainfo->ai_socktype,ainfo->ai_protocol);

                   if (s == -1)

         continue;

                   if (connect(s, ainfo->ai_addr, ainfo->ai_addrlen) == -1)

                            continue;

                   else

                            break;

         }

………………………..

}

java,Python And other languages have done a good encapsulation of these details , Such as python Medium socket.create_connection() You can achieve the above C All functions of the code .

12.  Conclusion

Based on different IP The access mode of the application of address family and the specific implementation method are briefly described , At the same time, it also introduces how to transform the existing IPv4 Apply to new IPv6 Environmental Science , It is also applicable to IPv4 as well as IPv6 The application method of environment .IPv6 Application migration is the development direction of application migration in the future , I hope this article is right IPv6 Readers interested in application migration can help .

reference

1[RFC4038]  Application Aspects of IPv6 Transition

2An Introduction to Computer Networks

3IPv6 advanced protocols implementation

原网站

版权声明
本文为[Wangwanlin Ben]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051340432460.html