OSDN Git Service

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