]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/inet.3
ffix
[thirdparty/man-pages.git] / man3 / inet.3
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
054f5228 4.\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
5.\"
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.
9.\"
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.
c13182ef 14.\"
fea681da
MK
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
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
25.\"
26.\" References consulted:
27.\" Linux libc source code
28.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29.\" 386BSD man pages
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.
054f5228
MK
35.\" 2008-06-19, mtk
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.
fea681da 40.\"
054f5228 41.TH INET 3 2008-06-19 "GNU" "Linux Programmer's Manual"
fea681da 42.SH NAME
c13182ef 43inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
fea681da
MK
44inet_netof \- Internet address manipulation routines
45.SH SYNOPSIS
46.nf
47.B #include <sys/socket.h>
48.B #include <netinet/in.h>
49.B #include <arpa/inet.h>
50.sp
51.BI "int inet_aton(const char *" cp ", struct in_addr *" inp );
52.sp
53.BI "in_addr_t inet_addr(const char *" cp );
54.sp
55.BI "in_addr_t inet_network(const char *" cp );
56.sp
57.BI "char *inet_ntoa(struct in_addr " in );
58.sp
59.BI "struct in_addr inet_makeaddr(int " net ", int " host );
60.sp
61.BI "in_addr_t inet_lnaof(struct in_addr " in );
62.sp
63.BI "in_addr_t inet_netof(struct in_addr " in );
64.fi
cc4615cc
MK
65.sp
66.in -4n
67Feature Test Macro Requirements for glibc (see
68.BR feature_test_macros (7)):
69.in
70.sp
71.BR inet_aton (),
72.BR inet_ntoa ():
73_BSD_SOURCE || _SVID_SOURCE
fea681da 74.SH DESCRIPTION
60a90ecd
MK
75.BR inet_aton ()
76converts the Internet host address \fIcp\fP from the
054f5228
MK
77IPv4 numbers-and-dots notation into binary form (in network byte order)
78and stores it in the structure that \fIinp\fP points to.
60a90ecd 79.BR inet_aton ()
054f5228
MK
80returns non-zero if the address is valid, zero if not.
81The address supplied in
82.I cp
83can have one of the following forms:
84.TP 10
85.I a.b.c.d
86Each of the for numeric parts specifies a byte of the address;
87the bytes are assigned in left-to-right order to produce the binary address.
88.TP
89.I a.b.c
90Parts
91.I a
92and
93.I b
94specify the first two bytes of the binary address.
95Part
96.I c
97is interpreted as a 16-bit value that defines the rightmost two bytes
98of the binary address.
99This notation is suitable for specifying (outmoded) Class B
100network addresses.
101.TP
102.I a.b
103Part
104.I a
105specifies the first byte of the binary address.
106Part
107.I b
108is interpreted as a 24-bit value that defines the rightmost three bytes
109of the binary address.
110This notation is suitable for specifying (outmoded) Class C
111network addresses.
112.TP
113.I a
114The value
115.I a
116is interpreted as a 32-bit value that is stored directly
117into the binary address without any byte rearrangement.
118.PP
119In all of the above forms,
120components of the dotted address can be specified in decimal,
121octal (with a leading
122.IR 0 ),
123or hexadecimal, with a leading
124.IR 0X ).
125Addresses in any of these forms are collectively termed
126.IR "IPV4 numbers-and-dots notation" .
127The form that uses exactly four decimal numbers is referred to as
128.IR "IPv4 dotted-decimal notation"
129(or sometimes:
130.IR "IPv4 dotted-quad notation" ).
fea681da 131.PP
60a90ecd
MK
132The
133.BR inet_addr ()
134function converts the Internet host address
054f5228 135\fIcp\fP from IPv4 numbers-and-dots notation into binary data in network
c13182ef 136byte order.
2f0af33b
MK
137If the input is invalid,
138.B INADDR_NONE
139(usually \-1) is returned.
054f5228
MK
140Use of this function is problematic because \-1 is a valid address
141(255.255.255.255).
142Avoid its use in favor of
60a90ecd 143.BR inet_aton (),
054f5228
MK
144.BR inet_pton (3),
145or
146.BR getaddrinfo (3)
147which provide a cleaner way to indicate error return.
fea681da 148.PP
60a90ecd
MK
149The
150.BR inet_network ()
054f5228
MK
151function converts
152.IR cp ,
153a string in IPv4 numbers-and-dots notation,
154into a number in host byte order suitable for use as an
155Internet network address.
156On success, the converted address is returned.
7cc028fb 157If the input is invalid, \-1 is returned.
fea681da 158.PP
60a90ecd
MK
159The
160.BR inet_ntoa ()
161function converts the Internet host address
054f5228
MK
162\fIin\fP, given in network byte order, to a string in IPv4
163dotted-decimal notation.
c13182ef 164The string is returned in a statically
fea681da
MK
165allocated buffer, which subsequent calls will overwrite.
166.PP
60a90ecd 167The
60a90ecd 168.BR inet_lnaof ()
054f5228 169function returns the local network address part
c13182ef 170of the Internet address \fIin\fP.
054f5228 171The returned value is in host byte order.
fea681da 172.PP
60a90ecd
MK
173The
174.BR inet_netof ()
175function returns the network number part of
054f5228
MK
176the Internet address \fIin\fP.
177The returned value is in host byte order.
178.PP
179The
180.BR inet_makeaddr ()
181function is the converse of
61792fc6 182.BR inet_netof ()
054f5228
MK
183and
184.BR inet_lnaof ().
185It returns an Internet host address in network byte order,
186created by combining the network number \fInet\fP
187with the local address \fIhost\fP, both in
188host byte order.
fea681da 189.PP
60a90ecd
MK
190The structure \fIin_addr\fP as used in
191.BR inet_ntoa (),
192.BR inet_makeaddr (),
3c775eb7 193.BR inet_lnaof ()
60a90ecd
MK
194and
195.BR inet_netof ()
a9a13a50
MK
196is defined in
197.I <netinet/in.h>
198as:
fea681da 199.sp
bd191423 200.in +4n
fea681da 201.nf
9f8162f9
MK
202typedef uint32_t in_addr_t;
203
fea681da 204struct in_addr {
9f8162f9
MK
205 in_addr_t s_addr;
206};
fea681da 207.fi
bd191423 208.in
2b2581ee
MK
209.SH "CONFORMING TO"
2104.3BSD.
054f5228 211.BR inet_addr ()
2b2581ee
MK
212and
213.BR inet_ntoa ()
214are specified in POSIX.1-2001.
054f5228
MK
215.BR inet_aton ()
216is not specified in POSIX.1.-2001, but is available on most systems.
19c98696 217.SH NOTES
054f5228
MK
218On the i386 the host byte order is Least Significant Byte
219first (little endian), whereas the network byte order, as used on the
220Internet, is Most Significant Byte first (big endian).
221
222.BR inet_lnaof (),
223.BR inet_netof (),
224and
225.BR inet_makeaddr ()
226are legacy functions that assume they are dealing with
227.IR "classful network addresses" .
228Classful networking divides IPv4 network addresses into host and network
229components at byte boundaries, as follows:
230.TP 10
231Class A
232This address type is indicated by the value 0 in the
233most significant bit of the (network byte ordered) address.
234The network address is contained in the most significant byte,
235and the host address occupies the remaining three bytes.
236.TP
237Class B
238This address type is indicated by the binary value 10 in the
239most significant two bits of the address.
240The network address is contained in the two most significant bytes,
241and the host address occupies the remaining two bytes.
242.TP
243Class C
244This address type is indicated by the binary value 110 in the
245most significant three bits of the address.
246The network address is contained in the three most significant bytes,
247and the host address occupies the remaining byte.
248.PP
249Classful network addresses are now obolete,
250having been superseded by Classless Inter-Domain Routing (CIDR),
251which divides addresses into network and host components at
252arbitrary bit (rather than byte) boundaries.
253.SH EXAMPLE
254An example of the use of
255.BR inet_aton ()
256and
257.BR inet_ntoa ()
258is shown below.
259Here are some example runs:
260.in +4n
261.nf
262
263$ ./a.out 226.000.000.037 # Last byte is in octal
264226.0.0.31
265$ ./a.out 0x7f.1 # First byte is in hex
266127.0.0.1
267
268.fi
269.in
270.nf
271#define _BSD_SOURCE
272#include <arpa/inet.h>
273#include <stdio.h>
274#include <stdlib.h>
275
276int
277main(int argc, char *argv[])
278{
279 struct in_addr addr;
280
281 if (argc != 2) {
282 fprintf(stderr, "%s <dotted\-address>\\n", argv[0]);
283 exit(EXIT_FAILURE);
284 }
285
286 if (inet_aton(argv[1], &addr) == 0) {
287 perror("inet_aton");
288 exit(EXIT_FAILURE);
289 }
290
291 printf("%s\\n", inet_ntoa(addr));
292 exit(EXIT_SUCCESS);
293}
294.fi
fea681da 295.SH "SEE ALSO"
054f5228
MK
296.BR byteorder (3),
297.BR getaddrinfo (3),
fea681da 298.BR gethostbyname (3),
054f5228 299.BR getnameinfo (3),
fea681da
MK
300.BR getnetent (3),
301.BR inet_ntop (3),
302.BR inet_pton (3),
303.BR hosts (5),
304.BR networks (5)