GETNAMEINFO(3POSIX)
π GETNAMEINFO(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual page for getnameinfo β yes, that manual, the IEEE/The Open Group one, 2017 edition. pull up a chair πͺβ¨
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β so if you're on Linux, go peek at the corresponding Linux manual page for the deets on Linux behavior. or... the interface might just not be implemented on Linux at all. π it's giving "check your platform first, babe."
β¨ NAME β¨
getnameinfo β get name information. that's it, that's the girl π
π SYNOPSIS π
#include <sys/socket.h>
#include <netdb.h>
int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
char *restrict node, socklen_t nodelen, char *restrict service,
socklen_t servicelen, int flags);
π¦ DESCRIPTION π¦
so the getnameinfo() function shall translate a socket address into a node name and a service location β and all of those lil concepts are defined the same way they are in freeaddrinfo(). iykyk π
the sa argument points to a socket address structure that we're translating. the salen argument holds the length of the address that sa points to.
now, if the socket address structure contains an IPv4-mapped IPv6 address or an IPv4-compatible IPv6 address, the implementation shall extract the embedded IPv4 address and look up the node name for that IPv4 address. sneaky but iconic.
if the address is the IPv6 unspecified address ("::"), no lookup shall be performed, and the behavior shall be the same as when the node's name cannot be located. so basically, nothing to see here, moving on π«π
the node buffer π«Ά
if the node argument is non-NULL and the nodelen argument is non-zero, then node points to a buffer that can hold up to nodelen bytes, and it receives the node name as a null-terminated string. if node is NULL or nodelen is zero, the node name shall not be returned. and if the node's name cannot be located? then the numeric form of the address inside the socket address structure pointed to by sa gets returned instead of its name. fallback queen π
the service buffer π
if the service argument is non-NULL and the servicelen argument is non-zero, then service points to a buffer that can hold up to servicelen bytes, and it receives the service name as a null-terminated string. if service is NULL or servicelen is zero, the service name shall not be returned. and if the service's name cannot be located, the numeric form of the service address (think: its port number) shall be returned instead of its name. same energy, different buffer β¨
the flags, aka the personality settings π₯
the flags argument is a flag that changes the default actions of the function. by default, the fully-qualified domain name (FQDN) for the host shall be returned. but:
- if the flag bit
NI_NOFQDNis set, only the node name portion of the FQDN shall be returned for local hosts. - if the flag bit
NI_NUMERICHOSTis set, the numeric form of the address contained in the socket address structure pointed to by thesaargument shall be returned instead of its name. - if the flag bit
NI_NAMEREQDis set, an error shall be returned if the host's name cannot be located. - if the flag bit
NI_NUMERICSERVis set, the numeric form of the service address shall be returned (for example, its port number) instead of its name. - if the flag bit
NI_NUMERICSCOPEis set, the numeric form of the scope identifier shall be returned (for example, interface index) instead of its name. this flag shall be ignored if thesaargument is not an IPv6 address. - if the flag bit
NI_DGRAMis set, this indicates that the service is a datagram service (SOCK_DGRAM). the default behavior shall assume that the service is a stream service (SOCK_STREAM).
notes π
- the two
NI_NUMERICxxxflags are required to support the-nflag that many commands provide. - the
NI_DGRAMflag is required for the fewAF_INETandAF_INET6port numbers (for example,[512,514]) that represent different services for UDP and TCP.
also the getnameinfo() function shall be thread-safe. go off, multithreaded icon π§΅π
π RETURN VALUE π
a zero return value for getnameinfo() indicates successful completion; a non-zero return value indicates failure. the possible values for the failures are listed in the ERRORS section (scroll down bestie, they're right there).
upon successful completion, getnameinfo() shall return the node and service names, if requested, in the buffers provided. and the returned names are always null-terminated strings. always. periodt. π
π ERRORS π
the getnameinfo() function shall fail and return the corresponding value if:
[EAI_AGAIN]β the name could not be resolved at this time. future attempts may succeed. (it's not a no, it's a not-right-now π₯²)[EAI_BADFLAGS]β the flags had an invalid value.[EAI_FAIL]β a non-recoverable error occurred.[EAI_FAMILY]β the address family was not recognized, or the address length was invalid for the specified family.[EAI_MEMORY]β there was a memory allocation failure.[EAI_NONAME]β the name does not resolve for the supplied parameters.NI_NAMEREQDis set and the host's name cannot be located, or bothnodenameandservnamewere null.
[EAI_OVERFLOW]β an argument buffer overflowed. the buffer pointed to by thenodeargument or theserviceargument was too small. size matters, unfortunately π[EAI_SYSTEM]β a system error occurred. the error code can be found inerrno.
the following sections are informative. (translation: the drama is over, this is bonus content π)
π EXAMPLES π
None. (yes really. nothing. the void. π)
π APPLICATION USAGE π
if the returned values are gonna be used as part of any further name resolution (like, say, passed to getaddrinfo()), applications should provide buffers large enough to store any result possible on the system. don't be stingy with your buffers, bestie π«Ά
given the IPv4-mapped IPv6 address "::ffff:1.2.3.4", the implementation performs a lookup as if the socket address structure contains the IPv4 address "1.2.3.4".
and important: the IPv6 unspecified address ("::") and the IPv6 loopback address ("::1") are not IPv4-compatible addresses. don't get it twisted π«
πΈ RATIONALE πΈ
None. (another empty one. the silence is loud. π)
β FUTURE DIRECTIONS β
None. (nothing on the horizon, we live in the now β¨)
π SEE ALSO π
endservent()freeaddrinfo()gai_strerror()inet_ntop()socket()
the Base Definitions volume of POSIX.1β2017, <netdb.h>, <sys_socket.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. (they're the final boss, no cap π)
the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole thing, babe π¦ β brought to you by IEEE/The Open Group, 2017, signing off on GETNAMEINFO(3POSIX). stay resolving, stay iconic, and remember: null-terminate your strings. mwah π
πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms