xref: /netbsd-src/lib/libc/inet/inet_net.3 (revision 4fe69537785853a97dba3e4a27b7beb90a728cfb)
1.\"	$NetBSD: inet_net.3,v 1.5 2012/07/20 20:48:59 wiz Exp $
2.\"
3.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Luke Mewburn.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd July 20, 2012
31.Dt INET_NET 3
32.Os
33.Sh NAME
34.Nm inet_net_ntop ,
35.Nm inet_net_pton
36.Nd Internet network number manipulation routines
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In sys/socket.h
41.In netinet/in.h
42.In arpa/inet.h
43.Ft char *
44.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
45.Ft int
46.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
47.Sh DESCRIPTION
48The
49.Fn inet_net_ntop
50function converts an Internet network number from network format (usually a
51.Vt struct in_addr
52or some other binary form, in network byte order) to CIDR presentation format
53(suitable for external display purposes).
54The
55.Fa bits
56argument is the number of bits in
57.Fa src
58that are the network number.
59It returns
60.Dv NULL
61if an error occurs (in which case
62.Va errno
63will have been set), or it returns a pointer to the destination string.
64.Pp
65The
66.Fn inet_net_pton
67function converts a presentation format Internet network number (that is,
68printable form as held in a character string) to network format (usually a
69.Vt struct in_addr
70or some other internal binary representation, in network byte order).
71It returns the number of bits (either computed based on the class, or
72specified with /CIDR), or \-1 if a failure occurred
73(in which case
74.Va errno
75will have been set).
76.Pp
77The currently supported values for
78.Fa af
79are
80.Dv AF_INET
81and
82.Dv AF_INET6 .
83The
84.Fa size
85argument is the size of the result buffer
86.Fa dst .
87.Sh NETWORK NUMBERS (IP VERSION 4)
88Internet network numbers may be specified in one of the following forms:
89.Bd -literal -offset indent
90a.b.c.d/bits
91a.b.c.d
92a.b.c
93a.b
94a
95.Ed
96.Pp
97When four parts are specified, each is interpreted
98as a byte of data and assigned, from left to right,
99to the four bytes of an Internet network number.
100Note that when an Internet network number is viewed as a 32-bit
101integer quantity on a system that uses little-endian
102byte order (such as the
103.Tn Intel 386 , 486 ,
104and
105.Tn Pentium
106processors) the bytes referred to above appear as
107.Dq Li d.c.b.a .
108That is, little-endian bytes are ordered from right to left.
109.Pp
110When a three part number is specified, the last
111part is interpreted as a 16-bit quantity and placed
112in the rightmost two bytes of the Internet network number.
113This makes the three part number format convenient
114for specifying Class B network numbers as
115.Dq Li 128.net.host .
116.Pp
117When a two part number is supplied, the last part
118is interpreted as a 24-bit quantity and placed in
119the rightmost three bytes of the Internet network number.
120This makes the two part number format convenient
121for specifying Class A network numbers as
122.Dq Li net.host .
123.Pp
124When only one part is given, the value is stored
125directly in the Internet network number without any byte
126rearrangement.
127.Pp
128All numbers supplied as
129.Dq parts
130in a
131.Ql \&.
132notation
133may be decimal, octal, or hexadecimal, as specified
134in the C language (i.e., a leading 0x or 0X implies
135hexadecimal; otherwise, a leading 0 implies octal;
136otherwise, the number is interpreted as decimal).
137.\"
138.\" .Sh NETWORK NUMBERS (IP VERSION 6)
139.\" XXX - document this!
140.\"
141.Sh ERRORS
142The
143.Fn inet_net_ntop
144and
145.Fn inet_net_pton
146functions may fail with
147.Bl -tag -width Er
148.It Bq Er EAFNOSUPPORT
149The value of
150.Fa af
151was not
152.Dv AF_INET
153or
154.Dv AF_INET6 .
155.It Bq Er EMSGSIZE
156The conversion of
157.Fa src
158overflows
159.Fa size
160of
161.Fa dst .
162.El
163.Pp
164The
165.Fn inet_net_ntop
166function may fail with
167.Bl -tag -width Er
168.It Bq Er EINVAL
169The
170.Fa bits
171argument contains an invalid number of bits
172for the requested address family.
173.El
174.Pp
175The
176.Fn inet_net_pton
177function may fail with
178.Bl -tag -width Er
179.It Bq Er ENOENT
180The
181.Fa src
182was not a valid Internet network number.
183.El
184.Sh SEE ALSO
185.Xr byteorder 3 ,
186.Xr inet 3 ,
187.Xr networks 5
188.Sh HISTORY
189The
190.Fn inet_net_ntop
191and
192.Fn inet_net_pton
193functions appeared in BIND 4.9.4 and thence
194.Nx 1.3 .
195Support for
196.Dv AF_INET6
197appeared in
198.Nx 1.6 .
199