.\" This man page is Copyright (C) 1999 Andi Kleen . .\" and copyright (c) 1999 Matthew Wilcox. .\" .\" %%%LICENSE_START(VERBATIM_ONE_PARA) .\" Permission is granted to distribute possibly modified copies .\" of this page provided the header is included verbatim, .\" and in case of nontrivial modification author and date .\" of the modification is added to the header. .\" %%%LICENSE_END .\" .\" 2002-10-30, Michael Kerrisk, .\" Added description of SO_ACCEPTCONN .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text. .\" Modified, 27 May 2004, Michael Kerrisk .\" Added notes on capability requirements .\" A few small grammar fixes .\" 2010-06-13 Jan Engelhardt .\" Documented SO_DOMAIN and SO_PROTOCOL. .\" .\" FIXME .\" The following are not yet documented: .\" .\" SO_PEERNAME (2.4?) .\" get only .\" Seems to do something similar to getpeername(), but then .\" why is it necessary / how does it differ? .\" .\" SO_TIMESTAMPING (2.6.30) .\" Documentation/networking/timestamping.txt .\" commit cb9eff097831007afb30d64373f29d99825d0068 .\" Author: Patrick Ohly .\" .\" SO_WIFI_STATUS (3.3) .\" commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0 .\" Author: Johannes Berg .\" Also: SCM_WIFI_STATUS .\" .\" SO_NOFCS (3.4) .\" commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f .\" Author: Ben Greear .\" .\" SO_GET_FILTER (3.8) .\" commit a8fc92778080c845eaadc369a0ecf5699a03bef0 .\" Author: Pavel Emelyanov .\" .\" SO_MAX_PACING_RATE (3.13) .\" commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7 .\" Author: Eric Dumazet .\" .\" SO_BPF_EXTENSIONS (3.14) .\" commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e .\" Author: Michal Sekletar .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1999 NAKANO Takeo all rights reserved. .\" Translated 1999-12-06, NAKANO Takeo .\" Updated 2003-01-20, Akihiro Motoki .\" Updated 2005-02-23, Akihiro MOTOKI .\" Updated 2005-10-05, Akihiro MOTOKI .\" Updated 2005-12-05, Akihiro MOTOKI, Catch up to LDP man-pages 2.16 .\" Updated 2005-12-26, Akihiro MOTOKI, Catch up to LDP man-pages 2.18 .\" Updated 2006-04-15, Akihiro MOTOKI, Catch up to LDP man-pages 2.29 .\" Updated 2007-01-05, Akihiro MOTOKI, Catch up to LDP man-pages 2.43 .\" Updated 2013-05-01, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-24, Akihiro MOTOKI .\" .TH SOCKET 7 2020\-08\-13 Linux "Linux Programmer's Manual" .SH 名前 socket \- Linux のソケットインターフェース .SH 書式 \fB#include \fP .PP \fIsockfd\fP\fB = socket(int \fP\fIsocket_family\fP\fB, int \fP\fIsocket_type\fP\fB, int \fP\fIprotocol\fP\fB);\fP .SH 説明 このマニュアルページは Linux ネットワークのソケット層に対する ユーザーインターフェースを記述するものである。 BSD 互換ソケットは、ユーザープロセスとカーネル内部の ネットワークプロトコルスタック群との間に、 統一的なインターフェースを提供するものである。 プロトコルモジュールは \fIプロトコルファミリー (protocol familiy)\fP (例: \fBAF_INET\fP, \fBAF_IPX\fP, \fBAF_PACKET\fP) と \fIソケットタイプ (socket types)\fP (例: \fBSOCK_STREAM\fP, \fBSOCK_DGRAM\fP) に分類できる。 これらに関するより詳しい情報は \fBsocket\fP(2) を参照のこと。 .SS ソケット層の関数群 これらの関数はユーザープロセスがパケットを送受信したり、その他のソケット操作を 行ったりするために用いられる。詳細はそれぞれのマニュアルページを 見てほしい。 .PP \fBsocket\fP(2) はソケットを生成する。 \fBconnect\fP(2) はソケットをリモートのソケットアドレスに接続する。 \fBbind\fP(2) はソケットをローカルのソケットアドレスにバインドする。 \fBlisten\fP(2) はソケットに新しい接続が来たら受信するように伝え、 \fBaccept\fP(2) は外部からやってきた接続に対して新しいソケットを得るために用いられる。 \fBsocketpair\fP(2) は互いに接続された二つの名前無しソケット (anonymous socket) を返す (\fBAF_UNIX\fP のような、いくつかのローカルなファミリーでしか実装されていない)。 .PP \fBsend\fP(2), \fBsendto\fP(2), \fBsendmsg\fP(2) はソケットを通してデータを送信し、 \fBrecv\fP(2) \fBrecvfrom\fP(2), \fBrecvmsg\fP(2) はソケットからデータを受信する。 \fBpoll\fP(2) と \fBselect\fP(2) はデータの到着を待ったり、データ送信の準備ができるまで待ったりする。 さらに、 \fBwrite\fP(2), \fBwritev\fP(2), \fBsendfile\fP(2), \fBread\fP(2), \fBreadv\fP(2) のような標準的な I/O 操作もデータの読み書きに用いることができる。 .PP \fBgetsockbyname\fP(2) はローカルのソケットアドレスを返し、 \fBgetpeername\fP(2) はリモートのソケットアドレスを返す。 \fBgetsockopt\fP(2) と \fBsetsockopt\fP(2) はソケット層のオプションやプロトコルオプションの取得・設定に用いられる。 他のいくつかのオプションの取得・設定には \fBioctl\fP(2) を使うことができる。 .PP \fBclose\fP(2) はソケットをクローズする。 \fBshutdown\fP(2) は全二重なソケット接続を部分的にクローズする。 .PP シーク動作や、 0 以外の位置に対する \fBpread\fP(2) や \fBpwrite\fP(2) はソケットではサポートされていない。 .PP 非ブロッキングな I/O をソケットで行うことは可能で、 \fBfcntl\fP(2) を使ってソケットのファイルディスクリプターに \fBO_NONBLOCK\fP フラグをセットすれば良い。 こうするとブロックされる操作は、 (通常) \fBEAGAIN\fP エラーで戻ることになる (後で処理が再試行されることが期待されている)。 \fBconnect\fP(2) では \fBEINPROGRESS\fP エラーが返される。 この場合、ユーザーはさまざまなイベントを \fBpoll\fP(2) や \fBselect\fP(2) を使って待つことができる。 .TS tab(:) allbox; c s s l l l. I/O イベント イベント:poll フラグ:内容 Read:POLLIN:T{ 新しいデータが到着した。 T} Read:POLLIN:T{ (接続志向のソケットで) 接続の設定が終了した。 T} Read:POLLHUP:T{ 接続先で切断要求が生成された。 T} Read:POLLHUP:T{ 接続が壊れた (接続志向のプロトコルのみ)。 この場合、ソケットに書き込みが行われると \fBSIGPIPE\fP も送信される。 T} Write:POLLOUT:T{ ソケットには新しいデータを書き込むのに十分なバッファーがある。 T} Read/Write:T{ POLLIN | .br POLLOUT T}:T{ 外部向けの \fBconnect\fP(2) が終了した。 T} Read/Write:POLLERR:非同期的 (asynchronous) なエラーが起こった。 Read/Write:POLLHUP:接続先が片方向を切断した。 Exception:POLLPRI:T{ 緊急データ (urgent data) が到着した。この場合は \fBSIGURG\fP が送信される。 T} .\" FIXME . The following is not true currently: .\" It is no I/O event when the connection .\" is broken from the local end using .\" .BR shutdown (2) .\" or .\" .BR close (2). .TE .PP \fBpoll\fP(2) や \fBselect\fP(2) を使う代わりに、カーネルからアプリケーションに イベントを通知させるのに \fBSIGIO\fP シグナルを使う方法もある。 この方法を使うには、 \fBfcntl\fP(2) を用いてソケットのファイルディスクリプターに \fBO_ASYNC\fP フラグをセットし、 \fBSIGIO\fP に対する有効なシグナルハンドラーを \fBsigaction\fP(2) によって設定しておく必要がある。 後述の \fIシグナル\fP に関する議論も参考にすること。 .SS ソケットアドレス構造体 各ソケットドメインにはそれぞれ独自のソケットアドレス形式があり、ドメイン固有のアドレス構造体を持っている。 これらの構造体の先頭には、アドレス構造体の種類を示す整数の "family" フィールド (型は \fIsa_family_t\fP) がある。 このフィールドにより、 すべてのソケットドメインで汎用的に使用されるシステムコール (例えば、 \fBconnect\fP(2), \fBbind\fP(2), \fBaccept\fP(2), \fBgetsockname\fP(2), \fBgetpeername\fP(2) など) が、特定のソケットアドレスのドメインを判定することができる。 .PP 任意の種類のソケットアドレスをソケット API のインターフェースに渡せるように、 \fIstruct sockaddr\fP 型が定義されている。 この型の目的は、 純粋に、 ドメイン固有のソケットアドレスを 「汎用的な」型にキャストできるようにする点にある。 これにより、 ソケット API 呼び出しにおいて、 コンパイラが型の不一致の警告を出すのを避けることができる。 .PP これに加えて、ソケット API ではデータ型 \fIstruct sockaddr_storage\fP が提供されている。 サポートしているすべてのドメイン固有のソケットアドレス構造体を収容するのに、この型を使うことができる。 この型は十分な大きさがあり、(メモリー境界への) アラインも適切に行われている (特に、 IPv6 ソケットアドレスを収容するのにも十分な大きさである)。 この構造体には次のフィールドがあり、 このフィールドを使って、 この構造体に実際に格納されているソケットアドレスの型を特定することができる。 .PP .in +4n .EX sa_family_t ss_family; .EE .in .PP \fIsockaddr_storage\fP 構造体は、 ソケットアドレスを汎用的な方法で扱う必要があるプログラム (例えば、 IPv4 と IPv6 の両方のソケットアドレスを扱う必要があるプログラム) で有用である。 .SS ソケットオプション .\" FIXME . .\" In the list below, the text used to describe argument types .\" for each socket option should be more consistent .\" .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in .\" W R Stevens, UNPv1 これらのソケットオプションは、 \fBsetsockopt\fP(2) を用いれば設定でき、 \fBgetsockopt\fP(2) を用いれば取得できる。 但し、どのソケットの場合も ソケットレベルには \fBSOL_SOCKET\fP を指定すること。 注釈がない限り、 \fIoptval\fP は \fIint\fP へのポインターである。 .TP \fBSO_ACCEPTCONN\fP このソケットが \fBlisten\fP(2) によって接続待ち受け状態に設定されているかどうかを示す値を返す。 値 0 は listen 状態のソケットでないことを、 値 1 は listen 状態のソケットであることを示す。このソケットオプションは読み込み専用である。 .TP \fBSO_ATTACH_FILTER\fP (since Linux 2.2), \fBSO_ATTACH_BPF\fP (since Linux 3.19) Attach a classic BPF (\fBSO_ATTACH_FILTER\fP) or an extended BPF (\fBSO_ATTACH_BPF\fP) program to the socket for use as a filter of incoming packets. A packet will be dropped if the filter program returns zero. If the filter program returns a nonzero value which is less than the packet's data length, the packet will be truncated to the length returned. If the value returned by the filter is greater than or equal to the packet's data length, the packet is allowed to proceed unmodified. .IP The argument for \fBSO_ATTACH_FILTER\fP is a \fIsock_fprog\fP structure, defined in \fI\fP: .IP .in +4n .EX struct sock_fprog { unsigned short len; struct sock_filter *filter; }; .EE .in .IP The argument for \fBSO_ATTACH_BPF\fP is a file descriptor returned by the \fBbpf\fP(2) system call and must refer to a program of type \fBBPF_PROG_TYPE_SOCKET_FILTER\fP. .IP These options may be set multiple times for a given socket, each time replacing the previous filter program. The classic and extended versions may be called on the same socket, but the previous filter will always be replaced such that a socket never has more than one filter defined. .IP Both classic and extended BPF are explained in the kernel source file \fIDocumentation/networking/filter.txt\fP .TP \fBSO_ATTACH_REUSEPORT_CBPF\fP, \fBSO_ATTACH_REUSEPORT_EBPF\fP For use with the \fBSO_REUSEPORT\fP option, these options allow the user to set a classic BPF (\fBSO_ATTACH_REUSEPORT_CBPF\fP) or an extended BPF (\fBSO_ATTACH_REUSEPORT_EBPF\fP) program which defines how packets are assigned to the sockets in the reuseport group (that is, all sockets which have \fBSO_REUSEPORT\fP set and are using the same local address to receive packets). .IP The BPF program must return an index between 0 and N\-1 representing the socket which should receive the packet (where N is the number of sockets in the group). If the BPF program returns an invalid index, socket selection will fall back to the plain \fBSO_REUSEPORT\fP mechanism. .IP Sockets are numbered in the order in which they are added to the group (that is, the order of \fBbind\fP(2) calls for UDP sockets or the order of \fBlisten\fP(2) calls for TCP sockets). New sockets added to a reuseport group will inherit the BPF program. When a socket is removed from a reuseport group (via \fBclose\fP(2)), the last socket in the group will be moved into the closed socket's position. .IP These options may be set repeatedly at any time on any socket in the group to replace the current BPF program used by all sockets in the group. .IP \fBSO_ATTACH_REUSEPORT_CBPF\fP takes the same argument type as \fBSO_ATTACH_FILTER\fP and \fBSO_ATTACH_REUSEPORT_EBPF\fP takes the same argument type as \fBSO_ATTACH_BPF\fP. .IP UDP support for this feature is available since Linux 4.5; TCP support is available since Linux 4.6. .TP \fBSO_BINDTODEVICE\fP このソケットを、引数で渡したインターフェース名で指定される (\(lqeth0\(rq のような) 特定のデバイスにバインドする。 名前が空文字列だったり、オプションの長さ (optlen) が 0 の場合には、 ソケットのバインドが削除される。 渡すオプションは、インターフェース名が 入ったヌル文字で終端された可変長の文字列である。 文字列の最大のサイズは \fBIFNAMSIX\fP である。 ソケットがインターフェースにバインドされると、 その特定のインターフェースから受信されたパケットだけを処理する。 このオプションはいくつかのソケットタイプ、 特に \fBAF_INET\fP に対してのみ動作する点に注意すること。 パケットソケットではサポートされていない (通常の \fBbind\fP(2) を使うこと)。 .IP Linux 3.8 より前のバージョンでは、このソケットオプションは \fBgetsockname\fP(2) で設定することはできたが、取得することができなかった。 Linux 3.8 以降では、読み出すことができる。 \fIoptlen\fP 引数には、 デバイス名を格納するのに十分なバッファーサイズを渡すべきであり、 \fBIFNAMSIZ\fP バイトにすることを推奨する。 実際のデバイス名の長さは \fIoptlen\fP 引数に格納されて返される。 .TP \fBSO_BROADCAST\fP ブロードキャストフラグを設定・取得する。有効になっていると、データグラ ムソケットはブロードキャストアドレスにパケットを送信できるようになる。 ストリーム指向のソケットには何の効果もない。 .TP \fBSO_BSDCOMPAT\fP BSD のバグに対して互換性を取るための機能を有効にする。 この機能は Linux 2.0 と 2.2 の UDP プロトコルモジュールで使用されている。 有効になっていると、 UDP ソケットで受信された ICMP エラーは ユーザープログラムに渡されない。 これ以降のバージョンのカーネルでは、このオプションのサポートは 段階的に廃止されてきた。 Linux 2.4 ではこのオプションは黙って無視され、 Linux 2.6 ではプログラムがこのオプションを使用すると (printk() を使って) カーネルの警告メッセージが出力される。 Linux 2.0 では、このオプションを指定すると、 raw ソケットにおいても BSD のバグ (ランダムヘッダー変更、 ブロードキャストフラグのスキップ) に対する互換機能が有効になっていた。 しかし、こちらは Linux 2.2 で削除された。 .TP \fBSO_DEBUG\fP ソケットのデバッグ機能を有効にする。 \fBCAP_NET_ADMIN\fP 権限を持つプロセスか、実効ユーザー ID が 0 のプロセスでしか 利用できない。 .TP \fBSO_DETACH_FILTER\fP (Linux 2.2 以降), \fBSO_DETACH_BPF\fP (Linux 3.19 以降) These two options, which are synonyms, may be used to remove the classic or extended BPF program attached to a socket with either \fBSO_ATTACH_FILTER\fP or \fBSO_ATTACH_BPF\fP. The option value is ignored. .TP \fBSO_DOMAIN\fP (Linux 2.6.32 以降) ソケットドメインを整数で取得する。 \fBAF_INET6\fP のような値が返される。 詳細は \fBsocket\fP(2) を参照。このソケットオプションは読み込み専用である。 .TP \fBSO_ERROR\fP 保留になっていたソケットエラーを取得してクリアする。 このソケットオプションは読み込み専用である。整数値をとる。 .TP \fBSO_DONTROUTE\fP ゲートウェイを経由せず、直接接続されているホストにのみ送信する。 \fBsend\fP(2) 操作で \fBMSG_DONTROUTE\fP フラグをセットした場合も同じ効果が得られる。 ブール整数のフラグを取る。 .TP \fBSO_INCOMING_CPU\fP (gettable since Linux 3.19, settable since Linux 4.4) .\" getsockopt 2c8c56e15df3d4c2af3d656e44feb18789f75837 .\" setsockopt 70da268b569d32a9fddeea85dc18043de9d89f89 Sets or gets the CPU affinity of a socket. Expects an integer flag. .IP .in +4n .EX int cpu = 1; setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu)); .EE .in .IP .\" .\" From an email conversation with Eric Dumazet: .\" >> Note that setting the option is not supported if SO_REUSEPORT is used. .\" > .\" > Please define "not supported". Does this yield an API diagnostic? .\" > If so, what is it? .\" > .\" >> Socket will be selected from an array, either by a hash or BPF program .\" >> that has no access to this information. .\" > .\" > Sorry -- I'm lost here. How does this comment relate to the proposed .\" > man page text above? .\" .\" Simply that : .\" .\" If an application uses both SO_INCOMING_CPU and SO_REUSEPORT, then .\" SO_REUSEPORT logic, selecting the socket to receive the packet, ignores .\" SO_INCOMING_CPU setting. Because all of the packets for a single stream (i.e., all packets for the same 4\-tuple) arrive on the single RX queue that is associated with a particular CPU, the typical use case is to employ one listening process per RX queue, with the incoming flow being handled by a listener on the same CPU that is handling the RX queue. This provides optimal NUMA behavior and keeps CPU caches hot. .TP \fBSO_INCOMING_NAPI_ID\fP (gettable since Linux 4.12) .\" getsockopt 6d4339028b350efbf87c61e6d9e113e5373545c9 Returns a system\-level unique ID called NAPI ID that is associated with a RX queue on which the last packet associated with that socket is received. .IP This can be used by an application to split the incoming flows among worker threads based on the RX queue on which the packets associated with the flows are received. It allows each worker thread to be associated with a NIC HW receive queue and service all the connection requests received on that RX queue. This mapping between a app thread and a HW NIC queue streamlines the flow of data from the NIC to the application. .TP \fBSO_KEEPALIVE\fP 接続志向のソケットに対する keep\-alive メッセージの送信を有効にする。 ブール値の整数フラグをとる。 .TP \fBSO_LINGER\fP \fBSO_LINGER\fP オプションを取得・設定する。引数には \fIlinger\fP 構造体を取る。 .IP .in +4n .EX struct linger { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; .EE .in .IP 有効になっていると、 \fBclose\fP(2) や \fBshutdown\fP(2) は、そのソケットにキューイングされたメッセージがすべて送信完了するか、 linger (居残り) タイムアウトになるまで返らない。無効になっていると、 これらのコールはただちに戻り、クローズ動作はバックグラウンドで行われる。 ソケットのクローズを \fBexit\fP(2) の一部として行った場合には、残っているソケットの クローズ動作は必ずバックグラウンドに送られる。 .TP \fBSO_LOCK_FILTER\fP .\" commit d59577b6ffd313d0ab3be39cb1ab47e29bdc9182 When set, this option will prevent changing the filters associated with the socket. These filters include any set using the socket options \fBSO_ATTACH_FILTER\fP, \fBSO_ATTACH_BPF\fP, \fBSO_ATTACH_REUSEPORT_CBPF\fP, and \fBSO_ATTACH_REUSEPORT_EBPF\fP. .IP The typical use case is for a privileged process to set up a raw socket (an operation that requires the \fBCAP_NET_RAW\fP capability), apply a restrictive filter, set the \fBSO_LOCK_FILTER\fP option, and then either drop its privileges or pass the socket file descriptor to an unprivileged process via a UNIX domain socket. .IP Once the \fBSO_LOCK_FILTER\fP option has been enabled, attempts to change or remove the filter attached to a socket, or to disable the \fBSO_LOCK_FILTER\fP option will fail with the error \fBEPERM\fP. .TP \fBSO_MARK\fP (Linux 2.6.25 以降) .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0 .\" and 914a9ab386a288d0f22252fc268ecbc048cdcbd5 このソケットから送信される各パケットにマークをセットする (netfilter の MARK ターゲットと似ているが、ソケット単位である点が異なる)。 マークの変更は、 netfilter なしでのマークに基づいてのルーティングや、 パケットフィルタリングに使うことができる。 このオプションを変更するには \fBCAP_NET_ADMIN\fP ケーパビリティが必要である。 .TP \fBSO_OOBINLINE\fP .\" don't document it because it can do too much harm. .\".B SO_NO_CHECK .\" The kernel has support for the SO_NO_CHECK socket .\" option (boolean: 0 == default, calculate checksum on xmit, .\" 1 == do not calculate checksum on xmit). .\" Additional note from Andi Kleen on SO_NO_CHECK (2010-08-30) .\" On Linux UDP checksums are essentially free and there's no reason .\" to turn them off and it would disable another safety line. .\" That is why I didn't document the option. このオプションを有効にすると、帯域外データ (out\-of\-band data) は 受信データストリーム中に置かれる。有効にしなければ、 帯域外データは受信時に \fBMSG_OOB\fP フラグがセットされている場合に限って渡される。 .TP \fBSO_PASSCRED\fP \fBSCM_CREDENTIALS\fP 制御メッセージの受信を有効/無効にする。詳細は \fBunix\fP(7) を参照のこと。 .TP \fBSO_PASSSEC\fP \fBSCM_SECURITY\fP 制御メッセージの受信を有効/無効にする。詳細は \fBunix\fP(7) を参照のこと。 .TP \fBSO_PEEK_OFF\fP (Linux 3.4 以降) .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54 \fBMSG_PEEK\fP フラグと一緒に使用された場合 \fBrecv\fP(2) システムコールの "peek offset" にこのオプションの値が設定される。現在のところ、このオプションは \fBunix\fP(7) ソケットでのみサポートされている。 .IP このオプションが負の値に設定された場合、従来の動作となる。 つまり \fBMSG_PEEK\fP フラグが指定された \fBrecv\fP(2) は、キューの先頭のデータに対して peek 処理を行う (データを読み出すが、キューからデータの削除を行わない)。 新規のソケットではこのオプションの値は必ず \-1 に設定される。 .IP このオプションに 0 以上の値が設定されると、 そのソケットのキュー上のオプション値で指定されたバイトオフセットにあるデータが次の peek 処理で返される。 同時に、 "peek offset" がキューから peek 処理されたバイト数だけ加算される。したがって、次の peek 処理ではキューのその次にあるデータが返される。 .IP \fBrecv\fP(2) (や同様のシステムコール) の \fBMSG_PEEK\fP フラグなしの呼び出しでキューの先頭のデータが削除された場合、 "peek offset" は削除されたバイト数だけ減算される。 言い換えると、 \fBMSG_PEEK\fP フラグなしでデータを受信すると、 "peek offset" が指すキュー内の相対的な位置が狂わないように調整され、この後の peek では、 データ削除が行われなかった場合に返されたのと同じ値が返されるということである。 .IP データグラムソケットでは、 "peek offset" がパケットの途中を指している場合には、 返されるデータには \fBMSG_TRUNC\fP フラグが付与される。 .IP 以下の例は \fBSO_PEEK_OFF\fP の利用例を示している。ストリームソケットのキューに以下の入力データが入っているものとする。 .IP aabbccddeeff .IP 以下の順序で \fBrecv\fP(2) の呼び出しを行うと、コメントに書かれた結果となる。 .IP .in +4n .EX int ov = 4; // Set peek offset to 4 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov)); recv(fd, buf, 2, MSG_PEEK); // Peeks "cc"; offset set to 6 recv(fd, buf, 2, MSG_PEEK); // Peeks "dd"; offset set to 8 recv(fd, buf, 2, 0); // Reads "aa"; offset set to 6 recv(fd, buf, 2, MSG_PEEK); // Peeks "ee"; offset set to 8 .EE .in .TP \fBSO_PEERCRED\fP Return the credentials of the peer process connected to this socket. For further details, see \fBunix\fP(7). .TP \fBSO_PEERSEC\fP (Linux 2.6.2 以降) Return the security context of the peer socket connected to this socket. For further details, see \fBunix\fP(7) and \fBip\fP(7). .TP \fBSO_PRIORITY\fP .\" For .\" .BR ip (7), .\" this also sets the IP type-of-service (TOS) field for outgoing packets. プロトコルで定義された優先度を、このソケットから 送信される全てのパケットにセットする。 Linux はネットワークキュー内部の 整列にこの値を用いる。高い優先度を持っているパケットは先に処理される。 ただしそのデバイスのキュー処理のやり方に依存する。 0 から 6 以外の優先度をセットするには \fBCAP_NET_ADMIN\fP ケーパビリティが必要である。 .TP \fBSO_PROTOCOL\fP (Linux 2.6.32 以降) ソケットのプロトコルを整数で取得する。 \fBIPPROTO_SCTP\fP のような値が返される。 詳細は \fBsocket\fP(2) を参照。このソケットオプションは読み込み専用である。 .TP \fBSO_RCVBUF\fP .\" Most (all?) other implementations do not do this -- MTK, Dec 05 .\" The following thread on LMKL is quite informative: .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior .\" 17 July 2012 .\" http://thread.gmane.org/gmane.linux.kernel/1328935 ソケットの受信バッファーの最大サイズを設定・取得する (バイト単位)。 \fBsetsockopt\fP(2) を使って値が設定されたときに (管理オーバヘッド用の領域を確保するために) カーネルはこの値を 2倍し、 \fBgetsockopt\fP(2) はこの 2倍された値を返す。 デフォルトの値は \fI/proc/sys/net/core/rmem_default\fP ファイルで設定され、許容される最大の値は \fI/proc/sys/net/core/rmem_max\fP ファイルで設定される。 このオプションの最小値は (2倍した値で) 256 である。 .TP \fBSO_RCVBUFFORCE\fP (Linux 2.6.14 以降) このソケットオプションを使うと、特権プロセス (\fBCAP_NET_ADMIN\fP を持つプロセス) は \fBSO_RCVBUF\fP と同じことを実行できる。 ただし、上限 \fIrmem_max\fP を上書きすることができる。 .TP \fBSO_RCVLOWAT\fP と \fBSO_SNDLOWAT\fP Specify the minimum number of bytes in the buffer until the socket layer will pass the data to the protocol (\fBSO_SNDLOWAT\fP) or the user on receiving (\fBSO_RCVLOWAT\fP). These two values are initialized to 1. \fBSO_SNDLOWAT\fP is not changeable on Linux (\fBsetsockopt\fP(2) fails with the error \fBENOPROTOOPT\fP). \fBSO_RCVLOWAT\fP is changeable only since Linux 2.4. .IP .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05 .\" commit c7004482e8dcb7c3c72666395cfa98a216a4fb70 Before Linux 2.6.28 \fBselect\fP(2), \fBpoll\fP(2), and \fBepoll\fP(7) did not respect the \fBSO_RCVLOWAT\fP setting on Linux, and indicated a socket as readable when even a single byte of data was available. A subsequent read from the socket would then block until \fBSO_RCVLOWAT\fP bytes are available. Since Linux 2.6.28, \fBselect\fP(2), \fBpoll\fP(2), and \fBepoll\fP(7) indicate a socket as readable only if at least \fBSO_RCVLOWAT\fP bytes are available. .TP \fBSO_RCVTIMEO\fP と \fBSO_SNDTIMEO\fP .\" Not implemented in 2.0. .\" Implemented in 2.1.11 for getsockopt: always return a zero struct. .\" Implemented in 2.3.41 for setsockopt, and actually used. .\" in fact to EAGAIN 送信・受信のタイムアウトを指定する。これを越えるとエラーを報告する。 引数は \fIstruct timeval\fP である。 入出力関数がタイムアウト時間の間ブロックされ、かつデータの送信または 受信が行われていた場合は、転送されたデータ量が関数の返り値となる。 何もデータが転送されずにタイムアウトに達した場合は、 \-1 を返し、 \fIerrno\fP に \fBEAGAIN\fP か \fBEWOULDBLOCK\fP か \fBEINPROGRESS\fP (\fBconnect\fP(2) の場合) が設定され、 あたかもソケットに非ブロッキングが指定されたように見える。 タイムアウト値に (デフォルト値である) 0 に設定すると、 操作は決してタイムアウトしなくなる。 タイムアウトが影響を及ぼすのは、 ソケット I/O を実行するシステムコールだけ (例えば \fBread\fP(2), \fBrecvmsg\fP(2), \fBsend\fP(2), \fBsendmsg\fP(2)) である。 \fBselect\fP(2), \fBpoll\fP(2), \fBepoll_wait\fP(2) などにはタイムアウトは影響を及ぼさない。 .TP \fBSO_REUSEADDR\fP .\" commit c617f398edd4db2b8567a28e899a88f8f574798d .\" https://lwn.net/Articles/542629/ \fBbind\fP(2) コールに与えられたアドレスが正しいかを判断するルールで、 ローカルアドレスの再利用を可能にする。 つまり \fBAF_INET\fP ソケットなら、そのアドレスにバインドされたアクティブな listen 状態のソケットが存在しない限り、バインドが行える。 listen 状態のソケットがアドレス \fBINADDR_ANY\fP で特定のポートにバインドされている場合には、 このポートに対しては、どんなローカルアドレスでもバインドできない。 引数はブール整数のフラグである。 .TP \fBSO_REUSEPORT\fP (Linux 3.9 以降) Permits multiple \fBAF_INET\fP or \fBAF_INET6\fP sockets to be bound to an identical socket address. This option must be set on each socket (including the first socket) prior to calling \fBbind\fP(2) on the socket. To prevent port hijacking, all of the processes binding to the same address must have the same effective UID. This option can be employed with both TCP and UDP sockets. .IP For TCP sockets, this option allows \fBaccept\fP(2) load distribution in a multi\-threaded server to be improved by using a distinct listener socket for each thread. This provides improved load distribution as compared to traditional techniques such using a single \fBaccept\fP(2)ing thread that distributes connections, or having multiple threads that compete to \fBaccept\fP(2) from the same socket. .IP For UDP sockets, the use of this option can provide better distribution of incoming datagrams to multiple processes (or threads) as compared to the traditional technique of having multiple processes compete to receive datagrams on the same socket. .TP \fBSO_RXQ_OVFL\fP (Linux 2.6.33 以降) .\" commit 3b885787ea4112eaa80945999ea0901bf742707f Indicates that an unsigned 32\-bit value ancillary message (cmsg) should be attached to received skbs indicating the number of packets dropped by the socket since its creation. .TP \fBSO_SELECT_ERR_QUEUE\fP (Linux 3.10 以降) .\" commit 7d4c04fc170087119727119074e72445f2bb192b .\" Author: Keller, Jacob E .\" It does not affect wake up. When this option is set on a socket, an error condition on a socket causes notification not only via the \fIexceptfds\fP set of \fBselect\fP(2). Similarly, \fBpoll\fP(2) also returns a \fBPOLLPRI\fP whenever an \fBPOLLERR\fP event is returned. .IP .\" commit 6e5d58fdc9bedd0255a8 .\" ("skbuff: Fix not waking applications when errors are enqueued") Background: this option was added when waking up on an error condition occurred only via the \fIreadfds\fP and \fIwritefds\fP sets of \fBselect\fP(2). The option was added to allow monitoring for error conditions via the \fIexceptfds\fP argument without simultaneously having to receive notifications (via \fIreadfds\fP) for regular data that can be read from the socket. After changes in Linux 4.16, the use of this flag to achieve the desired notifications is no longer necessary. This option is nevertheless retained for backwards compatibility. .TP \fBSO_SNDBUF\fP .\" Most (all?) other implementations do not do this -- MTK, Dec 05 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail) ソケットの送信バッファーの最大サイズを設定・取得する (バイト単位)。 \fBsetsockopt\fP(2) を使って値が設定されたときに (管理オーバヘッド用の領域を確保するために) カーネルはこの値を 2倍し、 \fBgetsockopt\fP(2) はこの 2倍された値を返す。 デフォルトの値は \fI/proc/sys/net/core/wmem_default\fP ファイルで設定され、許容される最大の値は \fI/proc/sys/net/core/wmem_max\fP ファイルで設定される。 このオプションの最小値は (2倍した値で) 2048 である。 .TP \fBSO_SNDBUFFORCE\fP (Linux 2.6.14 以降) このソケットオプションを使うと、特権プロセス (\fBCAP_NET_ADMIN\fP を持つプロセス) は \fBSO_SNDBUF\fP と同じことを実行できる。 ただし、上限 \fIwmem_max\fP を上書きすることができる。 .TP \fBSO_TIMESTAMP\fP Enable or disable the receiving of the \fBSO_TIMESTAMP\fP control message. The timestamp control message is sent with level \fBSOL_SOCKET\fP and a \fIcmsg_type\fP of \fBSCM_TIMESTAMP\fP. The \fIcmsg_data\fP field is a \fIstruct timeval\fP indicating the reception time of the last packet passed to the user in this call. See \fBcmsg\fP(3) for details on control messages. .TP \fBSO_TIMESTAMPNS\fP (Linux 2.6.22 以降) .\" commit 92f37fd2ee805aa77925c1e64fd56088b46094fc Enable or disable the receiving of the \fBSO_TIMESTAMPNS\fP control message. The timestamp control message is sent with level \fBSOL_SOCKET\fP and a \fIcmsg_type\fP of \fBSCM_TIMESTAMPNS\fP. The \fIcmsg_data\fP field is a \fIstruct timespec\fP indicating the reception time of the last packet passed to the user in this call. The clock used for the timestamp is \fBCLOCK_REALTIME\fP. See \fBcmsg\fP(3) for details on control messages. .IP A socket cannot mix \fBSO_TIMESTAMP\fP and \fBSO_TIMESTAMPNS\fP: the two modes are mutually exclusive. .TP \fBSO_TYPE\fP ソケットのタイプを整数で取得する (例: \fBSOCK_STREAM\fP)。 このソケットオプションは読み出し専用である。 .TP \fBSO_BUSY_POLL\fP (Linux 3.11 以降) データがなかった際にブロッキング受信での busy polling のおおよその時間をマイクロ秒単位で設定する。 この値を増やすには \fBCAP_NET_ADMIN\fP ケーパビリティが必要である。 このオプションのデフォルト値は \fI/proc/sys/net/core/busy_read\fP で制御できる。 .IP \fI/proc/sys/net/core/busy_poll\fP の値により、 \fBSO_BUSY_POLL\fP がセットされたソケットに対して \fBselect\fP(2) や \fBpoll\fP(2) を行い、報告すべきイベントがない場合に、 \fBselect\fP(2) や \fBpoll\fP(2) が busy polling をどのくらいの時間行うかが決まる。 .IP どちらの場合も、busy polling は、そのソケットが最後にデータを受信したネットワークデバイスがこのオプションに対応している場合のみ行われる。 .IP busy polling により遅延が改善されるはアプリケーションもあるが、 busy polling は CPU 使用率と電力使用量をともに増加させることになるので、使用する際は注意して行うこと。 .SS シグナル (ローカルもしくはリモート側で) 切断された 接続指向 (connection\-oriented) のソケットに対して 書き込みを行うと、その書き込みを行ったプロセスに \fBSIGPIPE\fP が送られ、 \fBEPIPE\fP が返される。 write 呼び出しに \fBMSG_NOSIGNAL\fP フラグを指定していた場合はシグナルは送られない。 .PP \fBFIOSETOWN\fP \fBfcntl\fP(2) や \fBSIOCSPGRP\fP \fBioctl\fP(2) をプロセスまたはプロセスグループに指定しておくと、 I/O イベントが起きたときに \fBSIGIO\fP が送られる。 \fBpoll\fP(2) や \fBselect\fP(2) をシグナルハンドラー内で用いれば、どのソケットでイベントが起こったかを 知ることができる。 (Linux 2.2 における) 別の方法としては、 \fBF_SETSIG\fP \fBfcntl\fP(2) を用いてリアルタイムシグナルを設定するやり方もある。 リアルタイムシグナルのハンドラーは、 \fIsiginfo_t\fP の \fIsi_fd\fP フィールドにファイルディスクリプターが入った状態で呼び出される。 詳細は \fBfcntl\fP(2) を参照のこと。 .PP .\" .SS Ancillary messages 状況によっては (例えば複数のプロセスが一つのソケットにアクセスしているなど)、 \fBSIGIO\fP の原因となった状態は、プロセスがそのシグナルへの対応を行ったときには 消えてしまっているかもしれない。 この場合は、プロセスは再び待つようにすべきである。 Linux は同じシグナルを後で再送するからである。 .SS "/proc インターフェース" core のソケットのネットワーキングパラメーターには、 \fI/proc/sys/net/core/\fP ディレクトリ内のファイルを通してアクセスできる。 .TP \fIrmem_default\fP ソケットの受信バッファーサイズのデフォルト値 (バイト単位)。 .TP \fIrmem_max\fP \fBSO_RCVBUF\fP ソケットオプションを用いてユーザーが設定できる ソケットの受信バッファーサイズの最大値 (バイト単位)。 .TP \fIwmem_default\fP ソケットの送信バッファーサイズのデフォルト値 (バイト単位)。 .TP \fIwmem_max\fP \fBSO_SNDBUF\fP ソケットオプションを用いてユーザーが設定できる ソケットの送信バッファーサイズの最大値 (バイト単位)。 .TP \fImessage_cost\fP と \fImessage_burst\fP トークンバケットフィルターを設定する。 これは外部のネットワークイベントによって引き起こされた 負荷限界の警告メッセージに用いられる。 .TP \fInetdev_max_backlog\fP グローバルな入力キューにおける最大のパケット数。 .TP \fIoptmem_max\fP .\" netdev_fastroute is not documented because it is experimental ソケットあたりの、補助データ (ancillary data) とユーザー制御データ (iovecs のようなもの) との和の最大長。 .SS ioctl 以下に示す操作には \fBioctl\fP(2) を用いてアクセスできる。 .PP .in +4n .EX \fIerror\fP\fB = ioctl(\fP\fIip_socket\fP\fB, \fP\fIioctl_type\fP\fB, \fP\fI&value_result\fP\fB);\fP .EE .in .TP \fBSIOCGSTAMP\fP .\" Return a \fIstruct timeval\fP with the receive timestamp of the last packet passed to the user. This is useful for accurate round trip time measurements. See \fBsetitimer\fP(2) for a description of \fIstruct timeval\fP. This ioctl should be used only if the socket options \fBSO_TIMESTAMP\fP and \fBSO_TIMESTAMPNS\fP are not set on the socket. Otherwise, it returns the timestamp of the last packet that was received while \fBSO_TIMESTAMP\fP and \fBSO_TIMESTAMPNS\fP were not set, or it fails if no such packet has been received, (i.e., \fBioctl\fP(2) returns \-1 with \fIerrno\fP set to \fBENOENT\fP). .TP \fBSIOCSPGRP\fP Set the process or process group that is to receive \fBSIGIO\fP or \fBSIGURG\fP signals when I/O becomes possible or urgent data is available. The argument is a pointer to a \fIpid_t\fP. For further details, see the description of \fBF_SETOWN\fP in \fBfcntl\fP(2). .TP \fBFIOASYNC\fP \fBO_ASYNC\fP フラグを変更し、ソケットの非同期 (asynchronous) I/O モードを 有効/無効にする。非同期 I/O モードでは、 新しい I/O イベントが起きたときに、 \fBSIGIO\fP シグナルや \fBF_SETSIG\fP で設定されたシグナルセットが発行される。 .IP .\" 引数はブール整数のフラグである。 (この操作は \fBfcntl\fP(2) を使って \fBO_ASYNC\fP フラグをセットするのと同じ意味である。) .TP \fBSIOCGPGRP\fP \fBSIGIO\fP や \fBSIGURG\fP を受信したカレントプロセスかプロセスグループを取得する。 ない場合は 0 が返る。 .PP 有効な \fBfcntl\fP(2) 操作: .TP \fBFIOGETOWN\fP \fBSIOCGPGRP\fP \fBioctl\fP(2) と同じ。 .TP \fBFIOSETOWN\fP \fBSIOCSPGRP\fP \fBioctl\fP(2) と同じ。 .SH バージョン \fBSO_BINDTODEVICE\fP は Linux 2.0.30 で導入された。 \fBSO_PASSCRED\fP は Linux 2.2 で登場した。 \fI/proc\fP インターフェースは Linux 2.2 で導入された。 \fBSO_RCVTIMEO\fP と \fBSO_SNDTIMEO\fP は Linux 2.3.41 以降でサポートされている。 それ以前は、タイムアウトはプロトコル固有の固定の設定値で、 読み書きをすることはできなかった。 .SH 注意 Linux は、送受信バッファーの半分を内部のカーネル構造体で用いると仮定している。 したがって、対応する \fI/proc\fP ファイルはネットワーク回線上での大きさの 2 倍になる。 .PP .\" .SH AUTHORS .\" This man page was written by Andi Kleen. Linux では、 \fBSO_REUSEADDR\fP オプションでポートの再利用が許可されるのは、 そのポートに対して \fBbind\fP(2) を前に実行したプログラムとそのポートを再利用 しようとするプログラムの両方で \fBSO_REUSEADDR\fP がセットされた場合のみである。 この動作は (FreeBSD などの) いくつかの実装とは異なる。これらでは、 後でポートを再利用しようとするプログラムで \fBSO_REUSEADDR\fP オプションをセットするだけでよい。 たいていはこの違いは見えない。なぜなら、例えばサーバプログラムは 常にこのオプションをセットするように設計されるからである。 .SH 関連項目 \fBwireshark\fP(1), \fBbpf\fP(2), \fBconnect\fP(2), \fBgetsockopt\fP(2), \fBsetsockopt\fP(2), \fBsocket\fP(2), \fBpcap\fP(3), \fBaddress_families\fP(7), \fBcapabilities\fP(7), \fBddp\fP(7), \fBip\fP(7), \fBipv6\fP(7), \fBpacket\fP(7), \fBtcp\fP(7), \fBudp\fP(7), \fBunix\fP(7), \fBtcpdump\fP(8) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。