1INET_PTON(3)               Linux Programmer's Manual              INET_PTON(3)
2
3
4

NAME

6       inet_pton - convert IPv4 and IPv6 addresses from text to binary form
7

SYNOPSIS

9       #include <arpa/inet.h>
10
11       int inet_pton(int af, const char *src, void *dst);
12

DESCRIPTION

14       This  function converts the character string src into a network address
15       structure in the af address family, then  copies  the  network  address
16       structure  to dst.  The af argument must be either AF_INET or AF_INET6.
17       dst is written in network byte order.
18
19       The following address families are currently supported:
20
21       AF_INET
22              src points to a character string containing an IPv4 network  ad‐
23              dress  in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is
24              a decimal number of up to three digits in the range  0  to  255.
25              The  address is converted to a struct in_addr and copied to dst,
26              which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
27
28       AF_INET6
29              src points to a character string containing an IPv6 network  ad‐
30              dress.  The address is converted to a struct in6_addr and copied
31              to dst, which must be sizeof(struct in6_addr)  (16)  bytes  (128
32              bits) long.  The allowed formats for IPv6 addresses follow these
33              rules:
34
35              1. The preferred format is x:x:x:x:x:x:x:x.  This form  consists
36                 of  eight  hexadecimal  numbers,  each  of  which expresses a
37                 16-bit value (i.e., each x can be up to 4 hex digits).
38
39              2. A series of contiguous zero values in  the  preferred  format
40                 can  be abbreviated to ::.  Only one instance of :: can occur
41                 in  an  address.    For   example,   the   loopback   address
42                 0:0:0:0:0:0:0:1  can be abbreviated as ::1.  The wildcard ad‐
43                 dress, consisting of all zeros, can be written as ::.
44
45              3. An alternate format is useful for expressing IPv4-mapped IPv6
46                 addresses.   This  form  is  written  as x:x:x:x:x:x:d.d.d.d,
47                 where the six leading xs are hexadecimal values  that  define
48                 the  six most-significant 16-bit pieces of the address (i.e.,
49                 96 bits), and the ds express a value in dotted-decimal  nota‐
50                 tion  that  defines  the least significant 32 bits of the ad‐
51                 dress.     An    example    of    such    an    address    is
52                 ::FFFF:204.152.189.116.
53
54              See  RFC  2373 for further details on the representation of IPv6
55              addresses.
56

RETURN VALUE

58       inet_pton() returns 1 on success (network address was successfully con‐
59       verted).  0 is returned if src does not contain a character string rep‐
60       resenting a valid network address in the specified address family.   If
61       af does not contain a valid address family, -1 is returned and errno is
62       set to EAFNOSUPPORT.
63

ATTRIBUTES

65       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
66       tributes(7).
67
68       ┌────────────┬───────────────┬────────────────┐
69Interface   Attribute     Value          
70       ├────────────┼───────────────┼────────────────┤
71inet_pton() │ Thread safety │ MT-Safe locale │
72       └────────────┴───────────────┴────────────────┘

CONFORMING TO

74       POSIX.1-2001, POSIX.1-2008.
75

NOTES

77       Unlike  inet_aton(3)  and  inet_addr(3),  inet_pton() supports IPv6 ad‐
78       dresses.  On the other hand, inet_pton() accepts only IPv4 addresses in
79       dotted-decimal  notation,  whereas  inet_aton(3) and inet_addr(3) allow
80       the more general numbers-and-dots notation (hexadecimal and octal  num‐
81       ber  formats,  and  formats that don't require all four bytes to be ex‐
82       plicitly written).  For an interface that handles both IPv6  addresses,
83       and IPv4 addresses in numbers-and-dots notation, see getaddrinfo(3).
84

BUGS

86       AF_INET6  does  not  recognize IPv4 addresses.  An explicit IPv4-mapped
87       IPv6 address must be supplied in src instead.
88

EXAMPLES

90       The program below demonstrates the use of inet_pton() and inet_ntop(3).
91       Here are some example runs:
92
93           $ ./a.out i6 0:0:0:0:0:0:0:0
94           ::
95           $ ./a.out i6 1:0:0:0:0:0:0:8
96           1::8
97           $ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116
98           ::ffff:204.152.189.116
99
100   Program source
101
102       #include <arpa/inet.h>
103       #include <stdio.h>
104       #include <stdlib.h>
105       #include <string.h>
106
107       int
108       main(int argc, char *argv[])
109       {
110           unsigned char buf[sizeof(struct in6_addr)];
111           int domain, s;
112           char str[INET6_ADDRSTRLEN];
113
114           if (argc != 3) {
115               fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv[0]);
116               exit(EXIT_FAILURE);
117           }
118
119           domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
120                    (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
121
122           s = inet_pton(domain, argv[2], buf);
123           if (s <= 0) {
124               if (s == 0)
125                   fprintf(stderr, "Not in presentation format");
126               else
127                   perror("inet_pton");
128               exit(EXIT_FAILURE);
129           }
130
131           if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
132               perror("inet_ntop");
133               exit(EXIT_FAILURE);
134           }
135
136           printf("%s\n", str);
137
138           exit(EXIT_SUCCESS);
139       }
140

SEE ALSO

142       getaddrinfo(3), inet(3), inet_ntop(3)
143

COLOPHON

145       This  page  is  part of release 5.10 of the Linux man-pages project.  A
146       description of the project, information about reporting bugs,  and  the
147       latest     version     of     this    page,    can    be    found    at
148       https://www.kernel.org/doc/man-pages/.
149
150
151
152Linux                             2020-06-09                      INET_PTON(3)
Impressum