1 .\" Copyright 2000 Sam Varshavchik <mrsam@courier-mta.com>
2 .\" and Copyright (c) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References: RFC 2553
7 .TH INET_PTON 3 2021-03-22 "Linux" "Linux Programmer's Manual"
9 inet_pton \- convert IPv4 and IPv6 addresses from text to binary form
12 .RI ( libc ", " \-lc )
15 .B #include <arpa/inet.h>
17 .BI "int inet_pton(int " af ", const char *restrict " src \
18 ", void *restrict " dst );
21 This function converts the character string
23 into a network address structure in the
27 the network address structure to
31 argument must be either
36 is written in network byte order.
38 The following address families are currently supported:
42 points to a character string containing an IPv4 network address in
43 dotted-decimal format, "\fIddd.ddd.ddd.ddd\fP", where
45 is a decimal number of up to three digits in the range 0 to 255.
46 The address is converted to a
51 .I sizeof(struct in_addr)
52 (4) bytes (32 bits) long.
56 points to a character string containing an IPv6 network address.
57 The address is converted to a
62 .I sizeof(struct in6_addr)
63 (16) bytes (128 bits) long.
64 The allowed formats for IPv6 addresses follow these rules:
67 The preferred format is
69 This form consists of eight hexadecimal numbers,
70 each of which expresses a 16-bit value (i.e., each
72 can be up to 4 hex digits).
74 A series of contiguous zero values in the preferred format
79 can occur in an address.
80 For example, the loopback address
84 The wildcard address, consisting of all zeros, can be written as
87 An alternate format is useful for expressing IPv4-mapped IPv6 addresses.
88 This form is written as
89 .IR x:x:x:x:x:x:d.d.d.d ,
92 are hexadecimal values that define the six most-significant
93 16-bit pieces of the address (i.e., 96 bits), and the
95 express a value in dotted-decimal notation that
96 defines the least significant 32 bits of the address.
97 An example of such an address is
98 .IR ::FFFF:204.152.189.116 .
101 See RFC 2373 for further details on the representation of IPv6 addresses.
104 returns 1 on success (network address was successfully converted).
107 does not contain a character string representing a valid network
108 address in the specified address family.
111 does not contain a valid address family, \-1 is returned and
116 For an explanation of the terms used in this section, see
124 Interface Attribute Value
127 T} Thread safety MT-Safe locale
133 POSIX.1-2001, POSIX.1-2008.
140 supports IPv6 addresses.
143 accepts only IPv4 addresses in dotted-decimal notation, whereas
147 allow the more general numbers-and-dots notation (hexadecimal
148 and octal number formats, and formats that don't require all
149 four bytes to be explicitly written).
150 For an interface that handles both IPv6 addresses, and IPv4
151 addresses in numbers-and-dots notation, see
155 does not recognize IPv4 addresses.
156 An explicit IPv4-mapped IPv6 address must be supplied in
160 The program below demonstrates the use of
164 Here are some example runs:
168 .RB "$" " ./a.out i6 0:0:0:0:0:0:0:0"
170 .RB "$" " ./a.out i6 1:0:0:0:0:0:0:8"
172 .RB "$" " ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116"
173 ::ffff:204.152.189.116
179 #include <arpa/inet.h>
185 main(int argc, char *argv[])
187 unsigned char buf[sizeof(struct in6_addr)];
189 char str[INET6_ADDRSTRLEN];
192 fprintf(stderr, "Usage: %s {i4|i6|<num>} string\en", argv[0]);
196 domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
197 (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
199 s = inet_pton(domain, argv[2], buf);
202 fprintf(stderr, "Not in presentation format");
208 if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
213 printf("%s\en", str);