1.\" $NetBSD: bind.2,v 1.15 2000/01/26 10:43:36 itojun Exp $ 2.\" 3.\" Copyright (c) 1983, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)bind.2 8.1 (Berkeley) 6/4/93 35.\" 36.Dd March 23, 1999 37.Dt BIND 2 38.Os 39.Sh NAME 40.Nm bind 41.Nd bind a name to a socket 42.Sh LIBRARY 43.Lb libc 44.Sh SYNOPSIS 45.Fd #include <sys/types.h> 46.Fd #include <sys/socket.h> 47.Ft int 48.Fn bind "int s" "const struct sockaddr *name" "socklen_t namelen" 49.Sh DESCRIPTION 50.Fn bind 51assigns a name to an unnamed socket. 52When a socket is created 53with 54.Xr socket 2 55it exists in a name space (address family) 56but has no name assigned. 57.Fn bind 58requests that 59.Fa name 60be assigned to the socket. 61.Sh NOTES 62Binding a name in the 63.Ux 64domain creates a socket in the file 65system that must be deleted by the caller when it is no longer 66needed (using 67.Xr unlink 2 ) . 68.Pp 69The rules used in name binding vary between communication domains. 70Consult the manual entries in section 4 for detailed information. 71.Sh RETURN VALUES 72If the bind is successful, a 0 value is returned. 73A return value of -1 indicates an error, which is 74further specified in the global 75.Va errno . 76.Sh ERRORS 77The 78.Fn bind 79call will fail if: 80.Bl -tag -width Er 81.It Bq Er EBADF 82.Fa s 83is not a valid descriptor. 84.It Bq Er ENOTSOCK 85.Fa s 86is not a socket. 87.It Bq Er EADDRNOTAVAIL 88The specified address is not available from the local machine. 89.It Bq Er EADDRINUSE 90The specified address is already in use. 91.It Bq Er EINVAL 92The socket is already bound to an address. 93.It Bq Er EINVAL 94The family of the socket and that requested in 95.Fa name->sa_family 96are not equivalent. 97.It Bq Er EACCES 98The requested address is protected, and the current user 99has inadequate permission to access it. 100.It Bq Er EFAULT 101The 102.Fa name 103parameter is not in a valid part of the user 104address space. 105.El 106.Pp 107The following errors are specific to binding names in the 108.Ux 109domain. 110.Bl -tag -width Er 111.It Bq Er ENOTDIR 112A component of the path prefix is not a directory. 113.It Bq Er ENAMETOOLONG 114A component of a pathname exceeded 115.Dv {NAME_MAX} 116characters, or an entire path name exceeded 117.Dv {PATH_MAX} 118characters. 119.It Bq Er ENOENT 120A prefix component of the path name does not exist. 121.It Bq Er ELOOP 122Too many symbolic links were encountered in translating the pathname. 123.It Bq Er EIO 124An I/O error occurred while making the directory entry or allocating the inode. 125.It Bq Er EROFS 126The name would reside on a read-only file system. 127.It Bq Er EISDIR 128An empty pathname was specified. 129.El 130.Sh SEE ALSO 131.Xr connect 2 , 132.Xr listen 2 , 133.Xr socket 2 , 134.Xr getsockname 2 135.Sh HISTORY 136The 137.Fn bind 138function call appeared in 139.Bx 4.2 . 140.Sh SECURITY CONSIDERATIONS 141.Fn bind 142was changed in 143.Nx 1.4 144to prevent the binding of a socket to the same port as an existing 145socket when all of the following is true: 146.Bl -bullet -compact -offset indent 147.It 148either of the existing or new addresses is 149.Li INADDR_ANY , 150.It 151the uid of the new socket is not root, and 152the uids of the creators of the sockets are different, 153.It 154the address is not a multicast address, and 155.It 156both sockets are not bound to 157.Li INADDR_ANY 158with 159.Li SO_REUSEPORT 160set. 161.El 162.Pp 163This prevents an attack where a user could bind to a port with 164the host's IP address (after setting 165.Li SO_REUSEADDR ) 166and 167.Sq steal 168packets destined for a server that bound to the 169same port with 170.Li INADDR_ANY . 171