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., each x can 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 address 0:0:0:0:0:0:0:1 can 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 leading xs are hexadecimal values that define the six most-significant 16-bit pieces of the address (i.e., 96 bits), and the ds 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 if src does 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 if af does not contain a valid address family, and errno is set to EAFNOSUPPORT.

๐ŸŽ€ 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms