OSDN Git Service

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