OSDN Git Service

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