1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
4 .\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk <mtk.manpages@gmail.com>
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.
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" libc.info (from glibc distribution)
31 .\" Modified Sat Jul 24 19:12:00 1993 by Rik Faith <faith@cs.unc.edu>
32 .\" Modified Sun Sep 3 20:29:36 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
33 .\" Changed network into host byte order (for inet_network),
34 .\" Andreas Jaeger <aj@arthur.rhein-neckar.de>, 980130.
36 .\" Describe the various address forms supported by inet_aton().
37 .\" Clarify discussion of inet_lnaof(), inet_netof(), and inet_makeaddr().
38 .\" Add discussion of Classful Addressing, noting that it is obsolete.
39 .\" Added an EXAMPLE program.
41 .TH INET 3 2008-06-19 "GNU" "Linux Programmer's Manual"
43 inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
44 inet_netof \- Internet address manipulation routines
47 .B #include <sys/socket.h>
48 .B #include <netinet/in.h>
49 .B #include <arpa/inet.h>
51 .BI "int inet_aton(const char *" cp ", struct in_addr *" inp );
53 .BI "in_addr_t inet_addr(const char *" cp );
55 .BI "in_addr_t inet_network(const char *" cp );
57 .BI "char *inet_ntoa(struct in_addr " in );
59 .BI "struct in_addr inet_makeaddr(int " net ", int " host );
61 .BI "in_addr_t inet_lnaof(struct in_addr " in );
63 .BI "in_addr_t inet_netof(struct in_addr " in );
67 Feature Test Macro Requirements for glibc (see
68 .BR feature_test_macros (7)):
73 _BSD_SOURCE || _SVID_SOURCE
76 converts the Internet host address \fIcp\fP from the
77 IPv4 numbers-and-dots notation into binary form (in network byte order)
78 and stores it in the structure that \fIinp\fP points to.
80 returns non-zero if the address is valid, zero if not.
81 The address supplied in
83 can have one of the following forms:
86 Each of the for numeric parts specifies a byte of the address;
87 the bytes are assigned in left-to-right order to produce the binary address.
94 specify the first two bytes of the binary address.
97 is interpreted as a 16-bit value that defines the rightmost two bytes
98 of the binary address.
99 This notation is suitable for specifying (outmoded) Class B
105 specifies the first byte of the binary address.
108 is interpreted as a 24-bit value that defines the rightmost three bytes
109 of the binary address.
110 This notation is suitable for specifying (outmoded) Class C
116 is interpreted as a 32-bit value that is stored directly
117 into the binary address without any byte rearrangement.
119 In all of the above forms,
120 components of the dotted address can be specified in decimal,
121 octal (with a leading
123 or hexadecimal, with a leading
125 Addresses in any of these forms are collectively termed
126 .IR "IPV4 numbers-and-dots notation" .
127 The form that uses exactly four decimal numbers is referred to as
128 .IR "IPv4 dotted-decimal notation"
130 .IR "IPv4 dotted-quad notation" ).
134 function converts the Internet host address
135 \fIcp\fP from IPv4 numbers-and-dots notation into binary data in network
137 If the input is invalid,
139 (usually \-1) is returned.
140 Use of this function is problematic because \-1 is a valid address
142 Avoid its use in favor of
147 which provide a cleaner way to indicate error return.
153 a string in IPv4 numbers-and-dots notation,
154 into a number in host byte order suitable for use as an
155 Internet network address.
156 On success, the converted address is returned.
157 If the input is invalid, \-1 is returned.
161 function converts the Internet host address
162 \fIin\fP, given in network byte order, to a string in IPv4
163 dotted-decimal notation.
164 The string is returned in a statically
165 allocated buffer, which subsequent calls will overwrite.
169 function returns the local network address part
170 of the Internet address \fIin\fP.
171 The returned value is in host byte order.
175 function returns the network number part of
176 the Internet address \fIin\fP.
177 The returned value is in host byte order.
181 function is the converse of
185 It returns an Internet host address in network byte order,
186 created by combining the network number \fInet\fP
187 with the local address \fIhost\fP, both in
190 The structure \fIin_addr\fP as used in
192 .BR inet_makeaddr (),
202 typedef uint32_t in_addr_t;
214 are specified in POSIX.1-2001.
216 is not specified in POSIX.1.-2001, but is available on most systems.
218 On the i386 the host byte order is Least Significant Byte
219 first (little endian), whereas the network byte order, as used on the
220 Internet, is Most Significant Byte first (big endian).
226 are legacy functions that assume they are dealing with
227 .IR "classful network addresses" .
228 Classful networking divides IPv4 network addresses into host and network
229 components at byte boundaries, as follows:
232 This address type is indicated by the value 0 in the
233 most significant bit of the (network byte ordered) address.
234 The network address is contained in the most significant byte,
235 and the host address occupies the remaining three bytes.
238 This address type is indicated by the binary value 10 in the
239 most significant two bits of the address.
240 The network address is contained in the two most significant bytes,
241 and the host address occupies the remaining two bytes.
244 This address type is indicated by the binary value 110 in the
245 most significant three bits of the address.
246 The network address is contained in the three most significant bytes,
247 and the host address occupies the remaining byte.
249 Classful network addresses are now obolete,
250 having been superseded by Classless Inter-Domain Routing (CIDR),
251 which divides addresses into network and host components at
252 arbitrary bit (rather than byte) boundaries.
254 An example of the use of
259 Here are some example runs:
263 $ ./a.out 226.000.000.037 # Last byte is in octal
265 $ ./a.out 0x7f.1 # First byte is in hex
272 #include <arpa/inet.h>
277 main(int argc, char *argv[])
282 fprintf(stderr, "%s <dotted\-address>\\n", argv[0]);
286 if (inet_aton(argv[1], &addr) == 0) {
291 printf("%s\\n", inet_ntoa(addr));
298 .BR gethostbyname (3),