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