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
23              address  in  dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd
24              is 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
30              address.   The  address  is  converted  to a struct in6_addr and
31              copied to dst, which must be sizeof(struct in6_addr) (16)  bytes
32              (128  bits) long.  The allowed formats for IPv6 addresses follow
33              these 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
43                 address, 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
51                 address.    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
66       attributes(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
78       addresses.  On the other hand, inet_pton() accepts only IPv4  addresses
79       in 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
82       explicitly  written).   For  an  interface  that  handles   both   IPv6
83       addresses,  and IPv4 addresses in numbers-and-dots notation, see getad‐
84       drinfo(3).
85

BUGS

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

EXAMPLES

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

SEE ALSO

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

COLOPHON

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