OSDN Git Service

Convert release and draft pages to UTF-8.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / 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 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
67 .\"         all rights reserved.
68 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
69 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
70 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
71 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
72 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
73 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
74 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
75 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
76 .\"
77 .TH BIND 2 2007-12-28 "Linux" "Linux Programmer's Manual"
78 .\"O .SH NAME
79 .SH 名前
80 .\"O bind \- bind a name to a socket
81 bind \- ソケットに名前をつける
82 .\"O .SH SYNOPSIS
83 .SH 書式
84 .nf
85 .\"O .BR "#include <sys/types.h>" "          /* See NOTES */"
86 .BR "#include <sys/types.h>" "          /* 「注意」参照 */"
87 .B #include <sys/socket.h>
88 .sp
89 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
90 .BI "         socklen_t " addrlen );
91 .fi
92 .\"O .SH DESCRIPTION
93 .SH 説明
94 .\"O When a socket is created with
95 .\"O .BR socket (2),
96 .\"O it exists in a name space (address family) but has no address assigned to it.
97 .\"O .BR bind ()
98 .\"O assigns the address specified to by
99 .\"O .I addr
100 .\"O to the socket referred to by the file descriptor
101 .\"O .IR sockfd .
102 .\"O .I addrlen
103 .\"O specifies the size, in bytes, of the address structure pointed to by
104 .\"O .IR addr .
105 .\"O Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
106 .BR socket (2)
107 でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に
108 存在するが、アドレスは割り当てられていない。
109 .BR bind ()
110 は、ファイルディスクリプタ
111 .I sockfd
112 で参照されるソケットに
113 .I addr
114 で指定されたアドレスを割り当てる。
115 .I addrlen
116 には
117 .I addr
118 が指すアドレス構造体のサイズをバイト単位で指定する。
119 伝統的にこの操作は
120 「ソケットに名前をつける」
121 と呼ばれる。
122 .PP
123 .\"O It is normally necessary to assign a local address using
124 .\"O .BR bind ()
125 .\"O before a
126 .\"O .B SOCK_STREAM
127 .\"O socket may receive connections (see
128 .\"O .BR accept (2)).
129 .B SOCK_STREAM
130 ソケットが接続を受け付けられるようにするには
131 .RB ( accept (2)
132 を参照)、通常その前に
133 .BR bind ()
134 を使用してローカルアドレスを割り当てる必要がある。
135
136 .\"O The rules used in name binding vary between address families.
137 .\"O Consult the manual entries in Section 7 for detailed information.
138 .\"O For
139 .\"O .B AF_INET
140 .\"O see
141 .\"O .BR ip (7),
142 名前付けのルールはアドレス・ファミリーごとに異なっている。詳細な情報は
143 第 7 章の各マニュアルを参照すること。
144 .B AF_INET
145
146 .BR ip (7)
147 を、
148 .\"O for
149 .\"O .B AF_INET6
150 .\"O see
151 .\"O .BR ipv6 (7),
152 .B AF_INET6
153
154 .BR ipv6 (7)
155 を、
156 .\"O for
157 .\"O .B AF_UNIX
158 .\"O see
159 .\"O .BR unix (7),
160 .B AF_UNIX
161
162 .BR unix (7)
163 を、
164 .\"O for
165 .\"O .B AF_APPLETALK
166 .\"O see
167 .\"O .BR ddp (7),
168 .B AF_APPLETALK
169
170 .BR ddp (7)
171 を、
172 .\"O for
173 .\"O .B AF_PACKET
174 .\"O see
175 .\"O .BR packet (7),
176 .B AF_PACKET
177
178 .BR packet (7)
179 を、
180 .\"O for
181 .\"O .B AF_X25
182 .\"O see
183 .\"O .BR x25 (7)
184 .B AF_X25
185
186 .BR x25 (7)
187 を、
188 .\"O and for
189 .\"O .B AF_NETLINK
190 .\"O see
191 .\"O .BR netlink (7).
192 .B AF_NETLINK
193
194 .BR netlink (7)
195 を参照。
196
197 .\"O The actual structure passed for the
198 .\"O .I addr
199 .\"O argument will depend on the address family.
200 .\"O The
201 .\"O .I sockaddr
202 .\"O structure is defined as something like:
203 .I addr
204 引き数に実際にどのような構造体が渡されるかは、
205 アドレス・ファミリーに依存する。
206 .I sockaddr
207 構造体は以下のような感じで定義されている:
208 .in +4n
209 .nf
210
211 struct sockaddr {
212     sa_family_t sa_family;
213     char        sa_data[14];
214 }
215
216 .fi
217 .in
218 .\"O The only purpose of this structure is to cast the structure
219 .\"O pointer passed in
220 .\"O .I addr
221 .\"O in order to avoid compiler warnings.
222 .\"O See EXAMPLE below.
223 この構造体は、
224 .I addr
225 に渡される構造体へのポインタをキャストし、
226 コンパイラの警告メッセージを抑えるためだけに存在する。
227 下記の「例」を参照。
228 .\"O .SH "RETURN VALUE"
229 .SH 返り値
230 .\"O On success, zero is returned.
231 .\"O On error, \-1 is returned, and
232 .\"O .I errno
233 .\"O is set appropriately.
234 成功すると 0 を返す。
235 失敗すると \-1 を返し、
236 .I errno
237 を適切に設定する。
238 .\"O .SH ERRORS
239 .SH エラー
240 .TP
241 .B EACCES
242 .\" e.g., privileged port in AF_INET domain
243 .\"O The address is protected, and the user is not the superuser.
244 そのアドレスは保護されていて、かつユーザがスーパーユーザではない。
245 .TP
246 .B EADDRINUSE
247 .\"O The given address is already in use.
248 指定されたアドレスが既に使用中である。
249 .TP
250 .B EBADF
251 .\"O .I sockfd
252 .\"O is not a valid descriptor.
253 .I sockfd
254 が不正なディスクリプタである。
255 .TP
256 .B EINVAL
257 .\"O The socket is already bound to an address.
258 .\"O .\" This may change in the future: see
259 .\"O .\" .I linux/unix/sock.c for details.
260 ソケットがすでにアドレスに結びつけ (bind) られている。
261 .\" これは将来変更されるかもしれない。
262 .\" 詳しくは
263 .\" .I linux/unix/sock.c
264 .\" を参照すること。
265 .B ENOTSOCK
266 .\"O .I sockfd
267 .\"O is a descriptor for a file, not a socket.
268 .I sockfd
269 がファイルに対するディスクリプタで、ソケットに対するものではない。
270 .PP
271 .\"O The following errors are specific to UNIX domain
272 .\"O .RB ( AF_UNIX )
273 .\"O sockets:
274 以下のエラーは UNIXドメイン
275 .RB ( AF_UNIX )
276 のソケット特有である:
277 .TP
278 .B EACCES
279 .\"O Search permission is denied on a component of the path prefix.
280 .\"O (See also
281 .\"O .BR path_resolution (7).)
282 パス名の構成要素に検索許可 (search permission) がない
283 .RB ( path_resolution (7)
284 も参照すること)。
285 .TP
286 .B EADDRNOTAVAIL
287 .\"O A nonexistent interface was requested or the requested
288 .\"O address was not local.
289 存在しないインタフェースが要求されたか、要求されたアドレスが
290 ローカルではなかった。
291 .TP
292 .B EFAULT
293 .\"O .I addr
294 .\"O points outside the user's accessible address space.
295 .I addr
296 がユーザのアクセス可能なアドレス空間の外を指している。
297 .TP
298 .B EINVAL
299 .\"O The
300 .\"O .I addrlen
301 .\"O is wrong, or the socket was not in the
302 .\"O .B AF_UNIX
303 .\"O family.
304 .I addrlen
305 が不正であるか、ソケットが
306 .B AF_UNIX
307 ファミリーではない。
308 .TP
309 .B ELOOP
310 .\"O Too many symbolic links were encountered in resolving
311 .\"O .IR addr .
312 .I addr
313 を解決する際に遭遇したシンボリック・リンクが多過ぎる。
314 .TP
315 .B ENAMETOOLONG
316 .\"O .I addr
317 .\"O is too long.
318 .I addr
319 が長過ぎる。
320 .TP
321 .B ENOENT
322 .\"O The file does not exist.
323 ファイルが存在しない。
324 .TP
325 .B ENOMEM
326 .\"O Insufficient kernel memory was available.
327 カーネルに、利用可能なメモリーが十分にない。
328 .TP
329 .B ENOTDIR
330 .\"O A component of the path prefix is not a directory.
331 パス名の構成要素がディレクトリではない。
332 .TP
333 .B EROFS
334 .\"O The socket inode would reside on a read-only file system.
335 ソケット inode が読み込み専用のファイルシステム上にある。
336 .\"O .SH "CONFORMING TO"
337 .SH 準拠
338 .\"O SVr4, 4.4BSD, POSIX.1-2001
339 .\"O .RB ( bind ()
340 .\"O first appeared in 4.2BSD).
341 .\"O .\" SVr4 documents an additional
342 .\"O .\" .B ENOSR
343 .\"O .\" general error condition, and
344 .\"O .\" additional
345 .\"O .\" .B EIO
346 .\"O .\" and
347 .\"O .\" .B EISDIR
348 .\"O .\" UNIX-domain error conditions.
349 SVr4, 4.4BSD, POSIX.1-2001
350 .RB ( bind ()
351 は 4.2BSD で最初に現われた)。
352 .\" SVr4 には他に、一般のエラー状態
353 .\" .B ENOSR
354 .\" 及び UNIX ドメインのエラー状態
355 .\" .BR EIO ,
356 .\" .B EISDIR
357 .\" についての記述がある。
358 .\"O .SH NOTES
359 .SH 注意
360 .\"O POSIX.1-2001 does not require the inclusion of
361 .\"O .IR <sys/types.h> ,
362 .\"O and this header file is not required on Linux.
363 .\"O However, some historical (BSD) implementations required this header
364 .\"O file, and portable applications are probably wise to include it.
365 POSIX.1-2001 では
366 .I <sys/types.h>
367 のインクルードは必須とされておらず、
368 Linux ではこのヘッダファイルは必要ではない。
369 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが
370 必要であり、移植性が必要なアプリケーションではこのファイルを
371 インクルードするのが賢明であろう。
372
373 .\"O The third argument of
374 .\"O .BR bind ()
375 .\"O is in reality an
376 .\"O .I int
377 .\"O (and this is what 4.x BSD and libc4 and libc5 have).
378 .\"O Some POSIX confusion resulted in the present
379 .\"O .IR socklen_t ,
380 .\"O also used by glibc.
381 .\"O See also
382 .\"O .BR accept (2).
383 .BR bind ()
384 の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には
385 .I int
386 である。glibc でも使われている現在の
387 .I socklen_t
388 に関して、POSIX には少し混乱がある。
389 詳しくは
390 .BR accept (2)
391 を参照のこと。
392 .\"O .SH BUGS
393 .SH バグ
394 .\"O The transparent proxy options are not described.
395 透過的プロキシ (transparent proxy) オプションについて記述していない。
396 .\" FIXME What *are* transparent proxy options?
397 .\"O .SH EXAMPLE
398 .SH 例
399 .\"O An example of the use of
400 .\"O .BR bind ()
401 .\"O with Internet domain sockets can be found in
402 .\"O .BR getaddrinfo (3).
403 インターネット・ドメイン・ソケットでの
404 .BR bind ()
405 の利用例が
406 .BR getaddrinfo (3)
407 に記載されている。
408
409 .\"O The following example shows how to bind a stream socket in the UNIX
410 .\"O .RB ( AF_UNIX )
411 .\"O domain, and accept connections:
412 以下の例は、UNIX ドメイン
413 .RB ( AF_UNIX )
414 でストリームソケットを bind する方法を示したものである。
415 .\" listen.7 refers to this example.
416 .\" accept.7 refers to this example.
417 .\" unix.7 refers to this example.
418
419 .nf
420 #include <sys/socket.h>
421 #include <sys/un.h>
422 #include <stdlib.h>
423 #include <stdio.h>
424 #include <string.h>
425
426 #define MY_SOCK_PATH "/somepath"
427 #define LISTEN_BACKLOG 50
428
429 #define handle_error(msg) \\
430     do { perror(msg); exit(EXIT_FAILURE); } while (0)
431
432 int
433 main(int argc, char *argv[])
434 {
435     int sfd, cfd;
436     struct sockaddr_un my_addr, peer_addr;
437     socklen_t peer_addr_size;
438
439     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
440     if (sfd == \-1)
441         handle_error("socket");
442
443     memset(&my_addr, 0, sizeof(struct sockaddr_un));
444                         /* Clear structure */
445     my_addr.sun_family = AF_UNIX;
446     strncpy(my_addr.sun_path, MY_SOCK_PATH,
447             sizeof(my_addr.sun_path) \- 1);
448
449     if (bind(sfd, (struct sockaddr *) &my_addr,
450             sizeof(struct sockaddr_un)) == \-1)
451         handle_error("bind");
452     
453     if (listen(sfd, LISTEN_BACKLOG) == \-1)
454         handle_error("listen");
455
456     /* Now we can accept incoming connections one
457        at a time using accept(2) */
458
459     peer_addr_size = sizeof(struct sockaddr_un);
460     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
461                  &peer_addr_size)
462     if (cfd == \-1)
463         handle_error("accept");
464
465     /* Code to deal with incoming connection(s)... */
466
467     /* When no longer required, the socket pathname, MY_SOCK_PATH
468        should be deleted using unlink(2) or remove(3) */
469 }
470 .fi
471 .\"O .SH "SEE ALSO"
472 .SH 関連項目
473 .BR accept (2),
474 .BR connect (2),
475 .BR getsockname (2),
476 .BR listen (2),
477 .BR socket (2),
478 .BR getaddrinfo (3),
479 .BR getifaddrs (3),
480 .BR ip (7),
481 .BR ipv6 (7),
482 .BR path_resolution (7),
483 .BR socket (7),
484 .BR unix (7)