1 .\" Hey Emacs! This file is -*- nroff -*- source.
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.
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.
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.
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
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" 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 .\" Redistribution and use in source and binary forms, with or without
34 .\" modification, are permitted provided that the following conditions
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.
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
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>
66 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
67 .\" all rights reserved.
68 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
69 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
70 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
71 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
72 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
73 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
74 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
75 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
77 .TH BIND 2 2007-12-28 "Linux" "Linux Programmer's Manual"
82 .BR "#include <sys/types.h>" " /* 「注意」参照 */"
83 .B #include <sys/socket.h>
85 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
86 .BI " socklen_t " addrlen );
90 でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に
91 存在するが、アドレスは割り当てられていない。
101 が指すアドレス構造体のサイズをバイト単位で指定する。
107 ソケットが接続を受け付けられるようにするには
111 を使用してローカルアドレスを割り当てる必要がある。
113 名前付けのルールはアドレス・ファミリーごとに異なっている。詳細な情報は
145 引き数に実際にどのような構造体が渡されるかは、
148 構造体は以下のような感じで定義されている:
153 sa_family_t sa_family;
161 に渡される構造体へのポインタをキャストし、
162 コンパイラの警告メッセージを抑えるためだけに存在する。
172 .\" e.g., privileged port in AF_INET domain
173 そのアドレスは保護されていて、かつユーザがスーパーユーザではない。
183 ソケットがすでにアドレスに結びつけ (bind) られている。
184 .\" これは将来変更されるかもしれない。
186 .\" .I linux/unix/sock.c
190 がファイルに対するディスクリプタで、ソケットに対するものではない。
197 パス名の構成要素に検索許可 (search permission) がない
198 .RB ( path_resolution (7)
202 存在しないインタフェースが要求されたか、要求されたアドレスが
207 がユーザのアクセス可能なアドレス空間の外を指している。
217 を解決する際に遭遇したシンボリック・リンクが多過ぎる。
227 カーネルに、利用可能なメモリーが十分にない。
233 ソケット inode が読み込み専用のファイルシステム上にある。
235 SVr4, 4.4BSD, POSIX.1-2001
238 .\" SVr4 には他に、一般のエラー状態
240 .\" 及び UNIX ドメインのエラー状態
248 Linux ではこのヘッダファイルは必要ではない。
249 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが
250 必要であり、移植性が必要なアプリケーションではこのファイルを
254 の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には
256 である。glibc でも使われている現在の
258 に関して、POSIX には少し混乱がある。
263 透過的プロキシ (transparent proxy) オプションについて記述していない。
264 .\" FIXME What *are* transparent proxy options?
274 でストリームソケットを bind する方法を示したものである。
275 .\" listen.7 refers to this example.
276 .\" accept.7 refers to this example.
277 .\" unix.7 refers to this example.
280 #include <sys/socket.h>
286 #define MY_SOCK_PATH "/somepath"
287 #define LISTEN_BACKLOG 50
289 #define handle_error(msg) \\
290 do { perror(msg); exit(EXIT_FAILURE); } while (0)
293 main(int argc, char *argv[])
296 struct sockaddr_un my_addr, peer_addr;
297 socklen_t peer_addr_size;
299 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
301 handle_error("socket");
303 memset(&my_addr, 0, sizeof(struct sockaddr_un));
304 /* Clear structure */
305 my_addr.sun_family = AF_UNIX;
306 strncpy(my_addr.sun_path, MY_SOCK_PATH,
307 sizeof(my_addr.sun_path) \- 1);
309 if (bind(sfd, (struct sockaddr *) &my_addr,
310 sizeof(struct sockaddr_un)) == \-1)
311 handle_error("bind");
313 if (listen(sfd, LISTEN_BACKLOG) == \-1)
314 handle_error("listen");
316 /* Now we can accept incoming connections one
317 at a time using accept(2) */
319 peer_addr_size = sizeof(struct sockaddr_un);
320 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
323 handle_error("accept");
325 /* Code to deal with incoming connection(s)... */
327 /* When no longer required, the socket pathname, MY_SOCK_PATH
328 should be deleted using unlink(2) or remove(3) */
341 .BR path_resolution (7),