* ext/socket/socket.c (optname_arg): defined.

(bsock_setsockopt): use optname_arg.
  (bsock_getsockopt): ditto.

* ext/socket/mkconstants.rb: generate so_optname_to_int,
  ip_optname_to_int, ipv6_optname_to_int, tcp_optname_to_int and
  udp_optname_to_int.
  more constants.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-01-01 17:40:41 +00:00
Родитель ff7e0f7f61
Коммит aa0ae0d3b6
4 изменённых файлов: 87 добавлений и 5 удалений

Просмотреть файл

@ -1,3 +1,14 @@
Fri Jan 2 02:39:08 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/socket.c (optname_arg): defined.
(bsock_setsockopt): use optname_arg.
(bsock_getsockopt): ditto.
* ext/socket/mkconstants.rb: generate so_optname_to_int,
ip_optname_to_int, ipv6_optname_to_int, tcp_optname_to_int and
udp_optname_to_int.
more constants.
Fri Jan 2 02:08:36 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/socket.c (constant_arg): extracted from family_arg.

Просмотреть файл

@ -157,6 +157,36 @@ level_to_int(char *str, int len, int *valp)
<%= gen_name_to_int("str", "len", "valp", /\A(SOL_SOCKET\z|IPPROTO_)/) %>
}
static int
so_optname_to_int(char *str, int len, int *valp)
{
<%= gen_name_to_int("str", "len", "valp", /\ASO_/) %>
}
static int
ip_optname_to_int(char *str, int len, int *valp)
{
<%= gen_name_to_int("str", "len", "valp", /\AIP_/) %>
}
static int
ipv6_optname_to_int(char *str, int len, int *valp)
{
<%= gen_name_to_int("str", "len", "valp", /\AIPV6_/) %>
}
static int
tcp_optname_to_int(char *str, int len, int *valp)
{
<%= gen_name_to_int("str", "len", "valp", /\ATCP_/) %>
}
static int
udp_optname_to_int(char *str, int len, int *valp)
{
<%= gen_name_to_int("str", "len", "valp", /\AUDP_/) %>
}
static char *
family_to_str(int val)
{
@ -335,6 +365,13 @@ IP_RECVOPTS
IP_RECVRETOPTS
IP_RECVDSTADDR
IP_RETOPTS
IP_MINTTL
IP_DONTFRAG
IP_SENDSRCADDR
IP_ONESBCAST
IP_RECVTTL
IP_RECVIF
IP_PORTRANGE
IP_MULTICAST_IF
IP_MULTICAST_TTL
IP_MULTICAST_LOOP
@ -390,6 +427,21 @@ IPX_TYPE
TCP_NODELAY
TCP_MAXSEG
TCP_CORK
TCP_DEFER_ACCEPT
TCP_INFO
TCP_KEEPCNT
TCP_KEEPIDLE
TCP_KEEPINTVL
TCP_LINGER2
TCP_MD5SIG
TCP_NOOPT
TCP_NOPUSH
TCP_QUICKACK
TCP_SYNCNT
TCP_WINDOW_CLAMP
UDP_CORK
EAI_ADDRFAMILY
EAI_AGAIN

Просмотреть файл

@ -258,7 +258,7 @@ constant_arg(VALUE arg, int (*str_to_int)(char*, int, int*), char *errmsg)
rb_check_safe_obj(arg);
ptr = RSTRING_PTR(arg);
if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1)
rb_raise(rb_eSocket, "%s %s", errmsg, ptr);
rb_raise(rb_eSocket, "%s: %s", errmsg, ptr);
}
else {
ret = NUM2INT(arg);
@ -287,6 +287,25 @@ level_arg(VALUE level)
return constant_arg(level, level_to_int, "unknown protocol level");
}
static int
optname_arg(int level, VALUE optname)
{
switch (level) {
case SOL_SOCKET:
return constant_arg(optname, so_optname_to_int, "unknown socket level option name");
case IPPROTO_IP:
return constant_arg(optname, ip_optname_to_int, "unknown IP level option name");
case IPPROTO_IPV6:
return constant_arg(optname, ipv6_optname_to_int, "unknown IPv6 level option name");
case IPPROTO_TCP:
return constant_arg(optname, tcp_optname_to_int, "unknown TCP level option name");
case IPPROTO_UDP:
return constant_arg(optname, udp_optname_to_int, "unknown UDP level option name");
default:
return NUM2INT(optname);
}
}
static VALUE
init_sock(VALUE sock, int fd)
{
@ -433,7 +452,7 @@ bsock_setsockopt(VALUE sock, VALUE lev, VALUE optname, VALUE val)
rb_secure(2);
level = level_arg(lev);
option = NUM2INT(optname);
option = optname_arg(level, optname);
switch (TYPE(val)) {
case T_FIXNUM:
@ -513,7 +532,7 @@ bsock_getsockopt(VALUE sock, VALUE lev, VALUE optname)
rb_io_t *fptr;
level = level_arg(lev);
option = NUM2INT(optname);
option = optname_arg(level, optname);
len = 256;
buf = ALLOCA_N(char,len);

Просмотреть файл

@ -16,9 +16,9 @@ class TestBasicSocket < Test::Unit::TestCase
inet_stream do |s|
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE)
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
n = s.getsockopt("SOL_SOCKET", Socket::SO_TYPE)
n = s.getsockopt("SOL_SOCKET", "SO_TYPE")
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
n = s.getsockopt(:SOL_SOCKET, Socket::SO_TYPE)
n = s.getsockopt(:SOL_SOCKET, :SO_TYPE)
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
assert_equal([0].pack("i"), n)