.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" 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. .\" .\" 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. .\" .\" .\" 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. .\" .\" 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. .\" .\" 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 .\" .\" 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 2007-12-28 "Linux" "Linux Programmer's Manual" .SH 名前 bind \- ソケットに名前をつける .SH 書式 .nf .BR "#include " " /* 「注意」参照 */" .B #include .sp .BI "int bind(int " sockfd ", const struct sockaddr *" addr , .BI " socklen_t " addrlen ); .fi .SH 説明 .BR socket (2) でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に 存在するが、アドレスは割り当てられていない。 .BR bind () は、ファイルディスクリプタ .I sockfd で参照されるソケットに .I addr で指定されたアドレスを割り当てる。 .I addrlen には .I addr が指すアドレス構造体のサイズをバイト単位で指定する。 伝統的にこの操作は 「ソケットに名前をつける」 と呼ばれる。 .PP .B SOCK_STREAM ソケットが接続を受け付けられるようにするには .RB ( accept (2) を参照)、通常その前に .BR bind () を使用してローカルアドレスを割り当てる必要がある。 名前付けのルールはアドレス・ファミリーごとに異なっている。詳細な情報は 第 7 章の各マニュアルを参照すること。 .B AF_INET は .BR ip (7) を、 .B AF_INET6 は .BR ipv6 (7) を、 .B AF_UNIX は .BR unix (7) を、 .B AF_APPLETALK は .BR ddp (7) を、 .B AF_PACKET は .BR packet (7) を、 .B AF_X25 は .BR x25 (7) を、 .B AF_NETLINK は .BR netlink (7) を参照。 .I addr 引き数に実際にどのような構造体が渡されるかは、 アドレス・ファミリーに依存する。 .I sockaddr 構造体は以下のような感じで定義されている: .in +4n .nf struct sockaddr { sa_family_t sa_family; char sa_data[14]; } .fi .in この構造体は、 .I addr に渡される構造体へのポインタをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。 下記の「例」を参照。 .SH 返り値 成功すると 0 を返す。 失敗すると \-1 を返し、 .I errno を適切に設定する。 .SH エラー .TP .B EACCES .\" e.g., privileged port in AF_INET domain そのアドレスは保護されていて、かつユーザがスーパーユーザではない。 .TP .B EADDRINUSE 指定されたアドレスが既に使用中である。 .TP .B EBADF .I sockfd が不正なディスクリプタである。 .TP .B EINVAL ソケットがすでにアドレスに結びつけ (bind) られている。 .\" これは将来変更されるかもしれない。 .\" 詳しくは .\" .I linux/unix/sock.c .\" を参照すること。 .B ENOTSOCK .I sockfd がファイルに対するディスクリプタで、ソケットに対するものではない。 .PP 以下のエラーは UNIXドメイン .RB ( AF_UNIX ) のソケット特有である: .TP .B EACCES パス名の構成要素に検索許可 (search permission) がない .RB ( path_resolution (7) も参照すること)。 .TP .B EADDRNOTAVAIL 存在しないインタフェースが要求されたか、要求されたアドレスが ローカルではなかった。 .TP .B EFAULT .I addr がユーザのアクセス可能なアドレス空間の外を指している。 .TP .B EINVAL .I addrlen が不正であるか、ソケットが .B AF_UNIX ファミリーではない。 .TP .B ELOOP .I addr を解決する際に遭遇したシンボリック・リンクが多過ぎる。 .TP .B ENAMETOOLONG .I addr が長過ぎる。 .TP .B ENOENT ファイルが存在しない。 .TP .B ENOMEM カーネルに、利用可能なメモリーが十分にない。 .TP .B ENOTDIR パス名の構成要素がディレクトリではない。 .TP .B EROFS ソケット inode が読み込み専用のファイルシステム上にある。 .SH 準拠 SVr4, 4.4BSD, POSIX.1-2001 .RB ( bind () は 4.2BSD で最初に現われた)。 .\" SVr4 には他に、一般のエラー状態 .\" .B ENOSR .\" 及び UNIX ドメインのエラー状態 .\" .BR EIO , .\" .B EISDIR .\" についての記述がある。 .SH 注意 POSIX.1-2001 では .I のインクルードは必須とされておらず、 Linux ではこのヘッダファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。 .BR bind () の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には .I int である。glibc でも使われている現在の .I socklen_t に関して、POSIX には少し混乱がある。 詳しくは .BR accept (2) を参照のこと。 .SH バグ 透過的プロキシ (transparent proxy) オプションについて記述していない。 .\" FIXME What *are* transparent proxy options? .SH 例 インターネット・ドメイン・ソケットでの .BR bind () の利用例が .BR getaddrinfo (3) に記載されている。 以下の例は、UNIX ドメイン .RB ( AF_UNIX ) でストリームソケットを bind する方法を示したものである。 .\" listen.7 refers to this example. .\" accept.7 refers to this example. .\" unix.7 refers to this example. .nf #include #include #include #include #include #define MY_SOCK_PATH "/somepath" #define LISTEN_BACKLOG 50 #define handle_error(msg) \\ 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 関連項目 .BR accept (2), .BR connect (2), .BR getsockname (2), .BR listen (2), .BR socket (2), .BR getaddrinfo (3), .BR getifaddrs (3), .BR ip (7), .BR ipv6 (7), .BR path_resolution (7), .BR socket (7), .BR unix (7)