]>
Commit | Line | Data |
---|---|---|
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 | .\" |
93015253 | 5 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
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. | |
4b72fb64 | 25 | .\" %%%LICENSE_END |
fea681da MK |
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. | |
054f5228 MK |
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. | |
fea681da | 41 | .\" |
4b8c67d9 | 42 | .TH INET 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
fea681da | 43 | .SH NAME |
c13182ef | 44 | inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, |
fea681da MK |
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> | |
68e4db0a | 51 | .PP |
fea681da | 52 | .BI "int inet_aton(const char *" cp ", struct in_addr *" inp ); |
68e4db0a | 53 | .PP |
fea681da | 54 | .BI "in_addr_t inet_addr(const char *" cp ); |
68e4db0a | 55 | .PP |
fea681da | 56 | .BI "in_addr_t inet_network(const char *" cp ); |
68e4db0a | 57 | .PP |
fea681da | 58 | .BI "char *inet_ntoa(struct in_addr " in ); |
68e4db0a | 59 | .PP |
92263c07 | 60 | .BI "struct in_addr inet_makeaddr(in_addr_t " net ", in_addr_t " host ); |
68e4db0a | 61 | .PP |
fea681da | 62 | .BI "in_addr_t inet_lnaof(struct in_addr " in ); |
68e4db0a | 63 | .PP |
fea681da MK |
64 | .BI "in_addr_t inet_netof(struct in_addr " in ); |
65 | .fi | |
68e4db0a | 66 | .PP |
cc4615cc MK |
67 | .in -4n |
68 | Feature Test Macro Requirements for glibc (see | |
69 | .BR feature_test_macros (7)): | |
70 | .in | |
68e4db0a | 71 | .PP |
cc4615cc MK |
72 | .BR inet_aton (), |
73 | .BR inet_ntoa (): | |
7f0ec8ee MK |
74 | .nf |
75 | Since glibc 2.19: | |
76 | _DEFAULT_SOURCE | |
77 | In glibc up to and including 2.19: | |
78 | _BSD_SOURCE || _BSD_SOURCE | |
79 | .fi | |
fea681da | 80 | .SH DESCRIPTION |
60a90ecd MK |
81 | .BR inet_aton () |
82 | converts the Internet host address \fIcp\fP from the | |
054f5228 MK |
83 | IPv4 numbers-and-dots notation into binary form (in network byte order) |
84 | and stores it in the structure that \fIinp\fP points to. | |
60a90ecd | 85 | .BR inet_aton () |
c7094399 | 86 | returns nonzero if the address is valid, zero if not. |
054f5228 MK |
87 | The address supplied in |
88 | .I cp | |
89 | can have one of the following forms: | |
90 | .TP 10 | |
91 | .I a.b.c.d | |
57e5ca03 | 92 | Each of the four numeric parts specifies a byte of the address; |
054f5228 MK |
93 | the bytes are assigned in left-to-right order to produce the binary address. |
94 | .TP | |
95 | .I a.b.c | |
96 | Parts | |
97 | .I a | |
98 | and | |
99 | .I b | |
100 | specify the first two bytes of the binary address. | |
101 | Part | |
102 | .I c | |
103 | is interpreted as a 16-bit value that defines the rightmost two bytes | |
104 | of the binary address. | |
105 | This notation is suitable for specifying (outmoded) Class B | |
106 | network addresses. | |
107 | .TP | |
108 | .I a.b | |
109 | Part | |
110 | .I a | |
111 | specifies the first byte of the binary address. | |
112 | Part | |
113 | .I b | |
114 | is interpreted as a 24-bit value that defines the rightmost three bytes | |
115 | of the binary address. | |
5526923a | 116 | This notation is suitable for specifying (outmoded) Class A |
054f5228 MK |
117 | network addresses. |
118 | .TP | |
119 | .I a | |
120 | The value | |
121 | .I a | |
122 | is interpreted as a 32-bit value that is stored directly | |
123 | into the binary address without any byte rearrangement. | |
124 | .PP | |
125 | In all of the above forms, | |
126 | components of the dotted address can be specified in decimal, | |
127 | octal (with a leading | |
128 | .IR 0 ), | |
129 | or hexadecimal, with a leading | |
130 | .IR 0X ). | |
131 | Addresses in any of these forms are collectively termed | |
132 | .IR "IPV4 numbers-and-dots notation" . | |
133 | The form that uses exactly four decimal numbers is referred to as | |
134 | .IR "IPv4 dotted-decimal notation" | |
135 | (or sometimes: | |
136 | .IR "IPv4 dotted-quad notation" ). | |
847e0d88 | 137 | .PP |
4cbfaed0 MK |
138 | .BR inet_aton () |
139 | returns 1 if the supplied string was successfully interpreted, | |
140 | or 0 if the string is invalid | |
141 | .RB ( errno | |
142 | is | |
143 | .I not | |
144 | set on error). | |
fea681da | 145 | .PP |
60a90ecd MK |
146 | The |
147 | .BR inet_addr () | |
148 | function converts the Internet host address | |
054f5228 | 149 | \fIcp\fP from IPv4 numbers-and-dots notation into binary data in network |
c13182ef | 150 | byte order. |
2f0af33b MK |
151 | If the input is invalid, |
152 | .B INADDR_NONE | |
153 | (usually \-1) is returned. | |
054f5228 MK |
154 | Use of this function is problematic because \-1 is a valid address |
155 | (255.255.255.255). | |
156 | Avoid its use in favor of | |
60a90ecd | 157 | .BR inet_aton (), |
054f5228 MK |
158 | .BR inet_pton (3), |
159 | or | |
a414d0b5 | 160 | .BR getaddrinfo (3), |
054f5228 | 161 | which provide a cleaner way to indicate error return. |
fea681da | 162 | .PP |
60a90ecd MK |
163 | The |
164 | .BR inet_network () | |
054f5228 MK |
165 | function converts |
166 | .IR cp , | |
167 | a string in IPv4 numbers-and-dots notation, | |
168 | into a number in host byte order suitable for use as an | |
169 | Internet network address. | |
170 | On success, the converted address is returned. | |
7cc028fb | 171 | If the input is invalid, \-1 is returned. |
fea681da | 172 | .PP |
60a90ecd MK |
173 | The |
174 | .BR inet_ntoa () | |
175 | function converts the Internet host address | |
054f5228 MK |
176 | \fIin\fP, given in network byte order, to a string in IPv4 |
177 | dotted-decimal notation. | |
c13182ef | 178 | The string is returned in a statically |
fea681da MK |
179 | allocated buffer, which subsequent calls will overwrite. |
180 | .PP | |
60a90ecd | 181 | The |
60a90ecd | 182 | .BR inet_lnaof () |
054f5228 | 183 | function returns the local network address part |
c13182ef | 184 | of the Internet address \fIin\fP. |
054f5228 | 185 | The returned value is in host byte order. |
fea681da | 186 | .PP |
60a90ecd MK |
187 | The |
188 | .BR inet_netof () | |
189 | function returns the network number part of | |
054f5228 MK |
190 | the Internet address \fIin\fP. |
191 | The returned value is in host byte order. | |
192 | .PP | |
193 | The | |
194 | .BR inet_makeaddr () | |
195 | function is the converse of | |
61792fc6 | 196 | .BR inet_netof () |
054f5228 MK |
197 | and |
198 | .BR inet_lnaof (). | |
199 | It returns an Internet host address in network byte order, | |
200 | created by combining the network number \fInet\fP | |
201 | with the local address \fIhost\fP, both in | |
202 | host byte order. | |
fea681da | 203 | .PP |
60a90ecd MK |
204 | The structure \fIin_addr\fP as used in |
205 | .BR inet_ntoa (), | |
206 | .BR inet_makeaddr (), | |
3c775eb7 | 207 | .BR inet_lnaof () |
60a90ecd MK |
208 | and |
209 | .BR inet_netof () | |
a9a13a50 MK |
210 | is defined in |
211 | .I <netinet/in.h> | |
212 | as: | |
51f5698d | 213 | .PP |
bd191423 | 214 | .in +4n |
bdd915e2 | 215 | .EX |
9f8162f9 MK |
216 | typedef uint32_t in_addr_t; |
217 | ||
fea681da | 218 | struct in_addr { |
9f8162f9 MK |
219 | in_addr_t s_addr; |
220 | }; | |
bdd915e2 | 221 | .EE |
bd191423 | 222 | .in |
b73c9bd5 PH |
223 | .SH ATTRIBUTES |
224 | For an explanation of the terms used in this section, see | |
225 | .BR attributes (7). | |
226 | .TS | |
227 | allbox; | |
228 | lbw30 lb lb | |
229 | l l l. | |
230 | Interface Attribute Value | |
231 | T{ | |
232 | .BR inet_aton (), | |
93ead13c MS |
233 | .BR inet_addr (), |
234 | .br | |
235 | .BR inet_network (), | |
236 | .BR inet_ntoa () | |
b73c9bd5 PH |
237 | T} Thread safety MT-Safe locale |
238 | T{ | |
b73c9bd5 PH |
239 | .BR inet_makeaddr (), |
240 | .BR inet_lnaof (), | |
241 | .br | |
242 | .BR inet_netof () | |
243 | T} Thread safety MT-Safe | |
244 | .TE | |
47297adb | 245 | .SH CONFORMING TO |
492a45fe MK |
246 | .BR inet_addr (), |
247 | .BR inet_ntoa (): | |
248 | POSIX.1-2001, POSIX.1-2008, 4.3BSD. | |
847e0d88 | 249 | .PP |
054f5228 | 250 | .BR inet_aton () |
492a45fe | 251 | is not specified in POSIX.1, but is available on most systems. |
19c98696 | 252 | .SH NOTES |
b3f78bdc | 253 | On x86 architectures, the host byte order is Least Significant Byte |
054f5228 MK |
254 | first (little endian), whereas the network byte order, as used on the |
255 | Internet, is Most Significant Byte first (big endian). | |
847e0d88 | 256 | .PP |
054f5228 MK |
257 | .BR inet_lnaof (), |
258 | .BR inet_netof (), | |
259 | and | |
260 | .BR inet_makeaddr () | |
261 | are legacy functions that assume they are dealing with | |
262 | .IR "classful network addresses" . | |
263 | Classful networking divides IPv4 network addresses into host and network | |
264 | components at byte boundaries, as follows: | |
265 | .TP 10 | |
266 | Class A | |
267 | This address type is indicated by the value 0 in the | |
268 | most significant bit of the (network byte ordered) address. | |
269 | The network address is contained in the most significant byte, | |
270 | and the host address occupies the remaining three bytes. | |
271 | .TP | |
272 | Class B | |
273 | This address type is indicated by the binary value 10 in the | |
274 | most significant two bits of the address. | |
275 | The network address is contained in the two most significant bytes, | |
276 | and the host address occupies the remaining two bytes. | |
277 | .TP | |
278 | Class C | |
279 | This address type is indicated by the binary value 110 in the | |
280 | most significant three bits of the address. | |
281 | The network address is contained in the three most significant bytes, | |
282 | and the host address occupies the remaining byte. | |
283 | .PP | |
ab186fbd | 284 | Classful network addresses are now obsolete, |
054f5228 MK |
285 | having been superseded by Classless Inter-Domain Routing (CIDR), |
286 | which divides addresses into network and host components at | |
287 | arbitrary bit (rather than byte) boundaries. | |
288 | .SH EXAMPLE | |
289 | An example of the use of | |
290 | .BR inet_aton () | |
291 | and | |
292 | .BR inet_ntoa () | |
293 | is shown below. | |
294 | Here are some example runs: | |
e646a1ba | 295 | .PP |
054f5228 | 296 | .in +4n |
e646a1ba | 297 | .EX |
b43a3b30 | 298 | .RB "$" " ./a.out 226.000.000.037" " # Last byte is in octal" |
054f5228 | 299 | 226.0.0.31 |
b43a3b30 | 300 | .RB "$" " ./a.out 0x7f.1 " " # First byte is in hex" |
054f5228 | 301 | 127.0.0.1 |
b8302363 | 302 | .EE |
054f5228 | 303 | .in |
9c330504 | 304 | .SS Program source |
d84d0300 | 305 | \& |
e7d0bb47 | 306 | .EX |
054f5228 MK |
307 | #define _BSD_SOURCE |
308 | #include <arpa/inet.h> | |
309 | #include <stdio.h> | |
310 | #include <stdlib.h> | |
311 | ||
312 | int | |
313 | main(int argc, char *argv[]) | |
314 | { | |
315 | struct in_addr addr; | |
316 | ||
317 | if (argc != 2) { | |
318 | fprintf(stderr, "%s <dotted\-address>\\n", argv[0]); | |
319 | exit(EXIT_FAILURE); | |
320 | } | |
321 | ||
322 | if (inet_aton(argv[1], &addr) == 0) { | |
7d90b3d3 | 323 | fprintf(stderr, "Invalid address\\n"); |
054f5228 MK |
324 | exit(EXIT_FAILURE); |
325 | } | |
326 | ||
327 | printf("%s\\n", inet_ntoa(addr)); | |
328 | exit(EXIT_SUCCESS); | |
329 | } | |
e7d0bb47 | 330 | .EE |
47297adb | 331 | .SH SEE ALSO |
054f5228 MK |
332 | .BR byteorder (3), |
333 | .BR getaddrinfo (3), | |
fea681da | 334 | .BR gethostbyname (3), |
054f5228 | 335 | .BR getnameinfo (3), |
fea681da | 336 | .BR getnetent (3), |
ebd9e5df | 337 | .BR inet_net_pton (3), |
fea681da MK |
338 | .BR inet_ntop (3), |
339 | .BR inet_pton (3), | |
340 | .BR hosts (5), | |
341 | .BR networks (5) |