man-pages/man2/bind.2.html
2021-03-31 01:06:50 +01:00

419 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of BIND</TITLE>
</HEAD><BODY>
<H1>BIND</H1>
Section: Linux Programmer's Manual (2)<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>
bind - bind a name to a socket
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B> /* See NOTES */
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>int bind(int </B><I>sockfd</I><B>, const struct sockaddr *</B><I>addr</I><B>,</B>
<B> socklen_t </B><I>addrlen</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
When a socket is created with
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
it exists in a name space (address family) but has no address assigned to it.
<B>bind</B>()
assigns the address specified by
<I>addr</I>
to the socket referred to by the file descriptor
<I>sockfd</I>.
<I>addrlen</I>
specifies the size, in bytes, of the address structure pointed to by
<I>addr</I>.
Traditionally, this operation is called "assigning a name to a socket".
<P>
It is normally necessary to assign a local address using
<B>bind</B>()
before a
<B>SOCK_STREAM</B>
socket may receive connections (see
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)).
<P>
The rules used in name binding vary between address families.
Consult the manual entries in Section 7 for detailed information.
For
<B>AF_INET</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7);
for
<B>AF_INET6</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+ipv6">ipv6</A></B>(7);
for
<B>AF_UNIX</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7);
for
<B>AF_APPLETALK</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+ddp">ddp</A></B>(7);
for
<B>AF_PACKET</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7);
for
<B>AF_X25</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+x25">x25</A></B>(7);
and for
<B>AF_NETLINK</B>,
see
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7).
<P>
The actual structure passed for the
<I>addr</I>
argument will depend on the address family.
The
<I>sockaddr</I>
structure is defined as something like:
<P>
struct sockaddr {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa_family_t&nbsp;sa_family;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sa_data[14];
}
<P>
The only purpose of this structure is to cast the structure
pointer passed in
<I>addr</I>
in order to avoid compiler warnings.
See EXAMPLE below.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EACCES</B>
<DD>
The address is protected, and the user is not the superuser.
<DT id="2"><B>EADDRINUSE</B>
<DD>
The given address is already in use.
<DT id="3"><B>EADDRINUSE</B>
<DD>
(Internet domain sockets)
The port number was specified as zero in the socket address structure,
but, upon attempting to bind to an ephemeral port,
it was determined that all port numbers in the ephemeral port range
are currently in use.
See the discussion of
<I>/proc/sys/net/ipv4/ip_local_port_range</I>
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
<DT id="4"><B>EBADF</B>
<DD>
<I>sockfd</I>
is not a valid file descriptor.
<DT id="5"><B>EINVAL</B>
<DD>
The socket is already bound to an address.
<DT id="6"><B>EINVAL</B>
<DD>
<I>addrlen</I>
is wrong, or
<I>addr</I>
is not a valid address for this socket's domain.
<DT id="7"><B>ENOTSOCK</B>
<DD>
The file descriptor
<I>sockfd</I>
does not refer to a socket.
</DL>
<P>
The following errors are specific to UNIX domain
(<B>AF_UNIX</B>)
sockets:
<DL COMPACT>
<DT id="8"><B>EACCES</B>
<DD>
Search permission is denied on a component of the path prefix.
(See also
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
<DT id="9"><B>EADDRNOTAVAIL</B>
<DD>
A nonexistent interface was requested or the requested
address was not local.
<DT id="10"><B>EFAULT</B>
<DD>
<I>addr</I>
points outside the user's accessible address space.
<DT id="11"><B>ELOOP</B>
<DD>
Too many symbolic links were encountered in resolving
<I>addr</I>.
<DT id="12"><B>ENAMETOOLONG</B>
<DD>
<I>addr</I>
is too long.
<DT id="13"><B>ENOENT</B>
<DD>
A component in the directory prefix of the socket pathname does not exist.
<DT id="14"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="15"><B>ENOTDIR</B>
<DD>
A component of the path prefix is not a directory.
<DT id="16"><B>EROFS</B>
<DD>
The socket inode would reside on a read-only filesystem.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
(<B>bind</B>()
first appeared in 4.2BSD).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1 does not require the inclusion of
<I>&lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</I>,
and this header file is not required on Linux.
However, some historical (BSD) implementations required this header
file, and portable applications are probably wise to include it.
<P>
For background on the
<I>socklen_t</I>
type, see
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
<A NAME="lbAI">&nbsp;</A>
<H2>BUGS</H2>
The transparent proxy options are not described.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
An example of the use of
<B>bind</B>()
with Internet domain sockets can be found in
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3).
<P>
The following example shows how to bind a stream socket in the UNIX
(<B>AF_UNIX</B>)
domain, and accept connections:
<P>
#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/un.h">sys/un.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
<P>
#define MY_SOCK_PATH &quot;/somepath&quot;
#define LISTEN_BACKLOG 50
<P>
#define handle_error(msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;sfd,&nbsp;cfd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sockaddr_un&nbsp;my_addr,&nbsp;peer_addr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;socklen_t&nbsp;peer_addr_size;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sfd&nbsp;=&nbsp;socket(AF_UNIX,&nbsp;SOCK_STREAM,&nbsp;0);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sfd&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;socket&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(&amp;my_addr,&nbsp;0,&nbsp;sizeof(struct&nbsp;sockaddr_un));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Clear&nbsp;structure&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;my_addr.sun_family&nbsp;=&nbsp;AF_UNIX;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strncpy(my_addr.sun_path,&nbsp;MY_SOCK_PATH,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizeof(my_addr.sun_path)&nbsp;-&nbsp;1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(bind(sfd,&nbsp;(struct&nbsp;sockaddr&nbsp;*)&nbsp;&amp;my_addr,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizeof(struct&nbsp;sockaddr_un))&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;bind&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(listen(sfd,&nbsp;LISTEN_BACKLOG)&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;listen&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Now&nbsp;we&nbsp;can&nbsp;accept&nbsp;incoming&nbsp;connections&nbsp;one
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;a&nbsp;time&nbsp;using&nbsp;<A HREF="/cgi-bin/man/man2html?2+accept">accept</A>(2)&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;peer_addr_size&nbsp;=&nbsp;sizeof(struct&nbsp;sockaddr_un);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;cfd&nbsp;=&nbsp;accept(sfd,&nbsp;(struct&nbsp;sockaddr&nbsp;*)&nbsp;&amp;peer_addr,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;peer_addr_size);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(cfd&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;accept&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Code&nbsp;to&nbsp;deal&nbsp;with&nbsp;incoming&nbsp;connection(s)...&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;When&nbsp;no&nbsp;longer&nbsp;required,&nbsp;the&nbsp;socket&nbsp;pathname,&nbsp;MY_SOCK_PATH
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;should&nbsp;be&nbsp;deleted&nbsp;using&nbsp;<A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A>(2)&nbsp;or&nbsp;<A HREF="/cgi-bin/man/man2html?3+remove">remove</A>(3)&nbsp;*/
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+getaddrinfo">getaddrinfo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getifaddrs">getifaddrs</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+ipv6">ipv6</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)
<A NAME="lbAL">&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="17"><A HREF="#lbAB">NAME</A><DD>
<DT id="18"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="19"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="20"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="21"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="22"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="23"><A HREF="#lbAH">NOTES</A><DD>
<DT id="24"><A HREF="#lbAI">BUGS</A><DD>
<DT id="25"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="26"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="27"><A HREF="#lbAL">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:32 GMT, March 31, 2021
</BODY>
</HTML>