inet_net_pton(3)
π inet_net_pton β Internet network number conversion π
okay bestie, welcome to the Library Functions Manual section, because this little icon lives in the library functions pages (specifically the resolver library, libresolv, -lresolv). Let me put you ON to the two functions that flip network numbers back and forth between "human can read it" and "computer can read it." no cap, it's giving translator core. π¦
β¨ NAME β¨
inet_net_pton, inet_net_ntop β Internet network number conversion
So basically: pton is presentation β network, and ntop is network β presentation. the way that naming scheme ate. π
π LIBRARY π
Resolver library (libresolv, -lresolv)
πΈ SYNOPSIS πΈ
#include <arpa/inet.h>
int inet_net_pton(size_t nsize;
int af, const char *pres,
void netp[nsize], size_t nsize);
char *inet_net_ntop(int bits, size_t psize;
int af,
const void netp[(bits - CHAR_BIT + 1) / CHAR_BIT],
int bits,
char pres[psize], size_t psize);
π Feature Test Macro Requirements for glibc (see feature_test_macros(7)) π
inet_net_pton(), inet_net_ntop():
- Since glibc 2.20:
_DEFAULT_SOURCE
- Before glibc 2.20:
_BSD_SOURCE || _SVID_SOURCE
like yes you gotta declare which era of glibc you're living in before it lets you in. gatekeeping fr. π
π« DESCRIPTION π«
These functions convert network numbers between presentation (aka printable, aka the cute readable version) format and network (aka binary, aka the raw bytes version) format.
For both functions, af specifies the address family for the conversion; the only supported value is AF_INET. that's it. that's the whole guest list. π
π§ inet_net_pton() π§
inet_net_pton() converts pres β a null-terminated string holding an Internet network number in presentation format β into network format. The result of the conversion, which is in network byte order, gets placed in the buffer pointed to by netp. (The netp argument typically points to an in_addr structure, iykyk.) The nsize argument says how many bytes are actually available in netp.
On success, inet_net_pton() returns the number of bits in the network number field of the result it dropped into netp. For a whole discussion of the input presentation format and the return value, go look at NOTES below, bestie.
Note: the buffer thatnetppoints to should be zeroed out before you callinet_net_pton(), because the call writes only as many bytes as are required for the network number (or as are explicitly specified bypres) β which may be fewer than the number of bytes in a complete network address. so like, if you don't zero it, you're gonna have mystery leftovers in there. lowkey a jump scare. π
π«Ά inet_net_ntop() π«Ά
inet_net_ntop() converts the network number in the buffer pointed to by netp into presentation format; *netp is interpreted as a value in network byte order. The bits argument specifies the number of bits in the network number in *netp.
The null-terminated presentation-format string gets placed in the buffer pointed to by pres. The psize argument specifies the number of bytes available in pres. The presentation string comes out in CIDR format: a dotted-decimal number representing the network address, followed by a slash, and then the size of the network number in bits. periodt. π
π₯ RETURN VALUE π₯
On success, inet_net_pton() returns the number of bits in the network number. On error, it returns -1, and errno is set to indicate the error. main character exit code energy. π
On success, inet_net_ntop() returns pres. On error, it returns NULL, and errno is set to indicate the error.
π ERRORS π
EAFNOSUPPORTβafspecified a value other thanAF_INET. girl, that's not on the list.EMSGSIZEβ the size of the output buffer was insufficient. not enough room, so sorry. πENOENT(this one isinet_net_pton()only) βpreswas not in correct presentation format.
β STANDARDS β
None.
(yeah. none. it's giving "we make our own rules." slay, honestly.) no POSIX, no ISO, just vibes. π
π¦ NOTES π¦
πΈ Input presentation format for inet_net_pton() πΈ
The network number may be specified either as a hexadecimal value or in dotted-decimal notation. two doors, pick your fighter.
Hexadecimal values are indicated by an initial "0x" or "0X". The hexadecimal digits populate the nibbles (half octets) of the network number from left to right in network byte order. so like, one hex digit per half-byte, marching left to right. obsessed. β¨
In dotted-decimal notation, up to four octets are specified, as decimal numbers separated by dots. So any of these forms are accepted:
a.b.c.d
a.b.c
a.b
a
Each part is a number in the range 0 to 255 that populates one byte of the resulting network number, going from left to right, in network-byte (big endian) order. Where a part is omitted, the resulting byte in the network number is simply zero. (the missing ones just get ghosted. π»)
For either hexadecimal or dotted-decimal format, the network number can optionally be followed by a slash and a number in the range 0 to 32, which specifies the size of the network number in bits. optional but iconic. π
π§ Return value of inet_net_pton() π§
The return value of inet_net_pton() is the number of bits in the network number field. If the input presentation string terminates with a slash and an explicit size value, then that size becomes the return value of inet_net_pton(). Otherwise, the return value, bits, is inferred as follows:
- If the most significant byte of the network number is greater than or equal to 240, then
bitsis 32. - Otherwise, if the most significant byte of the network number is greater than or equal to 224, then
bitsis 4. - Otherwise, if the most significant byte of the network number is greater than or equal to 192, then
bitsis 24. - Otherwise, if the most significant byte of the network number is greater than or equal to 128, then
bitsis 16. - Otherwise,
bitsis 8.
If the resulting bits value from the above steps is greater than or equal to 8, but the number of octets specified in the network number exceed bits/8, then bits is set to 8 times the number of octets actually specified. so it grows to fit what you actually wrote. it's giving β¨adaptableβ¨.
π EXAMPLES π
The program below demonstrates the use of inet_net_pton() and inet_net_ntop(). It uses inet_net_pton() to convert the presentation format network address provided in its first command-line argument to binary form, displays the return value from inet_net_pton(). It then uses inet_net_ntop() to convert the binary form back to presentation format, and displays the resulting string.
In order to demonstrate that inet_net_pton() may not write to all bytes of its netp argument, the program allows an optional second command-line argument, a number used to initialize the buffer before inet_net_pton() is called. As its final line of output, the program displays all of the bytes of the buffer returned by inet_net_pton() allowing the user to see which bytes have not been touched by inet_net_pton(). literally the "who got left on read" test. π
An example run, showing that inet_net_pton() infers the number of bits in the network number:
$ ./a.out 193.168;
inet_net_pton() returned: 24
inet_net_ntop() yielded: 193.168.0/24
Raw address: c1a80000
Demonstrate that inet_net_pton() does not zero out unused bytes in its result buffer:
$ ./a.out 193.168 0xffffffff;
inet_net_pton() returned: 24
inet_net_ntop() yielded: 193.168.0/24
Raw address: c1a800ff
Demonstrate that inet_net_pton() will widen the inferred size of the network number, if the supplied number of bytes in the presentation string exceeds the inferred value:
$ ./a.out 193.168.1.128;
inet_net_pton() returned: 32
inet_net_ntop() yielded: 193.168.1.128/32
Raw address: c1a80180
Explicitly specifying the size of the network number overrides any inference about its size (but any extra bytes that are explicitly specified will still be used by inet_net_pton(): to populate the result buffer):
$ ./a.out 193.168.1.128/24;
inet_net_pton() returned: 24
inet_net_ntop() yielded: 193.168.1/24
Raw address: c1a80180
π Program source π
/* Link with "-lresolv" */
#include <arpa/inet.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
char buf[100];
struct in_addr addr;
int bits;
if (argc < 2) {
fprintf(stderr,
"Usage: %s presentation-form [addr-init-value]\n",
argv[0]);
exit(EXIT_FAILURE);
}
/* If argv[2] is supplied (a numeric value), use it to initialize
the output buffer given to inet_net_pton(), so that we can see
that inet_net_pton() initializes only those bytes needed for
the network number. If argv[2] is not supplied, then initialize
the buffer to zero (as is recommended practice). */
addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0;
/* Convert presentation network number in argv[1] to binary. */
bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr));
if (bits == -1)
err(EXIT_FAILURE, "inet_net_ntop");
printf("inet_net_pton() returned: %d\n", bits);
/* Convert binary format back to presentation, using 'bits'
returned by inet_net_pton(). */
if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL)
err(EXIT_FAILURE, "inet_net_ntop");
printf("inet_net_ntop() yielded: %s\n", buf);
/* Display 'addr' in raw form (in network byte order), so we can
see bytes not displayed by inet_net_ntop(); some of those bytes
may not have been touched by inet_net_ntop(), and so will still
have any initial value that was specified in argv[2]. */
printf("Raw address: %x\n", htonl(addr.s_addr));
exit(EXIT_SUCCESS);
}
π SEE ALSO π
and that's the whole page, bestie!! you now know more about network number conversion than like 90% of people who confidently talk about it. π
this was Linux man-pages 6.20, dated 2026-02-08, from the inet_net_pton(3) page β so if you ever need to cite your sources, there they are. go forth and convert, my icon. πβ¨
π¦ 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
- π the page's license: Linux-man-pages-copyleft
- Copyright 2014, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms