Getaddrinfo
   HOME

TheInfoList



OR:

In C programming, the functions and convert
domain name In the Internet, a domain name is a string that identifies a realm of administrative autonomy, authority, or control. Domain names are often used to identify services provided through the Internet, such as websites, email services, and more. ...
s, hostnames, and
IP address An Internet Protocol address (IP address) is a numerical label such as that is assigned to a device connected to a computer network that uses the Internet Protocol for communication. IP addresses serve two main functions: network interface i ...
es between human-readable text representations and structured binary formats for the
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
's networking API. Both functions are contained in the
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
standard
application programming interface An application programming interface (API) is a connection between computers or between computer programs. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standard that des ...
(API). getaddrinfo and getnameinfo are inverse functions of each other. They are network protocol agnostic, and support both
IPv4 Internet Protocol version 4 (IPv4) is the first version of the Internet Protocol (IP) as a standalone specification. It is one of the core protocols of standards-based internetworking methods in the Internet and other packet-switched networks. ...
and
IPv6 Internet Protocol version 6 (IPv6) is the most recent version of the Internet Protocol (IP), the communication protocol, communications protocol that provides an identification and location system for computers on networks and routes traffic ...
. It is the recommended interface for name resolution in building protocol independent applications and for transitioning legacy IPv4 code to the IPv6 Internet. Internally, the functions may use a variety of resolution methods not limited to the
Domain Name System The Domain Name System (DNS) is a hierarchical and distributed name service that provides a naming system for computers, services, and other resources on the Internet or other Internet Protocol (IP) networks. It associates various information ...
(DNS). The Name Service Switch is commonly used on Unix-like systems and affects most implementation of this pair as it did with their BSD-socket era predecessors.


struct addrinfo

The C data structure used to represent addresses and hostnames within the networking API is the following: struct addrinfo ; In some older systems the type of is instead of . Most socket functions, such as and , require the parameter to have type and programmers often pass the address to the element of the ''addrinfo'' structure. If the types are incompatible, e.g., on a 64-bit Solaris 9 system where is 8 bytes and is 4 bytes, then run-time errors may result. The structure contains structures and ''sockaddr'' with its own field. These are set to the same value when the structure is created with function ''getaddrinfo'' in some implementations.


getaddrinfo()

getaddrinfo() converts human-readable text strings representing hostnames or
IP address An Internet Protocol address (IP address) is a numerical label such as that is assigned to a device connected to a computer network that uses the Internet Protocol for communication. IP addresses serve two main functions: network interface i ...
es into a dynamically allocated
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
of struct addrinfo structures. The function prototype for this function is specified as follows: int getaddrinfo(const char* hostname, const char* service, const struct addrinfo* hints, struct addrinfo** res); ; hostname : can be either a domain name, such as "
example.com The domain names example.com, example.net, example.org, and example.edu are second-level domain names in the Domain Name System of the Internet. They are reserved by the Internet Assigned Numbers Authority (IANA) at the direction of the Inter ...
", an address string, such as "", or NULL, in which case the address or is assigned depending on the hints flags. ; service : can be a port number passed as string, such as "80", or a service name, e.g. "echo". In the latter case a typical implementation uses to query the file ''/etc/services'' to resolve the service to a port number. ; hints : can be either NULL or an ''addrinfo'' structure with the type of service requested. ; res : is a pointer that points to a new ''addrinfo'' structure with the information requested after successful completion of the function. The function returns 0 upon success and non-zero error value if it fails. Although implementations vary among platforms, the function first attempts to obtain a port number usually by branching on ''service''. If the string value is a number, it converts it to an integer and calls . If it is a service name, such as ''www'', the service is looked up with , using the protocol derived from as the second parameter to that function. Then, if ''hostname'' is given (not NULL), a call to resolves it, or otherwise the address is used, if is set to , and otherwise. It allocated a new ''addrinfo'' structure filled with the appropriate in one of these conditions and also adds the port retrieved at the beginning to it. Finally, the parameter is dereferenced to make it point to a newly allocated ''addrinfo'' structure. In some implementations, such as the Unix version for Mac OS, the overrides the value while in others it is the opposite, so both need to be defined with equivalent values for the code to be work across multiple platforms.


freeaddrinfo()

This function frees the memory allocated by function . As the result of the latter is a linked list of addrinfo structures starting at the address ''ai'', loops through the list and frees each one in turn. void freeaddrinfo(struct addrinfo *ai);


getnameinfo()

The function converts the internal binary representation of an IP address in the form of a pointer to a into text strings consisting of the hostname or, if the address cannot be resolved into a name, a textual IP address representation, as well as the service port name or number. The function prototype is specified as follows: int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags);


Example

The following example uses to resolve the domain name ''www.example.com'' into its list of addresses and then calls on each result to return the canonical name for the address. In general, this produces the original hostname, unless the particular address has multiple names, in which case the ''canonical'' name is returned. In this example, the domain name is printed three times, once for each of the three results obtained. #include #include #include #include #include #ifndef NI_MAXHOST #define NI_MAXHOST 1025 #endif int main(void)


Security

On February 16, 2016, a security bug was announced in the
glibc The GNU C Library, commonly known as glibc, is the GNU Project implementation of the C standard library. It provides a wrapper around the system calls of the Linux kernel and other kernels for application use. Despite its name, it now also dir ...
implementation of , using a buffer overflow technique, that may allow execution of arbitrary code by the attacker.


See also

* Network address


References

{{reflist


External links


freeaddrinfo and getaddrinfo specifications in POSIX.1-2017
''The Open Group Base Specifications Issue 7, 2018 edition''
RFC 3493, Basic Socket Interface Extensions for IPv6
Internet Protocol Network addressing Domain Name System C POSIX library Articles with example C code