etc/services - sync with NetBSD-8
[minix.git] / lib / libc / sys / bind.2
blobfa3ca67311e03ecb1c8fc45c9af47e0857f6597a
1 .\"     $NetBSD: bind.2,v 1.29 2015/04/05 20:41:05 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. Neither the name of the University nor the names of its contributors
15 .\"    may be used to endorse or promote products derived from this software
16 .\"    without specific prior written permission.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" SUCH DAMAGE.
29 .\"
30 .\"     @(#)bind.2      8.1 (Berkeley) 6/4/93
31 .\"
32 .Dd April 5, 2015
33 .Dt BIND 2
34 .Os
35 .Sh NAME
36 .Nm bind
37 .Nd bind a name to a socket
38 .Sh LIBRARY
39 .Lb libc
40 .Sh SYNOPSIS
41 .In sys/socket.h
42 .Ft int
43 .Fn bind "int s" "const struct sockaddr *name" "socklen_t namelen"
44 .Sh DESCRIPTION
45 .Fn bind
46 assigns a name to an unnamed socket.
47 When a socket is created
48 with
49 .Xr socket 2
50 it exists in a name space (address family)
51 but has no name assigned.
52 .Fn bind
53 requests that
54 .Fa name
55 be assigned to the socket.
56 .Fa namelen
57 indicates the amount of space pointed to by
58 .Fa name ,
59 in bytes.
60 .Sh NOTES
61 Binding a name in the
62 .Ux
63 domain creates a socket in the file
64 system that must be deleted by the caller when it is no longer
65 needed (using
66 .Xr unlink 2 ) .
67 .Pp
68 The rules used in name binding vary between communication domains.
69 Consult the manual entries in section 4 for detailed information.
70 .Sh RETURN VALUES
71 If the bind is successful, a 0 value is returned.
72 A return value of \-1 indicates an error, which is
73 further specified in the global
74 .Va errno .
75 .Sh ERRORS
76 The
77 .Fn bind
78 call will fail if:
79 .Bl -tag -width Er
80 .It Bq Er EACCES
81 The requested address is protected, and the current user
82 has inadequate permission to access it.
83 .It Bq Er EADDRINUSE
84 The specified address is already in use.
85 .It Bq Er EADDRNOTAVAIL
86 The specified address is not available from the local machine.
87 .It Bq Er EAFNOSUPPORT
88 Addresses in the specified address family cannot be used with this socket.
89 .It Bq Er EBADF
90 .Fa s
91 is not a valid descriptor.
92 .It Bq Er EFAULT
93 The
94 .Fa name
95 parameter is not in a valid part of the user
96 address space.
97 .It Bq Er EINVAL
98 The socket is already bound to an address; or
99 the family of the socket and that requested in
100 .Fa name-\*[Gt]sa_family
101 are not equivalent.
102 .It Bq Er ENOTSOCK
103 .Fa s
104 is not a socket.
107 The following errors are specific to binding names in the
109 domain.
110 .Bl -tag -width Er
111 .It Bq Er EIO
112 An I/O error occurred while making the directory entry or allocating the inode.
113 .It Bq Er EISDIR
114 An empty pathname was specified.
115 .It Bq Er ELOOP
116 Too many symbolic links were encountered in translating the pathname.
117 .It Bq Er ENAMETOOLONG
118 A component of a pathname exceeded
119 .Brq Dv NAME_MAX
120 characters, or an entire path name exceeded
121 .Brq Dv PATH_MAX
122 characters.
123 .It Bq Er ENOENT
124 A prefix component of the path name does not exist.
125 .It Bq Er ENOTDIR
126 A component of the path prefix is not a directory.
127 .It Bq Er EROFS
128 The name would reside on a read-only file system.
130 .Sh SEE ALSO
131 .Xr connect 2 ,
132 .Xr getsockname 2 ,
133 .Xr listen 2 ,
134 .Xr socket 2
135 .Sh HISTORY
137 .Fn bind
138 function call appeared in
139 .Bx 4.2 .
140 .Sh SECURITY CONSIDERATIONS
141 .Fn bind
142 was changed in
143 .Nx 1.4
144 to prevent the binding of a socket to the same port as an existing
145 socket when all of the following is true:
146 .Bl -bullet -compact -offset indent
148 either of the existing or new addresses is
149 .Li INADDR_ANY ,
151 the uid of the new socket is not root, and
152 the uids of the creators of the sockets are different,
154 the address is not a multicast address, and
156 both sockets are not bound to
157 .Li INADDR_ANY
158 with
159 .Li SO_REUSEPORT
160 set.
163 This prevents an attack where a user could bind to a port with
164 the host's IP address (after setting
165 .Li SO_REUSEADDR )
167 .Sq steal
168 packets destined for a server that bound to the
169 same port with
170 .Li INADDR_ANY .
172 .Fn bind
173 was changed in
174 .Nx 4.0
175 to honor the user's umask when binding sockets in the local domain.
176 This was done to match the behavior of other operating systems,
177 including
178 .Fx ,
179 .Ox ,
181 Linux, and to improve compatibility with some third-party software.
182 Please note that this behavior
183 .Em is not portable .
184 If you must bind a local socket in a portable and secure way, you need
185 to make a directory with tight permissions and then create the socket
186 inside it.