inet_pton(3)
โจ inet_pton ๐ (that's her, from section 3 of the Library Functions Manual, bestie)
okay so. real talk. this is the lil libc function that takes an IP address you typed as text and turns it into the crunchy binary form the network actually wants. no cap, she's the glow-up translator of the internet. ๐
๐ธ NAME ๐ธ
inet_pton โ convert IPv4 and IPv6 addresses from text to binary form
๐ LIBRARY ๐
Standard C library (libc, -lc)
link her in, she's standard, she's free, she's mother. ๐
โจ SYNOPSIS โจ
#include <arpa/inet.h>
int inet_pton(int af, const char *restrict src, void *restrict dst);
๐ DESCRIPTION ๐
so here's the vibe: inet_pton() takes the character string src, converts it into a network address structure that belongs to the af address family, and then copies that whole network address structure straight into dst. simple. iconic.
the af argument has to be either AF_INET or AF_INET6 โ no third options, she's picky and honestly? valid.
dst gets written in network byte order. periodt.
these are the address families she's currently supporting (yes bestie, currently):
AF_INET ๐
src points at a character string holding an IPv4 network address in dotted-decimal format:
"ddd.ddd.ddd.ddd"
where each ddd is a decimal number of up to three digits, in the range 0 to 255.
the address gets converted to a struct in_addr and copied to dst, and dst must be sizeof(struct in_addr) (4) bytes (32 bits) long. don't skimp on the buffer, i'm begging. ๐ญ
AF_INET6 ๐ฆ
src points at a character string holding an IPv6 network address.
the address gets converted to a struct in6_addr and copied to dst, and dst must be sizeof(struct in6_addr) (16) bytes (128 bits) long.
the allowed formats for IPv6 addresses follow these rules (rules are rules, bestie):
- the preferred format is
x:x:x:x:x:x:x:x. this form consists of eight hexadecimal numbers, each of which expresses a 16-bit value (i.e., eachxcan be up to 4 hex digits). - a series of contiguous zero values in the preferred format can be abbreviated to
::. only one instance of::can occur in an address. for example, the loopback address0:0:0:0:0:0:0:1can be abbreviated as::1. the wildcard address, which is all zeros, can be written as::. the way that::just eats all those zeros... she's efficient, she's minimal, she's giving compression algorithm energy. ๐ - an alternate format is useful for expressing IPv4-mapped IPv6 addresses. this form is written as
x:x:x:x:x:x:d.d.d.d, where the six leadingxs are hexadecimal values that define the six most-significant 16-bit pieces of the address (i.e., 96 bits), and theds express a value in dotted-decimal notation that defines the least significant 32 bits of the address. an example of such an address is::FFFF:204.152.189.116.
see RFC 2373 for further details on the representation of IPv6 addresses. iykyk. ๐
๐ฅ RETURN VALUE ๐ฅ
1โ returned on success (the network address was successfully converted). slay!! ๐0โ returned ifsrcdoes not contain a character string representing a valid network address in the specified address family. she looked at it and said "not in presentation format, babes."-1โ returned ifafdoes not contain a valid address family, anderrnois set toEAFNOSUPPORT.
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
- Interface:
inet_pton()โ Attribute: Thread safety โ Value:MT-Safe locale
that's her. thread safe, locale-style. multitask queen. ๐
๐ VERSIONS ๐
unlike inet_aton(3) and inet_addr(3), inet_pton() supports IPv6 addresses. so like, she's the newer, shinier, more evolved friend. โจ
on the other hand โ and this is the trade-off, bestie โ inet_pton() accepts only IPv4 addresses in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow the more general numbers-and-dots notation (hexadecimal and octal number formats, and formats that don't require all four bytes to be explicitly written). so those old ones are chaotic and loose with the rules and honestly? sometimes you want that.
for an interface that handles both IPv6 addresses and IPv4 addresses in numbers-and-dots notation, see getaddrinfo(3). she does it all. ๐ซถ
โญ STANDARDS โญ
POSIX.1-2008.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
POSIX.1-2001.
she's been here since 2001 and she's still serving. legacy behavior but make it cunt. ๐
๐ BUGS ๐
AF_INET6 does not recognize IPv4 addresses. an explicit IPv4-mapped IPv6 address must be supplied in src instead. so don't roll up with a plain 192.168.0.1 and expect the AF_INET6 path to understand you โ she will not. wrap it in the mapped form or she's leaving you on read. ๐
๐ EXAMPLES ๐
the program below demonstrates the use of inet_pton() and inet_ntop(3). it does the round trip โ text in, binary, text out โ which is honestly such a flex.
here are some example runs:
$ ./a.out i6 0:0:0:0:0:0:0:0
::
$ ./a.out i6 1:0:0:0:0:0:0:8
1::8
$ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116
::ffff:204.152.189.116
look at that compression. 0:0:0:0:0:0:0:0 walks in, :: walks out. glow-up complete. ๐ง
๐ Program source ๐
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define streq(...) (strcmp(__VA_ARGS__) == 0)
int
main(int argc, char *argv[])
{
unsigned char buf[sizeof(struct in6_addr)];
int domain, s;
char str[INET6_ADDRSTRLEN];
if (argc != 3) {
fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv[0]);
exit(EXIT_FAILURE);
}
domain = streq(argv[1], "i4") ? AF_INET :
streq(argv[1], "i6") ? AF_INET6 : atoi(argv[1]);
s = inet_pton(domain, argv[2], buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}
printf("%s\n", str);
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
okay that's the whole page, bestie. go forth and convert responsibly. ๐ ๐ฆ๐ธ
mwah ๐ โ this has been Linux man-pages 6.20, dated 2026-02-22, signing off. stay safe, stay MT-Safe locale, and remember: network byte order is not a suggestion. periodt.
๐ฆ 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 2008, 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