1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
.\" Portions extracted from /usr/include/sys/socket.h, which does not have
.\" any authorship information in it. It is probably available under the GPL.
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\"
.\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
.\"
.\" Copyright (c) 1983 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\" %%%LICENSE_END
.\"
.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 1998 by Andi Kleen
.\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
.\" all rights reserved.
.\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
.\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
.\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
.\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
.\"
.TH BIND 2 2014\-08\-19 Linux "Linux Programmer's Manual"
.SH 名前
bind \- ソケットに名前をつける
.SH 書式
.nf
\fB#include <sys/types.h>\fP /* 「注意」参照 */
\fB#include <sys/socket.h>\fP
.sp
\fBint bind(int \fP\fIsockfd\fP\fB, const struct sockaddr *\fP\fIaddr\fP\fB,\fP
\fB socklen_t \fP\fIaddrlen\fP\fB);\fP
.fi
.SH 説明
\fBsocket\fP(2) でソケットが作成されたとき、そのソケットは名前空間 (アドレスファミリー) に 存在するが、アドレスは割り当てられていない。
\fBbind\fP() は、ファイルディスクリプター \fIsockfd\fP で参照されるソケットに \fIaddr\fP で指定されたアドレスを割り当てる。
\fIaddrlen\fP には \fIaddr\fP が指すアドレス構造体のサイズをバイト単位で指定する。 伝統的にこの操作は 「ソケットに名前をつける」
と呼ばれる。
.PP
\fBSOCK_STREAM\fP ソケットが接続を受け付けられるようにするには (\fBaccept\fP(2) を参照)、通常その前に \fBbind\fP()
を使用してローカルアドレスを割り当てる必要がある。
名前付けのルールはアドレスファミリーごとに異なっている。詳細な情報は 第 7 章の各マニュアルを参照すること。 \fBAF_INET\fP は
\fBip\fP(7) を、 \fBAF_INET6\fP は \fBipv6\fP(7) を、 \fBAF_UNIX\fP は \fBunix\fP(7) を、
\fBAF_APPLETALK\fP は \fBddp\fP(7) を、 \fBAF_PACKET\fP は \fBpacket\fP(7) を、 \fBAF_X25\fP は
\fBx25\fP(7) を、 \fBAF_NETLINK\fP は \fBnetlink\fP(7) を参照。
\fIaddr\fP 引き数に実際にどのような構造体が渡されるかは、 アドレスファミリーに依存する。 \fIsockaddr\fP
構造体は以下のような感じで定義されている:
.in +4n
.nf
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
.fi
.in
この構造体は、 \fIaddr\fP に渡される構造体へのポインターをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。
下記の「例」を参照。
.SH 返り値
成功した場合にはゼロが返される。エラー時には \-1 が返され、 \fIerrno\fP が適切に設定される。
.SH エラー
.TP
\fBEACCES\fP
.\" e.g., privileged port in AF_INET domain
そのアドレスは保護されていて、かつユーザーがスーパーユーザーではない。
.TP
\fBEADDRINUSE\fP
指定されたアドレスが既に使用中である。
.TP
\fBEADDRINUSE\fP
(インターネットドメインソケットの場合) ソケットアドレス構造体でポート番号に 0 が指定されたが、 一時ポート (ephemeral port)
を割り当てようとした際に、 一時ポートとして使用する範囲のすべてのポート番号が使用中であった。 \fBip\fP(7) の
\fI/proc/sys/net/ipv4/ip_local_port_range\fP の説明を参照。
.TP
.TP
\fBEBADF\fP
\fIsockfd\fP が不正なディスクリプターである。
.TP
\fBEINVAL\fP
.\" This may change in the future: see
.\" .I linux/unix/sock.c for details.
ソケットがすでにアドレスに結びつけ (bind) られている。
.TP
\fBEINVAL\fP
\fIaddrlen\fP が間違っているか、 \fIaddr\fP がこのソケットのドメインで有効なアドレスではない。
.TP
\fBENOTSOCK\fP
\fIsockfd\fP がファイルに対するディスクリプターで、ソケットに対するものではない。
.PP
以下のエラーは UNIXドメイン (\fBAF_UNIX\fP) のソケット特有である:
.TP
\fBEACCES\fP
パス名の構成要素に検索許可 (search permission) がない (\fBpath_resolution\fP(7) も参照すること)。
.TP
\fBEADDRNOTAVAIL\fP
存在しないインターフェースが要求されたか、要求されたアドレスが ローカルではなかった。
.TP
\fBEFAULT\fP
\fIaddr\fP がユーザーのアクセス可能なアドレス空間の外を指している。
.TP
\fBELOOP\fP
\fIaddr\fP を解決する際に遭遇したシンボリックリンクが多過ぎる。
.TP
\fBENAMETOOLONG\fP
\fIaddr\fP が長過ぎる。
.TP
\fBENOENT\fP
ファイルが存在しない。
.TP
\fBENOMEM\fP
カーネルに、利用可能なメモリーが十分にない。
.TP
\fBENOTDIR\fP
パス名の構成要素がディレクトリではない。
.TP
\fBEROFS\fP
ソケット inode が読み込み専用のファイルシステム上にある。
.SH 準拠
.\" SVr4 documents an additional
.\" .B ENOSR
.\" general error condition, and
.\" additional
.\" .B EIO
.\" and
.\" .B EISDIR
.\" UNIX-domain error conditions.
SVr4, 4.4BSD, POSIX.1\-2001 (\fBbind\fP() は 4.2BSD で最初に現われた)。
.SH 注意
POSIX.1\-2001 では \fI<sys/types.h>\fP のインクルードは必須とされておらず、 Linux
ではこのヘッダーファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダーファイルが
必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。
\fBbind\fP() の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には \fIint\fP である。glibc
でも使われている現在の \fIsocklen_t\fP に関して、POSIX には少し混乱がある。 詳しくは \fBaccept\fP(2) を参照のこと。
.SH バグ
.\" FIXME Document transparent proxy options
透過的プロキシ (transparent proxy) オプションについて記述していない。
.SH 例
インターネットドメインソケットでの \fBbind\fP() の利用例が \fBgetaddrinfo\fP(3) に記載されている。
.\" listen.7 refers to this example.
.\" accept.7 refers to this example.
.\" unix.7 refers to this example.
以下の例は、UNIX ドメイン (\fBAF_UNIX\fP) でストリームソケットを bind する方法を示したものである。
.nf
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MY_SOCK_PATH "/somepath"
#define LISTEN_BACKLOG 50
#define handle_error(msg) \e
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
int sfd, cfd;
struct sockaddr_un my_addr, peer_addr;
socklen_t peer_addr_size;
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == \-1)
handle_error("socket");
memset(&my_addr, 0, sizeof(struct sockaddr_un));
/* Clear structure */
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, MY_SOCK_PATH,
sizeof(my_addr.sun_path) \- 1);
if (bind(sfd, (struct sockaddr *) &my_addr,
sizeof(struct sockaddr_un)) == \-1)
handle_error("bind");
if (listen(sfd, LISTEN_BACKLOG) == \-1)
handle_error("listen");
/* Now we can accept incoming connections one
at a time using accept(2) */
peer_addr_size = sizeof(struct sockaddr_un);
cfd = accept(sfd, (struct sockaddr *) &peer_addr,
&peer_addr_size)
if (cfd == \-1)
handle_error("accept");
/* Code to deal with incoming connection(s)... */
/* When no longer required, the socket pathname, MY_SOCK_PATH
should be deleted using unlink(2) or remove(3) */
}
.fi
.SH 関連項目
\fBaccept\fP(2), \fBconnect\fP(2), \fBgetsockname\fP(2), \fBlisten\fP(2), \fBsocket\fP(2),
\fBgetaddrinfo\fP(3), \fBgetifaddrs\fP(3), \fBip\fP(7), \fBipv6\fP(7),
\fBpath_resolution\fP(7), \fBsocket\fP(7), \fBunix\fP(7)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
である。プロジェクトの説明とバグ報告に関する情報は
http://www.kernel.org/doc/man\-pages/ に書かれている。
|