1 .\" Copyright 2000 Sam Varshavchik <mrsam@courier-mta.com>
2 .\" and Copyright (c) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References: RFC 2553
27 .TH INET_PTON 3 2017-09-15 "Linux" "Linux Programmer's Manual"
29 inet_pton \- convert IPv4 and IPv6 addresses from text to binary form
32 .B #include <arpa/inet.h>
34 .BI "int inet_pton(int " "af" ", const char *" "src" ", void *" "dst" );
37 This function converts the character string
39 into a network address structure in the
43 the network address structure to
47 argument must be either
52 is written in network byte order.
54 The following address families are currently supported:
58 points to a character string containing an IPv4 network address in
59 dotted-decimal format, "\fIddd.ddd.ddd.ddd\fP", where
61 is a decimal number of up to three digits in the range 0 to 255.
62 The address is converted to a
67 .I sizeof(struct in_addr)
68 (4) bytes (32 bits) long.
72 points to a character string containing an IPv6 network address.
73 The address is converted to a
78 .I sizeof(struct in6_addr)
79 (16) bytes (128 bits) long.
80 The allowed formats for IPv6 addresses follow these rules:
83 The preferred format is
85 This form consists of eight hexadecimal numbers,
86 each of which expresses a 16-bit value (i.e., each
88 can be up to 4 hex digits).
90 A series of contiguous zero values in the preferred format
95 can occur in an address.
96 For example, the loopback address
100 The wildcard address, consisting of all zeros, can be written as
103 An alternate format is useful for expressing IPv4-mapped IPv6 addresses.
104 This form is written as
105 .IR x:x:x:x:x:x:d.d.d.d ,
106 where the six leading
108 are hexadecimal values that define the six most-significant
109 16-bit pieces of the address (i.e., 96 bits), and the
111 express a value in dotted-decimal notation that
112 defines the least significant 32 bits of the address.
113 An example of such an address is
114 .IR ::FFFF:204.152.189.116 .
117 See RFC 2373 for further details on the representation of IPv6 addresses.
120 returns 1 on success (network address was successfully converted).
123 does not contain a character string representing a valid network
124 address in the specified address family.
127 does not contain a valid address family, \-1 is returned and
132 For an explanation of the terms used in this section, see
138 Interface Attribute Value
141 T} Thread safety MT-Safe locale
144 POSIX.1-2001, POSIX.1-2008.
151 supports IPv6 addresses.
154 accepts only IPv4 addresses in dotted-decimal notation, whereas
158 allow the more general numbers-and-dots notation (hexadecimal
159 and octal number formats, and formats that don't require all
160 four bytes to be explicitly written).
161 For an interface that handles both IPv6 addresses, and IPv4
162 addresses in numbers-and-dots notation, see
166 does not recognize IPv4 addresses.
167 An explicit IPv4-mapped IPv6 address must be supplied in
171 The program below demonstrates the use of
175 Here are some example runs:
179 .RB "$" " ./a.out i6 0:0:0:0:0:0:0:0"
181 .RB "$" " ./a.out i6 1:0:0:0:0:0:0:8"
183 .RB "$" " ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116"
184 ::ffff:204.152.189.116
190 #include <arpa/inet.h>
196 main(int argc, char *argv[])
198 unsigned char buf[sizeof(struct in6_addr)];
200 char str[INET6_ADDRSTRLEN];
203 fprintf(stderr, "Usage: %s {i4|i6|<num>} string\\n", argv[0]);
207 domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
208 (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
210 s = inet_pton(domain, argv[2], buf);
213 fprintf(stderr, "Not in presentation format");
219 if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
224 printf("%s\\n", str);