OSDN Git Service

Update drafts for LDP 3.67
[linuxjm/LDP_man-pages.git] / draft / 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 .\"*******************************************************************
69 .\"
70 .\" This file was generated with po4a. Translate the source file.
71 .\"
72 .\"*******************************************************************
73 .\"
74 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
75 .\"         all rights reserved.
76 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
77 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
78 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
79 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
80 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
81 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
82 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
83 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
84 .\"
85 .TH BIND 2 2014\-05\-10 Linux "Linux Programmer's Manual"
86 .SH 名前
87 bind \- ソケットに名前をつける
88 .SH 書式
89 .nf
90 \fB#include <sys/types.h>\fP          /* 「注意」参照 */
91 \fB#include <sys/socket.h>\fP
92 .sp
93 \fBint bind(int \fP\fIsockfd\fP\fB, const struct sockaddr *\fP\fIaddr\fP\fB,\fP
94 \fB         socklen_t \fP\fIaddrlen\fP\fB);\fP
95 .fi
96 .SH 説明
97 \fBsocket\fP(2)  でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に
98 存在するが、アドレスは割り当てられていない。 \fBbind\fP()  は、ファイルディスクリプタ \fIsockfd\fP で参照されるソケットに
99 \fIaddr\fP で指定されたアドレスを割り当てる。 \fIaddrlen\fP には \fIaddr\fP が指すアドレス構造体のサイズをバイト単位で指定する。
100 伝統的にこの操作は 「ソケットに名前をつける」 と呼ばれる。
101 .PP
102 \fBSOCK_STREAM\fP ソケットが接続を受け付けられるようにするには (\fBaccept\fP(2)  を参照)、通常その前に \fBbind\fP()
103 を使用してローカルアドレスを割り当てる必要がある。
104
105 名前付けのルールはアドレス・ファミリーごとに異なっている。詳細な情報は 第 7 章の各マニュアルを参照すること。 \fBAF_INET\fP は
106 \fBip\fP(7)  を、 \fBAF_INET6\fP は \fBipv6\fP(7)  を、 \fBAF_UNIX\fP は \fBunix\fP(7)  を、
107 \fBAF_APPLETALK\fP は \fBddp\fP(7)  を、 \fBAF_PACKET\fP は \fBpacket\fP(7)  を、 \fBAF_X25\fP は
108 \fBx25\fP(7)  を、 \fBAF_NETLINK\fP は \fBnetlink\fP(7)  を参照。
109
110 \fIaddr\fP 引き数に実際にどのような構造体が渡されるかは、 アドレス・ファミリーに依存する。 \fIsockaddr\fP
111 構造体は以下のような感じで定義されている:
112 .in +4n
113 .nf
114
115 struct sockaddr {
116     sa_family_t sa_family;
117     char        sa_data[14];
118 }
119
120 .fi
121 .in
122 この構造体は、 \fIaddr\fP に渡される構造体へのポインタをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。 下記の「例」を参照。
123 .SH 返り値
124 成功した場合にはゼロが返される。エラー時には \-1 が返され、 \fIerrno\fP が適切に設定される。
125 .SH エラー
126 .TP 
127 \fBEACCES\fP
128 .\" e.g., privileged port in AF_INET domain
129 そのアドレスは保護されていて、かつユーザがスーパーユーザではない。
130 .TP 
131 \fBEADDRINUSE\fP
132 指定されたアドレスが既に使用中である。
133 .TP 
134 \fBEADDRINUSE\fP
135 (インターネットドメインソケットの場合) ソケットアドレス構造体でポート番号に 0 が指定されたが、 一時ポート (ephemeral port)
136 を割り当てようとした際に、 一時ポートとして使用する範囲のすべてのポート番号が使用中であった。 \fBip\fP(7) の
137 \fI/proc/sys/net/ipv4/ip_local_port_range\fP の説明を参照。
138 .TP 
139 .TP 
140 \fBEBADF\fP
141 \fIsockfd\fP が不正なディスクリプタである。
142 .TP 
143 \fBEINVAL\fP
144 .\" This may change in the future: see
145 .\" .I linux/unix/sock.c for details.
146 ソケットがすでにアドレスに結びつけ (bind) られている。
147 .TP 
148 \fBENOTSOCK\fP
149 \fIsockfd\fP がファイルに対するディスクリプタで、ソケットに対するものではない。
150 .PP
151 以下のエラーは UNIXドメイン (\fBAF_UNIX\fP)  のソケット特有である:
152 .TP 
153 \fBEACCES\fP
154 パス名の構成要素に検索許可 (search permission) がない (\fBpath_resolution\fP(7)  も参照すること)。
155 .TP 
156 \fBEADDRNOTAVAIL\fP
157 存在しないインタフェースが要求されたか、要求されたアドレスが ローカルではなかった。
158 .TP 
159 \fBEFAULT\fP
160 \fIaddr\fP がユーザのアクセス可能なアドレス空間の外を指している。
161 .TP 
162 \fBEINVAL\fP
163 \fIaddrlen\fP が不正であるか、ソケットが \fBAF_UNIX\fP ファミリーではない。
164 .TP 
165 \fBELOOP\fP
166 \fIaddr\fP を解決する際に遭遇したシンボリック・リンクが多過ぎる。
167 .TP 
168 \fBENAMETOOLONG\fP
169 \fIaddr\fP が長過ぎる。
170 .TP 
171 \fBENOENT\fP
172 ファイルが存在しない。
173 .TP 
174 \fBENOMEM\fP
175 カーネルに、利用可能なメモリーが十分にない。
176 .TP 
177 \fBENOTDIR\fP
178 パス名の構成要素がディレクトリではない。
179 .TP 
180 \fBEROFS\fP
181 ソケット inode が読み込み専用のファイルシステム上にある。
182 .SH 準拠
183 .\" SVr4 documents an additional
184 .\" .B ENOSR
185 .\" general error condition, and
186 .\" additional
187 .\" .B EIO
188 .\" and
189 .\" .B EISDIR
190 .\" UNIX-domain error conditions.
191 SVr4, 4.4BSD, POSIX.1\-2001 (\fBbind\fP()  は 4.2BSD で最初に現われた)。
192 .SH 注意
193 POSIX.1\-2001 では \fI<sys/types.h>\fP のインクルードは必須とされておらず、 Linux
194 ではこのヘッダファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが
195 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。
196
197 \fBbind\fP()  の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には \fIint\fP である。glibc
198 でも使われている現在の \fIsocklen_t\fP に関して、POSIX には少し混乱がある。 詳しくは \fBaccept\fP(2)  を参照のこと。
199 .SH バグ
200 .\" FIXME What *are* transparent proxy options?
201 透過的プロキシ (transparent proxy) オプションについて記述していない。
202 .SH 例
203 インターネット・ドメイン・ソケットでの \fBbind\fP()  の利用例が \fBgetaddrinfo\fP(3)  に記載されている。
204
205 .\" listen.7 refers to this example.
206 .\" accept.7 refers to this example.
207 .\" unix.7 refers to this example.
208 以下の例は、UNIX ドメイン (\fBAF_UNIX\fP)  でストリームソケットを bind する方法を示したものである。
209
210 .nf
211 #include <sys/socket.h>
212 #include <sys/un.h>
213 #include <stdlib.h>
214 #include <stdio.h>
215 #include <string.h>
216
217 #define MY_SOCK_PATH "/somepath"
218 #define LISTEN_BACKLOG 50
219
220 #define handle_error(msg) \e
221     do { perror(msg); exit(EXIT_FAILURE); } while (0)
222
223 int
224 main(int argc, char *argv[])
225 {
226     int sfd, cfd;
227     struct sockaddr_un my_addr, peer_addr;
228     socklen_t peer_addr_size;
229
230     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
231     if (sfd == \-1)
232         handle_error("socket");
233
234     memset(&my_addr, 0, sizeof(struct sockaddr_un));
235                         /* Clear structure */
236     my_addr.sun_family = AF_UNIX;
237     strncpy(my_addr.sun_path, MY_SOCK_PATH,
238             sizeof(my_addr.sun_path) \- 1);
239
240     if (bind(sfd, (struct sockaddr *) &my_addr,
241             sizeof(struct sockaddr_un)) == \-1)
242         handle_error("bind");
243
244     if (listen(sfd, LISTEN_BACKLOG) == \-1)
245         handle_error("listen");
246
247     /* Now we can accept incoming connections one
248        at a time using accept(2) */
249
250     peer_addr_size = sizeof(struct sockaddr_un);
251     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
252                  &peer_addr_size)
253     if (cfd == \-1)
254         handle_error("accept");
255
256     /* Code to deal with incoming connection(s)... */
257
258     /* When no longer required, the socket pathname, MY_SOCK_PATH
259        should be deleted using unlink(2) or remove(3) */
260 }
261 .fi
262 .SH 関連項目
263 \fBaccept\fP(2), \fBconnect\fP(2), \fBgetsockname\fP(2), \fBlisten\fP(2), \fBsocket\fP(2),
264 \fBgetaddrinfo\fP(3), \fBgetifaddrs\fP(3), \fBip\fP(7), \fBipv6\fP(7),
265 \fBpath_resolution\fP(7), \fBsocket\fP(7), \fBunix\fP(7)
266 .SH この文書について
267 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
268 である。プロジェクトの説明とバグ報告に関する情報は
269 http://www.kernel.org/doc/man\-pages/ に書かれている。