Getaddrinfo ()

Source: Internet
Author: User

From http://desktop.chinaitlab.com/VC/27753.html

In IPv4, The gethostbyname () function is used to resolve the host name to the address. However, this API does not allow the caller to specify any information of the desired address type. The returned structure only contains the space used to store IPv4 addresses. To solve this problem, the new getaddrinfo () API is introduced in IPv6, Which is protocol-independent and can be used either IPv4 or IPv6. Call this function to obtain a list of addrinfo structures. The returned value is the structure (list) pointer of addrinfo.
  
This article introduces the getaddrinfo and addrinfo data structures based on the experience of using this function on winowsxp and Windows2003 server, and discusses the parameter settings, it mainly includes the impact of nodename and servname values on the returned value, and the impact of hints member variable settings on the returned value.
  
There may be incomplete or inaccurate information. You are welcome to discuss and point out.
  
1. getaddrinfo function prototype
  

 

Getaddrinfo provides protocol-independent name resolution.
  
The first two parameters of the function are the node name and service name respectively. The node name can be either a host name or an address string (IPv4 point in decimal number or an IPv6 hexadecimal number string ). The service name can be a decimal port number or a defined service name, such as ftp or HTTP. Note: Both the node name and service name are optional, that is, the node name or service name can be null. In this case, the call result is set by default, which will be discussed in detail later.
  
The third parameter hints of the function is a pointer to the addrinfo structure. The caller fills in clues about the type of information it wants to return. The Return Value of the function is a linked list pointer res pointing to the addrinfo structure.
  
2. addrinfo Structure
  

 

3. parameter description
Before the getaddrinfo function, you must set the following six parameters: nodename, servname, ai_flags, ai_family, ai_socktype, and ai_protocol of hints.
  
Among the six parameters, nodename, sername, and hints. ai_flag have the greatest impact on the function.
  
Ai_family only differs from V4 or V6 addresses. Ai_protocol is generally set to 0 without modification.

(In addition, the description of the addrinfo structure in msdn contains the ai_protocol annotation: protocol, such as ipproto_tcp or ipproto_udp. For protocols other than IPv4 and IPv6, SetAi_protocolTo zero .)
  
The description of ai_flags, ai_family, and ai_socktype is as follows:
  

 

Description of ai_flags values:
  

 

As shown in the table above, the value range of ai_flagsde is 0 ~ 7. It depends on how the program sets three flag bits. For example, if ai_flags is set to "ai_passive | ai_canonname", the value of ai_flags is 3. The meanings of the three parameters are as follows:
  
(1) When this flag is set, ai_passive indicates that the caller will use the returned address structure in the BIND () function call. When this flag is not set, it indicates that it will be used in the connect () function call.
  
If the node name is null and this flag is set, the returned address is a wildcard address.
  
If the node name is null and this flag is not set, the return address is the loopback address.
  
(2) When this flag is set to a bit, the ai_cannoname member in the first addrinfo structure returned by the function should contain a string ending with an empty character, the content of the string is the regular name of the node name.
  
(3) When this flag is set to a bit, this flag indicates that the node name in the call must be a digital address string.
  
4. Several Common settings
In general, in Client/Server programming, the server calls BIND (if listen is required for connection-oriented), the client does not need to call the BIND function, and directly connect (connection-oriented) after resolving the address) or directly send data (no connection ). Therefore, common scenarios include:
  
(1) Before the server calls getaddrinfo, ai_flags sets ai_passive for bind. The nodename of the host name is usually set to null and the wildcard Address [:] is returned.
  
(2) When the client calls getaddrinfo, ai_flags generally does not set ai_passive, but the host name nodename and service name servname (more like called Port) should not be empty.
  
(3) Of course, even if ai_passive is not set, the retrieved address is not bind. In many programs, ai_flags is directly set to 0, that is, no three flag spaces are set, in this case, BIND is correct as long as the hostname and servname settings are correct.
  
The above is only a simple use of client/server, but when using getaddrinfo and referring to the open source code outside China, there were some situations where the servname (that is, the port) was set to null.
  
(Of course, nodename must not be null at this time; otherwise, an error is returned when getaddrinfo is called ). Tests were conducted in the following cases:
  
(1) If nodename is a string-type IPv6 address, a temporary port will be allocated during bind;
  
(2) If nodename is the local name and servname is null, it is slightly different from the Operating System. This article only tests on WINXP and win2003.
  
A) WINXP System (SP2) returns the loopback address [: 1]
  
B) win2003 returns the list of all IPv6 addresses on the local machine. Generally, an IPv6 host may have more than one IPv6 address, such as fe80: 1 (local loopback address), fe80: *** link-local address, and 3ffe:. In this case, calling getaddrinfo will return all these addresses. The caller should pay attention to how to use these addresses. In addition, you must specify the interface address when binding the fe80: address, that is, use fe80: 20d: 60ff: fe78: 51c2% 4 or fe80 :: in address format like 1% 1, The fe80 address can be directly taken out through getaddrinfo as if it cannot be directly bind.
  
5. A few nonsense words
In the Windows environment, I personally think that the use of WINXP (SP2) and win2003 is basically not much different, and win2003 is more standardized.
  
To write and debug IPv6 programs with VC, you must install the newer Windows SDK. I installed ms_platform_sdk_feb_2003, otherwise there may be problems with library functions and header files.

 

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.