xref: /freebsd/lib/libc/net/inet.3 (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
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. 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.Dd November 9, 2021
29.Dt INET 3
30.Os
31.Sh NAME
32.Nm inet_aton ,
33.Nm inet_addr ,
34.Nm inet_network ,
35.Nm inet_ntoa ,
36.Nm inet_ntoa_r ,
37.Nm inet_ntop ,
38.Nm inet_pton ,
39.Nm inet_makeaddr ,
40.Nm inet_lnaof ,
41.Nm inet_netof
42.Nd Internet address manipulation routines
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/types.h
47.In sys/socket.h
48.In netinet/in.h
49.In arpa/inet.h
50.Ft int
51.Fn inet_aton "const char *cp" "struct in_addr *pin"
52.Ft in_addr_t
53.Fn inet_addr "const char *cp"
54.Ft in_addr_t
55.Fn inet_network "const char *cp"
56.Ft char *
57.Fn inet_ntoa "struct in_addr in"
58.Ft char *
59.Fo inet_ntoa_r
60.Fa "struct in_addr in"
61.Fa "char *buf"
62.Fa "socklen_t size"
63.Fc
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 was not 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 .
133.Dv INET_ADDRSTRLEN
134and
135.Dv INET6_ADDRSTRLEN
136define the maximum size required to convert an address of the respective
137type.
138It returns NULL if a system error occurs (in which case,
139.Va errno
140will have been set), or it returns a pointer to the destination string.
141This function is presently valid for
142.Dv AF_INET
143and
144.Dv AF_INET6 .
145.Pp
146The routine
147.Fn inet_ntoa
148takes an Internet address and returns an
149.Tn ASCII
150string representing the address in
151.Ql .\&
152notation.
153The routine
154.Fn inet_ntoa_r
155is the reentrant version of
156.Fn inet_ntoa .
157The deprecated routine
158.Fn inet_makeaddr
159takes an Internet network number and a local
160host address on that network, and constructs an Internet address
161from it.
162It should only be assumed to work for historical class A/B/C networks.
163The deprecated routines
164.Fn inet_netof
165and
166.Fn inet_lnaof
167break apart Internet host addresses, returning
168the network number and local host address part,
169respectively,
170assuming the historical class A/B/C network masks.
171.Pp
172All Internet addresses are returned in network
173order (bytes ordered from left to right).
174All network numbers and local address parts are
175returned as machine byte order integer values.
176.Sh INTERNET ADDRESSES
177Values specified using the
178.Ql .\&
179notation take one
180of the following forms:
181.Bd -literal -offset indent
182a.b.c.d
183a.b.c
184a.b
185a
186.Ed
187.Pp
188When four parts are specified, each is interpreted
189as a byte of data and assigned, from left to right,
190to the four bytes of an Internet address.
191.Pp
192When a three part address is specified, the last
193part is interpreted as a 16-bit quantity and placed
194in the least significant two bytes of the network address.
195.Pp
196When a two part address is supplied, the last part
197is interpreted as a 24-bit quantity and placed in
198the least significant three bytes of the network address.
199.Pp
200When only one part is given, the value is stored
201directly in the network address without any byte
202rearrangement.
203.Pp
204All numbers supplied as
205.Dq parts
206in a
207.Ql .\&
208notation
209may be decimal, octal, or hexadecimal, as specified
210in the C language (i.e., a leading 0x or 0X implies
211hexadecimal; otherwise, a leading 0 implies octal;
212otherwise, the number is interpreted as decimal).
213.Sh DIAGNOSTICS
214The constant
215.Dv INADDR_NONE
216is returned by
217.Fn inet_addr
218and
219.Fn inet_network
220for malformed requests.
221.Sh ERRORS
222The
223.Fn inet_ntop
224call fails if:
225.Bl -tag -width Er
226.It Bq Er ENOSPC
227.Fa size
228was not large enough to store the presentation form of the address.
229.It Bq Er EAFNOSUPPORT
230.Fa *src
231was not an
232.Dv AF_INET
233or
234.Dv AF_INET6
235family address.
236.El
237.Sh SEE ALSO
238.Xr byteorder 3 ,
239.Xr getaddrinfo 3 ,
240.Xr gethostbyname 3 ,
241.Xr getnameinfo 3 ,
242.Xr getnetent 3 ,
243.Xr inet_net 3 ,
244.Xr hosts 5 ,
245.Xr networks 5
246.Rs
247.%R RFC
248.%N 2373
249.%D July 1998
250.%T "IP Version 6 Addressing Architecture"
251.Re
252.Sh STANDARDS
253The
254.Fn inet_ntop
255and
256.Fn inet_pton
257functions conform to
258.St -xns5.2 .
259Note that
260.Fn inet_pton
261does not accept 1-, 2-, or 3-part dotted addresses; all four parts
262must be specified and are interpreted only as decimal values.
263This is a narrower input set than that accepted by
264.Fn inet_aton .
265.Sh HISTORY
266These
267functions appeared in
268.Bx 4.2 .
269.Sh BUGS
270The value
271.Dv INADDR_NONE
272(0xffffffff) is a valid broadcast address, but
273.Fn inet_addr
274cannot return that value without indicating failure.
275The newer
276.Fn inet_aton
277function does not share this problem.
278The problem of host byte ordering versus network byte ordering is
279confusing.
280The string returned by
281.Fn inet_ntoa
282resides in a static memory area.
283.Pp
284The
285.Fn inet_addr
286function should return a
287.Fa struct in_addr .
288