2004-12-12 (Modify) IPv4 uses the gethostByName () function to complete the host name to address resolution, but the API does not allow the caller to specify any information of the desired address type, the returned structure contains only used to store IPv4 addresses. space. In order to solve this problem, the new API of GetDrinfo () is introduced in IPv6, which is independent of the protocol, which can be used for IPv4 or IPv6. Calling this function gets a list of AddrInfo structures, the return value called AddrInfo (list) pointer.
This article combines the experience of using this function on WinOWSXP and Windows2003 Server, introduces the getaddrinfo function and the Addrinfo data structure, and discusses its parameters, mainly including NodeName and ServName's impact on the return value, HINTS member variable The setting of the return value, etc.
There may be incomplete or inaccurate places, you are welcome to discuss and point out.
1. GetDrinfo function prototype
function
Parameter Description
Int getaddrinfo
Const char * nodename
Const char * servname,
Const struct addrinfo * hints, //
struct addrinfo ** res
);
NodeName: The node name can be a host name or a digital address. (IPv4 10% of points, or 16-based iPv6)
Servname: Contains port numbers or service names (FTP, HTTP)
Hints: It is an empty pointer or a pointer to an AddrInfo structure that fills in the clue of the information type it wants to return by the caller.
RES: Store the pointer to the AddrInfo structure linked list
GetDrinfo provides a name resolution independent of the protocol.
The first two parameters of the function are node names and service names, respectively. The node name can be the host name, or the address string (IPv4's point decimal number representation or IPv6 hexadecimal number string). The service name can be a decimal port number or a defined service name, such as FTP, HTTP, etc. Note: The node name and service name are optional, that is, the node name or service name can be null. At this time, the result of the call will be set by default, and will be discussed in detail later.
The third parameter of the function hints is the pointer of the AddrInfo structure, fills in the clues of the information type it wants to return. The return value of the function is a linked list pointer RES pointing to the Addrinfo structure.
2. Addrinfo structure
structure
Fixed parameters
Typedef struct addrinfo {
INT ai_flags;
INT ai_family;
INT ai_socktype;
INT ai_protocol;
SIZE_T AI_ADDRLEN
Char * ai_canname;
Struct SockAddr * ai_addr;
Struct Addrinfo * ai_next;
}
AI_ADDRLEN MUST BE ZERO OR A NULL POINTER
AI_CANONNAME MUST BE ZERO OR A NULL POINTER
AI_ADDR Must Be Zero or A Null Pointer
Ai_Next Must Be Zero or a Null Pointer
Can modified parameters
Ai_Flags: ai_passive, ai_canname, Ai_NuMerichost
Ai_Family: AF_INET, AF_INET6
AI_SOCKTYPE: SOCK_STREAM, SOCK_DGRAM
AI_PROTOCOL: IPPROTO_IP, IPPROTO_IPV4, IPPROTO_IPV6 etc.
The following sets of 6 parameters are usually required before the getadDrinfo function: Nodename, servname, Hints AI_FLAGS, AI_FAMILY, AI_SOCKTYPE, AI_PROTOCOL
In the 6 parameters, the maximum impact on the function is Nodename, Sername and Hints.ai_Flag
And Ai_Family is just the difference between the address of the V4 address or V6 address. AI_PROTOCOL is generally not changed for 0.
Where AI_Flags, AI_FAMILY, AI_SOCKTYPE describes the following:
parameter
Value
value
Description
AI_FAMILY
AF_INET
2
IPv4
AF_INET6
twenty three
IPv6
AF_UNSPEC
0
Agreement
AI_PROTOCOL
Ipproto_ip
0
IP protocol
Ipproto_IPv4
4
IPv4
Ipproto_IPv6
41
IPv6
Ipproto_UDP
In one
UDP
Ipproto_tcp
6
TCP
AI_SOCKTYPE
Sock_Stream
1
flow
SOCK_DGRAM
2
Datagram
Ai_flags
Ai_passive
1
Passive, used for Bind, usually used for Server Socket
Ai_canonname
2
Ai_NuMerichost
4
Address is a digital string
Description of the AI_FLAGS value:
Ai_NuMerichost
Ai_canonname
Ai_passive
0/1
0/1
0/1
As shown in the above table, the AI_FLAGSDE value ranges from 0 to 7, depending on how the program sets 3 sign bits, such as setting ai_flags "AI_Passive | ai_canname", the AI_FLAGS value is 3. The meaning of the three parameters is:
(1) AI_PASSIVE When this flag is set, it means that the caller will use the returned address structure in the bind () function call. When this flag is not set, it means that it will be used in the Connect () function call.
When the node is named NULL, and this flag is set, the returned address will be a wild address.
If the node name NULL, and this flag is not set, the return address will be a loop address.
(2) AI_CANNONAME When this flag is set, in the AI_CANNONAME member in the first addrinfo structure returned, it should contain a string ending with an empty character, and the content of the string is the regular name of the node name.
(3) AI_NUMERICHOST When this flag is set, this flag indicates that the node name in the call must be a digital address string.
4. Several common settings actually used
In general, in Client / Server programming, Server end calls BIND (if listen is required for connection, the client does not need to drop the BIND function, parse the address, directly Connect (connected) or directly send data (no connection). Therefore, more common situations
(1) Usually the server side before calling getaddrinfo, Ai_Flags sets AI_Passive for bind; hostname NodeName is usually set to NULL, returns a wild address [::].
(2) When the client calls GetDrinfo, AI_FLAGS generally does not set AI_Passive, but hostname NodeName and service name servname should not be empty. (3) Of course, even if it does not set AI_Passive, the take-out address is not could not be bind, and many programs are directly set to 0, that is, the three flag bits are not set. In this case, as long as HostName and Servname are set up. It can be correctly backed.
The above situation is only the use of the use of the simple client / server, but when using getaddrinfo and refer to the foreign open source code, there have been some situations where servname (ie port) is set to NULL.
(Of course, the NodeName will not be null, otherwise the getaddrinfo will report an error). The following sub-cases were tested:
(1) If NodeName is a string type IPv6 address, the temporary port is assigned when bind;
(2) If NodeName is this name, servname is NULL, depending on the different operating system, this article is only tested on WinXP and Win2003.
a) WinXP system (SP2) Returns the loopback address [:: 1] (under XP, even if servname is not empty, the returned address is still [:: 1], as long as nodename is this name, return address Just [:: 1], you can't return other addresses, I don't know if it is an XP implementation problem, because [:: 1] can only be used as this machine test, binding Socket on it is unable to be subject to other machines Addressable
b) Win2003 returns all IPv6 addresses of this unit. Because typically an IPv6 host is likely to be more than one IPv6 address, such as Fe80 :: 1 (this machine loopback address), FE80 :: *** Link-local address, 3FFE: *** global address, etc. In this case, calling GetDrinfo will return all of these addresses, and the caller should pay attention to how to use these addresses. Also note that for the address of the Fe80 :: must indicate the interface address when binding, even if Fe80 :: 20d: 60ff: Fe78:
51C
2% 4 or Fe80 :: 1% 1 This address format, directly taken out of the FE80 address directly through the getaddrinfo seems to be directly bind.
5. A few nonsense
Program personal feeling in Windows environmental debug IPv6 still uses WinXP (SP2) and Win2003 basically not very different, using Win2003 more standardized.
Writing and debugging the IPv6 with VC must install Windows newer SDK, I installed MS_PLATFORM_SDK_FEB_2003, otherwise the library function and header file may have problems.
[reference]
[1] MSDN LIBRARY - January 2004
MS-help: //ms.msdnqtr.2004jan.msdnqtr.2004jan.3033/winsock/winsock/getaddrinfo_2.htm
MS-help: //ms.msdnqtr.2004jan.3033/winsock/winsock/addrinfo_2.htm
[2] "Understand IPv6" ("Understanding IPV
6 "
),Tsinghua University Press