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