334 lines
8.7 KiB
HTML
334 lines
8.7 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of INET_PTON</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>INET_PTON</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
inet_pton - convert IPv4 and IPv6 addresses from text to binary form
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>></B>
|
|
|
|
<B>int inet_pton(int </B><I>af</I><B>, const char *</B><I>src</I><B>, void *</B><I>dst</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
This function converts the character string
|
|
<I>src</I>
|
|
|
|
into a network address structure in the
|
|
<I>af</I>
|
|
|
|
address family, then
|
|
copies
|
|
the network address structure to
|
|
<I>dst</I>.
|
|
|
|
The
|
|
<I>af</I>
|
|
|
|
argument must be either
|
|
<B>AF_INET</B>
|
|
|
|
or
|
|
<B>AF_INET6</B>.
|
|
|
|
<I>dst</I>
|
|
|
|
is written in network byte order.
|
|
<P>
|
|
|
|
The following address families are currently supported:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>AF_INET</B>
|
|
|
|
<DD>
|
|
<I>src</I>
|
|
|
|
points to a character string containing an IPv4 network address in
|
|
dotted-decimal format, "<I>ddd.ddd.ddd.ddd</I>", where
|
|
<I>ddd</I>
|
|
|
|
is a decimal number of up to three digits in the range 0 to 255.
|
|
The address is converted to a
|
|
<I>struct in_addr</I>
|
|
|
|
and copied to
|
|
<I>dst</I>,
|
|
|
|
which must be
|
|
<I>sizeof(struct in_addr)</I>
|
|
|
|
(4) bytes (32 bits) long.
|
|
<DT id="2"><B>AF_INET6</B>
|
|
|
|
<DD>
|
|
<I>src</I>
|
|
|
|
points to a character string containing an IPv6 network address.
|
|
The address is converted to a
|
|
<I>struct in6_addr</I>
|
|
|
|
and copied to
|
|
<I>dst</I>,
|
|
|
|
which must be
|
|
<I>sizeof(struct in6_addr)</I>
|
|
|
|
(16) bytes (128 bits) long.
|
|
The allowed formats for IPv6 addresses follow these rules:
|
|
<DL COMPACT><DT id="3"><DD>
|
|
<DL COMPACT>
|
|
<DT id="4">1.<DD>
|
|
The preferred format is
|
|
<I>x:x:x:x:x:x:x:x</I>.
|
|
|
|
This form consists of eight hexadecimal numbers,
|
|
each of which expresses a 16-bit value (i.e., each
|
|
<I>x</I>
|
|
|
|
can be up to 4 hex digits).
|
|
<DT id="5">2.<DD>
|
|
A series of contiguous zero values in the preferred format
|
|
can be abbreviated to
|
|
<I>::</I>.
|
|
|
|
Only one instance of
|
|
<I>::</I>
|
|
|
|
can occur in an address.
|
|
For example, the loopback address
|
|
<I>0:0:0:0:0:0:0:1</I>
|
|
|
|
can be abbreviated as
|
|
<I>::1</I>.
|
|
|
|
The wildcard address, consisting of all zeros, can be written as
|
|
<I>::</I>.
|
|
|
|
<DT id="6">3.<DD>
|
|
An alternate format is useful for expressing IPv4-mapped IPv6 addresses.
|
|
This form is written as
|
|
<I>x:x:x:x:x:x:d.d.d.d</I>,
|
|
|
|
where the six leading
|
|
<I>x</I>s
|
|
|
|
are hexadecimal values that define the six most-significant
|
|
16-bit pieces of the address (i.e., 96 bits), and the
|
|
<I>d</I>s
|
|
|
|
express a value in dotted-decimal notation that
|
|
defines the least significant 32 bits of the address.
|
|
An example of such an address is
|
|
<I>::FFFF:204.152.189.116</I>.
|
|
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="7"><DD>
|
|
See RFC 2373 for further details on the representation of IPv6 addresses.
|
|
</DL>
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
<B>inet_pton</B>()
|
|
|
|
returns 1 on success (network address was successfully converted).
|
|
0 is returned if
|
|
<I>src</I>
|
|
|
|
does not contain a character string representing a valid network
|
|
address in the specified address family.
|
|
If
|
|
<I>af</I>
|
|
|
|
does not contain a valid address family, -1 is returned and
|
|
<I>errno</I>
|
|
|
|
is set to
|
|
<B>EAFNOSUPPORT</B>.
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>inet_pton</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
Unlike
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_aton">inet_aton</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_addr">inet_addr</A></B>(3),
|
|
|
|
<B>inet_pton</B>()
|
|
|
|
supports IPv6 addresses.
|
|
On the other hand,
|
|
<B>inet_pton</B>()
|
|
|
|
accepts only IPv4 addresses in dotted-decimal notation, whereas
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_aton">inet_aton</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_addr">inet_addr</A></B>(3)
|
|
|
|
allow the more general numbers-and-dots notation (hexadecimal
|
|
and octal number formats, and formats that don't require all
|
|
four bytes to be explicitly written).
|
|
For an interface that handles both IPv6 addresses, and IPv4
|
|
addresses in numbers-and-dots notation, see
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3).
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
<B>AF_INET6</B>
|
|
|
|
does not recognize IPv4 addresses.
|
|
An explicit IPv4-mapped IPv6 address must be supplied in
|
|
<I>src</I>
|
|
|
|
instead.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>inet_pton</B>()
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_ntop">inet_ntop</A></B>(3).
|
|
|
|
Here are some example runs:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out i6 0:0:0:0:0:0:0:0</B>
|
|
|
|
::
|
|
$<B> ./a.out i6 1:0:0:0:0:0:0:8</B>
|
|
|
|
1::8
|
|
$<B> ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116</B>
|
|
|
|
::ffff:204.152.189.116
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> unsigned char buf[sizeof(struct in6_addr)];
|
|
<BR> int domain, s;
|
|
<BR> char str[INET6_ADDRSTRLEN];
|
|
<P>
|
|
<BR> if (argc != 3) {
|
|
<BR> fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
|
|
<BR> (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
|
|
<P>
|
|
<BR> s = inet_pton(domain, argv[2], buf);
|
|
<BR> if (s <= 0) {
|
|
<BR> if (s == 0)
|
|
<BR> fprintf(stderr, "Not in presentation format");
|
|
<BR> else
|
|
<BR> perror("inet_pton");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
|
|
<BR> perror("inet_ntop");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("%s\n", str);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet">inet</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+inet_ntop">inet_ntop</A></B>(3)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="8"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="9"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="10"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="11"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="12"><A HREF="#lbAF">ATTRIBUTES</A><DD>
|
|
<DT id="13"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="14"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="15"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="16"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="17"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="18"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="19"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:46 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|