]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/bind.2
mknod.2: tfix
[thirdparty/man-pages.git] / man2 / bind.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" and Copyright 2005-2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
4 .\" any authorship information in it. It is probably available under the GPL.
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .\"
29 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\"
31 .\" Copyright (c) 1983 The Regents of the University of California.
32 .\" All rights reserved.
33 .\"
34 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
35 .\" Redistribution and use in source and binary forms, with or without
36 .\" modification, are permitted provided that the following conditions
37 .\" are met:
38 .\" 1. Redistributions of source code must retain the above copyright
39 .\" notice, this list of conditions and the following disclaimer.
40 .\" 2. Redistributions in binary form must reproduce the above copyright
41 .\" notice, this list of conditions and the following disclaimer in the
42 .\" documentation and/or other materials provided with the distribution.
43 .\" 3. All advertising materials mentioning features or use of this software
44 .\" must display the following acknowledgement:
45 .\" This product includes software developed by the University of
46 .\" California, Berkeley and its contributors.
47 .\" 4. Neither the name of the University nor the names of its contributors
48 .\" may be used to endorse or promote products derived from this software
49 .\" without specific prior written permission.
50 .\"
51 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 .\" SUCH DAMAGE.
62 .\" %%%LICENSE_END
63 .\"
64 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
65 .\" Modified 1998 by Andi Kleen
66 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
67 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
68 .\"
69 .TH BIND 2 2019-03-06 "Linux" "Linux Programmer's Manual"
70 .SH NAME
71 bind \- bind a name to a socket
72 .SH SYNOPSIS
73 .nf
74 .BR "#include <sys/types.h>" " /* See NOTES */"
75 .B #include <sys/socket.h>
76 .PP
77 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
78 .BI " socklen_t " addrlen );
79 .fi
80 .SH DESCRIPTION
81 When a socket is created with
82 .BR socket (2),
83 it exists in a name space (address family) but has no address assigned to it.
84 .BR bind ()
85 assigns the address specified by
86 .I addr
87 to the socket referred to by the file descriptor
88 .IR sockfd .
89 .I addrlen
90 specifies the size, in bytes, of the address structure pointed to by
91 .IR addr .
92 Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
93 .PP
94 It is normally necessary to assign a local address using
95 .BR bind ()
96 before a
97 .B SOCK_STREAM
98 socket may receive connections (see
99 .BR accept (2)).
100 .PP
101 The rules used in name binding vary between address families.
102 Consult the manual entries in Section 7 for detailed information.
103 For
104 .BR AF_INET ,
105 see
106 .BR ip (7);
107 for
108 .BR AF_INET6 ,
109 see
110 .BR ipv6 (7);
111 for
112 .BR AF_UNIX ,
113 see
114 .BR unix (7);
115 for
116 .BR AF_APPLETALK ,
117 see
118 .BR ddp (7);
119 for
120 .BR AF_PACKET ,
121 see
122 .BR packet (7);
123 for
124 .BR AF_X25 ,
125 see
126 .BR x25 (7);
127 and for
128 .BR AF_NETLINK ,
129 see
130 .BR netlink (7).
131 .PP
132 The actual structure passed for the
133 .I addr
134 argument will depend on the address family.
135 The
136 .I sockaddr
137 structure is defined as something like:
138 .PP
139 .in +4n
140 .EX
141 struct sockaddr {
142 sa_family_t sa_family;
143 char sa_data[14];
144 }
145 .EE
146 .in
147 .PP
148 The only purpose of this structure is to cast the structure
149 pointer passed in
150 .I addr
151 in order to avoid compiler warnings.
152 See EXAMPLE below.
153 .SH RETURN VALUE
154 On success, zero is returned.
155 On error, \-1 is returned, and
156 .I errno
157 is set appropriately.
158 .SH ERRORS
159 .TP
160 .B EACCES
161 .\" e.g., privileged port in AF_INET domain
162 The address is protected, and the user is not the superuser.
163 .TP
164 .B EADDRINUSE
165 The given address is already in use.
166 .TP
167 .B EADDRINUSE
168 (Internet domain sockets)
169 The port number was specified as zero in the socket address structure,
170 but, upon attempting to bind to an ephemeral port,
171 it was determined that all port numbers in the ephemeral port range
172 are currently in use.
173 See the discussion of
174 .I /proc/sys/net/ipv4/ip_local_port_range
175 .BR ip (7).
176 .TP
177 .B EBADF
178 .I sockfd
179 is not a valid file descriptor.
180 .TP
181 .B EINVAL
182 The socket is already bound to an address.
183 .\" This may change in the future: see
184 .\" .I linux/unix/sock.c for details.
185 .TP
186 .B EINVAL
187 .I addrlen
188 is wrong, or
189 .I addr
190 is not a valid address for this socket's domain.
191 .TP
192 .B ENOTSOCK
193 The file descriptor
194 .I sockfd
195 does not refer to a socket.
196 .PP
197 The following errors are specific to UNIX domain
198 .RB ( AF_UNIX )
199 sockets:
200 .TP
201 .B EACCES
202 Search permission is denied on a component of the path prefix.
203 (See also
204 .BR path_resolution (7).)
205 .TP
206 .B EADDRNOTAVAIL
207 A nonexistent interface was requested or the requested
208 address was not local.
209 .TP
210 .B EFAULT
211 .I addr
212 points outside the user's accessible address space.
213 .TP
214 .B ELOOP
215 Too many symbolic links were encountered in resolving
216 .IR addr .
217 .TP
218 .B ENAMETOOLONG
219 .I addr
220 is too long.
221 .TP
222 .B ENOENT
223 A component in the directory prefix of the socket pathname does not exist.
224 .TP
225 .B ENOMEM
226 Insufficient kernel memory was available.
227 .TP
228 .B ENOTDIR
229 A component of the path prefix is not a directory.
230 .TP
231 .B EROFS
232 The socket inode would reside on a read-only filesystem.
233 .SH CONFORMING TO
234 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
235 .RB ( bind ()
236 first appeared in 4.2BSD).
237 .\" SVr4 documents an additional
238 .\" .B ENOSR
239 .\" general error condition, and
240 .\" additional
241 .\" .B EIO
242 .\" and
243 .\" .B EISDIR
244 .\" UNIX-domain error conditions.
245 .SH NOTES
246 POSIX.1 does not require the inclusion of
247 .IR <sys/types.h> ,
248 and this header file is not required on Linux.
249 However, some historical (BSD) implementations required this header
250 file, and portable applications are probably wise to include it.
251 .PP
252 For background on the
253 .I socklen_t
254 type, see
255 .BR accept (2).
256 .SH BUGS
257 The transparent proxy options are not described.
258 .\" FIXME Document transparent proxy options
259 .SH EXAMPLE
260 An example of the use of
261 .BR bind ()
262 with Internet domain sockets can be found in
263 .BR getaddrinfo (3).
264 .PP
265 The following example shows how to bind a stream socket in the UNIX
266 .RB ( AF_UNIX )
267 domain, and accept connections:
268 .\" listen.7 refers to this example.
269 .\" accept.7 refers to this example.
270 .\" unix.7 refers to this example.
271 .PP
272 .EX
273 #include <sys/socket.h>
274 #include <sys/un.h>
275 #include <stdlib.h>
276 #include <stdio.h>
277 #include <string.h>
278
279 #define MY_SOCK_PATH "/somepath"
280 #define LISTEN_BACKLOG 50
281
282 #define handle_error(msg) \e
283 do { perror(msg); exit(EXIT_FAILURE); } while (0)
284
285 int
286 main(int argc, char *argv[])
287 {
288 int sfd, cfd;
289 struct sockaddr_un my_addr, peer_addr;
290 socklen_t peer_addr_size;
291
292 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
293 if (sfd == \-1)
294 handle_error("socket");
295
296 memset(&my_addr, 0, sizeof(struct sockaddr_un));
297 /* Clear structure */
298 my_addr.sun_family = AF_UNIX;
299 strncpy(my_addr.sun_path, MY_SOCK_PATH,
300 sizeof(my_addr.sun_path) \- 1);
301
302 if (bind(sfd, (struct sockaddr *) &my_addr,
303 sizeof(struct sockaddr_un)) == \-1)
304 handle_error("bind");
305
306 if (listen(sfd, LISTEN_BACKLOG) == \-1)
307 handle_error("listen");
308
309 /* Now we can accept incoming connections one
310 at a time using accept(2) */
311
312 peer_addr_size = sizeof(struct sockaddr_un);
313 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
314 &peer_addr_size);
315 if (cfd == \-1)
316 handle_error("accept");
317
318 /* Code to deal with incoming connection(s)... */
319
320 /* When no longer required, the socket pathname, MY_SOCK_PATH
321 should be deleted using unlink(2) or remove(3) */
322 }
323 .EE
324 .SH SEE ALSO
325 .BR accept (2),
326 .BR connect (2),
327 .BR getsockname (2),
328 .BR listen (2),
329 .BR socket (2),
330 .BR getaddrinfo (3),
331 .BR getifaddrs (3),
332 .BR ip (7),
333 .BR ipv6 (7),
334 .BR path_resolution (7),
335 .BR socket (7),
336 .BR unix (7)