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