getsockopt(p1, p2)
public
Gets a socket option. These are protocol and system specific, see your
local sytem documentation for details. The option is returned as a String with the data being the binary value of the
socket option.
Parameters
- level is an integer, usually one of the SOL_ constants such as
Socket::SOL_SOCKET, or a protocol level.
- optname is an integer, usually one of the SO_ constants, such as
Socket::SO_REUSEADDR.
Examples
Some socket options are integers with boolean values, in this case #getsockopt could be called like
this:
optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
optval = optval.unpack "i"
reuseaddr = optval[0] == 0 ? false : true
Some socket options are integers with numeric values, in this case #getsockopt could be called like
this:
optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL)
ipttl = optval.unpack("i")[0]
Option values may be structs. Decoding them can be complex as it involves
examining your system headers to determine the correct definition. An
example is a +struct linger+, which may be defined in your system headers
as:
struct linger {
int l_onoff;
int l_linger;
};
In this case #getsockopt could
be called like this:
optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER)
onoff, linger = optval.unpack "ii"
Show source
/*
* Document-method: getsockopt
* call-seq: getsockopt(level, optname)
*
* Gets a socket option. These are protocol and system specific, see your
* local sytem documentation for details. The option is returned as
* a String with the data being the binary value of the socket option.
*
* === Parameters
* * +level+ is an integer, usually one of the SOL_ constants such as
* Socket::SOL_SOCKET, or a protocol level.
* * +optname+ is an integer, usually one of the SO_ constants, such
* as Socket::SO_REUSEADDR.
*
* === Examples
*
* Some socket options are integers with boolean values, in this case
* #getsockopt could be called like this:
* optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
* optval = optval.unpack "i"
* reuseaddr = optval[0] == 0 ? false : true
*
* Some socket options are integers with numeric values, in this case
* #getsockopt could be called like this:
* optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL)
* ipttl = optval.unpack("i")[0]
*
* Option values may be structs. Decoding them can be complex as it involves
* examining your system headers to determine the correct definition. An
* example is a +struct linger+, which may be defined in your system headers
* as:
* struct linger {
* int l_onoff;
* int l_linger;
* };
*
* In this case #getsockopt could be called like this:
* optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER)
* onoff, linger = optval.unpack "ii"
*/
static VALUE
bsock_getsockopt(sock, lev, optname)
VALUE sock, lev, optname;
{
int level, option;
socklen_t len;
char *buf;
rb_io_t *fptr;
level = NUM2INT(lev);
option = NUM2INT(optname);
len = 256;
buf = ALLOCA_N(char,len);
GetOpenFile(sock, fptr);
GetOpenFile(sock, fptr);
if (getsockopt(fileno(fptr->f), level, option, buf, &len) < 0)
rb_sys_fail(fptr->path);
return rb_str_new(buf, len);
rb_notimplement();
}