inet(3)

๐Ÿฆ‹ inet(3) โ€” Internet address manipulation routines ๐Ÿฆ‹

okay bestie, welcome to inet(3) from the Library Functions Manual ๐Ÿ’– these are the OG old-school IPv4 address glow-up routines โ€” turning numbers-and-dots strings into bytes and back again. some of them are iconic, some of them are deprecated, and we're gonna get into all of it. no cap.


โœจ NAME โœจ

inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, inet_netof โ€” Internet address manipulation routines ๐Ÿซถ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);

[[deprecated]] char *inet_ntoa(struct in_addr in);

[[deprecated]] struct in_addr inet_makeaddr(in_addr_t net,
                                            in_addr_t host);

[[deprecated]] in_addr_t inet_lnaof(struct in_addr in);
[[deprecated]] in_addr_t inet_netof(struct in_addr in);

๐Ÿงš Feature Test Macro Requirements for glibc (see feature_test_macros(7))

inet_aton(), inet_ntoa():
    Since glibc 2.19:
        _DEFAULT_SOURCE
    In glibc up to and including 2.19:
        _BSD_SOURCE || _BSD_SOURCE

(and yes, _BSD_SOURCE || _BSD_SOURCE is literally duplicated in the original, we love her for that ๐Ÿ™ˆ)


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

inet_aton() โ€” the one you should actually be using โœจ

inet_aton() takes the Internet host address cp, which is sitting there all pretty in IPv4 numbers-and-dots notation, and converts it into binary form (in network byte order), then tucks it away inside the structure that inp points to. ๐ŸŽ€

It returns nonzero if the address is valid, and zero if it's not.

The address you hand it in cp can be any one of these forms:

  • a.b.c.d โ€” each of the four numeric parts specifies one byte of the address; the bytes get assigned in left-to-right order to produce the binary address. ๐Ÿ’…
  • a.b.c โ€” parts a and b specify the first two bytes of the binary address. Part c is interpreted as a 16-bit value that defines the rightmost two bytes of the binary address. This notation is suitable for specifying (outmoded) Class B network addresses.
  • a.b โ€” part a specifies the first byte of the binary address. Part b is interpreted as a 24-bit value that defines the rightmost three bytes of the binary address. This notation is suitable for specifying (outmoded) Class A network addresses.
  • a โ€” the value a is interpreted as a 32-bit value that is stored directly into the binary address without any byte rearrangement. simple. clean. main character energy. ๐Ÿ‘‘

In all of the above forms, components of the dotted address can be specified in decimal, in octal (with a leading 0), or in hexadecimal, with a leading 0X. Addresses in any of these forms are collectively termed IPV4 numbers-and-dots notation. The form that uses exactly four decimal numbers is referred to as IPv4 dotted-decimal notation (or sometimes: IPv4 dotted-quad notation).

inet_aton() returns 1 if the supplied string was successfully interpreted, or 0 if the string is invalid (errno is not set on error ๐Ÿซ ).

inet_addr() โ€” highkey problematic ๐Ÿ˜ญ

The inet_addr() function converts the Internet host address cp from IPv4 numbers-and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned.

Using this function is problematic, bestie, because -1 is a valid address (255.255.255.255). So when you get -1 back you literally cannot tell if it's an error or a real address. Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3) โ€” they give you a much cleaner way to indicate an error return. ๐Ÿ’€

inet_network() โ€” host byte order, okay?

The inet_network() function converts cp, a string in IPv4 numbers-and-dots notation, into a number in host byte order suitable for use as an Internet network address. On success, the converted address is returned. If the input is invalid, -1 is returned.

inet_ntoa() โ€” deprecated but still beloved ๐Ÿ’…

The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation.

The string is returned in a statically allocated buffer, which subsequent calls will overwrite. so do not be out here storing that pointer for later, bestie, she will betray you. ๐Ÿ™ˆ

inet_lnaof() and inet_netof()

  • inet_lnaof() returns the local network address part of the Internet address in. The returned value is in host byte order.
  • inet_netof() returns the network number part of the Internet address in. The returned value is in host byte order.

inet_makeaddr() โ€” the converse bestie ๐ŸŒˆ

The inet_makeaddr() function is the converse of inet_netof() and inet_lnaof(). It returns an Internet host address in network byte order, created by combining the network number.I net with the local address host, both in host byte order.

the in_addr structure ๐Ÿ“

The structure in_addr as used in inet_ntoa(), inet_makeaddr(), inet_lnaof(), and inet_netof() is defined in <netinet/in.h> as:

typedef uint32_t in_addr_t;

struct in_addr {
    in_addr_t s_addr;
};

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

For an explanation of the terms used in this section, see attributes(7).

  • inet_aton(), inet_addr(), inet_network(), inet_ntoa() โ€” Thread safety: MT-Safe locale
  • inet_makeaddr(), inet_lnaof(), inet_netof() โ€” Thread safety: MT-Safe

โญ STANDARDS โญ

  • inet_addr()
  • inet_ntoa()

โ†’ POSIX.1-2008.

  • inet_aton()

โ†’ None.


โญ STANDARDS โญ (yes, again โ€” she said it twice ๐Ÿ’–)

  • inet_addr()
  • inet_ntoa()

โ†’ POSIX.1-2001, 4.3BSD.

inet_lnaof(), inet_netof(), and inet_makeaddr() are legacy functions that assume they are dealing with classful network addresses. Classful networking divides IPv4 network addresses into host and network components at byte boundaries, like this:

  • Class A โ€” this address type is indicated by the value 0 in the most significant bit of the (network byte ordered) address. The network address is contained in the most significant byte, and the host address occupies the remaining three bytes. ๐ŸŒธ
  • Class B โ€” this address type is indicated by the binary value 10 in the most significant two bits of the address. The network address is contained in the two most significant bytes, and the host address occupies the remaining two bytes.
  • Class C โ€” this address type is indicated by the binary value 110 in the most significant three bits of the address. The network address is contained in the three most significant bytes, and the host address occupies the remaining byte.

Classful network addresses are now obsolete, having been superseded by Classless Inter-Domain Routing (CIDR), which divides addresses into network and host components at arbitrary bit (rather than byte) boundaries. the glow-up was real. โœจ


๐ŸŽ€ NOTES ๐ŸŽ€

On x86 architectures, the host byte order is Least Significant Byte first (little endian), whereas the network byte order, as used on the Internet, is Most Significant Byte first (big endian). ๐Ÿ’…


๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

An example of the use of inet_aton() and inet_ntoa() is shown below. Here are some example runs:

$ ./a.out 226.000.000.037      # Last byte is in octal
226.0.0.31
$ ./a.out 0x7f.1               # First byte is in hex
127.0.0.1

๐Ÿงš Program source

#define _DEFAULT_SOURCE
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    struct in_addr addr;

    if (argc != 2) {
        fprintf(stderr, "%s <dotted-address>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (inet_aton(argv[1], &addr) == 0) {
        fprintf(stderr, "Invalid address\n");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", inet_ntoa(addr));
    exit(EXIT_SUCCESS);
}

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

byteorder(3), getaddrinfo(3), gethostbyname(3), getnameinfo(3), getnetent(3), inet_net_pton(3), inet_ntop(3), inet_pton(3), hosts(5), networks(5)


and that's the whole page, bestie!! ๐Ÿซถ this one is Linux man-pages 6.20, dated 2026-02-08, and it lives under inet(3). go forth and never use inet_addr() again, okay? mwah ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ 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