OSDN Git Service

257697074e9607f1f728651887a7a05d2c052a38
[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  2014-04-19 "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 A
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
133 .BR inet_aton ()
134 returns 1 if the supplied string was successfully interpreted,
135 or 0 if the string is invalid
136 .RB ( errno
137 is
138 .I not
139 set on error).
140 .PP
141 The
142 .BR inet_addr ()
143 function converts the Internet host address
144 \fIcp\fP from IPv4 numbers-and-dots notation into binary data in network
145 byte order.
146 If the input is invalid,
147 .B INADDR_NONE
148 (usually \-1) is returned.
149 Use of this function is problematic because \-1 is a valid address
150 (255.255.255.255).
151 Avoid its use in favor of
152 .BR inet_aton (),
153 .BR inet_pton (3),
154 or
155 .BR getaddrinfo (3),
156 which provide a cleaner way to indicate error return.
157 .PP
158 The
159 .BR inet_network ()
160 function converts
161 .IR cp ,
162 a string in IPv4 numbers-and-dots notation,
163 into a number in host byte order suitable for use as an
164 Internet network address.
165 On success, the converted address is returned.
166 If the input is invalid, \-1 is returned.
167 .PP
168 The
169 .BR inet_ntoa ()
170 function converts the Internet host address
171 \fIin\fP, given in network byte order, to a string in IPv4
172 dotted-decimal notation.
173 The string is returned in a statically
174 allocated buffer, which subsequent calls will overwrite.
175 .PP
176 The
177 .BR inet_lnaof ()
178 function returns the local network address part
179 of the Internet address \fIin\fP.
180 The returned value is in host byte order.
181 .PP
182 The
183 .BR inet_netof ()
184 function returns the network number part of
185 the Internet address \fIin\fP.
186 The returned value is in host byte order.
187 .PP
188 The
189 .BR inet_makeaddr ()
190 function is the converse of
191 .BR inet_netof ()
192 and
193 .BR inet_lnaof ().
194 It returns an Internet host address in network byte order,
195 created by combining the network number \fInet\fP
196 with the local address \fIhost\fP, both in
197 host byte order.
198 .PP
199 The structure \fIin_addr\fP as used in
200 .BR inet_ntoa (),
201 .BR inet_makeaddr (),
202 .BR inet_lnaof ()
203 and
204 .BR inet_netof ()
205 is defined in
206 .I <netinet/in.h>
207 as:
208 .sp
209 .in +4n
210 .nf
211 typedef uint32_t in_addr_t;
212
213 struct in_addr {
214     in_addr_t s_addr;
215 };
216 .fi
217 .in
218 .SH CONFORMING TO
219 4.3BSD.
220 .BR inet_addr ()
221 and
222 .BR inet_ntoa ()
223 are specified in POSIX.1-2001.
224 .BR inet_aton ()
225 is not specified in POSIX.1-2001, but is available on most systems.
226 .SH NOTES
227 On the i386 the host byte order is Least Significant Byte
228 first (little endian), whereas the network byte order, as used on the
229 Internet, is Most Significant Byte first (big endian).
230
231 .BR inet_lnaof (),
232 .BR inet_netof (),
233 and
234 .BR inet_makeaddr ()
235 are legacy functions that assume they are dealing with
236 .IR "classful network addresses" .
237 Classful networking divides IPv4 network addresses into host and network
238 components at byte boundaries, as follows:
239 .TP 10
240 Class A
241 This address type is indicated by the value 0 in the
242 most significant bit of the (network byte ordered) address.
243 The network address is contained in the most significant byte,
244 and the host address occupies the remaining three bytes.
245 .TP
246 Class B
247 This address type is indicated by the binary value 10 in the
248 most significant two bits of the address.
249 The network address is contained in the two most significant bytes,
250 and the host address occupies the remaining two bytes.
251 .TP
252 Class C
253 This address type is indicated by the binary value 110 in the
254 most significant three bits of the address.
255 The network address is contained in the three most significant bytes,
256 and the host address occupies the remaining byte.
257 .PP
258 Classful network addresses are now obsolete,
259 having been superseded by Classless Inter-Domain Routing (CIDR),
260 which divides addresses into network and host components at
261 arbitrary bit (rather than byte) boundaries.
262 .SH EXAMPLE
263 An example of the use of
264 .BR inet_aton ()
265 and
266 .BR inet_ntoa ()
267 is shown below.
268 Here are some example runs:
269 .in +4n
270 .nf
271
272 .RB "$" " ./a.out 226.000.000.037" "      # Last byte is in octal"
273 226.0.0.31
274 .RB "$" " ./a.out 0x7f.1         " "      # First byte is in hex"
275 127.0.0.1
276 .fi
277 .in
278 .SS Program source
279 \&
280 .nf
281 #define _BSD_SOURCE
282 #include <arpa/inet.h>
283 #include <stdio.h>
284 #include <stdlib.h>
285
286 int
287 main(int argc, char *argv[])
288 {
289     struct in_addr addr;
290
291     if (argc != 2) {
292         fprintf(stderr, "%s <dotted\-address>\\n", argv[0]);
293         exit(EXIT_FAILURE);
294     }
295
296     if (inet_aton(argv[1], &addr) == 0) {
297         fprintf(stderr, "Invalid address\\n");
298         exit(EXIT_FAILURE);
299     }
300
301     printf("%s\\n", inet_ntoa(addr));
302     exit(EXIT_SUCCESS);
303 }
304 .fi
305 .SH SEE ALSO
306 .BR byteorder (3),
307 .BR getaddrinfo (3),
308 .BR gethostbyname (3),
309 .BR getnameinfo (3),
310 .BR getnetent (3),
311 .BR inet_net_pton (3),
312 .BR inet_ntop (3),
313 .BR inet_pton (3),
314 .BR hosts (5),
315 .BR networks (5)
316 .SH COLOPHON
317 This page is part of release 3.67 of the Linux
318 .I man-pages
319 project.
320 A description of the project,
321 information about reporting bugs,
322 and the latest version of this page,
323 can be found at
324 \%http://www.kernel.org/doc/man\-pages/.