2009-02-01 20:53:02 -05:00
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
VALUE rb_cSockOpt;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
constant_to_sym(int constant, ID (*intern_const)(int))
|
|
|
|
{
|
|
|
|
ID name = intern_const(constant);
|
|
|
|
if (name) {
|
|
|
|
return ID2SYM(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2NUM(constant);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
optname_to_sym(int level, int optname)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
|
|
|
return constant_to_sym(optname, intern_so_optname);
|
|
|
|
case IPPROTO_IP:
|
|
|
|
return constant_to_sym(optname, intern_ip_optname);
|
2009-02-02 00:18:55 -05:00
|
|
|
#ifdef INET6
|
2009-02-01 20:53:02 -05:00
|
|
|
case IPPROTO_IPV6:
|
|
|
|
return constant_to_sym(optname, intern_ipv6_optname);
|
2009-02-01 22:48:31 -05:00
|
|
|
#endif
|
2009-02-01 20:53:02 -05:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
return constant_to_sym(optname, intern_tcp_optname);
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
return constant_to_sym(optname, intern_udp_optname);
|
|
|
|
default:
|
|
|
|
return INT2NUM(optname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:29:12 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Socket::Option.new(family, level, optname, data) => sockopt
|
|
|
|
*
|
|
|
|
* Returns a new Socket::Option object.
|
|
|
|
*
|
|
|
|
* sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
|
|
|
|
* p sockopt #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
|
|
|
|
*
|
|
|
|
*/
|
2009-02-01 20:53:02 -05:00
|
|
|
static VALUE
|
2009-02-08 10:12:27 -05:00
|
|
|
sockopt_initialize(VALUE self, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE data)
|
2009-02-01 20:53:02 -05:00
|
|
|
{
|
2009-02-09 05:52:55 -05:00
|
|
|
int family = family_arg(vfamily);
|
2009-02-09 08:56:43 -05:00
|
|
|
int level = level_arg(family, vlevel);
|
|
|
|
int optname = optname_arg(family, level, voptname);
|
2009-02-01 20:53:02 -05:00
|
|
|
StringValue(data);
|
2009-02-08 10:12:27 -05:00
|
|
|
rb_ivar_set(self, rb_intern("family"), INT2NUM(family));
|
2009-02-01 20:53:02 -05:00
|
|
|
rb_ivar_set(self, rb_intern("level"), INT2NUM(level));
|
2009-02-09 05:52:55 -05:00
|
|
|
rb_ivar_set(self, rb_intern("optname"), INT2NUM(optname));
|
2009-02-01 20:53:02 -05:00
|
|
|
rb_ivar_set(self, rb_intern("data"), data);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-02-08 10:12:27 -05:00
|
|
|
sockopt_new(int family, int level, int optname, VALUE data)
|
2009-02-01 20:53:02 -05:00
|
|
|
{
|
|
|
|
NEWOBJ(obj, struct RObject);
|
|
|
|
OBJSETUP(obj, rb_cSockOpt, T_OBJECT);
|
|
|
|
StringValue(data);
|
2009-02-08 10:12:27 -05:00
|
|
|
sockopt_initialize((VALUE)obj, INT2NUM(family), INT2NUM(level), INT2NUM(optname), data);
|
2009-02-01 20:53:02 -05:00
|
|
|
return (VALUE)obj;
|
|
|
|
}
|
|
|
|
|
2009-02-08 10:12:27 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.family => integer
|
|
|
|
*
|
|
|
|
* returns the socket family as an integer.
|
|
|
|
*
|
|
|
|
* p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).family
|
2009-02-09 07:52:11 -05:00
|
|
|
* #=> 10
|
2009-02-08 10:12:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-10 06:59:04 -05:00
|
|
|
sockopt_family_m(VALUE self)
|
2009-02-08 10:12:27 -05:00
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("family"));
|
|
|
|
}
|
|
|
|
|
2009-02-01 20:53:02 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.level => integer
|
|
|
|
*
|
|
|
|
* returns the socket level as an integer.
|
|
|
|
*
|
2009-02-08 10:12:27 -05:00
|
|
|
* p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).level
|
2009-02-09 07:52:11 -05:00
|
|
|
* #=> 41
|
2009-02-01 20:53:02 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-10 06:59:04 -05:00
|
|
|
sockopt_level_m(VALUE self)
|
2009-02-01 20:53:02 -05:00
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("level"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.optname => integer
|
|
|
|
*
|
|
|
|
* returns the socket option name as an integer.
|
|
|
|
*
|
2009-02-08 10:12:27 -05:00
|
|
|
* p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).optname
|
2009-02-09 07:52:11 -05:00
|
|
|
* #=> 2
|
2009-02-01 20:53:02 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-10 06:59:04 -05:00
|
|
|
sockopt_optname_m(VALUE self)
|
2009-02-01 20:53:02 -05:00
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("optname"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.data => string
|
|
|
|
*
|
|
|
|
* returns the socket option data as a string.
|
|
|
|
*
|
2009-02-09 07:52:11 -05:00
|
|
|
* p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).data
|
|
|
|
* #=> "\x01\x00\x00\x00"
|
2009-02-01 20:53:02 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
sockopt_data(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_attr_get(self, rb_intern("data"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-08 10:12:27 -05:00
|
|
|
* Socket::Option.int(family, level, optname, integer) => sockopt
|
2009-02-01 20:53:02 -05:00
|
|
|
*
|
|
|
|
* Creates a new Socket::Option object which contains an int as data.
|
|
|
|
*
|
|
|
|
* The size and endian is dependent on the host.
|
|
|
|
*
|
2009-02-09 06:04:51 -05:00
|
|
|
* p Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
|
2009-02-09 19:18:53 -05:00
|
|
|
* #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
|
2009-02-01 20:53:02 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-08 10:12:27 -05:00
|
|
|
sockopt_s_int(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vint)
|
2009-02-01 20:53:02 -05:00
|
|
|
{
|
2009-02-08 10:12:27 -05:00
|
|
|
int family = family_arg(vfamily);
|
2009-02-09 08:56:43 -05:00
|
|
|
int level = level_arg(family, vlevel);
|
|
|
|
int optname = optname_arg(family, level, voptname);
|
2009-02-01 20:53:02 -05:00
|
|
|
int i = NUM2INT(vint);
|
2009-02-08 10:12:27 -05:00
|
|
|
return sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.int => integer
|
|
|
|
*
|
|
|
|
* Returns the data in _sockopt_ as an int.
|
|
|
|
*
|
|
|
|
* The size and endian is dependent on the host.
|
|
|
|
*
|
2009-02-09 06:04:51 -05:00
|
|
|
* sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
|
2009-02-01 20:53:02 -05:00
|
|
|
* p sockopt.int => 1
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
sockopt_int(VALUE self)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE data = sockopt_data(self);
|
|
|
|
StringValue(data);
|
|
|
|
if (RSTRING_LEN(data) != sizeof(int))
|
|
|
|
rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
|
|
|
|
(int)sizeof(int), (long)RSTRING_LEN(data));
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
|
|
|
|
return INT2NUM(i);
|
|
|
|
}
|
|
|
|
|
2009-02-10 07:09:57 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Socket::Option.bool(family, level, optname, bool) => sockopt
|
|
|
|
*
|
|
|
|
* Creates a new Socket::Option object which contains boolean as data.
|
|
|
|
* Actually 0 or 1 as int is used.
|
|
|
|
*
|
|
|
|
* p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
|
|
|
|
* #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
|
|
|
|
*
|
|
|
|
* p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
|
|
|
|
* #=> #<Socket::Option: AF_INET SOCKET KEEPALIVE 0>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
sockopt_s_bool(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vbool)
|
|
|
|
{
|
|
|
|
int family = family_arg(vfamily);
|
|
|
|
int level = level_arg(family, vlevel);
|
|
|
|
int optname = optname_arg(family, level, voptname);
|
|
|
|
int i = RTEST(vbool) ? 1 : 0;
|
|
|
|
return sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.bool => true or false
|
|
|
|
*
|
|
|
|
* Returns the data in _sockopt_ as an boolean value.
|
|
|
|
*
|
|
|
|
* sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
|
|
|
|
* p sockopt.bool => true
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
sockopt_bool(VALUE self)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE data = sockopt_data(self);
|
|
|
|
StringValue(data);
|
|
|
|
if (RSTRING_LEN(data) != sizeof(int))
|
|
|
|
rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
|
|
|
|
(int)sizeof(int), (long)RSTRING_LEN(data));
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
|
|
|
|
return i == 0 ? Qfalse : Qtrue;
|
|
|
|
}
|
|
|
|
|
2009-02-01 20:53:02 -05:00
|
|
|
static int
|
|
|
|
inspect_int(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(int)) {
|
|
|
|
int i;
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
|
|
|
|
rb_str_catf(ret, " %d", i);
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
inspect_uint(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(int)) {
|
|
|
|
unsigned int i;
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(unsigned int));
|
|
|
|
rb_str_catf(ret, " %u", i);
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SOL_SOCKET) && defined(SO_LINGER) /* POSIX */
|
|
|
|
static int
|
|
|
|
inspect_linger(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct linger)) {
|
|
|
|
struct linger s;
|
|
|
|
memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
|
|
|
|
rb_str_catf(ret, " onoff:%d linger:%d", s.l_onoff, s.l_linger);
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SOL_SOCKET) && defined(SO_TYPE) /* POSIX */
|
|
|
|
static int
|
|
|
|
inspect_socktype(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(int)) {
|
|
|
|
int i;
|
|
|
|
ID id;
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
|
|
|
|
id = intern_socktype(i);
|
|
|
|
if (id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " %d", i);
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
inspect_timeval(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct linger)) {
|
|
|
|
struct timeval s;
|
|
|
|
memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
|
|
|
|
rb_str_catf(ret, " %ld.%06ldsec", (long)s.tv_sec, (long)s.tv_usec);
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-08 09:23:16 -05:00
|
|
|
#if defined(SOL_SOCKET) && defined(SO_PEERCRED) /* GNU/Linux */
|
|
|
|
static int
|
|
|
|
inspect_peercred(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct ucred)) {
|
|
|
|
struct ucred cred;
|
|
|
|
memcpy(&cred, RSTRING_PTR(data), sizeof(struct ucred));
|
2009-02-08 12:04:40 -05:00
|
|
|
rb_str_catf(ret, " pid=%u euid=%u egid=%u", cred.pid, cred.uid, cred.gid);
|
2009-02-08 09:23:16 -05:00
|
|
|
rb_str_cat2(ret, " (ucred)");
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-08 09:23:16 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-08 09:23:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-08 10:49:35 -05:00
|
|
|
#if defined(LOCAL_PEERCRED) /* FreeBSD, MacOS X */
|
2009-02-08 10:37:55 -05:00
|
|
|
static int
|
|
|
|
inspect_local_peercred(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct xucred)) {
|
|
|
|
struct xucred cred;
|
|
|
|
memcpy(&cred, RSTRING_PTR(data), sizeof(struct xucred));
|
2009-02-08 19:06:41 -05:00
|
|
|
if (cred.cr_version != XUCRED_VERSION)
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-08 10:37:55 -05:00
|
|
|
rb_str_catf(ret, " version=%u", cred.cr_version);
|
2009-02-08 10:42:17 -05:00
|
|
|
rb_str_catf(ret, " euid=%u", cred.cr_uid);
|
2009-02-08 10:37:55 -05:00
|
|
|
if (cred.cr_ngroups) {
|
|
|
|
int i;
|
2009-02-09 22:57:23 -05:00
|
|
|
const char *sep = " groups=";
|
2009-02-08 10:37:55 -05:00
|
|
|
for (i = 0; i < cred.cr_ngroups; i++) {
|
|
|
|
rb_str_catf(ret, "%s%u", sep, cred.cr_groups[i]);
|
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_str_cat2(ret, " (xucred)");
|
2009-02-09 06:04:51 -05:00
|
|
|
return 1;
|
2009-02-08 10:37:55 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-02-09 06:04:51 -05:00
|
|
|
return 0;
|
2009-02-08 10:37:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-10 23:29:12 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.inspect => string
|
|
|
|
*
|
|
|
|
* Returns a string which shows sockopt in human-readable form.
|
|
|
|
*
|
|
|
|
* p Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")).inspect
|
|
|
|
* #=> "#<Socket::Option: INET SOCKET KEEPALIVE 1>"
|
|
|
|
*
|
|
|
|
*/
|
2009-02-01 20:53:02 -05:00
|
|
|
static VALUE
|
|
|
|
sockopt_inspect(VALUE self)
|
|
|
|
{
|
2009-02-10 06:59:04 -05:00
|
|
|
int family = NUM2INT(sockopt_family_m(self));
|
|
|
|
int level = NUM2INT(sockopt_level_m(self));
|
|
|
|
int optname = NUM2INT(sockopt_optname_m(self));
|
2009-02-01 20:53:02 -05:00
|
|
|
VALUE data = sockopt_data(self);
|
|
|
|
VALUE v, ret;
|
2009-02-08 10:37:55 -05:00
|
|
|
ID family_id, level_id, optname_id;
|
2009-02-09 06:04:51 -05:00
|
|
|
int inspected;
|
2009-02-01 20:53:02 -05:00
|
|
|
|
|
|
|
StringValue(data);
|
|
|
|
|
2009-02-09 11:23:36 -05:00
|
|
|
ret = rb_sprintf("#<%s:", rb_obj_classname(self));
|
2009-02-01 20:53:02 -05:00
|
|
|
|
2009-02-09 11:03:08 -05:00
|
|
|
family_id = intern_family_noprefix(family);
|
2009-02-08 10:12:27 -05:00
|
|
|
if (family_id)
|
2009-02-09 11:23:36 -05:00
|
|
|
rb_str_catf(ret, " %s", rb_id2name(family_id));
|
2009-02-08 10:12:27 -05:00
|
|
|
else
|
2009-02-09 11:23:36 -05:00
|
|
|
rb_str_catf(ret, " family:%d", family);
|
2009-02-08 10:12:27 -05:00
|
|
|
|
2009-02-09 11:23:36 -05:00
|
|
|
if (level == SOL_SOCKET) {
|
|
|
|
rb_str_cat2(ret, " SOCKET");
|
|
|
|
|
|
|
|
optname_id = intern_so_optname(optname);
|
|
|
|
if (optname_id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(optname_id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " optname:%d", optname);
|
|
|
|
}
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
else if (family == AF_UNIX) {
|
2009-02-08 10:37:55 -05:00
|
|
|
rb_str_catf(ret, " level:%d", level);
|
2009-02-01 20:53:02 -05:00
|
|
|
|
2009-02-08 10:37:55 -05:00
|
|
|
optname_id = intern_local_optname(optname);
|
|
|
|
if (optname_id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(optname_id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " optname:%d", optname);
|
|
|
|
}
|
2009-02-09 11:23:36 -05:00
|
|
|
#endif
|
|
|
|
else if (IS_IP_FAMILY(family)) {
|
2009-02-09 09:05:37 -05:00
|
|
|
level_id = intern_iplevel(level);
|
2009-02-08 10:37:55 -05:00
|
|
|
if (level_id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(level_id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " level:%d", level);
|
|
|
|
|
|
|
|
v = optname_to_sym(level, optname);
|
|
|
|
if (SYMBOL_P(v))
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(SYM2ID(v)));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " optname:%d", optname);
|
|
|
|
}
|
2009-02-09 11:23:36 -05:00
|
|
|
else {
|
|
|
|
rb_str_catf(ret, " level:%d", level);
|
|
|
|
rb_str_catf(ret, " optname:%d", optname);
|
|
|
|
}
|
2009-02-08 10:37:55 -05:00
|
|
|
|
2009-02-09 06:04:51 -05:00
|
|
|
inspected = 0;
|
|
|
|
|
|
|
|
if (level == SOL_SOCKET)
|
|
|
|
family = AF_UNSPEC;
|
|
|
|
switch (family) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
|
|
|
switch (optname) {
|
|
|
|
# if defined(SO_DEBUG) /* POSIX */
|
|
|
|
case SO_DEBUG: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_ERROR) /* POSIX */
|
|
|
|
case SO_ERROR: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_TYPE) /* POSIX */
|
|
|
|
case SO_TYPE: inspected = inspect_socktype(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_ACCEPTCONN) /* POSIX */
|
|
|
|
case SO_ACCEPTCONN: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_BROADCAST) /* POSIX */
|
|
|
|
case SO_BROADCAST: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_REUSEADDR) /* POSIX */
|
|
|
|
case SO_REUSEADDR: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_KEEPALIVE) /* POSIX */
|
|
|
|
case SO_KEEPALIVE: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_OOBINLINE) /* POSIX */
|
|
|
|
case SO_OOBINLINE: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_SNDBUF) /* POSIX */
|
|
|
|
case SO_SNDBUF: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_RCVBUF) /* POSIX */
|
|
|
|
case SO_RCVBUF: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_DONTROUTE) /* POSIX */
|
|
|
|
case SO_DONTROUTE: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_RCVLOWAT) /* POSIX */
|
|
|
|
case SO_RCVLOWAT: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_SNDLOWAT) /* POSIX */
|
|
|
|
case SO_SNDLOWAT: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_LINGER) /* POSIX */
|
|
|
|
case SO_LINGER: inspected = inspect_linger(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_RCVTIMEO) /* POSIX */
|
|
|
|
case SO_RCVTIMEO: inspected = inspect_timeval(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_SNDTIMEO) /* POSIX */
|
|
|
|
case SO_SNDTIMEO: inspected = inspect_timeval(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SO_PEERCRED) /* GNU/Linux */
|
|
|
|
case SO_PEERCRED: inspected = inspect_peercred(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-02-09 06:04:51 -05:00
|
|
|
case AF_INET:
|
2009-02-09 11:23:36 -05:00
|
|
|
#ifdef INET6
|
2009-02-09 06:04:51 -05:00
|
|
|
case AF_INET6:
|
2009-02-09 11:23:36 -05:00
|
|
|
#endif
|
2009-02-09 06:04:51 -05:00
|
|
|
switch (level) {
|
|
|
|
# if defined(IPPROTO_IPV6)
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
switch (optname) {
|
|
|
|
/* IPV6_JOIN_GROUP ipv6_mreq, IPV6_LEAVE_GROUP ipv6_mreq */
|
|
|
|
# if defined(IPV6_MULTICAST_HOPS) /* POSIX */
|
|
|
|
case IPV6_MULTICAST_HOPS: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(IPV6_MULTICAST_IF) /* POSIX */
|
|
|
|
case IPV6_MULTICAST_IF: inspected = inspect_uint(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(IPV6_MULTICAST_LOOP) /* POSIX */
|
|
|
|
case IPV6_MULTICAST_LOOP: inspected = inspect_uint(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(IPV6_UNICAST_HOPS) /* POSIX */
|
|
|
|
case IPV6_UNICAST_HOPS: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(IPV6_V6ONLY) /* POSIX */
|
|
|
|
case IPV6_V6ONLY: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-01 20:53:02 -05:00
|
|
|
# endif
|
2009-02-09 06:04:51 -05:00
|
|
|
|
|
|
|
# if defined(IPPROTO_TCP)
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
switch (optname) {
|
|
|
|
# if defined(TCP_NODELAY) /* POSIX */
|
|
|
|
case TCP_NODELAY: inspected = inspect_int(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-01 20:53:02 -05:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-02-09 11:23:36 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-02-09 06:04:51 -05:00
|
|
|
case AF_UNIX:
|
|
|
|
switch (level) {
|
|
|
|
case 0:
|
|
|
|
switch (optname) {
|
|
|
|
# if defined(LOCAL_PEERCRED)
|
|
|
|
case LOCAL_PEERCRED: inspected = inspect_local_peercred(level, optname, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
break;
|
2009-02-09 11:23:36 -05:00
|
|
|
#endif
|
2009-02-09 06:04:51 -05:00
|
|
|
}
|
2009-02-01 20:53:02 -05:00
|
|
|
|
2009-02-09 06:04:51 -05:00
|
|
|
if (!inspected) {
|
2009-02-01 20:53:02 -05:00
|
|
|
data = rb_str_dump(data);
|
|
|
|
rb_str_catf(ret, " %s", StringValueCStr(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_str_cat2(ret, ">");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:29:12 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* sockopt.unpack(template) => array
|
|
|
|
*
|
|
|
|
* Calls String#unpack on sockopt.data.
|
|
|
|
*
|
|
|
|
* sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
|
|
|
|
* p sockopt.unpack("i") #=> [1]
|
|
|
|
* p sockopt.data.unpack("i") #=> [1]
|
|
|
|
*/
|
2009-02-01 20:53:02 -05:00
|
|
|
static VALUE
|
|
|
|
sockopt_unpack(VALUE self, VALUE template)
|
|
|
|
{
|
|
|
|
return rb_funcall(sockopt_data(self), rb_intern("unpack"), 1, template);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_sockopt(void)
|
|
|
|
{
|
|
|
|
rb_cSockOpt = rb_define_class_under(rb_cSocket, "Option", rb_cObject);
|
2009-02-08 10:12:27 -05:00
|
|
|
rb_define_method(rb_cSockOpt, "initialize", sockopt_initialize, 4);
|
2009-02-10 06:59:04 -05:00
|
|
|
rb_define_method(rb_cSockOpt, "family", sockopt_family_m, 0);
|
|
|
|
rb_define_method(rb_cSockOpt, "level", sockopt_level_m, 0);
|
|
|
|
rb_define_method(rb_cSockOpt, "optname", sockopt_optname_m, 0);
|
2009-02-01 20:53:02 -05:00
|
|
|
rb_define_method(rb_cSockOpt, "data", sockopt_data, 0);
|
|
|
|
rb_define_method(rb_cSockOpt, "inspect", sockopt_inspect, 0);
|
|
|
|
|
2009-02-08 10:12:27 -05:00
|
|
|
rb_define_singleton_method(rb_cSockOpt, "int", sockopt_s_int, 4);
|
2009-02-01 20:53:02 -05:00
|
|
|
rb_define_method(rb_cSockOpt, "int", sockopt_int, 0);
|
|
|
|
|
2009-02-10 07:09:57 -05:00
|
|
|
rb_define_singleton_method(rb_cSockOpt, "bool", sockopt_s_bool, 4);
|
|
|
|
rb_define_method(rb_cSockOpt, "bool", sockopt_bool, 0);
|
|
|
|
|
2009-02-01 20:53:02 -05:00
|
|
|
rb_define_method(rb_cSockOpt, "unpack", sockopt_unpack, 1);
|
2009-02-02 09:44:28 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cSockOpt, "to_s", sockopt_data, 0); /* compatibility for ruby before 1.9.2 */
|
2009-02-01 20:53:02 -05:00
|
|
|
}
|
|
|
|
|