419 lines
11 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
bind - bind a name to a socket
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B> /* See NOTES */
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></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"> </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> sa_family_t sa_family;
|
|
<BR> char 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"> </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"> </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"> </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"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
POSIX.1 does not require the inclusion of
|
|
<I><<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></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"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
The transparent proxy options are not described.
|
|
|
|
<A NAME="lbAJ"> </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 <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/un.h">sys/un.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
#define MY_SOCK_PATH "/somepath"
|
|
#define LISTEN_BACKLOG 50
|
|
<P>
|
|
#define handle_error(msg) \
|
|
<BR> do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int sfd, cfd;
|
|
<BR> struct sockaddr_un my_addr, peer_addr;
|
|
<BR> socklen_t peer_addr_size;
|
|
<P>
|
|
<BR> sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
<BR> if (sfd == -1)
|
|
<BR> handle_error("socket");
|
|
<P>
|
|
<BR> memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
|
<BR> /* Clear structure */
|
|
<BR> my_addr.sun_family = AF_UNIX;
|
|
<BR> strncpy(my_addr.sun_path, MY_SOCK_PATH,
|
|
<BR> sizeof(my_addr.sun_path) - 1);
|
|
<P>
|
|
<BR> if (bind(sfd, (struct sockaddr *) &my_addr,
|
|
<BR> sizeof(struct sockaddr_un)) == -1)
|
|
<BR> handle_error("bind");
|
|
<P>
|
|
<BR> if (listen(sfd, LISTEN_BACKLOG) == -1)
|
|
<BR> handle_error("listen");
|
|
<P>
|
|
<BR> /* Now we can accept incoming connections one
|
|
<BR> at a time using <A HREF="/cgi-bin/man/man2html?2+accept">accept</A>(2) */
|
|
<P>
|
|
<BR> peer_addr_size = sizeof(struct sockaddr_un);
|
|
<BR> cfd = accept(sfd, (struct sockaddr *) &peer_addr,
|
|
<BR> &peer_addr_size);
|
|
<BR> if (cfd == -1)
|
|
<BR> handle_error("accept");
|
|
<P>
|
|
<BR> /* Code to deal with incoming connection(s)... */
|
|
<P>
|
|
<BR> /* When no longer required, the socket pathname, MY_SOCK_PATH
|
|
<BR> should be deleted using <A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A>(2) or <A HREF="/cgi-bin/man/man2html?3+remove">remove</A>(3) */
|
|
}
|
|
|
|
<A NAME="lbAK"> </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"> </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="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>
|