INET_NTOP(3P)
๐ inet_ntop, inet_pton โ bestie, let's translate addresses ๐
okay so real quick, this page is part of the POSIX Programmer's Manual, and you're reading the INET_NTOP(3P) one. Let's get into it ๐
๐จ PROLOG ๐จ
This manual page is part of the POSIX Programmer's Manual. Heads up though: the Linux implementation of this interface may differ โ so if you're on Linux, go peek at the corresponding Linux manual page for the actual Linux behavior. Or, plot twist, the interface might not even be implemented on Linux at all. ๐ Not me warning you twice.
โจ NAME โจ
inet_ntop, inet_pton โ the little duo that converts IPv4 and IPv6 addresses between binary form and text form. That's it, that's their whole job, and they eat every time. ๐
๐ธ SYNOPSIS ๐ธ
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *restrict src,
char *restrict dst, socklen_t size);
int inet_pton(int af, const char *restrict src, void *restrict dst);
๐ DESCRIPTION ๐
inet_ntop() โ binary โก๏ธ text, the glow-up
The inet_ntop() function shall convert a numeric address into a text string suitable for presentation. The af argument shall specify the family of the address, and it can be AF_INET or AF_INET6.
The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET, or an IPv6 address if the af argument is AF_INET6 โ and bestie, that address must be in network byte order, no exceptions.
The dst argument points to a buffer where the function stores the resulting text string, and it shall not be NULL. The size argument specifies the size of this buffer, which shall be large enough to hold the text string โ that's INET_ADDRSTRLEN characters for IPv4, and INET6_ADDRSTRLEN characters for IPv6. Size it right or catch these hands (well, catch ENOSPC). ๐
inet_pton() โ text โก๏ธ binary, the reverse slay
The inet_pton() function shall convert an address in its standard text presentation form into its numeric binary form. The af argument shall specify the family of the address, and the AF_INET and AF_INET6 address families shall be supported.
The src argument points to the string being passed in. The dst argument points to a buffer into which the function stores the numeric address; this shall be large enough to hold the numeric address โ that's 32 bits for AF_INET, 128 bits for AF_INET6.
๐ if af is AF_INET ๐
If the af argument of inet_pton() is AF_INET, the src string shall be in the standard IPv4 dotted-decimal form:
ddd.ddd.ddd.ddd
where "ddd" is a one to three digit decimal number between 0 and 255 (see inet_addr()).
โ ๏ธ Important tea: theinet_pton()function does not accept other formats โ not the octal numbers, not the hexadecimal numbers, and not the fewer-than-four-numbers thing thatinet_addr()accepts.inet_addr()is out here being messy;inet_pton()said no ma'am. ๐ โโ๏ธ
๐ฆ if af is AF_INET6 ๐ฆ
If the af argument of inet_pton() is AF_INET6, the src string shall be in one of the following standard IPv6 text forms:
- The preferred form is
"x:x:x:x:x:x:x:x", where the 'x's are the hexadecimal values of the eight 16-bit pieces of the address. Leading zeros in individual fields can be omitted, but there shall be one to four hexadecimal digits in every field. - A string of contiguous zero fields in the preferred form can be shown as
"::". The"::"can only appear once in an address. Unspecified addresses ("0:0:0:0:0:0:0:0") may be represented simply as"::". - A third form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is
"x:x:x:x:x:x:d.d.d.d", where the 'x's are the hexadecimal values of the six high-order 16-bit pieces of the address, and the 'd's are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation).
๐ Note: a more extensive description of the standard representations of IPv6 addresses can be found in RFC 2373. iykyk ๐
๐ซถ RETURN VALUE ๐ซถ
inet_ntop()โ shall return a pointer to the buffer containing the text string if the conversion succeeds, andNULLotherwise, and seterrnoto indicate the error.inet_pton()โ shall return1if the conversion succeeds, with the address pointed to bydstin network byte order. It shall return0if the input is not a valid IPv4 dotted-decimal string or a valid IPv6 address string, or-1witherrnoset to[EAFNOSUPPORT]if theafargument is unknown.
๐ญ ERRORS ๐ญ
The inet_ntop() and inet_pton() functions shall fail if:
EAFNOSUPPORTโ Theafargument is invalid. (bestie... that family does not exist ๐)ENOSPCโ The size of theinet_ntop()result buffer is inadequate. (your buffer was too small, tragic ๐)
The following sections are informative. (aka, the vibes-only section of this page โจ)
๐ EXAMPLES ๐
None. Literally none. The examples said "not today." ๐ซ
๐ธ APPLICATION USAGE ๐ธ
None. Same energy. ๐
๐ RATIONALE ๐
None. We're keeping it mysterious. ๐ง
โญ FUTURE DIRECTIONS โญ
None. The future is... unspecified. ๐ฎ
๐ SEE ALSO ๐
The Base Definitions volume of POSIX.1โ2017, <arpa_inet.h>. Go read those, they're iconic too. ๐
๐ 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 .
okay that's the whole page, mwah ๐ โ this slay was brought to you by IEEE/The Open Group, dated 2017, and yes it's still serving INET_NTOP(3P). periodt. ๐
๐ฆ 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