OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man2 / bind.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
3 .\" any authorship information in it.  It is probably available under the GPL.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\"
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
29 .\"
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
32 .\"
33 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
34 .\" Redistribution and use in source and binary forms, with or without
35 .\" modification, are permitted provided that the following conditions
36 .\" are met:
37 .\" 1. Redistributions of source code must retain the above copyright
38 .\"    notice, this list of conditions and the following disclaimer.
39 .\" 2. Redistributions in binary form must reproduce the above copyright
40 .\"    notice, this list of conditions and the following disclaimer in the
41 .\"    documentation and/or other materials provided with the distribution.
42 .\" 3. All advertising materials mentioning features or use of this software
43 .\"    must display the following acknowledgement:
44 .\"     This product includes software developed by the University of
45 .\"     California, Berkeley and its contributors.
46 .\" 4. Neither the name of the University nor the names of its contributors
47 .\"    may be used to endorse or promote products derived from this software
48 .\"    without specific prior written permission.
49 .\"
50 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 .\" SUCH DAMAGE.
61 .\" %%%LICENSE_END
62 .\"
63 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
64 .\" Modified 1998 by Andi Kleen
65 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
66 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
67 .\"
68 .TH BIND 2 2014-05-10 "Linux" "Linux Programmer's Manual"
69 .SH NAME
70 bind \- bind a name to a socket
71 .SH SYNOPSIS
72 .nf
73 .BR "#include <sys/types.h>" "          /* See NOTES */"
74 .B #include <sys/socket.h>
75 .sp
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
100 The rules used in name binding vary between address families.
101 Consult the manual entries in Section 7 for detailed information.
102 For
103 .B AF_INET
104 see
105 .BR ip (7),
106 for
107 .B AF_INET6
108 see
109 .BR ipv6 (7),
110 for
111 .B AF_UNIX
112 see
113 .BR unix (7),
114 for
115 .B AF_APPLETALK
116 see
117 .BR ddp (7),
118 for
119 .B AF_PACKET
120 see
121 .BR packet (7),
122 for
123 .B AF_X25
124 see
125 .BR x25 (7)
126 and for
127 .B AF_NETLINK
128 see
129 .BR netlink (7).
130
131 The actual structure passed for the
132 .I addr
133 argument will depend on the address family.
134 The
135 .I sockaddr
136 structure is defined as something like:
137 .in +4n
138 .nf
139
140 struct sockaddr {
141     sa_family_t sa_family;
142     char        sa_data[14];
143 }
144
145 .fi
146 .in
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 EXAMPLE below.
152 .SH RETURN VALUE
153 On success, zero is returned.
154 On error, \-1 is returned, and
155 .I errno
156 is set appropriately.
157 .SH ERRORS
158 .TP
159 .B EACCES
160 .\" e.g., privileged port in AF_INET domain
161 The address is protected, and the user is not the superuser.
162 .TP
163 .B EADDRINUSE
164 The given address is already in use.
165 .TP
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).
175 .TP
176 .TP
177 .B EBADF
178 .I sockfd
179 is not a valid 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 ENOTSOCK
187 .I sockfd
188 is a descriptor for a file, not a socket.
189 .PP
190 The following errors are specific to UNIX domain
191 .RB ( AF_UNIX )
192 sockets:
193 .TP
194 .B EACCES
195 Search permission is denied on a component of the path prefix.
196 (See also
197 .BR path_resolution (7).)
198 .TP
199 .B EADDRNOTAVAIL
200 A nonexistent interface was requested or the requested
201 address was not local.
202 .TP
203 .B EFAULT
204 .I addr
205 points outside the user's accessible address space.
206 .TP
207 .B EINVAL
208 The
209 .I addrlen
210 is wrong, or the socket was not in the
211 .B AF_UNIX
212 family.
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 The file 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 SVr4, 4.4BSD, POSIX.1-2001
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-2001 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
252 The third argument of
253 .BR bind ()
254 is in reality an
255 .I int
256 (and this is what 4.x BSD and libc4 and libc5 have).
257 Some POSIX confusion resulted in the present
258 .IR socklen_t ,
259 also used by glibc.
260 See also
261 .BR accept (2).
262 .SH BUGS
263 The transparent proxy options are not described.
264 .\" FIXME What *are* transparent proxy options?
265 .SH EXAMPLE
266 An example of the use of
267 .BR bind ()
268 with Internet domain sockets can be found in
269 .BR getaddrinfo (3).
270
271 The following example shows how to bind a stream socket in the UNIX
272 .RB ( AF_UNIX )
273 domain, and accept connections:
274 .\" listen.7 refers to this example.
275 .\" accept.7 refers to this example.
276 .\" unix.7 refers to this example.
277
278 .nf
279 #include <sys/socket.h>
280 #include <sys/un.h>
281 #include <stdlib.h>
282 #include <stdio.h>
283 #include <string.h>
284
285 #define MY_SOCK_PATH "/somepath"
286 #define LISTEN_BACKLOG 50
287
288 #define handle_error(msg) \\
289     do { perror(msg); exit(EXIT_FAILURE); } while (0)
290
291 int
292 main(int argc, char *argv[])
293 {
294     int sfd, cfd;
295     struct sockaddr_un my_addr, peer_addr;
296     socklen_t peer_addr_size;
297
298     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
299     if (sfd == \-1)
300         handle_error("socket");
301
302     memset(&my_addr, 0, sizeof(struct sockaddr_un));
303                         /* Clear structure */
304     my_addr.sun_family = AF_UNIX;
305     strncpy(my_addr.sun_path, MY_SOCK_PATH,
306             sizeof(my_addr.sun_path) \- 1);
307
308     if (bind(sfd, (struct sockaddr *) &my_addr,
309             sizeof(struct sockaddr_un)) == \-1)
310         handle_error("bind");
311
312     if (listen(sfd, LISTEN_BACKLOG) == \-1)
313         handle_error("listen");
314
315     /* Now we can accept incoming connections one
316        at a time using accept(2) */
317
318     peer_addr_size = sizeof(struct sockaddr_un);
319     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
320                  &peer_addr_size);
321     if (cfd == \-1)
322         handle_error("accept");
323
324     /* Code to deal with incoming connection(s)... */
325
326     /* When no longer required, the socket pathname, MY_SOCK_PATH
327        should be deleted using unlink(2) or remove(3) */
328 }
329 .fi
330 .SH SEE ALSO
331 .BR accept (2),
332 .BR connect (2),
333 .BR getsockname (2),
334 .BR listen (2),
335 .BR socket (2),
336 .BR getaddrinfo (3),
337 .BR getifaddrs (3),
338 .BR ip (7),
339 .BR ipv6 (7),
340 .BR path_resolution (7),
341 .BR socket (7),
342 .BR unix (7)
343 .SH COLOPHON
344 This page is part of release 3.68 of the Linux
345 .I man-pages
346 project.
347 A description of the project,
348 information about reporting bugs,
349 and the latest version of this page,
350 can be found at
351 \%http://www.kernel.org/doc/man\-pages/.