.\" 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 .\" 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 .\" .\"******************************************************************* .\" .\" 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 .\" Modified 1997-11-06, HANATAKA Shinya .\" Modified 1999-08-02, HANATAKA Shinya .\" Updated 2005-02-03, Yuichi SATO .\" Updated 2005-12-26, Akihiro MOTOKI .\" Updated 2006-07-19, Akihiro MOTOKI , LDP v2.36 .\" Updated 2007-06-13, Akihiro MOTOKI , LDP v2.55 .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" .TH BIND 2 2014\-08\-19 Linux "Linux Programmer's Manual" .SH 名前 bind \- ソケットに名前をつける .SH 書式 .nf \fB#include \fP /* 「注意」参照 */ \fB#include \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\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 #include #include #include #include #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/ に書かれている。