1.\" $NetBSD: bind.2,v 1.22 2003/04/16 13:34:51 wiz 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 October 16, 2001 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.In sys/socket.h 46.Ft int 47.Fn bind "int s" "const struct sockaddr *name" "socklen_t namelen" 48.Sh DESCRIPTION 49.Fn bind 50assigns a name to an unnamed socket. 51When a socket is created 52with 53.Xr socket 2 54it exists in a name space (address family) 55but has no name assigned. 56.Fn bind 57requests that 58.Fa name 59be assigned to the socket. 60.Fa namelen 61indicates the amount of space pointed to by 62.Fa name , 63in bytes. 64.Sh NOTES 65Binding a name in the 66.Ux 67domain creates a socket in the file 68system that must be deleted by the caller when it is no longer 69needed (using 70.Xr unlink 2 ) . 71.Pp 72The rules used in name binding vary between communication domains. 73Consult the manual entries in section 4 for detailed information. 74.Sh RETURN VALUES 75If the bind is successful, a 0 value is returned. 76A return value of -1 indicates an error, which is 77further specified in the global 78.Va errno . 79.Sh ERRORS 80The 81.Fn bind 82call will fail if: 83.Bl -tag -width Er 84.It Bq Er EBADF 85.Fa s 86is not a valid descriptor. 87.It Bq Er ENOTSOCK 88.Fa s 89is not a socket. 90.It Bq Er EADDRNOTAVAIL 91The specified address is not available from the local machine. 92.It Bq Er EADDRINUSE 93The specified address is already in use. 94.It Bq Er EINVAL 95The socket is already bound to an address. 96.It Bq Er EINVAL 97The family of the socket and that requested in 98.Fa name-\*[Gt]sa_family 99are not equivalent. 100.It Bq Er EACCES 101The requested address is protected, and the current user 102has inadequate permission to access it. 103.It Bq Er EFAULT 104The 105.Fa name 106parameter is not in a valid part of the user 107address space. 108.El 109.Pp 110The following errors are specific to binding names in the 111.Ux 112domain. 113.Bl -tag -width Er 114.It Bq Er ENOTDIR 115A component of the path prefix is not a directory. 116.It Bq Er ENAMETOOLONG 117A component of a pathname exceeded 118.Dv {NAME_MAX} 119characters, or an entire path name exceeded 120.Dv {PATH_MAX} 121characters. 122.It Bq Er ENOENT 123A prefix component of the path name does not exist. 124.It Bq Er ELOOP 125Too many symbolic links were encountered in translating the pathname. 126.It Bq Er EIO 127An I/O error occurred while making the directory entry or allocating the inode. 128.It Bq Er EROFS 129The name would reside on a read-only file system. 130.It Bq Er EISDIR 131An empty pathname was specified. 132.El 133.Sh SEE ALSO 134.Xr connect 2 , 135.Xr getsockname 2 , 136.Xr listen 2 , 137.Xr socket 2 138.Sh HISTORY 139The 140.Fn bind 141function call appeared in 142.Bx 4.2 . 143.Sh SECURITY CONSIDERATIONS 144.Fn bind 145was changed in 146.Nx 1.4 147to prevent the binding of a socket to the same port as an existing 148socket when all of the following is true: 149.Bl -bullet -compact -offset indent 150.It 151either of the existing or new addresses is 152.Li INADDR_ANY , 153.It 154the uid of the new socket is not root, and 155the uids of the creators of the sockets are different, 156.It 157the address is not a multicast address, and 158.It 159both sockets are not bound to 160.Li INADDR_ANY 161with 162.Li SO_REUSEPORT 163set. 164.El 165.Pp 166This prevents an attack where a user could bind to a port with 167the host's IP address (after setting 168.Li SO_REUSEADDR ) 169and 170.Sq steal 171packets destined for a server that bound to the 172same port with 173.Li INADDR_ANY . 174