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