console_codes.4: tfix
[man-pages.git] / man2 / bind.2
blob14da79d52678d3a8a8f625207b5558af7ae9ce40
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 2021-03-22 "Linux" "Linux Programmer's Manual"
70 .SH NAME
71 bind \- bind a name to a socket
72 .SH SYNOPSIS
73 .nf
74 .B #include <sys/socket.h>
75 .PP
76 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
77 .BI "         socklen_t " addrlen );
78 .fi
79 .SH DESCRIPTION
80 When a socket is created with
81 .BR socket (2),
82 it exists in a name space (address family) but has no address assigned to it.
83 .BR bind ()
84 assigns the address specified by
85 .I addr
86 to the socket referred to by the file descriptor
87 .IR sockfd .
88 .I addrlen
89 specifies the size, in bytes, of the address structure pointed to by
90 .IR addr .
91 Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
92 .PP
93 It is normally necessary to assign a local address using
94 .BR bind ()
95 before a
96 .B SOCK_STREAM
97 socket may receive connections (see
98 .BR accept (2)).
99 .PP
100 The rules used in name binding vary between address families.
101 Consult the manual entries in Section 7 for detailed information.
103 .BR AF_INET ,
105 .BR ip (7);
107 .BR AF_INET6 ,
109 .BR ipv6 (7);
111 .BR AF_UNIX ,
113 .BR unix (7);
115 .BR AF_APPLETALK ,
117 .BR ddp (7);
119 .BR AF_PACKET ,
121 .BR packet (7);
123 .BR AF_X25 ,
125 .BR x25 (7);
126 and for
127 .BR AF_NETLINK ,
129 .BR netlink (7).
131 The actual structure passed for the
132 .I addr
133 argument will depend on the address family.
135 .I sockaddr
136 structure is defined as something like:
138 .in +4n
140 struct sockaddr {
141     sa_family_t sa_family;
142     char        sa_data[14];
147 The only purpose of this structure is to cast the structure
148 pointer passed in
149 .I addr
150 in order to avoid compiler warnings.
151 See EXAMPLES below.
152 .SH RETURN VALUE
153 On success, zero is returned.
154 On error, \-1 is returned, and
155 .I errno
156 is set to indicate the error.
157 .SH ERRORS
159 .B EACCES
160 .\" e.g., privileged port in AF_INET domain
161 The address is protected, and the user is not the superuser.
163 .B EADDRINUSE
164 The given address is already in use.
166 .B EADDRINUSE
167 (Internet domain sockets)
168 The port number was specified as zero in the socket address structure,
169 but, upon attempting to bind to an ephemeral port,
170 it was determined that all port numbers in the ephemeral port range
171 are currently in use.
172 See the discussion of
173 .I /proc/sys/net/ipv4/ip_local_port_range
174 .BR ip (7).
176 .B EBADF
177 .I sockfd
178 is not a valid file descriptor.
180 .B EINVAL
181 The socket is already bound to an address.
182 .\" This may change in the future: see
183 .\" .I linux/unix/sock.c for details.
185 .B EINVAL
186 .I addrlen
187 is wrong, or
188 .I addr
189 is not a valid address for this socket's domain.
191 .B ENOTSOCK
192 The file descriptor
193 .I sockfd
194 does not refer to a socket.
196 The following errors are specific to UNIX domain
197 .RB ( AF_UNIX )
198 sockets:
200 .B EACCES
201 Search permission is denied on a component of the path prefix.
202 (See also
203 .BR path_resolution (7).)
205 .B EADDRNOTAVAIL
206 A nonexistent interface was requested or the requested
207 address was not local.
209 .B EFAULT
210 .I addr
211 points outside the user's accessible address space.
213 .B ELOOP
214 Too many symbolic links were encountered in resolving
215 .IR addr .
217 .B ENAMETOOLONG
218 .I addr
219 is too long.
221 .B ENOENT
222 A component in the directory prefix of the socket pathname does not exist.
224 .B ENOMEM
225 Insufficient kernel memory was available.
227 .B ENOTDIR
228 A component of the path prefix is not a directory.
230 .B EROFS
231 The socket inode would reside on a read-only filesystem.
232 .SH CONFORMING TO
233 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD
234 .RB ( bind ()
235 first appeared in 4.2BSD).
236 .\" SVr4 documents an additional
237 .\" .B ENOSR
238 .\" general error condition, and
239 .\" additional
240 .\" .B EIO
241 .\" and
242 .\" .B EISDIR
243 .\" UNIX-domain error conditions.
244 .SH NOTES
245 For background on the
246 .I socklen_t
247 type, see
248 .BR accept (2).
249 .SH BUGS
250 The transparent proxy options are not described.
251 .\" FIXME Document transparent proxy options
252 .SH EXAMPLES
253 An example of the use of
254 .BR bind ()
255 with Internet domain sockets can be found in
256 .BR getaddrinfo (3).
258 The following example shows how to bind a stream socket in the UNIX
259 .RB ( AF_UNIX )
260 domain, and accept connections:
261 .\" listen.7 refers to this example.
262 .\" accept.7 refers to this example.
263 .\" unix.7 refers to this example.
266 #include <sys/socket.h>
267 #include <sys/un.h>
268 #include <stdlib.h>
269 #include <stdio.h>
270 #include <string.h>
272 #define MY_SOCK_PATH "/somepath"
273 #define LISTEN_BACKLOG 50
275 #define handle_error(msg) \e
276     do { perror(msg); exit(EXIT_FAILURE); } while (0)
279 main(int argc, char *argv[])
281     int sfd, cfd;
282     struct sockaddr_un my_addr, peer_addr;
283     socklen_t peer_addr_size;
285     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
286     if (sfd == \-1)
287         handle_error("socket");
289     memset(&my_addr, 0, sizeof(my_addr));
290     my_addr.sun_family = AF_UNIX;
291     strncpy(my_addr.sun_path, MY_SOCK_PATH,
292             sizeof(my_addr.sun_path) \- 1);
294     if (bind(sfd, (struct sockaddr *) &my_addr,
295             sizeof(my_addr)) == \-1)
296         handle_error("bind");
298     if (listen(sfd, LISTEN_BACKLOG) == \-1)
299         handle_error("listen");
301     /* Now we can accept incoming connections one
302        at a time using accept(2). */
304     peer_addr_size = sizeof(peer_addr);
305     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
306                  &peer_addr_size);
307     if (cfd == \-1)
308         handle_error("accept");
310     /* Code to deal with incoming connection(s)... */
312     /* When no longer required, the socket pathname, MY_SOCK_PATH
313        should be deleted using unlink(2) or remove(3). */
316 .SH SEE ALSO
317 .BR accept (2),
318 .BR connect (2),
319 .BR getsockname (2),
320 .BR listen (2),
321 .BR socket (2),
322 .BR getaddrinfo (3),
323 .BR getifaddrs (3),
324 .BR ip (7),
325 .BR ipv6 (7),
326 .BR path_resolution (7),
327 .BR socket (7),
328 .BR unix (7)