類別 Socket::Option
Socket::Option
代表 BasicSocket#getsockopt
和 BasicSocket#setsockopt
使用的 socket 選項。socket 選項包含 socket family
、通訊協定 level
、選項名稱 optname
和選項值 data
。
公開類別方法
建立新的 Socket::Option
物件,其中包含布林值作為資料。實際上使用 0 或 1 作為 int。
require 'socket' 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 = rsock_family_arg(vfamily); int level = rsock_level_arg(family, vlevel); int optname = rsock_optname_arg(family, level, voptname); int i = RTEST(vbool) ? 1 : 0; return rsock_sockopt_new(family, level, optname, pack_var(i)); }
建立新的 Socket::Option
物件,其中包含一個位元組作為資料。
p Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1) #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
static VALUE sockopt_s_byte(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vint) { int family = rsock_family_arg(vfamily); int level = rsock_level_arg(family, vlevel); int optname = rsock_optname_arg(family, level, voptname); return rsock_sockopt_new(family, level, optname, sockopt_pack_byte(vint)); }
建立新的 Socket::Option
物件,其中包含一個 int 作為資料。
大小和位元序取決於平台。
p Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1) #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
static VALUE sockopt_s_int(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vint) { int family = rsock_family_arg(vfamily); int level = rsock_level_arg(family, vlevel); int optname = rsock_optname_arg(family, level, voptname); return rsock_sockopt_new(family, level, optname, sockopt_pack_int(vint)); }
為 IP_MULTICAST_LOOP 建立新的 Socket::Option
物件。
大小取決於平台。
sockopt = Socket::Option.int(:INET, :IPPROTO_IP, :IP_MULTICAST_LOOP, 1) p sockopt.int => 1 p Socket::Option.ipv4_multicast_loop(10) #=> #<Socket::Option: INET IP MULTICAST_LOOP 10>
static VALUE sockopt_s_ipv4_multicast_loop(VALUE klass, VALUE value) { #if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP) VALUE o = XCAT(sockopt_pack_,TYPE_IP_MULTICAST_LOOP)(value); return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_LOOP, o); #else # error IPPROTO_IP or IP_MULTICAST_LOOP is not implemented #endif }
建立一個新的 Socket::Option
物件,用於 IP_MULTICAST_TTL。
大小取決於平台。
p Socket::Option.ipv4_multicast_ttl(10) #=> #<Socket::Option: INET IP MULTICAST_TTL 10>
static VALUE sockopt_s_ipv4_multicast_ttl(VALUE klass, VALUE value) { #if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL) VALUE o = XCAT(sockopt_pack_,TYPE_IP_MULTICAST_TTL)(value); return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_TTL, o); #else # error IPPROTO_IP or IP_MULTICAST_TTL is not implemented #endif }
建立一個新的 Socket::Option
物件,用於 SOL_SOCKET/SO_LINGER。
onoff 應為整數或布林值。
secs 應為秒數。
p Socket::Option.linger(true, 10) #=> #<Socket::Option: UNSPEC SOCKET LINGER on 10sec>
static VALUE sockopt_s_linger(VALUE klass, VALUE vonoff, VALUE vsecs) { VALUE tmp; struct linger l; memset(&l, 0, sizeof(l)); if (!NIL_P(tmp = rb_check_to_integer(vonoff, "to_int"))) l.l_onoff = NUM2INT(tmp); else l.l_onoff = RTEST(vonoff) ? 1 : 0; l.l_linger = NUM2INT(vsecs); return rsock_sockopt_new(AF_UNSPEC, SOL_SOCKET, SO_LINGER, pack_var(l)); }
傳回一個新的 Socket::Option
物件。
sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")) p sockopt #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
static VALUE sockopt_initialize(VALUE self, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE data) { int family = rsock_family_arg(vfamily); int level = rsock_level_arg(family, vlevel); int optname = rsock_optname_arg(family, level, voptname); StringValue(data); rb_ivar_set(self, rb_intern("family"), INT2NUM(family)); rb_ivar_set(self, rb_intern("level"), INT2NUM(level)); rb_ivar_set(self, rb_intern("optname"), INT2NUM(optname)); rb_ivar_set(self, rb_intern("data"), data); return self; }
公開實例方法
傳回 sockopt 中的資料,作為布林值。
sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1) p sockopt.bool => true
static VALUE sockopt_bool(VALUE self) { int i; long len; VALUE data = sockopt_data(self); StringValue(data); len = RSTRING_LEN(data); if (len == 1) { return *RSTRING_PTR(data) == 0 ? Qfalse : Qtrue; } check_size(len, sizeof(int)); memcpy((char*)&i, RSTRING_PTR(data), len); return i == 0 ? Qfalse : Qtrue; }
傳回 sockopt 中的資料,作為位元組。
sockopt = Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1) p sockopt.byte => 1
static VALUE sockopt_byte(VALUE self) { VALUE data = sockopt_data(self); StringValue(data); check_size(RSTRING_LEN(data), sizeof(char)); return CHR2FIX(*RSTRING_PTR(data)); }
傳回 socket 選項資料,作為字串。
p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).data #=> "\x01\x00\x00\x00"
static VALUE sockopt_data(VALUE self) { VALUE v = rb_attr_get(self, rb_intern("data")); StringValue(v); return v; }
傳回 socket 家族,作為整數。
p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).family #=> 10
static VALUE sockopt_family_m(VALUE self) { return rb_attr_get(self, rb_intern("family")); }
傳回字串,以人類可讀的形式顯示 sockopt。
p Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")).inspect #=> "#<Socket::Option: INET SOCKET KEEPALIVE 1>"
static VALUE sockopt_inspect(VALUE self) { int family = NUM2INT(sockopt_family_m(self)); int level = NUM2INT(sockopt_level_m(self)); int optname = NUM2INT(sockopt_optname_m(self)); VALUE data = sockopt_data(self); VALUE v, ret; ID family_id, level_id, optname_id; int inspected; StringValue(data); ret = rb_sprintf("#<%s:", rb_obj_classname(self)); family_id = rsock_intern_family_noprefix(family); if (family_id) rb_str_catf(ret, " %s", rb_id2name(family_id)); else rb_str_catf(ret, " family:%d", family); if (level == SOL_SOCKET) { rb_str_cat2(ret, " SOCKET"); optname_id = rsock_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_TYPE_STRUCT_SOCKADDR_UN else if (family == AF_UNIX) { rb_str_catf(ret, " level:%d", level); optname_id = rsock_intern_local_optname(optname); if (optname_id) rb_str_catf(ret, " %s", rb_id2name(optname_id)); else rb_str_catf(ret, " optname:%d", optname); } #endif else if (IS_IP_FAMILY(family)) { level_id = rsock_intern_iplevel(level); 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, " %"PRIsVALUE, rb_sym2str(v)); else rb_str_catf(ret, " optname:%d", optname); } else { rb_str_catf(ret, " level:%d", level); rb_str_catf(ret, " optname:%d", optname); } 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_errno(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_as_interval(level, optname, data, ret); break; # endif # if defined(SO_SNDTIMEO) /* POSIX */ case SO_SNDTIMEO: inspected = inspect_timeval_as_interval(level, optname, data, ret); break; # endif # if defined(SO_PEERCRED) /* GNU/Linux, OpenBSD */ case SO_PEERCRED: inspected = inspect_peercred(level, optname, data, ret); break; # endif } break; } break; case AF_INET: #ifdef INET6 case AF_INET6: #endif switch (level) { # if defined(IPPROTO_IP) case IPPROTO_IP: switch (optname) { # if defined(IP_MULTICAST_IF) && defined(HAVE_TYPE_STRUCT_IP_MREQN) /* 4.4BSD, GNU/Linux */ case IP_MULTICAST_IF: inspected = inspect_ipv4_multicast_if(level, optname, data, ret); break; # endif # if defined(IP_ADD_MEMBERSHIP) /* 4.4BSD, GNU/Linux */ case IP_ADD_MEMBERSHIP: inspected = inspect_ipv4_add_drop_membership(level, optname, data, ret); break; # endif # if defined(IP_DROP_MEMBERSHIP) /* 4.4BSD, GNU/Linux */ case IP_DROP_MEMBERSHIP: inspected = inspect_ipv4_add_drop_membership(level, optname, data, ret); break; # endif # if defined(IP_MULTICAST_LOOP) /* 4.4BSD, GNU/Linux */ case IP_MULTICAST_LOOP: inspected = inspect_ipv4_multicast_loop(level, optname, data, ret); break; # endif # if defined(IP_MULTICAST_TTL) /* 4.4BSD, GNU/Linux */ case IP_MULTICAST_TTL: inspected = inspect_ipv4_multicast_ttl(level, optname, data, ret); break; # endif } break; # endif # if defined(IPPROTO_IPV6) case IPPROTO_IPV6: switch (optname) { # 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_ipv6_multicast_if(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_JOIN_GROUP) /* POSIX */ case IPV6_JOIN_GROUP: inspected = inspect_ipv6_mreq(level, optname, data, ret); break; # endif # if defined(IPV6_LEAVE_GROUP) /* POSIX */ case IPV6_LEAVE_GROUP: inspected = inspect_ipv6_mreq(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; # endif # 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 # if defined(TCP_INFO) && defined(HAVE_TYPE_STRUCT_TCP_INFO) /* Linux, FreeBSD */ case TCP_INFO: inspected = inspect_tcp_info(level, optname, data, ret); break; # endif } break; # endif } break; #ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN 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; } break; #endif } if (!inspected) { rb_str_cat2(ret, " "); rb_str_append(ret, rb_str_dump(data)); } rb_str_cat2(ret, ">"); return ret; }
傳回 sockopt 中的資料,作為整數。
大小和位元序取決於平台。
sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1) p sockopt.int => 1
static VALUE sockopt_int(VALUE self) { int i; VALUE data = sockopt_data(self); StringValue(data); check_size(RSTRING_LEN(data), sizeof(int)); memcpy((char*)&i, RSTRING_PTR(data), sizeof(int)); return INT2NUM(i); }
傳回 sockopt 中的 ipv4_multicast_loop
資料,作為整數。
sockopt = Socket::Option.ipv4_multicast_loop(10) p sockopt.ipv4_multicast_loop => 10
static VALUE sockopt_ipv4_multicast_loop(VALUE self) { int family = NUM2INT(sockopt_family_m(self)); int level = sockopt_level(self); int optname = sockopt_optname(self); #if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP) if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_LOOP) { return XCAT(sockopt_,TYPE_IP_MULTICAST_LOOP)(self); } #endif rb_raise(rb_eTypeError, "ipv4_multicast_loop socket option expected"); UNREACHABLE_RETURN(Qnil); }
傳回 sockopt 中的 ipv4_multicast_ttl
資料,作為整數。
sockopt = Socket::Option.ipv4_multicast_ttl(10) p sockopt.ipv4_multicast_ttl => 10
static VALUE sockopt_ipv4_multicast_ttl(VALUE self) { int family = NUM2INT(sockopt_family_m(self)); int level = sockopt_level(self); int optname = sockopt_optname(self); #if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL) if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_TTL) { return XCAT(sockopt_,TYPE_IP_MULTICAST_TTL)(self); } #endif rb_raise(rb_eTypeError, "ipv4_multicast_ttl socket option expected"); UNREACHABLE_RETURN(Qnil); }
傳回 socket 層級,作為整數。
p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).level #=> 41
static VALUE sockopt_level_m(VALUE self) { return INT2NUM(sockopt_level(self)); }
傳回 sockopt 中的 linger 資料,作為布林值和整數的配對。
sockopt = Socket::Option.linger(true, 10) p sockopt.linger => [true, 10]
static VALUE sockopt_linger(VALUE self) { int level = sockopt_level(self); int optname = sockopt_optname(self); VALUE data = sockopt_data(self); struct linger l; VALUE vonoff, vsecs; if (level != SOL_SOCKET || optname != SO_LINGER) rb_raise(rb_eTypeError, "linger socket option expected"); check_size(RSTRING_LEN(data), sizeof(struct linger)); memcpy((char*)&l, RSTRING_PTR(data), sizeof(struct linger)); switch (l.l_onoff) { case 0: vonoff = Qfalse; break; case 1: vonoff = Qtrue; break; default: vonoff = INT2NUM(l.l_onoff); break; } vsecs = INT2NUM(l.l_linger); return rb_assoc_new(vonoff, vsecs); }
傳回 socket 選項名稱為整數。
p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).optname #=> 2
static VALUE sockopt_optname_m(VALUE self) { return INT2NUM(sockopt_optname(self)); }
傳回 socket 選項資料,作為字串。
p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).data #=> "\x01\x00\x00\x00"
在 sockopt.data 上呼叫 String#unpack
。
sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")) p sockopt.unpack("i") #=> [1] p sockopt.data.unpack("i") #=> [1]
static VALUE sockopt_unpack(VALUE self, VALUE template) { return rb_funcall(sockopt_data(self), rb_intern("unpack"), 1, template); }