{"id":6730,"date":"2022-12-20T19:33:32","date_gmt":"2022-12-20T22:33:32","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getaddrinfo-man3\/"},"modified":"2022-12-20T19:33:32","modified_gmt":"2022-12-20T22:33:32","slug":"getaddrinfo-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getaddrinfo-man3\/","title":{"rendered":"GETADDRINFO (man3)"},"content":{"rendered":"<h1 align=\"center\">GETADDRINFO<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#FILES\">FILES<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getaddrinfo, freeaddrinfo, gai_strerror \u2212 network address and service translation<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/types.h> <br \/> #include <sys\/socket.h> <br \/> #include <netdb.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>int getaddrinfo(const char *<\/b><i>node<\/i><b>, const char *<\/b><i>service<\/i><b>, <br \/> const struct addrinfo *<\/b><i>hints<\/i><b>, <br \/> struct addrinfo **<\/b><i>res<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>void freeaddrinfo(struct addrinfo *<\/b><i>res<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>const char *gai_strerror(int<\/b> <i>errcode<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:5%; margin-top: 1em\">Feature Test Macro Requirements for glibc (see <b>feature_test_macros<\/b>(7)):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getaddrinfo<\/b>(), <b>freeaddrinfo<\/b>(), <b>gai_strerror<\/b>(): <br \/> Since glibc 2.22: _POSIX_C_SOURCE >= 200112L <br \/> Glibc 2.21 and earlier: _POSIX_C_SOURCE<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Given <i>node<\/i> and <i>service<\/i>, which identify an Internet host and a service, <b>getaddrinfo<\/b>() returns one or more <i>addrinfo<\/i> structures, each of which contains an Internet address that can be specified in a call to <b>bind<\/b>(2) or <b>connect<\/b>(2). The <b>getaddrinfo<\/b>() function combines the functionality provided by the <b>gethostbyname<\/b>(3) and <b>getservbyname<\/b>(3) functions into a single interface, but unlike the latter functions, <b>getaddrinfo<\/b>() is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>addrinfo<\/i> structure used by <b>getaddrinfo<\/b>() contains the following fields:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct addrinfo { <br \/> int ai_flags; <br \/> int ai_family; <br \/> int ai_socktype; <br \/> int ai_protocol; <br \/> socklen_t ai_addrlen; <br \/> struct sockaddr *ai_addr; <br \/> char *ai_canonname; <br \/> struct addrinfo *ai_next; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>hints<\/i> argument points to an <i>addrinfo<\/i> structure that specifies criteria for selecting the socket address structures returned in the list pointed to by <i>res<\/i>. If <i>hints<\/i> is not NULL it points to an <i>addrinfo<\/i> structure whose <i>ai_family<\/i>, <i>ai_socktype<\/i>, and <i>ai_protocol<\/i> specify criteria that limit the set of socket addresses returned by <b>getaddrinfo<\/b>(), as follows: <i><br \/> ai_family<\/i><\/p>\n<p style=\"margin-left:22%;\">This field specifies the desired address family for the returned addresses. Valid values for this field include <b>AF_INET<\/b> and <b>AF_INET6<\/b>. The value <b>AF_UNSPEC<\/b> indicates that <b>getaddrinfo<\/b>() should return socket addresses for any address family (either IPv4 or IPv6, for example) that can be used with <i>node<\/i> and <i>service<\/i>.<\/p>\n<p style=\"margin-left:11%;\"><i>ai_socktype<\/i><\/p>\n<p style=\"margin-left:22%;\">This field specifies the preferred socket type, for example <b>SOCK_STREAM<\/b> or <b>SOCK_DGRAM<\/b>. Specifying 0 in this field indicates that socket addresses of any type can be returned by <b>getaddrinfo<\/b>().<\/p>\n<p style=\"margin-left:11%;\"><i>ai_protocol<\/i><\/p>\n<p style=\"margin-left:22%;\">This field specifies the protocol for the returned socket addresses. Specifying 0 in this field indicates that socket addresses with any protocol can be returned by <b>getaddrinfo<\/b>().<\/p>\n<p style=\"margin-left:11%;\"><i>ai_flags<\/i><\/p>\n<p style=\"margin-left:22%;\">This field specifies additional options, described below. Multiple flags are specified by bitwise OR-ing them together.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">All the other fields in the structure pointed to by <i>hints<\/i> must contain either 0 or a null pointer, as appropriate.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Specifying <i>hints<\/i> as NULL is equivalent to setting <i>ai_socktype<\/i> and <i>ai_protocol<\/i> to 0; <i>ai_family<\/i> to <b>AF_UNSPEC<\/b>; and <i>ai_flags<\/i> to <b>(AI_V4MAPPED\u00a0|\u00a0AI_ADDRCONFIG)<\/b>. (POSIX specifies different defaults for <i>ai_flags<\/i>; see NOTES.) <i>node<\/i> specifies either a numerical network address (for IPv4, numbers-and-dots notation as supported by <b>inet_aton<\/b>(3); for IPv6, hexadecimal string format as supported by <b>inet_pton<\/b>(3)), or a network hostname, whose network addresses are looked up and resolved. If <i>hints.ai_flags<\/i> contains the <b>AI_NUMERICHOST<\/b> flag, then <i>node<\/i> must be a numerical network address. The <b>AI_NUMERICHOST<\/b> flag suppresses any potentially lengthy network host address lookups.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the <b>AI_PASSIVE<\/b> flag is specified in <i>hints.ai_flags<\/i>, and <i>node<\/i> is NULL, then the returned socket addresses will be suitable for <b>bind<\/b>(2)ing a socket that will <b>accept<\/b>(2) connections. The returned socket address will contain the &#8220;wildcard address&#8221; (<b>INADDR_ANY<\/b> for IPv4 addresses, <b>IN6ADDR_ANY_INIT<\/b> for IPv6 address). The wildcard address is used by applications (typically servers) that intend to accept connections on any of the host\u2019s network addresses. If <i>node<\/i> is not NULL, then the <b>AI_PASSIVE<\/b> flag is ignored.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the <b>AI_PASSIVE<\/b> flag is not set in <i>hints.ai_flags<\/i>, then the returned socket addresses will be suitable for use with <b>connect<\/b>(2), <b>sendto<\/b>(2), or <b>sendmsg<\/b>(2). If <i>node<\/i> is NULL, then the network address will be set to the loopback interface address (<b>INADDR_LOOPBACK<\/b> for IPv4 addresses, <b>IN6ADDR_LOOPBACK_INIT<\/b> for IPv6 address); this is used by applications that intend to communicate with peers running on the same host.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>service<\/i> sets the port in each returned address structure. If this argument is a service name (see <b>services<\/b>(5)), it is translated to the corresponding port number. This argument can also be specified as a decimal number, which is simply converted to binary. If <i>service<\/i> is NULL, then the port number of the returned socket addresses will be left uninitialized. If <b>AI_NUMERICSERV<\/b> is specified in <i>hints.ai_flags<\/i> and <i>service<\/i> is not NULL, then <i>service<\/i> must point to a string containing a numeric port number. This flag is used to inhibit the invocation of a name resolution service in cases where it is known not to be required.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Either <i>node<\/i> or <i>service<\/i>, but not both, may be NULL.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>getaddrinfo<\/b>() function allocates and initializes a linked list of <i>addrinfo<\/i> structures, one for each network address that matches <i>node<\/i> and <i>service<\/i>, subject to any restrictions imposed by <i>hints<\/i>, and returns a pointer to the start of the list in <i>res<\/i>. The items in the linked list are linked by the <i>ai_next<\/i> field.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are several reasons why the linked list may have more than one <i>addrinfo<\/i> structure, including: the network host is multihomed, accessible over multiple protocols (e.g., both <b>AF_INET<\/b> and <b>AF_INET6<\/b>); or the same service is available from multiple socket types (one <b>SOCK_STREAM<\/b> address and another <b>SOCK_DGRAM<\/b> address, for example). Normally, the application should try using the addresses in the order in which they are returned. The sorting function used within <b>getaddrinfo<\/b>() is defined in RFC\u00a03484; the order can be tweaked for a particular system by editing <i>\/etc\/gai.conf<\/i> (available since glibc 2.5).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>hints.ai_flags<\/i> includes the <b>AI_CANONNAME<\/b> flag, then the <i>ai_canonname<\/i> field of the first of the <i>addrinfo<\/i> structures in the returned list is set to point to the official name of the host.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The remaining fields of each returned <i>addrinfo<\/i> structure are initialized as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p>The <i>ai_family<\/i>, <i>ai_socktype<\/i>, and <i>ai_protocol<\/i> fields return the socket creation parameters (i.e., these fields have the same meaning as the corresponding arguments of <b>socket<\/b>(2)). For example, <i>ai_family<\/i> might return <b>AF_INET<\/b> or <b>AF_INET6<\/b>; <i>ai_socktype<\/i> might return <b>SOCK_DGRAM<\/b> or <b>SOCK_STREAM<\/b>; and <i>ai_protocol<\/i> returns the protocol for the socket.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"86%\">\n<p>A pointer to the socket address is placed in the <i>ai_addr<\/i> field, and the length of the socket address, in bytes, is placed in the <i>ai_addrlen<\/i> field.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>hints.ai_flags<\/i> includes the <b>AI_ADDRCONFIG<\/b> flag, then IPv4 addresses are returned in the list pointed to by <i>res<\/i> only if the local system has at least one IPv4 address configured, and IPv6 addresses are returned only if the local system has at least one IPv6 address configured. The loopback address is not considered for this case as valid as a configured address. This flag is useful on, for example, IPv4-only systems, to ensure that <b>getaddrinfo<\/b>() does not return IPv6 socket addresses that would always fail in <b>connect<\/b>(2) or <b>bind<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>hints.ai_flags<\/i> specifies the <b>AI_V4MAPPED<\/b> flag, and <i>hints.ai_family<\/i> was specified as <b>AF_INET6<\/b>, and no matching IPv6 addresses could be found, then return IPv4-mapped IPv6 addresses in the list pointed to by <i>res<\/i>. If both <b>AI_V4MAPPED<\/b> and <b>AI_ALL<\/b> are specified in <i>hints.ai_flags<\/i>, then return both IPv6 and IPv4-mapped IPv6 addresses in the list pointed to by <i>res<\/i>. <b>AI_ALL<\/b> is ignored if <b>AI_V4MAPPED<\/b> is not also specified.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>freeaddrinfo<\/b>() function frees the memory that was allocated for the dynamically allocated linked list <i>res<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Extensions to getaddrinfo() for Internationalized Domain Names<\/b> <br \/> Starting with glibc 2.3.4, <b>getaddrinfo<\/b>() has been extended to selectively allow the incoming and outgoing hostnames to be transparently converted to and from the Internationalized Domain Name (IDN) format (see RFC 3490, <i>Internationalizing Domain Names in Applications (IDNA)<\/i>). Four new flags are defined:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>AI_IDN<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">If this flag is specified, then the node name given in <i>node<\/i> is converted to IDN format if necessary. The source encoding is that of the current locale.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:22%; margin-top: 1em\">If the input name contains non-ASCII characters, then the IDN encoding is used. Those parts of the node name (delimited by dots) that contain non-ASCII characters are encoded using ASCII Compatible Encoding (ACE) before being passed to the name resolution functions.<\/p>\n<p style=\"margin-left:11%;\"><b>AI_CANONIDN<\/b><\/p>\n<p style=\"margin-left:22%;\">After a successful name lookup, and if the <b>AI_CANONNAME<\/b> flag was specified, <b>getaddrinfo<\/b>() will return the canonical name of the node corresponding to the <i>addrinfo<\/i> structure value passed back. The return value is an exact copy of the value returned by the name resolution function.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">If the name is encoded using ACE, then it will contain the <i>xn\u2212\u2212<\/i> prefix for one or more components of the name. To convert these components into a readable form the <b>AI_CANONIDN<\/b> flag can be passed in addition to <b>AI_CANONNAME<\/b>. The resulting string is encoded using the current locale\u2019s encoding.<\/p>\n<p style=\"margin-left:11%;\"><b>AI_IDN_ALLOW_UNASSIGNED<\/b>, <b>AI_IDN_USE_STD3_ASCII_RULES<\/b><\/p>\n<p style=\"margin-left:22%;\">Setting these flags will enable the IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3 conforming hostname) flags respectively to be used in the IDNA handling.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getaddrinfo<\/b>() returns 0 if it succeeds, or one of the following nonzero error codes: <b><br \/> EAI_ADDRFAMILY<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified network host does not have any network addresses in the requested address family.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_AGAIN<\/b><\/p>\n<p style=\"margin-left:22%;\">The name server returned a temporary failure indication. Try again later.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_BADFLAGS<\/b><\/p>\n<p style=\"margin-left:22%;\"><i>hints.ai_flags<\/i> contains invalid flags; or, <i>hints.ai_flags<\/i> included <b>AI_CANONNAME<\/b> and <i>name<\/i> was NULL.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_FAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">The name server returned a permanent failure indication.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_FAMILY<\/b><\/p>\n<p style=\"margin-left:22%;\">The requested address family is not supported.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_MEMORY<\/b><\/p>\n<p style=\"margin-left:22%;\">Out of memory.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_NODATA<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified network host exists, but does not have any network addresses defined.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_NONAME<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>node<\/i> or <i>service<\/i> is not known; or both <i>node<\/i> and <i>service<\/i> are NULL; or <b>AI_NUMERICSERV<\/b> was specified in <i>hints.ai_flags<\/i> and <i>service<\/i> was not a numeric port-number string.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_SERVICE<\/b><\/p>\n<p style=\"margin-left:22%;\">The requested service is not available for the requested socket type. It may be available through another socket type. For example, this error could occur if <i>service<\/i> was &#8220;shell&#8221; (a service available only on stream sockets), and either <i>hints.ai_protocol<\/i> was <b>IPPROTO_UDP<\/b>, or <i>hints.ai_socktype<\/i> was <b>SOCK_DGRAM<\/b>; or the error could occur if <i>service<\/i> was not NULL, and <i>hints.ai_socktype<\/i> was <b>SOCK_RAW<\/b> (a socket type that does not support the concept of services).<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_SOCKTYPE<\/b><\/p>\n<p style=\"margin-left:22%;\">The requested socket type is not supported. This could occur, for example, if <i>hints.ai_socktype<\/i> and <i>hints.ai_protocol<\/i> are inconsistent (e.g., <b>SOCK_DGRAM<\/b> and <b>IPPROTO_TCP<\/b>, respectively).<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_SYSTEM<\/b><\/p>\n<p style=\"margin-left:22%;\">Other system error, check <i>errno<\/i> for details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>gai_strerror<\/b>() function translates these error codes to a human readable string, suitable for error reporting.<\/p>\n<h2>FILES <a name=\"FILES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>\/etc\/gai.conf<\/i><\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-1651831.png\" alt=\"Image grohtml-1651831.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">POSIX.1-2001, POSIX.1-2008. The <b>getaddrinfo<\/b>() function is documented in RFC\u00a02553.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getaddrinfo<\/b>() supports the <i>address<\/i><b>%<\/b><i>scope-id<\/i> notation for specifying the IPv6 scope-ID.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>AI_ADDRCONFIG<\/b>, <b>AI_ALL<\/b>, and <b>AI_V4MAPPED<\/b> are available since glibc 2.3.3. <b>AI_NUMERICSERV<\/b> is available since glibc 2.3.4.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">According to POSIX.1, specifying <i>hints<\/i> as NULL should cause <i>ai_flags<\/i> to be assumed as 0. The GNU C library instead assumes a value of <b>(AI_V4MAPPED\u00a0|\u00a0AI_ADDRCONFIG)<\/b> for this case, since this value is considered an improvement on the specification.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following programs demonstrate the use of <b>getaddrinfo<\/b>(), <b>gai_strerror<\/b>(), <b>freeaddrinfo<\/b>(), and <b>getnameinfo<\/b>(3). The programs are an echo server and client for UDP datagrams.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Server program<\/b> <br \/> #include <sys\/types.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h> <br \/> #include <string.h> <br \/> #include <sys\/socket.h> <br \/> #include <netdb.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define BUF_SIZE 500<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> struct addrinfo hints; <br \/> struct addrinfo *result, *rp; <br \/> int sfd, s; <br \/> struct sockaddr_storage peer_addr; <br \/> socklen_t peer_addr_len; <br \/> ssize_t nread; <br \/> char buf[BUF_SIZE];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (argc != 2) { <br \/> fprintf(stderr, &#8220;Usage: %s portn&#8221;, argv[0]); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">memset(&#038;hints, 0, sizeof(hints)); <br \/> hints.ai_family = AF_UNSPEC; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Allow IPv4 or IPv6 bodies\/ usr\/ <br \/> hints.ai_socktype = SOCK_DGRAM; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Datagram socket bodies\/ usr\/ <br \/> hints.ai_flags = AI_PASSIVE; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var For wildcard IP address bodies\/ usr\/ <br \/> hints.ai_protocol = 0; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Any protocol bodies\/ usr\/ <br \/> hints.ai_canonname = NULL; <br \/> hints.ai_addr = NULL; <br \/> hints.ai_next = NULL;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">s = getaddrinfo(NULL, argv[1], &#038;hints, &#038;result); <br \/> if (s != 0) { <br \/> fprintf(stderr, &#8220;getaddrinfo: %sn&#8221;, gai_strerror(s)); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* getaddrinfo() returns a list of address structures. <br \/> Try each address until we successfully bind(2). <br \/> If socket(2) (or bind(2)) fails, we (close the socket <br \/> and) try the next address. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (rp = result; rp != NULL; rp = rp\u2212>ai_next) { <br \/> sfd = socket(rp\u2212>ai_family, rp\u2212>ai_socktype, <br \/> rp\u2212>ai_protocol); <br \/> if (sfd == \u22121) <br \/> continue;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (bind(sfd, rp\u2212>ai_addr, rp\u2212>ai_addrlen) == 0) <br \/> break; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Success *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(sfd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">freeaddrinfo(result); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var No longer needed *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (rp == NULL) { \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var No address succeeded bodies\/ usr\/ <br \/> fprintf(stderr, &#8220;Could not bindn&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Read datagrams and echo them back to sender *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (;;) { <br \/> peer_addr_len = sizeof(peer_addr); <br \/> nread = recvfrom(sfd, buf, BUF_SIZE, 0, <br \/> (struct sockaddr *) &#038;peer_addr, &#038;peer_addr_len); <br \/> if (nread == \u22121) <br \/> continue; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Ignore failed request *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">char host[NI_MAXHOST], service[NI_MAXSERV];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">s = getnameinfo((struct sockaddr *) &#038;peer_addr, <br \/> peer_addr_len, host, NI_MAXHOST, <br \/> service, NI_MAXSERV, NI_NUMERICSERV); <br \/> if (s == 0) <br \/> printf(&#8220;Received %zd bytes from %s:%sn&#8221;, <br \/> nread, host, service); <br \/> else <br \/> fprintf(stderr, &#8220;getnameinfo: %sn&#8221;, gai_strerror(s));<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (sendto(sfd, buf, nread, 0, <br \/> (struct sockaddr *) &#038;peer_addr, <br \/> peer_addr_len) != nread) <br \/> fprintf(stderr, &#8220;Error sending responsen&#8221;); <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Client program<\/b> <br \/> #include <sys\/types.h> <br \/> #include <sys\/socket.h> <br \/> #include <netdb.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h> <br \/> #include <string.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define BUF_SIZE 500<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> struct addrinfo hints; <br \/> struct addrinfo *result, *rp; <br \/> int sfd, s; <br \/> size_t len; <br \/> ssize_t nread; <br \/> char buf[BUF_SIZE];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (argc < 3) { <br \/> fprintf(stderr, &#8220;Usage: %s host port msg&#8230;n&#8221;, argv[0]); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Obtain address(es) matching host\/port *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">memset(&#038;hints, 0, sizeof(hints)); <br \/> hints.ai_family = AF_UNSPEC; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Allow IPv4 or IPv6 bodies\/ usr\/ <br \/> hints.ai_socktype = SOCK_DGRAM; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Datagram socket bodies\/ usr\/ <br \/> hints.ai_flags = 0; <br \/> hints.ai_protocol = 0; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Any protocol *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">s = getaddrinfo(argv[1], argv[2], &#038;hints, &#038;result); <br \/> if (s != 0) { <br \/> fprintf(stderr, &#8220;getaddrinfo: %sn&#8221;, gai_strerror(s)); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* getaddrinfo() returns a list of address structures. <br \/> Try each address until we successfully connect(2). <br \/> If socket(2) (or connect(2)) fails, we (close the socket <br \/> and) try the next address. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (rp = result; rp != NULL; rp = rp\u2212>ai_next) { <br \/> sfd = socket(rp\u2212>ai_family, rp\u2212>ai_socktype, <br \/> rp\u2212>ai_protocol); <br \/> if (sfd == \u22121) <br \/> continue;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (connect(sfd, rp\u2212>ai_addr, rp\u2212>ai_addrlen) != \u22121) <br \/> break; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Success *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(sfd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">freeaddrinfo(result); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var No longer needed *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (rp == NULL) { \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var No address succeeded bodies\/ usr\/ <br \/> fprintf(stderr, &#8220;Could not connectn&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Send remaining command\u2212line arguments as separate <br \/> datagrams, and read responses from server *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (int j = 3; j < argc; j++) { <br \/> len = strlen(argv[j]) + 1; <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var +1 for terminating null byte *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (len > BUF_SIZE) { <br \/> fprintf(stderr, <br \/> &#8220;Ignoring long message in argument %dn&#8221;, j); <br \/> continue; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (write(sfd, argv[j], len) != len) { <br \/> fprintf(stderr, &#8220;partial\/failed writen&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">nread = read(sfd, buf, BUF_SIZE); <br \/> if (nread == \u22121) { <br \/> perror(&#8220;read&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Received %zd bytes: %sn&#8221;, nread, buf); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getaddrinfo_a<\/b>(3), <b>gethostbyname<\/b>(3), <b>getnameinfo<\/b>(3), <b>inet<\/b>(3), <b>gai.conf<\/b>(5), <b>hostname<\/b>(7), <b>ip<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getaddrinfo, freeaddrinfo, gai_strerror \u2212 network address and service translation <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,1800,3007],"class_list":["post-6730","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-getaddrinfo","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6730"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6730\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}