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.
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
9 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
11 .\" Copyright (c) 1983 The Regents of the University of California.
12 .\" All rights reserved.
14 .\" SPDX-License-Identifier: BSD-4-Clause-UC
16 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
17 .\" Modified 1998 by Andi Kleen
18 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
19 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
21 .TH bind 2 (date) "Linux man-pages (unreleased)"
23 bind \- bind a name to a socket
26 .RI ( libc ", " \-lc )
29 .B #include <sys/socket.h>
31 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
32 .BI " socklen_t " addrlen );
35 When a socket is created with
37 it exists in a name space (address family) but has no address assigned to it.
39 assigns the address specified by
41 to the socket referred to by the file descriptor
44 specifies the size, in bytes, of the address structure pointed to by
46 Traditionally, this operation is called \[lq]assigning a name to a socket\[rq].
48 It is normally necessary to assign a local address using
52 socket may receive connections (see
55 The rules used in name binding vary between address families.
56 Consult the manual entries in Section 7 for detailed information.
86 The actual structure passed for the
88 argument will depend on the address family.
91 structure is defined as something like:
96 sa_family_t sa_family;
102 The only purpose of this structure is to cast the structure
105 in order to avoid compiler warnings.
108 On success, zero is returned.
109 On error, \-1 is returned, and
111 is set to indicate the error.
115 .\" e.g., privileged port in AF_INET domain
116 The address is protected, and the user is not the superuser.
119 The given address is already in use.
122 (Internet domain sockets)
123 The port number was specified as zero in the socket address structure,
124 but, upon attempting to bind to an ephemeral port,
125 it was determined that all port numbers in the ephemeral port range
126 are currently in use.
127 See the discussion of
128 .I /proc/sys/net/ipv4/ip_local_port_range
133 is not a valid file descriptor.
136 The socket is already bound to an address.
137 .\" This may change in the future: see
138 .\" .I linux/unix/sock.c for details.
144 is not a valid address for this socket's domain.
149 does not refer to a socket.
151 The following errors are specific to UNIX domain
156 Search permission is denied on a component of the path prefix.
158 .BR path_resolution (7).)
161 A nonexistent interface was requested or the requested
162 address was not local.
166 points outside the user's accessible address space.
169 Too many symbolic links were encountered in resolving
177 A component in the directory prefix of the socket pathname does not exist.
180 Insufficient kernel memory was available.
183 A component of the path prefix is not a directory.
186 The socket inode would reside on a read-only filesystem.
190 POSIX.1-2001, SVr4, 4.4BSD
192 first appeared in 4.2BSD).
193 .\" SVr4 documents an additional
195 .\" general error condition, and
200 .\" UNIX-domain error conditions.
202 The transparent proxy options are not described.
203 .\" FIXME Document transparent proxy options
205 An example of the use of
207 with Internet domain sockets can be found in
210 The following example shows how to bind a stream socket in the UNIX
212 domain, and accept connections:
213 .\" listen.7 refers to this example.
214 .\" accept.7 refers to this example.
215 .\" unix.7 refers to this example.
217 .\" SRC BEGIN (bind.c)
222 #include <sys/socket.h>
226 #define MY_SOCK_PATH "/somepath"
227 #define LISTEN_BACKLOG 50
229 #define handle_error(msg) \e
230 do { perror(msg); exit(EXIT_FAILURE); } while (0)
236 socklen_t peer_addr_size;
237 struct sockaddr_un my_addr, peer_addr;
239 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
241 handle_error("socket");
243 memset(&my_addr, 0, sizeof(my_addr));
244 my_addr.sun_family = AF_UNIX;
245 strncpy(my_addr.sun_path, MY_SOCK_PATH,
246 sizeof(my_addr.sun_path) \- 1);
248 if (bind(sfd, (struct sockaddr *) &my_addr,
249 sizeof(my_addr)) == \-1)
250 handle_error("bind");
252 if (listen(sfd, LISTEN_BACKLOG) == \-1)
253 handle_error("listen");
255 /* Now we can accept incoming connections one
256 at a time using accept(2). */
258 peer_addr_size = sizeof(peer_addr);
259 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
262 handle_error("accept");
264 /* Code to deal with incoming connection(s)... */
266 if (close(sfd) == \-1)
267 handle_error("close");
269 if (unlink(MY_SOCK_PATH) == \-1)
270 handle_error("unlink");
284 .BR path_resolution (7),