man-pages/man3/inet_pton.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
inet_pton - convert IPv4 and IPv6 addresses from text to binary form
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>&gt;</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">&nbsp;</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, &quot;<I>ddd.ddd.ddd.ddd</I>&quot;, 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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAH">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;buf[sizeof(struct&nbsp;in6_addr)];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;domain,&nbsp;s;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;str[INET6_ADDRSTRLEN];
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;{i4|i6|&lt;num&gt;}&nbsp;string\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;domain&nbsp;=&nbsp;(strcmp(argv[1],&nbsp;&quot;i4&quot;)&nbsp;==&nbsp;0)&nbsp;?&nbsp;AF_INET&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(strcmp(argv[1],&nbsp;&quot;i6&quot;)&nbsp;==&nbsp;0)&nbsp;?&nbsp;AF_INET6&nbsp;:&nbsp;atoi(argv[1]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;inet_pton(domain,&nbsp;argv[2],&nbsp;buf);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;&lt;=&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;==&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Not&nbsp;in&nbsp;presentation&nbsp;format&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;inet_pton&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(inet_ntop(domain,&nbsp;buf,&nbsp;str,&nbsp;INET6_ADDRSTRLEN)&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;inet_ntop&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s\n&quot;,&nbsp;str);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</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">&nbsp;</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">&nbsp;</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>