xref: /freebsd/lib/libc/net/inet.3 (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1.\" Copyright (c) 1983, 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)inet.3	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd June 14, 2004
36.Dt INET 3
37.Os
38.Sh NAME
39.Nm inet_aton ,
40.Nm inet_addr ,
41.Nm inet_network ,
42.Nm inet_ntoa ,
43.Nm inet_ntop ,
44.Nm inet_pton ,
45.Nm inet_makeaddr ,
46.Nm inet_lnaof ,
47.Nm inet_netof
48.Nd Internet address manipulation routines
49.Sh LIBRARY
50.Lb libc
51.Sh SYNOPSIS
52.In sys/types.h
53.In sys/socket.h
54.In netinet/in.h
55.In arpa/inet.h
56.Ft int
57.Fn inet_aton "const char *cp" "struct in_addr *pin"
58.Ft in_addr_t
59.Fn inet_addr "const char *cp"
60.Ft in_addr_t
61.Fn inet_network "const char *cp"
62.Ft char *
63.Fn inet_ntoa "struct in_addr in"
64.Ft const char *
65.Fo inet_ntop
66.Fa "int af"
67.Fa "const void * restrict src"
68.Fa "char * restrict dst"
69.Fa "socklen_t size"
70.Fc
71.Ft int
72.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst"
73.Ft struct in_addr
74.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna"
75.Ft in_addr_t
76.Fn inet_lnaof "struct in_addr in"
77.Ft in_addr_t
78.Fn inet_netof "struct in_addr in"
79.Sh DESCRIPTION
80The routines
81.Fn inet_aton ,
82.Fn inet_addr
83and
84.Fn inet_network
85interpret character strings representing
86numbers expressed in the Internet standard
87.Ql .\&
88notation.
89.Pp
90The
91.Fn inet_pton
92function converts a presentation format address (that is, printable form
93as held in a character string) to network format (usually a
94.Ft struct in_addr
95or some other internal binary representation, in network byte order).
96It returns 1 if the address was valid for the specified address family, or
970 if the address wasn't parseable in the specified address family, or -1
98if some system error occurred (in which case
99.Va errno
100will have been set).
101This function is presently valid for
102.Dv AF_INET
103and
104.Dv AF_INET6 .
105.Pp
106The
107.Fn inet_aton
108routine interprets the specified character string as an Internet address,
109placing the address into the structure provided.
110It returns 1 if the string was successfully interpreted,
111or 0 if the string is invalid.
112The
113.Fn inet_addr
114and
115.Fn inet_network
116functions return numbers suitable for use
117as Internet addresses and Internet network
118numbers, respectively.
119.Pp
120The function
121.Fn inet_ntop
122converts an address
123.Fa *src
124from network format
125(usually a
126.Ft struct in_addr
127or some other binary form, in network byte order) to presentation format
128(suitable for external display purposes).
129The
130.Fa size
131argument specifies the size, in bytes, of the buffer
132.Fa *dst .
133It returns NULL if a system error occurs (in which case,
134.Va errno
135will have been set), or it returns a pointer to the destination string.
136This function is presently valid for
137.Dv AF_INET
138and
139.Dv AF_INET6 .
140.Pp
141The routine
142.Fn inet_ntoa
143takes an Internet address and returns an
144.Tn ASCII
145string representing the address in
146.Ql .\&
147notation.  The routine
148.Fn inet_makeaddr
149takes an Internet network number and a local
150network address and constructs an Internet address
151from it.  The routines
152.Fn inet_netof
153and
154.Fn inet_lnaof
155break apart Internet host addresses, returning
156the network number and local network address part,
157respectively.
158.Pp
159All Internet addresses are returned in network
160order (bytes ordered from left to right).
161All network numbers and local address parts are
162returned as machine byte order integer values.
163.Sh INTERNET ADDRESSES
164Values specified using the
165.Ql .\&
166notation take one
167of the following forms:
168.Bd -literal -offset indent
169a.b.c.d
170a.b.c
171a.b
172a
173.Ed
174.Pp
175When four parts are specified, each is interpreted
176as a byte of data and assigned, from left to right,
177to the four bytes of an Internet address.  Note
178that when an Internet address is viewed as a 32-bit
179integer quantity on the
180.Tn VAX
181the bytes referred to
182above appear as
183.Dq Li d.c.b.a .
184That is,
185.Tn VAX
186bytes are
187ordered from right to left.
188.Pp
189When a three part address is specified, the last
190part is interpreted as a 16-bit quantity and placed
191in the right-most two bytes of the network address.
192This makes the three part address format convenient
193for specifying Class B network addresses as
194.Dq Li 128.net.host .
195.Pp
196When a two part address is supplied, the last part
197is interpreted as a 24-bit quantity and placed in
198the right most three bytes of the network address.
199This makes the two part address format convenient
200for specifying Class A network addresses as
201.Dq Li net.host .
202.Pp
203When only one part is given, the value is stored
204directly in the network address without any byte
205rearrangement.
206.Pp
207All numbers supplied as
208.Dq parts
209in a
210.Ql .\&
211notation
212may be decimal, octal, or hexadecimal, as specified
213in the C language (i.e., a leading 0x or 0X implies
214hexadecimal; otherwise, a leading 0 implies octal;
215otherwise, the number is interpreted as decimal).
216.Pp
217The
218.Fn inet_aton
219and
220.Fn inet_ntoa
221functions are semi-deprecated in favor of the
222.Xr addr2ascii 3
223family.  However, since those functions are not yet widely implemented,
224portable programs cannot rely on their presence and will continue
225to use the
226.Xr inet 3
227functions for some time.
228.Sh DIAGNOSTICS
229The constant
230.Dv INADDR_NONE
231is returned by
232.Fn inet_addr
233and
234.Fn inet_network
235for malformed requests.
236.Sh ERRORS
237The
238.Fn inet_ntop
239call fails if:
240.Bl -tag -width Er
241.It Bq Er ENOSPC
242.Fa size
243was not large enough to store the presentation form of the address.
244.It Bq Er EAFNOSUPPORT
245.Fa *src
246was not an
247.Dv AF_INET
248or
249.Dv AF_INET6
250family address.
251.El
252.Sh SEE ALSO
253.Xr addr2ascii 3 ,
254.Xr byteorder 3 ,
255.Xr gethostbyname 3 ,
256.Xr getnetent 3 ,
257.Xr inet_net 3 ,
258.Xr hosts 5 ,
259.Xr networks 5
260.Rs
261.%R RFC
262.%N 2373
263.%D July 1998
264.%T "IP Version 6 Addressing Architecture"
265.Re
266.Sh STANDARDS
267The
268.Fn inet_ntop
269and
270.Fn inet_pton
271functions conform to
272.St -xns5.2 .
273Note that
274.Fn inet_pton
275does not accept 1-, 2-, or 3-part dotted addresses; all four parts
276must be specified and are interpreted only as decimal values.
277This is a narrower input set than that accepted by
278.Fn inet_aton .
279.Sh HISTORY
280These
281functions appeared in
282.Bx 4.2 .
283.Sh BUGS
284The value
285.Dv INADDR_NONE
286(0xffffffff) is a valid broadcast address, but
287.Fn inet_addr
288cannot return that value without indicating failure.
289The newer
290.Fn inet_aton
291function does not share this problem.
292The problem of host byte ordering versus network byte ordering is
293confusing.
294The string returned by
295.Fn inet_ntoa
296resides in a static memory area.
297.Pp
298Inet_addr should return a
299.Fa struct in_addr .
300