Blame man2/bind.2

Packit 7cfc04
.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Portions extracted from /usr/include/sys/socket.h, which does not have
Packit 7cfc04
.\" any authorship information in it.  It is probably available under the GPL.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
Packit 7cfc04
.\"
Packit 7cfc04
.\" Copyright (c) 1983 The Regents of the University of California.
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"     This product includes software developed by the University of
Packit 7cfc04
.\"     California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 1998 by Andi Kleen
Packit 7cfc04
.\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
Packit 7cfc04
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.TH BIND 2 2016-12-12 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
bind \- bind a name to a socket
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.BR "#include <sys/types.h>" "          /* See NOTES */"
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
Packit 7cfc04
.BI "         socklen_t " addrlen );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
When a socket is created with
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
it exists in a name space (address family) but has no address assigned to it.
Packit 7cfc04
.BR bind ()
Packit 7cfc04
assigns the address specified by
Packit 7cfc04
.I addr
Packit 7cfc04
to the socket referred to by the file descriptor
Packit 7cfc04
.IR sockfd .
Packit 7cfc04
.I addrlen
Packit 7cfc04
specifies the size, in bytes, of the address structure pointed to by
Packit 7cfc04
.IR addr .
Packit 7cfc04
Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
Packit 7cfc04
.PP
Packit 7cfc04
It is normally necessary to assign a local address using
Packit 7cfc04
.BR bind ()
Packit 7cfc04
before a
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
socket may receive connections (see
Packit 7cfc04
.BR accept (2)).
Packit 7cfc04
.PP
Packit 7cfc04
The rules used in name binding vary between address families.
Packit 7cfc04
Consult the manual entries in Section 7 for detailed information.
Packit 7cfc04
For
Packit 7cfc04
.BR AF_INET ,
Packit 7cfc04
see
Packit 7cfc04
.BR ip (7);
Packit 7cfc04
for
Packit 7cfc04
.BR AF_INET6 ,
Packit 7cfc04
see
Packit 7cfc04
.BR ipv6 (7);
Packit 7cfc04
for
Packit 7cfc04
.BR AF_UNIX ,
Packit 7cfc04
see
Packit 7cfc04
.BR unix (7);
Packit 7cfc04
for
Packit 7cfc04
.BR AF_APPLETALK ,
Packit 7cfc04
see
Packit 7cfc04
.BR ddp (7);
Packit 7cfc04
for
Packit 7cfc04
.BR AF_PACKET ,
Packit 7cfc04
see
Packit 7cfc04
.BR packet (7);
Packit 7cfc04
for
Packit 7cfc04
.BR AF_X25 ,
Packit 7cfc04
see
Packit 7cfc04
.BR x25 (7);
Packit 7cfc04
and for
Packit 7cfc04
.BR AF_NETLINK ,
Packit 7cfc04
see
Packit 7cfc04
.BR netlink (7).
Packit 7cfc04
.PP
Packit 7cfc04
The actual structure passed for the
Packit 7cfc04
.I addr
Packit 7cfc04
argument will depend on the address family.
Packit 7cfc04
The
Packit 7cfc04
.I sockaddr
Packit 7cfc04
structure is defined as something like:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct sockaddr {
Packit 7cfc04
    sa_family_t sa_family;
Packit 7cfc04
    char        sa_data[14];
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The only purpose of this structure is to cast the structure
Packit 7cfc04
pointer passed in
Packit 7cfc04
.I addr
Packit 7cfc04
in order to avoid compiler warnings.
Packit 7cfc04
See EXAMPLE below.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, zero is returned.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
.\" e.g., privileged port in AF_INET domain
Packit 7cfc04
The address is protected, and the user is not the superuser.
Packit 7cfc04
.TP
Packit 7cfc04
.B EADDRINUSE
Packit 7cfc04
The given address is already in use.
Packit 7cfc04
.TP
Packit 7cfc04
.B EADDRINUSE
Packit 7cfc04
(Internet domain sockets)
Packit 7cfc04
The port number was specified as zero in the socket address structure,
Packit 7cfc04
but, upon attempting to bind to an ephemeral port,
Packit 7cfc04
it was determined that all port numbers in the ephemeral port range
Packit 7cfc04
are currently in use.
Packit 7cfc04
See the discussion of
Packit 7cfc04
.I /proc/sys/net/ipv4/ip_local_port_range
Packit 7cfc04
.BR ip (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I sockfd
Packit 7cfc04
is not a valid file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The socket is already bound to an address.
Packit 7cfc04
.\" This may change in the future: see
Packit 7cfc04
.\" .I linux/unix/sock.c for details.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I addrlen
Packit 7cfc04
is wrong, or
Packit 7cfc04
.I addr
Packit 7cfc04
is not a valid address for this socket's domain.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTSOCK
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I sockfd
Packit 7cfc04
does not refer to a socket.
Packit 7cfc04
.PP
Packit 7cfc04
The following errors are specific to UNIX domain
Packit 7cfc04
.RB ( AF_UNIX )
Packit 7cfc04
sockets:
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
Search permission is denied on a component of the path prefix.
Packit 7cfc04
(See also
Packit 7cfc04
.BR path_resolution (7).)
Packit 7cfc04
.TP
Packit 7cfc04
.B EADDRNOTAVAIL
Packit 7cfc04
A nonexistent interface was requested or the requested
Packit 7cfc04
address was not local.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
.I addr
Packit 7cfc04
points outside the user's accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B ELOOP
Packit 7cfc04
Too many symbolic links were encountered in resolving
Packit 7cfc04
.IR addr .
Packit 7cfc04
.TP
Packit 7cfc04
.B ENAMETOOLONG
Packit 7cfc04
.I addr
Packit 7cfc04
is too long.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
A component in the directory prefix of the socket pathname does not exist.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Insufficient kernel memory was available.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTDIR
Packit 7cfc04
A component of the path prefix is not a directory.
Packit 7cfc04
.TP
Packit 7cfc04
.B EROFS
Packit 7cfc04
The socket inode would reside on a read-only filesystem.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
Packit 7cfc04
.RB ( bind ()
Packit 7cfc04
first appeared in 4.2BSD).
Packit 7cfc04
.\" SVr4 documents an additional
Packit 7cfc04
.\" .B ENOSR
Packit 7cfc04
.\" general error condition, and
Packit 7cfc04
.\" additional
Packit 7cfc04
.\" .B EIO
Packit 7cfc04
.\" and
Packit 7cfc04
.\" .B EISDIR
Packit 7cfc04
.\" UNIX-domain error conditions.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
POSIX.1 does not require the inclusion of
Packit 7cfc04
.IR <sys/types.h> ,
Packit 7cfc04
and this header file is not required on Linux.
Packit 7cfc04
However, some historical (BSD) implementations required this header
Packit 7cfc04
file, and portable applications are probably wise to include it.
Packit 7cfc04
.PP
Packit 7cfc04
For background on the
Packit 7cfc04
.I socklen_t
Packit 7cfc04
type, see
Packit 7cfc04
.BR accept (2).
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The transparent proxy options are not described.
Packit 7cfc04
.\" FIXME Document transparent proxy options
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
An example of the use of
Packit 7cfc04
.BR bind ()
Packit 7cfc04
with Internet domain sockets can be found in
Packit 7cfc04
.BR getaddrinfo (3).
Packit 7cfc04
.PP
Packit 7cfc04
The following example shows how to bind a stream socket in the UNIX
Packit 7cfc04
.RB ( AF_UNIX )
Packit 7cfc04
domain, and accept connections:
Packit 7cfc04
.\" listen.7 refers to this example.
Packit 7cfc04
.\" accept.7 refers to this example.
Packit 7cfc04
.\" unix.7 refers to this example.
Packit 7cfc04
.PP
Packit 7cfc04
.EX
Packit 7cfc04
#include <sys/socket.h>
Packit 7cfc04
#include <sys/un.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
Packit 7cfc04
#define MY_SOCK_PATH "/somepath"
Packit 7cfc04
#define LISTEN_BACKLOG 50
Packit 7cfc04
Packit 7cfc04
#define handle_error(msg) \\
Packit 7cfc04
    do { perror(msg); exit(EXIT_FAILURE); } while (0)
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    int sfd, cfd;
Packit 7cfc04
    struct sockaddr_un my_addr, peer_addr;
Packit 7cfc04
    socklen_t peer_addr_size;
Packit 7cfc04
Packit 7cfc04
    sfd = socket(AF_UNIX, SOCK_STREAM, 0);
Packit 7cfc04
    if (sfd == \-1)
Packit 7cfc04
        handle_error("socket");
Packit 7cfc04
Packit 7cfc04
    memset(&my_addr, 0, sizeof(struct sockaddr_un));
Packit 7cfc04
                        /* Clear structure */
Packit 7cfc04
    my_addr.sun_family = AF_UNIX;
Packit 7cfc04
    strncpy(my_addr.sun_path, MY_SOCK_PATH,
Packit 7cfc04
            sizeof(my_addr.sun_path) \- 1);
Packit 7cfc04
Packit 7cfc04
    if (bind(sfd, (struct sockaddr *) &my_addr,
Packit 7cfc04
            sizeof(struct sockaddr_un)) == \-1)
Packit 7cfc04
        handle_error("bind");
Packit 7cfc04
Packit 7cfc04
    if (listen(sfd, LISTEN_BACKLOG) == \-1)
Packit 7cfc04
        handle_error("listen");
Packit 7cfc04
Packit 7cfc04
    /* Now we can accept incoming connections one
Packit 7cfc04
       at a time using accept(2) */
Packit 7cfc04
Packit 7cfc04
    peer_addr_size = sizeof(struct sockaddr_un);
Packit 7cfc04
    cfd = accept(sfd, (struct sockaddr *) &peer_addr,
Packit 7cfc04
                 &peer_addr_size);
Packit 7cfc04
    if (cfd == \-1)
Packit 7cfc04
        handle_error("accept");
Packit 7cfc04
Packit 7cfc04
    /* Code to deal with incoming connection(s)... */
Packit 7cfc04
Packit 7cfc04
    /* When no longer required, the socket pathname, MY_SOCK_PATH
Packit 7cfc04
       should be deleted using unlink(2) or remove(3) */
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR accept (2),
Packit 7cfc04
.BR connect (2),
Packit 7cfc04
.BR getsockname (2),
Packit 7cfc04
.BR listen (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR getaddrinfo (3),
Packit 7cfc04
.BR getifaddrs (3),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR ipv6 (7),
Packit 7cfc04
.BR path_resolution (7),
Packit 7cfc04
.BR socket (7),
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.