Linux socket Programming: addrinfo structure and getaddrinfo function

Source: Internet
Author: User
Tags bind socket port number

1. Overview

IPV4 uses the gethostbyname () function to complete host name to address resolution, which only supports IPV4 and does not allow callers to specify any information about the desired address type, and the returned structure contains only the space used to store the IPV4 address. The new API for Getaddrinfo () is introduced in IPv6, which is protocol independent and can be used for both IPV4 and IPV6. The Getaddrinfo function handles the conversion of the first name to the address and the service to the port, and returns a addrinfo structure (list) pointer rather than an address list. These addrinfo structures can then be used directly by a set of interface functions. Thus, the Getaddrinfo function hides the protocol dependencies inside the library function. The application simply processes the socket address structure that is filled in by the getaddrinfo function. This function is defined in the POSIX specification.

2. Function description

Include header file

#include <netdb.h>

Function prototypes

int getaddrinfo (const char *hostname, const char *service, const struct addrinfo *hints, struct addrinfo);

Parameter description

Hostname: A host name or address string (IPv4 dotted decimal string or IPv6 16 string)

Services: The service name can be a decimal port number, or it can be a defined service name, such as FTP, HTTP, and so on

Hints: Either a null pointer or a pointer to a ADDRINFO structure in which the caller fills in a hint about the type of information expected to return. For example, if the specified service supports both TCP and UDP, the caller can set the Ai_socktype member in the hints structure to sock_dgram so that only information that is appropriate for the datagram socket interface is returned.

Result: This function returns a pointer to a linked list of addrinfo structures through the return pointer argument.

Return value: 0--successful, non 0--error

3. Parameter setting

The following 6 parameters typically need to be set before the Getaddrinfo function: nodename, servname, hints Ai_flags, ai_family, Ai_socktype, Ai_protocol.

Of the 6 parameters, the Nodename,sername and Hints.ai_flag are most affected by the function, and ai_family only has the difference of Address V4 address or V6 address. Ai_protocol is generally for 0 without making changes.

Several common parameter settings of getaddrinfo in practical use

In general, in Client/server programming, the server-side calls bind (if the connection-oriented also requires listen), the client does not need to drop the bind function, resolve the address directly connect (connection-oriented) or send the data directly (no connection). Therefore, the more common situation has

(1) Typically, the server side ai_flags set ai_passive for bind before calling Getaddrinfo, and host name NodeName is usually set to NULL, returning a wildcard address [::].

(2) When a client invokes getaddrinfo, Ai_flags generally does not set ai_passive, but the hostname nodename and service name ServName (more preferred as ports) should not be empty.

(3) Of course, even if not set ai_passive, remove the address is not can not be bind, many programs ai_flags directly set to 0, that is, 3 flags are not set, in this case, as long as the hostname and servname set no problem can be properly bind.

The above situation is only used in simple client/server, but in fact, when using getaddrinfo and referencing foreign open source code, there are some cases where the servname (that is, the port) is set to null (of course, nodename will not be null at this time). Otherwise, the call getaddrinfo will complain.

The following points were tested:

(1) If the nodename is a string-type IPV6 address, bind will assign a temporary port;

(2) If the nodename is a native name and ServName is NULL, this article is only tested on WinXP and Win2003, depending on the operating system's differences.

A) WinXP system (SP2) return loopback address [:: 1]

b) Win2003 will return all IPV6 address lists for this machine. Because usually a IPv6 host may have more than one IPV6 address, such as fe80::1 (native loopback address), FE80::* * * * link-local address, the 3ffe:*** global address, and so on. In this case, calling Getaddrinfo will return all these addresses, and the caller should be careful how they are used. Also note that for FE80:: The address in the binding must be marked with the interface address, that is, the use of fe80::20d:60ff:fe78:51c2%4 or fe80::1%1 address format, Removing the FE80 address directly from the getaddrinfo does not seem to bind directly.

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.