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
79       addresses.  On the other hand, inet_pton() accepts only IPv4  addresses
80       in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow
81       the more  general  numbers-and-dots  notation  (hexadecimal  and  octal
82       number  formats,  and  formats  that don't require all four bytes to be
83       explicitly  written).   For  an  interface  that  handles   both   IPv6
84       addresses,   and  IPv4  addresses  in  numbers-and-dots  notation,  see
85       getaddrinfo(3).
86

STANDARDS

88       POSIX.1-2008.
89

HISTORY

91       POSIX.1-2001.
92

BUGS

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

EXAMPLES

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

SEE ALSO

150       getaddrinfo(3), inet(3), inet_ntop(3)
151
152
153
154Linux man-pages 6.05              2023-07-20                      inet_pton(3)
Impressum