getsockoptのヘルプ・マニュアル
日本語 英語
getsockopt --help
man getsockopt
GETSOCKOPT(2) Linux Programmer’s Manual GETSOCKOPT(2)
名前
getsockopt, setsockopt - ソケットのオプションの設定と取得を行なう
書式
#include /* 「注意」参照 */
#include
int getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
説明
getsockopt() と setsockopt() は、ファイルディスクリプタ socket で参照さ
れるソケットに関連するオプションの操作を行う。オプションは複数のプロ ト
コル層(level)に存在するかもしれないが、これらは常に最上位のソケット層へ
と設定される。
ソケット・オプションを操作する際には、オプションの層 (level) とオプショ
ンの名前を指定しなければならない。ソケット API 層でオプションを操作する
為には、 level を SOL_SOCKET に指定する。他の全ての層でオプションを操作
す る為には、与えられたオプションの制御主体となるプロトコルのプロトコル
番号を指定する。例えば、オプションが TCP プロトコルで解釈されるべきこと
を 指示するには、 level に TCP のプロトコル番号を指定しなければならない
。 getprotoent(3) を参照すること。
optval と optlen 引き数は setsockopt() のオプションの値にアクセスするた
め に用いられる。 getsockopt() では要求したオプションの値を返すためのバ
ッファーを指定する。 getsockopt() では optlen は値と結果両用の引き数 で
、 最初に optval の指しているバッファーのサイズを与え、実際に返される値
のサイズに書き換えられる。もしオプション値を与えず、返されもしない場 合
には optval は NULL でも良い。
optname および指定されたオプションは解釈されずに、適切なプロトコル・モ
ジュールに渡されて解釈される。インクルード・ファイル に
は 後述のソケット層のオプションの定義が含まれている。他のプロトコル層で
は名前や形式は異なる。マニュアルのセクション 4 の適切なエントリを参照す
ること。
ほ と ん ど のソケット層のオプションは optval に int 引き数を利用する。
setsockopt() で、二値(boolean)オプションを有効(enable)にするにはゼロ 以
外を指定し、無効(disable)にするにはゼロを指定する。
利用可能なソケットオプションの説明に関しては、 socket(7) と適切なプロト
コルの man ページを参照のこと。
返り値
成功した場合にはゼロを返す。エラー時には -1 が返され、 errno が適切に設
定される。
エラー
EBADF 引き数 sockfd が有効なディスクリプタでない。
EFAULT optval で指定されたアドレスがプロセスのアドレス空間の有効な部
分ではない。 getsockopt() の場合、 optlen がプロセスのアド レ
ス空間の有効な部分でない場合にもこのエラーが返される。
EINVAL setsockopt() で option が不正である。 optval に無効な値が指定
された場合にも、このエラーが発生する可能性がある (例 え ば 、
ip(7) に説明がある IP_ADD_MEMBERSHIP オプションなど)。
ENOPROTOOPT
指定された層(level)にはこのオプションは存在しない。
ENOTSOCK 引き数 sockfd はファイルであり、ソケットでない。
準拠
SVr4, 4.4BSD ( こ れ ら の システム・コールは 4.2BSD で最初に現れた),
POSIX.1-2001.
注意
POSIX.1-2001 では のインクルードは必須とされてお ら ず 、
Linux ではこのヘッダファイルは必要ではない。しかし、歴史的には、いくつ
かの実装 (BSD 系) でこのヘッダファイルが必要であり、移植性が必要なア プ
リケーションではこのファイルをインクルードするのが賢明であろう。
getsockopt() と setsockopt() の optlen 引き数は実際は int [*] である (
そして 4.x BSD と libc4 と libc5 はそうなっている)。 POSIX では紆余曲折
を経て現在の socklen_t になっており、 glibc でも socklen_t を使っている
。 accept(2) も参照のこと。
バグ
ソケットのオプションのいくつかはシステムのより低い層で処理されるべき で
ある。
関連項目
ioctl(2), socket(2), getprotoent(3), protocols(5), socket(7), tcp(7),
unix(7)
Linux 2008-12-03 GETSOCKOPT(2)
GETSOCKOPT(2) Linux Programmer’s Manual GETSOCKOPT(2)
NAME
getsockopt, setsockopt - get and set options on sockets
SYNOPSIS
#include /* See NOTES */
#include
int getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
DESCRIPTION
getsockopt() and setsockopt() manipulate options for the socket
referred to by the file descriptor sockfd. Options may exist at multi-
ple protocol levels; they are always present at the uppermost socket
level.
When manipulating socket options, the level at which the option resides
and the name of the option must be specified. To manipulate options at
the sockets API level, level is specified as SOL_SOCKET. To manipulate
options at any other level the protocol number of the appropriate pro-
tocol controlling the option is supplied. For example, to indicate
that an option is to be interpreted by the TCP protocol, level should
be set to the protocol number of TCP; see getprotoent(3).
The arguments optval and optlen are used to access option values for
setsockopt(). For getsockopt() they identify a buffer in which the
value for the requested option(s) are to be returned. For getsock-
opt(), optlen is a value-result argument, initially containing the size
of the buffer pointed to by optval, and modified on return to indicate
the actual size of the value returned. If no option value is to be
supplied or returned, optval may be NULL.
Optname and any specified options are passed uninterpreted to the
appropriate protocol module for interpretation. The include file
contains definitions for socket level options, described
below. Options at other protocol levels vary in format and name; con-
sult the appropriate entries in section 4 of the manual.
Most socket-level options utilize an int argument for optval. For set-
sockopt(), the argument should be non-zero to enable a boolean option,
or zero if the option is to be disabled.
For a description of the available socket options see socket(7) and the
appropriate protocol man pages.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
ERRORS
EBADF The argument sockfd is not a valid descriptor.
EFAULT The address pointed to by optval is not in a valid part of
the process address space. For getsockopt(), this error may
also be returned if optlen is not in a valid part of the pro-
cess address space.
EINVAL optlen invalid in setsockopt(). In some cases this error can
also occur for an invalid value in optval (e.g., for the
IP_ADD_MEMBERSHIP option described in ip(7)).
ENOPROTOOPT
The option is unknown at the level indicated.
ENOTSOCK The argument sockfd is a file, not a socket.
CONFORMING TO
SVr4, 4.4BSD (these system calls first appeared in 4.2BSD),
POSIX.1-2001.
NOTES
POSIX.1-2001 does not require the inclusion of , and this
header file is not required on Linux. However, some historical (BSD)
implementations required this header file, and portable applications
are probably wise to include it.
The optlen argument of getsockopt() and setsockopt() is in reality an
int [*] (and this is what 4.x BSD and libc4 and libc5 have). Some
POSIX confusion resulted in the present socklen_t, also used by glibc.
See also accept(2).
BUGS
Several of the socket options should be handled at lower levels of the
system.
SEE ALSO
ioctl(2), socket(2), getprotoent(3), protocols(5), socket(7), tcp(7),
unix(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-12-03 GETSOCKOPT(2)