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

RETURN VALUE

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

CONFORMING TO

64       POSIX.1-2001.
65

NOTES

67       Unlike   inet_aton(3)   and  inet_addr(3),  inet_pton()  supports  IPv6
68       addresses.  On the other hand, inet_pton() only accepts IPv4  addresses
69       in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow
70       the more general numbers-and-dots notation (hexadecimal and octal  num‐
71       ber  formats,  and  formats  that  don't  require  all four bytes to be
72       explicitly  written).   For  an  interface  that  handles   both   IPv6
73       addresses,  and IPv4 addresses in numbers-and-dots notation, see getad‐
74       drinfo(3).
75

BUGS

77       AF_INET6 does not recognize IPv4 addresses.   An  explicit  IPv4-mapped
78       IPv6 address must be supplied in src instead.
79

EXAMPLE

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

SEE ALSO

133       getaddrinfo(3), inet(3), inet_ntop(3)
134

COLOPHON

136       This page is part of release 3.25 of the Linux  man-pages  project.   A
137       description  of  the project, and information about reporting bugs, can
138       be found at http://www.kernel.org/doc/man-pages/.
139
140
141
142Linux                             2008-06-18                      INET_PTON(3)
Impressum