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

453 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of INET</TITLE>
</HEAD><BODY>
<H1>INET</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_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof,
inet_netof - Internet address manipulation routines
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/netinet/in.h">netinet/in.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>&gt;</B>
<B>int inet_aton(const char *</B><I>cp</I><B>, struct in_addr *</B><I>inp</I><B>);</B>
<B>in_addr_t inet_addr(const char *</B><I>cp</I><B>);</B>
<B>in_addr_t inet_network(const char *</B><I>cp</I><B>);</B>
<B>char *inet_ntoa(struct in_addr </B><I>in</I><B>);</B>
<B>struct in_addr inet_makeaddr(in_addr_t </B><I>net</I><B>, in_addr_t </B><I>host</I><B>);</B>
<B>in_addr_t inet_lnaof(struct in_addr </B><I>in</I><B>);</B>
<B>in_addr_t inet_netof(struct in_addr </B><I>in</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>inet_aton</B>(),
<B>inet_ntoa</B>():
<PRE>
Since glibc 2.19:
_DEFAULT_SOURCE
In glibc up to and including 2.19:
_BSD_SOURCE || _BSD_SOURCE
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>inet_aton</B>()
converts the Internet host address <I>cp</I> from the
IPv4 numbers-and-dots notation into binary form (in network byte order)
and stores it in the structure that <I>inp</I> points to.
<B>inet_aton</B>()
returns nonzero if the address is valid, zero if not.
The address supplied in
<I>cp</I>
can have one of the following forms:
<DL COMPACT>
<DT id="1"><I>a.b.c.d</I>
<DD>
Each of the four numeric parts specifies a byte of the address;
the bytes are assigned in left-to-right order to produce the binary address.
<DT id="2"><I>a.b.c</I>
<DD>
Parts
<I>a</I>
and
<I>b</I>
specify the first two bytes of the binary address.
Part
<I>c</I>
is interpreted as a 16-bit value that defines the rightmost two bytes
of the binary address.
This notation is suitable for specifying (outmoded) Class B
network addresses.
<DT id="3"><I>a.b</I>
<DD>
Part
<I>a</I>
specifies the first byte of the binary address.
Part
<I>b</I>
is interpreted as a 24-bit value that defines the rightmost three bytes
of the binary address.
This notation is suitable for specifying (outmoded) Class A
network addresses.
<DT id="4"><I>a</I>
<DD>
The value
<I>a</I>
is interpreted as a 32-bit value that is stored directly
into the binary address without any byte rearrangement.
</DL>
<P>
In all of the above forms,
components of the dotted address can be specified in decimal,
octal (with a leading
<I>0</I>),
or hexadecimal, with a leading
<I>0X</I>).
Addresses in any of these forms are collectively termed
<I>IPV4 numbers-and-dots notation</I>.
The form that uses exactly four decimal numbers is referred to as
<I>IPv4 dotted-decimal notation</I>
(or sometimes:
<I>IPv4 dotted-quad notation</I>).
<P>
<B>inet_aton</B>()
returns 1 if the supplied string was successfully interpreted,
or 0 if the string is invalid
(<B>errno</B>
is
<I>not</I>
set on error).
<P>
The
<B>inet_addr</B>()
function converts the Internet host address
<I>cp</I> from IPv4 numbers-and-dots notation into binary data in network
byte order.
If the input is invalid,
<B>INADDR_NONE</B>
(usually -1) is returned.
Use of this function is problematic because -1 is a valid address
(255.255.255.255).
Avoid its use in favor of
<B>inet_aton</B>(),
<B><A HREF="/cgi-bin/man/man2html?3+inet_pton">inet_pton</A></B>(3),
or
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3),
which provide a cleaner way to indicate error return.
<P>
The
<B>inet_network</B>()
function converts
<I>cp</I>,
a string in IPv4 numbers-and-dots notation,
into a number in host byte order suitable for use as an
Internet network address.
On success, the converted address is returned.
If the input is invalid, -1 is returned.
<P>
The
<B>inet_ntoa</B>()
function converts the Internet host address
<I>in</I>, given in network byte order, to a string in IPv4
dotted-decimal notation.
The string is returned in a statically
allocated buffer, which subsequent calls will overwrite.
<P>
The
<B>inet_lnaof</B>()
function returns the local network address part
of the Internet address <I>in</I>.
The returned value is in host byte order.
<P>
The
<B>inet_netof</B>()
function returns the network number part of
the Internet address <I>in</I>.
The returned value is in host byte order.
<P>
The
<B>inet_makeaddr</B>()
function is the converse of
<B>inet_netof</B>()
and
<B>inet_lnaof</B>().
It returns an Internet host address in network byte order,
created by combining the network number <I>net</I>
with the local address <I>host</I>, both in
host byte order.
<P>
The structure <I>in_addr</I> as used in
<B>inet_ntoa</B>(),
<B>inet_makeaddr</B>(),
<B>inet_lnaof</B>()
and
<B>inet_netof</B>()
is defined in
<I>&lt;<A HREF="file:///usr/include/netinet/in.h">netinet/in.h</A>&gt;</I>
as:
<P>
typedef uint32_t in_addr_t;
<P>
struct in_addr {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;in_addr_t&nbsp;s_addr;
};
<A NAME="lbAE">&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_aton</B>(),
<B>inet_addr</B>(),
<BR>
<B>inet_network</B>(),
<B>inet_ntoa</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
<TR VALIGN=top><TD>
<B>inet_makeaddr</B>(),
<B>inet_lnaof</B>(),
<BR>
<B>inet_netof</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAF">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>inet_addr</B>(),
<B>inet_ntoa</B>():
POSIX.1-2001, POSIX.1-2008, 4.3BSD.
<P>
<B>inet_aton</B>()
is not specified in POSIX.1, but is available on most systems.
<A NAME="lbAG">&nbsp;</A>
<H2>NOTES</H2>
On x86 architectures, the host byte order is Least Significant Byte
first (little endian), whereas the network byte order, as used on the
Internet, is Most Significant Byte first (big endian).
<P>
<B>inet_lnaof</B>(),
<B>inet_netof</B>(),
and
<B>inet_makeaddr</B>()
are legacy functions that assume they are dealing with
<I>classful network addresses</I>.
Classful networking divides IPv4 network addresses into host and network
components at byte boundaries, as follows:
<DL COMPACT>
<DT id="5">Class A<DD>
This address type is indicated by the value 0 in the
most significant bit of the (network byte ordered) address.
The network address is contained in the most significant byte,
and the host address occupies the remaining three bytes.
<DT id="6">Class B<DD>
This address type is indicated by the binary value 10 in the
most significant two bits of the address.
The network address is contained in the two most significant bytes,
and the host address occupies the remaining two bytes.
<DT id="7">Class C<DD>
This address type is indicated by the binary value 110 in the
most significant three bits of the address.
The network address is contained in the three most significant bytes,
and the host address occupies the remaining byte.
</DL>
<P>
Classful network addresses are now obsolete,
having been superseded by Classless Inter-Domain Routing (CIDR),
which divides addresses into network and host components at
arbitrary bit (rather than byte) boundaries.
<A NAME="lbAH">&nbsp;</A>
<H2>EXAMPLE</H2>
An example of the use of
<B>inet_aton</B>()
and
<B>inet_ntoa</B>()
is shown below.
Here are some example runs:
<P>
$<B> ./a.out 226.000.000.037</B> # Last byte is in octal
226.0.0.31
$<B> ./a.out 0x7f.1 </B> # First byte is in hex
127.0.0.1
<A NAME="lbAI">&nbsp;</A>
<H3>Program source</H3>
#define _BSD_SOURCE
#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;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;in_addr&nbsp;addr;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;%s&nbsp;&lt;dotted-address&gt;\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;if&nbsp;(inet_aton(argv[1],&nbsp;&amp;addr)&nbsp;==&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Invalid&nbsp;address\n&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;inet_ntoa(addr));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+byteorder">byteorder</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+gethostbyname">gethostbyname</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getnameinfo">getnameinfo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getnetent">getnetent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+inet_net_pton">inet_net_pton</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+inet_ntop">inet_ntop</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+inet_pton">inet_pton</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+hosts">hosts</A></B>(5),
<B><A HREF="/cgi-bin/man/man2html?5+networks">networks</A></B>(5)
<A NAME="lbAK">&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">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAF">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAG">NOTES</A><DD>
<DT id="14"><A HREF="#lbAH">EXAMPLE</A><DD>
<DL>
<DT id="15"><A HREF="#lbAI">Program source</A><DD>
</DL>
<DT id="16"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAK">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>