getnameinfo(3)

๐Ÿ’– getnameinfo(3) ๐Ÿ’–

okay bestie, gather round, because we're about to talk about the Library Functions Manual entry for getnameinfo and honestly? it's giving iconic. ๐Ÿฆ‹


โœจ NAME โœจ

getnameinfo โ€” address-to-name translation, but make it protocol-independent. no cap. ๐Ÿ’…


๐ŸŒธ LIBRARY ๐ŸŒธ

Standard C library (that's libc, linked with -lc) ๐ŸŽ€


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <sys/socket.h>
#include <netdb.h>

int getnameinfo(socklen_t hostlen, socklen_t servlen;
                const struct sockaddr *restrict addr, socklen_t addrlen,
                char host[_Nullable restrict hostlen],
                socklen_t hostlen,
                char serv[_Nullable restrict servlen],
                socklen_t servlen,
                int flags);

Feature Test Macro Requirements for glibc (peep feature_test_macros(7) for the full tea):

getnameinfo():

  • Since glibc 2.22:
    • _POSIX_C_SOURCE >= 200112L
  • glibc 2.21 and earlier:
    • _POSIX_C_SOURCE
yes bestie, that synopsis really does look like that. copy it exactly, don't get cute with it. ๐Ÿ™ˆ

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

So getnameinfo() is literally the inverse of getaddrinfo(3) โ€” it takes a socket address and turns it back into a matching host and service, all protocol-independent style. It's basically gethostbyaddr(3) and getservbyport(3) fused together into one queen ๐Ÿ‘‘ โ€” except unlike those two, getnameinfo() is reentrant and lets your program fully eliminate its IPv4-versus-IPv6 dependency drama.

The addr argument is a pointer to a generic socket address structure (type sockaddr_in or sockaddr_in6) of size addrlen, and it's holding your input IP address plus port number. The host and serv arguments are pointers to buffers you allocated (sizes hostlen and servlen respectively), and getnameinfo() drops null-terminated strings containing the host name and service name into them. ๐Ÿซถ

And here's the slay: if you don't need a hostname (or don't need a service name), you can just say so โ€” pass a NULL host (or serv) argument, or a zero hostlen (or servlen). But at least one of hostname or service name must be requested. you can't opt out of both, bestie, pick a lane. ๐Ÿ˜ญ

The flags argument changes how getnameinfo() behaves, like this:

  • NI_NAMEREQD โ€” if set, you get an error back when the hostname just cannot be determined. no vibes, no name, error. ๐Ÿ’€
  • NI_DGRAM โ€” if set, the service is treated as datagram (UDP) based instead of stream (TCP) based. You need this for the few ports (512โ€“514) that have different services for UDP and TCP. iykyk.
  • NI_NOFQDN โ€” if set, you only get the hostname part of the fully qualified domain name, and only for local hosts. โœ‚๏ธ
  • NI_NUMERICHOST โ€” if set, you get the numeric form of the hostname returned. (Even when it's not set, this still happens if the node's name can't be determined โ€” so it's the fallback bestie either way.)
  • NI_NUMERICSERV โ€” if set, you get the numeric form of the service address returned. (Same deal: even when not set, it still happens if the service's name can't be determined.)

๐ŸŒˆ Extensions to getnameinfo() for Internationalized Domain Names ๐ŸŒˆ

Starting with glibc 2.3.4, getnameinfo() got extended so hostnames can be transparently converted to and from the Internationalized Domain Name (IDN) format โ€” selectively, of course, she's not doing anything without consent. See RFC 3490, Internationalizing Domain Names in Applications (IDNA). Three new flags got defined:

  • NI_IDN โ€” if you use this flag, then the name found in the lookup process gets converted from IDN format to the locale's encoding, if that conversion is necessary. ASCII-only names are not affected by the conversion, which is exactly what makes this flag usable in existing programs and environments. ๐Ÿงš
  • NI_IDN_ALLOW_UNASSIGNED, NI_IDN_USE_STD3_ASCII_RULES โ€” setting these flags enables the IDNA_ALLOW_UNASSIGNED flag (allow unassigned Unicode code points) and the IDNA_USE_STD3_ASCII_RULES flag (check the output to make sure it's a STD3 conforming hostname) respectively, to be used in the IDNA handling. two-for-one deal, obsessed. ๐Ÿ’–

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

On success, 0 comes back, and the node and service names (if you requested them) are filled with null-terminated strings โ€” possibly truncated so they fit the buffer lengths you specified. bestie said "make it fit" and she meant it.

On error, you get one of these nonzero error codes instead:

  • EAI_AGAIN โ€” the name could not be resolved at this time. Try again later. โณ
  • EAI_BADFLAGS โ€” the flags argument has an invalid value. ๐Ÿ™…
  • EAI_FAIL โ€” a nonrecoverable error occurred. ๐Ÿ˜ญ
  • EAI_FAMILY โ€” the address family was not recognized, or the address length was invalid for the specified family.
  • EAI_MEMORY โ€” out of memory. ๐Ÿ’€
  • EAI_NONAME โ€” the name does not resolve for the supplied arguments. That means either NI_NAMEREQD is set and the host's name cannot be located, or neither hostname nor service name were requested.
  • EAI_OVERFLOW โ€” the buffer pointed to by host or serv was too small. ๐Ÿ˜ฌ
  • EAI_SYSTEM โ€” a system error occurred. The error code can be found in errno.

And for turning those codes into something human-readable and suitable for error reporting? That's gai_strerror(3), your translator bestie. ๐ŸŽ€


๐Ÿ“ FILES ๐Ÿ“

  • /etc/hosts
  • /etc/nsswitch.conf
  • /etc/resolv.conf

โญ ATTRIBUTES โญ

For an explanation of the terms used in this section, peep attributes(7).

  • Interface: getnameinfo()
  • Attribute: Thread safety
  • Value: MT-Safe env locale
so yeah, she's MT-Safe, with the note that it's env locale โ€” translation: environment and locale sensitivities are in play. ๐Ÿ’…

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2008. RFC 2553. certified classic, no notes. โœจ


๐ŸŒธ HISTORY ๐ŸŒธ

  • glibc 2.1.
  • POSIX.1-2001.

And before glibc 2.2, the hostlen and servlen arguments were typed as size_t. glow-up era. ๐Ÿ’…


๐Ÿฆ‹ NOTES ๐Ÿฆ‹

To help you (the programmer, the main character) pick reasonable sizes for the buffers you're handing over, <netdb.h> defines these constants:

#define NI_MAXHOST      1025
#define NI_MAXSERV      32

Since glibc 2.8, these definitions are only exposed if suitable feature test macros are defined, namely: _GNU_SOURCE, _DEFAULT_SOURCE (since glibc 2.19), or โ€” in glibc versions up to and including 2.19 โ€” _BSD_SOURCE or _SVID_SOURCE.

The former (NI_MAXHOST) is the constant MAXDNAME in recent versions of BIND's <arpa/nameser.h> header file. The latter (NI_MAXSERV) is a guess based on the services listed in the current Assigned Numbers RFC. she's estimating, and she's honest about it. ๐Ÿ™ˆ


๐Ÿ’– EXAMPLES ๐Ÿ’–

Okay so this first snippet tries to get the numeric hostname and service name for a given socket address. Notice there is no hardcoded reference to a particular address family โ€” that's the whole point, bestie:

struct sockaddr *addr;     /* input */
socklen_t addrlen;         /* input */
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), sbuf,
            sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
    printf("host=%s, serv=%s\n", hbuf, sbuf);

And this next version checks whether the socket address has a reverse address mapping:

struct sockaddr *addr;     /* input */
socklen_t addrlen;         /* input */
char hbuf[NI_MAXHOST];

if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf),
            NULL, 0, NI_NAMEREQD))
    printf("could not resolve hostname");
else
    printf("host=%s\n", hbuf);

Also โ€” an example program using getnameinfo() can be found over in getaddrinfo(3). go peek, she ate. ๐Ÿ”ฅ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

accept(2), getpeername(2), getsockname(2), recvfrom(2), socket(2), getaddrinfo(3), gethostbyaddr(3), getservbyname(3), getservbyport(3), inet_ntop(3), hosts(5), services(5), hostname(7), named(8)

R. Gilligan, S. Thomson, J. Bound and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC 2553, March 1999.

Tatsuya Jinmei and Atsushi Onoe, An Extension of Format for IPv6 Scoped Addresses, internet draft, work in progress ftp://ftp.ietf.org/internet-drafts/draft-ietf-ipngwg-scopedaddr-format-02.txt.

Craig Metz, Protocol Independence Using the Sockets API, Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000 http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html.


and that's the whole page, bestie!! ๐ŸŽ€ this was the Linux man-pages 6.20 version, dated 2025-12-25 โ€” a lil christmas gift of socket knowledge, fr fr. go forth and translate those addresses. mwah. ๐Ÿ’…๐Ÿ’–๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms