xref: /openbsd/lib/libc/net/inet_net_ntop.3 (revision d415bd75)
1.\"	$OpenBSD: inet_net_ntop.3,v 1.4 2022/09/11 06:38:10 jmc Exp $
2.\"	$NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $
3.\"
4.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Luke Mewburn.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29.\" POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: September 11 2022 $
32.Dt INET_NET_NTOP 3
33.Os
34.Sh NAME
35.Nm inet_net_ntop ,
36.Nm inet_net_pton
37.Nd Internet network number manipulation routines
38.Sh SYNOPSIS
39.In sys/socket.h
40.In arpa/inet.h
41.Ft char *
42.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
43.Ft int
44.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
45.Sh DESCRIPTION
46The
47.Fn inet_net_ntop
48function converts an Internet network number from network format (usually a
49.Vt struct in_addr
50or some other binary form, in network byte order) to CIDR presentation format
51(suitable for external display purposes).
52.Fa bits
53is the number of bits in
54.Fa src
55that are the network number.
56It returns
57.Dv NULL
58if a system error occurs (in which case,
59.Va errno
60will have been set), or it returns a pointer to the destination string.
61.Pp
62The
63.Fn inet_net_pton
64function converts a presentation format Internet network number (that is,
65printable form as held in a character string) to network format (usually a
66.Vt struct in_addr
67or some other internal binary representation, in network byte order).
68It returns the number of bits (either computed based on the class, or
69specified with /CIDR), or \-1 if a failure occurred
70(in which case
71.Va errno
72will have been set.
73It will be set to
74.Er ENOENT
75if the Internet network number was not valid).
76.Pp
77Caution:
78The
79.Fa dst
80field should be zeroed before calling
81.Fn inet_net_pton
82as the function will only fill the number of bytes necessary to
83encode the network number in network byte order.
84.Pp
85The only values for
86.Fa af
87currently supported are
88.Dv AF_INET
89and
90.Dv AF_INET6 .
91.Fa size
92is the size of the result buffer
93.Fa dst .
94.Sh NETWORK NUMBERS (IP VERSION 4)
95The external representation of Internet network numbers may be specified in
96one of the following forms:
97.Bd -literal -offset indent
98a
99a.b
100a.b.c
101a.b.c.d
102.Ed
103.Pp
104Any of the above four forms may have
105.Dq Li /bits
106appended where
107.Dq Li bits
108is in the range
109.Li 0-32
110and is used to explicitly specify the number of bits in the network address.
111When
112.Dq Li /bits
113is not specified, the number of bits in the network address is calculated
114as the larger of the number of bits in the class to which the address
115belongs and the number of bits provided rounded up modulo 8.
116Examples:
117.Pp
118.Bl -tag -width 10.1.2.3/24 -offset indent -compact
119.It Li 10
120an 8-bit network number (class A), value
121.Li 10.0.0.0 .
122.It Li 192
123a 24-bit network number (class C), value
124.Li 192.0.0.0 .
125.It Li 10.10
126a 16-bit network number, value
127.Li 10.10.0.0 .
128.It Li 10.1.2
129a 24-bit network number, value
130.Li 10.1.2.0 .
131.It Li 10.1.2.3
132a 32-bit network number, value
133.Li 10.1.2.3 .
134.It Li 10.1.2.3/24
135a 24-bit network number (explicit), value
136.Li 10.1.2.3 .
137.El
138.Pp
139Note that when the number of bits is specified using
140.Dq Li /bits
141notation, the value of the address still includes all bits supplied
142in the external representation, even those bits which are the host
143part of an Internet address.
144Also, unlike
145.Xr inet_pton 3
146where the external representation is assumed to be a host address, the
147external representation for
148.Fn inet_net_pton
149is assumed to be a network address.
150Thus
151.Dq Li 10.1
152is assumed to be
153.Dq Li 10.1.0.0
154not
155.Dq Li 10.0.0.1
156.Pp
157All numbers supplied as
158.Dq parts
159in a
160.Ql \&.
161notation
162may be decimal, octal, or hexadecimal, as specified
163in the C language (i.e., a leading 0x or 0X implies
164hexadecimal; otherwise, a leading 0 implies octal;
165otherwise, the number is interpreted as decimal).
166.Sh NETWORK NUMBERS (IP VERSION 6)
167See
168.Xr inet_pton 3
169for valid external representations of IP version 6 addresses.
170A valid external representation may have
171.Dq Li /bits
172appended where
173.Dq Li bits
174is in the range
175.Li 0-128
176and is used to explicitly specify the number of bits in the network address.
177When
178.Dq Li /bits
179is not specified, 128 is used.
180Note that when the number of bits is specified using
181.Dq Li /bits
182notation, the value of the address still includes all bits supplied
183in the external representation, even those bits which are the host
184part of an Internet address.
185.Sh SEE ALSO
186.Xr htonl 3 ,
187.Xr inet_pton 3 ,
188.Xr inet 4 ,
189.Xr hosts 5
190.Sh HISTORY
191The
192.Nm inet_net_ntop
193and
194.Nm inet_net_pton
195functions first appeared in BIND 4.9.4.
196