1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" References consulted:
28 .\" Linux libc source code
29 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
31 .\" libc.info (from glibc distribution)
32 .\" Modified Sat Jul 24 19:12:00 1993 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified Sun Sep 3 20:29:36 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
34 .\" Changed network into host byte order (for inet_network),
35 .\" Andreas Jaeger <aj@arthur.rhein-neckar.de>, 980130.
37 .\" Describe the various address forms supported by inet_aton().
38 .\" Clarify discussion of inet_lnaof(), inet_netof(), and inet_makeaddr().
39 .\" Add discussion of Classful Addressing, noting that it is obsolete.
40 .\" Added an EXAMPLE program.
42 .TH INET 3 2014-05-28 "GNU" "Linux Programmer's Manual"
44 inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
45 inet_netof \- Internet address manipulation routines
48 .B #include <sys/socket.h>
49 .B #include <netinet/in.h>
50 .B #include <arpa/inet.h>
52 .BI "int inet_aton(const char *" cp ", struct in_addr *" inp );
54 .BI "in_addr_t inet_addr(const char *" cp );
56 .BI "in_addr_t inet_network(const char *" cp );
58 .BI "char *inet_ntoa(struct in_addr " in );
60 .BI "struct in_addr inet_makeaddr(in_addr_t " net ", in_addr_t " host );
62 .BI "in_addr_t inet_lnaof(struct in_addr " in );
64 .BI "in_addr_t inet_netof(struct in_addr " in );
68 Feature Test Macro Requirements for glibc (see
69 .BR feature_test_macros (7)):
74 _BSD_SOURCE || _SVID_SOURCE
77 converts the Internet host address \fIcp\fP from the
78 IPv4 numbers-and-dots notation into binary form (in network byte order)
79 and stores it in the structure that \fIinp\fP points to.
81 returns nonzero if the address is valid, zero if not.
82 The address supplied in
84 can have one of the following forms:
87 Each of the four numeric parts specifies a byte of the address;
88 the bytes are assigned in left-to-right order to produce the binary address.
95 specify the first two bytes of the binary address.
98 is interpreted as a 16-bit value that defines the rightmost two bytes
99 of the binary address.
100 This notation is suitable for specifying (outmoded) Class B
106 specifies the first byte of the binary address.
109 is interpreted as a 24-bit value that defines the rightmost three bytes
110 of the binary address.
111 This notation is suitable for specifying (outmoded) Class A
117 is interpreted as a 32-bit value that is stored directly
118 into the binary address without any byte rearrangement.
120 In all of the above forms,
121 components of the dotted address can be specified in decimal,
122 octal (with a leading
124 or hexadecimal, with a leading
126 Addresses in any of these forms are collectively termed
127 .IR "IPV4 numbers-and-dots notation" .
128 The form that uses exactly four decimal numbers is referred to as
129 .IR "IPv4 dotted-decimal notation"
131 .IR "IPv4 dotted-quad notation" ).
134 returns 1 if the supplied string was successfully interpreted,
135 or 0 if the string is invalid
143 function converts the Internet host address
144 \fIcp\fP from IPv4 numbers-and-dots notation into binary data in network
146 If the input is invalid,
148 (usually \-1) is returned.
149 Use of this function is problematic because \-1 is a valid address
151 Avoid its use in favor of
156 which provide a cleaner way to indicate error return.
162 a string in IPv4 numbers-and-dots notation,
163 into a number in host byte order suitable for use as an
164 Internet network address.
165 On success, the converted address is returned.
166 If the input is invalid, \-1 is returned.
170 function converts the Internet host address
171 \fIin\fP, given in network byte order, to a string in IPv4
172 dotted-decimal notation.
173 The string is returned in a statically
174 allocated buffer, which subsequent calls will overwrite.
178 function returns the local network address part
179 of the Internet address \fIin\fP.
180 The returned value is in host byte order.
184 function returns the network number part of
185 the Internet address \fIin\fP.
186 The returned value is in host byte order.
190 function is the converse of
194 It returns an Internet host address in network byte order,
195 created by combining the network number \fInet\fP
196 with the local address \fIhost\fP, both in
199 The structure \fIin_addr\fP as used in
201 .BR inet_makeaddr (),
211 typedef uint32_t in_addr_t;
223 are specified in POSIX.1-2001.
225 is not specified in POSIX.1-2001, but is available on most systems.
227 On the i386 the host byte order is Least Significant Byte
228 first (little endian), whereas the network byte order, as used on the
229 Internet, is Most Significant Byte first (big endian).
235 are legacy functions that assume they are dealing with
236 .IR "classful network addresses" .
237 Classful networking divides IPv4 network addresses into host and network
238 components at byte boundaries, as follows:
241 This address type is indicated by the value 0 in the
242 most significant bit of the (network byte ordered) address.
243 The network address is contained in the most significant byte,
244 and the host address occupies the remaining three bytes.
247 This address type is indicated by the binary value 10 in the
248 most significant two bits of the address.
249 The network address is contained in the two most significant bytes,
250 and the host address occupies the remaining two bytes.
253 This address type is indicated by the binary value 110 in the
254 most significant three bits of the address.
255 The network address is contained in the three most significant bytes,
256 and the host address occupies the remaining byte.
258 Classful network addresses are now obsolete,
259 having been superseded by Classless Inter-Domain Routing (CIDR),
260 which divides addresses into network and host components at
261 arbitrary bit (rather than byte) boundaries.
263 An example of the use of
268 Here are some example runs:
272 .RB "$" " ./a.out 226.000.000.037" " # Last byte is in octal"
274 .RB "$" " ./a.out 0x7f.1 " " # First byte is in hex"
282 #include <arpa/inet.h>
287 main(int argc, char *argv[])
292 fprintf(stderr, "%s <dotted\-address>\\n", argv[0]);
296 if (inet_aton(argv[1], &addr) == 0) {
297 fprintf(stderr, "Invalid address\\n");
301 printf("%s\\n", inet_ntoa(addr));
308 .BR gethostbyname (3),
311 .BR inet_net_pton (3),
317 This page is part of release 3.75 of the Linux
320 A description of the project,
321 information about reporting bugs,
322 and the latest version of this page,
324 \%http://www.kernel.org/doc/man\-pages/.