1*0a6a1f1dSLionel Sambuc.\" $NetBSD: bind.2,v 1.29 2015/04/05 20:41:05 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)bind.2 8.1 (Berkeley) 6/4/93 312fe8fb19SBen Gras.\" 32*0a6a1f1dSLionel Sambuc.Dd April 5, 2015 332fe8fb19SBen Gras.Dt BIND 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm bind 372fe8fb19SBen Gras.Nd bind a name to a socket 382fe8fb19SBen Gras.Sh LIBRARY 392fe8fb19SBen Gras.Lb libc 402fe8fb19SBen Gras.Sh SYNOPSIS 412fe8fb19SBen Gras.In sys/socket.h 422fe8fb19SBen Gras.Ft int 432fe8fb19SBen Gras.Fn bind "int s" "const struct sockaddr *name" "socklen_t namelen" 442fe8fb19SBen Gras.Sh DESCRIPTION 452fe8fb19SBen Gras.Fn bind 462fe8fb19SBen Grasassigns a name to an unnamed socket. 472fe8fb19SBen GrasWhen a socket is created 482fe8fb19SBen Graswith 492fe8fb19SBen Gras.Xr socket 2 502fe8fb19SBen Grasit exists in a name space (address family) 512fe8fb19SBen Grasbut has no name assigned. 522fe8fb19SBen Gras.Fn bind 532fe8fb19SBen Grasrequests that 542fe8fb19SBen Gras.Fa name 552fe8fb19SBen Grasbe assigned to the socket. 562fe8fb19SBen Gras.Fa namelen 572fe8fb19SBen Grasindicates the amount of space pointed to by 582fe8fb19SBen Gras.Fa name , 592fe8fb19SBen Grasin bytes. 602fe8fb19SBen Gras.Sh NOTES 612fe8fb19SBen GrasBinding a name in the 622fe8fb19SBen Gras.Ux 632fe8fb19SBen Grasdomain creates a socket in the file 642fe8fb19SBen Grassystem that must be deleted by the caller when it is no longer 652fe8fb19SBen Grasneeded (using 662fe8fb19SBen Gras.Xr unlink 2 ) . 672fe8fb19SBen Gras.Pp 682fe8fb19SBen GrasThe rules used in name binding vary between communication domains. 692fe8fb19SBen GrasConsult the manual entries in section 4 for detailed information. 702fe8fb19SBen Gras.Sh RETURN VALUES 712fe8fb19SBen GrasIf the bind is successful, a 0 value is returned. 722fe8fb19SBen GrasA return value of \-1 indicates an error, which is 732fe8fb19SBen Grasfurther specified in the global 742fe8fb19SBen Gras.Va errno . 752fe8fb19SBen Gras.Sh ERRORS 762fe8fb19SBen GrasThe 772fe8fb19SBen Gras.Fn bind 782fe8fb19SBen Grascall will fail if: 792fe8fb19SBen Gras.Bl -tag -width Er 802fe8fb19SBen Gras.It Bq Er EACCES 812fe8fb19SBen GrasThe requested address is protected, and the current user 822fe8fb19SBen Grashas inadequate permission to access it. 83*0a6a1f1dSLionel Sambuc.It Bq Er EADDRINUSE 84*0a6a1f1dSLionel SambucThe specified address is already in use. 85*0a6a1f1dSLionel Sambuc.It Bq Er EADDRNOTAVAIL 86*0a6a1f1dSLionel SambucThe specified address is not available from the local machine. 87*0a6a1f1dSLionel Sambuc.It Bq Er EAFNOSUPPORT 88*0a6a1f1dSLionel SambucAddresses in the specified address family cannot be used with this socket. 89*0a6a1f1dSLionel Sambuc.It Bq Er EBADF 90*0a6a1f1dSLionel Sambuc.Fa s 91*0a6a1f1dSLionel Sambucis not a valid descriptor. 922fe8fb19SBen Gras.It Bq Er EFAULT 932fe8fb19SBen GrasThe 942fe8fb19SBen Gras.Fa name 952fe8fb19SBen Grasparameter is not in a valid part of the user 962fe8fb19SBen Grasaddress space. 97*0a6a1f1dSLionel Sambuc.It Bq Er EINVAL 98*0a6a1f1dSLionel SambucThe socket is already bound to an address; or 99*0a6a1f1dSLionel Sambucthe family of the socket and that requested in 100*0a6a1f1dSLionel Sambuc.Fa name-\*[Gt]sa_family 101*0a6a1f1dSLionel Sambucare not equivalent. 102*0a6a1f1dSLionel Sambuc.It Bq Er ENOTSOCK 103*0a6a1f1dSLionel Sambuc.Fa s 104*0a6a1f1dSLionel Sambucis not a socket. 1052fe8fb19SBen Gras.El 1062fe8fb19SBen Gras.Pp 1072fe8fb19SBen GrasThe following errors are specific to binding names in the 1082fe8fb19SBen Gras.Ux 1092fe8fb19SBen Grasdomain. 1102fe8fb19SBen Gras.Bl -tag -width Er 111*0a6a1f1dSLionel Sambuc.It Bq Er EIO 112*0a6a1f1dSLionel SambucAn I/O error occurred while making the directory entry or allocating the inode. 113*0a6a1f1dSLionel Sambuc.It Bq Er EISDIR 114*0a6a1f1dSLionel SambucAn empty pathname was specified. 115*0a6a1f1dSLionel Sambuc.It Bq Er ELOOP 116*0a6a1f1dSLionel SambucToo many symbolic links were encountered in translating the pathname. 1172fe8fb19SBen Gras.It Bq Er ENAMETOOLONG 1182fe8fb19SBen GrasA component of a pathname exceeded 1192fe8fb19SBen Gras.Brq Dv NAME_MAX 1202fe8fb19SBen Grascharacters, or an entire path name exceeded 1212fe8fb19SBen Gras.Brq Dv PATH_MAX 1222fe8fb19SBen Grascharacters. 1232fe8fb19SBen Gras.It Bq Er ENOENT 1242fe8fb19SBen GrasA prefix component of the path name does not exist. 125*0a6a1f1dSLionel Sambuc.It Bq Er ENOTDIR 126*0a6a1f1dSLionel SambucA component of the path prefix is not a directory. 1272fe8fb19SBen Gras.It Bq Er EROFS 1282fe8fb19SBen GrasThe name would reside on a read-only file system. 1292fe8fb19SBen Gras.El 1302fe8fb19SBen Gras.Sh SEE ALSO 1312fe8fb19SBen Gras.Xr connect 2 , 1322fe8fb19SBen Gras.Xr getsockname 2 , 1332fe8fb19SBen Gras.Xr listen 2 , 1342fe8fb19SBen Gras.Xr socket 2 1352fe8fb19SBen Gras.Sh HISTORY 1362fe8fb19SBen GrasThe 1372fe8fb19SBen Gras.Fn bind 1382fe8fb19SBen Grasfunction call appeared in 1392fe8fb19SBen Gras.Bx 4.2 . 1402fe8fb19SBen Gras.Sh SECURITY CONSIDERATIONS 1412fe8fb19SBen Gras.Fn bind 1422fe8fb19SBen Graswas changed in 1432fe8fb19SBen Gras.Nx 1.4 1442fe8fb19SBen Grasto prevent the binding of a socket to the same port as an existing 1452fe8fb19SBen Grassocket when all of the following is true: 1462fe8fb19SBen Gras.Bl -bullet -compact -offset indent 1472fe8fb19SBen Gras.It 1482fe8fb19SBen Graseither of the existing or new addresses is 1492fe8fb19SBen Gras.Li INADDR_ANY , 1502fe8fb19SBen Gras.It 1512fe8fb19SBen Grasthe uid of the new socket is not root, and 1522fe8fb19SBen Grasthe uids of the creators of the sockets are different, 1532fe8fb19SBen Gras.It 1542fe8fb19SBen Grasthe address is not a multicast address, and 1552fe8fb19SBen Gras.It 1562fe8fb19SBen Grasboth sockets are not bound to 1572fe8fb19SBen Gras.Li INADDR_ANY 1582fe8fb19SBen Graswith 1592fe8fb19SBen Gras.Li SO_REUSEPORT 1602fe8fb19SBen Grasset. 1612fe8fb19SBen Gras.El 1622fe8fb19SBen Gras.Pp 1632fe8fb19SBen GrasThis prevents an attack where a user could bind to a port with 1642fe8fb19SBen Grasthe host's IP address (after setting 1652fe8fb19SBen Gras.Li SO_REUSEADDR ) 1662fe8fb19SBen Grasand 1672fe8fb19SBen Gras.Sq steal 1682fe8fb19SBen Graspackets destined for a server that bound to the 1692fe8fb19SBen Grassame port with 1702fe8fb19SBen Gras.Li INADDR_ANY . 1712fe8fb19SBen Gras.Pp 1722fe8fb19SBen Gras.Fn bind 1732fe8fb19SBen Graswas changed in 1742fe8fb19SBen Gras.Nx 4.0 1752fe8fb19SBen Grasto honor the user's umask when binding sockets in the local domain. 1762fe8fb19SBen GrasThis was done to match the behavior of other operating systems, 1772fe8fb19SBen Grasincluding 1782fe8fb19SBen Gras.Fx , 1792fe8fb19SBen Gras.Ox , 1802fe8fb19SBen Grasand 1812fe8fb19SBen GrasLinux, and to improve compatibility with some third-party software. 1822fe8fb19SBen GrasPlease note that this behavior 1832fe8fb19SBen Gras.Em is not portable . 1842fe8fb19SBen GrasIf you must bind a local socket in a portable and secure way, you need 1852fe8fb19SBen Grasto make a directory with tight permissions and then create the socket 1862fe8fb19SBen Grasinside it. 187