xref: /openbsd/lib/libc/net/inet_net_ntop.3 (revision d32639f6)
1*d32639f6Sjmc.\"	$OpenBSD: inet_net_ntop.3,v 1.4 2022/09/11 06:38:10 jmc Exp $
24d9461faSderaadt.\"	$NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $
34d9461faSderaadt.\"
44d9461faSderaadt.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
54d9461faSderaadt.\" All rights reserved.
64d9461faSderaadt.\"
74d9461faSderaadt.\" This code is derived from software contributed to The NetBSD Foundation
84d9461faSderaadt.\" by Luke Mewburn.
94d9461faSderaadt.\"
104d9461faSderaadt.\" Redistribution and use in source and binary forms, with or without
114d9461faSderaadt.\" modification, are permitted provided that the following conditions
124d9461faSderaadt.\" are met:
134d9461faSderaadt.\" 1. Redistributions of source code must retain the above copyright
144d9461faSderaadt.\"    notice, this list of conditions and the following disclaimer.
154d9461faSderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
164d9461faSderaadt.\"    notice, this list of conditions and the following disclaimer in the
174d9461faSderaadt.\"    documentation and/or other materials provided with the distribution.
184d9461faSderaadt.\"
194d9461faSderaadt.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204d9461faSderaadt.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214d9461faSderaadt.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224d9461faSderaadt.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
234d9461faSderaadt.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244d9461faSderaadt.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254d9461faSderaadt.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264d9461faSderaadt.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274d9461faSderaadt.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284d9461faSderaadt.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294d9461faSderaadt.\" POSSIBILITY OF SUCH DAMAGE.
304d9461faSderaadt.\"
31*d32639f6Sjmc.Dd $Mdocdate: September 11 2022 $
324d9461faSderaadt.Dt INET_NET_NTOP 3
334d9461faSderaadt.Os
344d9461faSderaadt.Sh NAME
354d9461faSderaadt.Nm inet_net_ntop ,
364d9461faSderaadt.Nm inet_net_pton
374d9461faSderaadt.Nd Internet network number manipulation routines
384d9461faSderaadt.Sh SYNOPSIS
394d9461faSderaadt.In sys/socket.h
404d9461faSderaadt.In arpa/inet.h
414d9461faSderaadt.Ft char *
424d9461faSderaadt.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
434d9461faSderaadt.Ft int
444d9461faSderaadt.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
454d9461faSderaadt.Sh DESCRIPTION
464d9461faSderaadtThe
474d9461faSderaadt.Fn inet_net_ntop
484d9461faSderaadtfunction converts an Internet network number from network format (usually a
49*d32639f6Sjmc.Vt struct in_addr
504d9461faSderaadtor some other binary form, in network byte order) to CIDR presentation format
514d9461faSderaadt(suitable for external display purposes).
524d9461faSderaadt.Fa bits
534d9461faSderaadtis the number of bits in
544d9461faSderaadt.Fa src
554d9461faSderaadtthat are the network number.
564d9461faSderaadtIt returns
574d9461faSderaadt.Dv NULL
584d9461faSderaadtif a system error occurs (in which case,
594d9461faSderaadt.Va errno
604d9461faSderaadtwill have been set), or it returns a pointer to the destination string.
614d9461faSderaadt.Pp
624d9461faSderaadtThe
634d9461faSderaadt.Fn inet_net_pton
644d9461faSderaadtfunction converts a presentation format Internet network number (that is,
654d9461faSderaadtprintable form as held in a character string) to network format (usually a
66*d32639f6Sjmc.Vt struct in_addr
674d9461faSderaadtor some other internal binary representation, in network byte order).
684d9461faSderaadtIt returns the number of bits (either computed based on the class, or
694d9461faSderaadtspecified with /CIDR), or \-1 if a failure occurred
704d9461faSderaadt(in which case
714d9461faSderaadt.Va errno
724d9461faSderaadtwill have been set.
734d9461faSderaadtIt will be set to
744d9461faSderaadt.Er ENOENT
754d9461faSderaadtif the Internet network number was not valid).
764d9461faSderaadt.Pp
774d9461faSderaadtCaution:
784d9461faSderaadtThe
794d9461faSderaadt.Fa dst
804d9461faSderaadtfield should be zeroed before calling
814d9461faSderaadt.Fn inet_net_pton
824d9461faSderaadtas the function will only fill the number of bytes necessary to
834d9461faSderaadtencode the network number in network byte order.
844d9461faSderaadt.Pp
854d9461faSderaadtThe only values for
864d9461faSderaadt.Fa af
874d9461faSderaadtcurrently supported are
884d9461faSderaadt.Dv AF_INET
894d9461faSderaadtand
904d9461faSderaadt.Dv AF_INET6 .
914d9461faSderaadt.Fa size
924d9461faSderaadtis the size of the result buffer
934d9461faSderaadt.Fa dst .
944d9461faSderaadt.Sh NETWORK NUMBERS (IP VERSION 4)
954d9461faSderaadtThe external representation of Internet network numbers may be specified in
964d9461faSderaadtone of the following forms:
974d9461faSderaadt.Bd -literal -offset indent
984d9461faSderaadta
994d9461faSderaadta.b
1004d9461faSderaadta.b.c
1014d9461faSderaadta.b.c.d
1024d9461faSderaadt.Ed
1034d9461faSderaadt.Pp
1044d9461faSderaadtAny of the above four forms may have
1054d9461faSderaadt.Dq Li /bits
1064d9461faSderaadtappended where
1074d9461faSderaadt.Dq Li bits
1084d9461faSderaadtis in the range
1094d9461faSderaadt.Li 0-32
1104d9461faSderaadtand is used to explicitly specify the number of bits in the network address.
1114d9461faSderaadtWhen
1124d9461faSderaadt.Dq Li /bits
11341ce3b17Snaddyis not specified, the number of bits in the network address is calculated
1144d9461faSderaadtas the larger of the number of bits in the class to which the address
1154d9461faSderaadtbelongs and the number of bits provided rounded up modulo 8.
1164d9461faSderaadtExamples:
1174d9461faSderaadt.Pp
1184d9461faSderaadt.Bl -tag -width 10.1.2.3/24 -offset indent -compact
1194d9461faSderaadt.It Li 10
1204d9461faSderaadtan 8-bit network number (class A), value
1214d9461faSderaadt.Li 10.0.0.0 .
1224d9461faSderaadt.It Li 192
1234d9461faSderaadta 24-bit network number (class C), value
1244d9461faSderaadt.Li 192.0.0.0 .
1254d9461faSderaadt.It Li 10.10
1264d9461faSderaadta 16-bit network number, value
1274d9461faSderaadt.Li 10.10.0.0 .
1284d9461faSderaadt.It Li 10.1.2
1294d9461faSderaadta 24-bit network number, value
1304d9461faSderaadt.Li 10.1.2.0 .
1314d9461faSderaadt.It Li 10.1.2.3
1324d9461faSderaadta 32-bit network number, value
1334d9461faSderaadt.Li 10.1.2.3 .
1344d9461faSderaadt.It Li 10.1.2.3/24
1354d9461faSderaadta 24-bit network number (explicit), value
1364d9461faSderaadt.Li 10.1.2.3 .
1374d9461faSderaadt.El
1384d9461faSderaadt.Pp
1394d9461faSderaadtNote that when the number of bits is specified using
1404d9461faSderaadt.Dq Li /bits
1414d9461faSderaadtnotation, the value of the address still includes all bits supplied
1424d9461faSderaadtin the external representation, even those bits which are the host
1434d9461faSderaadtpart of an Internet address.
1444d9461faSderaadtAlso, unlike
1454d9461faSderaadt.Xr inet_pton 3
1464d9461faSderaadtwhere the external representation is assumed to be a host address, the
1474d9461faSderaadtexternal representation for
1484d9461faSderaadt.Fn inet_net_pton
1494d9461faSderaadtis assumed to be a network address.
1504d9461faSderaadtThus
1514d9461faSderaadt.Dq Li 10.1
1524d9461faSderaadtis assumed to be
1534d9461faSderaadt.Dq Li 10.1.0.0
1544d9461faSderaadtnot
1554d9461faSderaadt.Dq Li 10.0.0.1
1564d9461faSderaadt.Pp
1574d9461faSderaadtAll numbers supplied as
1584d9461faSderaadt.Dq parts
1594d9461faSderaadtin a
1604d9461faSderaadt.Ql \&.
1614d9461faSderaadtnotation
1624d9461faSderaadtmay be decimal, octal, or hexadecimal, as specified
1634d9461faSderaadtin the C language (i.e., a leading 0x or 0X implies
1644d9461faSderaadthexadecimal; otherwise, a leading 0 implies octal;
1654d9461faSderaadtotherwise, the number is interpreted as decimal).
1664d9461faSderaadt.Sh NETWORK NUMBERS (IP VERSION 6)
1674d9461faSderaadtSee
1684d9461faSderaadt.Xr inet_pton 3
1694d9461faSderaadtfor valid external representations of IP version 6 addresses.
1704d9461faSderaadtA valid external representation may have
1714d9461faSderaadt.Dq Li /bits
1724d9461faSderaadtappended where
1734d9461faSderaadt.Dq Li bits
1744d9461faSderaadtis in the range
1754d9461faSderaadt.Li 0-128
1764d9461faSderaadtand is used to explicitly specify the number of bits in the network address.
1774d9461faSderaadtWhen
1784d9461faSderaadt.Dq Li /bits
17941ce3b17Snaddyis not specified, 128 is used.
1804d9461faSderaadtNote that when the number of bits is specified using
1814d9461faSderaadt.Dq Li /bits
1824d9461faSderaadtnotation, the value of the address still includes all bits supplied
1834d9461faSderaadtin the external representation, even those bits which are the host
1844d9461faSderaadtpart of an Internet address.
1854d9461faSderaadt.Sh SEE ALSO
1864d9461faSderaadt.Xr htonl 3 ,
1874d9461faSderaadt.Xr inet_pton 3 ,
1884d9461faSderaadt.Xr inet 4 ,
1894d9461faSderaadt.Xr hosts 5
1904d9461faSderaadt.Sh HISTORY
1914d9461faSderaadtThe
1924d9461faSderaadt.Nm inet_net_ntop
1934d9461faSderaadtand
1944d9461faSderaadt.Nm inet_net_pton
1954d9461faSderaadtfunctions first appeared in BIND 4.9.4.
196