From Fedora Project Wiki
No edit summary
Line 25: Line 25:
int error;
int error;


error = getaddrinfo(node, service, &hints, &result)
error = getaddrinfo(node, service, &hints, &result);
</pre>
</pre>
The input of getaddrinfo() consists of ''node'' specification,
''service'' specification and further ''hints''.
* ''node'': literal IPv4 or IPv6 address, or a hostname to be resolved
* ''service'': numeric port number or a symbolic service name
* ''hints.ai_family'': enable dualprotocol, IPv4-only or IPv6-only queries
* ''hints.ai_socktype'': select socket type (and thus protocol family)
<code>getaddrinfo()</code> can be futher tweaked with the ''hints.ai_flags''. Other
attributes are either not needed (''ai_protocol'') or not supposed
to be set in hints (''ai_canonname'', ''ai_addr'' and ''ai_next'').

Revision as of 20:44, 21 November 2012

Domain Name System

Resolving using getaddrinfo() in applications

The getaddrinfo() function is a dualstack-friendly API to name resolution. It is used by applications to translate host and service names to a linked list of struct addrinfo objects.

And example of getaddrinfo() call:

const char *node = "www.fedoraproject.org";
const char *service = "http";
struct addrinfo hints = {
    .ai_family = AF_UNSPEC,
    .ai_socktype = SOCK_DGRAM,
    .ai_flags = 0,
    .ai_protocol = 0,
    .ai_canonname = NULL,
    .ai_addr = NULL,
    .ai_next = NULL
};
struct addrinfo *result;
int error;

error = getaddrinfo(node, service, &hints, &result);

The input of getaddrinfo() consists of node specification, service specification and further hints.

  • node: literal IPv4 or IPv6 address, or a hostname to be resolved
  • service: numeric port number or a symbolic service name
  • hints.ai_family: enable dualprotocol, IPv4-only or IPv6-only queries
  • hints.ai_socktype: select socket type (and thus protocol family)

getaddrinfo() can be futher tweaked with the hints.ai_flags. Other attributes are either not needed (ai_protocol) or not supposed to be set in hints (ai_canonname, ai_addr and ai_next).