Getaddrinfo ()

Source: Internet
Author: User

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
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 combines winowsxp and Windows2003
This function is used on the server to introduce the getaddrinfo function and addrinfo data structure, and discuss the parameter settings, including
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

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. (Of IPv4, or hexadecimal of IPv6)

Servname: the port number or service name that contains the decimal number, such as (FTP, HTTP)

Hints: a null pointer or a pointer to an addrinfo structure. The caller fills in clues about the type of information it wants to return.

Res: pointer to the linked list of the returned addrinfo Structure

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: The node name and service name are both optional, that is, the node name or service name can beNullIn this case, the call result will take the default settings, 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

Structure

Fixed Parameters

Typedef struct addrinfo {

Int ai_flags;

Int ai_family;

Int ai_socktype;

Int ai_protocol;

Size_t ai_addrlen;

Char * ai_canonname;

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

Modifiable Parameters

Ai_flags: ai_passive, ai_canonname, ai_numerichost

Ai_family: af_inet, af_inet6

Ai_socktype: sock_stream, sock_dgram

Ai_protocol: ipproto_ip, ipproto_ipv4, ipproto_ipv6 etc.

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.

The description of ai_flags, ai_family, and ai_socktype is as follows:

Parameters

Value

Value

Description

Ai_family

Af_inet

2

IPv4

Af_inet6

23

IPv6

Af_unspec

0

Protocol independence

Ai_protocol

Ipproto_ip

0

IP protocol

Ipproto_ipv4

4

IPv4

Ipproto_ipv6

41

IPv6

Ipproto_udp

17

UDP

Ipproto_tcp

6

TCP

Ai_socktype

Sock_stream

1

Stream

Sock_dgram

2

Datagram

Ai_flags

Ai_passive

1

Passive, used for BIND, usually used for server socket

Ai_canonname

2

Ai_numerichost

4

Number of address strings

Description of ai_flags values:

Ai_numerichost

Ai_canonname

Ai_passive

0/1

0/1

0/1

As shown in the table above, the value range of ai_flagsde is 0 ~ 7, depending onProgramHow to Set three flag bits, for example, set ai_flags to "ai_passive | ai_canonname", and ai_flags to 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 .

In general, in Client/Server programming, the server calls BIND (if listen is required for connection-oriented), the client does not need to drop 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 the actual use of getaddrinfo and referenceSource codeWhen the server name (port) is 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 depending on the Operating System

Note: This function is get, but it can be understood as creat or build. Because you can build your own address structure addrinfo at will.

For example
If this function returns a successful result, the variable pointed to by the result parameter has been filled with a pointer pointing to the addrinfo Structure linked list linked by the ai_next member. Yes
As a result, multiple addrinfo structures are returned:
1.
If there are multiple addresses associated with the hostname parameter, each address in the requested address cluster returns a corresponding structure.
2.
If the service parameter specifies that the Service supports multiple sets of interface types, each set of interface types may return a corresponding structure, depending on the ai_socktype of hints Structure
Member.

We must first allocate a hints structure, clear it, fill in the required fields, call getaddrinfo, and traverse a linked list to try each returned location one by one.
Address.

Getaddrinfo solves the problem of converting the interface address structure of Host Name and service name.

If getaddrinfo is output
Otherwise, a non-zero error value is returned.

Official link: http://www.kernel.org/doc/man-pages/online/pages/man3/gai_strerror.3.html

Very detailed.

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.