{"id":6916,"date":"2022-12-20T19:34:20","date_gmt":"2022-12-20T22:34:20","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getnameinfo-man3\/"},"modified":"2022-12-20T19:34:20","modified_gmt":"2022-12-20T22:34:20","slug":"getnameinfo-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getnameinfo-man3\/","title":{"rendered":"GETNAMEINFO (man3)"},"content":{"rendered":"<h1 align=\"center\">GETNAMEINFO<\/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=\"#VERSIONS\">VERSIONS<\/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\">getnameinfo \u2212 address-to-name translation in protocol-independent manner<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/socket.h> <br \/> #include <netdb.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>int getnameinfo(const struct sockaddr *<\/b><i>addr<\/i><b>, socklen_t<\/b> <i>addrlen<\/i><b>, <br \/> char *<\/b><i>host<\/i><b>, socklen_t<\/b> <i>hostlen<\/i><b>, <br \/> char *<\/b><i>serv<\/i><b>, socklen_t<\/b> <i>servlen<\/i><b>, int<\/b> <i>flags<\/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>getnameinfo<\/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\">The <b>getnameinfo<\/b>() function is the inverse of <b>getaddrinfo<\/b>(3): it converts a socket address to a corresponding host and service, in a protocol-independent manner. It combines the functionality of <b>gethostbyaddr<\/b>(3) and <b>getservbyport<\/b>(3), but unlike those functions, <b>getnameinfo<\/b>() is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>addr<\/i> argument is a pointer to a generic socket address structure (of type <i>sockaddr_in<\/i> or <i>sockaddr_in6<\/i>) of size <i>addrlen<\/i> that holds the input IP address and port number. The arguments <i>host<\/i> and <i>serv<\/i> are pointers to caller-allocated buffers (of size <i>hostlen<\/i> and <i>servlen<\/i> respectively) into which <b>getnameinfo<\/b>() places null-terminated strings containing the host and service names respectively.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The caller can specify that no hostname (or no service name) is required by providing a NULL <i>host<\/i> (or <i>serv<\/i>) argument or a zero <i>hostlen<\/i> (or <i>servlen<\/i>) argument. However, at least one of hostname or service name must be requested.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>flags<\/i> argument modifies the behavior of <b>getnameinfo<\/b>() as follows: <b><br \/> NI_NAMEREQD<\/b><\/p>\n<p style=\"margin-left:22%;\">If set, then an error is returned if the hostname cannot be determined.<\/p>\n<p style=\"margin-left:11%;\"><b>NI_DGRAM<\/b><\/p>\n<p style=\"margin-left:22%;\">If set, then the service is datagram (UDP) based rather than stream (TCP) based. This is required for the few ports (512\u2013514) that have different services for UDP and TCP.<\/p>\n<p style=\"margin-left:11%;\"><b>NI_NOFQDN<\/b><\/p>\n<p style=\"margin-left:22%;\">If set, return only the hostname part of the fully qualified domain name for local hosts.<\/p>\n<p style=\"margin-left:11%;\"><b>NI_NUMERICHOST<\/b><\/p>\n<p style=\"margin-left:22%;\">If set, then the numeric form of the hostname is returned. (When not set, this will still happen in case the node\u2019s name cannot be determined.)<\/p>\n<p style=\"margin-left:11%;\"><b>NI_NUMERICSERV<\/b><\/p>\n<p style=\"margin-left:22%;\">If set, then the numeric form of the service address is returned. (When not set, this will still happen in case the service\u2019s name cannot be determined.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Extensions to getnameinfo() for Internationalized Domain Names<\/b> <br \/> Starting with glibc 2.3.4, <b>getnameinfo<\/b>() has been extended to selectively allow 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>). Three 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><b>NI_IDN<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>If this flag is used, then the name found in the lookup process is converted from IDN format to the locale\u2019s encoding if necessary. ASCII-only names are not affected by the conversion, which makes this flag usable in existing programs and environments.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>NI_IDN_ALLOW_UNASSIGNED<\/b>, <b>NI_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\">On success, 0 is returned, and node and service names, if requested, are filled with null-terminated strings, possibly truncated to fit the specified buffer lengths. On error, one of the following nonzero error codes is returned: <b><br \/> EAI_AGAIN<\/b><\/p>\n<p style=\"margin-left:22%;\">The name could not be resolved at this time. Try again later.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_BADFLAGS<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>flags<\/i> argument has an invalid value.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_FAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">A nonrecoverable error occurred.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_FAMILY<\/b><\/p>\n<p style=\"margin-left:22%;\">The address family was not recognized, or the address length was invalid for the specified family.<\/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_NONAME<\/b><\/p>\n<p style=\"margin-left:22%;\">The name does not resolve for the supplied arguments. <b>NI_NAMEREQD<\/b> is set and the host\u2019s name cannot be located, or neither hostname nor service name were requested.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_OVERFLOW<\/b><\/p>\n<p style=\"margin-left:22%;\">The buffer pointed to by <i>host<\/i> or <i>serv<\/i> was too small.<\/p>\n<p style=\"margin-left:11%;\"><b>EAI_SYSTEM<\/b><\/p>\n<p style=\"margin-left:22%;\">A system error occurred. The error code can be found in <i>errno<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>gai_strerror<\/b>(3) 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\/hosts <br \/> \/etc\/nsswitch.conf <br \/> \/etc\/resolv.conf<\/i><\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><b>getnameinfo<\/b><\/i>() is provided in glibc since version 2.1.<\/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-1661341.png\" alt=\"Image grohtml-1661341.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, RFC\u00a02553.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In order to assist the programmer in choosing reasonable sizes for the supplied buffers, <i><netdb.h><\/i> defines the constants<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#define NI_MAXHOST 1025 <br \/> #define NI_MAXSERV 32<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Since glibc 2.8, these definitions are exposed only if suitable feature test macros are defined, namely: <b>_GNU_SOURCE<\/b>, <b>_DEFAULT_SOURCE<\/b> (since glibc 2.19), or (in glibc versions up to and including 2.19) <b>_BSD_SOURCE<\/b> or <b>_SVID_SOURCE<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The former is the constant <b>MAXDNAME<\/b> in recent versions of BIND\u2019s <i><arpa\/nameser.h><\/i> header file. The latter is a guess based on the services listed in the current Assigned Numbers RFC.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Before glibc version 2.2, the <i>hostlen<\/i> and <i>servlen<\/i> arguments were typed as <i>size_t<\/i>.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following code tries to get the numeric hostname and service name, for a given socket address. Note that there is no hardcoded reference to a particular address family.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct sockaddr *addr; \/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 input bodies\/ usr\/ <br \/> socklen_t addrlen; \/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 input bodies\/ usr\/ <br \/> char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf, <br \/> sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) <br \/> printf(&#8220;host=%s, serv=%sn&#8221;, hbuf, sbuf);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following version checks if the socket address has a reverse address mapping.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct sockaddr *addr; \/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 input bodies\/ usr\/ <br \/> socklen_t addrlen; \/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 input bodies\/ usr\/ <br \/> char hbuf[NI_MAXHOST];<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), <br \/> NULL, 0, NI_NAMEREQD)) <br \/> printf(&#8220;could not resolve hostname&#8221;); <br \/> else <br \/> printf(&#8220;host=%sn&#8221;, hbuf);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An example program using <b>getnameinfo<\/b>() can be found in <b>getaddrinfo<\/b>(3).<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>accept<\/b>(2), <b>getpeername<\/b>(2), <b>getsockname<\/b>(2), <b>recvfrom<\/b>(2), <b>socket<\/b>(2), <b>getaddrinfo<\/b>(3), <b>gethostbyaddr<\/b>(3), <b>getservbyname<\/b>(3), <b>getservbyport<\/b>(3), <b>inet_ntop<\/b>(3), <b>hosts<\/b>(5), <b>services<\/b>(5), <b>hostname<\/b>(7), <b>named<\/b>(8)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">R. Gilligan, S. Thomson, J. Bound and W. Stevens, <i>Basic Socket Interface Extensions for IPv6<\/i>, RFC\u00a02553, March 1999.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Tatsuya Jinmei and Atsushi Onoe, <i>An Extension of Format for IPv6 Scoped Addresses<\/i>, internet draft, work in progress <a href=\"ftp:\/\/ftp.ietf.org\/internet-drafts\/draft-ietf-ipngwg-scopedaddr-format-02.txt\">ftp:\/\/ftp.ietf.org\/internet-drafts\/draft-ietf-ipngwg-scopedaddr-format-02.txt<\/a>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Craig Metz, <i>Protocol Independence Using the Sockets API<\/i>, Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000 <a href=\"http:\/\/www.usenix.org\/publications\/library\/proceedings\/usenix2000\/freenix\/metzprotocol.html\">http:\/\/www.usenix.org\/publications\/library\/proceedings\/usenix2000\/freenix\/metzprotocol.html<\/a>.<\/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>  getnameinfo \u2212 address-to-name translation in protocol-independent manner <\/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,2141,3007],"class_list":["post-6916","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-getnameinfo","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6916","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=6916"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6916\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}