---
name: "getaddrinfo"
section: "3bind"
platform: "linux"
package: "bind-dev"
version: "1:8.4.7-1"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/getaddrinfo/3bind"
---

# getaddrinfo(3bind)

nodename-to-address translation in protocol-independent manner

## NAME

**getaddrinfo** **freeaddrinfo**, **gai\_strerror** – nodename-to-address translation in protocol-independent manner

## SYNOPSIS

**#include \<sys/socket.h\>**

**#include \<netdb.h\>**

*int*\

**getaddrinfo**(*const char \*nodename*, *const char \*servname*, *const struct addrinfo \*hints*, *struct addrinfo \*\*res*);

*void*\

**freeaddrinfo**(*struct addrinfo \*ai*);

*char \**\

**gai\_strerror**(*int ecode*);

## DESCRIPTION

The **getaddrinfo**() function is defined for protocol-independent nodename-to-address translation. It performs functionality of  [gethostbyname(3)](/gethostbyname/3.md) and [getservbyname(3)](/getservbyname/3.md), in more sophisticated manner.

The addrinfo structure is defined as a result of including the \<netdb.h\> header:

```
struct addrinfo {                                                  *
     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
     int     ai_family;    /* PF_xxx */
     int     ai_socktype;  /* SOCK_xxx */
     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
     size_t  ai_addrlen;   /* length of ai_addr */
     char   *ai_canonname; /* canonical name for nodename */
     struct sockaddr  *ai_addr; /* binary address */
     struct addrinfo  *ai_next; /* next structure in linked list */
};
```

The *nodename* and *servname* arguments are pointers to null-terminated strings or NULL. One or both of these two arguments must be a non-NULL pointer. In the normal client scenario, both the *nodename* and *servname* are specified. In the normal server scenario, only the *servname* is specified. A non-NULL *nodename* string can be either a node name or a numeric host address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). A non-NULL *servname* string can be either a service name or a decimal port number.

The caller can optionally pass an addrinfo structure, pointed to by the third argument, to provide hints concerning the type of socket that the caller supports. In this *hints* structure all members other than *ai\_flags*, *ai\_family*, *ai\_socktype*, and *ai\_protocol* must be zero or a NULL pointer. A value of PF\_UNSPEC for *ai\_family* means the caller will accept any protocol family. A value of 0 for *ai\_socktype* means the caller will accept any socket type. A value of 0 for *ai\_protocol* means the caller will accept any protocol. For example, if the caller handles only TCP and not UDP, then the *ai\_socktype* member of the hints structure should be set to SOCK\_STREAM when **getaddrinfo**() is called. If the caller handles only IPv4 and not IPv6, then the *ai\_family* member of the *hints* structure should be set to PF\_INET when **getaddrinfo**() is called. If the third argument to **getaddrinfo**() is a NULL pointer, this is the same as if the caller had filled in an addrinfo structure initialized to zero with *ai\_family* set to PF\_UNSPEC.

Upon successful return a pointer to a linked list of one or more addrinfo structures is returned through the final argument. The caller can process each addrinfo structure in this list by following the *ai\_next* pointer, until a NULL pointer is encountered. In each returned addrinfo structure the three members *ai\_family*, *ai\_socktype*, and *ai\_protocol* are the corresponding arguments for a call to the **socket**() function. In each addrinfo structure the *ai\_addr* member points to a filled-in socket address structure whose length is specified by the *ai\_addrlen* member.

If the AI\_PASSIVE bit is set in the *ai\_flags* member of the *hints* structure, then the caller plans to use the returned socket address structure in a call to **bind**(). In this case, if the *nodename* argument is a NULL pointer, then the IP address portion of the socket address structure will be set to INADDR\_ANY for an IPv4 address or IN6ADDR\_ANY\_INIT for an IPv6 address.

If the AI\_PASSIVE bit is not set in the *ai\_flags* member of the *hints* structure, then the returned socket address structure will be ready for a call to **connect**() (for a connection-oriented protocol) or either **connect**(), **sendto**(), or **sendmsg**() (for a connectionless protocol). In this case, if the *nodename* argument is a NULL pointer, then the IP address portion of the socket address structure will be set to the loopback address.

If the AI\_CANONNAME bit is set in the *ai\_flags* member of the *hints* structure, then upon successful return the *ai\_canonname* member of the first addrinfo structure in the linked list will point to a null-terminated string containing the canonical name of the specified *nodename*.

If the AI\_NUMERICHOST bit is set in the *ai\_flags* member of the *hints* structure, then a non-NULL *nodename* string must be a numeric host address string. Otherwise an error of EAI\_NONAME is returned. This flag prevents any type of name resolution service (e.g., the DNS) from being called.

All of the information returned by **getaddrinfo**() is dynamically allocated: the addrinfo structures, and the socket address structures and canonical node name strings pointed to by the addrinfo structures. To return this information to the system the function Fn freeaddrinfo is called. The *addrinfo* structure pointed to by the *ai*, *argument* is freed, along with any dynamic storage pointed to by the structure. This operation is repeated until a NULL *ai\_next* pointer is encountered.

To aid applications in printing error messages based on the EAI\_xxx codes returned by **getaddrinfo**(), **gai\_strerror**() is defined. The argument is one of the EAI\_xxx values defined earlier and the return value points to a string describing the error. If the argument is not one of the EAI\_xxx values, the function still returns a pointer to a string whose contents indicate an unknown error.

## FILES

- */etc/hosts*

  - */etc/host.conf*

  - */etc/resolv.conf*

  ## DIAGNOSTICS

Error return status from  **getaddrinfo**() is zero on success and non-zero on errors. Non-zero error codes are defined in \<netdb.h\>, and as follows:

- EAI\_ADDRFAMILY

  address family for nodename not supported

- EAI\_AGAIN

  temporary failure in name resolution

- EAI\_BADFLAGS

  invalid value for ai\_flags

- EAI\_FAIL

  non-recoverable failure in name resolution

- EAI\_FAMILY

  ai\_family not supported

- EAI\_MEMORY

  memory allocation failure

- EAI\_NODATA

  no address associated with nodename

- EAI\_NONAME

  nodename nor servname provided, or not known

- EAI\_SERVICE

  servname not supported for ai\_socktype

- EAI\_SOCKTYPE

  ai\_socktype not supported

- EAI\_SYSTEM

  system error returned in errno

If called with proper argument, **gai\_strerror**() returns a pointer to a string describing the given error code. If the argument is not one of the EAI\_xxx values, the function still returns a pointer to a string whose contents indicate an unknown error.

## SEE ALSO

[getnameinfo(3)](/getnameinfo/3.md), [gethostbyname(3)](/gethostbyname/3.md), [getservbyname(3)](/getservbyname/3.md), [hosts(5)](/hosts/5.md), [services(5)](/services/5.md), [hostname(7)](/hostname/7.md), [named(8)](/named/8.md)

R. Gilligan, S.  Thomson, J. Bound, and W. Stevens, \`\`Basic Socket Interface Extensions for IPv6,'' RFC2133, April 1997.

## HISTORY

The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.

## STANDARDS

The **getaddrinfo**() function is defined IEEE POSIX 1003.1g draft specification, and documented in \`\`Basic Socket Interface Extensions for IPv6'' (RFC2133).

## BUGS

The text was shamelessly copied from RFC2133.
