1inet_pton(3)               Library Functions Manual               inet_pton(3)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <arpa/inet.h>
13
14       int inet_pton(int af, const char *restrict src, void *restrict dst);
15

DESCRIPTION

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

RETURN VALUE

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

ATTRIBUTES

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

VERSIONS

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

STANDARDS

87       POSIX.1-2008.
88

HISTORY

90       POSIX.1-2001.
91

BUGS

93       AF_INET6  does  not  recognize IPv4 addresses.  An explicit IPv4-mapped
94       IPv6 address must be supplied in src instead.
95

EXAMPLES

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

SEE ALSO

149       getaddrinfo(3), inet(3), inet_ntop(3)
150
151
152
153Linux man-pages 6.04              2023-03-30                      inet_pton(3)
Impressum