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