1.\" $OpenBSD: inet_net_ntop.3,v 1.1 2019/08/30 18:33:17 deraadt 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: August 30 2019 $ 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/types.h 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.Li struct in_addr 52or some other binary form, in network byte order) to CIDR presentation format 53(suitable for external display purposes). 54.Fa bits 55is the number of bits in 56.Fa src 57that are the network number. 58It returns 59.Dv NULL 60if a system error occurs (in which case, 61.Va errno 62will have been set), or it returns a pointer to the destination string. 63.Pp 64The 65.Fn inet_net_pton 66function converts a presentation format Internet network number (that is, 67printable form as held in a character string) to network format (usually a 68.Li struct in_addr 69or some other internal binary representation, in network byte order). 70It returns the number of bits (either computed based on the class, or 71specified with /CIDR), or \-1 if a failure occurred 72(in which case 73.Va errno 74will have been set. 75It will be set to 76.Er ENOENT 77if the Internet network number was not valid). 78.Pp 79Caution: 80The 81.Fa dst 82field should be zeroed before calling 83.Fn inet_net_pton 84as the function will only fill the number of bytes necessary to 85encode the network number in network byte order. 86.Pp 87The only values for 88.Fa af 89currently supported are 90.Dv AF_INET 91and 92.Dv AF_INET6 . 93.Fa size 94is the size of the result buffer 95.Fa dst . 96.Sh NETWORK NUMBERS (IP VERSION 4) 97The external representation of Internet network numbers may be specified in 98one of the following forms: 99.Bd -literal -offset indent 100a 101a.b 102a.b.c 103a.b.c.d 104.Ed 105.Pp 106Any of the above four forms may have 107.Dq Li /bits 108appended where 109.Dq Li bits 110is in the range 111.Li 0-32 112and is used to explicitly specify the number of bits in the network address. 113When 114.Dq Li /bits 115is not specified the number of bits in the network address is calculated 116as the larger of the number of bits in the class to which the address 117belongs and the number of bits provided rounded up modulo 8. 118Examples: 119.Pp 120.Bl -tag -width 10.1.2.3/24 -offset indent -compact 121.It Li 10 122an 8-bit network number (class A), value 123.Li 10.0.0.0 . 124.It Li 192 125a 24-bit network number (class C), value 126.Li 192.0.0.0 . 127.It Li 10.10 128a 16-bit network number, value 129.Li 10.10.0.0 . 130.It Li 10.1.2 131a 24-bit network number, value 132.Li 10.1.2.0 . 133.It Li 10.1.2.3 134a 32-bit network number, value 135.Li 10.1.2.3 . 136.It Li 10.1.2.3/24 137a 24-bit network number (explicit), value 138.Li 10.1.2.3 . 139.El 140.Pp 141Note that when the number of bits is specified using 142.Dq Li /bits 143notation, the value of the address still includes all bits supplied 144in the external representation, even those bits which are the host 145part of an Internet address. 146Also, unlike 147.Xr inet_pton 3 148where the external representation is assumed to be a host address, the 149external representation for 150.Fn inet_net_pton 151is assumed to be a network address. 152Thus 153.Dq Li 10.1 154is assumed to be 155.Dq Li 10.1.0.0 156not 157.Dq Li 10.0.0.1 158.Pp 159All numbers supplied as 160.Dq parts 161in a 162.Ql \&. 163notation 164may be decimal, octal, or hexadecimal, as specified 165in the C language (i.e., a leading 0x or 0X implies 166hexadecimal; otherwise, a leading 0 implies octal; 167otherwise, the number is interpreted as decimal). 168.Sh NETWORK NUMBERS (IP VERSION 6) 169See 170.Xr inet_pton 3 171for valid external representations of IP version 6 addresses. 172A valid external representation may have 173.Dq Li /bits 174appended where 175.Dq Li bits 176is in the range 177.Li 0-128 178and is used to explicitly specify the number of bits in the network address. 179When 180.Dq Li /bits 181is not specified 128 is used. 182Note that when the number of bits is specified using 183.Dq Li /bits 184notation, the value of the address still includes all bits supplied 185in the external representation, even those bits which are the host 186part of an Internet address. 187.Sh SEE ALSO 188.Xr htonl 3 , 189.Xr inet_pton 3 , 190.Xr inet 4 , 191.Xr hosts 5 192.Sh HISTORY 193The 194.Nm inet_net_ntop 195and 196.Nm inet_net_pton 197functions first appeared in BIND 4.9.4. 198