GETNAMEINFO(3P)
π« GETNAMEINFO(3P) π«
okay bestie, gather round, we're reading the POSIX Programmer's Manual page for getnameinfo(3P) and honestly? she's iconic. this is the POSIX flavor, not the Linux one β the Linux version of this interface may lowkey differ (go check the corresponding Linux man page for the deets on Linux behavior), or the interface might not even be implemented on Linux at all. so yeah. keep that in mind. π«Ά
πΈ PROLOG πΈ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
no cap, that's literally the prolog. it's giving "read the fine print bestie".
β¨ NAME β¨
getnameinfo β get name information
π 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 like, what does getnameinfo() actually do? She translates a socket address into a node name and a service location β and both of those are defined the same way as in freeaddrinfo(). slay. π
The sa argument points to a socket address structure that's about to get translated. The salen argument holds the length of the address that sa points to.
If that 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.
If the address is the IPv6 unspecified address ("::"), a lookup shall not be performed and the behavior shall be the same as when the node's name cannot be located.
If node is non-NULL and nodelen is non-zero, then node points to a buffer able to hold up to nodelen bytes that 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? You get the numeric form of the address inside the socket address structure that sa points to, instead of its name. bestie really said "no name? fine, numbers it is". π
If service is non-NULL and servicelen is non-zero, then service points to a buffer able to hold up to servicelen bytes that receives the service name as a null-terminated string. If service is NULL or servicelen is zero, the service name shall not be returned. If the service's name cannot be located, the numeric form of the service address (for example, its port number) shall be returned instead of its name.
The flags argument is the little dial 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 bysashall 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 β and this is the good news β the getnameinfo() function shall be thread-safe. periodt. π
π RETURN VALUE π
A zero return value for getnameinfo() means successful completion π; a non-zero return value means failure. The possible failure values are all listed in the ERRORS section, keep scrolling.
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. no exceptions. π
π₯ 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.[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.[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.
not a single example. she said "figure it out bestie". π
π APPLICATION USAGE π
If the returned values are gonna be used as part of any further name resolution (for example, passed to getaddrinfo()), applications should provide buffers large enough to store any result possible on the system. do not skimp on the buffer size, i'm begging.
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".
The IPv6 unspecified address ("::") and the IPv6 loopback address ("::1") are not IPv4-compatible addresses. don't get it twisted. π
π¦ RATIONALE π¦
None.
β FUTURE DIRECTIONS β
None.
π 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. 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 page bestie, mwah π this edition was brought to you by IEEE/The Open Group, dated 2017 β so when you cite her, you cite her properly. stay null-terminated, stay thread-safe, and go resolve something cute. π«Άβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms