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.
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
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
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.
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
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>
68 .\"*******************************************************************
70 .\" This file was generated with po4a. Translate the source file.
72 .\"*******************************************************************
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
85 .TH BIND 2 2007\-12\-28 Linux "Linux Programmer's Manual"
90 \fB#include <sys/types.h>\fP /* 「注意」参照 */
91 \fB#include <sys/socket.h>\fP
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
97 \fBsocket\fP(2) でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に
98 存在するが、アドレスは割り当てられていない。 \fBbind\fP() は、ファイルディスクリプタ \fIsockfd\fP で参照されるソケットに
99 \fIaddr\fP で指定されたアドレスを割り当てる。 \fIaddrlen\fP には \fIaddr\fP が指すアドレス構造体のサイズをバイト単位で指定する。
100 伝統的にこの操作は 「ソケットに名前をつける」 と呼ばれる。
102 \fBSOCK_STREAM\fP ソケットが接続を受け付けられるようにするには (\fBaccept\fP(2) を参照)、通常その前に \fBbind\fP()
103 を使用してローカルアドレスを割り当てる必要がある。
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) を参照。
110 \fIaddr\fP 引き数に実際にどのような構造体が渡されるかは、 アドレス・ファミリーに依存する。 \fIsockaddr\fP
111 構造体は以下のような感じで定義されている:
116 sa_family_t sa_family;
122 この構造体は、 \fIaddr\fP に渡される構造体へのポインタをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。 下記の「例」を参照。
124 成功した場合にはゼロが返される。エラー時には \-1 が返され、 \fIerrno\fP が適切に設定される。
128 .\" e.g., privileged port in AF_INET domain
129 そのアドレスは保護されていて、かつユーザがスーパーユーザではない。
135 \fIsockfd\fP が不正なディスクリプタである。
138 .\" This may change in the future: see
139 .\" .I linux/unix/sock.c for details.
140 ソケットがすでにアドレスに結びつけ (bind) られている。
143 \fIsockfd\fP がファイルに対するディスクリプタで、ソケットに対するものではない。
145 以下のエラーは UNIXドメイン (\fBAF_UNIX\fP) のソケット特有である:
148 パス名の構成要素に検索許可 (search permission) がない (\fBpath_resolution\fP(7) も参照すること)。
151 存在しないインタフェースが要求されたか、要求されたアドレスが ローカルではなかった。
154 \fIaddr\fP がユーザのアクセス可能なアドレス空間の外を指している。
157 \fIaddrlen\fP が不正であるか、ソケットが \fBAF_UNIX\fP ファミリーではない。
160 \fIaddr\fP を解決する際に遭遇したシンボリック・リンクが多過ぎる。
169 カーネルに、利用可能なメモリーが十分にない。
175 ソケット inode が読み込み専用のファイルシステム上にある。
177 .\" SVr4 documents an additional
179 .\" general error condition, and
184 .\" UNIX-domain error conditions.
185 SVr4, 4.4BSD, POSIX.1\-2001 (\fBbind\fP() は 4.2BSD で最初に現われた)。
187 POSIX.1\-2001 では \fI<sys/types.h>\fP のインクルードは必須とされておらず、 Linux
188 ではこのヘッダファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが
189 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。
191 \fBbind\fP() の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には \fIint\fP である。glibc
192 でも使われている現在の \fIsocklen_t\fP に関して、POSIX には少し混乱がある。 詳しくは \fBaccept\fP(2) を参照のこと。
194 .\" FIXME What *are* transparent proxy options?
195 透過的プロキシ (transparent proxy) オプションについて記述していない。
197 インターネット・ドメイン・ソケットでの \fBbind\fP() の利用例が \fBgetaddrinfo\fP(3) に記載されている。
199 .\" listen.7 refers to this example.
200 .\" accept.7 refers to this example.
201 .\" unix.7 refers to this example.
202 以下の例は、UNIX ドメイン (\fBAF_UNIX\fP) でストリームソケットを bind する方法を示したものである。
205 #include <sys/socket.h>
211 #define MY_SOCK_PATH "/somepath"
212 #define LISTEN_BACKLOG 50
214 #define handle_error(msg) \e
215 do { perror(msg); exit(EXIT_FAILURE); } while (0)
218 main(int argc, char *argv[])
221 struct sockaddr_un my_addr, peer_addr;
222 socklen_t peer_addr_size;
224 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
226 handle_error("socket");
228 memset(&my_addr, 0, sizeof(struct sockaddr_un));
229 /* Clear structure */
230 my_addr.sun_family = AF_UNIX;
231 strncpy(my_addr.sun_path, MY_SOCK_PATH,
232 sizeof(my_addr.sun_path) \- 1);
234 if (bind(sfd, (struct sockaddr *) &my_addr,
235 sizeof(struct sockaddr_un)) == \-1)
236 handle_error("bind");
238 if (listen(sfd, LISTEN_BACKLOG) == \-1)
239 handle_error("listen");
241 /* Now we can accept incoming connections one
242 at a time using accept(2) */
244 peer_addr_size = sizeof(struct sockaddr_un);
245 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
248 handle_error("accept");
250 /* Code to deal with incoming connection(s)... */
252 /* When no longer required, the socket pathname, MY_SOCK_PATH
253 should be deleted using unlink(2) or remove(3) */
257 \fBaccept\fP(2), \fBconnect\fP(2), \fBgetsockname\fP(2), \fBlisten\fP(2), \fBsocket\fP(2),
258 \fBgetaddrinfo\fP(3), \fBgetifaddrs\fP(3), \fBip\fP(7), \fBipv6\fP(7),
259 \fBpath_resolution\fP(7), \fBsocket\fP(7), \fBunix\fP(7)
261 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
262 である。プロジェクトの説明とバグ報告に関する情報は
263 http://www.kernel.org/doc/man\-pages/ に書かれている。