OSDN Git Service

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