2 .\" Copyright 2000 Sam Varshavchik <mrsam@courier-mta.com>
3 .\" and Copyright (c) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References: RFC 2553
8 .TH inet_pton 3 (date) "Linux man-pages (unreleased)"
10 inet_pton \- convert IPv4 and IPv6 addresses from text to binary form
13 .RI ( libc ", " \-lc )
16 .B #include <arpa/inet.h>
18 .BI "int inet_pton(int " af ", const char *restrict " src \
19 ", void *restrict " dst );
22 This function converts the character string
24 into a network address structure in the
28 the network address structure to
32 argument must be either
37 is written in network byte order.
39 The following address families are currently supported:
43 points to a character string containing an IPv4 network address in
44 dotted-decimal format, "\fIddd.ddd.ddd.ddd\fP", where
46 is a decimal number of up to three digits in the range 0 to 255.
47 The address is converted to a
52 .I sizeof(struct in_addr)
53 (4) bytes (32 bits) long.
57 points to a character string containing an IPv6 network address.
58 The address is converted to a
63 .I sizeof(struct in6_addr)
64 (16) bytes (128 bits) long.
65 The allowed formats for IPv6 addresses follow these rules:
68 The preferred format is
70 This form consists of eight hexadecimal numbers,
71 each of which expresses a 16-bit value (i.e., each
73 can be up to 4 hex digits).
75 A series of contiguous zero values in the preferred format
80 can occur in an address.
81 For example, the loopback address
85 The wildcard address, consisting of all zeros, can be written as
88 An alternate format is useful for expressing IPv4-mapped IPv6 addresses.
89 This form is written as
90 .IR x:x:x:x:x:x:d.d.d.d ,
93 are hexadecimal values that define the six most-significant
94 16-bit pieces of the address (i.e., 96 bits), and the
96 express a value in dotted-decimal notation that
97 defines the least significant 32 bits of the address.
98 An example of such an address is
99 .IR ::FFFF:204.152.189.116 .
102 See RFC 2373 for further details on the representation of IPv6 addresses.
105 returns 1 on success (network address was successfully converted).
108 does not contain a character string representing a valid network
109 address in the specified address family.
112 does not contain a valid address family, \-1 is returned and
117 For an explanation of the terms used in this section, see
123 Interface Attribute Value
128 T} Thread safety MT-Safe locale
137 supports IPv6 addresses.
140 accepts only IPv4 addresses in dotted-decimal notation, whereas
144 allow the more general numbers-and-dots notation (hexadecimal
145 and octal number formats, and formats that don't require all
146 four bytes to be explicitly written).
147 For an interface that handles both IPv6 addresses, and IPv4
148 addresses in numbers-and-dots notation, see
156 does not recognize IPv4 addresses.
157 An explicit IPv4-mapped IPv6 address must be supplied in
161 The program below demonstrates the use of
165 Here are some example runs:
169 .RB "$" " ./a.out i6 0:0:0:0:0:0:0:0"
171 .RB "$" " ./a.out i6 1:0:0:0:0:0:0:8"
173 .RB "$" " ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116"
174 ::ffff:204.152.189.116
179 .\" SRC BEGIN (inet_pton.c)
181 #include <arpa/inet.h>
187 main(int argc, char *argv[])
189 unsigned char buf[sizeof(struct in6_addr)];
191 char str[INET6_ADDRSTRLEN];
194 fprintf(stderr, "Usage: %s {i4|i6|<num>} string\en", argv[0]);
198 domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
199 (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
201 s = inet_pton(domain, argv[2], buf);
204 fprintf(stderr, "Not in presentation format");
210 if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
215 printf("%s\en", str);