]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/inet.3
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man3 / inet.3
CommitLineData
a1eaacb1 1'\" t
fea681da 2.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3d54a910
MK
3.\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
4.\" <mtk.manpages@gmail.com>
fea681da 5.\"
5fbde956 6.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
7.\"
8.\" References consulted:
9.\" Linux libc source code
10.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
11.\" 386BSD man pages
12.\" libc.info (from glibc distribution)
13.\" Modified Sat Jul 24 19:12:00 1993 by Rik Faith <faith@cs.unc.edu>
14.\" Modified Sun Sep 3 20:29:36 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
15.\" Changed network into host byte order (for inet_network),
16.\" Andreas Jaeger <aj@arthur.rhein-neckar.de>, 980130.
054f5228
MK
17.\" 2008-06-19, mtk
18.\" Describe the various address forms supported by inet_aton().
19.\" Clarify discussion of inet_lnaof(), inet_netof(), and inet_makeaddr().
20.\" Add discussion of Classful Addressing, noting that it is obsolete.
21.\" Added an EXAMPLE program.
fea681da 22.\"
a5ebdc8d 23.TH inet 3 (date) "Linux man-pages (unreleased)"
fea681da 24.SH NAME
c13182ef 25inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
fea681da 26inet_netof \- Internet address manipulation routines
4508f8a0
AC
27.SH LIBRARY
28Standard C library
8fc3b2cf 29.RI ( libc ", " \-lc )
fea681da
MK
30.SH SYNOPSIS
31.nf
32.B #include <sys/socket.h>
33.B #include <netinet/in.h>
34.B #include <arpa/inet.h>
c6d039a3 35.P
fea681da 36.BI "int inet_aton(const char *" cp ", struct in_addr *" inp );
c6d039a3 37.P
fea681da 38.BI "in_addr_t inet_addr(const char *" cp );
fea681da 39.BI "in_addr_t inet_network(const char *" cp );
c6d039a3 40.P
1dd2af73 41.BI "[[deprecated]] char *inet_ntoa(struct in_addr " in );
c6d039a3 42.P
4131356c
AC
43.BI "[[deprecated]] struct in_addr inet_makeaddr(in_addr_t " net ,
44.BI " in_addr_t " host );
c6d039a3 45.P
4131356c
AC
46.BI "[[deprecated]] in_addr_t inet_lnaof(struct in_addr " in );
47.BI "[[deprecated]] in_addr_t inet_netof(struct in_addr " in );
fea681da 48.fi
c6d039a3 49.P
d39ad78f 50.RS -4
cc4615cc
MK
51Feature Test Macro Requirements for glibc (see
52.BR feature_test_macros (7)):
d39ad78f 53.RE
c6d039a3 54.P
cc4615cc
MK
55.BR inet_aton (),
56.BR inet_ntoa ():
7f0ec8ee
MK
57.nf
58 Since glibc 2.19:
59 _DEFAULT_SOURCE
60 In glibc up to and including 2.19:
61 _BSD_SOURCE || _BSD_SOURCE
62.fi
fea681da 63.SH DESCRIPTION
60a90ecd
MK
64.BR inet_aton ()
65converts the Internet host address \fIcp\fP from the
054f5228
MK
66IPv4 numbers-and-dots notation into binary form (in network byte order)
67and stores it in the structure that \fIinp\fP points to.
60a90ecd 68.BR inet_aton ()
c7094399 69returns nonzero if the address is valid, zero if not.
054f5228
MK
70The address supplied in
71.I cp
72can have one of the following forms:
73.TP 10
74.I a.b.c.d
57e5ca03 75Each of the four numeric parts specifies a byte of the address;
054f5228
MK
76the bytes are assigned in left-to-right order to produce the binary address.
77.TP
78.I a.b.c
79Parts
80.I a
81and
82.I b
83specify the first two bytes of the binary address.
84Part
85.I c
86is interpreted as a 16-bit value that defines the rightmost two bytes
87of the binary address.
88This notation is suitable for specifying (outmoded) Class B
89network addresses.
90.TP
91.I a.b
92Part
93.I a
94specifies the first byte of the binary address.
95Part
96.I b
97is interpreted as a 24-bit value that defines the rightmost three bytes
98of the binary address.
5526923a 99This notation is suitable for specifying (outmoded) Class A
054f5228
MK
100network addresses.
101.TP
102.I a
103The value
104.I a
105is interpreted as a 32-bit value that is stored directly
106into the binary address without any byte rearrangement.
c6d039a3 107.P
054f5228
MK
108In all of the above forms,
109components of the dotted address can be specified in decimal,
110octal (with a leading
111.IR 0 ),
112or hexadecimal, with a leading
113.IR 0X ).
114Addresses in any of these forms are collectively termed
115.IR "IPV4 numbers-and-dots notation" .
116The form that uses exactly four decimal numbers is referred to as
1ae6b2c7 117.I IPv4 dotted-decimal notation
054f5228
MK
118(or sometimes:
119.IR "IPv4 dotted-quad notation" ).
c6d039a3 120.P
4cbfaed0
MK
121.BR inet_aton ()
122returns 1 if the supplied string was successfully interpreted,
123or 0 if the string is invalid
124.RB ( errno
125is
126.I not
127set on error).
c6d039a3 128.P
60a90ecd
MK
129The
130.BR inet_addr ()
131function converts the Internet host address
054f5228 132\fIcp\fP from IPv4 numbers-and-dots notation into binary data in network
c13182ef 133byte order.
2f0af33b
MK
134If the input is invalid,
135.B INADDR_NONE
136(usually \-1) is returned.
054f5228
MK
137Use of this function is problematic because \-1 is a valid address
138(255.255.255.255).
139Avoid its use in favor of
60a90ecd 140.BR inet_aton (),
054f5228
MK
141.BR inet_pton (3),
142or
a414d0b5 143.BR getaddrinfo (3),
054f5228 144which provide a cleaner way to indicate error return.
c6d039a3 145.P
60a90ecd
MK
146The
147.BR inet_network ()
054f5228
MK
148function converts
149.IR cp ,
150a string in IPv4 numbers-and-dots notation,
151into a number in host byte order suitable for use as an
152Internet network address.
153On success, the converted address is returned.
7cc028fb 154If the input is invalid, \-1 is returned.
c6d039a3 155.P
60a90ecd
MK
156The
157.BR inet_ntoa ()
158function converts the Internet host address
054f5228
MK
159\fIin\fP, given in network byte order, to a string in IPv4
160dotted-decimal notation.
c13182ef 161The string is returned in a statically
fea681da 162allocated buffer, which subsequent calls will overwrite.
c6d039a3 163.P
60a90ecd 164The
60a90ecd 165.BR inet_lnaof ()
054f5228 166function returns the local network address part
c13182ef 167of the Internet address \fIin\fP.
054f5228 168The returned value is in host byte order.
c6d039a3 169.P
60a90ecd
MK
170The
171.BR inet_netof ()
172function returns the network number part of
054f5228
MK
173the Internet address \fIin\fP.
174The returned value is in host byte order.
c6d039a3 175.P
054f5228
MK
176The
177.BR inet_makeaddr ()
178function is the converse of
61792fc6 179.BR inet_netof ()
054f5228
MK
180and
181.BR inet_lnaof ().
182It returns an Internet host address in network byte order,
183created by combining the network number \fInet\fP
184with the local address \fIhost\fP, both in
185host byte order.
c6d039a3 186.P
60a90ecd
MK
187The structure \fIin_addr\fP as used in
188.BR inet_ntoa (),
189.BR inet_makeaddr (),
d556548b 190.BR inet_lnaof (),
60a90ecd
MK
191and
192.BR inet_netof ()
a9a13a50
MK
193is defined in
194.I <netinet/in.h>
195as:
c6d039a3 196.P
bd191423 197.in +4n
bdd915e2 198.EX
9f8162f9 199typedef uint32_t in_addr_t;
fe5dba13 200\&
fea681da 201struct in_addr {
9f8162f9
MK
202 in_addr_t s_addr;
203};
bdd915e2 204.EE
bd191423 205.in
b73c9bd5
PH
206.SH ATTRIBUTES
207For an explanation of the terms used in this section, see
208.BR attributes (7).
209.TS
210allbox;
c466875e 211lbx lb lb
b73c9bd5
PH
212l l l.
213Interface Attribute Value
214T{
9e54434e
BR
215.na
216.nh
b73c9bd5 217.BR inet_aton (),
93ead13c 218.BR inet_addr (),
93ead13c
MS
219.BR inet_network (),
220.BR inet_ntoa ()
b73c9bd5
PH
221T} Thread safety MT-Safe locale
222T{
9e54434e
BR
223.na
224.nh
b73c9bd5
PH
225.BR inet_makeaddr (),
226.BR inet_lnaof (),
b73c9bd5
PH
227.BR inet_netof ()
228T} Thread safety MT-Safe
229.TE
3113c7f3 230.SH STANDARDS
4131356c
AC
231.TP
232.BR inet_addr ()
233.TQ
234.BR inet_ntoa ()
235POSIX.1-2008.
236.TP
054f5228 237.BR inet_aton ()
4131356c
AC
238None.
239.SH STANDARDS
240.TP
241.BR inet_addr ()
242.TQ
243.BR inet_ntoa ()
244POSIX.1-2001, 4.3BSD.
c6d039a3 245.P
054f5228
MK
246.BR inet_lnaof (),
247.BR inet_netof (),
248and
249.BR inet_makeaddr ()
250are legacy functions that assume they are dealing with
251.IR "classful network addresses" .
252Classful networking divides IPv4 network addresses into host and network
253components at byte boundaries, as follows:
254.TP 10
255Class A
256This address type is indicated by the value 0 in the
257most significant bit of the (network byte ordered) address.
258The network address is contained in the most significant byte,
259and the host address occupies the remaining three bytes.
260.TP
261Class B
262This address type is indicated by the binary value 10 in the
263most significant two bits of the address.
264The network address is contained in the two most significant bytes,
265and the host address occupies the remaining two bytes.
266.TP
267Class C
268This address type is indicated by the binary value 110 in the
269most significant three bits of the address.
270The network address is contained in the three most significant bytes,
271and the host address occupies the remaining byte.
c6d039a3 272.P
ab186fbd 273Classful network addresses are now obsolete,
054f5228
MK
274having been superseded by Classless Inter-Domain Routing (CIDR),
275which divides addresses into network and host components at
276arbitrary bit (rather than byte) boundaries.
4131356c
AC
277.SH NOTES
278On x86 architectures, the host byte order is Least Significant Byte
279first (little endian), whereas the network byte order, as used on the
280Internet, is Most Significant Byte first (big endian).
a14af333 281.SH EXAMPLES
054f5228
MK
282An example of the use of
283.BR inet_aton ()
284and
285.BR inet_ntoa ()
286is shown below.
287Here are some example runs:
c6d039a3 288.P
054f5228 289.in +4n
e646a1ba 290.EX
b43a3b30 291.RB "$" " ./a.out 226.000.000.037" " # Last byte is in octal"
054f5228 292226.0.0.31
b43a3b30 293.RB "$" " ./a.out 0x7f.1 " " # First byte is in hex"
054f5228 294127.0.0.1
b8302363 295.EE
054f5228 296.in
9c330504 297.SS Program source
d84d0300 298\&
b0b6ab4e 299.\" SRC BEGIN (inet.c)
e7d0bb47 300.EX
88d2b3fd 301#define _DEFAULT_SOURCE
054f5228
MK
302#include <arpa/inet.h>
303#include <stdio.h>
304#include <stdlib.h>
fe5dba13 305\&
054f5228
MK
306int
307main(int argc, char *argv[])
308{
309 struct in_addr addr;
fe5dba13 310\&
054f5228 311 if (argc != 2) {
d1a71985 312 fprintf(stderr, "%s <dotted\-address>\en", argv[0]);
054f5228
MK
313 exit(EXIT_FAILURE);
314 }
fe5dba13 315\&
054f5228 316 if (inet_aton(argv[1], &addr) == 0) {
d1a71985 317 fprintf(stderr, "Invalid address\en");
054f5228
MK
318 exit(EXIT_FAILURE);
319 }
fe5dba13 320\&
d1a71985 321 printf("%s\en", inet_ntoa(addr));
054f5228
MK
322 exit(EXIT_SUCCESS);
323}
e7d0bb47 324.EE
b0b6ab4e 325.\" SRC END
47297adb 326.SH SEE ALSO
054f5228
MK
327.BR byteorder (3),
328.BR getaddrinfo (3),
fea681da 329.BR gethostbyname (3),
054f5228 330.BR getnameinfo (3),
fea681da 331.BR getnetent (3),
ebd9e5df 332.BR inet_net_pton (3),
fea681da
MK
333.BR inet_ntop (3),
334.BR inet_pton (3),
335.BR hosts (5),
336.BR networks (5)