xref: /freebsd/lib/libc/net/inet.3 (revision 9231c421)
158f0484fSRodney W. Grimes.\" Copyright (c) 1983, 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
2812bd931dSMike Karels.Dd November 9, 2021
2958f0484fSRodney W. Grimes.Dt INET 3
30a307d598SRuslan Ermilov.Os
3158f0484fSRodney W. Grimes.Sh NAME
3258f0484fSRodney W. Grimes.Nm inet_aton ,
3358f0484fSRodney W. Grimes.Nm inet_addr ,
3458f0484fSRodney W. Grimes.Nm inet_network ,
3558f0484fSRodney W. Grimes.Nm inet_ntoa ,
360c927cddSXin LI.Nm inet_ntoa_r ,
3789482e46SYoshinobu Inoue.Nm inet_ntop ,
3889482e46SYoshinobu Inoue.Nm inet_pton ,
3958f0484fSRodney W. Grimes.Nm inet_makeaddr ,
4058f0484fSRodney W. Grimes.Nm inet_lnaof ,
4158f0484fSRodney W. Grimes.Nm inet_netof
4258f0484fSRodney W. Grimes.Nd Internet address manipulation routines
4325bb73e0SAlexey Zelkin.Sh LIBRARY
4425bb73e0SAlexey Zelkin.Lb libc
4558f0484fSRodney W. Grimes.Sh SYNOPSIS
4632eef9aeSRuslan Ermilov.In sys/types.h
4732eef9aeSRuslan Ermilov.In sys/socket.h
4832eef9aeSRuslan Ermilov.In netinet/in.h
4932eef9aeSRuslan Ermilov.In arpa/inet.h
5058f0484fSRodney W. Grimes.Ft int
51433671e3SBruce Evans.Fn inet_aton "const char *cp" "struct in_addr *pin"
522da24fa6SHajimu UMEMOTO.Ft in_addr_t
53433671e3SBruce Evans.Fn inet_addr "const char *cp"
542da24fa6SHajimu UMEMOTO.Ft in_addr_t
55433671e3SBruce Evans.Fn inet_network "const char *cp"
5658f0484fSRodney W. Grimes.Ft char *
5758f0484fSRodney W. Grimes.Fn inet_ntoa "struct in_addr in"
580c927cddSXin LI.Ft char *
590c927cddSXin LI.Fo inet_ntoa_r
600c927cddSXin LI.Fa "struct in_addr in"
610c927cddSXin LI.Fa "char *buf"
620bb263dfSXin LI.Fa "socklen_t size"
630c927cddSXin LI.Fc
6489482e46SYoshinobu Inoue.Ft const char *
657cfcd027SRobert Drehmel.Fo inet_ntop
667cfcd027SRobert Drehmel.Fa "int af"
677cfcd027SRobert Drehmel.Fa "const void * restrict src"
687cfcd027SRobert Drehmel.Fa "char * restrict dst"
697cfcd027SRobert Drehmel.Fa "socklen_t size"
707cfcd027SRobert Drehmel.Fc
7189482e46SYoshinobu Inoue.Ft int
72b7dbaf7bSRobert Drehmel.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst"
7358f0484fSRodney W. Grimes.Ft struct in_addr
742da24fa6SHajimu UMEMOTO.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna"
752da24fa6SHajimu UMEMOTO.Ft in_addr_t
7658f0484fSRodney W. Grimes.Fn inet_lnaof "struct in_addr in"
772da24fa6SHajimu UMEMOTO.Ft in_addr_t
7858f0484fSRodney W. Grimes.Fn inet_netof "struct in_addr in"
7958f0484fSRodney W. Grimes.Sh DESCRIPTION
8058f0484fSRodney W. GrimesThe routines
8158f0484fSRodney W. Grimes.Fn inet_aton ,
8258f0484fSRodney W. Grimes.Fn inet_addr
8358f0484fSRodney W. Grimesand
8458f0484fSRodney W. Grimes.Fn inet_network
8558f0484fSRodney W. Grimesinterpret character strings representing
8658f0484fSRodney W. Grimesnumbers expressed in the Internet standard
8742635956SRuslan Ermilov.Ql .\&
8858f0484fSRodney W. Grimesnotation.
8989482e46SYoshinobu Inoue.Pp
9089482e46SYoshinobu InoueThe
9189482e46SYoshinobu Inoue.Fn inet_pton
9289482e46SYoshinobu Inouefunction converts a presentation format address (that is, printable form
9389482e46SYoshinobu Inoueas held in a character string) to network format (usually a
9489482e46SYoshinobu Inoue.Ft struct in_addr
9589482e46SYoshinobu Inoueor some other internal binary representation, in network byte order).
9689482e46SYoshinobu InoueIt returns 1 if the address was valid for the specified address family, or
970227791bSRuslan Ermilov0 if the address was not parseable in the specified address family, or -1
9889482e46SYoshinobu Inoueif some system error occurred (in which case
9989482e46SYoshinobu Inoue.Va errno
10089482e46SYoshinobu Inouewill have been set).
10189482e46SYoshinobu InoueThis function is presently valid for
10289482e46SYoshinobu Inoue.Dv AF_INET
10389482e46SYoshinobu Inoueand
10489482e46SYoshinobu Inoue.Dv AF_INET6 .
10589482e46SYoshinobu Inoue.Pp
10658f0484fSRodney W. GrimesThe
10758f0484fSRodney W. Grimes.Fn inet_aton
10858f0484fSRodney W. Grimesroutine interprets the specified character string as an Internet address,
10958f0484fSRodney W. Grimesplacing the address into the structure provided.
11058f0484fSRodney W. GrimesIt returns 1 if the string was successfully interpreted,
11158f0484fSRodney W. Grimesor 0 if the string is invalid.
11258f0484fSRodney W. GrimesThe
11358f0484fSRodney W. Grimes.Fn inet_addr
11458f0484fSRodney W. Grimesand
11558f0484fSRodney W. Grimes.Fn inet_network
11658f0484fSRodney W. Grimesfunctions return numbers suitable for use
11758f0484fSRodney W. Grimesas Internet addresses and Internet network
11858f0484fSRodney W. Grimesnumbers, respectively.
11989482e46SYoshinobu Inoue.Pp
12089482e46SYoshinobu InoueThe function
12189482e46SYoshinobu Inoue.Fn inet_ntop
1223c6ba09bSBruce M Simpsonconverts an address
1233c6ba09bSBruce M Simpson.Fa *src
1243c6ba09bSBruce M Simpsonfrom network format
1253c6ba09bSBruce M Simpson(usually a
12689482e46SYoshinobu Inoue.Ft struct in_addr
12789482e46SYoshinobu Inoueor some other binary form, in network byte order) to presentation format
12889482e46SYoshinobu Inoue(suitable for external display purposes).
1293c6ba09bSBruce M SimpsonThe
1303c6ba09bSBruce M Simpson.Fa size
1313c6ba09bSBruce M Simpsonargument specifies the size, in bytes, of the buffer
1323c6ba09bSBruce M Simpson.Fa *dst .
13302023242SMax Laier.Dv INET_ADDRSTRLEN
13402023242SMax Laierand
13502023242SMax Laier.Dv INET6_ADDRSTRLEN
13602023242SMax Laierdefine the maximum size required to convert an address of the respective
13702023242SMax Laiertype.
13889482e46SYoshinobu InoueIt returns NULL if a system error occurs (in which case,
13989482e46SYoshinobu Inoue.Va errno
14089482e46SYoshinobu Inouewill have been set), or it returns a pointer to the destination string.
14189482e46SYoshinobu InoueThis function is presently valid for
14289482e46SYoshinobu Inoue.Dv AF_INET
14389482e46SYoshinobu Inoueand
14489482e46SYoshinobu Inoue.Dv AF_INET6 .
14589482e46SYoshinobu Inoue.Pp
14658f0484fSRodney W. GrimesThe routine
14758f0484fSRodney W. Grimes.Fn inet_ntoa
14858f0484fSRodney W. Grimestakes an Internet address and returns an
14958f0484fSRodney W. Grimes.Tn ASCII
15058f0484fSRodney W. Grimesstring representing the address in
15142635956SRuslan Ermilov.Ql .\&
1521a0a9345SRuslan Ermilovnotation.
1531a0a9345SRuslan ErmilovThe routine
1540c927cddSXin LI.Fn inet_ntoa_r
1550c927cddSXin LIis the reentrant version of
1560c927cddSXin LI.Fn inet_ntoa .
15712bd931dSMike KarelsThe deprecated routine
15858f0484fSRodney W. Grimes.Fn inet_makeaddr
15958f0484fSRodney W. Grimestakes an Internet network number and a local
16012bd931dSMike Karelshost address on that network, and constructs an Internet address
1611a0a9345SRuslan Ermilovfrom it.
16212bd931dSMike KarelsIt should only be assumed to work for historical class A/B/C networks.
16312bd931dSMike KarelsThe deprecated routines
16458f0484fSRodney W. Grimes.Fn inet_netof
16558f0484fSRodney W. Grimesand
16658f0484fSRodney W. Grimes.Fn inet_lnaof
16758f0484fSRodney W. Grimesbreak apart Internet host addresses, returning
16812bd931dSMike Karelsthe network number and local host address part,
16912bd931dSMike Karelsrespectively,
17012bd931dSMike Karelsassuming the historical class A/B/C network masks.
17158f0484fSRodney W. Grimes.Pp
17258f0484fSRodney W. GrimesAll Internet addresses are returned in network
17358f0484fSRodney W. Grimesorder (bytes ordered from left to right).
17458f0484fSRodney W. GrimesAll network numbers and local address parts are
1757125977bSRuslan Ermilovreturned as machine byte order integer values.
1769231c421SMike Karels.Sh INTERNET ADDRESSES (IP VERSION 4)
1779231c421SMike KarelsThe
1789231c421SMike Karels.Fn inet_aton
1799231c421SMike Karelsand
1809231c421SMike Karels.Fn inet_addr
1819231c421SMike Karelsfunctions accept IPv4
1829231c421SMike Karelsvalues specified using the
18342635956SRuslan Ermilov.Ql .\&
1849231c421SMike Karelsnotation in one
18558f0484fSRodney W. Grimesof the following forms:
18658f0484fSRodney W. Grimes.Bd -literal -offset indent
18758f0484fSRodney W. Grimesa.b.c.d
18858f0484fSRodney W. Grimesa.b.c
18958f0484fSRodney W. Grimesa.b
19058f0484fSRodney W. Grimesa
19158f0484fSRodney W. Grimes.Ed
19258f0484fSRodney W. Grimes.Pp
19358f0484fSRodney W. GrimesWhen four parts are specified, each is interpreted
19458f0484fSRodney W. Grimesas a byte of data and assigned, from left to right,
1951a0a9345SRuslan Ermilovto the four bytes of an Internet address.
19658f0484fSRodney W. Grimes.Pp
19758f0484fSRodney W. GrimesWhen a three part address is specified, the last
19858f0484fSRodney W. Grimespart is interpreted as a 16-bit quantity and placed
19912bd931dSMike Karelsin the least significant two bytes of the network address.
20058f0484fSRodney W. Grimes.Pp
20158f0484fSRodney W. GrimesWhen a two part address is supplied, the last part
20258f0484fSRodney W. Grimesis interpreted as a 24-bit quantity and placed in
20312bd931dSMike Karelsthe least significant three bytes of the network address.
20458f0484fSRodney W. Grimes.Pp
20558f0484fSRodney W. GrimesWhen only one part is given, the value is stored
20658f0484fSRodney W. Grimesdirectly in the network address without any byte
20758f0484fSRodney W. Grimesrearrangement.
20858f0484fSRodney W. Grimes.Pp
20958f0484fSRodney W. GrimesAll numbers supplied as
21058f0484fSRodney W. Grimes.Dq parts
21158f0484fSRodney W. Grimesin a
21242635956SRuslan Ermilov.Ql .\&
21358f0484fSRodney W. Grimesnotation
21458f0484fSRodney W. Grimesmay be decimal, octal, or hexadecimal, as specified
21558f0484fSRodney W. Grimesin the C language (i.e., a leading 0x or 0X implies
21658f0484fSRodney W. Grimeshexadecimal; otherwise, a leading 0 implies octal;
21758f0484fSRodney W. Grimesotherwise, the number is interpreted as decimal).
2189231c421SMike Karels.Pp
2199231c421SMike KarelsNote that
2209231c421SMike Karels.Fn inet_pton
2219231c421SMike Karelsdoes not accept 1-, 2-, or 3-part dotted addresses; all four parts
2229231c421SMike Karelsmust be specified and are interpreted only as decimal values.
2239231c421SMike KarelsThis is a narrower input set than that accepted by
2249231c421SMike Karels.Fn inet_aton .
22558f0484fSRodney W. Grimes.Sh DIAGNOSTICS
22658f0484fSRodney W. GrimesThe constant
22758f0484fSRodney W. Grimes.Dv INADDR_NONE
22858f0484fSRodney W. Grimesis returned by
22958f0484fSRodney W. Grimes.Fn inet_addr
23058f0484fSRodney W. Grimesand
23158f0484fSRodney W. Grimes.Fn inet_network
23258f0484fSRodney W. Grimesfor malformed requests.
2333c6ba09bSBruce M Simpson.Sh ERRORS
2343c6ba09bSBruce M SimpsonThe
2353c6ba09bSBruce M Simpson.Fn inet_ntop
2363c6ba09bSBruce M Simpsoncall fails if:
2373c6ba09bSBruce M Simpson.Bl -tag -width Er
2383c6ba09bSBruce M Simpson.It Bq Er ENOSPC
2393c6ba09bSBruce M Simpson.Fa size
2403c6ba09bSBruce M Simpsonwas not large enough to store the presentation form of the address.
2413c6ba09bSBruce M Simpson.It Bq Er EAFNOSUPPORT
2423c6ba09bSBruce M Simpson.Fa *src
24333992dc0SRuslan Ermilovwas not an
24433992dc0SRuslan Ermilov.Dv AF_INET
24533992dc0SRuslan Ermilovor
24633992dc0SRuslan Ermilov.Dv AF_INET6
24733992dc0SRuslan Ermilovfamily address.
2483c6ba09bSBruce M Simpson.El
24958f0484fSRodney W. Grimes.Sh SEE ALSO
25081a3cb97SRuslan Ermilov.Xr byteorder 3 ,
25167228c46SBruce M Simpson.Xr getaddrinfo 3 ,
25258f0484fSRodney W. Grimes.Xr gethostbyname 3 ,
25367228c46SBruce M Simpson.Xr getnameinfo 3 ,
25458f0484fSRodney W. Grimes.Xr getnetent 3 ,
25581a3cb97SRuslan Ermilov.Xr inet_net 3 ,
25658f0484fSRodney W. Grimes.Xr hosts 5 ,
25713608f66SWolfram Schneider.Xr networks 5
25889482e46SYoshinobu Inoue.Rs
25989482e46SYoshinobu Inoue.%R RFC
26089482e46SYoshinobu Inoue.%N 2373
26189482e46SYoshinobu Inoue.%D July 1998
26289482e46SYoshinobu Inoue.%T "IP Version 6 Addressing Architecture"
26389482e46SYoshinobu Inoue.Re
26489482e46SYoshinobu Inoue.Sh STANDARDS
26589482e46SYoshinobu InoueThe
266563f6bdeSRuslan Ermilov.Fn inet_ntop
26789482e46SYoshinobu Inoueand
268563f6bdeSRuslan Ermilov.Fn inet_pton
26989482e46SYoshinobu Inouefunctions conform to
27033dfeb89SRuslan Ermilov.St -xns5.2 .
27158f0484fSRodney W. Grimes.Sh HISTORY
27258f0484fSRodney W. GrimesThese
27358f0484fSRodney W. Grimesfunctions appeared in
27458f0484fSRodney W. Grimes.Bx 4.2 .
27558f0484fSRodney W. Grimes.Sh BUGS
27658f0484fSRodney W. GrimesThe value
27758f0484fSRodney W. Grimes.Dv INADDR_NONE
27858f0484fSRodney W. Grimes(0xffffffff) is a valid broadcast address, but
27958f0484fSRodney W. Grimes.Fn inet_addr
28058f0484fSRodney W. Grimescannot return that value without indicating failure.
28158f0484fSRodney W. GrimesThe newer
28258f0484fSRodney W. Grimes.Fn inet_aton
28358f0484fSRodney W. Grimesfunction does not share this problem.
28458f0484fSRodney W. GrimesThe problem of host byte ordering versus network byte ordering is
28558f0484fSRodney W. Grimesconfusing.
28658f0484fSRodney W. GrimesThe string returned by
28758f0484fSRodney W. Grimes.Fn inet_ntoa
28858f0484fSRodney W. Grimesresides in a static memory area.
28958f0484fSRodney W. Grimes.Pp
2900f0eddbbSRuslan ErmilovThe
2910f0eddbbSRuslan Ermilov.Fn inet_addr
2920f0eddbbSRuslan Ermilovfunction should return a
29358f0484fSRodney W. Grimes.Fa struct in_addr .
294