xref: /freebsd/lib/libc/net/inet.3 (revision 67228c46217cea62a8c5ec26cd7093ea2f8b4cc0)
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.
1258f0484fSRodney W. Grimes.\" 4. 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.\"
28538e8768SGarrett Wollman.\"     From: @(#)inet.3	8.1 (Berkeley) 6/4/93
297f3dea24SPeter Wemm.\" $FreeBSD$
3058f0484fSRodney W. Grimes.\"
3167228c46SBruce M Simpson.Dd February 28, 2007
3258f0484fSRodney W. Grimes.Dt INET 3
33a307d598SRuslan Ermilov.Os
3458f0484fSRodney W. Grimes.Sh NAME
3558f0484fSRodney W. Grimes.Nm inet_aton ,
3658f0484fSRodney W. Grimes.Nm inet_addr ,
3758f0484fSRodney W. Grimes.Nm inet_network ,
3858f0484fSRodney W. Grimes.Nm inet_ntoa ,
3989482e46SYoshinobu Inoue.Nm inet_ntop ,
4089482e46SYoshinobu Inoue.Nm inet_pton ,
4158f0484fSRodney W. Grimes.Nm inet_makeaddr ,
4258f0484fSRodney W. Grimes.Nm inet_lnaof ,
4358f0484fSRodney W. Grimes.Nm inet_netof
4458f0484fSRodney W. Grimes.Nd Internet address manipulation routines
4525bb73e0SAlexey Zelkin.Sh LIBRARY
4625bb73e0SAlexey Zelkin.Lb libc
4758f0484fSRodney W. Grimes.Sh SYNOPSIS
4832eef9aeSRuslan Ermilov.In sys/types.h
4932eef9aeSRuslan Ermilov.In sys/socket.h
5032eef9aeSRuslan Ermilov.In netinet/in.h
5132eef9aeSRuslan Ermilov.In arpa/inet.h
5258f0484fSRodney W. Grimes.Ft int
53433671e3SBruce Evans.Fn inet_aton "const char *cp" "struct in_addr *pin"
542da24fa6SHajimu UMEMOTO.Ft in_addr_t
55433671e3SBruce Evans.Fn inet_addr "const char *cp"
562da24fa6SHajimu UMEMOTO.Ft in_addr_t
57433671e3SBruce Evans.Fn inet_network "const char *cp"
5858f0484fSRodney W. Grimes.Ft char *
5958f0484fSRodney W. Grimes.Fn inet_ntoa "struct in_addr in"
6089482e46SYoshinobu Inoue.Ft const char *
617cfcd027SRobert Drehmel.Fo inet_ntop
627cfcd027SRobert Drehmel.Fa "int af"
637cfcd027SRobert Drehmel.Fa "const void * restrict src"
647cfcd027SRobert Drehmel.Fa "char * restrict dst"
657cfcd027SRobert Drehmel.Fa "socklen_t size"
667cfcd027SRobert Drehmel.Fc
6789482e46SYoshinobu Inoue.Ft int
68b7dbaf7bSRobert Drehmel.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst"
6958f0484fSRodney W. Grimes.Ft struct in_addr
702da24fa6SHajimu UMEMOTO.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna"
712da24fa6SHajimu UMEMOTO.Ft in_addr_t
7258f0484fSRodney W. Grimes.Fn inet_lnaof "struct in_addr in"
732da24fa6SHajimu UMEMOTO.Ft in_addr_t
7458f0484fSRodney W. Grimes.Fn inet_netof "struct in_addr in"
7558f0484fSRodney W. Grimes.Sh DESCRIPTION
7658f0484fSRodney W. GrimesThe routines
7758f0484fSRodney W. Grimes.Fn inet_aton ,
7858f0484fSRodney W. Grimes.Fn inet_addr
7958f0484fSRodney W. Grimesand
8058f0484fSRodney W. Grimes.Fn inet_network
8158f0484fSRodney W. Grimesinterpret character strings representing
8258f0484fSRodney W. Grimesnumbers expressed in the Internet standard
8342635956SRuslan Ermilov.Ql .\&
8458f0484fSRodney W. Grimesnotation.
8589482e46SYoshinobu Inoue.Pp
8689482e46SYoshinobu InoueThe
8789482e46SYoshinobu Inoue.Fn inet_pton
8889482e46SYoshinobu Inouefunction converts a presentation format address (that is, printable form
8989482e46SYoshinobu Inoueas held in a character string) to network format (usually a
9089482e46SYoshinobu Inoue.Ft struct in_addr
9189482e46SYoshinobu Inoueor some other internal binary representation, in network byte order).
9289482e46SYoshinobu InoueIt returns 1 if the address was valid for the specified address family, or
930227791bSRuslan Ermilov0 if the address was not parseable in the specified address family, or -1
9489482e46SYoshinobu Inoueif some system error occurred (in which case
9589482e46SYoshinobu Inoue.Va errno
9689482e46SYoshinobu Inouewill have been set).
9789482e46SYoshinobu InoueThis function is presently valid for
9889482e46SYoshinobu Inoue.Dv AF_INET
9989482e46SYoshinobu Inoueand
10089482e46SYoshinobu Inoue.Dv AF_INET6 .
10189482e46SYoshinobu Inoue.Pp
10258f0484fSRodney W. GrimesThe
10358f0484fSRodney W. Grimes.Fn inet_aton
10458f0484fSRodney W. Grimesroutine interprets the specified character string as an Internet address,
10558f0484fSRodney W. Grimesplacing the address into the structure provided.
10658f0484fSRodney W. GrimesIt returns 1 if the string was successfully interpreted,
10758f0484fSRodney W. Grimesor 0 if the string is invalid.
10858f0484fSRodney W. GrimesThe
10958f0484fSRodney W. Grimes.Fn inet_addr
11058f0484fSRodney W. Grimesand
11158f0484fSRodney W. Grimes.Fn inet_network
11258f0484fSRodney W. Grimesfunctions return numbers suitable for use
11358f0484fSRodney W. Grimesas Internet addresses and Internet network
11458f0484fSRodney W. Grimesnumbers, respectively.
11589482e46SYoshinobu Inoue.Pp
11689482e46SYoshinobu InoueThe function
11789482e46SYoshinobu Inoue.Fn inet_ntop
1183c6ba09bSBruce M Simpsonconverts an address
1193c6ba09bSBruce M Simpson.Fa *src
1203c6ba09bSBruce M Simpsonfrom network format
1213c6ba09bSBruce M Simpson(usually a
12289482e46SYoshinobu Inoue.Ft struct in_addr
12389482e46SYoshinobu Inoueor some other binary form, in network byte order) to presentation format
12489482e46SYoshinobu Inoue(suitable for external display purposes).
1253c6ba09bSBruce M SimpsonThe
1263c6ba09bSBruce M Simpson.Fa size
1273c6ba09bSBruce M Simpsonargument specifies the size, in bytes, of the buffer
1283c6ba09bSBruce M Simpson.Fa *dst .
12989482e46SYoshinobu InoueIt returns NULL if a system error occurs (in which case,
13089482e46SYoshinobu Inoue.Va errno
13189482e46SYoshinobu Inouewill have been set), or it returns a pointer to the destination string.
13289482e46SYoshinobu InoueThis function is presently valid for
13389482e46SYoshinobu Inoue.Dv AF_INET
13489482e46SYoshinobu Inoueand
13589482e46SYoshinobu Inoue.Dv AF_INET6 .
13689482e46SYoshinobu Inoue.Pp
13758f0484fSRodney W. GrimesThe routine
13858f0484fSRodney W. Grimes.Fn inet_ntoa
13958f0484fSRodney W. Grimestakes an Internet address and returns an
14058f0484fSRodney W. Grimes.Tn ASCII
14158f0484fSRodney W. Grimesstring representing the address in
14242635956SRuslan Ermilov.Ql .\&
1431a0a9345SRuslan Ermilovnotation.
1441a0a9345SRuslan ErmilovThe routine
14558f0484fSRodney W. Grimes.Fn inet_makeaddr
14658f0484fSRodney W. Grimestakes an Internet network number and a local
14758f0484fSRodney W. Grimesnetwork address and constructs an Internet address
1481a0a9345SRuslan Ermilovfrom it.
1491a0a9345SRuslan ErmilovThe routines
15058f0484fSRodney W. Grimes.Fn inet_netof
15158f0484fSRodney W. Grimesand
15258f0484fSRodney W. Grimes.Fn inet_lnaof
15358f0484fSRodney W. Grimesbreak apart Internet host addresses, returning
15458f0484fSRodney W. Grimesthe network number and local network address part,
15558f0484fSRodney W. Grimesrespectively.
15658f0484fSRodney W. Grimes.Pp
15758f0484fSRodney W. GrimesAll Internet addresses are returned in network
15858f0484fSRodney W. Grimesorder (bytes ordered from left to right).
15958f0484fSRodney W. GrimesAll network numbers and local address parts are
1607125977bSRuslan Ermilovreturned as machine byte order integer values.
16158f0484fSRodney W. Grimes.Sh INTERNET ADDRESSES
16258f0484fSRodney W. GrimesValues specified using the
16342635956SRuslan Ermilov.Ql .\&
16458f0484fSRodney W. Grimesnotation take one
16558f0484fSRodney W. Grimesof the following forms:
16658f0484fSRodney W. Grimes.Bd -literal -offset indent
16758f0484fSRodney W. Grimesa.b.c.d
16858f0484fSRodney W. Grimesa.b.c
16958f0484fSRodney W. Grimesa.b
17058f0484fSRodney W. Grimesa
17158f0484fSRodney W. Grimes.Ed
17258f0484fSRodney W. Grimes.Pp
17358f0484fSRodney W. GrimesWhen four parts are specified, each is interpreted
17458f0484fSRodney W. Grimesas a byte of data and assigned, from left to right,
1751a0a9345SRuslan Ermilovto the four bytes of an Internet address.
1761a0a9345SRuslan ErmilovNote
17758f0484fSRodney W. Grimesthat when an Internet address is viewed as a 32-bit
17858f0484fSRodney W. Grimesinteger quantity on the
17958f0484fSRodney W. Grimes.Tn VAX
18058f0484fSRodney W. Grimesthe bytes referred to
18158f0484fSRodney W. Grimesabove appear as
18258f0484fSRodney W. Grimes.Dq Li d.c.b.a .
18358f0484fSRodney W. GrimesThat is,
18458f0484fSRodney W. Grimes.Tn VAX
18558f0484fSRodney W. Grimesbytes are
18658f0484fSRodney W. Grimesordered from right to left.
18758f0484fSRodney W. Grimes.Pp
18858f0484fSRodney W. GrimesWhen a three part address is specified, the last
18958f0484fSRodney W. Grimespart is interpreted as a 16-bit quantity and placed
19058f0484fSRodney W. Grimesin the right-most two bytes of the network address.
19158f0484fSRodney W. GrimesThis makes the three part address format convenient
19258f0484fSRodney W. Grimesfor specifying Class B network addresses as
19358f0484fSRodney W. Grimes.Dq Li 128.net.host .
19458f0484fSRodney W. Grimes.Pp
19558f0484fSRodney W. GrimesWhen a two part address is supplied, the last part
19658f0484fSRodney W. Grimesis interpreted as a 24-bit quantity and placed in
19758f0484fSRodney W. Grimesthe right most three bytes of the network address.
19858f0484fSRodney W. GrimesThis makes the two part address format convenient
19958f0484fSRodney W. Grimesfor specifying Class A network addresses as
20058f0484fSRodney W. Grimes.Dq Li net.host .
20158f0484fSRodney W. Grimes.Pp
20258f0484fSRodney W. GrimesWhen only one part is given, the value is stored
20358f0484fSRodney W. Grimesdirectly in the network address without any byte
20458f0484fSRodney W. Grimesrearrangement.
20558f0484fSRodney W. Grimes.Pp
20658f0484fSRodney W. GrimesAll numbers supplied as
20758f0484fSRodney W. Grimes.Dq parts
20858f0484fSRodney W. Grimesin a
20942635956SRuslan Ermilov.Ql .\&
21058f0484fSRodney W. Grimesnotation
21158f0484fSRodney W. Grimesmay be decimal, octal, or hexadecimal, as specified
21258f0484fSRodney W. Grimesin the C language (i.e., a leading 0x or 0X implies
21358f0484fSRodney W. Grimeshexadecimal; otherwise, a leading 0 implies octal;
21458f0484fSRodney W. Grimesotherwise, the number is interpreted as decimal).
21558f0484fSRodney W. Grimes.Sh DIAGNOSTICS
21658f0484fSRodney W. GrimesThe constant
21758f0484fSRodney W. Grimes.Dv INADDR_NONE
21858f0484fSRodney W. Grimesis returned by
21958f0484fSRodney W. Grimes.Fn inet_addr
22058f0484fSRodney W. Grimesand
22158f0484fSRodney W. Grimes.Fn inet_network
22258f0484fSRodney W. Grimesfor malformed requests.
2233c6ba09bSBruce M Simpson.Sh ERRORS
2243c6ba09bSBruce M SimpsonThe
2253c6ba09bSBruce M Simpson.Fn inet_ntop
2263c6ba09bSBruce M Simpsoncall fails if:
2273c6ba09bSBruce M Simpson.Bl -tag -width Er
2283c6ba09bSBruce M Simpson.It Bq Er ENOSPC
2293c6ba09bSBruce M Simpson.Fa size
2303c6ba09bSBruce M Simpsonwas not large enough to store the presentation form of the address.
2313c6ba09bSBruce M Simpson.It Bq Er EAFNOSUPPORT
2323c6ba09bSBruce M Simpson.Fa *src
23333992dc0SRuslan Ermilovwas not an
23433992dc0SRuslan Ermilov.Dv AF_INET
23533992dc0SRuslan Ermilovor
23633992dc0SRuslan Ermilov.Dv AF_INET6
23733992dc0SRuslan Ermilovfamily address.
2383c6ba09bSBruce M Simpson.El
23958f0484fSRodney W. Grimes.Sh SEE ALSO
24081a3cb97SRuslan Ermilov.Xr byteorder 3 ,
24167228c46SBruce M Simpson.Xr getaddrinfo 3 ,
24258f0484fSRodney W. Grimes.Xr gethostbyname 3 ,
24367228c46SBruce M Simpson.Xr getnameinfo 3 ,
24458f0484fSRodney W. Grimes.Xr getnetent 3 ,
24581a3cb97SRuslan Ermilov.Xr inet_net 3 ,
24658f0484fSRodney W. Grimes.Xr hosts 5 ,
24713608f66SWolfram Schneider.Xr networks 5
24889482e46SYoshinobu Inoue.Rs
24989482e46SYoshinobu Inoue.%R RFC
25089482e46SYoshinobu Inoue.%N 2373
25189482e46SYoshinobu Inoue.%D July 1998
25289482e46SYoshinobu Inoue.%T "IP Version 6 Addressing Architecture"
25389482e46SYoshinobu Inoue.Re
25489482e46SYoshinobu Inoue.Sh STANDARDS
25589482e46SYoshinobu InoueThe
256563f6bdeSRuslan Ermilov.Fn inet_ntop
25789482e46SYoshinobu Inoueand
258563f6bdeSRuslan Ermilov.Fn inet_pton
25989482e46SYoshinobu Inouefunctions conform to
26033dfeb89SRuslan Ermilov.St -xns5.2 .
26189482e46SYoshinobu InoueNote that
262563f6bdeSRuslan Ermilov.Fn inet_pton
26389482e46SYoshinobu Inouedoes not accept 1-, 2-, or 3-part dotted addresses; all four parts
2642d69bc9bSCrist J. Clarkmust be specified and are interpreted only as decimal values.
26589482e46SYoshinobu InoueThis is a narrower input set than that accepted by
266563f6bdeSRuslan Ermilov.Fn inet_aton .
26758f0484fSRodney W. Grimes.Sh HISTORY
26858f0484fSRodney W. GrimesThese
26958f0484fSRodney W. Grimesfunctions appeared in
27058f0484fSRodney W. Grimes.Bx 4.2 .
27158f0484fSRodney W. Grimes.Sh BUGS
27258f0484fSRodney W. GrimesThe value
27358f0484fSRodney W. Grimes.Dv INADDR_NONE
27458f0484fSRodney W. Grimes(0xffffffff) is a valid broadcast address, but
27558f0484fSRodney W. Grimes.Fn inet_addr
27658f0484fSRodney W. Grimescannot return that value without indicating failure.
27758f0484fSRodney W. GrimesThe newer
27858f0484fSRodney W. Grimes.Fn inet_aton
27958f0484fSRodney W. Grimesfunction does not share this problem.
28058f0484fSRodney W. GrimesThe problem of host byte ordering versus network byte ordering is
28158f0484fSRodney W. Grimesconfusing.
28258f0484fSRodney W. GrimesThe string returned by
28358f0484fSRodney W. Grimes.Fn inet_ntoa
28458f0484fSRodney W. Grimesresides in a static memory area.
28558f0484fSRodney W. Grimes.Pp
2860f0eddbbSRuslan ErmilovThe
2870f0eddbbSRuslan Ermilov.Fn inet_addr
2880f0eddbbSRuslan Ermilovfunction should return a
28958f0484fSRodney W. Grimes.Fa struct in_addr .
290